Skip to main content

wgpu/api/
external_texture.rs

1use crate::*;
2
3/// Handle to an external texture on the GPU.
4///
5/// It can be created with [`Device::create_external_texture`].
6///
7/// Corresponds to [WebGPU `GPUExternalTexture`](https://gpuweb.github.io/gpuweb/#gpuexternaltexture).
8#[derive(Debug, Clone)]
9pub struct ExternalTexture {
10    pub(crate) inner: dispatch::DispatchExternalTexture,
11}
12#[cfg(send_sync)]
13static_assertions::assert_impl_all!(ExternalTexture: Send, Sync);
14
15crate::cmp::impl_eq_ord_hash_proxy!(ExternalTexture => .inner);
16
17impl ExternalTexture {
18    /// Destroy the associated native resources as soon as possible.
19    pub fn destroy(&self) {
20        self.inner.destroy();
21    }
22
23    /// Returns custom implementation of ExternalTexture (if custom backend and is internally T)
24    #[cfg(custom)]
25    pub fn as_custom<T: custom::ExternalTextureInterface>(&self) -> Option<&T> {
26        self.inner.as_custom()
27    }
28}
29
30#[cfg(wgpu_core)]
31impl ExternalTexture {
32    /// Create a new external texture of wgpu from a wgpu-core external texture.
33    ///
34    /// # Arguments
35    ///
36    /// - `core_external_texture` - wgpu-core external texture.
37    pub fn from_core(
38        core_external_texture: alloc::sync::Arc<wgc::resource::ExternalTexture>,
39    ) -> Self {
40        Self {
41            inner: crate::backend::wgpu_core::CoreExternalTexture::from_core(core_external_texture)
42                .into(),
43        }
44    }
45
46    /// Returns the underlying wgpu-core external texture if this `ExternalTexture` is on the wgpu-core backend, otherwise `None`.
47    pub fn as_core(&self) -> Option<alloc::sync::Arc<wgc::resource::ExternalTexture>> {
48        self.inner.as_core_opt().map(|et| et.as_core())
49    }
50}
51/// Describes an [`ExternalTexture`].
52///
53/// For use with [`Device::create_external_texture`].
54///
55/// Corresponds to [WebGPU `GPUExternalTextureDescriptor`](
56/// https://gpuweb.github.io/gpuweb/#dictdef-gpuexternaltexturedescriptor).
57pub type ExternalTextureDescriptor<'a> = wgt::ExternalTextureDescriptor<Label<'a>>;
58static_assertions::assert_impl_all!(ExternalTextureDescriptor<'_>: Send, Sync);