Skip to main content

scenix_renderer/
shadow.rs

1/// Shared depth texture array for shadow-casting lights.
2#[derive(Debug)]
3pub struct ShadowMapAtlas {
4    texture: wgpu::Texture,
5    view: wgpu::TextureView,
6    size: u32,
7    layers: u32,
8}
9
10impl ShadowMapAtlas {
11    /// Allocates a square depth texture array.
12    pub fn new(device: &wgpu::Device, size: u32, layers: u32) -> Self {
13        let size = size.max(1);
14        let layers = layers.max(1);
15        let texture = device.create_texture(&wgpu::TextureDescriptor {
16            label: Some("scenix.shadow.atlas"),
17            size: wgpu::Extent3d {
18                width: size,
19                height: size,
20                depth_or_array_layers: layers,
21            },
22            mip_level_count: 1,
23            sample_count: 1,
24            dimension: wgpu::TextureDimension::D2,
25            format: wgpu::TextureFormat::Depth32Float,
26            usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
27            view_formats: &[],
28        });
29        let view = texture.create_view(&wgpu::TextureViewDescriptor {
30            label: Some("scenix.shadow.atlas.view"),
31            dimension: Some(wgpu::TextureViewDimension::D2Array),
32            ..Default::default()
33        });
34        Self {
35            texture,
36            view,
37            size,
38            layers,
39        }
40    }
41
42    /// Returns the texture view used by shaders.
43    #[inline]
44    pub const fn view(&self) -> &wgpu::TextureView {
45        &self.view
46    }
47
48    /// Returns the backing texture.
49    #[inline]
50    pub const fn texture(&self) -> &wgpu::Texture {
51        &self.texture
52    }
53
54    /// Returns shadow map width and height.
55    #[inline]
56    pub const fn size(&self) -> u32 {
57        self.size
58    }
59
60    /// Returns the number of texture-array layers.
61    #[inline]
62    pub const fn layers(&self) -> u32 {
63        self.layers
64    }
65}