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 | |
parent | c71e165e7afe563b7a65d6b994314bb403df5f04 (diff) |
simplify shader bindings a bit
-rw-r--r-- | src/new_render/immdraw.c | 5 | ||||
-rw-r--r-- | src/new_render/pbr.c | 34 | ||||
-rw-r--r-- | src/new_render/shadows.c | 12 | ||||
-rw-r--r-- | src/new_render/skybox.c | 9 | ||||
-rw-r--r-- | src/platform/path.c | 4 | ||||
-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 | ||||
-rw-r--r-- | src/systems/grid.c | 5 | ||||
-rw-r--r-- | src/systems/terrain.c | 9 |
10 files changed, 106 insertions, 99 deletions
diff --git a/src/new_render/immdraw.c b/src/new_render/immdraw.c index c79afa0..c711b0c 100644 --- a/src/new_render/immdraw.c +++ b/src/new_render/immdraw.c @@ -14,14 +14,15 @@ void Immdraw_Init(Immdraw_Storage* storage) { // storage->sphere = Mesh_Create(&sphere_geo, false); // pipeline / material - ShaderData camera_data = { .get_layout = &Binding_Camera_GetLayout }; + ShaderDataLayout camera_data = Binding_Camera_GetLayout(NULL); GraphicsPipelineDesc pipeline_desc = { .debug_name = "Immediate Draw Pipeline", .data_layouts = { camera_data }, .data_layouts_count = 1, }; - // storage->colour_pipeline = GPU_GraphicsPipeline_Create(pipeline_desc, GPU_GetDefaultRenderpass()); + // storage->colour_pipeline = GPU_GraphicsPipeline_Create(pipeline_desc, + // GPU_GetDefaultRenderpass()); } void Immdraw_Sphere(Transform tf, f32 size, Vec4 colour, bool wireframe) {}
\ No newline at end of file diff --git a/src/new_render/pbr.c b/src/new_render/pbr.c index 0581f4a..1e94e81 100644 --- a/src/new_render/pbr.c +++ b/src/new_render/pbr.c @@ -39,10 +39,10 @@ GPU_Pipeline* PBR_PipelineCreate(GPU_Renderpass* rpass) { char* vert_shader = string_from_file(vert_path); char* frag_shader = string_from_file(frag_path); - ShaderData camera_data = { .get_layout = &Binding_Camera_GetLayout }; - ShaderData model_data = { .get_layout = &Binding_Model_GetLayout }; - ShaderData material_data = { .get_layout = &PBRMaterial_GetLayout }; - ShaderData lights_data = { .get_layout = &Binding_Lights_GetLayout }; + ShaderDataLayout camera_data = Binding_Camera_GetLayout(NULL); + ShaderDataLayout model_data = Binding_Model_GetLayout(NULL); + ShaderDataLayout material_data = PBRMaterial_GetLayout(NULL); + ShaderDataLayout lights_data = Binding_Lights_GetLayout(NULL); GraphicsPipelineDesc desc = { .debug_name = "PBR Pipeline", @@ -86,8 +86,7 @@ void PBR_Execute(PBR_Storage* storage, Camera camera, TextureHandle shadowmap_te .projection = proj, .viewPos = vec4(camera.position.x, camera.position.y, camera.position.z, 1.0) }; - GPU_EncodeBindShaderData( - enc, 0, (ShaderData){ .data = &camera_data, .get_layout = &Binding_Camera_GetLayout }); + GPU_EncodeBindShaderData(enc, 0, Binding_Camera_GetLayout(&camera_data)); Vec3 light_color = vec3(300.0, 300.0, 300.0); Binding_Lights @@ -99,8 +98,7 @@ void PBR_Execute(PBR_Storage* storage, Camera camera, TextureHandle shadowmap_te (pbr_point_light){ .pos = vec3(10, -10, 10), .color = light_color }, (pbr_point_light){ .pos = vec3(-10, -10, 10), .color = light_color }, } }; - GPU_EncodeBindShaderData( - enc, 3, (ShaderData){ .data = &lights_data, .get_layout = &Binding_Lights_GetLayout }); + GPU_EncodeBindShaderData(enc, 3, Binding_Lights_GetLayout(&lights_data)); // TODO: Add shadowmap texture to uniforms Mesh_pool* mesh_pool = Render_GetMeshPool(); @@ -113,13 +111,11 @@ void PBR_Execute(PBR_Storage* storage, Camera camera, TextureHandle shadowmap_te // upload material data PBRMaterialUniforms material_data = { .mat = *mat }; - GPU_EncodeBindShaderData( - enc, 2, (ShaderData){ .data = &material_data, .get_layout = PBRMaterial_GetLayout }); + GPU_EncodeBindShaderData(enc, 2, PBRMaterial_GetLayout(&material_data)); // upload model transform Binding_Model model_data = { .model = renderable.affine }; - GPU_EncodeBindShaderData( - enc, 1, (ShaderData){ .data = &model_data, .get_layout = &Binding_Model_GetLayout }); + GPU_EncodeBindShaderData(enc, 1, Binding_Model_GetLayout(&model_data)); // set buffers GPU_EncodeSetVertexBuffer(enc, mesh->vertex_buffer); @@ -131,6 +127,20 @@ void PBR_Execute(PBR_Storage* storage, Camera camera, TextureHandle shadowmap_te GPU_CmdEncoder_EndRender(enc); } +void PBRMaterial_BindData(ShaderDataLayout* layout, const void* data) { + PBRMaterialUniforms* d = (PBRMaterialUniforms*)data; + CASSERT(data); + CASSERT(layout->binding_count == 5); + + TextureHandle white1x1 = Render_GetWhiteTexture(); + if (d->mat.albedo_map.raw != INVALID_TEX_HANDLE.raw) { + layout->bindings[0].data.texture.handle = d->mat.albedo_map; + } else { + layout->bindings[0].data.texture.handle = white1x1; + } + // TODO .. the rest +} + ShaderDataLayout PBRMaterial_GetLayout(void* data) { PBRMaterialUniforms* d = (PBRMaterialUniforms*)data; bool has_data = data != NULL; diff --git a/src/new_render/shadows.c b/src/new_render/shadows.c index 92fce81..1ca9119 100644 --- a/src/new_render/shadows.c +++ b/src/new_render/shadows.c @@ -79,7 +79,7 @@ void Shadow_Init(Shadow_Storage* storage, u32 shadowmap_width, u32 shadowmap_hei ERROR_EXIT("Failed to load shaders from disk"); } - ShaderData uniforms = { .data = NULL, .get_layout = &ShadowUniforms_GetLayout }; + ShaderDataLayout uniforms = ShadowUniforms_GetLayout(NULL); GraphicsPipelineDesc pipeline_desc = { .debug_name = "Shadows Pipeline", @@ -109,7 +109,7 @@ void Shadow_Init(Shadow_Storage* storage, u32 shadowmap_width, u32 shadowmap_hei ERROR_EXIT("Failed to load shaders from disk"); } - ShaderData debugquad_uniforms = { .data = NULL, .get_layout = &ShadowDebugQuad_GetLayout }; + ShaderDataLayout debugquad_uniforms = ShadowDebugQuad_GetLayout(NULL); GraphicsPipelineDesc debugquad_pipeline_desc = { .debug_name = "Shadows debug quad Pipeline", @@ -164,8 +164,7 @@ void Shadow_DrawDebugQuad() { GPU_CmdEncoder_BeginRender(enc, shadow_storage->debugquad_pass); GPU_EncodeBindPipeline(enc, shadow_storage->debugquad_pipeline); - ShaderData quad_data = { .data = &shadow_storage->depth_texture, - .get_layout = ShadowDebugQuad_GetLayout }; + ShaderDataLayout quad_data = ShadowDebugQuad_GetLayout(&shadow_storage->depth_texture); GPU_EncodeBindShaderData(enc, 0, quad_data); GPU_EncodeSetVertexBuffer(enc, shadow_storage->quad.vertex_buffer); GPU_EncodeSetIndexBuffer(enc, shadow_storage->quad.index_buffer); @@ -190,10 +189,7 @@ void Shadow_ShadowmapExecute(Shadow_Storage* storage, Mat4 light_space_transform .light_space = light_space_transform, .model = mat4_ident() // this will be overwritten for each Model }; - ShaderData shader_data = { - .data = &uniforms, - .get_layout = &ShadowUniforms_GetLayout, - }; + ShaderDataLayout shader_data = ShadowUniforms_GetLayout(&uniforms); for (size_t ent_i = 0; ent_i < entity_count; ent_i++) { RenderEnt renderable = entities[ent_i]; diff --git a/src/new_render/skybox.c b/src/new_render/skybox.c index 5456401..cc5797f 100644 --- a/src/new_render/skybox.c +++ b/src/new_render/skybox.c @@ -95,8 +95,8 @@ Skybox Skybox_Create(const char** face_paths, int n) { // VertexDesc_AddAttr(&pos_only, "inPos", ATTR_F32x3); // pos_only.use_full_vertex_size = true; - ShaderData camera_data = { .data = NULL, .get_layout = &Binding_Camera_GetLayout }; - ShaderData shader_data = { .data = NULL, .get_layout = &Skybox_GetLayout }; + ShaderDataLayout camera_data = Binding_Camera_GetLayout(NULL); + ShaderDataLayout shader_data = Skybox_GetLayout(NULL); VertexDescription builder = { .debug_label = "pos only" }; VertexDesc_AddAttr(&builder, "inPosition", ATTR_F32x3); @@ -151,11 +151,10 @@ void Skybox_Draw(Skybox* skybox, Camera camera) { .projection = proj, .viewPos = vec4(camera.position.x, camera.position.y, camera.position.z, 1.0) }; - GPU_EncodeBindShaderData( - enc, 0, (ShaderData){ .data = &camera_data, .get_layout = &Binding_Camera_GetLayout }); + GPU_EncodeBindShaderData(enc, 0, Binding_Camera_GetLayout(&camera_data)); SkyboxUniforms uniforms = { .cubemap = skybox->texture }; - ShaderData skybox_data = { .data = &uniforms, .get_layout = &Skybox_GetLayout }; + ShaderDataLayout skybox_data = Skybox_GetLayout(&uniforms); GPU_EncodeBindShaderData(enc, 0, skybox_data); GPU_EncodeSetVertexBuffer(enc, skybox->cube.vertex_buffer); diff --git a/src/platform/path.c b/src/platform/path.c index 3607089..f43d954 100644 --- a/src/platform/path.c +++ b/src/platform/path.c @@ -16,8 +16,8 @@ path_opt path_parent(arena* a, const char* path) { } #endif #ifdef CEL_PLATFORM_WINDOWS -#include <windows.h> #include <shlwapi.h> +#include <windows.h> #pragma comment(lib, "Shlwapi.lib") path_opt path_parent(arena* a, const char* path) { @@ -29,7 +29,7 @@ path_opt path_parent(arena* a, const char* path) { if (PathRemoveFileSpecA(path_copy)) { return (path_opt){ .path = Str8_cstr_view(path_copy), .has_value = true }; } else { - return (path_opt){ .has_value = false}; + return (path_opt){ .has_value = false }; } } #endif 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; diff --git a/src/systems/grid.c b/src/systems/grid.c index 70092e0..14722e3 100644 --- a/src/systems/grid.c +++ b/src/systems/grid.c @@ -37,7 +37,7 @@ void Grid_Init(Grid_Storage* storage) { ERROR_EXIT("Failed to load shaders from disk") } - ShaderData camera_data = { .get_layout = &Binding_Camera_GetLayout }; + ShaderDataLayout camera_data = Binding_Camera_GetLayout(NULL); GraphicsPipelineDesc pipeline_desc = { .debug_name = "Infinite grid pipeline", @@ -77,8 +77,7 @@ void Grid_Execute(Grid_Storage* storage) { .projection = proj, .viewPos = vec4(camera.position.x, camera.position.y, camera.position.z, 1.0) }; - GPU_EncodeBindShaderData( - enc, 0, (ShaderData){ .data = &camera_data, .get_layout = &Binding_Camera_GetLayout }); + GPU_EncodeBindShaderData(enc, 0, Binding_Camera_GetLayout(&camera_data)); GPU_EncodeSetVertexBuffer(enc, storage->plane_vertices); GPU_EncodeSetIndexBuffer(enc, storage->plane_indices); GPU_EncodeDrawIndexed(enc, 6); diff --git a/src/systems/terrain.c b/src/systems/terrain.c index d2a9300..bde4b4d 100644 --- a/src/systems/terrain.c +++ b/src/systems/terrain.c @@ -38,8 +38,8 @@ bool Terrain_Init(Terrain_Storage* storage) { ERROR_EXIT("Failed to load shaders from disk") } - ShaderData camera_data = { .get_layout = &Binding_Camera_GetLayout }; - ShaderData terrain_data = { .get_layout = &TerrainUniforms_GetLayout }; + ShaderDataLayout camera_data = Binding_Camera_GetLayout(NULL); + ShaderDataLayout terrain_data = TerrainUniforms_GetLayout(NULL); GraphicsPipelineDesc pipeline_desc = { .debug_name = "terrain rendering pipeline", @@ -166,11 +166,10 @@ void Terrain_Draw(Terrain_Storage* storage) { .projection = proj, .viewPos = vec4(scene->camera.position.x, scene->camera.position.y, scene->camera.position.z, 1.0) }; - GPU_EncodeBindShaderData( - enc, 0, (ShaderData){ .data = &camera_data, .get_layout = &Binding_Camera_GetLayout }); + GPU_EncodeBindShaderData(enc, 0, Binding_Camera_GetLayout(&camera_data)); TerrainUniforms uniforms = { .tex_slot_1 = storage->texture }; - ShaderData terrain_data = { .data = &uniforms, .get_layout = &TerrainUniforms_GetLayout }; + ShaderDataLayout terrain_data = TerrainUniforms_GetLayout(&uniforms); GPU_EncodeBindShaderData(enc, 1, terrain_data); GPU_EncodeSetVertexBuffer(enc, storage->vertex_buffer); |