Skip to content

Commit

Permalink
Explicit check for destroyed textures/buffers in a few entry points
Browse files Browse the repository at this point in the history
This used to be checked automatically when getting the resource from the registry, but has to be done manually now that we track we track the destroyed state in the resources.
  • Loading branch information
nical committed Jan 5, 2024
1 parent 9662d60 commit 3c1c78e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
27 changes: 26 additions & 1 deletion wgpu-core/src/device/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,12 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
Err(_) => break resource::CreateTextureViewError::InvalidTexture,
};
let device = &texture.device;
{
let snatch_guard = device.snatchable_lock.read();
if texture.is_destroyed(&snatch_guard) {
break resource::CreateTextureViewError::InvalidTexture;
}
}
#[cfg(feature = "trace")]
if let Some(ref mut trace) = *device.trace.lock() {
trace.add(trace::Action::CreateTextureView {
Expand Down Expand Up @@ -2388,6 +2394,12 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
},
));
}

let snatch_guard = device.snatchable_lock.read();
if buffer.is_destroyed(&snatch_guard) {
return Err((op, BufferAccessError::Destroyed));
}

{
let map_state = &mut *buffer.map_state.lock();
*map_state = match *map_state {
Expand All @@ -2412,10 +2424,11 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
let mut trackers = buffer.device.as_ref().trackers.lock();
trackers.buffers.set_single(&buffer, internal_use);
//TODO: Check if draining ALL buffers is correct!
let snatch_guard = device.snatchable_lock.read();
let _ = trackers.buffers.drain_transitions(&snatch_guard);
}

drop(snatch_guard);

buffer
};

Expand All @@ -2440,6 +2453,13 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.get(buffer_id)
.map_err(|_| BufferAccessError::Invalid)?;

{
let snatch_guard = buffer.device.snatchable_lock.read();
if buffer.is_destroyed(&snatch_guard) {
return Err(BufferAccessError::Destroyed);
}
}

let range_size = if let Some(size) = size {
size
} else if offset > buffer.size {
Expand Down Expand Up @@ -2502,6 +2522,11 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
.get(buffer_id)
.map_err(|_| BufferAccessError::Invalid)?;

let snatch_guard = buffer.device.snatchable_lock.read();
if buffer.is_destroyed(&snatch_guard) {
return Err(BufferAccessError::Destroyed);
}

if !buffer.device.is_valid() {
return Err(DeviceError::Lost.into());
}
Expand Down
8 changes: 8 additions & 0 deletions wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@ impl<A: HalApi> Buffer<A> {
self.raw.get(guard)
}

pub(crate) fn is_destroyed(&self, guard: &SnatchGuard) -> bool {
self.raw.get(guard).is_none()
}

// Note: This must not be called while holding a lock.
pub(crate) fn unmap(self: &Arc<Self>) -> Result<(), BufferAccessError> {
if let Some((mut operation, status)) = self.unmap_inner()? {
Expand Down Expand Up @@ -801,6 +805,10 @@ impl<A: HalApi> Texture<A> {
self.inner.get(snatch_guard)?.raw()
}

pub(crate) fn is_destroyed(&self, guard: &SnatchGuard) -> bool {
self.inner.get(guard).is_none()
}

pub(crate) fn inner_mut<'a>(
&'a self,
guard: &mut ExclusiveSnatchGuard,
Expand Down

0 comments on commit 3c1c78e

Please sign in to comment.