mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-24 08:21:25 +01:00
fix(examples): use pod_collect_to_vec for mapped buffer reads (#9378)
Co-authored-by: countgitmick <countgitmick@users.noreply.github.com>
This commit is contained in:
1 parent
1c0434d7d1
commit
5d03e09b54
8 files changed
+16
-10
No files matched your search
@@ -86,7 +86,8 @@ pub async fn execute_gpu_inner(
|
||||
for staging_buffer in &staging_buffers {
|
||||
let slice = staging_buffer.slice(..);
|
||||
let mapped = slice.get_mapped_range();
|
||||
data.extend_from_slice(bytemuck::cast_slice(&mapped));
|
||||
let chunk: Vec<f32> = bytemuck::allocation::pod_collect_to_vec(&mapped);
|
||||
data.extend_from_slice(&chunk);
|
||||
drop(mapped);
|
||||
staging_buffer.unmap();
|
||||
}
|
||||
|
||||
@@ -415,10 +415,10 @@ async fn execute(
|
||||
|
||||
// Convert result back to f32 for comparison
|
||||
let result: Vec<f32> = if use_f16 {
|
||||
let result_f16: &[f16] = bytemuck::cast_slice(&data);
|
||||
let result_f16: Vec<f16> = bytemuck::allocation::pod_collect_to_vec(&data);
|
||||
result_f16.iter().map(|x| x.to_f32()).collect()
|
||||
} else {
|
||||
bytemuck::cast_slice::<_, f32>(&data).to_vec()
|
||||
bytemuck::allocation::pod_collect_to_vec(&data)
|
||||
};
|
||||
|
||||
// Compute reference result on CPU for verification
|
||||
|
||||
@@ -184,7 +184,9 @@ async fn get_data<T: bytemuck::Pod>(
|
||||
buffer_slice.map_async(wgpu::MapMode::Read, move |r| sender.send(r).unwrap());
|
||||
device.poll(wgpu::PollType::wait_indefinitely()).unwrap();
|
||||
receiver.recv_async().await.unwrap().unwrap();
|
||||
output.copy_from_slice(bytemuck::cast_slice(&buffer_slice.get_mapped_range()[..]));
|
||||
let data: Vec<T> =
|
||||
bytemuck::allocation::pod_collect_to_vec(&buffer_slice.get_mapped_range()[..]);
|
||||
output.copy_from_slice(&data);
|
||||
staging_buffer.unmap();
|
||||
}
|
||||
|
||||
|
||||
@@ -173,7 +173,9 @@ async fn get_data<T: bytemuck::Pod>(
|
||||
buffer_slice.map_async(wgpu::MapMode::Read, move |r| sender.send(r).unwrap());
|
||||
device.poll(wgpu::PollType::wait_indefinitely()).unwrap();
|
||||
receiver.recv_async().await.unwrap().unwrap();
|
||||
output.copy_from_slice(bytemuck::cast_slice(&buffer_slice.get_mapped_range()[..]));
|
||||
let data: Vec<T> =
|
||||
bytemuck::allocation::pod_collect_to_vec(&buffer_slice.get_mapped_range()[..]);
|
||||
output.copy_from_slice(&data);
|
||||
staging_buffer.unmap();
|
||||
}
|
||||
|
||||
|
||||
@@ -116,7 +116,8 @@ async fn compute(local_buffer: &mut [u32], context: &WgpuContext) {
|
||||
// NOW we can call get_mapped_range.
|
||||
{
|
||||
let view = buffer_slice.get_mapped_range();
|
||||
local_buffer.copy_from_slice(bytemuck::cast_slice(&view));
|
||||
let data: Vec<u32> = bytemuck::allocation::pod_collect_to_vec(&view);
|
||||
local_buffer.copy_from_slice(&data);
|
||||
}
|
||||
log::info!("Results written to local buffer.");
|
||||
// We need to make sure all `BufferView`'s are dropped before we do what we're about
|
||||
|
||||
@@ -182,7 +182,7 @@ impl Queries {
|
||||
.destination_buffer
|
||||
.slice(..(size_of::<u64>() as wgpu::BufferAddress * self.num_queries))
|
||||
.get_mapped_range();
|
||||
bytemuck::cast_slice(×tamp_view).to_vec()
|
||||
bytemuck::allocation::pod_collect_to_vec(×tamp_view)
|
||||
};
|
||||
|
||||
self.destination_buffer.unmap();
|
||||
|
||||
@@ -5,7 +5,7 @@ rust-version = "1.87"
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
bytemuck = "1.22.0"
|
||||
bytemuck = { version = "1.22.0", features = ["extern_crate_alloc"] }
|
||||
env_logger = "0.11"
|
||||
pollster = "0.4"
|
||||
wgpu = "29.0.0"
|
||||
@@ -246,8 +246,8 @@ fn main() {
|
||||
|
||||
// We can now read the data from the buffer.
|
||||
let data = buffer_slice.get_mapped_range();
|
||||
// Convert the data back to a slice of f32.
|
||||
let result: &[f32] = bytemuck::cast_slice(&data);
|
||||
// Convert the data back to f32 via an aligned copy.
|
||||
let result: Vec<f32> = bytemuck::allocation::pod_collect_to_vec(&data);
|
||||
|
||||
// Print out the result.
|
||||
println!("Result: {result:?}");
|
||||
|
||||
Reference in new issue
Block a user