From b978091cd433defee65e5ed7db8117484f2b8ec0 Mon Sep 17 00:00:00 2001 From: nullprop Date: Sat, 15 Apr 2023 23:20:26 +0300 Subject: [PATCH] Different depth settings for shadows --- src/core/pass.rs | 14 ++++++++++++-- src/core/state.rs | 7 +++++-- src/core/texture.rs | 6 +++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/core/pass.rs b/src/core/pass.rs index 1aa0ca1..c819568 100644 --- a/src/core/pass.rs +++ b/src/core/pass.rs @@ -18,6 +18,7 @@ impl RenderPass { depth_format: Option, vertex_layouts: &[VertexBufferLayout], label: &str, + is_shadow: bool, ) -> Self { let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some((label.to_owned() + " pipeline Layout").as_str()), @@ -36,6 +37,7 @@ impl RenderPass { vertex_layouts, shader, label, + is_shadow, ); Self { pipeline } @@ -49,6 +51,7 @@ impl RenderPass { vertex_layouts: &[wgpu::VertexBufferLayout], shader: wgpu::ShaderModuleDescriptor, label: &str, + is_shadow: bool, ) -> wgpu::RenderPipeline { let shader = device.create_shader_module(shader); @@ -93,9 +96,16 @@ impl RenderPass { depth_stencil: depth_format.map(|format| wgpu::DepthStencilState { format, depth_write_enabled: true, - depth_compare: wgpu::CompareFunction::LessEqual, + depth_compare: if is_shadow { wgpu::CompareFunction::LessEqual } else { wgpu::CompareFunction::Less }, stencil: wgpu::StencilState::default(), - bias: wgpu::DepthBiasState::default(), + bias: if is_shadow { + wgpu::DepthBiasState { + constant: 2, // bilinear + slope_scale: 2.0, + clamp: 0.0, + } + } + else { wgpu::DepthBiasState::default() }, }), multisample: wgpu::MultisampleState { count: 1, diff --git a/src/core/state.rs b/src/core/state.rs index 952a565..778d689 100644 --- a/src/core/state.rs +++ b/src/core/state.rs @@ -138,7 +138,7 @@ impl State { &device, &config, "depth_texture", - Some(wgpu::CompareFunction::LessEqual), + Some(wgpu::CompareFunction::Less), 1, wgpu::TextureUsages::RENDER_ATTACHMENT, ); @@ -373,6 +373,7 @@ impl State { Some(Texture::DEPTH_FORMAT), &[ModelVertex::desc(), InstanceRaw::desc()], "light depth pass", + true, ); let geometry_pass = RenderPass::new( @@ -389,6 +390,7 @@ impl State { Some(Texture::DEPTH_FORMAT), &[ModelVertex::desc(), InstanceRaw::desc()], "geometry pass", + false, ); let light_debug_pass = RenderPass::new( @@ -400,6 +402,7 @@ impl State { Some(Texture::DEPTH_FORMAT), &[ModelVertex::desc()], "light debug pass", + false, ); Self { @@ -446,7 +449,7 @@ impl State { &self.device, &self.config, "depth_texture", - Some(wgpu::CompareFunction::LessEqual), + Some(wgpu::CompareFunction::Less), 1, wgpu::TextureUsages::RENDER_ATTACHMENT, ); diff --git a/src/core/texture.rs b/src/core/texture.rs index 86a040c..c301c5b 100644 --- a/src/core/texture.rs +++ b/src/core/texture.rs @@ -37,9 +37,9 @@ impl Texture { let view = texture.create_view(&wgpu::TextureViewDescriptor::default()); let sampler = device.create_sampler(&wgpu::SamplerDescriptor { - address_mode_u: wgpu::AddressMode::Repeat, - address_mode_v: wgpu::AddressMode::Repeat, - address_mode_w: wgpu::AddressMode::Repeat, + address_mode_u: wgpu::AddressMode::ClampToEdge, + address_mode_v: wgpu::AddressMode::ClampToEdge, + address_mode_w: wgpu::AddressMode::ClampToEdge, mag_filter: wgpu::FilterMode::Linear, min_filter: wgpu::FilterMode::Linear, mipmap_filter: wgpu::FilterMode::Nearest,