Depth buffer

This commit is contained in:
marceline-cramer 2022-02-01 17:48:59 -07:00
parent 33d4402738
commit 2d6f408230
1 changed files with 53 additions and 3 deletions

View File

@ -22,9 +22,10 @@ struct Renderer {
surface: wgpu::Surface, surface: wgpu::Surface,
queue: wgpu::Queue, queue: wgpu::Queue,
config: wgpu::SurfaceConfiguration, config: wgpu::SurfaceConfiguration,
depth_texture: wgpu::Texture,
depth_texture_view: wgpu::TextureView,
camera_uniform: CameraUniform, camera_uniform: CameraUniform,
camera_buffer: wgpu::Buffer, camera_buffer: wgpu::Buffer,
camera_bind_group: wgpu::BindGroup, camera_bind_group: wgpu::BindGroup,
meshes_buffer: wgpu::Buffer, meshes_buffer: wgpu::Buffer,
meshes_bind_group: wgpu::BindGroup, meshes_bind_group: wgpu::BindGroup,
@ -70,6 +71,8 @@ impl Renderer {
let mesh_pool = MeshPool::default(); let mesh_pool = MeshPool::default();
let texture_pool = TexturePool::new(&device); let texture_pool = TexturePool::new(&device);
let (depth_texture, depth_texture_view) = Self::make_depth_texture(&device, &config);
let camera_uniform = CameraUniform::new(); let camera_uniform = CameraUniform::new();
let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
@ -172,7 +175,13 @@ impl Renderer {
unclipped_depth: false, unclipped_depth: false,
conservative: false, conservative: false,
}, },
depth_stencil: None, depth_stencil: Some(wgpu::DepthStencilState {
format: Self::DEPTH_FORMAT,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::Less,
stencil: wgpu::StencilState::default(),
bias: wgpu::DepthBiasState::default(),
}),
multisample: wgpu::MultisampleState { multisample: wgpu::MultisampleState {
count: 1, count: 1,
mask: !0, mask: !0,
@ -189,6 +198,8 @@ impl Renderer {
config, config,
mesh_pool, mesh_pool,
texture_pool, texture_pool,
depth_texture,
depth_texture_view,
camera_uniform, camera_uniform,
camera_buffer, camera_buffer,
camera_bind_group, camera_bind_group,
@ -198,12 +209,44 @@ impl Renderer {
} }
} }
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
fn make_depth_texture(
device: &wgpu::Device,
config: &wgpu::SurfaceConfiguration,
) -> (wgpu::Texture, wgpu::TextureView) {
let size = wgpu::Extent3d {
width: config.width,
height: config.height,
depth_or_array_layers: 1,
};
let desc = wgpu::TextureDescriptor {
label: Some("Depth Texture"),
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());
(texture, view)
}
pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) { pub fn resize(&mut self, new_size: winit::dpi::PhysicalSize<u32>) {
if new_size.width > 0 && new_size.height > 0 { if new_size.width > 0 && new_size.height > 0 {
self.size = new_size; self.size = new_size;
self.config.width = new_size.width; self.config.width = new_size.width;
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);
let (depth_texture, depth_texture_view) =
Self::make_depth_texture(&self.device, &self.config);
self.depth_texture = depth_texture;
self.depth_texture_view = depth_texture_view;
} }
} }
@ -251,7 +294,14 @@ impl Renderer {
store: true, store: true,
}, },
}], }],
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,
}),
}); });
rp.set_pipeline(&self.render_pipeline); rp.set_pipeline(&self.render_pipeline);