diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-08-04 10:57:07 +1000 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-08-04 10:57:07 +1000 |
commit | a837297eab7447db797198b84fde5f29f1c992ce (patch) | |
tree | 01850c363b4b70ea9bd0e44e0378449745ab19db /src/ral | |
parent | c71e165e7afe563b7a65d6b994314bb403df5f04 (diff) |
simplify shader bindings a bit
Diffstat (limited to 'src/ral')
-rw-r--r-- | src/ral/backends/opengl/backend_opengl.c | 110 | ||||
-rw-r--r-- | src/ral/ral_impl.h | 4 | ||||
-rw-r--r-- | src/ral/ral_types.h | 13 |
3 files changed, 65 insertions, 62 deletions
diff --git a/src/ral/backends/opengl/backend_opengl.c b/src/ral/backends/opengl/backend_opengl.c index b04726c..fc7d3b4 100644 --- a/src/ral/backends/opengl/backend_opengl.c +++ b/src/ral/backends/opengl/backend_opengl.c @@ -141,7 +141,7 @@ GPU_Pipeline* GPU_GraphicsPipeline_Create(GraphicsPipelineDesc description, u32 ubo_count = 0; // printf("data layouts %d\n", description.data_layouts_count); for (u32 layout_i = 0; layout_i < description.data_layouts_count; layout_i++) { - ShaderDataLayout sdl = description.data_layouts[layout_i].get_layout(NULL); + ShaderDataLayout sdl = description.data_layouts[layout_i]; TRACE("Got shader data layout %d's bindings! . found %d", layout_i, sdl.binding_count); for (u32 binding_j = 0; binding_j < sdl.binding_count; binding_j++) { @@ -342,66 +342,66 @@ void GPU_EncodeBindPipeline(GPU_CmdEncoder* encoder, GPU_Pipeline* pipeline) { } } -PUB void GPU_EncodeBindShaderData(GPU_CmdEncoder* encoder, u32 group, ShaderData data) { - ShaderDataLayout sdl = data.get_layout(data.data); - - for (u32 i = 0; i < sdl.binding_count; i++) { - ShaderBinding binding = sdl.bindings[i]; - /* print_shader_binding(binding); */ - - if (binding.kind == BINDING_BYTES) { - BufferHandle b; - GPU_Buffer* ubo_buf; - bool found = false; - for (u32 i = 0; i < encoder->pipeline->uniform_count; i++) { - b = encoder->pipeline->uniform_bindings[i]; - ubo_buf = BUFFER_GET(b); - assert(ubo_buf->name != NULL); - if (strcmp(ubo_buf->name, binding.label) == 0) { - found = true; - break; - } - } - if (!found) { - ERROR("Couldnt find uniform buffer object for %s!!", binding.label); - } - - i32 blockIndex = glGetUniformBlockIndex(encoder->pipeline->shader_id, binding.label); - if (blockIndex < 0) { - WARN("Couldn't retrieve block index for uniform block '%s'", binding.label); - } else { - // DEBUG("Retrived block index %d for %s", blockIndex, binding.label); - } - - glBindBuffer(GL_UNIFORM_BUFFER, ubo_buf->id.ubo); - glBufferSubData(GL_UNIFORM_BUFFER, 0, ubo_buf->size, binding.data.bytes.data); - - } else if (binding.kind == BINDING_TEXTURE) { - GPU_Texture* tex = TEXTURE_GET(binding.data.texture.handle); - GLint tex_slot = glGetUniformLocation(encoder->pipeline->shader_id, binding.label); - // printf("%d slot \n", tex_slot); - if (tex_slot == GL_INVALID_VALUE || tex_slot < 0) { - WARN("Invalid binding label for texture %s - couldn't fetch texture slot uniform", - binding.label); - } - glUniform1i(tex_slot, i); - glActiveTexture(GL_TEXTURE0 + i); - GLenum gl_tex_type = opengl_tex_type(tex->type); - glBindTexture(gl_tex_type, tex->id); - } - } -} - -void GPU_EncodeBindShaderDataRaw(GPU_CmdEncoder* encoder, u32 group, ShaderDataLayout layout) { +// PUB void GPU_EncodeBindShaderData(GPU_CmdEncoder* encoder, u32 group, ShaderData data) { +// ShaderDataLayout sdl = data.get_layout(data.data); + +// for (u32 i = 0; i < sdl.binding_count; i++) { +// ShaderBinding binding = sdl.bindings[i]; +// /* print_shader_binding(binding); */ + +// if (binding.kind == BINDING_BYTES) { +// BufferHandle b; +// GPU_Buffer* ubo_buf; +// bool found = false; +// for (u32 i = 0; i < encoder->pipeline->uniform_count; i++) { +// b = encoder->pipeline->uniform_bindings[i]; +// ubo_buf = BUFFER_GET(b); +// assert(ubo_buf->name != NULL); +// if (strcmp(ubo_buf->name, binding.label) == 0) { +// found = true; +// break; +// } +// } +// if (!found) { +// ERROR("Couldnt find uniform buffer object for %s!!", binding.label); +// } + +// i32 blockIndex = glGetUniformBlockIndex(encoder->pipeline->shader_id, binding.label); +// if (blockIndex < 0) { +// WARN("Couldn't retrieve block index for uniform block '%s'", binding.label); +// } else { +// // DEBUG("Retrived block index %d for %s", blockIndex, binding.label); +// } + +// glBindBuffer(GL_UNIFORM_BUFFER, ubo_buf->id.ubo); +// glBufferSubData(GL_UNIFORM_BUFFER, 0, ubo_buf->size, binding.data.bytes.data); + +// } else if (binding.kind == BINDING_TEXTURE) { +// GPU_Texture* tex = TEXTURE_GET(binding.data.texture.handle); +// GLint tex_slot = glGetUniformLocation(encoder->pipeline->shader_id, binding.label); +// // printf("%d slot \n", tex_slot); +// if (tex_slot == GL_INVALID_VALUE || tex_slot < 0) { +// WARN("Invalid binding label for texture %s - couldn't fetch texture slot uniform", +// binding.label); +// } +// glUniform1i(tex_slot, i); +// glActiveTexture(GL_TEXTURE0 + i); +// GLenum gl_tex_type = opengl_tex_type(tex->type); +// glBindTexture(gl_tex_type, tex->id); +// } +// } +// } + +void GPU_EncodeBindShaderData(GPU_CmdEncoder* encoder, u32 group, ShaderDataLayout layout) { for (u32 binding_i = 0; binding_i < layout.binding_count; binding_i++) { ShaderBinding binding = layout.bindings[binding_i]; switch (binding.kind) { case BINDING_BYTES: { - #ifdef RAL_ASSERTS +#ifdef RAL_ASSERTS CASSERT_MSG(binding.data.bytes.data, "void* data pointer should be non null"); CASSERT_MSG(binding.data.bytes.size > 0, "size should be greater than 0 bytes"); - #endif +#endif // TODO } case BINDING_TEXTURE: { @@ -409,7 +409,7 @@ void GPU_EncodeBindShaderDataRaw(GPU_CmdEncoder* encoder, u32 group, ShaderDataL GLint tex_slot = glGetUniformLocation(encoder->pipeline->shader_id, binding.label); if (tex_slot == GL_INVALID_VALUE || tex_slot < 0) { WARN("Invalid binding label for texture %s - couldn't fetch texture slot uniform", - binding.label); + binding.label); } glUniform1i(tex_slot, binding_i); glActiveTexture(GL_TEXTURE0 + binding_i); diff --git a/src/ral/ral_impl.h b/src/ral/ral_impl.h index 99c8d18..0ca73c7 100644 --- a/src/ral/ral_impl.h +++ b/src/ral/ral_impl.h @@ -73,8 +73,8 @@ void copy_buffer_to_image_oneshot(BufferHandle src, TextureHandle dst); // --- Render commands PUB void GPU_EncodeBindPipeline(GPU_CmdEncoder* encoder, GPU_Pipeline* pipeline); -PUB void GPU_EncodeBindShaderData(GPU_CmdEncoder* encoder, u32 group, ShaderData data); -PUB void GPU_EncodeBindShaderDataRaw(GPU_CmdEncoder* encoder, u32 group, ShaderDataLayout layout); +// PUB void GPU_EncodeBindShaderData(GPU_CmdEncoder* encoder, u32 group, ShaderData data); +PUB void GPU_EncodeBindShaderData(GPU_CmdEncoder* encoder, u32 group, ShaderDataLayout layout); void GPU_EncodeSetDefaults(GPU_CmdEncoder* encoder); PUB void GPU_EncodeSetVertexBuffer(GPU_CmdEncoder* encoder, BufferHandle buf); PUB void GPU_EncodeSetIndexBuffer(GPU_CmdEncoder* encoder, BufferHandle buf); diff --git a/src/ral/ral_types.h b/src/ral/ral_types.h index 8a73041..ec302c1 100644 --- a/src/ral/ral_types.h +++ b/src/ral/ral_types.h @@ -211,10 +211,13 @@ typedef struct ShaderDataLayout { typedef ShaderDataLayout (*FN_GetBindingLayout)(void* data); -typedef struct ShaderData { - FN_GetBindingLayout get_layout; - void* data; -} ShaderData; +/** @brief takes a `ShaderDataLayout` without data, and puts the correct data into each binding */ +typedef void (*FN_BindShaderData)(ShaderDataLayout* layout, const void* data); + +// typedef struct ShaderData { +// FN_GetBindingLayout get_layout; +// void* data; +// } ShaderData; // --- Miscellaneous @@ -239,7 +242,7 @@ typedef struct GraphicsPipelineDesc { // examples: // - uniform buffer representing view projection matrix // - texture for shadow map - ShaderData data_layouts[MAX_SHADER_DATA_LAYOUTS]; + ShaderDataLayout data_layouts[MAX_SHADER_DATA_LAYOUTS]; u32 data_layouts_count; bool wireframe; |