Shader compilado como valor; binarios gerados pelos backends
"compile o shader [pilha]" vira um reporter: devolve o shader compilado,
um bloco-handle com referencias contadas que se guarda numa variavel
("mude [ondas] para (compile o shader ...)", "use o shader (ondas) no
objeto"); sai o registro de shaders por nome.
A 3d continua sem saber de backend: compila os blocos para GLSL e para o
interpretador do software e pede o binario a cada renderizador pelo
contrato (Tb3dRenderer.compile, versao 3). A vk gera SPIR-V (a libshaderc
e a interface do fragment voltam para ela); a gl liga o programa na thread
dela e devolve o binario do driver (glGetProgramBinary), que carrega com
glProgramBinary e, se recusado, recompila o GLSL. O monitor mostra
"shader compilado (vulkan: 2940 bytes, opengl: 10953 bytes)".
Notacao do forum: a pilha de um encaixe vem embaixo tambem quando o bloco
com o encaixe e um reporter dentro do bloco da linha.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
16009eca8e
commit
1c3103ec55
16 files changed
+1186
-608
No files matched your search
@@ -23,9 +23,12 @@ static struct {
|
||||
pthread_mutex_t mu;
|
||||
pthread_cond_t cv;
|
||||
bool started, ready, failed, quit;
|
||||
/* um pedido por vez */
|
||||
/* um pedido por vez: desenhar um quadro, ou compilar o binario de um shader */
|
||||
const Tb3dFrame* job;
|
||||
uint8_t* out;
|
||||
const char* glsl; /* != NULL: compilar */
|
||||
void** bin;
|
||||
size_t* bin_size;
|
||||
bool busy, ok;
|
||||
char err[256], device[128];
|
||||
} G = { .mu = PTHREAD_MUTEX_INITIALIZER, .cv = PTHREAD_COND_INITIALIZER };
|
||||
@@ -41,9 +44,9 @@ static size_t s_rows_cap;
|
||||
typedef struct { GLuint vao, vbo, ebo; int icount; } GpuMesh;
|
||||
static GpuMesh s_mesh[32]; /* pelo id da malha */
|
||||
|
||||
/* Programas dos shaders feitos com blocos, pelo id (recompila se a versao muda) */
|
||||
typedef struct { GLuint prog; int version; GLint mvp, model, nrm, color, light, ambient, time; } GpuShader;
|
||||
static GpuShader s_shd[65];
|
||||
/* Programas dos shaders feitos com blocos: cache pelo id (id % 64) */
|
||||
typedef struct { GLuint prog; int id; GLint mvp, model, nrm, color, light, ambient, time; } GpuShader;
|
||||
static GpuShader s_shd[64];
|
||||
|
||||
static void fail(const char* fmt, const char* a) { snprintf(G.err, sizeof(G.err), fmt, a); }
|
||||
|
||||
@@ -115,11 +118,12 @@ static GLuint shader(GLenum kind, const char* src) {
|
||||
return s;
|
||||
}
|
||||
|
||||
static GLuint link(GLuint vs, GLuint fs) {
|
||||
static GLuint link_hint(GLuint vs, GLuint fs, bool retrievable) {
|
||||
if (!vs || !fs) { if (vs) glDeleteShader(vs); if (fs) glDeleteShader(fs); return 0; }
|
||||
GLuint p = glCreateProgram();
|
||||
glAttachShader(p, vs);
|
||||
glAttachShader(p, fs);
|
||||
if (retrievable) glProgramParameteri(p, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE);
|
||||
glLinkProgram(p);
|
||||
glDeleteShader(vs);
|
||||
glDeleteShader(fs);
|
||||
@@ -129,21 +133,65 @@ static GLuint link(GLuint vs, GLuint fs) {
|
||||
return p;
|
||||
}
|
||||
|
||||
/* O programa de um shader feito com blocos (compila na 1a vez e a cada versao) */
|
||||
/* 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);
|
||||
free(fs);
|
||||
return p;
|
||||
}
|
||||
|
||||
/* 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) {
|
||||
*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);
|
||||
if (!p) return false;
|
||||
GLint len = 0;
|
||||
glGetProgramiv(p, GL_PROGRAM_BINARY_LENGTH, &len);
|
||||
uint8_t* buf = len > 0 ? malloc((size_t)len + 4) : NULL;
|
||||
GLenum fmt = 0;
|
||||
if (buf) glGetProgramBinary(p, len, &len, &fmt, buf + 4);
|
||||
glDeleteProgram(p);
|
||||
if (!buf || len <= 0) { free(buf); fail("%s", "o driver não devolveu o binário"); return false; }
|
||||
uint32_t f = (uint32_t)fmt;
|
||||
memcpy(buf, &f, 4);
|
||||
*data = buf;
|
||||
*size = (size_t)len + 4;
|
||||
return true;
|
||||
}
|
||||
|
||||
static GLuint program_from_binary(const void* data, size_t size) {
|
||||
if (size <= 4) return 0;
|
||||
uint32_t f;
|
||||
memcpy(&f, data, 4);
|
||||
GLuint p = glCreateProgram();
|
||||
glProgramBinary(p, (GLenum)f, (const uint8_t*)data + 4, (GLsizei)(size - 4));
|
||||
GLint ok = 0;
|
||||
glGetProgramiv(p, GL_LINK_STATUS, &ok);
|
||||
if (!ok) { glDeleteProgram(p); return 0; }
|
||||
return p;
|
||||
}
|
||||
|
||||
/* O programa de um shader feito com blocos (na 1a vez que o id aparece):
|
||||
do binario que veio no shader, ou do GLSL */
|
||||
static GpuShader* gpu_shader(const Tb3dShader* sh) {
|
||||
if (sh->id <= 0 || sh->id >= 65) { fail("%s", "id de shader inválido"); return NULL; }
|
||||
GpuShader* g = &s_shd[sh->id];
|
||||
if (g->prog && g->version == sh->version) return g;
|
||||
GpuShader* g = &s_shd[(unsigned)sh->id % 64u];
|
||||
if (g->prog && g->id == sh->id) return g;
|
||||
if (g->prog) glDeleteProgram(g->prog);
|
||||
memset(g, 0, sizeof(*g));
|
||||
size_t n = strlen(FS_HEAD) + strlen(sh->glsl) + strlen(FS_MAIN) + 1;
|
||||
char* fs = malloc(n);
|
||||
if (!fs) return NULL;
|
||||
snprintf(fs, n, "%s%s%s", FS_HEAD, sh->glsl, FS_MAIN);
|
||||
g->prog = link(shader(GL_VERTEX_SHADER, VS_SHADER), shader(GL_FRAGMENT_SHADER, fs));
|
||||
free(fs);
|
||||
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) return NULL;
|
||||
g->version = sh->version;
|
||||
g->id = sh->id;
|
||||
g->mvp = glGetUniformLocation(g->prog, "u_mvp");
|
||||
g->model = glGetUniformLocation(g->prog, "u_model");
|
||||
g->nrm = glGetUniformLocation(g->prog, "u_nrm");
|
||||
@@ -293,7 +341,7 @@ static void cleanup(void) {
|
||||
glDeleteBuffers(1, &s_mesh[i].ebo);
|
||||
}
|
||||
memset(s_mesh, 0, sizeof(s_mesh));
|
||||
for (int i = 0; i < 65; i++) if (s_shd[i].prog) glDeleteProgram(s_shd[i].prog);
|
||||
for (int i = 0; i < 64; i++) if (s_shd[i].prog) glDeleteProgram(s_shd[i].prog);
|
||||
memset(s_shd, 0, sizeof(s_shd));
|
||||
if (s_prog) glDeleteProgram(s_prog);
|
||||
if (s_fbo) { glDeleteFramebuffers(1, &s_fbo); glDeleteRenderbuffers(1, &s_color); glDeleteRenderbuffers(1, &s_depth); }
|
||||
@@ -319,8 +367,9 @@ static void* gl_thread(void* arg) {
|
||||
if (G.quit) break;
|
||||
const Tb3dFrame* f = G.job;
|
||||
uint8_t* out = G.out;
|
||||
const char* glsl = G.glsl;
|
||||
pthread_mutex_unlock(&G.mu);
|
||||
bool r = draw(f, out);
|
||||
bool r = glsl ? binary_compile(glsl, G.bin, G.bin_size) : draw(f, out);
|
||||
pthread_mutex_lock(&G.mu);
|
||||
G.ok = r;
|
||||
G.busy = false;
|
||||
@@ -347,7 +396,7 @@ static bool gl_available(void) {
|
||||
static bool gl_render(const Tb3dFrame* f, uint8_t* rgba) {
|
||||
if (!gl_available()) return false;
|
||||
pthread_mutex_lock(&G.mu);
|
||||
G.job = f; G.out = rgba; G.busy = true;
|
||||
G.job = f; G.out = rgba; G.glsl = NULL; G.busy = true;
|
||||
pthread_cond_broadcast(&G.cv);
|
||||
while (G.busy) pthread_cond_wait(&G.cv, &G.mu);
|
||||
bool ok = G.ok;
|
||||
@@ -359,8 +408,22 @@ static const char* gl_device(void) { return G.device[0] ? G.device : "?"; }
|
||||
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) {
|
||||
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;
|
||||
pthread_cond_broadcast(&G.cv);
|
||||
while (G.busy) pthread_cond_wait(&G.cv, &G.mu);
|
||||
bool ok = G.ok;
|
||||
G.glsl = NULL;
|
||||
if (!ok) snprintf(err, cap, "%s", G.err);
|
||||
pthread_mutex_unlock(&G.mu);
|
||||
return ok;
|
||||
}
|
||||
|
||||
static const Tb3dRenderer s_renderer = {
|
||||
TB_3D_RENDERER_VERSION, "opengl", gl_available, gl_render, gl_device, gl_error, gl_shaders };
|
||||
TB_3D_RENDERER_VERSION, "opengl", gl_available, gl_render, gl_device, gl_error, gl_shaders, gl_compile };
|
||||
|
||||
static bool lib_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
(void)rt;
|
||||
|
||||
Reference in new issue
Block a user