diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-10 17:27:34 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-10 17:27:34 +0100 |
| commit | e7cd4d65f9f55ccc14045cbcac9d61358ba0c2bf (patch) | |
| tree | c9365a3cb26953478a4bcaf684b7e2ec92442557 /src/gpu/effects/post_process_helper.cc | |
| parent | 61104d5b9e1774c11f0dba3b6d6018dabc2bce8f (diff) | |
refactor: Factor WGPU boilerplate into builder pattern helpers
Add BindGroupLayoutBuilder, BindGroupBuilder, RenderPipelineBuilder,
and SamplerCache to reduce repetitive WGPU code. Refactor
post_process_helper, cnn_effect, and rotating_cube_effect.
Changes:
- Bind group creation: 19 instances, 14→4 lines each
- Pipeline creation: 30-50→8 lines
- Sampler deduplication: 6 instances → cached
- Total boilerplate reduction: -122 lines across 3 files
Builder pattern prevents binding index errors and consolidates
platform-specific #ifdef in fewer locations. Binary size unchanged
(6.3M debug). Tests pass.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Diffstat (limited to 'src/gpu/effects/post_process_helper.cc')
| -rw-r--r-- | src/gpu/effects/post_process_helper.cc | 102 |
1 files changed, 27 insertions, 75 deletions
diff --git a/src/gpu/effects/post_process_helper.cc b/src/gpu/effects/post_process_helper.cc index e99467f..0c339c7 100644 --- a/src/gpu/effects/post_process_helper.cc +++ b/src/gpu/effects/post_process_helper.cc @@ -5,69 +5,30 @@ #include "../demo_effects.h" #include "gpu/gpu.h" #include "gpu/effects/shader_composer.h" +#include "gpu/bind_group_builder.h" +#include "gpu/sampler_cache.h" +#include "gpu/pipeline_builder.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); + WGPUBindGroupLayout bgl = BindGroupLayoutBuilder() + .sampler(PP_BINDING_SAMPLER, WGPUShaderStage_Fragment) + .texture(PP_BINDING_TEXTURE, WGPUShaderStage_Fragment) + .uniform(PP_BINDING_UNIFORMS, WGPUShaderStage_Vertex | WGPUShaderStage_Fragment) + .uniform(PP_BINDING_EFFECT_PARAMS, WGPUShaderStage_Fragment) + .build(device); - WGPUShaderModuleDescriptor shader_desc = {}; - WGPUShaderSourceWGSL wgsl_src = {}; - wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL; - wgsl_src.code = str_view(composed_shader.c_str()); - shader_desc.nextInChain = &wgsl_src.chain; - WGPUShaderModule shader_module = - wgpuDeviceCreateShaderModule(device, &shader_desc); + WGPURenderPipeline pipeline = RenderPipelineBuilder(device) + .shader(shader_code) + .bind_group_layout(bgl) + .format(format) + .build(); - WGPUBindGroupLayoutEntry bgl_entries[4] = {}; - bgl_entries[0].binding = PP_BINDING_SAMPLER; - bgl_entries[0].visibility = WGPUShaderStage_Fragment; - bgl_entries[0].sampler.type = WGPUSamplerBindingType_Filtering; - bgl_entries[1].binding = PP_BINDING_TEXTURE; - bgl_entries[1].visibility = WGPUShaderStage_Fragment; - bgl_entries[1].texture.sampleType = WGPUTextureSampleType_Float; - bgl_entries[1].texture.viewDimension = WGPUTextureViewDimension_2D; - bgl_entries[2].binding = PP_BINDING_UNIFORMS; - bgl_entries[2].visibility = WGPUShaderStage_Vertex | WGPUShaderStage_Fragment; - bgl_entries[2].buffer.type = WGPUBufferBindingType_Uniform; - - // Add an entry for effect-specific parameters - bgl_entries[3].binding = PP_BINDING_EFFECT_PARAMS; - bgl_entries[3].visibility = WGPUShaderStage_Fragment; - bgl_entries[3].buffer.type = WGPUBufferBindingType_Uniform; - - WGPUBindGroupLayoutDescriptor bgl_desc = {}; - bgl_desc.entryCount = 4; - bgl_desc.entries = bgl_entries; - WGPUBindGroupLayout bgl = wgpuDeviceCreateBindGroupLayout(device, &bgl_desc); - - WGPUPipelineLayoutDescriptor pl_desc = {}; - pl_desc.bindGroupLayoutCount = 1; - pl_desc.bindGroupLayouts = &bgl; - WGPUPipelineLayout pl = wgpuDeviceCreatePipelineLayout(device, &pl_desc); - - WGPUColorTargetState color_target = {}; - color_target.format = format; - color_target.writeMask = WGPUColorWriteMask_All; - - WGPUFragmentState fragment_state = {}; - fragment_state.module = shader_module; - fragment_state.entryPoint = str_view("fs_main"); - fragment_state.targetCount = 1; - fragment_state.targets = &color_target; - - WGPURenderPipelineDescriptor pipeline_desc = {}; - pipeline_desc.layout = pl; - pipeline_desc.vertex.module = shader_module; - pipeline_desc.vertex.entryPoint = str_view("vs_main"); - pipeline_desc.fragment = &fragment_state; - pipeline_desc.primitive.topology = WGPUPrimitiveTopology_TriangleList; - pipeline_desc.multisample.count = 1; - pipeline_desc.multisample.mask = 0xFFFFFFFF; - - return wgpuDeviceCreateRenderPipeline(device, &pipeline_desc); + wgpuBindGroupLayoutRelease(bgl); + return pipeline; } // --- PostProcess Implementation Helper --- @@ -82,25 +43,16 @@ void pp_update_bind_group(WGPUDevice device, WGPURenderPipeline pipeline, if (*bind_group) wgpuBindGroupRelease(*bind_group); + WGPUBindGroupLayout bgl = wgpuRenderPipelineGetBindGroupLayout(pipeline, 0); - WGPUSamplerDescriptor sd = {}; - sd.magFilter = WGPUFilterMode_Linear; - sd.minFilter = WGPUFilterMode_Linear; - sd.maxAnisotropy = 1; - WGPUSampler sampler = wgpuDeviceCreateSampler(device, &sd); - WGPUBindGroupEntry bge[4] = {}; - bge[0].binding = PP_BINDING_SAMPLER; - bge[0].sampler = sampler; - bge[1].binding = PP_BINDING_TEXTURE; - bge[1].textureView = input_view; - bge[2].binding = PP_BINDING_UNIFORMS; - bge[2].buffer = uniforms.buffer; - bge[2].size = uniforms.size; - bge[3].binding = PP_BINDING_EFFECT_PARAMS; - bge[3].buffer = - effect_params.buffer ? effect_params.buffer : g_dummy_buffer.buffer; - bge[3].size = effect_params.buffer ? effect_params.size : g_dummy_buffer.size; - WGPUBindGroupDescriptor bgd = { - .layout = bgl, .entryCount = 4, .entries = bge}; - *bind_group = wgpuDeviceCreateBindGroup(device, &bgd); + WGPUSampler sampler = SamplerCache::Get().get_or_create(device, SamplerCache::linear()); + + *bind_group = BindGroupBuilder() + .sampler(PP_BINDING_SAMPLER, sampler) + .texture(PP_BINDING_TEXTURE, input_view) + .buffer(PP_BINDING_UNIFORMS, uniforms.buffer, uniforms.size) + .buffer(PP_BINDING_EFFECT_PARAMS, + effect_params.buffer ? effect_params.buffer : g_dummy_buffer.buffer, + effect_params.buffer ? effect_params.size : g_dummy_buffer.size) + .build(device, bgl); } |
