-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.rs
306 lines (261 loc) · 8.98 KB
/
image.rs
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
297
298
299
300
301
302
303
304
305
306
use std::sync::Arc;
pub use wgpu::{Extent3d, ImageDataLayout};
use crate::Context;
pub type ImageFormat = wgpu::TextureFormat;
pub type ImageDimension = wgpu::TextureDimension;
pub type StorageImageAccess = wgpu::StorageTextureAccess;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ImageSampleType {
/// textureLoad returns f32s.
Float { filterable: bool },
/// textureLoad returns u32s.
Uint,
/// textureLoad returns i32s.
Sint,
}
impl From<ImageSampleType> for wgpu::TextureSampleType {
fn from(sample_type: ImageSampleType) -> Self {
match sample_type {
ImageSampleType::Float { filterable } => wgpu::TextureSampleType::Float { filterable },
ImageSampleType::Uint => wgpu::TextureSampleType::Uint,
ImageSampleType::Sint => wgpu::TextureSampleType::Sint,
}
}
}
/// Information to create an Image.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ImageInfo {
/// Size of the image.
///
/// For 2D images set `depht_or_array_layers` to 1.
pub size: Extent3d,
/// Format of the image.
pub format: ImageFormat,
}
/// Handle of an image stored in the GPU.
#[derive(Debug)]
pub struct Image {
pub(crate) device: Arc<crate::Device>,
pub(crate) texture: wgpu::Texture,
pub(crate) view: wgpu::TextureView,
pub(crate) size: Extent3d,
pub(crate) format: ImageFormat,
pub(crate) dimension: ImageDimension,
}
impl Image {
// cheesy workaround to be able to make a const bitflag
// see: https://github.com/bitflags/bitflags/issues/180
const USAGES: wgpu::TextureUsages = wgpu::TextureUsages::from_bits_truncate(
wgpu::TextureUsages::TEXTURE_BINDING.bits()
| wgpu::TextureUsages::STORAGE_BINDING.bits()
| wgpu::TextureUsages::COPY_DST.bits()
| wgpu::TextureUsages::COPY_SRC.bits(),
);
/// Creates an empty image with the specified info.
///
/// # Note
///
/// The easiest way to submit an image to the GPU is by creating an
/// [RgbaImage](https://docs.rs/image/latest/image/type.RgbaImage.html)
/// and using `Image::from_rgba8_image()` (or `Context::image_from_rgba8_img()`)
/// that is unlocked by enabling the "image" feature.
pub fn new(context: &Context, info: &ImageInfo) -> Self {
let dimension = if info.size.depth_or_array_layers == 1 {
wgpu::TextureDimension::D2
} else {
wgpu::TextureDimension::D3
};
let texture = context
.device
.handle
.create_texture(&wgpu::TextureDescriptor {
label: Some("Image"),
usage: Self::USAGES,
mip_level_count: 1,
sample_count: 1,
format: info.format,
size: info.size,
dimension,
});
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
Self {
view,
texture,
dimension,
size: info.size,
format: info.format,
device: Arc::clone(&context.device),
}
}
/// Creates an empty image with the same size and format of the original image.
pub fn empty_like(original: &Self) -> Self {
let &Image {
format,
size,
dimension,
..
} = original;
let texture = original
.device
.handle
.create_texture(&wgpu::TextureDescriptor {
label: Some("Image"),
usage: Self::USAGES,
mip_level_count: 1,
sample_count: 1,
dimension,
format,
size,
});
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
Self {
size,
view,
format,
texture,
dimension,
device: Arc::clone(&original.device),
}
}
/// Writes data to an image.
///
/// # Panics
///
/// - if data overruns the size of the image.
pub fn write(&self, data: &[u8], data_layout: ImageDataLayout, size: Extent3d) {
self.device.queue.write_texture(
wgpu::ImageCopyTexture {
texture: &self.texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
data,
data_layout,
size,
);
}
/// Reads an image to a Vec of bytes.
pub fn read_to_vec(&self) -> Vec<u8> {
// KUDOS to @redwarp I struggled to much trying to copy a texture into a buffer
// https://github.com/redwarp/blog/tree/main/code-sample/image-filters
let bytes_per_pixel = self.format.describe().block_size as usize;
let Extent3d { width, height, .. } = self.size;
let padded_bytes_per_row = {
let bytes_per_row = bytes_per_pixel * width as usize;
let padding = (256 - bytes_per_row % 256) % 256;
bytes_per_row + padding
};
let unpadded_bytes_per_row = bytes_per_pixel * width as usize;
let output_buffer_size =
padded_bytes_per_row as u64 * height as u64 * std::mem::size_of::<u8>() as u64;
let dst_buffer = self.device.handle.create_buffer(&wgpu::BufferDescriptor {
label: Some("Destination copy buffer"),
size: output_buffer_size,
usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
mapped_at_creation: false,
});
let mut encoder =
self.device
.handle
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
label: Some("Copy buffer command encoder"),
});
encoder.copy_texture_to_buffer(
wgpu::ImageCopyTexture {
texture: &self.texture,
mip_level: 0,
origin: wgpu::Origin3d::ZERO,
aspect: wgpu::TextureAspect::All,
},
wgpu::ImageCopyBuffer {
buffer: &dst_buffer,
layout: wgpu::ImageDataLayout {
offset: 0,
bytes_per_row: std::num::NonZeroU32::new(padded_bytes_per_row as u32),
rows_per_image: None,
},
},
self.size,
);
self.device.queue.submit(std::iter::once(encoder.finish()));
let dst_slice = dst_buffer.slice(..);
dst_slice.map_async(wgpu::MapMode::Read, move |_| {});
self.device.handle.poll(wgpu::Maintain::Wait);
let mut pixels = vec![0; unpadded_bytes_per_row * height as usize];
dst_slice
.get_mapped_range()
.chunks_exact(padded_bytes_per_row)
.zip(pixels.chunks_exact_mut(unpadded_bytes_per_row))
.for_each(|(padded, pixels)| {
pixels.copy_from_slice(&padded[..unpadded_bytes_per_row]);
});
pixels
}
/// Size of the image.
pub fn size(&self) -> Extent3d {
self.size
}
/// Format of the image.
pub fn format(&self) -> ImageFormat {
self.format
}
/// Dimension of the image.
pub fn dimension(&self) -> ImageDimension {
self.dimension
}
#[cfg(feature = "from_image")]
/// Creates an image from an Rgba8 image buffer.
///
/// The `sample_type` parameter is used to choose the correct image format.
pub fn from_rgba8_image(
context: &Context,
image: &image::RgbaImage,
sample_type: ImageSampleType,
) -> Self {
let (width, height) = image.dimensions();
let size = Extent3d {
width,
height,
depth_or_array_layers: 1,
};
let format = match sample_type {
ImageSampleType::Float { .. } => ImageFormat::Rgba8Unorm,
ImageSampleType::Uint => ImageFormat::Rgba8Uint,
ImageSampleType::Sint => ImageFormat::Rgba8Sint,
};
let dimension = ImageDimension::D2;
let texture = context
.device
.handle
.create_texture(&wgpu::TextureDescriptor {
label: Some("texture"),
usage: Self::USAGES,
mip_level_count: 1,
sample_count: 1,
dimension,
format,
size,
});
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
let self_ = Self {
size,
view,
format,
texture,
dimension,
device: Arc::clone(&context.device),
};
let bytes_per_pixel = 4;
self_.write(
image,
ImageDataLayout {
offset: 0,
bytes_per_row: std::num::NonZeroU32::new(width * bytes_per_pixel),
rows_per_image: None,
},
size,
);
self_
}
}