bevy_render/render_resource/
texture.rs1use crate::define_atomic_id;
2use crate::renderer::WgpuWrapper;
3use bevy_derive::{Deref, DerefMut};
4use bevy_ecs::resource::Resource;
5use core::ops::Deref;
6
7define_atomic_id!(TextureId);
8
9#[derive(Clone, Debug)]
23pub struct Texture {
24 id: TextureId,
25 value: WgpuWrapper<wgpu::Texture>,
26}
27
28impl Texture {
29 #[inline]
31 pub fn id(&self) -> TextureId {
32 self.id
33 }
34
35 pub fn create_view(&self, desc: &wgpu::TextureViewDescriptor) -> TextureView {
37 TextureView::from(self.value.create_view(desc))
38 }
39}
40
41impl From<wgpu::Texture> for Texture {
42 fn from(value: wgpu::Texture) -> Self {
43 Texture {
44 id: TextureId::new(),
45 value: WgpuWrapper::new(value),
46 }
47 }
48}
49
50impl Deref for Texture {
51 type Target = wgpu::Texture;
52
53 #[inline]
54 fn deref(&self) -> &Self::Target {
55 &self.value
56 }
57}
58
59define_atomic_id!(TextureViewId);
60
61#[derive(Clone, Debug)]
63pub struct TextureView {
64 id: TextureViewId,
65 value: WgpuWrapper<wgpu::TextureView>,
66}
67
68pub struct SurfaceTexture {
69 value: WgpuWrapper<wgpu::SurfaceTexture>,
70}
71
72impl SurfaceTexture {
73 pub fn present(self) {
74 self.value.into_inner().present();
75 }
76}
77
78impl TextureView {
79 #[inline]
81 pub fn id(&self) -> TextureViewId {
82 self.id
83 }
84}
85
86impl From<wgpu::TextureView> for TextureView {
87 fn from(value: wgpu::TextureView) -> Self {
88 TextureView {
89 id: TextureViewId::new(),
90 value: WgpuWrapper::new(value),
91 }
92 }
93}
94
95impl From<wgpu::SurfaceTexture> for SurfaceTexture {
96 fn from(value: wgpu::SurfaceTexture) -> Self {
97 SurfaceTexture {
98 value: WgpuWrapper::new(value),
99 }
100 }
101}
102
103impl Deref for TextureView {
104 type Target = wgpu::TextureView;
105
106 #[inline]
107 fn deref(&self) -> &Self::Target {
108 &self.value
109 }
110}
111
112impl Deref for SurfaceTexture {
113 type Target = wgpu::SurfaceTexture;
114
115 #[inline]
116 fn deref(&self) -> &Self::Target {
117 &self.value
118 }
119}
120
121define_atomic_id!(SamplerId);
122
123#[derive(Clone, Debug)]
129pub struct Sampler {
130 id: SamplerId,
131 value: WgpuWrapper<wgpu::Sampler>,
132}
133
134impl Sampler {
135 #[inline]
137 pub fn id(&self) -> SamplerId {
138 self.id
139 }
140}
141
142impl From<wgpu::Sampler> for Sampler {
143 fn from(value: wgpu::Sampler) -> Self {
144 Sampler {
145 id: SamplerId::new(),
146 value: WgpuWrapper::new(value),
147 }
148 }
149}
150
151impl Deref for Sampler {
152 type Target = wgpu::Sampler;
153
154 #[inline]
155 fn deref(&self) -> &Self::Target {
156 &self.value
157 }
158}
159
160#[derive(Resource, Debug, Clone, Deref, DerefMut)]
166pub struct DefaultImageSampler(pub(crate) Sampler);