From 1b89a26a750cc32725564a0c5186a437f372fd93 Mon Sep 17 00:00:00 2001 From: skal Date: Sun, 8 Feb 2026 22:17:31 +0100 Subject: feat: Fix CircleMaskEffect and RotatingCubeEffect auxiliary texture masking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolves critical shader composition and format mismatch issues, enabling the circle mask + rotating cube demonstration at 2.0s-4.0s in the timeline. **Key Fixes:** 1. **Shader Function Name** (masked_cube.wgsl): - Fixed call to `ray_box_intersection()` (was incorrectly `ray_box()`) - Updated return value handling to use `RayBounds` struct (`.hit`, `.t_entry`, `.t_exit`) - Removed unused `sky_tex` binding to match pipeline layout expectations (5 bindings → 4) 2. **Shader Lifetime Issue** (rotating_cube_effect.h/cc): - Added `std::string composed_shader_` member to persist shader source - Prevents use-after-free when WebGPU asynchronously parses shader code 3. **Format Mismatch** (circle_mask_effect.cc): - Changed compute pipeline format from hardcoded `RGBA8Unorm` to `ctx_.format` (Bgra8UnormSrgb) - Matches auxiliary texture format created by `MainSequence::register_auxiliary_texture()` - Added depth stencil state to render pipeline to match scene pass requirements: * Format: Depth24Plus * depthWriteEnabled: False (no depth writes needed) * depthCompare: Always (always pass depth test) 4. **WebGPU Descriptor Initialization** (circle_mask_effect.cc): - Added `depthSlice = WGPU_DEPTH_SLICE_UNDEFINED` for non-Windows builds - Ensures proper initialization of render pass color attachments 5. **Test Coverage** (test_demo_effects.cc): - Updated EXPECTED_SCENE_COUNT: 6 → 8 (added CircleMaskEffect, RotatingCubeEffect) - Marked both effects as requiring 3D pipeline setup (skipped in basic tests) **Technical Details:** - **Auxiliary Texture Flow**: CircleMaskEffect generates mask (1.0 inside, 0.0 outside) → RotatingCubeEffect samples mask to render only inside circle → GaussianBlurEffect post-processes - **Pipeline Format Matching**: All pipelines targeting same render pass must use matching formats: * Color: Bgra8UnormSrgb (system framebuffer format) * Depth: Depth24Plus (scene pass depth buffer) - **ShaderComposer Integration**: Relies on `InitShaderComposer()` (called in `gpu.cc:372`) registering snippets: `common_uniforms`, `math/sdf_utils`, `ray_box`, etc. **Effect Behavior:** - Runs from 2.0s to 4.0s in demo timeline - CircleMaskEffect (priority 0): Draws green outside circle, transparent inside - RotatingCubeEffect (priority 1): Renders bump-mapped cube inside circle - GaussianBlurEffect (priority 2): Post-process blur on entire composition **Test Results:** - All 33 tests pass (100%) - No WebGPU validation errors - Demo runs cleanly with `--seek 2.0` Co-Authored-By: Claude Sonnet 4.5 --- src/gpu/effects/circle_mask_effect.cc | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'src/gpu/effects/circle_mask_effect.cc') diff --git a/src/gpu/effects/circle_mask_effect.cc b/src/gpu/effects/circle_mask_effect.cc index 55bcb90..226b603 100644 --- a/src/gpu/effects/circle_mask_effect.cc +++ b/src/gpu/effects/circle_mask_effect.cc @@ -51,7 +51,7 @@ void CircleMaskEffect::init(MainSequence* demo) { WGPUShaderModule compute_module = wgpuDeviceCreateShaderModule(ctx_.device, &compute_desc); const WGPUColorTargetState compute_target = { - .format = WGPUTextureFormat_RGBA8Unorm, + .format = ctx_.format, // Match auxiliary texture format .writeMask = WGPUColorWriteMask_All, }; WGPUFragmentState compute_frag = {}; @@ -60,6 +60,7 @@ void CircleMaskEffect::init(MainSequence* demo) { compute_frag.targetCount = 1; compute_frag.targets = &compute_target; WGPURenderPipelineDescriptor compute_pipeline_desc = {}; + compute_pipeline_desc.label = label_view("CircleMaskEffect_compute"); compute_pipeline_desc.vertex.module = compute_module; compute_pipeline_desc.vertex.entryPoint = str_view("vs_main"); compute_pipeline_desc.primitive.topology = WGPUPrimitiveTopology_TriangleList; @@ -98,11 +99,19 @@ void CircleMaskEffect::init(MainSequence* demo) { render_frag.entryPoint = str_view("fs_main"); render_frag.targetCount = 1; render_frag.targets = &render_target; + const WGPUDepthStencilState depth_stencil = { + .format = WGPUTextureFormat_Depth24Plus, + .depthWriteEnabled = WGPUOptionalBool_False, // Don't write depth + .depthCompare = WGPUCompareFunction_Always, // Always pass + }; + WGPURenderPipelineDescriptor render_pipeline_desc = {}; + render_pipeline_desc.label = label_view("CircleMaskEffect_render"); render_pipeline_desc.vertex.module = render_module; render_pipeline_desc.vertex.entryPoint = str_view("vs_main"); render_pipeline_desc.primitive.topology = WGPUPrimitiveTopology_TriangleList; render_pipeline_desc.primitive.cullMode = WGPUCullMode_None; + render_pipeline_desc.depthStencil = &depth_stencil; render_pipeline_desc.multisample.count = 1; render_pipeline_desc.multisample.mask = 0xFFFFFFFF; render_pipeline_desc.fragment = &render_frag; @@ -139,16 +148,18 @@ void CircleMaskEffect::compute(WGPUCommandEncoder encoder, float time, compute_uniforms_.update(ctx_.queue, uniforms); WGPUTextureView mask_view = demo_->get_auxiliary_view("circle_mask"); - const WGPURenderPassColorAttachment color_attachment = { - .view = mask_view, - .loadOp = WGPULoadOp_Clear, - .storeOp = WGPUStoreOp_Store, - .clearValue = {0.0, 0.0, 0.0, 1.0}, - }; - const WGPURenderPassDescriptor pass_desc = { - .colorAttachmentCount = 1, - .colorAttachments = &color_attachment, - }; + WGPURenderPassColorAttachment color_attachment = {}; + color_attachment.view = mask_view; + color_attachment.loadOp = WGPULoadOp_Clear; + color_attachment.storeOp = WGPUStoreOp_Store; + color_attachment.clearValue = {0.0, 0.0, 0.0, 1.0}; +#if !defined(DEMO_CROSS_COMPILE_WIN32) + color_attachment.depthSlice = WGPU_DEPTH_SLICE_UNDEFINED; +#endif + + WGPURenderPassDescriptor pass_desc = {}; + pass_desc.colorAttachmentCount = 1; + pass_desc.colorAttachments = &color_attachment; WGPURenderPassEncoder pass = wgpuCommandEncoderBeginRenderPass(encoder, &pass_desc); wgpuRenderPassEncoderSetPipeline(pass, compute_pipeline_); -- cgit v1.2.3