bevy_render/render_resource/
texture.rs

1use crate::define_atomic_id;
2use crate::renderer::WgpuWrapper;
3use alloc::sync::Arc;
4use bevy_derive::{Deref, DerefMut};
5use bevy_ecs::system::Resource;
6use core::ops::Deref;
7
8define_atomic_id!(TextureId);
9
10/// A GPU-accessible texture.
11///
12/// May be converted from and dereferences to a wgpu [`Texture`](wgpu::Texture).
13/// Can be created via [`RenderDevice::create_texture`](crate::renderer::RenderDevice::create_texture).
14#[derive(Clone, Debug)]
15pub struct Texture {
16    id: TextureId,
17    value: Arc<WgpuWrapper<wgpu::Texture>>,
18}
19
20impl Texture {
21    /// Returns the [`TextureId`].
22    #[inline]
23    pub fn id(&self) -> TextureId {
24        self.id
25    }
26
27    /// Creates a view of this texture.
28    pub fn create_view(&self, desc: &wgpu::TextureViewDescriptor) -> TextureView {
29        TextureView::from(self.value.create_view(desc))
30    }
31}
32
33impl From<wgpu::Texture> for Texture {
34    fn from(value: wgpu::Texture) -> Self {
35        Texture {
36            id: TextureId::new(),
37            value: Arc::new(WgpuWrapper::new(value)),
38        }
39    }
40}
41
42impl Deref for Texture {
43    type Target = wgpu::Texture;
44
45    #[inline]
46    fn deref(&self) -> &Self::Target {
47        &self.value
48    }
49}
50
51define_atomic_id!(TextureViewId);
52
53/// Describes a [`Texture`] with its associated metadata required by a pipeline or [`BindGroup`](super::BindGroup).
54#[derive(Clone, Debug)]
55pub struct TextureView {
56    id: TextureViewId,
57    value: Arc<WgpuWrapper<wgpu::TextureView>>,
58}
59
60pub struct SurfaceTexture {
61    value: Arc<WgpuWrapper<wgpu::SurfaceTexture>>,
62}
63
64impl SurfaceTexture {
65    pub fn try_unwrap(self) -> Option<wgpu::SurfaceTexture> {
66        Arc::try_unwrap(self.value)
67            .map(WgpuWrapper::into_inner)
68            .ok()
69    }
70}
71
72impl TextureView {
73    /// Returns the [`TextureViewId`].
74    #[inline]
75    pub fn id(&self) -> TextureViewId {
76        self.id
77    }
78}
79
80impl From<wgpu::TextureView> for TextureView {
81    fn from(value: wgpu::TextureView) -> Self {
82        TextureView {
83            id: TextureViewId::new(),
84            value: Arc::new(WgpuWrapper::new(value)),
85        }
86    }
87}
88
89impl From<wgpu::SurfaceTexture> for SurfaceTexture {
90    fn from(value: wgpu::SurfaceTexture) -> Self {
91        SurfaceTexture {
92            value: Arc::new(WgpuWrapper::new(value)),
93        }
94    }
95}
96
97impl Deref for TextureView {
98    type Target = wgpu::TextureView;
99
100    #[inline]
101    fn deref(&self) -> &Self::Target {
102        &self.value
103    }
104}
105
106impl Deref for SurfaceTexture {
107    type Target = wgpu::SurfaceTexture;
108
109    #[inline]
110    fn deref(&self) -> &Self::Target {
111        &self.value
112    }
113}
114
115define_atomic_id!(SamplerId);
116
117/// A Sampler defines how a pipeline will sample from a [`TextureView`].
118/// They define image filters (including anisotropy) and address (wrapping) modes, among other things.
119///
120/// May be converted from and dereferences to a wgpu [`Sampler`](wgpu::Sampler).
121/// Can be created via [`RenderDevice::create_sampler`](crate::renderer::RenderDevice::create_sampler).
122#[derive(Clone, Debug)]
123pub struct Sampler {
124    id: SamplerId,
125    value: Arc<WgpuWrapper<wgpu::Sampler>>,
126}
127
128impl Sampler {
129    /// Returns the [`SamplerId`].
130    #[inline]
131    pub fn id(&self) -> SamplerId {
132        self.id
133    }
134}
135
136impl From<wgpu::Sampler> for Sampler {
137    fn from(value: wgpu::Sampler) -> Self {
138        Sampler {
139            id: SamplerId::new(),
140            value: Arc::new(WgpuWrapper::new(value)),
141        }
142    }
143}
144
145impl Deref for Sampler {
146    type Target = wgpu::Sampler;
147
148    #[inline]
149    fn deref(&self) -> &Self::Target {
150        &self.value
151    }
152}
153
154/// A rendering resource for the default image sampler which is set during renderer
155/// initialization.
156///
157/// The [`ImagePlugin`](crate::texture::ImagePlugin) can be set during app initialization to change the default
158/// image sampler.
159#[derive(Resource, Debug, Clone, Deref, DerefMut)]
160pub struct DefaultImageSampler(pub(crate) Sampler);