wgpu/api/texture.rs
1use crate::*;
2
3/// Handle to a texture on the GPU.
4///
5/// It can be created with [`Device::create_texture`].
6///
7/// Corresponds to [WebGPU `GPUTexture`](https://gpuweb.github.io/gpuweb/#texture-interface).
8#[derive(Debug, Clone)]
9pub struct Texture {
10 pub(crate) inner: dispatch::DispatchTexture,
11 pub(crate) descriptor: TextureDescriptor<'static>,
12}
13#[cfg(send_sync)]
14static_assertions::assert_impl_all!(Texture: Send, Sync);
15
16crate::cmp::impl_eq_ord_hash_proxy!(Texture => .inner);
17
18impl Texture {
19 /// Returns the inner hal Texture using a callback. The hal texture will be `None` if the
20 /// backend type argument does not match with this wgpu Texture
21 ///
22 /// # Safety
23 ///
24 /// - The raw handle obtained from the hal Texture must not be manually destroyed
25 #[cfg(wgpu_core)]
26 pub unsafe fn as_hal<A: wgc::hal_api::HalApi, F: FnOnce(Option<&A::Texture>) -> R, R>(
27 &self,
28 hal_texture_callback: F,
29 ) -> R {
30 if let Some(tex) = self.inner.as_core_opt() {
31 unsafe {
32 tex.context
33 .texture_as_hal::<A, F, R>(tex, hal_texture_callback)
34 }
35 } else {
36 hal_texture_callback(None)
37 }
38 }
39
40 /// Creates a view of this texture.
41 pub fn create_view(&self, desc: &TextureViewDescriptor<'_>) -> TextureView {
42 let view = self.inner.create_view(desc);
43
44 TextureView { inner: view }
45 }
46
47 /// Destroy the associated native resources as soon as possible.
48 pub fn destroy(&self) {
49 self.inner.destroy();
50 }
51
52 /// Make an `TexelCopyTextureInfo` representing the whole texture.
53 pub fn as_image_copy(&self) -> TexelCopyTextureInfo<'_> {
54 TexelCopyTextureInfo {
55 texture: self,
56 mip_level: 0,
57 origin: Origin3d::ZERO,
58 aspect: TextureAspect::All,
59 }
60 }
61
62 /// Returns the size of this `Texture`.
63 ///
64 /// This is always equal to the `size` that was specified when creating the texture.
65 pub fn size(&self) -> Extent3d {
66 self.descriptor.size
67 }
68
69 /// Returns the width of this `Texture`.
70 ///
71 /// This is always equal to the `size.width` that was specified when creating the texture.
72 pub fn width(&self) -> u32 {
73 self.descriptor.size.width
74 }
75
76 /// Returns the height of this `Texture`.
77 ///
78 /// This is always equal to the `size.height` that was specified when creating the texture.
79 pub fn height(&self) -> u32 {
80 self.descriptor.size.height
81 }
82
83 /// Returns the depth or layer count of this `Texture`.
84 ///
85 /// This is always equal to the `size.depth_or_array_layers` that was specified when creating the texture.
86 pub fn depth_or_array_layers(&self) -> u32 {
87 self.descriptor.size.depth_or_array_layers
88 }
89
90 /// Returns the mip_level_count of this `Texture`.
91 ///
92 /// This is always equal to the `mip_level_count` that was specified when creating the texture.
93 pub fn mip_level_count(&self) -> u32 {
94 self.descriptor.mip_level_count
95 }
96
97 /// Returns the sample_count of this `Texture`.
98 ///
99 /// This is always equal to the `sample_count` that was specified when creating the texture.
100 pub fn sample_count(&self) -> u32 {
101 self.descriptor.sample_count
102 }
103
104 /// Returns the dimension of this `Texture`.
105 ///
106 /// This is always equal to the `dimension` that was specified when creating the texture.
107 pub fn dimension(&self) -> TextureDimension {
108 self.descriptor.dimension
109 }
110
111 /// Returns the format of this `Texture`.
112 ///
113 /// This is always equal to the `format` that was specified when creating the texture.
114 pub fn format(&self) -> TextureFormat {
115 self.descriptor.format
116 }
117
118 /// Returns the allowed usages of this `Texture`.
119 ///
120 /// This is always equal to the `usage` that was specified when creating the texture.
121 pub fn usage(&self) -> TextureUsages {
122 self.descriptor.usage
123 }
124}
125
126/// Describes a [`Texture`].
127///
128/// For use with [`Device::create_texture`].
129///
130/// Corresponds to [WebGPU `GPUTextureDescriptor`](
131/// https://gpuweb.github.io/gpuweb/#dictdef-gputexturedescriptor).
132pub type TextureDescriptor<'a> = wgt::TextureDescriptor<Label<'a>, &'a [TextureFormat]>;
133static_assertions::assert_impl_all!(TextureDescriptor<'_>: Send, Sync);