Skip to content

Commit

Permalink
fix panic with as_hal functions (#2871)
Browse files Browse the repository at this point in the history
  • Loading branch information
i509VCB authored and cwfitzgerald committed Jul 14, 2022
1 parent 67221ed commit 53e034e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Bottom level categories:
- Allow multi-sampled textures that are supported by the device but not WebGPU if `TEXTURE_ADAPTER_SPECIFIC_FORMAT_FEATURES` is enabled by @cwfitzgerald in [#2856](https://github.com/gfx-rs/wgpu/pull/2856)
- `get_texture_format_features` only lists the COPY_* usages if the adapter actually supports that usage by @cwfitzgerald in [#2856](https://github.com/gfx-rs/wgpu/pull/2856)
- Fix bind group / pipeline deduplication not taking into account RenderBundle execution resetting these values by @shoebe [#2867](https://github.com/gfx-rs/wgpu/pull/2867)
- Fix panics that occur when using `as_hal` functions when the hal generic type does not match the hub being looked up in by @i509VCB [#2871](https://github.com/gfx-rs/wgpu/pull/2871)

#### DX12
- `DownlevelCapabilities::default()` now returns the `ANISOTROPIC_FILTERING` flag set to true so DX12 lists `ANISOTROPIC_FILTERING` as true again by @cwfitzgerald in [#2851](https://github.com/gfx-rs/wgpu/pull/2851)
Expand Down
22 changes: 22 additions & 0 deletions wgpu-core/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,28 @@ impl<T, I: id::TypedId> Storage<T, I> {
}
}

/// Attempts to get a reference to an item behind a potentially invalid ID.
///
/// Returns [`None`] if there is an epoch mismatch, or the entry is empty.
///
/// This function is primarily intended for the `as_hal` family of functions where you may need to
/// fallibly get a object backed by an id that could be in a different hub.
pub(crate) fn try_get(&self, id: I) -> Result<Option<&T>, InvalidId> {
let (index, epoch, _) = id.unzip();
let (result, storage_epoch) = match self.map.get(index as usize) {
Some(&Element::Occupied(ref v, epoch)) => (Ok(Some(v)), epoch),
Some(&Element::Vacant) => return Ok(None),
Some(&Element::Error(epoch, ..)) => (Err(InvalidId), epoch),
None => return Err(InvalidId),
};
assert_eq!(
epoch, storage_epoch,
"{}[{}] is no longer alive",
self.kind, index
);
result
}

/// Get a reference to an item behind a potentially invalid ID.
/// Panics if there is an epoch mismatch, or the entry is empty.
pub(crate) fn get(&self, id: I) -> Result<&T, InvalidId> {
Expand Down
6 changes: 3 additions & 3 deletions wgpu-core/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
let hub = A::hub(self);
let mut token = Token::root();
let (guard, _) = hub.textures.read(&mut token);
let texture = guard.get(id).ok();
let texture = guard.try_get(id).ok().flatten();
let hal_texture = texture.map(|tex| tex.inner.as_raw().unwrap());

hal_texture_callback(hal_texture);
Expand All @@ -313,7 +313,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
let mut token = Token::root();

let (guard, _) = hub.adapters.read(&mut token);
let adapter = guard.get(id).ok();
let adapter = guard.try_get(id).ok().flatten();
let hal_adapter = adapter.map(|adapter| &adapter.raw.adapter);

hal_adapter_callback(hal_adapter)
Expand All @@ -332,7 +332,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
let hub = A::hub(self);
let mut token = Token::root();
let (guard, _) = hub.devices.read(&mut token);
let device = guard.get(id).ok();
let device = guard.try_get(id).ok().flatten();
let hal_device = device.map(|device| &device.raw);

hal_device_callback(hal_device)
Expand Down

0 comments on commit 53e034e

Please sign in to comment.