diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-14 12:38:59 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-14 12:38:59 +0100 |
| commit | c4cfc8459dbc6fde74d5553519dc3fcb1afccad0 (patch) | |
| tree | a6869d1f3c14778345ab45b9c69763d07adcca45 /src/gpu/gpu.cc | |
| parent | 0f53ed1ed8ed7c07cd7ea8e88e21b5be5d5494e5 (diff) | |
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 <noreply@anthropic.com>
Diffstat (limited to 'src/gpu/gpu.cc')
| -rw-r--r-- | src/gpu/gpu.cc | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/gpu/gpu.cc b/src/gpu/gpu.cc index 41f5bcf..cf5d85d 100644 --- a/src/gpu/gpu.cc +++ b/src/gpu/gpu.cc @@ -51,6 +51,53 @@ GpuBuffer gpu_create_buffer(WGPUDevice device, size_t size, uint32_t usage, return {buffer, size}; } +TextureWithView gpu_create_texture_2d(WGPUDevice device, uint32_t width, + uint32_t height, WGPUTextureFormat format, + WGPUTextureUsage usage, + uint32_t mip_levels) { + WGPUTextureDescriptor desc = {}; + desc.usage = usage; + desc.dimension = WGPUTextureDimension_2D; + desc.size = {width, height, 1}; + desc.format = format; + desc.mipLevelCount = mip_levels; + desc.sampleCount = 1; + + WGPUTexture texture = wgpuDeviceCreateTexture(device, &desc); + + WGPUTextureViewDescriptor view_desc = {}; + view_desc.format = format; + view_desc.dimension = WGPUTextureViewDimension_2D; + view_desc.mipLevelCount = mip_levels; + view_desc.arrayLayerCount = 1; + + WGPUTextureView view = wgpuTextureCreateView(texture, &view_desc); + + return {texture, view}; +} + +TextureWithView gpu_create_storage_texture_2d(WGPUDevice device, uint32_t width, + uint32_t height, + WGPUTextureFormat format) { + return gpu_create_texture_2d( + device, width, height, format, + WGPUTextureUsage_StorageBinding | WGPUTextureUsage_TextureBinding, 1); +} + +WGPUTextureView gpu_create_mip_view(WGPUTexture texture, + WGPUTextureFormat format, + uint32_t mip_level) { + WGPUTextureViewDescriptor view_desc = {}; + view_desc.format = format; + view_desc.dimension = WGPUTextureViewDimension_2D; + view_desc.baseMipLevel = mip_level; + view_desc.mipLevelCount = 1; + view_desc.baseArrayLayer = 0; + view_desc.arrayLayerCount = 1; + + return wgpuTextureCreateView(texture, &view_desc); +} + RenderPass gpu_create_render_pass(WGPUDevice device, WGPUTextureFormat format, const char* shader_code, ResourceBinding* bindings, int num_bindings) { |
