Skip to content

Commit a8bb208

Browse files
authored
optimize ktx2 level data concatenation (#19845)
# Objective - avoid several internal vec copies while collecting all the level data in ktx2 load - merge another little piece of #18411 (benchmarks there found this to be a significant win) ## Solution - reserve and extend ## Testing - ran a few examples that load ktx2 images, like ssr. looks fine ## Future work - fast path logic to skip the reading into different vecs and just read it all in one go into the final buffer instead - as above, but directly into gpu staging buffer perhaps
1 parent 764be91 commit a8bb208

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

crates/bevy_image/src/ktx2.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,16 @@ pub fn ktx2_buffer_to_image(
238238
)));
239239
}
240240

241+
// Collect all level data into a contiguous buffer
242+
let mut image_data = Vec::new();
243+
image_data.reserve_exact(levels.iter().map(Vec::len).sum());
244+
levels.iter().for_each(|level| image_data.extend(level));
245+
241246
// Assign the data and fill in the rest of the metadata now the possible
242247
// error cases have been handled
243248
let mut image = Image::default();
244249
image.texture_descriptor.format = texture_format;
245-
image.data = Some(levels.into_iter().flatten().collect::<Vec<_>>());
250+
image.data = Some(image_data);
246251
image.data_order = wgpu_types::TextureDataOrder::MipMajor;
247252
// Note: we must give wgpu the logical texture dimensions, so it can correctly compute mip sizes.
248253
// However this currently causes wgpu to panic if the dimensions arent a multiple of blocksize.

0 commit comments

Comments
 (0)