three_d/core/texture/
depth_texture2d_array.rs

1use crate::core::texture::*;
2
3///
4/// An array of 2D depth textures that can be rendered into and read from. See also [RenderTarget] and [DepthTarget].
5///
6pub struct DepthTexture2DArray {
7    context: Context,
8    id: crate::context::Texture,
9    width: u32,
10    height: u32,
11    depth: u32,
12}
13
14impl DepthTexture2DArray {
15    ///
16    /// Creates a new array of depth textures.
17    ///
18    pub fn new<T: DepthTextureDataType>(
19        context: &Context,
20        width: u32,
21        height: u32,
22        depth: u32,
23        wrap_s: Wrapping,
24        wrap_t: Wrapping,
25    ) -> Self {
26        let id = generate(context);
27        let texture = Self {
28            context: context.clone(),
29            id,
30            width,
31            height,
32            depth,
33        };
34        texture.bind();
35        set_parameters(
36            context,
37            crate::context::TEXTURE_2D_ARRAY,
38            Interpolation::Nearest,
39            Interpolation::Nearest,
40            None,
41            wrap_s,
42            wrap_t,
43            None,
44        );
45        unsafe {
46            context.tex_storage_3d(
47                crate::context::TEXTURE_2D_ARRAY,
48                1,
49                T::internal_format(),
50                width as i32,
51                height as i32,
52                depth as i32,
53            );
54        }
55        texture
56    }
57
58    ///
59    /// Returns a [DepthTarget] which can be used to clear, write to and read from the given layer of this texture.
60    /// Combine this together with a [ColorTarget] with [RenderTarget::new] to be able to write to both a depth and color target at the same time.
61    ///
62    pub fn as_depth_target(&mut self, layer: u32) -> DepthTarget<'_> {
63        DepthTarget::new_texture_2d_array(&self.context, self, layer)
64    }
65
66    /// The width of this texture.
67    pub fn width(&self) -> u32 {
68        self.width
69    }
70
71    /// The height of this texture.
72    pub fn height(&self) -> u32 {
73        self.height
74    }
75
76    /// The number of layers.
77    pub fn depth(&self) -> u32 {
78        self.depth
79    }
80
81    pub(in crate::core) fn bind_as_depth_target(&self, layer: u32) {
82        unsafe {
83            self.context.framebuffer_texture_layer(
84                crate::context::DRAW_FRAMEBUFFER,
85                crate::context::DEPTH_ATTACHMENT,
86                Some(self.id),
87                0,
88                layer as i32,
89            );
90        }
91    }
92
93    pub(in crate::core) fn bind(&self) {
94        unsafe {
95            self.context
96                .bind_texture(crate::context::TEXTURE_2D_ARRAY, Some(self.id));
97        }
98    }
99}
100
101impl Drop for DepthTexture2DArray {
102    fn drop(&mut self) {
103        unsafe {
104            self.context.delete_texture(self.id);
105        }
106    }
107}