Skip to content

Commit

Permalink
Add support for reading from a mapped buffer.
Browse files Browse the repository at this point in the history
  • Loading branch information
rmsc committed Jan 21, 2021
1 parent c8192f4 commit 6cd9387
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ impl RenderResourceContext for HeadlessRenderResourceContext {
write(&mut buffer, self);
}

fn read_mapped_buffer(
&self,
id: BufferId,
_range: Range<u64>,
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) {}
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_render/src/renderer/render_resource_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ pub trait RenderResourceContext: Downcast + Send + Sync + 'static {
range: Range<u64>,
write: &mut dyn FnMut(&mut [u8], &dyn RenderResourceContext),
);
fn read_mapped_buffer(
&self,
id: BufferId,
range: Range<u64>,
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;
Expand Down
15 changes: 15 additions & 0 deletions crates/bevy_wgpu/src/renderer/wgpu_render_resource_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,21 @@ impl RenderResourceContext for WgpuRenderResourceContext {
write(&mut data, self);
}

fn read_mapped_buffer(
&self,
id: BufferId,
range: Range<u64>,
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();
Expand Down

0 comments on commit 6cd9387

Please sign in to comment.