summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-09 15:20:09 +0100
committerskal <pascal.massimino@gmail.com>2026-02-09 15:20:09 +0100
commitaf55c4a7a54a90d4c252aa4fe354e7bf8624072e (patch)
tree6aef045c1f52f0df766e5b091cfb61a950d48c3f /src
parentff4108b383e8b543a298debdb3518a19e08d74ab (diff)
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 <noreply@anthropic.com>
Diffstat (limited to 'src')
-rw-r--r--src/gpu/effects/post_process_helper.cc5
-rw-r--r--src/gpu/gpu.cc11
-rw-r--r--src/tests/test_demo_effects.cc3
-rw-r--r--src/tests/test_effect_base.cc3
-rw-r--r--src/tests/test_shader_compilation.cc7
5 files changed, 24 insertions, 5 deletions
diff --git a/src/gpu/effects/post_process_helper.cc b/src/gpu/effects/post_process_helper.cc
index bc66448..e99467f 100644
--- a/src/gpu/effects/post_process_helper.cc
+++ b/src/gpu/effects/post_process_helper.cc
@@ -4,16 +4,19 @@
#include "post_process_helper.h"
#include "../demo_effects.h"
#include "gpu/gpu.h"
+#include "gpu/effects/shader_composer.h"
#include <cstring>
// Helper to create a standard post-processing pipeline
WGPURenderPipeline create_post_process_pipeline(WGPUDevice device,
WGPUTextureFormat format,
const char* shader_code) {
+ std::string composed_shader = ShaderComposer::Get().Compose({}, shader_code);
+
WGPUShaderModuleDescriptor shader_desc = {};
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());
shader_desc.nextInChain = &wgsl_src.chain;
WGPUShaderModule shader_module =
wgpuDeviceCreateShaderModule(device, &shader_desc);
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 <cassert>
@@ -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 =
diff --git a/src/tests/test_demo_effects.cc b/src/tests/test_demo_effects.cc
index d0163c2..0d2b09a 100644
--- a/src/tests/test_demo_effects.cc
+++ b/src/tests/test_demo_effects.cc
@@ -197,6 +197,9 @@ static void test_effect_type_classification() {
int main() {
fprintf(stdout, "=== Demo Effects Tests ===\n");
+ extern void InitShaderComposer();
+ InitShaderComposer();
+
test_post_process_effects();
test_scene_effects();
test_effect_type_classification();
diff --git a/src/tests/test_effect_base.cc b/src/tests/test_effect_base.cc
index e280e05..612e9da 100644
--- a/src/tests/test_effect_base.cc
+++ b/src/tests/test_effect_base.cc
@@ -249,6 +249,9 @@ static void test_pixel_helpers() {
int main() {
fprintf(stdout, "=== Effect Base Tests ===\n");
+ extern void InitShaderComposer();
+ InitShaderComposer();
+
test_webgpu_fixture();
test_offscreen_render_target();
test_effect_construction();
diff --git a/src/tests/test_shader_compilation.cc b/src/tests/test_shader_compilation.cc
index e2c0adc..a322e8a 100644
--- a/src/tests/test_shader_compilation.cc
+++ b/src/tests/test_shader_compilation.cc
@@ -115,16 +115,19 @@ static bool test_shader_compilation(const char* name, const char* shader_code) {
return true; // Not a failure, just skipped
}
+ // Compose shader to resolve #include directives
+ std::string composed_shader = ShaderComposer::Get().Compose({}, shader_code);
+
#if defined(DEMO_CROSS_COMPILE_WIN32)
WGPUShaderModuleWGSLDescriptor wgsl_desc = {};
wgsl_desc.chain.sType = WGPUSType_ShaderModuleWGSLDescriptor;
- wgsl_desc.code = shader_code;
+ wgsl_desc.code = composed_shader.c_str();
WGPUShaderModuleDescriptor shader_desc = {};
shader_desc.nextInChain = (const WGPUChainedStruct*)&wgsl_desc.chain;
#else
WGPUShaderSourceWGSL wgsl_desc = {};
wgsl_desc.chain.sType = WGPUSType_ShaderSourceWGSL;
- wgsl_desc.code = str_view(shader_code);
+ wgsl_desc.code = str_view(composed_shader.c_str());
WGPUShaderModuleDescriptor shader_desc = {};
shader_desc.nextInChain = (const WGPUChainedStruct*)&wgsl_desc.chain;
#endif