Debian Codegraph @

third_party/rust/wgpu-types/src/transfers.rs

raw
1 use crate::{BufferAddress, Extent3d, TexelCopyBufferLayout, TextureAspect, TextureFormat};
2
3 impl TexelCopyBufferLayout {
4 /// Extract a variety of information about the given copy operation.
5 #[inline(always)]
6 pub fn get_buffer_texture_copy_info(
7 &self,
8 format: TextureFormat,
9 aspect: TextureAspect,
10 copy_size: &Extent3d,
11 ) -> BufferTextureCopyInfo {
12 // Convert all inputs to BufferAddress (u64) to avoid some of the overflow issues
13 // Note: u64 is not always enough to prevent overflow, especially when multiplying
14 // something with a potentially large depth value, so it is preferable to validate
15 // the copy size before calling this function (for example via `validate_texture_copy_range`).
16 let copy_width = copy_size.width as BufferAddress;
17 let copy_height = copy_size.height as BufferAddress;
18 let depth_or_array_layers = copy_size.depth_or_array_layers as BufferAddress;
19
20 let block_size_bytes = format.block_copy_size(Some(aspect)).unwrap() as BufferAddress;
21 let (block_width, block_height) = format.block_dimensions();
22 let block_width_texels = block_width as BufferAddress;
23 let block_height_texels = block_height as BufferAddress;
24
25 let width_blocks = copy_width.div_ceil(block_width_texels);
26 let height_blocks = copy_height.div_ceil(block_height_texels);
27
28 let row_bytes_dense = width_blocks * block_size_bytes;
29
30 let row_stride_bytes = self.bytes_per_row.map_or(row_bytes_dense, u64::from);
31 let image_stride_rows = self.rows_per_image.map_or(height_blocks, u64::from);
32
33 let image_stride_bytes = row_stride_bytes * image_stride_rows;
34
35 let image_rows_dense = height_blocks;
36 let image_bytes_dense =
37 image_rows_dense.saturating_sub(1) * row_stride_bytes + row_bytes_dense;
38
39 let mut bytes_in_copy = image_stride_bytes * depth_or_array_layers.saturating_sub(1);
40 if height_blocks > 0 {
41 bytes_in_copy += row_stride_bytes * (height_blocks - 1) + row_bytes_dense;
42 }
43
44 BufferTextureCopyInfo {
45 copy_width,
46 copy_height,
47 depth_or_array_layers,
48
49 offset: self.offset,
50
51 block_size_bytes,
52 block_width_texels,
53 block_height_texels,
54
55 width_blocks,
56 height_blocks,
57
58 row_bytes_dense,
59 row_stride_bytes,
60
61 image_stride_rows,
62 image_stride_bytes,
63
64 image_rows_dense,
65 image_bytes_dense,
66
67 bytes_in_copy,
68 }
69 }
70 }
71
72 /// Information about a copy between a buffer and a texture.
73 ///
74 /// Mostly used for internal calculations, but useful nonetheless.
75 /// Generated by [`TexelCopyBufferLayout::get_buffer_texture_copy_info`].
76 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
77 pub struct BufferTextureCopyInfo {
78 /// The width of the copy region in pixels.
79 pub copy_width: u64,
80 /// The height of the copy region in pixels.
81 pub copy_height: u64,
82 /// The depth of the copy region in pixels.
83 pub depth_or_array_layers: u64,
84
85 /// The offset in the buffer where the copy starts.
86 pub offset: u64,
87
88 /// The size of a single texture texel block in bytes.
89 pub block_size_bytes: u64,
90 /// The number of texel in a texel block in the x direction.
91 pub block_width_texels: u64,
92 /// The number of texel in a texel block in the y direction.
93 pub block_height_texels: u64,
94
95 /// The width of the copy region in blocks.
96 pub width_blocks: u64,
97 /// The height of the copy region in blocks.
98 pub height_blocks: u64,
99
100 /// The number of bytes in the last row of the copy region.
101 pub row_bytes_dense: u64,
102 /// The stride in bytes between the start of one row in an image and the next row in the same image.
103 ///
104 /// This includes any padding between one row and the next row.
105 pub row_stride_bytes: u64,
106
107 /// The stride in rows between the start of one image and the next image.
108 pub image_stride_rows: u64,
109 /// The stride in bytes between the start of one image and the next image.
110 pub image_stride_bytes: u64,
111
112 /// The number of rows in a densely packed list of images.
113 ///
114 /// This is the number of rows in the image that are actually used for texel data,
115 /// and does not include any padding rows, unlike `image_stride_rows`.
116 pub image_rows_dense: u64,
117 /// The number of bytes in a densely packed list of images.
118 ///
119 /// This is the number of bytes in the image that are actually used for texel data,
120 /// or are used for padding between _rows_. Padding at the end of the last row and
121 /// between _images_ is not included.
122 pub image_bytes_dense: u64,
123
124 /// The total number of bytes in the copy region.
125 ///
126 /// This includes all padding except the padding after the last row in the copy.
127 pub bytes_in_copy: u64,
128 }
129
130 #[cfg(test)]
131 mod tests {
132 use super::*;
133
134 struct LTDTest {
135 layout: TexelCopyBufferLayout,
136 format: TextureFormat,
137 aspect: TextureAspect,
138 copy_size: Extent3d,
139 expected_result: BufferTextureCopyInfo,
140 }
141
142 impl LTDTest {
143 #[track_caller]
144 fn run(&self) {
145 let linear_texture_data =
146 self.layout
147 .get_buffer_texture_copy_info(self.format, self.aspect, &self.copy_size);
148 assert_eq!(linear_texture_data, self.expected_result);
149 }
150 }
151
152 #[test]
153 fn linear_texture_data_1d_copy() {
154 let mut test = LTDTest {
155 layout: TexelCopyBufferLayout {
156 offset: 0,
157 bytes_per_row: None,
158 rows_per_image: None,
159 },
160 format: TextureFormat::Rgba8Unorm,
161 aspect: TextureAspect::All,
162 copy_size: Extent3d {
163 width: 4,
164 height: 1,
165 depth_or_array_layers: 1,
166 },
167 expected_result: BufferTextureCopyInfo {
168 copy_width: 4,
169 copy_height: 1,
170 depth_or_array_layers: 1,
171 offset: 0,
172 block_size_bytes: 4,
173 block_width_texels: 1,
174 block_height_texels: 1,
175 width_blocks: 4,
176 height_blocks: 1,
177 row_bytes_dense: 16,
178 row_stride_bytes: 16,
179 image_stride_rows: 1,
180 image_stride_bytes: 16,
181 image_rows_dense: 1,
182 image_bytes_dense: 16,
183 bytes_in_copy: 16,
184 },
185 };
186
187 test.run();
188
189 // Changing bytes_per_row should only change the bytes_per_row, not the bytes_in_copy
190 // as that is only affected by the last row size.
191 test.layout.bytes_per_row = Some(32);
192 test.expected_result.row_stride_bytes = 32;
193 test.expected_result.image_stride_bytes = 32;
194
195 test.run();
196
197 // Changing rows_per_image should only change the rows_per_image and bytes_per_image, nothing else
198 test.layout.rows_per_image = Some(4);
199 test.expected_result.image_stride_bytes = 128; // 32 * 4
200 test.expected_result.image_stride_rows = 4;
201
202 test.run();
203
204 // Changing the offset should change nothing.
205
206 test.layout.offset = 4;
207 test.expected_result.offset = 4;
208
209 test.run();
210 }
211
212 #[test]
213 fn linear_texture_data_2d_3d_copy() {
214 let mut test = LTDTest {
215 layout: TexelCopyBufferLayout {
216 offset: 0,
217 bytes_per_row: None,
218 rows_per_image: None,
219 },
220 format: TextureFormat::Rgba8Unorm,
221 aspect: TextureAspect::All,
222 copy_size: Extent3d {
223 width: 7,
224 height: 12,
225 depth_or_array_layers: 1,
226 },
227 expected_result: BufferTextureCopyInfo {
228 copy_width: 7,
229 copy_height: 12,
230 depth_or_array_layers: 1,
231 offset: 0,
232 block_size_bytes: 4,
233 block_width_texels: 1,
234 block_height_texels: 1,
235 width_blocks: 7,
236 height_blocks: 12,
237 row_bytes_dense: 4 * 7,
238 row_stride_bytes: 4 * 7,
239 image_stride_rows: 12,
240 image_stride_bytes: 4 * 7 * 12,
241 image_rows_dense: 12,
242 image_bytes_dense: 4 * 7 * 12,
243 bytes_in_copy: 4 * 7 * 12,
244 },
245 };
246
247 test.run();
248
249 // Changing bytes_per_row changes a number of other properties.
250 test.layout.bytes_per_row = Some(48);
251 test.expected_result.row_stride_bytes = 48;
252 test.expected_result.image_stride_bytes = 48 * 12;
253 test.expected_result.image_bytes_dense = 48 * 11 + (4 * 7);
254 test.expected_result.bytes_in_copy = 48 * 11 + (4 * 7);
255
256 // Making this a 3D copy only changes the depth_or_array_layers and the bytes_in_copy.
257 test.copy_size.depth_or_array_layers = 4;
258 test.expected_result.depth_or_array_layers = 4;
259 test.expected_result.bytes_in_copy = 48 * 12 * 3 + 48 * 11 + (4 * 7); // 4 layers
260
261 test.run();
262 }
263
264 #[test]
265 fn linear_texture_data_2d_3d_compressed_copy() {
266 let mut test = LTDTest {
267 layout: TexelCopyBufferLayout {
268 offset: 0,
269 bytes_per_row: None,
270 rows_per_image: None,
271 },
272 format: TextureFormat::Bc1RgbaUnorm,
273 aspect: TextureAspect::All,
274 copy_size: Extent3d {
275 width: 7,
276 height: 13,
277 depth_or_array_layers: 1,
278 },
279 expected_result: BufferTextureCopyInfo {
280 copy_width: 7,
281 copy_height: 13,
282 depth_or_array_layers: 1,
283 offset: 0,
284 block_size_bytes: 8,
285 block_width_texels: 4,
286 block_height_texels: 4,
287 width_blocks: 2,
288 height_blocks: 4,
289 row_bytes_dense: 8 * 2, // block size * width_blocks
290 row_stride_bytes: 8 * 2,
291 image_stride_rows: 4,
292 image_stride_bytes: 8 * 2 * 4, // block size * width_blocks * height_blocks
293 image_rows_dense: 4,
294 image_bytes_dense: 8 * 2 * 4,
295 bytes_in_copy: 8 * 2 * 4,
296 },
297 };
298
299 test.run();
300
301 // Changing bytes_per_row.
302 test.layout.bytes_per_row = Some(48);
303 test.expected_result.row_stride_bytes = 48;
304 test.expected_result.image_stride_bytes = 48 * 4;
305 test.expected_result.image_bytes_dense = 48 * 3 + (8 * 2);
306 test.expected_result.bytes_in_copy = 48 * 3 + (8 * 2);
307
308 test.run();
309
310 // Changing rows_per_image.
311 test.layout.rows_per_image = Some(8);
312 test.expected_result.image_stride_bytes = 48 * 8;
313 test.expected_result.image_stride_rows = 8;
314
315 test.run();
316
317 // Making this a 3D copy only changes the depth_or_array_layers and the bytes_in_copy.
318 test.copy_size.depth_or_array_layers = 4;
319 test.expected_result.depth_or_array_layers = 4;
320 test.expected_result.bytes_in_copy = 48 * 8 * 3 + 48 * 3 + (8 * 2); // 4 layers
321
322 test.run();
323 }
324 }