From b3de9fc9d4458a15f72ffe7d9f9e1278e072bf5b Mon Sep 17 00:00:00 2001 From: matthiascy Date: Thu, 26 Jan 2023 12:02:45 +0100 Subject: [PATCH] fix tutorial8 challenge depth map sampling --- .../beginner/tutorial8-depth/src/challenge.rs | 9 ++-- .../tutorial8-depth/src/challenge.wgsl | 8 ++-- code/beginner/tutorial8-depth/src/texture.rs | 41 +++++++++++++++++++ 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/code/beginner/tutorial8-depth/src/challenge.rs b/code/beginner/tutorial8-depth/src/challenge.rs index 2a61d7b9..ad21cf55 100644 --- a/code/beginner/tutorial8-depth/src/challenge.rs +++ b/code/beginner/tutorial8-depth/src/challenge.rs @@ -1,6 +1,7 @@ use std::iter; use cgmath::prelude::*; +use wgpu::{include_spirv_raw}; use wgpu::util::DeviceExt; use winit::{ event::*, @@ -295,7 +296,7 @@ struct DepthPass { impl DepthPass { fn new(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) -> Self { - let texture = texture::Texture::create_depth_texture(device, config, "depth_texture"); + let texture = texture::Texture::create_depth_texture_non_comparison_sampler(device, config, "depth_texture"); let layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some("Depth Pass Layout"), @@ -304,7 +305,7 @@ impl DepthPass { binding: 0, count: None, ty: wgpu::BindingType::Texture { - sample_type: wgpu::TextureSampleType::Depth, + sample_type: wgpu::TextureSampleType::Float { filterable: false }, multisampled: false, view_dimension: wgpu::TextureViewDimension::D2, }, @@ -313,7 +314,7 @@ impl DepthPass { wgpu::BindGroupLayoutEntry { binding: 1, count: None, - ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Comparison), + ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::NonFiltering), visibility: wgpu::ShaderStages::FRAGMENT, }, ], @@ -412,7 +413,7 @@ impl DepthPass { } fn resize(&mut self, device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) { - self.texture = texture::Texture::create_depth_texture(device, config, "depth_texture"); + self.texture = texture::Texture::create_depth_texture_non_comparison_sampler(device, config, "depth_texture"); self.bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor { layout: &self.layout, entries: &[ diff --git a/code/beginner/tutorial8-depth/src/challenge.wgsl b/code/beginner/tutorial8-depth/src/challenge.wgsl index 00860329..62d8655f 100644 --- a/code/beginner/tutorial8-depth/src/challenge.wgsl +++ b/code/beginner/tutorial8-depth/src/challenge.wgsl @@ -23,15 +23,15 @@ fn vs_main( // Fragment shader @group(0) @binding(0) -var t_shadow: texture_depth_2d; -@group(0)@binding(1) -var s_shadow: sampler_comparison; +var t_shadow: texture_2d; +@group(0) @binding(1) +var s_shadow: sampler; @fragment fn fs_main(in: VertexOutput) -> @location(0) vec4 { let near = 0.1; let far = 100.0; - let depth = textureSampleCompare(t_shadow, s_shadow, in.tex_coords, in.clip_position.w); + let depth = textureSample(t_shadow, s_shadow, in.tex_coords).x; let r = (2.0 * near) / (far + near - depth * (far - near)); return vec4(vec3(r), 1.0); } \ No newline at end of file diff --git a/code/beginner/tutorial8-depth/src/texture.rs b/code/beginner/tutorial8-depth/src/texture.rs index e33b4a9a..7e681e42 100644 --- a/code/beginner/tutorial8-depth/src/texture.rs +++ b/code/beginner/tutorial8-depth/src/texture.rs @@ -53,6 +53,47 @@ impl Texture { } } + pub fn create_depth_texture_non_comparison_sampler( + device: &wgpu::Device, + config: &wgpu::SurfaceConfiguration, + label: &str, + ) -> Self { + let size = wgpu::Extent3d { + width: config.width, + height: config.height, + depth_or_array_layers: 1, + }; + let desc = wgpu::TextureDescriptor { + label: Some(label), + size, + mip_level_count: 1, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: Self::DEPTH_FORMAT, + usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING, + }; + let texture = device.create_texture(&desc); + let view = texture.create_view(&wgpu::TextureViewDescriptor::default()); + let sampler = device.create_sampler(&wgpu::SamplerDescriptor { + address_mode_u: wgpu::AddressMode::ClampToEdge, + address_mode_v: wgpu::AddressMode::ClampToEdge, + address_mode_w: wgpu::AddressMode::ClampToEdge, + mag_filter: wgpu::FilterMode::Nearest, + min_filter: wgpu::FilterMode::Nearest, + mipmap_filter: wgpu::FilterMode::Nearest, + compare: None, + lod_min_clamp: 0.0, + lod_max_clamp: 100.0, + ..Default::default() + }); + + Self { + texture, + view, + sampler, + } + } + pub fn from_bytes( device: &wgpu::Device, queue: &wgpu::Queue,