diff --git a/crates/bevy_render/src/renderer/headless_render_resource_context.rs b/crates/bevy_render/src/renderer/headless_render_resource_context.rs index 987119849cf2c..738c065966b1c 100644 --- a/crates/bevy_render/src/renderer/headless_render_resource_context.rs +++ b/crates/bevy_render/src/renderer/headless_render_resource_context.rs @@ -68,6 +68,17 @@ impl RenderResourceContext for HeadlessRenderResourceContext { write(&mut buffer, self); } + fn read_mapped_buffer( + &self, + id: BufferId, + _range: Range, + read: &dyn Fn(&[u8], &dyn RenderResourceContext), + ) { + let size = self.buffer_info.read().get(&id).unwrap().size; + let buffer = vec![0; size]; + read(&buffer, self); + } + fn map_buffer(&self, _id: BufferId, _mode: BufferMapMode) {} fn unmap_buffer(&self, _id: BufferId) {} diff --git a/crates/bevy_render/src/renderer/render_resource_context.rs b/crates/bevy_render/src/renderer/render_resource_context.rs index 7d6a6018c7686..87d3e21a0603c 100644 --- a/crates/bevy_render/src/renderer/render_resource_context.rs +++ b/crates/bevy_render/src/renderer/render_resource_context.rs @@ -26,6 +26,12 @@ pub trait RenderResourceContext: Downcast + Send + Sync + 'static { range: Range, write: &mut dyn FnMut(&mut [u8], &dyn RenderResourceContext), ); + fn read_mapped_buffer( + &self, + id: BufferId, + range: Range, + read: &dyn Fn(&[u8], &dyn RenderResourceContext), + ); fn map_buffer(&self, id: BufferId, mode: BufferMapMode); fn unmap_buffer(&self, id: BufferId); fn create_buffer_with_data(&self, buffer_info: BufferInfo, data: &[u8]) -> BufferId; diff --git a/crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs b/crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs index 27afefff69f82..6bbaf74eb44b9 100644 --- a/crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs +++ b/crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs @@ -622,6 +622,21 @@ impl RenderResourceContext for WgpuRenderResourceContext { write(&mut data, self); } + fn read_mapped_buffer( + &self, + id: BufferId, + range: Range, + read: &dyn Fn(&[u8], &dyn RenderResourceContext), + ) { + let buffer = { + let buffers = self.resources.buffers.read(); + buffers.get(&id).unwrap().clone() + }; + let buffer_slice = buffer.slice(range); + let data = buffer_slice.get_mapped_range(); + read(&data, self); + } + fn map_buffer(&self, id: BufferId, mode: BufferMapMode) { let buffers = self.resources.buffers.read(); let buffer = buffers.get(&id).unwrap();