Skip to content

Commit

Permalink
Merge gfx-rs#1239
Browse files Browse the repository at this point in the history
1239: Avoid panic when requesting to unmap a buffer that is pending mapping r=kvark a=Imberflur

**Connections**
Fix for gfx-rs#1238 

**Description**
This might not be the cleanest fix but it is quite minimal. I'm not sure what the exact intent is with the organization of things and with the async status enum, would be happy to hear critiques.

**Testing**
Tested against example in gfx-rs#1238 
<!--
Non-trivial functional changes would need to be tested through:
  - [wgpu-rs](https://github.com/gfx-rs/wgpu-rs) - test the examples.
  - [wgpu-native](https://github.com/gfx-rs/wgpu-native/) - check the generated C header for sanity.

Ideally, a PR needs to link to the draft PRs in these projects with relevant modifications.
See gfx-rs#666 for an example.
If you can add a unit/integration test here in `wgpu`, that would be best.
-->


Co-authored-by: Imbris <[email protected]>
  • Loading branch information
bors[bot] and Imberflur authored Feb 28, 2021
2 parents c3a18c3 + c82a3a7 commit ecbdded
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 2 additions & 0 deletions wgpu-core/src/device/life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,8 @@ impl<B: GfxBackend> LifetimeTracker<B> {
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 {
Expand Down
19 changes: 15 additions & 4 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4560,10 +4560,10 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
}
}

pub fn buffer_unmap<B: GfxBackend>(
fn buffer_unmap_inner<B: GfxBackend>(
&self,
buffer_id: id::BufferId,
) -> Result<(), resource::BufferAccessError> {
) -> Result<Option<BufferMapPendingCallback>, resource::BufferAccessError> {
span!(_guard, INFO, "Device::buffer_unmap");

let hub = B::hub(self);
Expand Down Expand Up @@ -4645,7 +4645,9 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
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,
Expand All @@ -4671,6 +4673,15 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
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()))
}
}
1 change: 1 addition & 0 deletions wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ bitflags::bitflags! {
pub enum BufferMapAsyncStatus {
Success,
Error,
Aborted,
Unknown,
ContextLost,
}
Expand Down

0 comments on commit ecbdded

Please sign in to comment.