From c82a3a77d2b60401e5c2570fcc54c3e32c09560a Mon Sep 17 00:00:00 2001 From: Imbris Date: Sat, 27 Feb 2021 17:38:53 -0500 Subject: [PATCH] Avoid panic when requesting to unmap a buffer that is pending mapping --- wgpu-core/src/device/life.rs | 2 ++ wgpu-core/src/device/mod.rs | 19 +++++++++++++++---- wgpu-core/src/resource.rs | 1 + 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/wgpu-core/src/device/life.rs b/wgpu-core/src/device/life.rs index e54911c3a6..c4694b2c3b 100644 --- a/wgpu-core/src/device/life.rs +++ b/wgpu-core/src/device/life.rs @@ -698,6 +698,8 @@ impl LifetimeTracker { resource::BufferMapState::Idle, ) { resource::BufferMapState::Waiting(pending_mapping) => pending_mapping, + // Mapping cancelled + resource::BufferMapState::Idle => continue, _ => panic!("No pending mapping."), }; let status = if mapping.range.start != mapping.range.end { diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 1a292e992e..3fdd512d32 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -4560,10 +4560,10 @@ impl Global { } } - pub fn buffer_unmap( + fn buffer_unmap_inner( &self, buffer_id: id::BufferId, - ) -> Result<(), resource::BufferAccessError> { + ) -> Result, resource::BufferAccessError> { span!(_guard, INFO, "Device::buffer_unmap"); let hub = B::hub(self); @@ -4645,7 +4645,9 @@ impl Global { resource::BufferMapState::Idle => { return Err(resource::BufferAccessError::NotMapped); } - resource::BufferMapState::Waiting(_) => {} + resource::BufferMapState::Waiting(pending) => { + return Ok(Some((pending.op, resource::BufferMapAsyncStatus::Aborted))); + } resource::BufferMapState::Active { ptr, sub_range, @@ -4671,6 +4673,15 @@ impl Global { unmap_buffer(&device.raw, buffer)?; } } - Ok(()) + Ok(None) + } + + pub fn buffer_unmap( + &self, + buffer_id: id::BufferId, + ) -> Result<(), resource::BufferAccessError> { + self.buffer_unmap_inner::(buffer_id) + //Note: outside inner function so no locks are held when calling the callback + .map(|pending_callback| fire_map_callbacks(pending_callback.into_iter())) } } diff --git a/wgpu-core/src/resource.rs b/wgpu-core/src/resource.rs index 045f2ec65f..36c276be43 100644 --- a/wgpu-core/src/resource.rs +++ b/wgpu-core/src/resource.rs @@ -76,6 +76,7 @@ bitflags::bitflags! { pub enum BufferMapAsyncStatus { Success, Error, + Aborted, Unknown, ContextLost, }