bevy_render/render_resource/
texture.rs

1use crate::define_atomic_id;
2use std::ops::Deref;
3
4use crate::render_resource::resource_macros::*;
5
6define_atomic_id!(TextureId);
7render_resource_wrapper!(ErasedTexture, wgpu::Texture);
8
9/// A GPU-accessible texture.
10///
11/// May be converted from and dereferences to a wgpu [`Texture`](wgpu::Texture).
12/// Can be created via [`RenderDevice::create_texture`](crate::renderer::RenderDevice::create_texture).
13#[derive(Clone, Debug)]
14pub struct Texture {
15    id: TextureId,
16    value: ErasedTexture,
17}
18
19impl Texture {
20    /// Returns the [`TextureId`].
21    #[inline]
22    pub fn id(&self) -> TextureId {
23        self.id
24    }
25
26    /// Creates a view of this texture.
27    pub fn create_view(&self, desc: &wgpu::TextureViewDescriptor) -> TextureView {
28        TextureView::from(self.value.create_view(desc))
29    }
30}
31
32impl From<wgpu::Texture> for Texture {
33    fn from(value: wgpu::Texture) -> Self {
34        Texture {
35            id: TextureId::new(),
36            value: ErasedTexture::new(value),
37        }
38    }
39}
40
41impl Deref for Texture {
42    type Target = wgpu::Texture;
43
44    #[inline]
45    fn deref(&self) -> &Self::Target {
46        &self.value
47    }
48}
49
50define_atomic_id!(TextureViewId);
51render_resource_wrapper!(ErasedTextureView, wgpu::TextureView);
52render_resource_wrapper!(ErasedSurfaceTexture, wgpu::SurfaceTexture);
53
54/// Describes a [`Texture`] with its associated metadata required by a pipeline or [`BindGroup`](super::BindGroup).
55#[derive(Clone, Debug)]
56pub struct TextureView {
57    id: TextureViewId,
58    value: ErasedTextureView,
59}
60
61pub struct SurfaceTexture {
62    value: ErasedSurfaceTexture,
63}
64
65impl SurfaceTexture {
66    pub fn try_unwrap(self) -> Option<wgpu::SurfaceTexture> {
67        self.value.try_unwrap()
68    }
69}
70
71impl TextureView {
72    /// Returns the [`TextureViewId`].
73    #[inline]
74    pub fn id(&self) -> TextureViewId {
75        self.id
76    }
77}
78
79impl From<wgpu::TextureView> for TextureView {
80    fn from(value: wgpu::TextureView) -> Self {
81        TextureView {
82            id: TextureViewId::new(),
83            value: ErasedTextureView::new(value),
84        }
85    }
86}
87
88impl From<wgpu::SurfaceTexture> for SurfaceTexture {
89    fn from(value: wgpu::SurfaceTexture) -> Self {
90        SurfaceTexture {
91            value: ErasedSurfaceTexture::new(value),
92        }
93    }
94}
95
96impl Deref for TextureView {
97    type Target = wgpu::TextureView;
98
99    #[inline]
100    fn deref(&self) -> &Self::Target {
101        &self.value
102    }
103}
104
105impl Deref for SurfaceTexture {
106    type Target = wgpu::SurfaceTexture;
107
108    #[inline]
109    fn deref(&self) -> &Self::Target {
110        &self.value
111    }
112}
113
114define_atomic_id!(SamplerId);
115render_resource_wrapper!(ErasedSampler, wgpu::Sampler);
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: ErasedSampler,
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: ErasedSampler::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}