wgpu/api/
texture_view.rs

1use std::{sync::Arc, thread};
2
3use crate::*;
4
5/// Handle to a texture view.
6///
7/// A `TextureView` object describes a texture and associated metadata needed by a
8/// [`RenderPipeline`] or [`BindGroup`].
9///
10/// Corresponds to [WebGPU `GPUTextureView`](https://gpuweb.github.io/gpuweb/#gputextureview).
11#[derive(Debug)]
12pub struct TextureView {
13    pub(crate) context: Arc<C>,
14    pub(crate) data: Box<Data>,
15}
16#[cfg(send_sync)]
17static_assertions::assert_impl_all!(TextureView: Send, Sync);
18
19super::impl_partialeq_eq_hash!(TextureView);
20
21impl TextureView {
22    /// Returns the inner hal TextureView using a callback. The hal texture will be `None` if the
23    /// backend type argument does not match with this wgpu Texture
24    ///
25    /// # Safety
26    ///
27    /// - The raw handle obtained from the hal TextureView must not be manually destroyed
28    #[cfg(wgpu_core)]
29    pub unsafe fn as_hal<A: wgc::hal_api::HalApi, F: FnOnce(Option<&A::TextureView>) -> R, R>(
30        &self,
31        hal_texture_view_callback: F,
32    ) -> R {
33        if let Some(ctx) = self
34            .context
35            .as_any()
36            .downcast_ref::<crate::backend::ContextWgpuCore>()
37        {
38            unsafe {
39                ctx.texture_view_as_hal::<A, F, R>(
40                    crate::context::downcast_ref(self.data.as_ref()),
41                    hal_texture_view_callback,
42                )
43            }
44        } else {
45            hal_texture_view_callback(None)
46        }
47    }
48}
49
50impl Drop for TextureView {
51    fn drop(&mut self) {
52        if !thread::panicking() {
53            self.context.texture_view_drop(self.data.as_ref());
54        }
55    }
56}
57
58/// Describes a [`TextureView`].
59///
60/// For use with [`Texture::create_view`].
61///
62/// Corresponds to [WebGPU `GPUTextureViewDescriptor`](
63/// https://gpuweb.github.io/gpuweb/#dictdef-gputextureviewdescriptor).
64#[derive(Clone, Debug, Default, Eq, PartialEq)]
65pub struct TextureViewDescriptor<'a> {
66    /// Debug label of the texture view. This will show up in graphics debuggers for easy identification.
67    pub label: Label<'a>,
68    /// Format of the texture view. Either must be the same as the texture format or in the list
69    /// of `view_formats` in the texture's descriptor.
70    pub format: Option<TextureFormat>,
71    /// The dimension of the texture view. For 1D textures, this must be `D1`. For 2D textures it must be one of
72    /// `D2`, `D2Array`, `Cube`, and `CubeArray`. For 3D textures it must be `D3`
73    pub dimension: Option<TextureViewDimension>,
74    /// Aspect of the texture. Color textures must be [`TextureAspect::All`].
75    pub aspect: TextureAspect,
76    /// Base mip level.
77    pub base_mip_level: u32,
78    /// Mip level count.
79    /// If `Some(count)`, `base_mip_level + count` must be less or equal to underlying texture mip count.
80    /// If `None`, considered to include the rest of the mipmap levels, but at least 1 in total.
81    pub mip_level_count: Option<u32>,
82    /// Base array layer.
83    pub base_array_layer: u32,
84    /// Layer count.
85    /// If `Some(count)`, `base_array_layer + count` must be less or equal to the underlying array count.
86    /// If `None`, considered to include the rest of the array layers, but at least 1 in total.
87    pub array_layer_count: Option<u32>,
88}
89static_assertions::assert_impl_all!(TextureViewDescriptor<'_>: Send, Sync);