1use wgpu::{
2 Device, Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages,
3 TextureView, TextureViewDescriptor,
4};
5
6pub struct DepthBuffer {
7 pub view: TextureView,
8 pub format: TextureFormat,
9 size: (u32, u32),
10}
11
12impl DepthBuffer {
13 pub fn new(device: &Device, format: TextureFormat, size: (u32, u32)) -> Self {
14 let texture = device.create_texture(&TextureDescriptor {
15 label: Some("loam-render DepthBuffer"),
16 size: Extent3d {
17 width: size.0,
18 height: size.1,
19 depth_or_array_layers: 1,
20 },
21 mip_level_count: 1,
22 sample_count: 1,
23 dimension: TextureDimension::D2,
24 format,
25 usage: TextureUsages::RENDER_ATTACHMENT,
26 view_formats: &[],
27 });
28 let view = texture.create_view(&TextureViewDescriptor::default());
29 Self { view, format, size }
30 }
31
32 pub fn ensure(
33 slot: &mut Option<DepthBuffer>,
34 device: &Device,
35 format: TextureFormat,
36 size: (u32, u32),
37 ) {
38 let needs_recreate = match slot {
39 Some(b) => b.format != format || b.size != size,
40 None => true,
41 };
42 if needs_recreate {
43 *slot = Some(DepthBuffer::new(device, format, size));
44 }
45 }
46}