3D: shader de vértice (o "compile o shader" descobre o estágio)

- novo bloco "mude a posição do vértice para x y z" (Shader.Vertex): a
  pilha que o usa vira um shader de VÉRTICE; a que usa "cor do pixel" vira
  de FRAGMENTO; misturar os dois dá erro claro
- o GLSL gerado é tb_vert (vértice) ou tb_shade (fragmento); o software
  roda o mesmo programa vértice a vértice
- OpenGL e Vulkan montam o programa do estágio certo e juntam com o
  estágio padrão (posição intacta / cor do objeto com luz)
- Tb3dShader ganha "stage"; Tb3dRenderer.compile leva o estágio;
  TB_3D_RENDERER_VERSION -> 5
- docs regeradas (blocos, referência da API, libraries, pendências) e
  exemplo examples/jogo-3d/onda.sb (esfera que pulsa)
- test-3d: o shader de vértice dá igual nos 3 renderizadores

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
mguinhosandClaude Opus 5 committed 2026-10-29 00:58:21 -03:00
1 parent 1e171411f7
commit d5c16a5237
14 files changed
+667 -85

No files matched your search

+39 -18
View File
@@ -27,6 +27,7 @@ static struct {
const Tb3dFrame* job;
uint8_t* out;
const char* glsl; /* != NULL: compilar */
int stage; /* estagio do shader a compilar */
void** bin;
size_t* bin_size;
bool busy, ok;
@@ -73,7 +74,7 @@ static const char* FS =
"out vec4 o_color;\n"
"void main() { o_color = vec4(min(v_col, vec3(1.0)), 1.0); }\n";
static const char* VS_SHADER =
static const char* VS_VERT_HEAD =
"#version 330 core\n"
"layout(location = 0) in vec3 a_pos;\n"
"layout(location = 1) in vec3 a_nrm;\n"
@@ -81,17 +82,29 @@ static const char* VS_SHADER =
"uniform mat4 u_mvp;\n"
"uniform mat4 u_model;\n"
"uniform mat3 u_nrm;\n"
"uniform vec4 u_color;\n"
"uniform vec3 u_light;\n"
"uniform float u_ambient;\n"
"uniform float u_time;\n"
"out vec3 v_pos;\n"
"out vec3 v_nrm;\n"
"out vec3 v_local;\n"
"out vec2 v_uv;\n"
"out vec2 v_uv;\n";
static const char* VS_VERT_MAIN =
"void main() {\n"
" gl_Position = u_mvp * vec4(a_pos, 1.0);\n"
" v_pos = (u_model * vec4(a_pos, 1.0)).xyz;\n"
" vec3 n = normalize(u_nrm * a_nrm);\n"
" float light = u_ambient + (1.0 - u_ambient) * max(dot(n, u_light), 0.0);\n"
" vec3 P = tb_vert(a_pos, a_nrm, a_pos, a_uv, u_color.rgb, light, u_time);\n"
" gl_Position = u_mvp * vec4(P, 1.0);\n"
" v_pos = (u_model * vec4(P, 1.0)).xyz;\n"
" v_nrm = u_nrm * a_nrm;\n"
" v_local = a_pos;\n"
" v_local = P;\n"
" v_uv = a_uv;\n"
"}\n";
static const char* DEFAULT_VERT =
"vec3 tb_vert(vec3 P, vec3 N, vec3 L, vec2 UV, vec3 base, float light, float time) { return P; }\n";
static const char* DEFAULT_FRAG =
"vec3 tb_shade(vec3 P, vec3 N, vec3 L, vec2 UV, vec3 base, float light, float time) { return base * light; }\n";
static const char* FS_HEAD =
"#version 330 core\n"
"in vec3 v_pos;\n"
@@ -141,13 +154,21 @@ static GLuint link_hint(GLuint vs, GLuint fs, bool retrievable) {
return p;
}
/* O programa do shader feito com blocos, compilado do GLSL */
static GLuint program_from_glsl(const char* glsl, bool retrievable) {
size_t n = strlen(FS_HEAD) + strlen(glsl) + strlen(FS_MAIN) + 1;
char* fs = malloc(n);
if (!fs) return 0;
snprintf(fs, n, "%s%s%s", FS_HEAD, glsl, FS_MAIN);
GLuint p = link_hint(shader(GL_VERTEX_SHADER, VS_SHADER), shader(GL_FRAGMENT_SHADER, fs), retrievable);
/* O programa do shader feito com blocos, compilado do GLSL. O estagio diz
qual funcao o GLSL traz (tb_vert ou tb_shade); o outro estagio usa o
padrao (posicao intacta / cor do objeto com luz). */
static GLuint program_from_glsl(const char* glsl, int stage, bool retrievable) {
bool vert = stage == TB_3D_STAGE_VERTEX;
const char* vb = vert ? glsl : DEFAULT_VERT;
const char* fb = vert ? DEFAULT_FRAG : glsl;
size_t nv = strlen(VS_VERT_HEAD) + strlen(vb) + strlen(VS_VERT_MAIN) + 1;
size_t nf = strlen(FS_HEAD) + strlen(fb) + strlen(FS_MAIN) + 1;
char* vs = malloc(nv), *fs = malloc(nf);
if (!vs || !fs) { free(vs); free(fs); return 0; }
snprintf(vs, nv, "%s%s%s", VS_VERT_HEAD, vb, VS_VERT_MAIN);
snprintf(fs, nf, "%s%s%s", FS_HEAD, fb, FS_MAIN);
GLuint p = link_hint(shader(GL_VERTEX_SHADER, vs), shader(GL_FRAGMENT_SHADER, fs), retrievable);
free(vs);
free(fs);
return p;
}
@@ -155,12 +176,12 @@ static GLuint program_from_glsl(const char* glsl, bool retrievable) {
/* O binario do OpenGL e o programa ja ligado, como o driver o devolve
(glGetProgramBinary): 4 bytes com o formato e os bytes do driver. So
serve para a mesma placa e driver; se o driver o recusar, recompila. */
static bool binary_compile(const char* glsl, void** data, size_t* size) {
static bool binary_compile(const char* glsl, int stage, void** data, size_t* size) {
*data = NULL; *size = 0;
GLint nfmt = 0;
glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &nfmt);
if (nfmt < 1) { fail("%s", "o driver não devolve binários de programa"); return false; }
GLuint p = program_from_glsl(glsl, true);
GLuint p = program_from_glsl(glsl, stage, true);
if (!p) return false;
GLint len = 0;
glGetProgramiv(p, GL_PROGRAM_BINARY_LENGTH, &len);
@@ -197,7 +218,7 @@ static GpuShader* gpu_shader(const Tb3dShader* sh) {
memset(g, 0, sizeof(*g));
for (int i = 0; i < sh->bin_n && !g->prog; i++)
if (!strcmp(sh->bin[i].renderer, "opengl")) g->prog = program_from_binary(sh->bin[i].data, sh->bin[i].size);
if (!g->prog) g->prog = program_from_glsl(sh->glsl, false);
if (!g->prog) g->prog = program_from_glsl(sh->glsl, sh->stage, false);
if (!g->prog) return NULL;
g->id = sh->id;
g->mvp = glGetUniformLocation(g->prog, "u_mvp");
@@ -384,7 +405,7 @@ static void* gl_thread(void* arg) {
uint8_t* out = G.out;
const char* glsl = G.glsl;
pthread_mutex_unlock(&G.mu);
bool r = glsl ? binary_compile(glsl, G.bin, G.bin_size) : draw(f, out);
bool r = glsl ? binary_compile(glsl, G.stage, G.bin, G.bin_size) : draw(f, out);
pthread_mutex_lock(&G.mu);
G.ok = r;
G.busy = false;
@@ -424,10 +445,10 @@ static const char* gl_error(void) { return G.err; }
static bool gl_shaders(void) { return true; }
/* Tb3dRenderer.compile: pede o binario a thread do OpenGL */
static bool gl_compile(const char* glsl, void** data, size_t* size, char* err, size_t cap) {
static bool gl_compile(const char* glsl, int stage, void** data, size_t* size, char* err, size_t cap) {
if (!gl_available()) { snprintf(err, cap, "%s", G.err); return false; }
pthread_mutex_lock(&G.mu);
G.glsl = glsl; G.bin = data; G.bin_size = size; G.busy = true;
G.glsl = glsl; G.stage = stage; G.bin = data; G.bin_size = size; G.busy = true;
pthread_cond_broadcast(&G.cv);
while (G.busy) pthread_cond_wait(&G.cv, &G.mu);
bool ok = G.ok;