From c8554b59d4a398f958b33fa94013c5cfa8b4b515 Mon Sep 17 00:00:00 2001 From: Sven Niederberger Date: Mon, 14 Jul 2025 16:00:47 +0200 Subject: [PATCH] update the example --- examples/shader/gpu_readback.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/examples/shader/gpu_readback.rs b/examples/shader/gpu_readback.rs index 964776291e..7766db5e8b 100644 --- a/examples/shader/gpu_readback.rs +++ b/examples/shader/gpu_readback.rs @@ -74,7 +74,7 @@ fn setup( mut buffers: ResMut>, ) { // Create a storage buffer with some data - let buffer = vec![0u32; BUFFER_LEN]; + let buffer: Vec = (0..BUFFER_LEN as u32).collect(); let mut buffer = ShaderStorageBuffer::from(buffer); // We need to enable the COPY_SRC usage so we can copy the buffer to the cpu buffer.buffer_description.usage |= BufferUsages::COPY_SRC; @@ -110,6 +110,19 @@ fn setup( let data: Vec = trigger.event().to_shader_type(); info!("Buffer {:?}", data); }); + + // It is also possible to read only a range of the buffer. + commands + .spawn(Readback::buffer_range( + buffer.clone(), + 4 * u32::SHADER_SIZE.get(), // skip the first four elements + 8 * u32::SHADER_SIZE.get(), // read eight elements + )) + .observe(|trigger: On| { + let data: Vec = trigger.event().to_shader_type(); + info!("Buffer range {:?}", data); + }); + // This is just a simple way to pass the buffer handle to the render app for our compute node commands.insert_resource(ReadbackBuffer(buffer));