From af55c4a7a54a90d4c252aa4fe354e7bf8624072e Mon Sep 17 00:00:00 2001 From: skal Date: Mon, 9 Feb 2026 15:20:09 +0100 Subject: refactor: Deduplicate CommonUniforms with #include in WGSL shaders Replace redundant CommonUniforms struct definitions across 13 shaders with #include "common_uniforms" directive. Integrate ShaderComposer preprocessing into all shader creation pipelines. Changes: - Replace 9-line CommonUniforms definitions with single #include line - Add ShaderComposer.Compose() to create_post_process_pipeline() - Add ShaderComposer.Compose() to gpu_create_render_pass() - Add ShaderComposer.Compose() to gpu_create_compute_pass() - Add InitShaderComposer() calls to test_effect_base and test_demo_effects - Update test_shader_compilation to compose shaders before validation Net reduction: 83 lines of duplicate code eliminated All 35 tests passing (100%) Co-Authored-By: Claude Sonnet 4.5 --- src/gpu/gpu.cc | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/gpu/gpu.cc') diff --git a/src/gpu/gpu.cc b/src/gpu/gpu.cc index fde241d..e89a2f0 100644 --- a/src/gpu/gpu.cc +++ b/src/gpu/gpu.cc @@ -5,6 +5,7 @@ #include "gpu.h" #include "effect.h" #include "gpu/effects/shaders.h" +#include "gpu/effects/shader_composer.h" #include "platform/platform.h" #include @@ -55,10 +56,13 @@ RenderPass gpu_create_render_pass(WGPUDevice device, WGPUTextureFormat format, ResourceBinding* bindings, int num_bindings) { RenderPass pass = {}; + // Compose shader to resolve #include directives + std::string composed_shader = ShaderComposer::Get().Compose({}, shader_code); + // Create Shader Module WGPUShaderSourceWGSL wgsl_src = {}; wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL; - wgsl_src.code = str_view(shader_code); + wgsl_src.code = str_view(composed_shader.c_str()); WGPUShaderModuleDescriptor shader_desc = {}; shader_desc.nextInChain = &wgsl_src.chain; WGPUShaderModule shader_module = @@ -156,9 +160,12 @@ ComputePass gpu_create_compute_pass(WGPUDevice device, const char* shader_code, int num_bindings) { ComputePass pass = {}; + // Compose shader to resolve #include directives + std::string composed_shader = ShaderComposer::Get().Compose({}, shader_code); + WGPUShaderSourceWGSL wgsl_src = {}; wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL; - wgsl_src.code = str_view(shader_code); + wgsl_src.code = str_view(composed_shader.c_str()); WGPUShaderModuleDescriptor shader_desc = {}; shader_desc.nextInChain = &wgsl_src.chain; WGPUShaderModule shader_module = -- cgit v1.2.3