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
@@ -19,12 +19,11 @@
|
||||
typedef struct { VkBuffer buf; VkDeviceMemory mem; void* map; VkDeviceSize size; } Buf;
|
||||
typedef struct { Buf vb, ib; int icount; } GpuMesh;
|
||||
|
||||
/* Pipeline de um shader feito com blocos, pelo id (refeito se a versao muda) */
|
||||
typedef struct { VkPipeline pipe; int version; } GpuShader;
|
||||
/* Pipeline de um shader feito com blocos: cache pelo id (id % 64) */
|
||||
typedef struct { VkPipeline pipe; int id; } GpuShader;
|
||||
|
||||
/* -- libshaderc (GLSL -> SPIR-V em execucao), carregada por dlopen --------
|
||||
So as funcoes da API C usadas aqui (a API e estavel; nao precisa do
|
||||
header). Sem a biblioteca, a vk so nao desenha shaders feitos com blocos. */
|
||||
/* -- libshaderc (GLSL -> SPIR-V), carregada por dlopen -------------------- */
|
||||
/* So as funcoes da API C usadas aqui (estavel; nao precisa do header) */
|
||||
typedef void* (*ShcInit)(void);
|
||||
typedef void (*ShcRelease)(void*);
|
||||
typedef void* (*ShcCompile)(void*, const char*, size_t, int, const char*, const char*, const void*);
|
||||
@@ -37,6 +36,73 @@ static struct {
|
||||
ShcStatus status; ShcLength length; ShcBytes bytes, error;
|
||||
} SC;
|
||||
|
||||
static bool shaderc_load(void) {
|
||||
if (SC.tried) return SC.compiler != NULL;
|
||||
SC.tried = true;
|
||||
const char* names[] = { "libshaderc_shared.so.1", "libshaderc.so.1", "libshaderc_shared.so", "libshaderc.so" };
|
||||
for (int i = 0; i < 4 && !SC.dl; i++) SC.dl = dlopen(names[i], RTLD_NOW | RTLD_LOCAL);
|
||||
if (!SC.dl) return false;
|
||||
#define SYM(field, name) do { void* p_ = dlsym(SC.dl, name); memcpy(&SC.field, &p_, sizeof(p_)); } while (0)
|
||||
SYM(init, "shaderc_compiler_initialize");
|
||||
SYM(release_compiler, "shaderc_compiler_release");
|
||||
SYM(compile, "shaderc_compile_into_spv");
|
||||
SYM(status, "shaderc_result_get_compilation_status");
|
||||
SYM(length, "shaderc_result_get_length");
|
||||
SYM(bytes, "shaderc_result_get_bytes");
|
||||
SYM(error, "shaderc_result_get_error_message");
|
||||
SYM(release_result, "shaderc_result_release");
|
||||
#undef SYM
|
||||
if (SC.init && SC.compile && SC.status && SC.length && SC.bytes && SC.error && SC.release_result)
|
||||
SC.compiler = SC.init();
|
||||
return SC.compiler != NULL;
|
||||
}
|
||||
|
||||
static const char* FS_HEAD =
|
||||
"#version 450\n"
|
||||
"layout(location = 0) in vec3 v_pos;\n"
|
||||
"layout(location = 1) in vec3 v_nrm;\n"
|
||||
"layout(push_constant) uniform PC { mat4 mvp; mat4 model; vec4 color; vec4 light; } pc;\n"
|
||||
"layout(location = 0) out vec4 o_color;\n";
|
||||
static const char* FS_MAIN =
|
||||
"void main() {\n"
|
||||
" vec3 N = normalize(v_nrm);\n"
|
||||
" float light = pc.color.a + (1.0 - pc.color.a) * max(dot(N, pc.light.xyz), 0.0);\n"
|
||||
" o_color = vec4(tb_shade(v_pos, N, pc.color.rgb, light, pc.light.w), 1.0);\n"
|
||||
"}\n";
|
||||
|
||||
/* Tb3dRenderer.compile: o binario do shader para o Vulkan e o SPIR-V do
|
||||
fragment inteiro (a interface com o vertice fixo esta em FS_HEAD) */
|
||||
static uint32_t* spirv_fragment(const char* glsl, size_t* words, char* err, size_t cap) {
|
||||
*words = 0;
|
||||
if (err && cap) err[0] = 0;
|
||||
if (!shaderc_load()) { snprintf(err, cap, "sem a libshaderc: shaders feitos com blocos não rodam no Vulkan"); return NULL; }
|
||||
size_t n = strlen(FS_HEAD) + strlen(glsl) + strlen(FS_MAIN) + 1;
|
||||
char* src = malloc(n);
|
||||
if (!src) return NULL;
|
||||
snprintf(src, n, "%s%s%s", FS_HEAD, glsl, FS_MAIN);
|
||||
void* res = SC.compile(SC.compiler, src, strlen(src), 1 /* fragment */, "shader.frag", "main", NULL);
|
||||
free(src);
|
||||
if (!res || SC.status(res) != 0) {
|
||||
snprintf(err, cap, "shaderc: %.200s", res ? SC.error(res) : "falhou");
|
||||
if (res) SC.release_result(res);
|
||||
return NULL;
|
||||
}
|
||||
size_t len = SC.length(res);
|
||||
uint32_t* spv = malloc(len ? len : 4);
|
||||
if (spv) { memcpy(spv, SC.bytes(res), len); *words = len / 4; }
|
||||
SC.release_result(res);
|
||||
return spv;
|
||||
}
|
||||
|
||||
|
||||
static bool vk_compile(const char* glsl, void** data, size_t* size, char* err, size_t cap) {
|
||||
size_t words = 0;
|
||||
uint32_t* spv = spirv_fragment(glsl, &words, err, cap);
|
||||
*data = spv;
|
||||
*size = words * 4;
|
||||
return spv != NULL;
|
||||
}
|
||||
|
||||
static struct {
|
||||
bool tried, ok;
|
||||
char err[256], device[160];
|
||||
@@ -56,7 +122,7 @@ static struct {
|
||||
VkPipelineLayout shd_layout; /* 160 bytes de push constants */
|
||||
VkShaderModule shd_vert;
|
||||
bool shd_ok; /* da para usar shaders feitos com blocos */
|
||||
GpuShader shd[65];
|
||||
GpuShader shd[64];
|
||||
/* alvo (recriado quando muda o tamanho) */
|
||||
int w, h;
|
||||
VkImage color, depth;
|
||||
@@ -320,71 +386,32 @@ static GpuMesh* gpu_mesh(const Tb3dMesh* m) {
|
||||
return g;
|
||||
}
|
||||
|
||||
static bool shaderc_load(void) {
|
||||
if (SC.tried) return SC.compiler != NULL;
|
||||
SC.tried = true;
|
||||
const char* names[] = { "libshaderc_shared.so.1", "libshaderc.so.1", "libshaderc_shared.so", "libshaderc.so" };
|
||||
for (int i = 0; i < 4 && !SC.dl; i++) SC.dl = dlopen(names[i], RTLD_NOW | RTLD_LOCAL);
|
||||
if (!SC.dl) { snprintf(V.err, sizeof(V.err), "sem a libshaderc: shaders feitos com blocos não rodam no Vulkan"); return false; }
|
||||
#define SYM(field, name) do { void* p_ = dlsym(SC.dl, name); memcpy(&SC.field, &p_, sizeof(p_)); } while (0)
|
||||
SYM(init, "shaderc_compiler_initialize");
|
||||
SYM(release_compiler, "shaderc_compiler_release");
|
||||
SYM(compile, "shaderc_compile_into_spv");
|
||||
SYM(status, "shaderc_result_get_compilation_status");
|
||||
SYM(length, "shaderc_result_get_length");
|
||||
SYM(bytes, "shaderc_result_get_bytes");
|
||||
SYM(error, "shaderc_result_get_error_message");
|
||||
SYM(release_result, "shaderc_result_release");
|
||||
#undef SYM
|
||||
if (SC.init && SC.compile && SC.status && SC.length && SC.bytes && SC.error && SC.release_result)
|
||||
SC.compiler = SC.init();
|
||||
if (!SC.compiler) snprintf(V.err, sizeof(V.err), "a libshaderc não iniciou");
|
||||
return SC.compiler != NULL;
|
||||
}
|
||||
|
||||
static const char* FS_HEAD =
|
||||
"#version 450\n"
|
||||
"layout(location = 0) in vec3 v_pos;\n"
|
||||
"layout(location = 1) in vec3 v_nrm;\n"
|
||||
"layout(push_constant) uniform PC { mat4 mvp; mat4 model; vec4 color; vec4 light; } pc;\n"
|
||||
"layout(location = 0) out vec4 o_color;\n";
|
||||
static const char* FS_MAIN =
|
||||
"void main() {\n"
|
||||
" vec3 N = normalize(v_nrm);\n"
|
||||
" float light = pc.color.a + (1.0 - pc.color.a) * max(dot(N, pc.light.xyz), 0.0);\n"
|
||||
" o_color = vec4(tb_shade(v_pos, N, pc.color.rgb, light, pc.light.w), 1.0);\n"
|
||||
"}\n";
|
||||
|
||||
/* O pipeline de um shader feito com blocos (compila na 1a vez e a cada versao) */
|
||||
/* O pipeline de um shader feito com blocos (na 1a vez que o id aparece) */
|
||||
static GpuShader* gpu_shader(const Tb3dShader* sh) {
|
||||
if (sh->id <= 0 || sh->id >= 65) { snprintf(V.err, sizeof(V.err), "id de shader inválido"); return NULL; }
|
||||
GpuShader* g = &V.shd[sh->id];
|
||||
if (g->pipe && g->version == sh->version) return g;
|
||||
if (!shaderc_load()) return NULL;
|
||||
size_t n = strlen(FS_HEAD) + strlen(sh->glsl) + strlen(FS_MAIN) + 1;
|
||||
char* src = malloc(n);
|
||||
if (!src) return NULL;
|
||||
snprintf(src, n, "%s%s%s", FS_HEAD, sh->glsl, FS_MAIN);
|
||||
void* res = SC.compile(SC.compiler, src, strlen(src), 1 /* fragment */, "shader.frag", "main", NULL);
|
||||
free(src);
|
||||
if (!res || SC.status(res) != 0) {
|
||||
snprintf(V.err, sizeof(V.err), "shaderc: %.200s", res ? SC.error(res) : "falhou");
|
||||
if (res) SC.release_result(res);
|
||||
return NULL;
|
||||
GpuShader* g = &V.shd[(unsigned)sh->id % 64u];
|
||||
if (g->pipe && g->id == sh->id) return g;
|
||||
/* o binario que veio no shader ("compile o shader"); sem ele, compila agora */
|
||||
const void* code = NULL;
|
||||
size_t size = 0;
|
||||
void* own = NULL;
|
||||
for (int i = 0; i < sh->bin_n; i++)
|
||||
if (!strcmp(sh->bin[i].renderer, "vulkan")) { code = sh->bin[i].data; size = sh->bin[i].size; }
|
||||
if (!code) {
|
||||
if (!vk_compile(sh->glsl, &own, &size, V.err, sizeof(V.err))) return NULL;
|
||||
code = own;
|
||||
}
|
||||
VkShaderModuleCreateInfo sm = { VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, NULL, 0, SC.length(res),
|
||||
(const uint32_t*)(const void*)SC.bytes(res) };
|
||||
VkShaderModuleCreateInfo sm = { VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO, NULL, 0, size, code };
|
||||
VkShaderModule fs;
|
||||
VkResult r = vkCreateShaderModule(V.dev, &sm, NULL, &fs);
|
||||
SC.release_result(res);
|
||||
if (r != VK_SUCCESS) { snprintf(V.err, sizeof(V.err), "módulo do shader recusado (%d)", (int)r); return NULL; }
|
||||
free(own);
|
||||
if (r != VK_SUCCESS) { snprintf(V.err, sizeof(V.err), "binário do shader recusado (%d)", (int)r); return NULL; }
|
||||
vkDeviceWaitIdle(V.dev);
|
||||
if (g->pipe) vkDestroyPipeline(V.dev, g->pipe, NULL);
|
||||
g->pipe = VK_NULL_HANDLE;
|
||||
r = build_pipe(V.shd_vert, fs, V.shd_layout, &g->pipe);
|
||||
vkDestroyShaderModule(V.dev, fs, NULL);
|
||||
if (r != VK_SUCCESS) { snprintf(V.err, sizeof(V.err), "pipeline do shader recusado (%d)", (int)r); return NULL; }
|
||||
g->version = sh->version;
|
||||
g->id = sh->id;
|
||||
return g;
|
||||
}
|
||||
|
||||
@@ -419,7 +446,7 @@ static bool vk_draw(const Tb3dFrame* f, uint8_t* out) {
|
||||
memcpy(pc + 16, d->model, sizeof(d->model));
|
||||
pc[32] = d->color[0]; pc[33] = d->color[1]; pc[34] = d->color[2]; pc[35] = f->ambient;
|
||||
pc[36] = f->light_dir[0]; pc[37] = f->light_dir[1]; pc[38] = f->light_dir[2]; pc[39] = f->time;
|
||||
vkCmdBindPipeline(V.cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, V.shd[d->shader->id].pipe);
|
||||
vkCmdBindPipeline(V.cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, V.shd[(unsigned)d->shader->id % 64u].pipe);
|
||||
vkCmdPushConstants(V.cmd, V.shd_layout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
|
||||
0, sizeof(pc), pc);
|
||||
VkDeviceSize off = 0;
|
||||
@@ -466,7 +493,7 @@ static void vk_cleanup(void) {
|
||||
vkDeviceWaitIdle(V.dev);
|
||||
for (int i = 0; i < 32; i++) { buf_free(&V.mesh[i].vb); buf_free(&V.mesh[i].ib); }
|
||||
target_free();
|
||||
for (int i = 0; i < 65; i++) if (V.shd[i].pipe) vkDestroyPipeline(V.dev, V.shd[i].pipe, NULL);
|
||||
for (int i = 0; i < 64; i++) if (V.shd[i].pipe) vkDestroyPipeline(V.dev, V.shd[i].pipe, NULL);
|
||||
if (V.shd_vert) vkDestroyShaderModule(V.dev, V.shd_vert, NULL);
|
||||
if (V.shd_layout) vkDestroyPipelineLayout(V.dev, V.shd_layout, NULL);
|
||||
if (V.pipe) vkDestroyPipeline(V.dev, V.pipe, NULL);
|
||||
@@ -502,7 +529,7 @@ static const char* vk_error(void) { return V.err; }
|
||||
static bool vk_shaders(void) { return V.ok && V.shd_ok && shaderc_load(); }
|
||||
|
||||
static const Tb3dRenderer s_renderer = {
|
||||
TB_3D_RENDERER_VERSION, "vulkan", vk_available, vk_render, vk_device, vk_error, vk_shaders };
|
||||
TB_3D_RENDERER_VERSION, "vulkan", vk_available, vk_render, vk_device, vk_error, vk_shaders, vk_compile };
|
||||
|
||||
static bool lib_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
|
||||
(void)rt;
|
||||
|
||||
Reference in new issue
Block a user