wgpu/api/
texture_view.rs

1#[cfg(wgpu_core)]
2use core::ops::Deref;
3
4use crate::*;
5
6/// Handle to a texture view.
7///
8/// A `TextureView` object refers to a [`Texture`], or a subset of its layers and mip levels, and
9/// specifies an interpretation of the texture’s texels, which is needed to use a texture as a
10/// binding in a [`BindGroup`] or as an attachment in a [`RenderPass`].
11/// It can be created using [`Texture::create_view()`], which accepts a [`TextureViewDescriptor`]
12/// specifying the properties of the view.
13///
14/// Corresponds to [WebGPU `GPUTextureView`](https://gpuweb.github.io/gpuweb/#gputextureview).
15#[derive(Debug, Clone)]
16pub struct TextureView {
17    pub(crate) inner: dispatch::DispatchTextureView,
18    pub(crate) texture: Texture,
19}
20#[cfg(send_sync)]
21static_assertions::assert_impl_all!(TextureView: Send, Sync);
22
23crate::cmp::impl_eq_ord_hash_proxy!(TextureView => .inner);
24
25impl TextureView {
26    /// Returns the [`Texture`] that this `TextureView` refers to.
27    ///
28    /// All wgpu resources are refcounted, so you can own the returned [`Texture`]
29    /// by cloning it.
30    pub fn texture(&self) -> &Texture {
31        &self.texture
32    }
33
34    /// Get the [`wgpu_hal`] texture view from this `TextureView`.
35    ///
36    /// Find the Api struct corresponding to the active backend in [`wgpu_hal::api`],
37    /// and pass that struct to the to the `A` type parameter.
38    ///
39    /// Returns a guard that dereferences to the type of the hal backend
40    /// which implements [`A::TextureView`].
41    ///
42    /// # Types
43    ///
44    /// The returned type depends on the backend:
45    ///
46    #[doc = crate::hal_type_vulkan!("TextureView")]
47    #[doc = crate::hal_type_metal!("TextureView")]
48    #[doc = crate::hal_type_dx12!("TextureView")]
49    #[doc = crate::hal_type_gles!("TextureView")]
50    ///
51    /// # Deadlocks
52    ///
53    /// - The returned guard holds a read-lock on a device-local "destruction"
54    ///   lock, which will cause all calls to `destroy` to block until the
55    ///   guard is released.
56    ///
57    /// # Errors
58    ///
59    /// This method will return None if:
60    /// - The texture view is not from the backend specified by `A`.
61    /// - The texture view is from the `webgpu` or `custom` backend.
62    /// - The texture this view points to has had [`Texture::destroy()`] called on it.
63    ///
64    /// # Safety
65    ///
66    /// - The returned resource must not be destroyed unless the guard
67    ///   is the last reference to it and it is not in use by the GPU.
68    ///   The guard and handle may be dropped at any time however.
69    /// - All the safety requirements of wgpu-hal must be upheld.
70    ///
71    /// [`A::TextureView`]: hal::Api::TextureView
72    #[cfg(wgpu_core)]
73    pub unsafe fn as_hal<A: hal::Api>(&self) -> Option<impl Deref<Target = A::TextureView>> {
74        let view = self.inner.as_core_opt()?;
75        unsafe { view.context.texture_view_as_hal::<A>(view) }
76    }
77
78    #[cfg(custom)]
79    /// Returns custom implementation of TextureView (if custom backend and is internally T)
80    pub fn as_custom<T: custom::TextureViewInterface>(&self) -> Option<&T> {
81        self.inner.as_custom()
82    }
83}
84
85/// Describes a [`TextureView`].
86///
87/// For use with [`Texture::create_view`].
88///
89/// Corresponds to [WebGPU `GPUTextureViewDescriptor`](
90/// https://gpuweb.github.io/gpuweb/#dictdef-gputextureviewdescriptor).
91pub type TextureViewDescriptor<'a> = wgt::TextureViewDescriptor<Label<'a>>;
92static_assertions::assert_impl_all!(TextureViewDescriptor<'_>: Send, Sync);