From 9ed3cea3cae11f65d63a522d572b6e8edeee8a02 Mon Sep 17 00:00:00 2001 From: Albin Chaboissier Date: Wed, 9 Sep 2026 23:01:23 +0200 Subject: [PATCH] Z prepass --- shaders/voxel.wgsl | 16 ++++++++++++++-- src/main.rs | 26 +++++++++++++++----------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/shaders/voxel.wgsl b/shaders/voxel.wgsl index e7b7ec4..9b3db90 100644 --- a/shaders/voxel.wgsl +++ b/shaders/voxel.wgsl @@ -6,7 +6,8 @@ struct VertexOutput @location(2) cam_pos: vec3, @location(3) world_pos: vec3, @location(4) @interpolate(flat) structure_id: u32, - @location(5) chunk_position: vec3 + @location(5) chunk_position: vec3, + @location(6) ndc: vec4 } struct ChunkImmediate @@ -123,6 +124,7 @@ fn chunk(@builtin(vertex_index) index: u32, @location(0) position: vec3, @l output.world_pos = vertex + position; output.structure_id = id; output.chunk_position = position; + output.ndc = output_vertex; //let output = vec4(vertex, 1.0f); return output; @@ -389,6 +391,14 @@ fn fragment_prepass(in: VertexOutput) -> FragmentPrepassOutput @fragment fn fragment(in: VertexOutput) -> FragmentOutput { + let surface_depth = in.ndc.z; + let prepass_depth = textureSample(prepass_depth, prepass_depth_sampler, in.ndc.xy).x; + + if surface_depth <= prepass_depth + { + discard; + } + //frag_out.color = vec4(2 * 0.01 / (100. + 0.01 - depth * (100. - 0.01))); let ray_dir = normalize(in.world_pos - in.cam_pos); let interp = box_inter(in.cam_pos - in.chunk_position, ray_dir, vec3(0.), vec3(1)); @@ -401,7 +411,9 @@ fn fragment(in: VertexOutput) -> FragmentOutput var frag_out: FragmentOutput; //frag_out.color = result.color; frag_out.color = result.color; - frag_out.depth = depth; + //frag_out.color = vec4(smpl); + //frag_out.color = vec4((2. * 0.01 * 100.) / (0.01 + 100. - prepass_depth * (100. - 0.01))); + //frag_out.depth = depth; return frag_out; //return vec4(ray_origin, 1.); diff --git a/src/main.rs b/src/main.rs index 859747b..6ee1c6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -429,23 +429,27 @@ impl State @compute @workgroup_size(8, 8) fn main(@builtin(global_invocation_id) id: vec3) {{ - let dp = vec2(id.xy); - let sp = dp * {prepass_downsampling}; - + let source_pos = vec2(id.xy); var depth = 1.; + depth = min(depth, textureLoad(input_tex, source_pos + vec2(0, 0)).x); + depth = min(depth, textureLoad(input_tex, source_pos + vec2(1, 0)).x); + depth = min(depth, textureLoad(input_tex, source_pos + vec2(0, 1)).x); + depth = min(depth, textureLoad(input_tex, source_pos + vec2(1, 1)).x); + + let dest_pos = source_pos * {prepass_downsampling}; + for(var ox = 0; ox < {prepass_downsampling}; ox ++) {{ for(var oy = 0; oy < {prepass_downsampling}; oy ++) {{ - depth = min(depth, textureLoad(input_tex, sp + vec2(ox, oy)).x); + textureStore( + output_tex, + dest_pos + vec2(ox, oy), + vec4(depth) + ); }} }} - textureStore( - output_tex, - dp, - vec4(depth) - ); }} " ) @@ -688,7 +692,7 @@ impl State }, wgpu::BindGroupEntry { binding: 1, - resource: wgpu::BindingResource::TextureView(&self.upsampled_prepass_depth.1), + resource: wgpu::BindingResource::TextureView(&self.prepass_depth.1), }, ], }); @@ -783,7 +787,7 @@ impl State }, })], depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { - view: &self.depth_buffer.1, + view: &self.prepass_depth_buffer.1, depth_ops: Some(Operations { load: wgpu::LoadOp::Clear(1.), store: wgpu::StoreOp::Store,