From c4cfc8459dbc6fde74d5553519dc3fcb1afccad0 Mon Sep 17 00:00:00 2001 From: skal Date: Sat, 14 Feb 2026 12:38:59 +0100 Subject: Refactor: factorize common WGPU patterns into helper functions Add texture creation helpers (gpu_create_texture_2d, gpu_create_storage_texture_2d, gpu_create_mip_view) and extend BindGroupLayoutBuilder with uint_texture and storage_texture methods. Refactored files: - cnn_v2_effect.cc: Use texture helpers (~70% code reduction in create_textures) - rotating_cube_effect.cc: Use BindGroupLayoutBuilder and texture helpers - circle_mask_effect.cc: Use BindGroupBuilder Benefits: - Improved code readability - Reduced boilerplate for texture/bind group creation - Consistent patterns across effects Co-Authored-By: Claude Sonnet 4.5 --- src/gpu/effects/circle_mask_effect.cc | 46 ++++++++++++++--------------------- 1 file changed, 18 insertions(+), 28 deletions(-) (limited to 'src/gpu/effects/circle_mask_effect.cc') diff --git a/src/gpu/effects/circle_mask_effect.cc b/src/gpu/effects/circle_mask_effect.cc index f34ffb7..dfe7d03 100644 --- a/src/gpu/effects/circle_mask_effect.cc +++ b/src/gpu/effects/circle_mask_effect.cc @@ -3,8 +3,9 @@ // Generates circular mask and renders green background outside circle. #include "gpu/effects/circle_mask_effect.h" -#include "gpu/effects/shader_composer.h" #include "generated/assets.h" +#include "gpu/bind_group_builder.h" +#include "gpu/effects/shader_composer.h" CircleMaskEffect::CircleMaskEffect(const GpuContext& ctx, float radius) : Effect(ctx), radius_(radius) { @@ -83,21 +84,14 @@ void CircleMaskEffect::init(MainSequence* demo) { wgpuDeviceCreateRenderPipeline(ctx_.device, &compute_pipeline_desc); wgpuShaderModuleRelease(compute_module); - const WGPUBindGroupEntry compute_entries[] = { - {.binding = 0, - .buffer = uniforms_.get().buffer, - .size = sizeof(CommonPostProcessUniforms)}, - {.binding = 1, - .buffer = compute_params_.get().buffer, - .size = sizeof(CircleMaskParams)}, - }; - const WGPUBindGroupDescriptor compute_bg_desc = { - .layout = wgpuRenderPipelineGetBindGroupLayout(compute_pipeline_, 0), - .entryCount = 2, - .entries = compute_entries, - }; + WGPUBindGroupLayout compute_layout = + wgpuRenderPipelineGetBindGroupLayout(compute_pipeline_, 0); compute_bind_group_ = - wgpuDeviceCreateBindGroup(ctx_.device, &compute_bg_desc); + BindGroupBuilder() + .buffer(0, uniforms_.get().buffer, sizeof(CommonPostProcessUniforms)) + .buffer(1, compute_params_.get().buffer, sizeof(CircleMaskParams)) + .build(ctx_.device, compute_layout); + wgpuBindGroupLayoutRelease(compute_layout); std::string composed_render = ShaderComposer::Get().Compose({}, render_shader); @@ -172,19 +166,15 @@ void CircleMaskEffect::resize(int width, int height) { wgpuBindGroupRelease(render_bind_group_); WGPUTextureView mask_view = demo_->get_auxiliary_view("circle_mask"); - const WGPUBindGroupEntry render_entries[] = { - {.binding = 0, .textureView = mask_view}, - {.binding = 1, .sampler = mask_sampler_}, - {.binding = 2, - .buffer = uniforms_.get().buffer, - .size = sizeof(CommonPostProcessUniforms)}, - }; - const WGPUBindGroupDescriptor render_bg_desc = { - .layout = wgpuRenderPipelineGetBindGroupLayout(render_pipeline_, 0), - .entryCount = 3, - .entries = render_entries, - }; - render_bind_group_ = wgpuDeviceCreateBindGroup(ctx_.device, &render_bg_desc); + WGPUBindGroupLayout render_layout = + wgpuRenderPipelineGetBindGroupLayout(render_pipeline_, 0); + render_bind_group_ = + BindGroupBuilder() + .texture(0, mask_view) + .sampler(1, mask_sampler_) + .buffer(2, uniforms_.get().buffer, sizeof(CommonPostProcessUniforms)) + .build(ctx_.device, render_layout); + wgpuBindGroupLayoutRelease(render_layout); } void CircleMaskEffect::compute(WGPUCommandEncoder encoder, -- cgit v1.2.3