diff options
| author | skal <pascal.massimino@gmail.com> | 2026-05-20 22:44:44 +0200 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-05-20 22:44:44 +0200 |
| commit | 5d20c892dedce7bc7486acbd72fbd35da69e413e (patch) | |
| tree | 05e04d5e689504c2421cd5772e91a42ee69608ab /src/gpu/gpu.cc | |
| parent | 6ef8f578817ee0134fd5867ca3b80590e3eb2368 (diff) | |
fix: code review cleanup — bugs, dead code, factorization (-167 lines)
Bugs:
- B1: fix dead tempo debug (prev_tempo captured after assignment)
- B2: fix ReloadAssetsFromFile leak for disk-loaded assets; simplify DropAsset
- B3: fix get_free_pool_slot leak (unregister synth + free data on reuse)
- B4: volatile -> std::atomic with acquire/release in miniaudio_backend, synth
- B5: fix unaligned reads in scene_loader (memcpy-based read_f32/read_u32)
- B6: fix shader module + BGL + pipeline layout leaks in gpu.cc, pipeline_builder
Dead code:
- D1: remove unused particle_defs.h
- D3: remove create_post_process_pipeline_simple (zero callers)
- D4: remove empty gpu_draw()
- D5: remove write-only Hybrid3D::initialized_
- D6: remove legacy pending buffer path in audio.cc
Factorization:
- F1: Effect::run_fullscreen_pass() replaces boilerplate in 5 effects
- F2: particle_common.wgsl snippet, #include in 3 WGSL shaders
- F3: gpu_create_shader_module() helper, used in 3 call sites
- F5: get_world_aabb() shared between bvh.cc and physics.cc
- F6: samples_to_seconds() replaces 6 inline expressions
- F7: gpu_create_linear/nearest_sampler use SamplerCache; add nearest() preset
37/37 tests passing.
handoff(Claude): code review batch — all items verified, no regressions.
Diffstat (limited to 'src/gpu/gpu.cc')
| -rw-r--r-- | src/gpu/gpu.cc | 71 |
1 files changed, 30 insertions, 41 deletions
diff --git a/src/gpu/gpu.cc b/src/gpu/gpu.cc index 0be5afe..f5f1515 100644 --- a/src/gpu/gpu.cc +++ b/src/gpu/gpu.cc @@ -5,6 +5,7 @@ #include "gpu.h" #include "effects/shaders.h" #include "generated/timeline.h" +#include "gpu/sampler_cache.h" #include "gpu/shader_composer.h" #include "platform/platform.h" @@ -124,26 +125,28 @@ WGPUTextureView gpu_create_texture_view_2d(WGPUTexture texture, return wgpuTextureCreateView(texture, &view_desc); } +WGPUShaderModule gpu_create_shader_module(WGPUDevice device, + const char* wgsl_code, + const char* label) { + WGPUShaderSourceWGSL wgsl_src = {}; + wgsl_src.chain.sType = WGPUSType_ShaderSourceWGSL; + wgsl_src.code = str_view(wgsl_code); + WGPUShaderModuleDescriptor desc = {}; + desc.label = label_view(label); + desc.nextInChain = &wgsl_src.chain; + return wgpuDeviceCreateShaderModule(device, &desc); +} + WGPUSampler gpu_create_linear_sampler(WGPUDevice device) { - WGPUSamplerDescriptor desc = {}; - desc.addressModeU = WGPUAddressMode_ClampToEdge; - desc.addressModeV = WGPUAddressMode_ClampToEdge; - desc.addressModeW = WGPUAddressMode_ClampToEdge; - desc.magFilter = WGPUFilterMode_Linear; - desc.minFilter = WGPUFilterMode_Linear; - desc.mipmapFilter = WGPUMipmapFilterMode_Nearest; - desc.maxAnisotropy = 1; - return wgpuDeviceCreateSampler(device, &desc); + WGPUSampler s = SamplerCache::Get().get_or_create(device, SamplerCache::clamp()); + if (s) wgpuSamplerAddRef(s); // Caller owns a reference (for RAII wrappers) + return s; } WGPUSampler gpu_create_nearest_sampler(WGPUDevice device) { - WGPUSamplerDescriptor desc = {}; - desc.addressModeU = WGPUAddressMode_ClampToEdge; - desc.addressModeV = WGPUAddressMode_ClampToEdge; - desc.magFilter = WGPUFilterMode_Nearest; - desc.minFilter = WGPUFilterMode_Nearest; - desc.maxAnisotropy = 1; - return wgpuDeviceCreateSampler(device, &desc); + WGPUSampler s = SamplerCache::Get().get_or_create(device, SamplerCache::nearest()); + if (s) wgpuSamplerAddRef(s); // Caller owns a reference (for RAII wrappers) + return s; } TextureWithView gpu_create_dummy_scene_texture(WGPUDevice device) { @@ -167,15 +170,8 @@ RenderPass gpu_create_render_pass(WGPUDevice device, WGPUTextureFormat format, // 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(composed_shader.c_str()); - WGPUShaderModuleDescriptor shader_desc = {}; - shader_desc.label = label_view("render_shader"); - shader_desc.nextInChain = &wgsl_src.chain; WGPUShaderModule shader_module = - wgpuDeviceCreateShaderModule(device, &shader_desc); + gpu_create_shader_module(device, composed_shader.c_str(), "render_shader"); // Create Bind Group Layout & Bind Group std::vector<WGPUBindGroupLayoutEntry> bgl_entries; @@ -257,6 +253,10 @@ RenderPass gpu_create_render_pass(WGPUDevice device, WGPUTextureFormat format, pass.pipeline = wgpuDeviceCreateRenderPipeline(device, &pipeline_desc); + wgpuShaderModuleRelease(shader_module); + wgpuPipelineLayoutRelease(pipeline_layout); + wgpuBindGroupLayoutRelease(bind_group_layout); + return pass; } @@ -268,14 +268,8 @@ ComputePass gpu_create_compute_pass(WGPUDevice device, const char* shader_code, // 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(composed_shader.c_str()); - WGPUShaderModuleDescriptor shader_desc = {}; - shader_desc.label = label_view("compute_shader"); - shader_desc.nextInChain = &wgsl_src.chain; WGPUShaderModule shader_module = - wgpuDeviceCreateShaderModule(device, &shader_desc); + gpu_create_shader_module(device, composed_shader.c_str(), "compute_shader"); std::vector<WGPUBindGroupLayoutEntry> bgl_entries; std::vector<WGPUBindGroupEntry> bg_entries; @@ -319,6 +313,11 @@ ComputePass gpu_create_compute_pass(WGPUDevice device, const char* shader_code, pipeline_desc.compute.entryPoint = str_view("main"); pass.pipeline = wgpuDeviceCreateComputePipeline(device, &pipeline_desc); + + wgpuShaderModuleRelease(shader_module); + wgpuPipelineLayoutRelease(pipeline_layout); + wgpuBindGroupLayoutRelease(bind_group_layout); + return pass; } @@ -439,16 +438,6 @@ void gpu_init(PlatformState* platform_state) { } -void gpu_draw(float audio_peak, float aspect_ratio, float time, float beat_time, - float beat_phase) { - // Rendering is driven externally via the sequence pipeline - (void)audio_peak; - (void)aspect_ratio; - (void)time; - (void)beat_time; - (void)beat_phase; -} - void gpu_resize(int width, int height) { if (width <= 0 || height <= 0) return; |
