diff --git a/flare3d/src/state.rs b/flare3d/src/state.rs index a1269b7..f4323e1 100644 --- a/flare3d/src/state.rs +++ b/flare3d/src/state.rs @@ -6,7 +6,7 @@ use winit::{ window::Window, }; -use crate::texture; +use crate::{texture, Texture}; #[repr(C)] #[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)] @@ -302,7 +302,7 @@ pub struct State { vertex_buffer: wgpu::Buffer, index_buffer: wgpu::Buffer, diffuse_bind_group: wgpu::BindGroup, - diffuse_texture: texture::Texture, + diffuse_texture: Texture, camera: Camera, camera_uniform: CameraUniform, camera_buffer: wgpu::Buffer, @@ -310,6 +310,7 @@ pub struct State { camera_controller: CameraController, instances: Vec, instance_buffer: wgpu::Buffer, + depth_texture: Texture, } impl State { @@ -436,6 +437,8 @@ impl State { label: Some("camera_bind_group"), }); + let depth_texture = texture::Texture::create_depth_texture(&device, &config, "depth_texture"); + let shader = device.create_shader_module(wgpu::include_wgsl!("shader.wgsl")); let render_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { @@ -469,7 +472,13 @@ impl State { unclipped_depth: false, conservative: false, }, - depth_stencil: None, + depth_stencil: Some(wgpu::DepthStencilState { + format: texture::Texture::DEPTH_FORMAT, + depth_write_enabled: true, + depth_compare: wgpu::CompareFunction::Less, + stencil: wgpu::StencilState::default(), + bias: wgpu::DepthBiasState::default(), + }), multisample: wgpu::MultisampleState { count: 1, mask: !0, @@ -539,6 +548,7 @@ impl State { camera_controller, instances, instance_buffer, + depth_texture, } } } @@ -551,6 +561,7 @@ impl State { self.config.height = new_size.height; self.surface.configure(&self.device, &self.config); } + self.depth_texture = texture::Texture::create_depth_texture(&self.device, &self.config, "depth_texture"); } pub fn update(&mut self) { @@ -590,8 +601,15 @@ impl State { store: wgpu::StoreOp::Store, }, })], - depth_stencil_attachment: None, - ..Default::default() + depth_stencil_attachment: Some(wgpu::RenderPassDepthStencilAttachment { + view: &self.depth_texture.view, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(1.0), + store: wgpu::StoreOp::Store + }), + stencil_ops: None, + }), + ..Default::default() }); render_pass.set_pipeline(&self.render_pipeline); diff --git a/flare3d/src/texture.rs b/flare3d/src/texture.rs index dec216f..b96f688 100644 --- a/flare3d/src/texture.rs +++ b/flare3d/src/texture.rs @@ -80,3 +80,45 @@ impl Texture { }) } } + +impl Texture { + pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float; + + 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, + view_formats: &[], + }; + 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: 0.0, + lod_max_clamp: 200.0, + ..Default::default() + } + ); + + Self { texture, view, sampler } + } +}