gli_rs/texture/
t2d_array.rs

1
2use crate::ffi::root::gli;
3use crate::ffi::root::glm;
4use crate::ffi::root::bindings::Texture2DArray as bindings;
5
6use crate::format::Format;
7use crate::target::Target;
8use crate::texture::{GliTexture, Texture2D};
9use crate::texture::inner::TextureAccessible;
10use crate::Extent2d;
11
12/// 2d array texture
13#[repr(transparent)]
14pub struct Texture2DArray {
15    ffi: gli::texture2d_array,
16}
17
18impl Texture2DArray {
19
20    /// Create an empty texture 2D array.
21    #[inline]
22    pub fn new_empty() -> Texture2DArray {
23        Texture2DArray { ffi: unsafe { bindings::tex2darray_new_empty() } }
24    }
25
26    /// Create a texture2d_array and allocate a new storage_linear.
27    #[inline]
28    pub fn new(format: Format, extent: Extent2d, layers: usize, levels: usize) -> Texture2DArray {
29        Texture2DArray { ffi: unsafe { bindings::tex2darray_new_(format.0, glm::ivec2(extent.into()), layers, levels) } }
30    }
31
32    /// Create a texture2d_array and allocate a new storage_linear with a complete mipmap chain.
33    #[inline]
34    pub fn new_with_mipmap_chain(format: Format, extent: Extent2d, layers: usize) -> Texture2DArray {
35        Texture2DArray { ffi: unsafe { bindings::tex2darray_new_with_mipmap_chain(format.0, glm::ivec2(extent.into()), layers) } }
36    }
37
38    /// Create a texture2d_array view with an existing storage_linear.
39    #[inline]
40    pub fn share_from(texture: &impl GliTexture) -> Texture2DArray {
41        Texture2DArray { ffi: unsafe { bindings::tex2darray_share_from(texture.raw_texture()) } }
42    }
43
44    /// Create a texture2d_array view with an existing storage_linear.
45    #[inline]
46    pub fn share_from_detail(texture: &impl GliTexture, format: Format, base_layer: usize, max_layer: usize, base_face: usize, max_face: usize, base_level: usize, max_level: usize) -> Texture2DArray {
47
48        Texture2DArray {
49            ffi: unsafe { bindings::tex2darray_share_from_detail(texture.raw_texture(), format.0, base_layer, max_layer, base_face, max_face, base_level, max_level) }
50        }
51    }
52
53    /// Create a texture2d_array view, reference a subset of an existing texture2d_array instance.
54    #[inline]
55    pub fn share_from_subset(texture: &Texture2DArray, base_layer: usize, max_layer: usize, base_level: usize, max_level: usize) -> Texture2DArray {
56
57        Texture2DArray {
58            ffi: unsafe { bindings::tex2darray_share_from_subset(&texture.ffi, base_layer, max_layer, base_level, max_level) }
59        }
60    }
61
62    /// Create a view of the texture identified by Layer in the texture array.
63    ///
64    /// This method is equivalent to `[]` operator in C++ version.
65    #[inline]
66    pub fn get_layer(&self, layer: usize) -> Texture2D {
67
68        debug_assert!(layer < self.layers());
69
70        Texture2D::share_from_detail(
71            self, self.format(),
72            self.base_layer() + layer, self.base_layer() + layer,
73            self.base_face(), self.max_face(),
74            self.base_level(), self.max_level())
75    }
76
77    #[doc(hidden)]
78    #[inline]
79    pub(crate) fn raw_ffi(&self) -> &gli::texture2d_array {
80        &self.ffi
81    }
82}
83
84impl GliTexture for Texture2DArray {
85    const TARGET_TYPE: Target = Target::TARGET_2D_ARRAY;
86    type ExtentType = Extent2d; // equivalent to gli::texture2d_extent_type.
87}
88
89impl TextureAccessible for Texture2DArray {
90
91    fn raw_texture(&self) -> &gli::texture {
92        &self.ffi._base
93    }
94
95    fn raw_texture_mut(&mut self) -> &mut gli::texture {
96        &mut self.ffi._base
97    }
98}
99
100impl From<gli::texture> for Texture2DArray {
101
102    fn from(ffi: gli::texture) -> Texture2DArray {
103        Texture2DArray { ffi: gli::texture2d_array { _base: ffi } }
104    }
105}
106
107impl std::cmp::PartialEq for Texture2DArray {
108
109    /// Compare two textures. Two textures are the same when the data, the format and the targets are the same.
110    fn eq(&self, other: &Texture2DArray) -> bool {
111
112        use crate::ffi::root::bindings::Comparison::is_texture_equal;
113
114        unsafe {
115            is_texture_equal(self.raw_texture(), other.raw_texture())
116        }
117    }
118
119    /// Compare two textures. Two textures are the same when the data, the format and the targets are the same.
120    fn ne(&self, other: &Texture2DArray) -> bool {
121
122        use crate::ffi::root::bindings::Comparison::is_texture_unequal;
123
124        unsafe {
125            is_texture_unequal(self.raw_texture(), other.raw_texture())
126        }
127    }
128}
129
130impl std::cmp::Eq for Texture2DArray {}