Skip to main content

engvis_renderer/
depth.rs

1pub struct DepthTexture {
2    pub texture: wgpu::Texture,
3    pub view: wgpu::TextureView,
4    pub format: wgpu::TextureFormat,
5    pub sample_count: u32,
6}
7
8impl DepthTexture {
9    pub const FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float;
10
11    pub fn new(device: &wgpu::Device, width: u32, height: u32, sample_count: u32) -> Self {
12        let size = wgpu::Extent3d {
13            width: width.max(1),
14            height: height.max(1),
15            depth_or_array_layers: 1,
16        };
17        let texture = device.create_texture(&wgpu::TextureDescriptor {
18            label: Some("Depth Texture"),
19            size,
20            mip_level_count: 1,
21            sample_count,
22            dimension: wgpu::TextureDimension::D2,
23            format: Self::FORMAT,
24            usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
25            view_formats: &[],
26        });
27        let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
28
29        Self {
30            texture,
31            view,
32            format: Self::FORMAT,
33            sample_count,
34        }
35    }
36
37    pub fn resize(&mut self, device: &wgpu::Device, width: u32, height: u32) {
38        let sample_count = self.sample_count;
39        *self = Self::new(device, width, height, sample_count);
40    }
41}