From 35d9371c0eda3fe208bcbc970f73adee0481b37f Mon Sep 17 00:00:00 2001 From: cinnaboot Date: Wed, 24 Nov 2021 10:24:20 -0500 Subject: [PATCH] break out parseUniform() from parseShaderUniforms() --- .gitignore | 1 + src/main.cpp | 24 ++++++-------- src/shader.cpp | 88 ++++++++++++++++++++++++++++---------------------- src/shader.h | 15 ++++++++- 4 files changed, 73 insertions(+), 55 deletions(-) diff --git a/.gitignore b/.gitignore index 0ff805c..e6b1f3a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ tags *.d build/ +bin/ shader_testing diff --git a/src/main.cpp b/src/main.cpp index 3555da5..f3abb0d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -77,20 +77,8 @@ getModel(RenderState* rs, const char* filepath) return mdl; } -bool -addGLMesh(gl_mesh_array* gma, gl_mesh* gm) -{ - if (gma->count < gma->max) { - // FIXME: wtf was I doing here? - gma->count++; - - } - - return false; -} - gl_mesh* -getGLMesh(gl_mesh_array* gma) +getFreeGLMesh(gl_mesh_array* gma) { if (gma->count < gma->max) { gl_mesh* glm = &gma->gl_meshes[gma->count]; @@ -135,9 +123,15 @@ initGLContext(memory_arena* arena) GLContext* gl_ctx = (GLContext*) arenaAllocateBlock(arena, sizeof(GLContext)); gl_ctx->shader_arr = initShaderArray(arena, 256); - glGetIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS, &gl_ctx->max_binding_points); gl_ctx->binding_count = 0; + // FIXME: why does only binding points work here? + // the other values were accessable after linking a shader maybe? + glGetIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS, &gl_ctx->max_binding_points); + glGetIntegerv(GL_MAX_VERTEX_UNIFORM_BLOCKS, &gl_ctx->max_vertex_blocks); + glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_BLOCKS, &gl_ctx->max_fragment_blocks); + glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &gl_ctx->max_ublock_size); + return gl_ctx; } @@ -254,7 +248,7 @@ loadScene(RenderState* rs) return false; for (u32 i = 0; i < NUM_CUBES; i++) { - gl_mesh* gmesh = getGLMesh(rs->gl_mesh_arr); + gl_mesh* gmesh = getFreeGLMesh(rs->gl_mesh_arr); if (!gmesh) return false; diff --git a/src/shader.cpp b/src/shader.cpp index 1adfce8..ea81cb4 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -82,10 +82,7 @@ addShaderProgram(memory_arena* arena, block_idx, gl_ctx->xform_ubo.binding_idx); - // FIXME: fill out new shader extra uniforms and buffers here - dumpShader(s->prog_id); - printf("offset of RenderState.xforms: %lu bytes\n", - offsetof(RenderState, xforms)); + //dumpShader(s->prog_id); parseShaderUniforms(arena, s, gl_ctx); /// @@ -134,6 +131,53 @@ dumpTextFile(const char* filepath) return s; } +// NOTE: returns sizes based on GLSL layout std140 +// https://www.khronos.org/opengl/wiki/Interface_Block_(GLSL)#Memory_layout +u32 +getGLTypeSize(GLenum e) +{ + switch (e) { + case 0x8b51: return 4 * sizeof(GLfloat); // GL_FLOAT_VEC3 + case 0x8b52: return 4 * sizeof(GLfloat); // GL_FLOAT_VEC4 + case 0x8b5c: return 16 * sizeof(GLfloat); // GL_FLOAT_MAT4 + default: return 0; + } +} + +gl_uniform +parseUniform(memory_arena* arena, shader_program* s, u32 uniform_idx) +{ + assert(uniform_idx < s->num_uniforms); + gl_uniform& unif = s->uniforms[uniform_idx]; + + GLchar unif_name[256] = {0}; + GLsizei length; + GLint size; + GLenum type; + + glGetActiveUniform(s->prog_id, uniform_idx, sizeof(unif_name), + &length, &size, &type, unif_name); + + unif.data_type = type; + unif.name = (char*) arenaAllocateBlock( + arena, (length + 1) * sizeof(char)); + std::strncpy(unif.name, unif_name, length); + + // FIXME: testing idx == unif location + unif.idx = uniform_idx; +#if 1 + GLuint location = glGetUniformLocation(s->prog_id, unif.name); + printf("uniform idx: %d, loc: %d, block idx: %d \n", + unif.idx, location, unif.block_idx); +#endif + unif.data_size = getGLTypeSize(unif.data_type); + + if (unif.data_size == 0) + printf("%s(), error getting data_size\n", __FUNCTION__); + + return unif; +} + void parseShaderUniforms(memory_arena* arena, shader_program* s, GLContext* gl_ctx) { @@ -144,45 +188,11 @@ parseShaderUniforms(memory_arena* arena, shader_program* s, GLContext* gl_ctx) arena, s->num_uniforms * sizeof(gl_uniform)); for (u32 i = 0; i < s->num_uniforms; i++) { - GLchar unif_name[256] = {0}; - GLsizei length; - GLint size; - GLenum type; - - glGetActiveUniform(s->prog_id, i, sizeof(unif_name), - &length, &size, &type, unif_name); - - s->uniforms[i].data_type = type; - s->uniforms[i].name = (char*) arenaAllocateBlock( - arena, (length + 1) * sizeof(char)); - std::strncpy(s->uniforms[i].name, unif_name, length); - glGetActiveUniformsiv(s->prog_id, 1, &i, GL_UNIFORM_BLOCK_INDEX, &s->uniforms[i].block_idx); - // FIXME: testing idx == unif location - s->uniforms[i].idx = i; - GLuint location = glGetUniformLocation(s->prog_id, s->uniforms[i].name); - printf("idx: %d, loc: %d, block idx: %d \n", - i, location, s->uniforms[i].block_idx); - - - // TODO: get uniform.data_size from data_type - //printf("i: %d, uniform name: %s, l: %d \n", i, unif_name, length); + parseUniform(arena, s, i); } - - GLint max_vert_ublocks; - GLint max_frag_ublocks; - GLint max_ublock_size; - glGetIntegerv(GL_MAX_VERTEX_UNIFORM_BLOCKS, &max_vert_ublocks); - glGetIntegerv(GL_MAX_FRAGMENT_UNIFORM_BLOCKS, &max_frag_ublocks); - glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &max_ublock_size); - printf("max vert uniform blocks: %d, max frag uniform blocks: %d," - "max uniform block size: %d bytes \n", - max_vert_ublocks, max_frag_ublocks, max_ublock_size); - GLint max_ubo_bindings; - glGetIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS, &max_ubo_bindings); - printf("max uniform buffer object binding points: %d \n", max_ubo_bindings); } gl_buffer diff --git a/src/shader.h b/src/shader.h index 3162c03..2eb247d 100644 --- a/src/shader.h +++ b/src/shader.h @@ -36,13 +36,23 @@ struct gl_uniform char* name; }; +struct GLUniformBlock +{ + GLuint block_id; + GLuint prog_id; +}; + struct shader_program { GLuint prog_id; GLuint xforms_ubo_id; GLuint model_xform_id; + u32 num_uniform_blocks; + GLUniformBlock* uniform_blocks; + u32 num_uniforms; + // NOTE: contains individual uniforms, and uniforms part of a block gl_uniform* uniforms; u32 num_buffers; @@ -63,10 +73,13 @@ struct ShaderArray struct GLContext { gl_buffer xform_ubo; - // TODO: keep an array of uniform buffer objects store on ctx? + // TODO: keep an array of uniform buffer objects stored on ctx? GLuint binding_count; GLint max_binding_points; + GLint max_vertex_blocks; + GLint max_fragment_blocks; + GLint max_ublock_size; ShaderArray* shader_arr; };