1
0
Fork 0

[DEPTH] From tutorial

This commit is contained in:
Florian RICHER 2022-06-14 13:24:33 +02:00
parent d9e8b96754
commit c2c3e4fd46
2 changed files with 59 additions and 3 deletions

View file

@ -62,6 +62,7 @@ pub struct State {
diffuse_bind_group: wgpu::BindGroup, diffuse_bind_group: wgpu::BindGroup,
#[allow(dead_code)] #[allow(dead_code)]
diffuse_texture: super::texture::Texture, diffuse_texture: super::texture::Texture,
depth_texture: super::texture::Texture,
toggle: bool toggle: bool
} }
@ -316,6 +317,8 @@ impl State {
usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST,
}); });
let depth_texture = super::texture::Texture::create_depth_texture(&device, &config, "depth_texture");
let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { let render_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor {
label: Some("Render Pipeline"), label: Some("Render Pipeline"),
layout: Some(&render_pipeline_layout), layout: Some(&render_pipeline_layout),
@ -345,7 +348,13 @@ impl State {
// Requires Features::CONSERVATIVE_RASTERIZATION // Requires Features::CONSERVATIVE_RASTERIZATION
conservative: false, conservative: false,
}, },
depth_stencil: None, depth_stencil: Some(wgpu::DepthStencilState {
format: super::texture::Texture::DEPTH_FORMAT,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less, // 1.
stencil: wgpu::StencilState::default(), // 2.
bias: wgpu::DepthBiasState::default(),
}),
multisample: wgpu::MultisampleState { multisample: wgpu::MultisampleState {
count: 1, count: 1,
mask: !0, mask: !0,
@ -376,7 +385,8 @@ impl State {
diffuse_texture_pikachu, diffuse_texture_pikachu,
instances, instances,
instance_buffer, instance_buffer,
toggle: false toggle: false,
depth_texture,
} }
} }
@ -387,6 +397,7 @@ impl State {
self.config.height = new_size.height; self.config.height = new_size.height;
self.surface.configure(&self.device, &self.config); self.surface.configure(&self.device, &self.config);
} }
self.depth_texture = super::texture::Texture::create_depth_texture(&self.device, &self.config, "depth_texture");
} }
pub fn input(&mut self, event: &WindowEvent) -> bool { pub fn input(&mut self, event: &WindowEvent) -> bool {
@ -469,7 +480,14 @@ impl State {
}, },
}, },
], ],
depth_stencil_attachment: None, depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment {
view: &self.depth_texture.view,
depth_ops: Some(wgpu::Operations {
load: wgpu::LoadOp::Clear(1.0),
store: true,
}),
stencil_ops: None,
})
}); });
render_pass.set_pipeline(&self.render_pipeline); render_pass.set_pipeline(&self.render_pipeline);

View file

@ -8,6 +8,7 @@ pub struct Texture {
} }
impl Texture { impl Texture {
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
pub fn from_bytes( pub fn from_bytes(
device: &wgpu::Device, device: &wgpu::Device,
queue: &wgpu::Queue, queue: &wgpu::Queue,
@ -75,4 +76,41 @@ impl Texture {
sampler, sampler,
}) })
} }
pub fn create_depth_texture(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::Linear,
min_filter: wgpu::FilterMode::Linear,
mipmap_filter: wgpu::FilterMode::Nearest,
compare: Some(wgpu::CompareFunction::LessEqual),
lod_min_clamp: -100.0,
lod_max_clamp: 100.0,
..Default::default()
}
);
Self { texture, view, sampler }
}
} }