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/rotating_cube_effect.cc | 69 ++++++++++----------------------- 1 file changed, 20 insertions(+), 49 deletions(-) (limited to 'src/gpu/effects/rotating_cube_effect.cc') diff --git a/src/gpu/effects/rotating_cube_effect.cc b/src/gpu/effects/rotating_cube_effect.cc index cd31100..96b02f1 100644 --- a/src/gpu/effects/rotating_cube_effect.cc +++ b/src/gpu/effects/rotating_cube_effect.cc @@ -4,7 +4,9 @@ #include "gpu/effects/rotating_cube_effect.h" #include "generated/assets.h" +#include "gpu/bind_group_builder.h" #include "gpu/effects/shader_composer.h" +#include "gpu/gpu.h" #include "gpu/sampler_cache.h" #include "util/asset_manager_utils.h" @@ -35,17 +37,11 @@ void RotatingCubeEffect::init(MainSequence* demo) { gpu_create_buffer(ctx_.device, sizeof(ObjectData), WGPUBufferUsage_Storage | WGPUBufferUsage_CopyDst); - const WGPUTextureDescriptor tex_desc = { - .usage = - WGPUTextureUsage_TextureBinding | WGPUTextureUsage_RenderAttachment, - .dimension = WGPUTextureDimension_2D, - .size = {1, 1, 1}, - .format = WGPUTextureFormat_RGBA8Unorm, - .mipLevelCount = 1, - .sampleCount = 1, - }; - noise_texture_ = wgpuDeviceCreateTexture(ctx_.device, &tex_desc); - noise_view_ = wgpuTextureCreateView(noise_texture_, nullptr); + TextureWithView noise = gpu_create_texture_2d( + ctx_.device, 1, 1, WGPUTextureFormat_RGBA8Unorm, + WGPUTextureUsage_TextureBinding | WGPUTextureUsage_RenderAttachment, 1); + noise_texture_ = noise.texture; + noise_view_ = noise.view; noise_sampler_ = SamplerCache::Get().get_or_create(ctx_.device, SamplerCache::linear()); mask_sampler_ = SamplerCache::Get().get_or_create(ctx_.device, SamplerCache::clamp()); @@ -68,45 +64,20 @@ void RotatingCubeEffect::init(MainSequence* demo) { WGPUShaderModule shader_module = wgpuDeviceCreateShaderModule(ctx_.device, &shader_desc); - const WGPUBindGroupLayoutEntry bgl_entries_0[] = { - {.binding = 0, - .visibility = WGPUShaderStage_Vertex | WGPUShaderStage_Fragment, - .buffer = {.type = WGPUBufferBindingType_Uniform, - .minBindingSize = sizeof(Uniforms)}}, - {.binding = 1, - .visibility = WGPUShaderStage_Vertex | WGPUShaderStage_Fragment, - .buffer = {.type = WGPUBufferBindingType_ReadOnlyStorage, - .minBindingSize = sizeof(ObjectData)}}, - {.binding = 3, - .visibility = WGPUShaderStage_Fragment, - .texture = {.sampleType = WGPUTextureSampleType_Float, - .viewDimension = WGPUTextureViewDimension_2D}}, - {.binding = 4, - .visibility = WGPUShaderStage_Fragment, - .sampler = {.type = WGPUSamplerBindingType_Filtering}}, - }; - const WGPUBindGroupLayoutDescriptor bgl_desc_0 = { - .entryCount = 4, - .entries = bgl_entries_0, - }; WGPUBindGroupLayout bgl_0 = - wgpuDeviceCreateBindGroupLayout(ctx_.device, &bgl_desc_0); - - const WGPUBindGroupLayoutEntry bgl_entries_1[] = { - {.binding = 0, - .visibility = WGPUShaderStage_Fragment, - .texture = {.sampleType = WGPUTextureSampleType_Float, - .viewDimension = WGPUTextureViewDimension_2D}}, - {.binding = 1, - .visibility = WGPUShaderStage_Fragment, - .sampler = {.type = WGPUSamplerBindingType_Filtering}}, - }; - const WGPUBindGroupLayoutDescriptor bgl_desc_1 = { - .entryCount = 2, - .entries = bgl_entries_1, - }; - WGPUBindGroupLayout bgl_1 = - wgpuDeviceCreateBindGroupLayout(ctx_.device, &bgl_desc_1); + BindGroupLayoutBuilder() + .uniform(0, WGPUShaderStage_Vertex | WGPUShaderStage_Fragment, + sizeof(Uniforms)) + .storage(1, WGPUShaderStage_Vertex | WGPUShaderStage_Fragment, + sizeof(ObjectData)) + .texture(3, WGPUShaderStage_Fragment) + .sampler(4, WGPUShaderStage_Fragment) + .build(ctx_.device); + + WGPUBindGroupLayout bgl_1 = BindGroupLayoutBuilder() + .texture(0, WGPUShaderStage_Fragment) + .sampler(1, WGPUShaderStage_Fragment) + .build(ctx_.device); const WGPUBindGroupLayout bgls[] = {bgl_0, bgl_1}; const WGPUPipelineLayoutDescriptor pl_desc = { -- cgit v1.2.3