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/tests/test_demo_effects.cc | 22 +++++++++++----------- src/tests/test_noise_functions.cc | 28 +++++++++++++++------------- src/tests/test_post_process_helper.cc | 27 +++++++++++++++++++++++---- 3 files changed, 49 insertions(+), 28 deletions(-) (limited to 'src/tests') diff --git a/src/tests/test_demo_effects.cc b/src/tests/test_demo_effects.cc index e6b794b..d0163c2 100644 --- a/src/tests/test_demo_effects.cc +++ b/src/tests/test_demo_effects.cc @@ -121,17 +121,17 @@ static void test_scene_effects() { // Test each scene effect std::vector>> effects = { - // {"HeptagonEffect", std::make_shared(fixture.ctx())}, - // {"ParticlesEffect", std::make_shared(fixture.ctx())}, - // {"ParticleSprayEffect", - // std::make_shared(fixture.ctx())}, - // {"MovingEllipseEffect", - // std::make_shared(fixture.ctx())}, - // {"FlashCubeEffect", std::make_shared(fixture.ctx())}, - // {"Hybrid3DEffect", std::make_shared(fixture.ctx())}, - // {"CircleMaskEffect", std::make_shared(fixture.ctx())}, - // {"RotatingCubeEffect", - // std::make_shared(fixture.ctx())}, + {"HeptagonEffect", std::make_shared(fixture.ctx())}, + {"ParticlesEffect", std::make_shared(fixture.ctx())}, + {"ParticleSprayEffect", + std::make_shared(fixture.ctx())}, + {"MovingEllipseEffect", + std::make_shared(fixture.ctx())}, + {"FlashCubeEffect", std::make_shared(fixture.ctx())}, + {"Hybrid3DEffect", std::make_shared(fixture.ctx())}, + {"CircleMaskEffect", std::make_shared(fixture.ctx())}, + {"RotatingCubeEffect", + std::make_shared(fixture.ctx())}, }; int passed = 0; diff --git a/src/tests/test_noise_functions.cc b/src/tests/test_noise_functions.cc index bdb42c9..f8dfc93 100644 --- a/src/tests/test_noise_functions.cc +++ b/src/tests/test_noise_functions.cc @@ -11,7 +11,8 @@ // Test that noise shader can be loaded and composed static bool test_noise_shader_loading() { - const char* noise_shader = (const char*)GetAsset(AssetId::ASSET_SHADER_MATH_NOISE); + const char* noise_shader = + (const char*)GetAsset(AssetId::ASSET_SHADER_MATH_NOISE); if (!noise_shader) { fprintf(stderr, "FAILED: Could not load noise shader asset\n"); return false; @@ -19,17 +20,17 @@ static bool test_noise_shader_loading() { // Check for key function signatures const char* expected_funcs[] = { - "fn hash_1f(x: f32) -> f32", - "fn hash_2f(p: vec2) -> f32", - "fn hash_3f(p: vec3) -> f32", - "fn hash_2f_2f(p: vec2) -> vec2", - "fn hash_3f_3f(p: vec3) -> vec3", - "fn hash_1u(p: u32) -> f32", - "fn noise_2d(p: vec2) -> f32", - "fn noise_3d(p: vec3) -> f32", - "fn gyroid(p: vec3) -> f32", - "fn fbm_2d(p: vec2, octaves: i32) -> f32", - "fn fbm_3d(p: vec3, octaves: i32) -> f32", + "fn hash_1f(x: f32) -> f32", + "fn hash_2f(p: vec2) -> f32", + "fn hash_3f(p: vec3) -> f32", + "fn hash_2f_2f(p: vec2) -> vec2", + "fn hash_3f_3f(p: vec3) -> vec3", + "fn hash_1u(p: u32) -> f32", + "fn noise_2d(p: vec2) -> f32", + "fn noise_3d(p: vec3) -> f32", + "fn gyroid(p: vec3) -> f32", + "fn fbm_2d(p: vec2, octaves: i32) -> f32", + "fn fbm_3d(p: vec3, octaves: i32) -> f32", }; int func_count = sizeof(expected_funcs) / sizeof(expected_funcs[0]); @@ -50,7 +51,8 @@ static bool test_noise_composition() { // Debug: Check if noise asset can be loaded size_t noise_size = 0; - const char* noise_data = (const char*)GetAsset(AssetId::ASSET_SHADER_MATH_NOISE, &noise_size); + const char* noise_data = + (const char*)GetAsset(AssetId::ASSET_SHADER_MATH_NOISE, &noise_size); if (!noise_data) { fprintf(stderr, "FAILED: Could not load ASSET_SHADER_MATH_NOISE\n"); return false; diff --git a/src/tests/test_post_process_helper.cc b/src/tests/test_post_process_helper.cc index 7078c6e..104bbc3 100644 --- a/src/tests/test_post_process_helper.cc +++ b/src/tests/test_post_process_helper.cc @@ -61,6 +61,7 @@ fn vs_main(@builtin(vertex_index) vid: u32) -> @builtin(position) vec4 { @group(0) @binding(0) var input_sampler: sampler; @group(0) @binding(1) var input_texture: texture_2d; @group(0) @binding(2) var uniforms: vec4; +@group(0) @binding(3) var effect_params: vec4; // Dummy for testing @fragment fn fs_main(@builtin(position) pos: vec4) -> @location(0) vec4 { @@ -122,10 +123,15 @@ static void test_bind_group_creation() { GpuBuffer uniforms = {uniform_buffer, 16}; + // Dummy effect params buffer for testing (matches vec4) + WGPUBuffer dummy_params_buffer_handle = + wgpuDeviceCreateBuffer(fixture.device(), &uniform_desc); + GpuBuffer dummy_effect_params_buffer = {dummy_params_buffer_handle, 16}; + // Test bind group creation WGPUBindGroup bind_group = nullptr; pp_update_bind_group(fixture.device(), pipeline, &bind_group, input_view, - uniforms); + uniforms, dummy_effect_params_buffer); assert(bind_group != nullptr && "Bind group should be created successfully"); fprintf(stdout, " ✓ Bind group created successfully\n"); @@ -135,6 +141,7 @@ static void test_bind_group_creation() { wgpuTextureViewRelease(input_view); wgpuTextureRelease(input_texture); wgpuBufferRelease(uniform_buffer); + wgpuBufferRelease(dummy_params_buffer_handle); wgpuRenderPipelineRelease(pipeline); fprintf(stdout, " ✓ Resources released\n"); } @@ -168,16 +175,21 @@ static void test_bind_group_update() { wgpuDeviceCreateBuffer(fixture.device(), &uniform_desc); GpuBuffer uniforms = {uniform_buffer, 16}; + // Dummy effect params buffer for testing (matches vec4) + WGPUBuffer dummy_params_buffer_handle = + wgpuDeviceCreateBuffer(fixture.device(), &uniform_desc); + GpuBuffer dummy_effect_params_buffer = {dummy_params_buffer_handle, 16}; + // Create initial bind group WGPUBindGroup bind_group = nullptr; pp_update_bind_group(fixture.device(), pipeline, &bind_group, view1, - uniforms); + uniforms, dummy_effect_params_buffer); assert(bind_group != nullptr && "Initial bind group should be created"); fprintf(stdout, " ✓ Initial bind group created\n"); // Update bind group (should release old and create new) pp_update_bind_group(fixture.device(), pipeline, &bind_group, view2, - uniforms); + uniforms, dummy_effect_params_buffer); assert(bind_group != nullptr && "Updated bind group should be created"); fprintf(stdout, " ✓ Bind group updated successfully\n"); @@ -188,6 +200,7 @@ static void test_bind_group_update() { wgpuTextureViewRelease(view2); wgpuTextureRelease(texture2); wgpuBufferRelease(uniform_buffer); + wgpuBufferRelease(dummy_params_buffer_handle); wgpuRenderPipelineRelease(pipeline); fprintf(stdout, " ✓ Resources released\n"); } @@ -225,10 +238,15 @@ static void test_full_setup() { wgpuDeviceCreateBuffer(fixture.device(), &uniform_desc); GpuBuffer uniforms = {uniform_buffer, 16}; + // Dummy effect params buffer for testing (matches vec4) + WGPUBuffer dummy_params_buffer_handle = + wgpuDeviceCreateBuffer(fixture.device(), &uniform_desc); + GpuBuffer dummy_effect_params_buffer = {dummy_params_buffer_handle, 16}; + // Create bind group WGPUBindGroup bind_group = nullptr; pp_update_bind_group(fixture.device(), pipeline, &bind_group, input_view, - uniforms); + uniforms, dummy_effect_params_buffer); assert(bind_group != nullptr && "Bind group creation failed"); fprintf(stdout, " ✓ Pipeline and bind group ready\n"); @@ -269,6 +287,7 @@ static void test_full_setup() { wgpuTextureViewRelease(input_view); wgpuTextureRelease(input_texture); wgpuBufferRelease(uniform_buffer); + wgpuBufferRelease(dummy_params_buffer_handle); wgpuRenderPipelineRelease(pipeline); fprintf(stdout, " ✓ Full setup test completed\n"); -- cgit v1.2.3