Skip to content

Commit

Permalink
Add a generic parameter to wgpu::Id (gfx-rs#3575)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheOnlyMrCat authored Mar 29, 2023
1 parent e4844a1 commit 53ddf68
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 61 deletions.
16 changes: 8 additions & 8 deletions wgpu/src/backend/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn create_identified<T>(value: T) -> Identified<T> {
if #[cfg(feature = "expose-ids")] {
static NEXT_ID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);
let id = NEXT_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
Identified(value, crate::Id(core::num::NonZeroU64::new(id).unwrap()))
Identified(value, core::num::NonZeroU64::new(id).unwrap())
} else {
Identified(value)
}
Expand All @@ -43,8 +43,8 @@ fn create_identified<T>(value: T) -> Identified<T> {
// is integrated (or not integrated) with values like those in webgpu, this may become unsound.

impl<T: FromWasmAbi<Abi = u32> + JsCast> From<ObjectId> for Identified<T> {
fn from(id: ObjectId) -> Self {
let id = id.id().get() as u32;
fn from(object_id: ObjectId) -> Self {
let id = object_id.id().get() as u32;
// SAFETY: wasm_bindgen says an ABI representation may only be cast to a wrapper type if it was created
// using into_abi.
//
Expand All @@ -55,18 +55,18 @@ impl<T: FromWasmAbi<Abi = u32> + JsCast> From<ObjectId> for Identified<T> {
Self(
wasm.unchecked_into(),
#[cfg(feature = "expose-ids")]
id.global_id(),
object_id.global_id(),
)
}
}

impl<T: IntoWasmAbi<Abi = u32>> From<Identified<T>> for ObjectId {
fn from(id: Identified<T>) -> Self {
let id = core::num::NonZeroU64::new(id.0.into_abi() as u64).unwrap();
fn from(identified: Identified<T>) -> Self {
let id = core::num::NonZeroU64::new(identified.0.into_abi() as u64).unwrap();
Self::new(
id,
#[cfg(feature = "expose-ids")]
id.1,
identified.1,
)
}
}
Expand All @@ -77,7 +77,7 @@ unsafe impl<T> Send for Sendable<T> {}
unsafe impl<T> Sync for Sendable<T> {}

#[derive(Clone, Debug)]
pub(crate) struct Identified<T>(T, #[cfg(feature = "expose-ids")] crate::Id);
pub(crate) struct Identified<T>(T, #[cfg(feature = "expose-ids")] std::num::NonZeroU64);
unsafe impl<T> Send for Identified<T> {}
unsafe impl<T> Sync for Identified<T> {}

Expand Down
8 changes: 4 additions & 4 deletions wgpu/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ pub struct ObjectId {
id: Option<NonZeroU64>,
#[cfg(feature = "expose-ids")]
/// ID that is unique at all times
global_id: Option<crate::Id>,
global_id: Option<NonZeroU64>,
}

impl ObjectId {
Expand All @@ -1008,7 +1008,7 @@ impl ObjectId {
global_id: None,
};

pub fn new(id: NonZeroU64, #[cfg(feature = "expose-ids")] global_id: crate::Id) -> Self {
pub fn new(id: NonZeroU64, #[cfg(feature = "expose-ids")] global_id: NonZeroU64) -> Self {
Self {
id: Some(id),
#[cfg(feature = "expose-ids")]
Expand All @@ -1021,7 +1021,7 @@ impl ObjectId {
Self {
id: Some(global_id),
#[cfg(feature = "expose-ids")]
global_id: Some(crate::Id(global_id)),
global_id: Some(global_id),
}
}

Expand All @@ -1031,7 +1031,7 @@ impl ObjectId {

#[cfg(feature = "expose-ids")]
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
pub fn global_id(&self) -> crate::Id {
pub fn global_id(&self) -> NonZeroU64 {
self.global_id.unwrap()
}
}
Expand Down
114 changes: 65 additions & 49 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4244,17 +4244,18 @@ impl Surface {
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
#[repr(transparent)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Id(core::num::NonZeroU64);
pub struct Id<T>(core::num::NonZeroU64, std::marker::PhantomData<*mut T>);

#[cfg(feature = "expose-ids")]
impl Adapter {
/// Returns a globally-unique identifier for this `Adapter`.
///
/// Calling this method multiple times on the same object will always return the same value.
/// The returned value is guaranteed to be different for all resources created from the same `Instance`.
/// The returned value is guaranteed to be unique among all `Adapter`s created from the same
/// `Instance`.
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
pub fn global_id(&self) -> Id {
self.id.global_id()
pub fn global_id(&self) -> Id<Adapter> {
Id(self.id.global_id(), std::marker::PhantomData)
}
}

Expand All @@ -4263,10 +4264,11 @@ impl Device {
/// Returns a globally-unique identifier for this `Device`.
///
/// Calling this method multiple times on the same object will always return the same value.
/// The returned value is guaranteed to be different for all resources created from the same `Instance`.
/// The returned value is guaranteed to be unique among all `Device`s created from the same
/// `Instance`.
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
pub fn global_id(&self) -> Id {
self.id.global_id()
pub fn global_id(&self) -> Id<Device> {
Id(self.id.global_id(), std::marker::PhantomData)
}
}

Expand All @@ -4275,10 +4277,11 @@ impl Queue {
/// Returns a globally-unique identifier for this `Queue`.
///
/// Calling this method multiple times on the same object will always return the same value.
/// The returned value is guaranteed to be different for all resources created from the same `Instance`.
/// The returned value is guaranteed to be unique among all `Queue`s created from the same
/// `Instance`.
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
pub fn global_id(&self) -> Id {
self.id.global_id()
pub fn global_id(&self) -> Id<Queue> {
Id(self.id.global_id(), std::marker::PhantomData)
}
}

Expand All @@ -4287,10 +4290,11 @@ impl ShaderModule {
/// Returns a globally-unique identifier for this `ShaderModule`.
///
/// Calling this method multiple times on the same object will always return the same value.
/// The returned value is guaranteed to be different for all resources created from the same `Instance`.
/// The returned value is guaranteed to be unique among all `ShaderModule`s created from the same
/// `Instance`.
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
pub fn global_id(&self) -> Id {
self.id.global_id()
pub fn global_id(&self) -> Id<ShaderModule> {
Id(self.id.global_id(), std::marker::PhantomData)
}
}

Expand All @@ -4299,10 +4303,11 @@ impl BindGroupLayout {
/// Returns a globally-unique identifier for this `BindGroupLayout`.
///
/// Calling this method multiple times on the same object will always return the same value.
/// The returned value is guaranteed to be different for all resources created from the same `Instance`.
/// The returned value is guaranteed to be unique among all `BindGroupLayout`s created from the same
/// `Instance`.
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
pub fn global_id(&self) -> Id {
self.id.global_id()
pub fn global_id(&self) -> Id<BindGroupLayout> {
Id(self.id.global_id(), std::marker::PhantomData)
}
}

Expand All @@ -4311,10 +4316,11 @@ impl BindGroup {
/// Returns a globally-unique identifier for this `BindGroup`.
///
/// Calling this method multiple times on the same object will always return the same value.
/// The returned value is guaranteed to be different for all resources created from the same `Instance`.
/// The returned value is guaranteed to be unique among all `BindGroup`s created from the same
/// `Instance`.
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
pub fn global_id(&self) -> Id {
self.id.global_id()
pub fn global_id(&self) -> Id<BindGroup> {
Id(self.id.global_id(), std::marker::PhantomData)
}
}

Expand All @@ -4323,10 +4329,11 @@ impl TextureView {
/// Returns a globally-unique identifier for this `TextureView`.
///
/// Calling this method multiple times on the same object will always return the same value.
/// The returned value is guaranteed to be different for all resources created from the same `Instance`.
/// The returned value is guaranteed to be unique among all `TextureView`s created from the same
/// `Instance`.
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
pub fn global_id(&self) -> Id {
self.id.global_id()
pub fn global_id(&self) -> Id<TextureView> {
Id(self.id.global_id(), std::marker::PhantomData)
}
}

Expand All @@ -4335,10 +4342,11 @@ impl Sampler {
/// Returns a globally-unique identifier for this `Sampler`.
///
/// Calling this method multiple times on the same object will always return the same value.
/// The returned value is guaranteed to be different for all resources created from the same `Instance`.
/// The returned value is guaranteed to be unique among all `Sampler`s created from the same
/// `Instance`.
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
pub fn global_id(&self) -> Id {
self.id.global_id()
pub fn global_id(&self) -> Id<Sampler> {
Id(self.id.global_id(), std::marker::PhantomData)
}
}

Expand All @@ -4347,10 +4355,11 @@ impl Buffer {
/// Returns a globally-unique identifier for this `Buffer`.
///
/// Calling this method multiple times on the same object will always return the same value.
/// The returned value is guaranteed to be different for all resources created from the same `Instance`.
/// The returned value is guaranteed to be unique among all `Buffer`s created from the same
/// `Instance`.
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
pub fn global_id(&self) -> Id {
self.id.global_id()
pub fn global_id(&self) -> Id<Buffer> {
Id(self.id.global_id(), std::marker::PhantomData)
}
}

Expand All @@ -4359,10 +4368,11 @@ impl Texture {
/// Returns a globally-unique identifier for this `Texture`.
///
/// Calling this method multiple times on the same object will always return the same value.
/// The returned value is guaranteed to be different for all resources created from the same `Instance`.
/// The returned value is guaranteed to be unique among all `Texture`s created from the same
/// `Instance`.
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
pub fn global_id(&self) -> Id {
self.id.global_id()
pub fn global_id(&self) -> Id<Texture> {
Id(self.id.global_id(), std::marker::PhantomData)
}
}

Expand All @@ -4371,10 +4381,11 @@ impl QuerySet {
/// Returns a globally-unique identifier for this `QuerySet`.
///
/// Calling this method multiple times on the same object will always return the same value.
/// The returned value is guaranteed to be different for all resources created from the same `Instance`.
/// The returned value is guaranteed to be unique among all `QuerySet`s created from the same
/// `Instance`.
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
pub fn global_id(&self) -> Id {
self.id.global_id()
pub fn global_id(&self) -> Id<QuerySet> {
Id(self.id.global_id(), std::marker::PhantomData)
}
}

Expand All @@ -4383,10 +4394,11 @@ impl PipelineLayout {
/// Returns a globally-unique identifier for this `PipelineLayout`.
///
/// Calling this method multiple times on the same object will always return the same value.
/// The returned value is guaranteed to be different for all resources created from the same `Instance`.
/// The returned value is guaranteed to be unique among all `PipelineLayout`s created from the same
/// `Instance`.
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
pub fn global_id(&self) -> Id {
self.id.global_id()
pub fn global_id(&self) -> Id<PipelineLayout> {
Id(self.id.global_id(), std::marker::PhantomData)
}
}

Expand All @@ -4395,10 +4407,11 @@ impl RenderPipeline {
/// Returns a globally-unique identifier for this `RenderPipeline`.
///
/// Calling this method multiple times on the same object will always return the same value.
/// The returned value is guaranteed to be different for all resources created from the same `Instance`.
/// The returned value is guaranteed to be unique among all `RenderPipeline`s created from the same
/// `Instance`.
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
pub fn global_id(&self) -> Id {
self.id.global_id()
pub fn global_id(&self) -> Id<RenderPipeline> {
Id(self.id.global_id(), std::marker::PhantomData)
}
}

Expand All @@ -4407,10 +4420,11 @@ impl ComputePipeline {
/// Returns a globally-unique identifier for this `ComputePipeline`.
///
/// Calling this method multiple times on the same object will always return the same value.
/// The returned value is guaranteed to be different for all resources created from the same `Instance`.
/// The returned value is guaranteed to be unique among all `ComputePipeline`s created from the same
/// `Instance`.
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
pub fn global_id(&self) -> Id {
self.id.global_id()
pub fn global_id(&self) -> Id<ComputePipeline> {
Id(self.id.global_id(), std::marker::PhantomData)
}
}

Expand All @@ -4419,10 +4433,11 @@ impl RenderBundle {
/// Returns a globally-unique identifier for this `RenderBundle`.
///
/// Calling this method multiple times on the same object will always return the same value.
/// The returned value is guaranteed to be different for all resources created from the same `Instance`.
/// The returned value is guaranteed to be unique among all `RenderBundle`s created from the same
/// `Instance`.
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
pub fn global_id(&self) -> Id {
self.id.global_id()
pub fn global_id(&self) -> Id<RenderBundle> {
Id(self.id.global_id(), std::marker::PhantomData)
}
}

Expand All @@ -4431,10 +4446,11 @@ impl Surface {
/// Returns a globally-unique identifier for this `Surface`.
///
/// Calling this method multiple times on the same object will always return the same value.
/// The returned value is guaranteed to be different for all resources created from the same `Instance`.
/// The returned value is guaranteed to be unique among all `Surface`s created from the same
/// `Instance`.
#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))]
pub fn global_id(&self) -> Id {
self.id.global_id()
pub fn global_id(&self) -> Id<Surface> {
Id(self.id.global_id(), std::marker::PhantomData)
}
}

Expand Down

0 comments on commit 53ddf68

Please sign in to comment.