diff --git a/wgpu-core/src/device/life.rs b/wgpu-core/src/device/life.rs index 2615cf031b..2743aa6f9d 100644 --- a/wgpu-core/src/device/life.rs +++ b/wgpu-core/src/device/life.rs @@ -554,10 +554,10 @@ impl LifetimeTracker { 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) } }; diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 9b29fbdb30..4c7dce8690 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -181,11 +181,11 @@ fn fire_map_callbacks>(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) } } } diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 36c56b5af4..d517a6859a 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -31,8 +31,14 @@ pub enum BufferMapAsyncStatus { } pub enum BufferMapOperation { - Read(Box), - Write(Box), + 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); } } } } diff --git a/wgpu-native/src/device.rs b/wgpu-native/src/device.rs index 7b68796743..6edb37ec2b 100644 --- a/wgpu-native/src/device.rs +++ b/wgpu-native/src/device.rs @@ -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)) } diff --git a/wgpu-remote/src/server.rs b/wgpu-remote/src/server.rs index 18bc0cbf91..d9f0139108 100644 --- a/wgpu-remote/src/server.rs +++ b/wgpu-remote/src/server.rs @@ -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,