mirror of
https://github.com/sotrh/learn-wgpu.git
synced 2024-11-11 19:10:34 +00:00
commit
424a33bf3d
@ -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: &[
|
||||
|
@ -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<f32>;
|
||||
@group(0) @binding(1)
|
||||
var s_shadow: sampler;
|
||||
|
||||
@fragment
|
||||
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
|
||||
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<f32>(vec3<f32>(r), 1.0);
|
||||
}
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user