Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_render/src/render_resource/texture.rs
6596 views
1
use crate::define_atomic_id;
2
use crate::renderer::WgpuWrapper;
3
use bevy_derive::{Deref, DerefMut};
4
use bevy_ecs::resource::Resource;
5
use core::ops::Deref;
6
7
define_atomic_id!(TextureId);
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
///
14
/// Other options for storing GPU-accessible data are:
15
/// * [`BufferVec`](crate::render_resource::BufferVec)
16
/// * [`DynamicStorageBuffer`](crate::render_resource::DynamicStorageBuffer)
17
/// * [`DynamicUniformBuffer`](crate::render_resource::DynamicUniformBuffer)
18
/// * [`GpuArrayBuffer`](crate::render_resource::GpuArrayBuffer)
19
/// * [`RawBufferVec`](crate::render_resource::RawBufferVec)
20
/// * [`StorageBuffer`](crate::render_resource::StorageBuffer)
21
/// * [`UniformBuffer`](crate::render_resource::UniformBuffer)
22
#[derive(Clone, Debug)]
23
pub struct Texture {
24
id: TextureId,
25
value: WgpuWrapper<wgpu::Texture>,
26
}
27
28
impl Texture {
29
/// Returns the [`TextureId`].
30
#[inline]
31
pub fn id(&self) -> TextureId {
32
self.id
33
}
34
35
/// Creates a view of this texture.
36
pub fn create_view(&self, desc: &wgpu::TextureViewDescriptor) -> TextureView {
37
TextureView::from(self.value.create_view(desc))
38
}
39
}
40
41
impl 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
50
impl Deref for Texture {
51
type Target = wgpu::Texture;
52
53
#[inline]
54
fn deref(&self) -> &Self::Target {
55
&self.value
56
}
57
}
58
59
define_atomic_id!(TextureViewId);
60
61
/// Describes a [`Texture`] with its associated metadata required by a pipeline or [`BindGroup`](super::BindGroup).
62
#[derive(Clone, Debug)]
63
pub struct TextureView {
64
id: TextureViewId,
65
value: WgpuWrapper<wgpu::TextureView>,
66
}
67
68
pub struct SurfaceTexture {
69
value: WgpuWrapper<wgpu::SurfaceTexture>,
70
}
71
72
impl SurfaceTexture {
73
pub fn present(self) {
74
self.value.into_inner().present();
75
}
76
}
77
78
impl TextureView {
79
/// Returns the [`TextureViewId`].
80
#[inline]
81
pub fn id(&self) -> TextureViewId {
82
self.id
83
}
84
}
85
86
impl 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
95
impl From<wgpu::SurfaceTexture> for SurfaceTexture {
96
fn from(value: wgpu::SurfaceTexture) -> Self {
97
SurfaceTexture {
98
value: WgpuWrapper::new(value),
99
}
100
}
101
}
102
103
impl Deref for TextureView {
104
type Target = wgpu::TextureView;
105
106
#[inline]
107
fn deref(&self) -> &Self::Target {
108
&self.value
109
}
110
}
111
112
impl Deref for SurfaceTexture {
113
type Target = wgpu::SurfaceTexture;
114
115
#[inline]
116
fn deref(&self) -> &Self::Target {
117
&self.value
118
}
119
}
120
121
define_atomic_id!(SamplerId);
122
123
/// A Sampler defines how a pipeline will sample from a [`TextureView`].
124
/// They define image filters (including anisotropy) and address (wrapping) modes, among other things.
125
///
126
/// May be converted from and dereferences to a wgpu [`Sampler`](wgpu::Sampler).
127
/// Can be created via [`RenderDevice::create_sampler`](crate::renderer::RenderDevice::create_sampler).
128
#[derive(Clone, Debug)]
129
pub struct Sampler {
130
id: SamplerId,
131
value: WgpuWrapper<wgpu::Sampler>,
132
}
133
134
impl Sampler {
135
/// Returns the [`SamplerId`].
136
#[inline]
137
pub fn id(&self) -> SamplerId {
138
self.id
139
}
140
}
141
142
impl 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
151
impl Deref for Sampler {
152
type Target = wgpu::Sampler;
153
154
#[inline]
155
fn deref(&self) -> &Self::Target {
156
&self.value
157
}
158
}
159
160
/// A rendering resource for the default image sampler which is set during renderer
161
/// initialization.
162
///
163
/// The [`ImagePlugin`](bevy_image::ImagePlugin) can be set during app initialization to change the default
164
/// image sampler.
165
#[derive(Resource, Debug, Clone, Deref, DerefMut)]
166
pub struct DefaultImageSampler(pub(crate) Sampler);
167
168