1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//! On-GPU textures.
use anyhow::Result;
/// Raw texture without a bind group.
pub struct RawTexture {
pub texture: wgpu::Texture,
pub view: wgpu::TextureView,
pub sampler: wgpu::Sampler,
pub label: Option<&'static str>,
pub dimensions: (u32, u32),
}
impl RawTexture {
pub const DEPTH_FORMAT: wgpu::TextureFormat = wgpu::TextureFormat::Depth32Float; // 1.
/// Create a depth texture.
///
/// # Arguments
///
/// * `device` - Device to create the texture on.
/// * `config` - Framebuffer configuration.
/// * `label` - Optional label to identify the texture.
pub fn create_depth_texture(
device: &wgpu::Device,
config: &wgpu::SurfaceConfiguration,
label: &'static str,
sample_count: u32,
) -> Self {
let size = wgpu::Extent3d {
width: config.width,
height: config.height,
depth_or_array_layers: 1,
};
let desc = wgpu::TextureDescriptor {
label: Some(label),
size,
mip_level_count: 1,
sample_count,
dimension: wgpu::TextureDimension::D2,
format: Self::DEPTH_FORMAT,
usage: wgpu::TextureUsages::RENDER_ATTACHMENT | wgpu::TextureUsages::TEXTURE_BINDING,
};
let texture = device.create_texture(&desc);
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Linear,
mipmap_filter: wgpu::FilterMode::Nearest,
compare: Some(wgpu::CompareFunction::LessEqual),
lod_min_clamp: -100.0,
lod_max_clamp: 100.0,
..Default::default()
});
Self {
texture,
view,
sampler,
label: Some(label),
dimensions: (config.width, config.height),
}
}
/// Create a uninitialised texture.
///
/// # Arguments
///
/// * `device` - Device to create the texture on.
/// * `label` - Optional label to identify the texture.
/// * `dimensions` - Width and height of the texture.
/// * `sample_count` - MSAA samples.
/// * `format` - Texture format.
/// * `usage` - Texture usage.
pub fn create_custom(
device: &wgpu::Device,
label: Option<&'static str>,
dimensions: (u32, u32),
sample_count: u32,
format: wgpu::TextureFormat,
usage: wgpu::TextureUsages,
) -> Self {
let size = wgpu::Extent3d {
width: dimensions.0,
height: dimensions.1,
depth_or_array_layers: 1,
};
let texture = device.create_texture(&wgpu::TextureDescriptor {
label,
size,
mip_level_count: 1,
sample_count,
dimension: wgpu::TextureDimension::D2,
format,
usage,
});
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
address_mode_w: wgpu::AddressMode::ClampToEdge,
mag_filter: wgpu::FilterMode::Linear,
min_filter: wgpu::FilterMode::Nearest,
mipmap_filter: wgpu::FilterMode::Nearest,
..Default::default()
});
Self {
texture,
view,
sampler,
label,
dimensions,
}
}
/// Create a texture from RGBA image.
///
/// # Arguments
///
/// * `device` - Device to create the texture on.
/// * `queue` - Command queue to write the texture through.
/// * `label` - Optional identification label.
/// * `rgba` - Image data.
/// * `height` - Height of the texture.
pub fn from_rgba_frame(
device: &wgpu::Device,
queue: &wgpu::Queue,
label: Option<&'static str>,
rgba: &[u8],
height: usize,
) -> Result<Self> {
let height = height as u32;
let width = (rgba.len() / 4) as u32 / height;
let dimensions = (width, height);
let size = wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
};
let ret = Self::create_custom(
device,
label,
dimensions,
1,
wgpu::TextureFormat::Rgba8UnormSrgb,
wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
);
queue.write_texture(
wgpu::ImageCopyTexture {
aspect: wgpu::TextureAspect::All,
texture: &ret.texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
},
rgba,
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: std::num::NonZeroU32::new(4 * width),
rows_per_image: std::num::NonZeroU32::new(height),
},
size,
);
Ok(ret)
}
}
/// Texture with a bind group.
///
/// This is the typical texture object to be used, as the bind group is needed for applying the
/// texture to rendered objects.
pub struct Texture {
pub raw: RawTexture,
pub bind_group: wgpu::BindGroup,
}
impl Texture {
/// Update the texture with new image.
///
/// This function will update the existing buffers if the dimensions match, or create new
/// buffers with new dimensions.
///
/// # Arguments
///
/// * `device` - Device of the texture. Must match the original one.
/// * `queue` - Command queue for writing the data. Must match the original one.
/// * `bind_group_layout` - Layout for binding this texture to a shader. Must match the
/// original one.
/// * `rgba` - New image data.
/// * `height` - Height of this image.
pub fn update_texture(
self,
device: &wgpu::Device,
queue: &wgpu::Queue,
bind_group_layout: &wgpu::BindGroupLayout,
rgba: &[u8],
height: usize,
) -> Result<Self> {
let height = height as u32;
let width = (rgba.len() / 4) as u32 / height;
let dimensions = (width, height);
if dimensions == self.raw.dimensions {
let size = wgpu::Extent3d {
width,
height,
depth_or_array_layers: 1,
};
queue.write_texture(
wgpu::ImageCopyTexture {
aspect: wgpu::TextureAspect::All,
texture: &self.raw.texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
},
rgba,
wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: std::num::NonZeroU32::new(4 * width),
rows_per_image: std::num::NonZeroU32::new(height),
},
size,
);
Ok(self)
} else {
Self::from_rgba_frame(
device,
queue,
bind_group_layout,
self.raw.label,
rgba,
height as usize,
)
}
}
/// Create a texture from RGBA image.
///
/// # Arguments
///
/// * `device` - Device to create the texture on.
/// * `queue` - Command queue to write the texture through.
/// * `bind_group_layout` - Layout for binding this texture.
/// * `label` - Optional identification label.
/// * `rgba` - Image data.
/// * `height` - Height of the texture.
pub fn from_rgba_frame(
device: &wgpu::Device,
queue: &wgpu::Queue,
bind_group_layout: &wgpu::BindGroupLayout,
label: Option<&'static str>,
rgba: &[u8],
height: usize,
) -> Result<Self> {
let raw = RawTexture::from_rgba_frame(device, queue, label, rgba, height)?;
let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
layout: bind_group_layout,
entries: &[
wgpu::BindGroupEntry {
binding: 0,
resource: wgpu::BindingResource::TextureView(&raw.view),
},
wgpu::BindGroupEntry {
binding: 1,
resource: wgpu::BindingResource::Sampler(&raw.sampler),
},
],
label: Some("diffuse_bind_group"),
});
Ok(Self { raw, bind_group })
}
}
impl core::ops::Deref for Texture {
type Target = RawTexture;
fn deref(&self) -> &Self::Target {
&self.raw
}
}