From e72c4d832624d380a3e6153d18cef83aa5f48eeb Mon Sep 17 00:00:00 2001 From: Zachary Harrold Date: Tue, 10 Jun 2025 16:40:07 +1000 Subject: [PATCH] MVP `no_std` for `wgpu` (#7747) Co-authored-by: Andreas Reich <1220815+Wumpf@users.noreply.github.com> --- .github/workflows/ci.yml | 2 ++ Cargo.lock | 1 + wgpu/Cargo.toml | 29 ++++++++++++++-- wgpu/build.rs | 12 ++++++- wgpu/src/api/buffer.rs | 3 +- wgpu/src/api/device.rs | 3 +- wgpu/src/api/instance.rs | 4 +-- wgpu/src/api/surface.rs | 5 ++- wgpu/src/api/surface_texture.rs | 22 ++++++++++-- wgpu/src/backend/wgpu_core.rs | 2 +- wgpu/src/lib.rs | 1 + wgpu/src/util/init.rs | 14 +++++--- wgpu/src/util/mod.rs | 7 ++++ wgpu/src/util/mutex.rs | 60 +++++++++++++++++++++++++++++++++ 14 files changed, 146 insertions(+), 19 deletions(-) create mode 100644 wgpu/src/util/mutex.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e8cde1a15..eb17c9bf8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -300,11 +300,13 @@ jobs: cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p wgpu-types --no-default-features cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p naga --no-default-features cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p wgpu-hal --no-default-features + cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p wgpu --no-default-features # Check with all compatible features cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p wgpu-types --no-default-features --features strict_asserts,fragile-send-sync-non-atomic-wasm,serde,counters cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p naga --no-default-features --features dot-out,compact cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p wgpu-hal --no-default-features --features fragile-send-sync-non-atomic-wasm + cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p wgpu --no-default-features --features serde # Building for native platforms with standard tests. - name: Check native diff --git a/Cargo.lock b/Cargo.lock index d16b61a65..076b4866f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4760,6 +4760,7 @@ dependencies = [ "arrayvec", "bitflags 2.9.1", "bytemuck", + "cfg-if", "cfg_aliases 0.2.1", "document-features", "hashbrown", diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 353f7b2f7..da200d6e8 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -29,7 +29,16 @@ ignored = ["cfg_aliases"] [lib] [features] -default = ["dx12", "metal", "gles", "vulkan", "wgsl", "webgpu"] +default = [ + "std", + "parking_lot", + "dx12", + "metal", + "gles", + "vulkan", + "wgsl", + "webgpu", +] #! ### Backends # -------------------------------------------------------------------- @@ -155,6 +164,19 @@ fragile-send-sync-non-atomic-wasm = [ ## context around the WASM VM, e.g., when the WASM binary is used in a browser. web = ["dep:wasm-bindgen", "dep:js-sys", "dep:web-sys", "wgpu-types/web"] +## Enables use of the standard library within `wgpu` and its dependencies. +## +## This can allow for better error reporting and for improved multithreading +## support. +std = ["raw-window-handle/std", "wgpu-types/std", "wgpu-core?/std"] + +## Uses `parking_lot` as the implementation for locking primitives. +## +## This is a recommended feature for most users and should only be disabled when +## required, e.g., for `no_std` support. +## If disabled, either `std::sync::Mutex` or `core::cell::RefCell` will be used, +## based on whether `std` is enabled or not. +parking_lot = ["dep:parking_lot"] ######################### # Standard Dependencies # @@ -168,12 +190,13 @@ wgpu-types.workspace = true # Needed for both wgpu-core and webgpu arrayvec.workspace = true bitflags.workspace = true +cfg-if.workspace = true document-features.workspace = true hashbrown.workspace = true log.workspace = true -parking_lot.workspace = true +parking_lot = { workspace = true, optional = true } profiling.workspace = true -raw-window-handle = { workspace = true, features = ["std"] } +raw-window-handle = { workspace = true, features = ["alloc"] } static_assertions.workspace = true ######################################## diff --git a/wgpu/build.rs b/wgpu/build.rs index 3f998d649..70543ba3e 100644 --- a/wgpu/build.rs +++ b/wgpu/build.rs @@ -47,6 +47,16 @@ fn main() { // ⚠️ Keep in sync with target.cfg() definition in wgpu-hal/Cargo.toml and cfg_alias in `wgpu-hal` crate ⚠️ static_dxc: { all(target_os = "windows", feature = "static-dxc", not(target_arch = "aarch64")) }, supports_64bit_atomics: { target_has_atomic = "64" }, - custom: {any(feature = "custom")} + custom: {any(feature = "custom")}, + std: { any( + feature = "std", + // TODO: Remove this when an alternative Mutex implementation is available for `no_std`. + // send_sync requires an appropriate Mutex implementation, which is only currently + // possible with `std` enabled. + send_sync, + // Unwinding panics necessitate access to `std` to determine if a thread is panicking + panic = "unwind" + ) }, + no_std: { not(std) } } } diff --git a/wgpu/src/api/buffer.rs b/wgpu/src/api/buffer.rs index 6d79f8074..4025eaf04 100644 --- a/wgpu/src/api/buffer.rs +++ b/wgpu/src/api/buffer.rs @@ -4,8 +4,7 @@ use core::{ ops::{Bound, Deref, DerefMut, Range, RangeBounds}, }; -use parking_lot::Mutex; - +use crate::util::Mutex; use crate::*; /// Handle to a GPU-accessible buffer. diff --git a/wgpu/src/api/device.rs b/wgpu/src/api/device.rs index 2955f322a..10f536985 100644 --- a/wgpu/src/api/device.rs +++ b/wgpu/src/api/device.rs @@ -1,10 +1,9 @@ use alloc::{boxed::Box, string::String, sync::Arc}; use core::{error, fmt, future::Future}; -use parking_lot::Mutex; - use crate::api::blas::{Blas, BlasGeometrySizeDescriptors, CreateBlasDescriptor}; use crate::api::tlas::{CreateTlasDescriptor, Tlas}; +use crate::util::Mutex; use crate::*; /// Open connection to a graphics and/or compute device. diff --git a/wgpu/src/api/instance.rs b/wgpu/src/api/instance.rs index ca0e9532b..0d9818f78 100644 --- a/wgpu/src/api/instance.rs +++ b/wgpu/src/api/instance.rs @@ -2,9 +2,7 @@ use alloc::vec::Vec; use core::future::Future; -use parking_lot::Mutex; - -use crate::{dispatch::InstanceInterface, *}; +use crate::{dispatch::InstanceInterface, util::Mutex, *}; bitflags::bitflags! { /// WGSL language extensions. diff --git a/wgpu/src/api/surface.rs b/wgpu/src/api/surface.rs index ada862247..c53d7289c 100644 --- a/wgpu/src/api/surface.rs +++ b/wgpu/src/api/surface.rs @@ -1,9 +1,9 @@ use alloc::{boxed::Box, string::String, vec, vec::Vec}; use core::{error, fmt}; -use parking_lot::Mutex; use raw_window_handle::{HasDisplayHandle, HasWindowHandle}; +use crate::util::Mutex; use crate::*; /// Describes a [`Surface`]. @@ -413,7 +413,10 @@ impl error::Error for CreateSurfaceError { #[cfg(wgpu_core)] CreateSurfaceErrorKind::Hal(e) => e.source(), CreateSurfaceErrorKind::Web(_) => None, + #[cfg(feature = "std")] CreateSurfaceErrorKind::RawHandle(e) => e.source(), + #[cfg(not(feature = "std"))] + CreateSurfaceErrorKind::RawHandle(_) => None, } } } diff --git a/wgpu/src/api/surface_texture.rs b/wgpu/src/api/surface_texture.rs index 6758e61a7..28298c9f2 100644 --- a/wgpu/src/api/surface_texture.rs +++ b/wgpu/src/api/surface_texture.rs @@ -1,5 +1,4 @@ use core::{error, fmt}; -use std::thread; use crate::*; @@ -48,7 +47,7 @@ impl SurfaceTexture { impl Drop for SurfaceTexture { fn drop(&mut self) { - if !self.presented && !thread::panicking() { + if !self.presented && !thread_panicking() { self.detail.texture_discard(); } } @@ -83,3 +82,22 @@ impl fmt::Display for SurfaceError { } impl error::Error for SurfaceError {} + +fn thread_panicking() -> bool { + cfg_if::cfg_if! { + if #[cfg(std)] { + std::thread::panicking() + } else if #[cfg(panic = "abort")] { + // If `panic = "abort"` then a thread _cannot_ be observably panicking by definition. + false + } else { + // TODO: This is potentially overly pessimistic; it may be appropriate to instead allow a + // texture to not be discarded. + // Alternatively, this could _also_ be a `panic!`, since we only care if the thread is panicking + // when the surface has not been presented. + compile_error!( + "cannot determine if a thread is panicking without either `panic = \"abort\"` or `std`" + ); + } + } +} diff --git a/wgpu/src/backend/wgpu_core.rs b/wgpu/src/backend/wgpu_core.rs index c9cf287c2..290a725ba 100644 --- a/wgpu/src/backend/wgpu_core.rs +++ b/wgpu/src/backend/wgpu_core.rs @@ -10,11 +10,11 @@ use alloc::{ use core::{error::Error, fmt, future::ready, ops::Range, pin::Pin, ptr::NonNull, slice}; use arrayvec::ArrayVec; -use parking_lot::Mutex; use smallvec::SmallVec; use wgc::{command::bundle_ffi::*, error::ContextErrorSource, pipeline::CreateShaderModuleError}; use wgt::WasmNotSendSync; +use crate::util::Mutex; use crate::{ api, dispatch::{self, BufferMappedRangeInterface}, diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 475005733..a29af32c8 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -31,6 +31,7 @@ #![cfg_attr(not(any(wgpu_core, webgpu)), allow(unused))] extern crate alloc; +#[cfg(std)] extern crate std; #[cfg(wgpu_core)] pub extern crate wgpu_core as wgc; diff --git a/wgpu/src/util/init.rs b/wgpu/src/util/init.rs index 5a22670c6..5e775e5aa 100644 --- a/wgpu/src/util/init.rs +++ b/wgpu/src/util/init.rs @@ -9,10 +9,16 @@ pub fn initialize_adapter_from_env( instance: &Instance, compatible_surface: Option<&Surface<'_>>, ) -> Result { - let desired_adapter_name = std::env::var("WGPU_ADAPTER_NAME") - .as_deref() - .map(str::to_lowercase) - .map_err(|_| wgt::RequestAdapterError::EnvNotSet)?; + cfg_if::cfg_if! { + if #[cfg(std)] { + let desired_adapter_name = std::env::var("WGPU_ADAPTER_NAME") + .as_deref() + .map(str::to_lowercase) + .map_err(|_| wgt::RequestAdapterError::EnvNotSet)?; + } else { + return Err(wgt::RequestAdapterError::EnvNotSet); + } + } let adapters = instance.enumerate_adapters(crate::Backends::all()); diff --git a/wgpu/src/util/mod.rs b/wgpu/src/util/mod.rs index 1af0f4948..9814cc658 100644 --- a/wgpu/src/util/mod.rs +++ b/wgpu/src/util/mod.rs @@ -3,15 +3,20 @@ //! Nothing in this module is a part of the WebGPU API specification; //! they are unique to the `wgpu` library. +// TODO: For [`belt::StagingBelt`] to be available in `no_std` its usage of [`std::sync::mpsc`] +// must be replaced with an appropriate alternative. +#[cfg(std)] mod belt; mod device; mod encoder; mod init; +mod mutex; mod texture_blitter; use alloc::{borrow::Cow, format, string::String, vec}; use core::ptr::copy_nonoverlapping; +#[cfg(std)] pub use belt::StagingBelt; pub use device::{BufferInitDescriptor, DeviceExt}; pub use encoder::RenderEncoder; @@ -22,6 +27,8 @@ pub use wgt::{ math::*, DispatchIndirectArgs, DrawIndexedIndirectArgs, DrawIndirectArgs, TextureDataOrder, }; +pub(crate) use mutex::Mutex; + use crate::dispatch; /// Treat the given byte slice as a SPIR-V module. diff --git a/wgpu/src/util/mutex.rs b/wgpu/src/util/mutex.rs new file mode 100644 index 000000000..86dff533d --- /dev/null +++ b/wgpu/src/util/mutex.rs @@ -0,0 +1,60 @@ +//! Provides a [`Mutex`] for internal use based on what features are available. + +cfg_if::cfg_if! { + if #[cfg(feature = "parking_lot")] { + use parking_lot::Mutex as MutexInner; + } else if #[cfg(std)] { + use std::sync::Mutex as MutexInner; + } else { + use core::cell::RefCell as MutexInner; + } +} + +pub(crate) struct Mutex { + inner: MutexInner, +} + +impl core::fmt::Debug for Mutex +where + MutexInner: core::fmt::Debug, +{ + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + as core::fmt::Debug>::fmt(&self.inner, f) + } +} + +impl Default for Mutex { + fn default() -> Self { + Self::new(::default()) + } +} + +impl Mutex { + pub const fn new(value: T) -> Self { + Self { + inner: MutexInner::new(value), + } + } +} + +impl Mutex { + pub fn lock(&self) -> impl core::ops::DerefMut + '_ { + cfg_if::cfg_if! { + if #[cfg(feature = "parking_lot")] { + self.inner.lock() + } else if #[cfg(std)] { + self.inner.lock().unwrap_or_else(std::sync::PoisonError::into_inner) + } else { + loop { + let Ok(lock) = self.inner.try_borrow_mut() else { + // Without `std` all we can do is spin until the current lock is released + core::hint::spin_loop(); + continue; + }; + + break lock; + } + } + } + } +}