Shaders feitos com blocos: tinyblocks-shader e o compilador da 3d
"compile o shader [nome] com [pilha]" tem um encaixe de pilha: a pilha nao roda, a 3d le o codigo dela e o compila (d3_shader.c) em nos com tipo (numero, cor, condicao), de onde saem o GLSL da funcao tb_shade e um interpretador para o renderizador em software (ponto e normal com perspectiva, pixel a pixel). "use o shader [nome] no objeto" e "GLSL do shader" completam. tinyblocks-shader: o vocabulario (cor do pixel =, cor do pixel, cor do objeto, luz, tempo, x/y/z do ponto e da normal, cor r g b, cor [nome], misture, canais, brilho). Valem tambem as contas, comparacoes, o se/senao e as variaveis comuns (viram locais do shader). Contrato v2: Tb3dDraw.shader, Tb3dFrame.time, Tb3dRenderer.shaders. GL: um programa por shader. Vulkan: o fragment compilado em execucao pela libshaderc (dlopen) e push constants de 160 bytes; sem ela, a 3d usa o OpenGL ou o software nesses quadros. test-3d: xadrez e degrade iguais nos tres renderizadores (diferenca media 0,002), sem avisos da camada de validacao na NVIDIA, Intel e llvmpipe; erros claros para bloco que nao cabe e para comparar cores. examples/jogo-3d/shaders: ondas no chao e listras na bola. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a3bae660fb
commit
16009eca8e
27 files changed
+4376
-88
No files matched your search
+60
-1
@@ -35,6 +35,25 @@ static TinyBlocks_Block* run(const char* type, const char* const* args, int n) {
|
||||
}
|
||||
#define RUN(type, ...) run(type, (const char* const[]){ __VA_ARGS__ }, (int)(sizeof((const char* const[]){ __VA_ARGS__ }) / sizeof(char*)))
|
||||
|
||||
/* "compile o shader [nome] com [pilha]" com o codigo (JSON) da pilha */
|
||||
static bool compile_shader(const char* name, const char* json) {
|
||||
TinyBlocks_Block* x = tb_block_create(rt, tb_type_block_find(rt, "3D.ShaderCompile"));
|
||||
TinyBlocks_Value nv = tb_value_text(name);
|
||||
tb_block_set_default(x, 0, nv);
|
||||
tb_value_free(&nv);
|
||||
TinyBlocks_JsonValue* j = tb_json_parse(json, NULL, 0);
|
||||
TinyBlocks_Value jv = tb_value_json(j);
|
||||
tb_block_set_default(x, 1, jv);
|
||||
tb_value_free(&jv);
|
||||
tb_json_release(j);
|
||||
tinyblocks_clear_error(rt);
|
||||
tb_block_eval(x, rt);
|
||||
return !tinyblocks_last_error(rt)[0];
|
||||
}
|
||||
|
||||
#define BLK(t, ...) "{\"type\":\"" t "\",\"inputs\":[" __VA_ARGS__ "]}"
|
||||
#define BLK0(t) "{\"type\":\"" t "\",\"inputs\":[]}"
|
||||
|
||||
static TinyBlocks_Image* render_with(const char* who) {
|
||||
RUN("3D.Use", who);
|
||||
TinyBlocks_Block* b = RUN("3D.Image", "320", "240");
|
||||
@@ -120,6 +139,40 @@ int main(void) {
|
||||
}
|
||||
|
||||
const char* gpu[] = { "opengl", "vulkan" };
|
||||
for (int pass = 0; pass < 2; pass++) {
|
||||
if (pass == 1) {
|
||||
/* -- shaders feitos com blocos: xadrez no chao, degrade na bola */
|
||||
const char* xadrez = "[{\"type\":\"Control.IfElse\",\"inputs\":["
|
||||
BLK("Operators.Eq", BLK("Math.Mod", BLK("Math.Add", BLK("Number.Floor", BLK0("Shader.X")) ","
|
||||
BLK("Number.Floor", BLK0("Shader.Z"))) ",2") ",0")
|
||||
"],\"mouths\":[[" BLK("Shader.Pixel", BLK("Math.Mul", BLK("Shader.Color", "\"branco\"") "," BLK0("Shader.Light")))
|
||||
"],[" BLK("Shader.Pixel", BLK("Math.Mul", BLK("Shader.Color", "\"#333333\"") "," BLK0("Shader.Light"))) "]]}]";
|
||||
CHECK(compile_shader("xadrez", xadrez), "compila o xadrez (se/senão, resto, arredonde, cor, luz)");
|
||||
const char* degrade = "[" BLK("Variables.Set", "\"t\"," BLK("Math.Div", BLK("Math.Add", BLK0("Shader.NY") ",1") ",2"))
|
||||
"," BLK("Shader.Pixel", BLK("Math.Mul", BLK("Shader.Mix", BLK("Shader.Color", "\"azul\"") ","
|
||||
BLK("Shader.Color", "\"branco\"") "," BLK("Variables.Get", "\"t\"")) "," BLK0("Shader.Light"))) "]";
|
||||
CHECK(compile_shader("degrade", degrade), "compila o degradê (variável local, misture)");
|
||||
RUN("3D.ShaderUse", "xadrez", "chão");
|
||||
RUN("3D.ShaderUse", "degrade", "bola");
|
||||
CHECK(!tinyblocks_last_error(rt)[0], "use o shader no objeto");
|
||||
TinyBlocks_Block* g = RUN("3D.ShaderGlsl", "xadrez");
|
||||
CHECK(g->outputs[0].kind == TB_VALUE_TEXT && strstr(g->outputs[0].as.text, "vec3 tb_shade(") &&
|
||||
strstr(g->outputs[0].as.text, "mod("), "GLSL gerado");
|
||||
CHECK(!compile_shader("ruim", "[" BLK("Shader.Pixel", BLK0("Input.Timer")) "]") &&
|
||||
strstr(tinyblocks_last_error(rt), "não pode ser usado num shader"), "bloco que não cabe num shader: erro claro");
|
||||
CHECK(!compile_shader("ruim", "[{\"type\":\"Control.If\",\"inputs\":[" BLK("Operators.Lt", BLK0("Shader.PixelColor") ",1")
|
||||
"],\"mouths\":[[]]}]") && strstr(tinyblocks_last_error(rt), "comparar números"), "comparar cor: erro claro");
|
||||
tinyblocks_clear_error(rt);
|
||||
tb_image_release(sw);
|
||||
sw = render_with("software");
|
||||
CHECK(sw != NULL, "cena com shaders pelo software");
|
||||
if (sw) {
|
||||
uint8_t r, g2, b, a, r2, g3, b2;
|
||||
tb_image_get_pixel(sw, 40, 225, &r, &g2, &b, &a);
|
||||
tb_image_get_pixel(sw, 75, 225, &r2, &g3, &b2, &a);
|
||||
CHECK(abs(r - g2) < 4 && abs(r2 - g3) < 4 && abs(r - r2) > 60, "o chão virou xadrez (cinza claro e escuro)");
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 2; i++) {
|
||||
const char* lib = i ? "tinyblocks-vk" : "tinyblocks-gl";
|
||||
if (!tb_rt_find_library(rt, lib)) { printf("-- %s não compilado neste teste\n", gpu[i]); continue; }
|
||||
@@ -131,10 +184,16 @@ int main(void) {
|
||||
if (img && sw) compare(sw, img, &mean, &bad);
|
||||
printf(" %s x software: diferença média %.3f, pixels muito diferentes %.2f%%\n", gpu[i], mean, bad * 100);
|
||||
char m[96];
|
||||
snprintf(m, sizeof(m), "%s desenha quase igual ao software", gpu[i]);
|
||||
snprintf(m, sizeof(m), pass ? "%s com shaders desenha quase igual ao software" : "%s desenha quase igual ao software", gpu[i]);
|
||||
CHECK(img && mean < 4.0 && bad < 0.02, m);
|
||||
if (pass) {
|
||||
char path[64];
|
||||
snprintf(path, sizeof(path), "/tmp/tb_3d_%s_shader.png", gpu[i]);
|
||||
tb_image_save(img, path);
|
||||
}
|
||||
tb_image_release(img);
|
||||
}
|
||||
}
|
||||
tb_image_release(sw);
|
||||
tinyblocks_deinit(&rt);
|
||||
if (fails) printf("\n%d FALHA(S)\n", fails);
|
||||
|
||||
@@ -1439,7 +1439,7 @@ int main(void) {
|
||||
free(t);
|
||||
t = mcp("add_scratchblocks", "{\"code\":\"when flag clicked\\n"
|
||||
"say (join [e=] (código de ((seno de (x)) * (2))))\\n"
|
||||
"say (join [p=] (código da pilha []))\\n"
|
||||
"say (join [p=] (código da pilha))\\n"
|
||||
"say (join [x=] (x))\\n\",\"x\":40,\"y\":40}", &err, NULL);
|
||||
CHECK(!err, "encaixes: cria o script");
|
||||
int stk = -1;
|
||||
|
||||
Reference in new issue
Block a user