Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge #538
Browse files Browse the repository at this point in the history
538: Unbox unnecessarily boxed function r=kvark a=lachlansneff

Internally, the async buffer mapping callbacks were unnecessarily boxed. This was pretty easy to fix.

Co-authored-by: Lachlan Sneff <lachlan.sneff@gmail.com>
bors[bot] and lachlansneff authored Mar 27, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 86981d9 + bcd7744 commit 8d8f684
Showing 5 changed files with 35 additions and 29 deletions.
4 changes: 2 additions & 2 deletions wgpu-core/src/device/life.rs
Original file line number Diff line number Diff line change
@@ -554,10 +554,10 @@ impl<B: GfxBackend> LifetimeTracker<B> {
let buffer = &mut buffer_guard[buffer_id];
let mapping = buffer.pending_mapping.take().unwrap();
let result = match mapping.op {
resource::BufferMapOperation::Read(..) => {
resource::BufferMapOperation::Read { .. } => {
super::map_buffer(raw, buffer, mapping.range, super::HostMap::Read)
}
resource::BufferMapOperation::Write(..) => {
resource::BufferMapOperation::Write { .. } => {
super::map_buffer(raw, buffer, mapping.range, super::HostMap::Write)
}
};
8 changes: 4 additions & 4 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
@@ -181,11 +181,11 @@ fn fire_map_callbacks<I: IntoIterator<Item = BufferMapPendingCallback>>(callback
}
};
match operation {
resource::BufferMapOperation::Read(on_read) => {
on_read(status, ptr)
resource::BufferMapOperation::Read { callback, userdata } => unsafe {
callback(status, ptr, userdata)
}
resource::BufferMapOperation::Write(on_write) => {
on_write(status, ptr)
resource::BufferMapOperation::Write { callback, userdata } => unsafe {
callback(status, ptr, userdata)
}
}
}
22 changes: 14 additions & 8 deletions wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
@@ -31,8 +31,14 @@ pub enum BufferMapAsyncStatus {
}

pub enum BufferMapOperation {
Read(Box<dyn FnOnce(BufferMapAsyncStatus, *const u8)>),
Write(Box<dyn FnOnce(BufferMapAsyncStatus, *mut u8)>),
Read {
callback: crate::device::BufferMapReadCallback,
userdata: *mut u8,
},
Write {
callback: crate::device::BufferMapWriteCallback,
userdata: *mut u8,
}
}

//TODO: clarify if/why this is needed here
@@ -42,8 +48,8 @@ unsafe impl Sync for BufferMapOperation {}
impl fmt::Debug for BufferMapOperation {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let op = match *self {
BufferMapOperation::Read(_) => "read",
BufferMapOperation::Write(_) => "write",
BufferMapOperation::Read { .. } => "read",
BufferMapOperation::Write { .. } => "write",
};
write!(fmt, "BufferMapOperation <{}>", op)
}
@@ -52,13 +58,13 @@ impl fmt::Debug for BufferMapOperation {
impl BufferMapOperation {
pub(crate) fn call_error(self) {
match self {
BufferMapOperation::Read(callback) => {
BufferMapOperation::Read { callback, userdata } => {
log::error!("wgpu_buffer_map_read_async failed: buffer mapping is pending");
callback(BufferMapAsyncStatus::Error, std::ptr::null());
unsafe { callback(BufferMapAsyncStatus::Error, std::ptr::null(), userdata); }
}
BufferMapOperation::Write(callback) => {
BufferMapOperation::Write { callback, userdata } => {
log::error!("wgpu_buffer_map_write_async failed: buffer mapping is pending");
callback(BufferMapAsyncStatus::Error, std::ptr::null_mut());
unsafe { callback(BufferMapAsyncStatus::Error, std::ptr::null_mut(), userdata); }
}
}
}
20 changes: 10 additions & 10 deletions wgpu-native/src/device.rs
Original file line number Diff line number Diff line change
@@ -380,11 +380,11 @@ pub extern "C" fn wgpu_buffer_map_read_async(
callback: core::device::BufferMapReadCallback,
userdata: *mut u8,
) {
let operation = core::resource::BufferMapOperation::Read(
Box::new(move |status, data| unsafe {
callback(status, data, userdata)
}),
);
let operation = core::resource::BufferMapOperation::Read {
callback,
userdata,
};

gfx_select!(buffer_id => GLOBAL.buffer_map_async(buffer_id, wgt::BufferUsage::MAP_READ, start .. start + size, operation))
}

@@ -396,11 +396,11 @@ pub extern "C" fn wgpu_buffer_map_write_async(
callback: core::device::BufferMapWriteCallback,
userdata: *mut u8,
) {
let operation = core::resource::BufferMapOperation::Write(
Box::new(move |status, data| unsafe {
callback(status, data, userdata)
}),
);
let operation = core::resource::BufferMapOperation::Write {
callback,
userdata,
};

gfx_select!(buffer_id => GLOBAL.buffer_map_async(buffer_id, wgt::BufferUsage::MAP_WRITE, start .. start + size, operation))
}

10 changes: 5 additions & 5 deletions wgpu-remote/src/server.rs
Original file line number Diff line number Diff line change
@@ -123,11 +123,11 @@ pub extern "C" fn wgpu_server_buffer_map_read(
callback: core::device::BufferMapReadCallback,
userdata: *mut u8,
) {
let operation = core::resource::BufferMapOperation::Read(
Box::new(move |status, data| unsafe {
callback(status, data, userdata)
}),
);
let operation = core::resource::BufferMapOperation::Read {
callback,
userdata,
};

gfx_select!(buffer_id => global.buffer_map_async(
buffer_id,
wgt::BufferUsage::MAP_READ,

0 comments on commit 8d8f684

Please sign in to comment.