From 267bc708a9524cb4ee0349e2a74991a54fd2b9c4 Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Mon, 2 Sep 2024 18:34:31 -0700 Subject: [PATCH] gpu: get_texels_from_gpu(): Don't destroy the temp buffer before the view. --- all-is-cubes-gpu/src/in_wgpu/init.rs | 36 ++++++++++++++++------------ 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/all-is-cubes-gpu/src/in_wgpu/init.rs b/all-is-cubes-gpu/src/in_wgpu/init.rs index 4aeee0066..4da2e1c4f 100644 --- a/all-is-cubes-gpu/src/in_wgpu/init.rs +++ b/all-is-cubes-gpu/src/in_wgpu/init.rs @@ -239,24 +239,30 @@ where .await .expect("communication failed") .expect("buffer reading failed"); - let mapped: &[u8] = &temp_buffer.slice(..).get_mapped_range(); - let element_count = camera::area_usize(dimensions).unwrap() * components; + { + let element_count = camera::area_usize(dimensions).unwrap() * components; - // Copy the mapped buffer data into a Rust vector, removing row padding if present - // by copying it one row at a time. - let mut texel_vector: Vec = Vec::with_capacity(element_count); - for row in 0..dimensions.height { - let byte_start_of_row = padded_bytes_per_row * row; - // TODO: this cast_slice() could fail if `C`’s alignment is higher than the buffer. - texel_vector.extend(bytemuck::cast_slice::( - &mapped[byte_start_of_row as usize..][..dense_bytes_per_row as usize], - )); - } - debug_assert_eq!(texel_vector.len(), element_count); + // Copy the mapped buffer data into a Rust vector, removing row padding if present + // by copying it one row at a time. + let mut texel_vector: Vec = Vec::with_capacity(element_count); + { + let mapped: &[u8] = &temp_buffer.slice(..).get_mapped_range(); + for row in 0..dimensions.height { + let byte_start_of_row = padded_bytes_per_row * row; + // TODO: this cast_slice() could fail if `C`’s alignment is higher than the buffer. + texel_vector.extend(bytemuck::cast_slice::( + &mapped[byte_start_of_row as usize..][..dense_bytes_per_row as usize], + )); + } + debug_assert_eq!(texel_vector.len(), element_count); + } - temp_buffer.destroy(); + // Note: We do this after the get_mapped_range() has been dropped, due to the + // WebGPU backend otherwise crashing: . + temp_buffer.destroy(); - texel_vector + texel_vector + } } }