diff --git a/CHANGELOG.md b/CHANGELOG.md index 30fa02f7aa..6fe3646b4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,6 +51,10 @@ This adds a way to allow a Vulkan driver which is non-compliant per VK_KHR_drive Previously, `DeviceExt::create_texture_with_data` only allowed data to be provided in layer major order. There is now a `order` parameter which allows you to specify if the data is in layer major or mip major order. +### `expose-ids` feature now available unconditionally + +This feature allowed you to call `global_id` on any wgpu opaque handle to get a unique hashable identity for the given resource. This is now available without the feature flag. By @cwfitzgerald in [#4841](https://github.com/gfx-rs/wgpu/pull/4841) + ### New Features #### General diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index bebeeaf5ff..f101928380 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -38,7 +38,6 @@ angle = ["wgc/gles"] webgl = ["hal", "wgc/gles"] # Enables the Vulkan backend on macOS & iOS vulkan-portability = ["wgc/vulkan"] -expose-ids = [] # Implement `Send` and `Sync` on Wasm. fragile-send-sync-non-atomic-wasm = [ "hal/fragile-send-sync-non-atomic-wasm", diff --git a/wgpu/src/backend/web.rs b/wgpu/src/backend/web.rs index 9b59a2d4ba..543a583575 100644 --- a/wgpu/src/backend/web.rs +++ b/wgpu/src/backend/web.rs @@ -7,9 +7,11 @@ use std::{ fmt, future::Future, marker::PhantomData, + num::NonZeroU64, ops::Range, pin::Pin, rc::Rc, + sync::atomic::{AtomicU64, Ordering}, task::{self, Poll}, }; use wasm_bindgen::{prelude::*, JsCast}; @@ -20,15 +22,12 @@ use crate::{ }; fn create_identified(value: T) -> (Identified, Sendable) { - cfg_if::cfg_if! { - 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(core::num::NonZeroU64::new(id).unwrap(), PhantomData), Sendable(value)) - } else { - (Identified(PhantomData), Sendable(value)) - } - } + static NEXT_ID: AtomicU64 = AtomicU64::new(1); + let id = NEXT_ID.fetch_add(1, Ordering::Relaxed); + ( + Identified(NonZeroU64::new(id).unwrap(), PhantomData), + Sendable(value), + ) } // We need to make a wrapper for some of the handle types returned by the web backend to make them @@ -39,25 +38,18 @@ fn create_identified(value: T) -> (Identified, Sendable) { // type is (for now) harmless. Eventually wasm32 will support threading, and depending on how this // is integrated (or not integrated) with values like those in webgpu, this may become unsound. -#[allow(unused_variables)] impl From for Identified { fn from(object_id: ObjectId) -> Self { - Self( - #[cfg(feature = "expose-ids")] - object_id.global_id(), - PhantomData, - ) + Self(object_id.global_id(), PhantomData) } } -#[allow(unused_variables)] impl From> for ObjectId { fn from(identified: Identified) -> Self { Self::new( // TODO: the ID isn't used, so we hardcode it to 1 for now until we rework this // API. - core::num::NonZeroU64::new(1).unwrap(), - #[cfg(feature = "expose-ids")] + NonZeroU64::new(1).unwrap(), identified.0, ) } @@ -77,10 +69,7 @@ unsafe impl Send for Sendable {} unsafe impl Sync for Sendable {} #[derive(Clone, Debug)] -pub(crate) struct Identified( - #[cfg(feature = "expose-ids")] std::num::NonZeroU64, - PhantomData, -); +pub(crate) struct Identified(std::num::NonZeroU64, PhantomData); #[cfg(all( feature = "fragile-send-sync-non-atomic-wasm", not(target_feature = "atomics") diff --git a/wgpu/src/context.rs b/wgpu/src/context.rs index 12da5b2a95..f81867a2ea 100644 --- a/wgpu/src/context.rs +++ b/wgpu/src/context.rs @@ -1033,7 +1033,6 @@ pub trait Context: Debug + WasmNotSendSync + Sized { pub struct ObjectId { /// ID that is unique at any given time id: Option, - #[cfg(feature = "expose-ids")] /// ID that is unique at all times global_id: Option, } @@ -1041,14 +1040,12 @@ pub struct ObjectId { impl ObjectId { pub(crate) const UNUSED: Self = ObjectId { id: None, - #[cfg(feature = "expose-ids")] global_id: None, }; - pub fn new(id: NonZeroU64, #[cfg(feature = "expose-ids")] global_id: NonZeroU64) -> Self { + pub fn new(id: NonZeroU64, global_id: NonZeroU64) -> Self { Self { id: Some(id), - #[cfg(feature = "expose-ids")] global_id: Some(global_id), } } @@ -1057,7 +1054,6 @@ impl ObjectId { pub fn from_global_id(global_id: NonZeroU64) -> Self { Self { id: Some(global_id), - #[cfg(feature = "expose-ids")] global_id: Some(global_id), } } @@ -1067,8 +1063,6 @@ impl ObjectId { self.id.unwrap() } - #[cfg(feature = "expose-ids")] - #[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] pub fn global_id(&self) -> NonZeroU64 { self.global_id.unwrap() } diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 19199a0490..939964ce99 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -18,7 +18,7 @@ use std::{ error, fmt, future::Future, marker::PhantomData, - num::NonZeroU32, + num::{NonZeroU32, NonZeroU64}, ops::{Bound, Deref, DerefMut, Range, RangeBounds}, sync::Arc, thread, @@ -5124,243 +5124,201 @@ impl Surface<'_> { } /// Opaque globally-unique identifier -#[cfg(feature = "expose-ids")] -#[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] #[repr(transparent)] -pub struct Id(::core::num::NonZeroU64, std::marker::PhantomData<*mut T>); +pub struct Id(NonZeroU64, PhantomData<*mut T>); // SAFETY: `Id` is a bare `NonZeroU64`, the type parameter is a marker purely to avoid confusing Ids // returned for different types , so `Id` can safely implement Send and Sync. -#[cfg(feature = "expose-ids")] unsafe impl Send for Id {} // SAFETY: See the implementation for `Send`. -#[cfg(feature = "expose-ids")] unsafe impl Sync for Id {} -#[cfg(feature = "expose-ids")] impl Clone for Id { fn clone(&self) -> Self { *self } } -#[cfg(feature = "expose-ids")] impl Copy for Id {} -#[cfg(feature = "expose-ids")] impl fmt::Debug for Id { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_tuple("Id").field(&self.0).finish() } } -#[cfg(feature = "expose-ids")] impl PartialEq for Id { fn eq(&self, other: &Id) -> bool { self.0 == other.0 } } -#[cfg(feature = "expose-ids")] impl Eq for Id {} -#[cfg(feature = "expose-ids")] impl std::hash::Hash for Id { fn hash(&self, state: &mut H) { self.0.hash(state) } } -#[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`. - #[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] pub fn global_id(&self) -> Id { - Id(self.id.global_id(), std::marker::PhantomData) + Id(self.id.global_id(), PhantomData) } } -#[cfg(feature = "expose-ids")] 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`. - #[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] pub fn global_id(&self) -> Id { - Id(self.id.global_id(), std::marker::PhantomData) + Id(self.id.global_id(), PhantomData) } } -#[cfg(feature = "expose-ids")] 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`. - #[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] pub fn global_id(&self) -> Id { - Id(self.id.global_id(), std::marker::PhantomData) + Id(self.id.global_id(), PhantomData) } } -#[cfg(feature = "expose-ids")] 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`. - #[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] pub fn global_id(&self) -> Id { - Id(self.id.global_id(), std::marker::PhantomData) + Id(self.id.global_id(), PhantomData) } } -#[cfg(feature = "expose-ids")] 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`. - #[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] pub fn global_id(&self) -> Id { - Id(self.id.global_id(), std::marker::PhantomData) + Id(self.id.global_id(), PhantomData) } } -#[cfg(feature = "expose-ids")] 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`. - #[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] pub fn global_id(&self) -> Id { - Id(self.id.global_id(), std::marker::PhantomData) + Id(self.id.global_id(), PhantomData) } } -#[cfg(feature = "expose-ids")] 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`. - #[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] pub fn global_id(&self) -> Id { - Id(self.id.global_id(), std::marker::PhantomData) + Id(self.id.global_id(), PhantomData) } } -#[cfg(feature = "expose-ids")] 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`. - #[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] pub fn global_id(&self) -> Id { - Id(self.id.global_id(), std::marker::PhantomData) + Id(self.id.global_id(), PhantomData) } } -#[cfg(feature = "expose-ids")] 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`. - #[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] pub fn global_id(&self) -> Id { - Id(self.id.global_id(), std::marker::PhantomData) + Id(self.id.global_id(), PhantomData) } } -#[cfg(feature = "expose-ids")] 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`. - #[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] pub fn global_id(&self) -> Id { - Id(self.id.global_id(), std::marker::PhantomData) + Id(self.id.global_id(), PhantomData) } } -#[cfg(feature = "expose-ids")] 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`. - #[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] pub fn global_id(&self) -> Id { - Id(self.id.global_id(), std::marker::PhantomData) + Id(self.id.global_id(), PhantomData) } } -#[cfg(feature = "expose-ids")] 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`. - #[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] pub fn global_id(&self) -> Id { - Id(self.id.global_id(), std::marker::PhantomData) + Id(self.id.global_id(), PhantomData) } } -#[cfg(feature = "expose-ids")] 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`. - #[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] pub fn global_id(&self) -> Id { - Id(self.id.global_id(), std::marker::PhantomData) + Id(self.id.global_id(), PhantomData) } } -#[cfg(feature = "expose-ids")] 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`. - #[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] pub fn global_id(&self) -> Id { - Id(self.id.global_id(), std::marker::PhantomData) + Id(self.id.global_id(), PhantomData) } } -#[cfg(feature = "expose-ids")] 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`. - #[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] pub fn global_id(&self) -> Id { - Id(self.id.global_id(), std::marker::PhantomData) + Id(self.id.global_id(), PhantomData) } } -#[cfg(feature = "expose-ids")] 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`. - #[cfg_attr(docsrs, doc(cfg(feature = "expose-ids")))] pub fn global_id(&self) -> Id> { - Id(self.id.global_id(), std::marker::PhantomData) + Id(self.id.global_id(), PhantomData) } }