-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gpu: Replace use of wgpu
.global_id()
with explicitly added IDs.
The next release of wgpu will remove `.global_id()`, so we have to stop using them. It will also add `Eq` implementations, which could perhaps substitute in some cases, but we usually want to compare resources which currently exist with the IDs of resources that may be obsolete, so using `Eq` would mean unnecessarily retaining those obsolete resources, which is worse than generating our own IDs. So, this commit adds a wrapper type `Identified` which essentially recreates the wgpu ID system.
- Loading branch information
Showing
12 changed files
with
139 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//! DIY replacement for the `wgpu` `.global_id()` functionality which is being removed | ||
//! in `wgpu` version 23. | ||
|
||
use std::fmt; | ||
use std::marker::PhantomData; | ||
use std::ops; | ||
use std::sync::atomic::{self, Ordering}; | ||
use std::sync::Arc; | ||
|
||
/// Adds a globally unique identifier to a value. | ||
/// | ||
/// This identifier may be used to compare two values which do not implement [`PartialEq`], | ||
/// or remember their identities without necessarily retaining the values themselves. | ||
#[derive(Clone, Debug)] | ||
pub(crate) struct Identified<T> { | ||
id: Id<T>, | ||
value: T, | ||
} | ||
|
||
/// A unique identifier for some [`Identified`] value. | ||
pub(crate) struct Id<T> { | ||
id: u64, | ||
_phantom: PhantomData<Arc<T>>, | ||
} | ||
|
||
impl<T> Identified<T> { | ||
/// Wraps the given value and assigns a new unique [`Id`] for it. | ||
pub fn new(value: T) -> Self { | ||
static ID_COUNTER: atomic::AtomicU64 = atomic::AtomicU64::new(0); | ||
let id = ID_COUNTER.fetch_add(1, Ordering::Relaxed); | ||
Self { | ||
id: Id { | ||
id, | ||
_phantom: PhantomData, | ||
}, | ||
value, | ||
} | ||
} | ||
|
||
pub fn global_id(&self) -> Id<T> { | ||
self.id | ||
} | ||
} | ||
|
||
impl<T> ops::Deref for Identified<T> { | ||
type Target = T; | ||
fn deref(&self) -> &T { | ||
&self.value | ||
} | ||
} | ||
|
||
impl<T> Eq for Id<T> {} | ||
impl<T> PartialEq for Id<T> { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.id == other.id | ||
} | ||
} | ||
impl<T> std::hash::Hash for Id<T> { | ||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { | ||
self.id.hash(state); | ||
} | ||
} | ||
|
||
impl<T> fmt::Debug for Id<T> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_tuple("Id").field(&self.id).finish() | ||
} | ||
} | ||
|
||
impl<T> Copy for Id<T> {} | ||
impl<T> Clone for Id<T> { | ||
fn clone(&self) -> Self { | ||
*self | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.