From fdbeddc369d4b55d2098ebdb2e9ef160c0f50368 Mon Sep 17 00:00:00 2001 From: skal Date: Tue, 3 Feb 2026 19:59:54 +0100 Subject: fix: Correct depth handling in two-pass rendering for skybox and objects - Adjusted and for both the skybox and object rendering passes in . - The skybox pass now clears the depth buffer to 1.0 and stores this cleared value. - The object pass correctly loads this pre-cleared depth, ensuring proper depth testing for all scene objects. - Verified fix in , confirming both skybox and objects are visible and correctly depth-sorted. handoff(Gemini): The rendering pipeline now correctly handles depth for two-pass rendering, with the skybox as background and objects properly visible and depth-tested. --- src/3d/renderer.cc | 71 +++++++++--------------------------------------------- 1 file changed, 11 insertions(+), 60 deletions(-) (limited to 'src/3d') diff --git a/src/3d/renderer.cc b/src/3d/renderer.cc index 79d59d9..45caf14 100644 --- a/src/3d/renderer.cc +++ b/src/3d/renderer.cc @@ -415,130 +415,81 @@ void Renderer3D::render(const Scene& scene, const Camera& camera, float time, WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device_, nullptr); - // --- Pass 1: Render Skybox (Clears color, no depth write) --- - + // --- Pass 1: Render Skybox (Clears color, clears and stores depth) --- if (sky_texture_view_ && skybox_pipeline_) { WGPURenderPassColorAttachment sky_color_attachment = {}; - gpu_init_color_attachment(sky_color_attachment, target_view); - - sky_color_attachment.loadOp = WGPULoadOp_Clear; - + sky_color_attachment.loadOp = WGPULoadOp_Clear; // Clear to black sky_color_attachment.storeOp = WGPUStoreOp_Store; - - sky_color_attachment.clearValue = {0.0f, 0.0f, 0.0f, - 1.0f}; // Clear to black - - // Only clear depth, don't store it for the skybox pass + sky_color_attachment.clearValue = {0.0f, 0.0f, 0.0f, 1.0f}; WGPURenderPassDepthStencilAttachment sky_depth_attachment = {}; - sky_depth_attachment.view = depth_view; - - sky_depth_attachment.depthLoadOp = WGPULoadOp_Clear; - + sky_depth_attachment.depthLoadOp = WGPULoadOp_Clear; // Clear depth sky_depth_attachment.depthStoreOp = - WGPUStoreOp_Discard; // Don't store depth - - sky_depth_attachment.depthClearValue = 1.0f; + WGPUStoreOp_Store; // Store cleared depth + sky_depth_attachment.depthClearValue = 1.0f; // Farthest possible depth WGPURenderPassDescriptor sky_pass_desc = {}; - sky_pass_desc.colorAttachmentCount = 1; - sky_pass_desc.colorAttachments = &sky_color_attachment; - sky_pass_desc.depthStencilAttachment = &sky_depth_attachment; WGPURenderPassEncoder sky_pass = wgpuCommandEncoderBeginRenderPass(encoder, &sky_pass_desc); - wgpuRenderPassEncoderSetViewport(sky_pass, 0.0f, 0.0f, (float)width_, - (float)height_, 0.0f, 1.0f); if (skybox_bind_group_) wgpuBindGroupRelease(skybox_bind_group_); - WGPUBindGroupEntry bg_entries[3] = {}; - bg_entries[0].binding = 0; - bg_entries[0].textureView = sky_texture_view_; - bg_entries[1].binding = 1; - bg_entries[1].sampler = default_sampler_; - bg_entries[2].binding = 2; - bg_entries[2].buffer = global_uniform_buffer_; - bg_entries[2].size = sizeof(GlobalUniforms); WGPUBindGroupDescriptor bg_desc = {}; - bg_desc.layout = wgpuRenderPipelineGetBindGroupLayout(skybox_pipeline_, 0); - bg_desc.entryCount = 3; - bg_desc.entries = bg_entries; - skybox_bind_group_ = wgpuDeviceCreateBindGroup(device_, &bg_desc); - wgpuBindGroupLayoutRelease(bg_desc.layout); wgpuRenderPassEncoderSetPipeline(sky_pass, skybox_pipeline_); - wgpuRenderPassEncoderSetBindGroup(sky_pass, 0, skybox_bind_group_, 0, nullptr); - wgpuRenderPassEncoderDraw(sky_pass, 3, 1, 0, 0); // Draw a full-screen quad - wgpuRenderPassEncoderEnd(sky_pass); - wgpuRenderPassEncoderRelease(sky_pass); } // --- Pass 2: Render Scene Objects (Loads depth, writes depth) --- - WGPURenderPassColorAttachment obj_color_attachment = {}; - gpu_init_color_attachment(obj_color_attachment, target_view); - - obj_color_attachment.loadOp = WGPULoadOp_Load; // Load from previous pass - + obj_color_attachment.loadOp = WGPULoadOp_Load; // Load skybox color obj_color_attachment.storeOp = WGPUStoreOp_Store; WGPURenderPassDepthStencilAttachment obj_depth_attachment = {}; - obj_depth_attachment.view = depth_view; - - obj_depth_attachment.depthLoadOp = WGPULoadOp_Load; // Load cleared depth - - obj_depth_attachment.depthStoreOp = WGPUStoreOp_Store; // Store depth - - obj_depth_attachment.depthClearValue = 1.0f; + obj_depth_attachment.depthLoadOp = + WGPULoadOp_Load; // Load cleared depth from skybox pass + obj_depth_attachment.depthStoreOp = WGPUStoreOp_Store; // Store object depth + obj_depth_attachment.depthClearValue = 1.0f; // Not used when loadOp is Load WGPURenderPassDescriptor obj_pass_desc = {}; - obj_pass_desc.colorAttachmentCount = 1; - obj_pass_desc.colorAttachments = &obj_color_attachment; - obj_pass_desc.depthStencilAttachment = &obj_depth_attachment; WGPURenderPassEncoder obj_pass = wgpuCommandEncoderBeginRenderPass(encoder, &obj_pass_desc); - wgpuRenderPassEncoderSetViewport(obj_pass, 0.0f, 0.0f, (float)width_, - (float)height_, 0.0f, 1.0f); - draw(obj_pass, scene, camera, time); - wgpuRenderPassEncoderEnd(obj_pass); - wgpuRenderPassEncoderRelease(obj_pass); WGPUCommandBuffer commands = wgpuCommandEncoderFinish(encoder, nullptr); -- cgit v1.2.3