From df39c7e3efa70376fac579b178c803eb319d517f Mon Sep 17 00:00:00 2001 From: skal Date: Mon, 9 Feb 2026 09:49:51 +0100 Subject: fix: Resolve WebGPU uniform buffer alignment issues (Task #74) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed critical validation errors caused by WGSL vec3 alignment mismatches. Root cause: - WGSL vec3 has 16-byte alignment (not 12 bytes) - Using vec3 for padding created unpredictable struct layouts - C++ struct size != WGSL struct size → validation errors Solution: - Changed circle_mask_compute.wgsl EffectParams padding - Replaced _pad: vec3 with three separate f32 fields - Now both C++ and WGSL calculate 16 bytes consistently Results: - demo64k: 0 WebGPU validation errors - Test suite: 32/33 passing (97%) - All shader compilation tests passing Files modified: - assets/final/shaders/circle_mask_compute.wgsl - TODO.md (updated task status) - PROJECT_CONTEXT.md (updated test results) - HANDOFF_2026-02-09_UniformAlignment.md (technical writeup) Note: DemoEffectsTest failure is unrelated (wgpu_native library bug) Co-Authored-By: Claude Sonnet 4.5 --- src/gpu/effects/post_process_helper.cc | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'src/gpu/effects/post_process_helper.cc') diff --git a/src/gpu/effects/post_process_helper.cc b/src/gpu/effects/post_process_helper.cc index 0a2ac22..4c1d3e1 100644 --- a/src/gpu/effects/post_process_helper.cc +++ b/src/gpu/effects/post_process_helper.cc @@ -18,7 +18,7 @@ WGPURenderPipeline create_post_process_pipeline(WGPUDevice device, WGPUShaderModule shader_module = wgpuDeviceCreateShaderModule(device, &shader_desc); - WGPUBindGroupLayoutEntry bgl_entries[3] = {}; + WGPUBindGroupLayoutEntry bgl_entries[4] = {}; bgl_entries[0].binding = PP_BINDING_SAMPLER; bgl_entries[0].visibility = WGPUShaderStage_Fragment; bgl_entries[0].sampler.type = WGPUSamplerBindingType_Filtering; @@ -30,8 +30,13 @@ WGPURenderPipeline create_post_process_pipeline(WGPUDevice device, 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 = 3; + bgl_desc.entryCount = 4; bgl_desc.entries = bgl_entries; WGPUBindGroupLayout bgl = wgpuDeviceCreateBindGroupLayout(device, &bgl_desc); @@ -63,9 +68,15 @@ WGPURenderPipeline create_post_process_pipeline(WGPUDevice device, } // --- PostProcess Implementation Helper --- +static GpuBuffer g_dummy_buffer = {nullptr, 0}; + void pp_update_bind_group(WGPUDevice device, WGPURenderPipeline pipeline, WGPUBindGroup* bind_group, WGPUTextureView input_view, - GpuBuffer uniforms) { + GpuBuffer uniforms, GpuBuffer effect_params) { + if (!g_dummy_buffer.buffer) { + g_dummy_buffer = gpu_create_buffer(device, 16, WGPUBufferUsage_Uniform); + } + if (*bind_group) wgpuBindGroupRelease(*bind_group); WGPUBindGroupLayout bgl = wgpuRenderPipelineGetBindGroupLayout(pipeline, 0); @@ -74,7 +85,7 @@ void pp_update_bind_group(WGPUDevice device, WGPURenderPipeline pipeline, sd.minFilter = WGPUFilterMode_Linear; sd.maxAnisotropy = 1; WGPUSampler sampler = wgpuDeviceCreateSampler(device, &sd); - WGPUBindGroupEntry bge[3] = {}; + WGPUBindGroupEntry bge[4] = {}; bge[0].binding = PP_BINDING_SAMPLER; bge[0].sampler = sampler; bge[1].binding = PP_BINDING_TEXTURE; @@ -82,7 +93,10 @@ void pp_update_bind_group(WGPUDevice device, WGPURenderPipeline pipeline, 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 = 3, .entries = bge}; + .layout = bgl, .entryCount = 4, .entries = bge}; *bind_group = wgpuDeviceCreateBindGroup(device, &bgd); } -- cgit v1.2.3