Skip to content

Commit

Permalink
Merge gfx-rs#391
Browse files Browse the repository at this point in the history
391: Remote buffer mapping r=kvark a=kvark

Reflects the ongoing changes in Gecko.

Co-authored-by: Dzmitry Malyshau <[email protected]>
  • Loading branch information
bors[bot] and kvark authored Nov 29, 2019
2 parents 2006c07 + 2f74e22 commit a13e40c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 17 deletions.
77 changes: 60 additions & 17 deletions wgpu-core/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,27 @@ fn map_buffer<B: hal::Backend>(
Ok(ptr.as_ptr())
}

fn unmap_buffer<B: hal::Backend>(
raw: &B::Device,
buffer: &mut resource::Buffer<B>,
) {
if !buffer.mapped_write_ranges.is_empty() {
unsafe {
raw
.flush_mapped_memory_ranges(
buffer
.mapped_write_ranges
.iter()
.map(|r| (buffer.memory.memory(), r.clone())),
)
.unwrap()
};
buffer.mapped_write_ranges.clear();
}

buffer.memory.unmap(raw);
}

#[derive(Debug)]
pub struct Device<B: hal::Backend> {
pub(crate) raw: B::Device,
Expand Down Expand Up @@ -846,6 +867,41 @@ impl<F: IdentityFilter<BufferId>> Global<F> {
id
}

pub fn device_set_buffer_sub_data<B: GfxBackend>(
&self,
device_id: DeviceId,
buffer_id: BufferId,
offset: BufferAddress,
data: &[u8],
) {
let hub = B::hub(self);
let mut token = Token::root();

let (device_guard, mut token) = hub.devices.read(&mut token);
let (mut buffer_guard, _) = hub.buffers.write(&mut token);
let device = &device_guard[device_id];
let mut buffer = &mut buffer_guard[buffer_id];
assert!(buffer.usage.contains(resource::BufferUsage::MAP_WRITE));
//assert!(buffer isn't used by the GPU);

match map_buffer(
&device.raw,
&mut buffer,
offset .. offset + data.len() as BufferAddress,
HostMap::Write,
) {
Ok(ptr) => unsafe {
ptr::copy_nonoverlapping(data.as_ptr(), ptr, data.len());
},
Err(e) => {
log::error!("failed to map a buffer: {:?}", e);
return;
}
}

unmap_buffer(&device.raw, buffer);
}

pub fn buffer_destroy<B: GfxBackend>(&self, buffer_id: BufferId) {
let hub = B::hub(self);
let mut token = Token::root();
Expand Down Expand Up @@ -2049,24 +2105,11 @@ impl<F> Global<F> {

let (device_guard, mut token) = hub.devices.read(&mut token);
let (mut buffer_guard, _) = hub.buffers.write(&mut token);

let buffer = &mut buffer_guard[buffer_id];
let device_raw = &device_guard[buffer.device_id.value].raw;

if !buffer.mapped_write_ranges.is_empty() {
unsafe {
device_raw
.flush_mapped_memory_ranges(
buffer
.mapped_write_ranges
.iter()
.map(|r| (buffer.memory.memory(), r.clone())),
)
.unwrap()
};
buffer.mapped_write_ranges.clear();
}

buffer.memory.unmap(device_raw);
unmap_buffer(
&device_guard[buffer.device_id.value].raw,
buffer,
);
}
}
15 changes: 15 additions & 0 deletions wgpu-remote/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,18 @@ pub extern "C" fn wgpu_server_device_create_buffer(
) {
gfx_select!(self_id => global.device_create_buffer(self_id, desc, new_id));
}

#[no_mangle]
pub extern "C" fn wgpu_server_device_set_buffer_sub_data(
global: &Global<()>,
self_id: id::DeviceId,
buffer_id: id::BufferId,
offset: core::BufferAddress,
data: *const u8,
size: core::BufferAddress,
) {
let slice = unsafe {
slice::from_raw_parts(data, size as usize)
};
gfx_select!(self_id => global.device_set_buffer_sub_data(self_id, buffer_id, offset, slice));
}

0 comments on commit a13e40c

Please sign in to comment.