diff options
Diffstat (limited to 'src/3d')
| -rw-r--r-- | src/3d/renderer.cc | 19 | ||||
| -rw-r--r-- | src/3d/renderer.h | 1 |
2 files changed, 16 insertions, 4 deletions
diff --git a/src/3d/renderer.cc b/src/3d/renderer.cc index 45caf14..778509f 100644 --- a/src/3d/renderer.cc +++ b/src/3d/renderer.cc @@ -111,7 +111,12 @@ void Renderer3D::create_skybox_pipeline() { desc.primitive.frontFace = WGPUFrontFace_CCW; desc.multisample.count = 1; desc.multisample.mask = 0xFFFFFFFF; - desc.depthStencil = nullptr; // Explicitly no depth for skybox + + WGPUDepthStencilState depth_stencil = {}; + depth_stencil.format = WGPUTextureFormat_Depth24Plus; + depth_stencil.depthWriteEnabled = WGPUOptionalBool_False; + depth_stencil.depthCompare = WGPUCompareFunction_Always; + desc.depthStencil = &depth_stencil; skybox_pipeline_ = wgpuDeviceCreateRenderPipeline(device_, &desc); wgpuBindGroupLayoutRelease(bgl); @@ -295,6 +300,7 @@ void Renderer3D::update_uniforms(const Scene& scene, const Camera& camera, float time) { GlobalUniforms globals; globals.view_proj = camera.get_projection_matrix() * camera.get_view_matrix(); + globals.inv_view_proj = globals.view_proj.inverse(); globals.camera_pos_time = vec4(camera.position.x, camera.position.y, camera.position.z, time); globals.params = @@ -416,6 +422,7 @@ void Renderer3D::render(const Scene& scene, const Camera& camera, float time, WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device_, nullptr); // --- Pass 1: Render Skybox (Clears color, clears and stores depth) --- + bool skybox_rendered = false; if (sky_texture_view_ && skybox_pipeline_) { WGPURenderPassColorAttachment sky_color_attachment = {}; gpu_init_color_attachment(sky_color_attachment, target_view); @@ -464,20 +471,24 @@ void Renderer3D::render(const Scene& scene, const Camera& camera, float time, wgpuRenderPassEncoderDraw(sky_pass, 3, 1, 0, 0); // Draw a full-screen quad wgpuRenderPassEncoderEnd(sky_pass); wgpuRenderPassEncoderRelease(sky_pass); + skybox_rendered = true; } // --- 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 skybox color + obj_color_attachment.loadOp = + skybox_rendered ? WGPULoadOp_Load : WGPULoadOp_Clear; // Load or Clear obj_color_attachment.storeOp = WGPUStoreOp_Store; + obj_color_attachment.clearValue = {0.05f, 0.05f, 0.05f, + 1.0f}; // Dark gray if no sky WGPURenderPassDepthStencilAttachment obj_depth_attachment = {}; obj_depth_attachment.view = depth_view; obj_depth_attachment.depthLoadOp = - WGPULoadOp_Load; // Load cleared depth from skybox pass + skybox_rendered ? WGPULoadOp_Load : WGPULoadOp_Clear; // Load or Clear obj_depth_attachment.depthStoreOp = WGPUStoreOp_Store; // Store object depth - obj_depth_attachment.depthClearValue = 1.0f; // Not used when loadOp is Load + obj_depth_attachment.depthClearValue = 1.0f; WGPURenderPassDescriptor obj_pass_desc = {}; obj_pass_desc.colorAttachmentCount = 1; diff --git a/src/3d/renderer.h b/src/3d/renderer.h index 148a521..57ad671 100644 --- a/src/3d/renderer.h +++ b/src/3d/renderer.h @@ -16,6 +16,7 @@ // Matches the GPU struct layout struct GlobalUniforms { mat4 view_proj; + mat4 inv_view_proj; // Added for skybox/raymarching vec4 camera_pos_time; // xyz = camera_pos, w = time vec4 params; // x = num_objects, yzw = padding vec2 resolution; |
