From aa9d8af9f5c0933764a94edb61bd6039b283a2c8 Mon Sep 17 00:00:00 2001 From: Zac Harrold Date: Tue, 18 Aug 2026 11:40:45 +1000 Subject: [PATCH] wgpu-hal: use `wgpu-sync` for atomics --- Cargo.lock | 2 -- wgpu-hal/Cargo.toml | 17 ++--------------- wgpu-hal/build.rs | 2 -- wgpu-hal/src/dx12/adapter.rs | 4 ++-- wgpu-hal/src/dx12/mod.rs | 3 ++- wgpu-hal/src/gles/adapter.rs | 3 +-- wgpu-hal/src/gles/mod.rs | 9 ++++----- wgpu-hal/src/lib.rs | 16 +++------------- wgpu-hal/src/metal/adapter.rs | 3 +-- wgpu-hal/src/metal/command.rs | 3 ++- wgpu-hal/src/metal/mod.rs | 4 ++-- wgpu-hal/src/noop/buffer.rs | 8 +------- wgpu-hal/src/noop/mod.rs | 5 +---- wgpu-hal/src/vulkan/mod.rs | 4 ++-- 14 files changed, 23 insertions(+), 60 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 606c8624c..4585d0497 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4984,8 +4984,6 @@ dependencies = [ "objc2-metal 0.3.2", "objc2-quartz-core 0.3.2", "ordered-float", - "portable-atomic", - "portable-atomic-util", "profiling", "range-alloc", "raw-window-handle", diff --git a/wgpu-hal/Cargo.toml b/wgpu-hal/Cargo.toml index 2b93401d0..782eb950f 100644 --- a/wgpu-hal/Cargo.toml +++ b/wgpu-hal/Cargo.toml @@ -87,7 +87,6 @@ metal = [ "dep:profiling", "dep:smallvec", "dep:raw-window-metal", - "dep:wgpu-sync", ] vulkan = [ "naga/spv-out", @@ -102,7 +101,6 @@ vulkan = [ "dep:profiling", "dep:raw-window-metal", "dep:smallvec", - "dep:wgpu-sync", "dep:windows", "dep:windows-core", "gpu-allocator/vulkan", @@ -127,7 +125,6 @@ gles = [ "dep:wasm-bindgen", "dep:wayland-sys", "dep:web-sys", - "dep:wgpu-sync", "dep:windows-result", "naga/glsl-out", "wgpu-sync/std", # Required for send/sync @@ -148,7 +145,6 @@ dx12 = [ "dep:ordered-float", "dep:profiling", "dep:range-alloc", - "dep:wgpu-sync", "dep:windows-core", "gpu-allocator/d3d12", "naga/hlsl-out", @@ -179,7 +175,7 @@ renderdoc = ["dep:libloading", "dep:renderdoc-sys"] fragile-send-sync-non-atomic-wasm = [ "wgpu-types/fragile-send-sync-non-atomic-wasm", ] -portable-atomic = ["dep:portable-atomic", "dep:portable-atomic-util"] +portable-atomic = ["wgpu-sync/portable-atomic"] ################################### ### Internal Debugging Features ### @@ -196,7 +192,6 @@ device_lost_panic = [] internal_error_panic = [] # Tracks validation errors in a `VALIDATION_CANARY` static. validation_canary = [ - "dep:wgpu-sync", "wgpu-sync/std", # Required for send/sync ] @@ -215,7 +210,7 @@ required-features = ["gles"] naga.workspace = true wgpu-naga-bridge.workspace = true naga-types = { workspace = true } -wgpu-sync = { workspace = true, default-features = false, optional = true } +wgpu-sync = { workspace = true, default-features = false } wgpu-types = { workspace = true, default-features = false } # Dependencies in the lib and empty backend @@ -348,14 +343,6 @@ khronos-egl = { workspace = true, optional = true, features = [ # Note: it's unused by emscripten, but we keep it to have single code base in egl.rs libloading = { workspace = true, optional = true } -[target.'cfg(any(not(target_has_atomic = "64"), not(target_has_atomic = "ptr")))'.dependencies] -portable-atomic = { workspace = true, optional = true } - -[target.'cfg(not(target_has_atomic = "ptr"))'.dependencies] -portable-atomic-util = { workspace = true, features = [ - "alloc", -], optional = true } - ### Platform: Windows + MacOS + Linux for "raw-gles" example ### [target.'cfg(not(any(target_family = "wasm", target_os = "ios", target_os = "visionos", target_env = "ohos")))'.dev-dependencies] glutin-winit = { workspace = true, features = ["egl", "wgl", "wayland", "x11"] } diff --git a/wgpu-hal/build.rs b/wgpu-hal/build.rs index dc441284c..8892da454 100644 --- a/wgpu-hal/build.rs +++ b/wgpu-hal/build.rs @@ -30,7 +30,5 @@ fn main() { any_backend: { any(dx12, metal, vulkan, gles) }, // ⚠️ Keep in sync with target.cfg() definition in Cargo.toml and cfg_alias in `wgpu` crate ⚠️ static_dxc: { all(target_os = "windows", feature = "static-dxc", not(target_arch = "aarch64"), target_env = "msvc") }, - supports_64bit_atomics: { target_has_atomic = "64" }, - supports_ptr_atomics: { target_has_atomic = "ptr" } } } diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index 2e3a093f1..383dc6fb7 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -1,8 +1,8 @@ use alloc::{string::String, sync::Arc, vec::Vec}; -use core::{ptr, sync::atomic::AtomicU64}; +use core::ptr; use std::thread; -use wgpu_sync::Mutex; +use wgpu_sync::{atomic::AtomicU64, Mutex}; use windows::{ core::Interface as _, Win32::{ diff --git a/wgpu-hal/src/dx12/mod.rs b/wgpu-hal/src/dx12/mod.rs index cc81bac68..72bef7f84 100644 --- a/wgpu-hal/src/dx12/mod.rs +++ b/wgpu-hal/src/dx12/mod.rs @@ -93,11 +93,12 @@ mod types; mod view; use alloc::{borrow::ToOwned as _, string::String, sync::Arc, vec::Vec}; -use core::{ffi, fmt, mem, ops::Deref, sync::atomic::AtomicU64}; +use core::{ffi, fmt, mem, ops::Deref}; use arrayvec::ArrayVec; use hashbrown::HashMap; use suballocation::Allocator; +use wgpu_sync::atomic::AtomicU64; use wgpu_sync::{Mutex, RwLock}; use windows::{ core::{Free as _, Interface}, diff --git a/wgpu-hal/src/gles/adapter.rs b/wgpu-hal/src/gles/adapter.rs index 654fc7bcb..dfd6c9b10 100644 --- a/wgpu-hal/src/gles/adapter.rs +++ b/wgpu-hal/src/gles/adapter.rs @@ -1,8 +1,7 @@ use alloc::{borrow::ToOwned as _, format, string::String, sync::Arc, vec, vec::Vec}; -use core::sync::atomic::AtomicU8; use glow::HasContext; -use wgpu_sync::Mutex; +use wgpu_sync::{atomic::AtomicU8, Mutex}; use wgt::AstcChannel; use crate::auxil::db; diff --git a/wgpu-hal/src/gles/mod.rs b/wgpu-hal/src/gles/mod.rs index a8aac9301..caa37f4d7 100644 --- a/wgpu-hal/src/gles/mod.rs +++ b/wgpu-hal/src/gles/mod.rs @@ -110,12 +110,11 @@ pub use self::wgl::{AdapterContext, AdapterContextLock, Instance, Surface}; pub use fence::Fence; use alloc::{boxed::Box, string::String, string::ToString as _, sync::Arc, vec::Vec}; -use core::{ - fmt, - ops::Range, - sync::atomic::{AtomicU32, AtomicU8}, +use core::{fmt, ops::Range}; +use wgpu_sync::{ + atomic::{AtomicU32, AtomicU8}, + Mutex, }; -use wgpu_sync::Mutex; use arrayvec::ArrayVec; use glow::HasContext; diff --git a/wgpu-hal/src/lib.rs b/wgpu-hal/src/lib.rs index 2cb1f310a..39bc8681c 100644 --- a/wgpu-hal/src/lib.rs +++ b/wgpu-hal/src/lib.rs @@ -308,16 +308,9 @@ use core::{ use bitflags::bitflags; use raw_window_handle::DisplayHandle; use thiserror::Error; +use wgpu_sync::Arc; use wgt::WasmNotSendSync; -cfg_if::cfg_if! { - if #[cfg(supports_ptr_atomics)] { - use alloc::sync::Arc; - } else if #[cfg(feature = "portable-atomic")] { - use portable_atomic_util::Arc; - } -} - // - Vertex + Fragment // - Compute // Task + Mesh + Fragment @@ -334,10 +327,7 @@ pub const QUERY_SIZE: wgt::BufferAddress = 8; pub type Label<'a> = Option<&'a str>; pub type MemoryRange = Range; pub type FenceValue = u64; -#[cfg(supports_64bit_atomics)] -pub type AtomicFenceValue = core::sync::atomic::AtomicU64; -#[cfg(not(supports_64bit_atomics))] -pub type AtomicFenceValue = portable_atomic::AtomicU64; +pub type AtomicFenceValue = wgpu_sync::atomic::AtomicU64; /// A callback to signal that wgpu is no longer using a resource. #[cfg(all(any(gles, vulkan, metal), not(webgl)))] @@ -597,7 +587,7 @@ impl InstanceError { #[allow(dead_code, reason = "may be unused on some platforms")] pub(crate) fn with_source(message: String, source: impl Error + Send + Sync + 'static) -> Self { cfg_if::cfg_if! { - if #[cfg(supports_ptr_atomics)] { + if #[cfg(target_has_atomic = "ptr")] { let source = Arc::new(source); } else { // TODO(https://github.com/rust-lang/rust/issues/18598): avoid indirection via Box once arbitrary types support unsized coercion diff --git a/wgpu-hal/src/metal/adapter.rs b/wgpu-hal/src/metal/adapter.rs index 0b5e2abd5..298464517 100644 --- a/wgpu-hal/src/metal/adapter.rs +++ b/wgpu-hal/src/metal/adapter.rs @@ -10,8 +10,7 @@ use objc2_metal::{ use wgt::{AstcBlock, AstcChannel}; use alloc::{string::ToString as _, sync::Arc, vec::Vec}; -use core::sync::atomic; -use wgpu_sync::{Mutex, OnceCell}; +use wgpu_sync::{atomic, Mutex, OnceCell}; use crate::metal::QueueShared; diff --git a/wgpu-hal/src/metal/command.rs b/wgpu-hal/src/metal/command.rs index 3be233a8f..d918ae714 100644 --- a/wgpu-hal/src/metal/command.rs +++ b/wgpu-hal/src/metal/command.rs @@ -23,8 +23,9 @@ use alloc::{ sync::Arc, vec::Vec, }; -use core::{ops::Range, ptr::NonNull, sync::atomic}; +use core::{ops::Range, ptr::NonNull}; use smallvec::SmallVec; +use wgpu_sync::atomic; // has to match `Temp::binding_sizes` const WORD_SIZE: usize = 4; diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs index 14f814e40..1ef4eabef 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -33,7 +33,7 @@ use alloc::{ sync::Arc, vec::Vec, }; -use core::{fmt, iter, ops, ptr::NonNull, sync::atomic}; +use core::{fmt, iter, ops, ptr::NonNull}; use bitflags::bitflags; use hashbrown::HashMap; @@ -55,7 +55,7 @@ use objc2_metal::{ MTLTriangleFillMode, MTLWinding, }; use objc2_quartz_core::CAMetalLayer; -use wgpu_sync::{Condvar, Mutex, OnceCell, RwLock}; +use wgpu_sync::{atomic, Condvar, Mutex, OnceCell, RwLock}; #[derive(Clone, Debug)] pub struct Api; diff --git a/wgpu-hal/src/noop/buffer.rs b/wgpu-hal/src/noop/buffer.rs index 9b8ab17a1..1a12913e5 100644 --- a/wgpu-hal/src/noop/buffer.rs +++ b/wgpu-hal/src/noop/buffer.rs @@ -1,13 +1,7 @@ use alloc::vec::Vec; use core::{cell::UnsafeCell, ops::Range, ptr}; -cfg_if::cfg_if! { - if #[cfg(supports_ptr_atomics)] { - use alloc::sync::Arc; - } else if #[cfg(feature = "portable-atomic")] { - use portable_atomic_util::Arc; - } -} +use wgpu_sync::Arc; #[derive(Clone, Debug)] pub struct Buffer { diff --git a/wgpu-hal/src/noop/mod.rs b/wgpu-hal/src/noop/mod.rs index 609940711..c42f00d73 100644 --- a/wgpu-hal/src/noop/mod.rs +++ b/wgpu-hal/src/noop/mod.rs @@ -3,10 +3,7 @@ use alloc::{string::String, sync::Arc, vec, vec::Vec}; use core::{ptr, sync::atomic::Ordering, time::Duration}; -#[cfg(supports_64bit_atomics)] -use core::sync::atomic::AtomicU64; -#[cfg(not(supports_64bit_atomics))] -use portable_atomic::AtomicU64; +use wgpu_sync::atomic::AtomicU64; use crate::TlasInstance; diff --git a/wgpu-hal/src/vulkan/mod.rs b/wgpu-hal/src/vulkan/mod.rs index 516eba8ae..c307fd9a9 100644 --- a/wgpu-hal/src/vulkan/mod.rs +++ b/wgpu-hal/src/vulkan/mod.rs @@ -1009,7 +1009,7 @@ struct ResourceIdentityFactory { #[cfg(not(target_has_atomic = "64"))] next_id: Mutex, #[cfg(target_has_atomic = "64")] - next_id: core::sync::atomic::AtomicU64, + next_id: wgpu_sync::atomic::AtomicU64, _phantom: PhantomData, } @@ -1019,7 +1019,7 @@ impl ResourceIdentityFactory { #[cfg(not(target_has_atomic = "64"))] next_id: Mutex::new(0), #[cfg(target_has_atomic = "64")] - next_id: core::sync::atomic::AtomicU64::new(0), + next_id: wgpu_sync::atomic::AtomicU64::new(0), _phantom: PhantomData, } }