Avoid panic when requesting to unmap a buffer that is pending mapping

This commit is contained in:
Imbris 2021-02-27 17:38:53 -05:00
parent c3a18c3d8b
commit c82a3a77d2
3 changed files with 18 additions and 4 deletions

View File

@ -698,6 +698,8 @@ impl<B: GfxBackend> LifetimeTracker<B> {
resource::BufferMapState::Idle, resource::BufferMapState::Idle,
) { ) {
resource::BufferMapState::Waiting(pending_mapping) => pending_mapping, resource::BufferMapState::Waiting(pending_mapping) => pending_mapping,
// Mapping cancelled
resource::BufferMapState::Idle => continue,
_ => panic!("No pending mapping."), _ => panic!("No pending mapping."),
}; };
let status = if mapping.range.start != mapping.range.end { let status = if mapping.range.start != mapping.range.end {

View File

@ -4560,10 +4560,10 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
} }
} }
pub fn buffer_unmap<B: GfxBackend>( fn buffer_unmap_inner<B: GfxBackend>(
&self, &self,
buffer_id: id::BufferId, buffer_id: id::BufferId,
) -> Result<(), resource::BufferAccessError> { ) -> Result<Option<BufferMapPendingCallback>, resource::BufferAccessError> {
span!(_guard, INFO, "Device::buffer_unmap"); span!(_guard, INFO, "Device::buffer_unmap");
let hub = B::hub(self); let hub = B::hub(self);
@ -4645,7 +4645,9 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
resource::BufferMapState::Idle => { resource::BufferMapState::Idle => {
return Err(resource::BufferAccessError::NotMapped); return Err(resource::BufferAccessError::NotMapped);
} }
resource::BufferMapState::Waiting(_) => {} resource::BufferMapState::Waiting(pending) => {
return Ok(Some((pending.op, resource::BufferMapAsyncStatus::Aborted)));
}
resource::BufferMapState::Active { resource::BufferMapState::Active {
ptr, ptr,
sub_range, sub_range,
@ -4671,6 +4673,15 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
unmap_buffer(&device.raw, buffer)?; unmap_buffer(&device.raw, buffer)?;
} }
} }
Ok(()) Ok(None)
}
pub fn buffer_unmap<B: GfxBackend>(
&self,
buffer_id: id::BufferId,
) -> Result<(), resource::BufferAccessError> {
self.buffer_unmap_inner::<B>(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()))
} }
} }

View File

@ -76,6 +76,7 @@ bitflags::bitflags! {
pub enum BufferMapAsyncStatus { pub enum BufferMapAsyncStatus {
Success, Success,
Error, Error,
Aborted,
Unknown, Unknown,
ContextLost, ContextLost,
} }