From 53ddf687c6bf45a796ee434bfc21f9c63c1886e2 Mon Sep 17 00:00:00 2001 From: TheOnlyMrCat Date: Thu, 30 Mar 2023 04:11:36 +1000 Subject: [PATCH] Add a generic parameter to `wgpu::Id` (#3575) --- wgpu/src/backend/web.rs | 16 +++--- wgpu/src/context.rs | 8 +-- wgpu/src/lib.rs | 114 +++++++++++++++++++++++----------------- 3 files changed, 77 insertions(+), 61 deletions(-) diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index cfa22e3e6b..bb39335d1c 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -27,7 +27,7 @@ fn create_identified(value: T) -> Identified { 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) } @@ -43,8 +43,8 @@ fn create_identified(value: T) -> Identified { // is integrated (or not integrated) with values like those in webgpu, this may become unsound. impl + JsCast> From for Identified { - 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. // @@ -55,18 +55,18 @@ impl + JsCast> From for Identified { Self( wasm.unchecked_into(), #[cfg(feature = "expose-ids")] - id.global_id(), + object_id.global_id(), ) } } impl> From> for ObjectId { - fn from(id: Identified) -> Self { - let id = core::num::NonZeroU64::new(id.0.into_abi() as u64).unwrap(); + fn from(identified: Identified) -> 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, ) } } @@ -77,7 +77,7 @@ unsafe impl Send for Sendable {} unsafe impl Sync for Sendable {} #[derive(Clone, Debug)] -pub(crate) struct Identified(T, #[cfg(feature = "expose-ids")] crate::Id); +pub(crate) struct Identified(T, #[cfg(feature = "expose-ids")] std::num::NonZeroU64); unsafe impl Send for Identified {} unsafe impl Sync for Identified {} diff --git a/wgpu/src/context.rs b/wgpu/src/context.rs index 92b85a9a04..d45f2a6321 100644 --- a/wgpu/src/context.rs +++ b/wgpu/src/context.rs @@ -998,7 +998,7 @@ pub struct ObjectId { id: Option, #[cfg(feature = "expose-ids")] /// ID that is unique at all times - global_id: Option, + global_id: Option, } impl ObjectId { @@ -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")] @@ -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), } } @@ -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() } } diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index ee92091f01..e9c700053c 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -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(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 { + Id(self.id.global_id(), std::marker::PhantomData) } } @@ -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 { + Id(self.id.global_id(), std::marker::PhantomData) } } @@ -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 { + Id(self.id.global_id(), std::marker::PhantomData) } } @@ -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 { + Id(self.id.global_id(), std::marker::PhantomData) } } @@ -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 { + Id(self.id.global_id(), std::marker::PhantomData) } } @@ -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 { + Id(self.id.global_id(), std::marker::PhantomData) } } @@ -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 { + Id(self.id.global_id(), std::marker::PhantomData) } } @@ -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 { + Id(self.id.global_id(), std::marker::PhantomData) } } @@ -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 { + Id(self.id.global_id(), std::marker::PhantomData) } } @@ -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 { + Id(self.id.global_id(), std::marker::PhantomData) } } @@ -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 { + Id(self.id.global_id(), std::marker::PhantomData) } } @@ -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 { + Id(self.id.global_id(), std::marker::PhantomData) } } @@ -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 { + Id(self.id.global_id(), std::marker::PhantomData) } } @@ -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 { + Id(self.id.global_id(), std::marker::PhantomData) } } @@ -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 { + Id(self.id.global_id(), std::marker::PhantomData) } } @@ -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 { + Id(self.id.global_id(), std::marker::PhantomData) } }