Skip to content

Make std usage optional for wgpu. #7337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ wgpu = { version = "24.0.0", path = "./wgpu", default-features = false, features
"webgl",
"noop", # This should be removed if we ever have non-test crates that depend on wgpu
] }
wgpu-core = { version = "24.0.0", path = "./wgpu-core" }
wgpu-core = { version = "24.0.0", path = "./wgpu-core", default-features = false }
wgpu-hal = { version = "24.0.0", path = "./wgpu-hal" }
wgpu-macros = { version = "24.0.0", path = "./wgpu-macros" }
wgpu-test = { version = "24.0.0", path = "./tests" }
wgpu-types = { version = "24.0.0", path = "./wgpu-types" }
wgpu-types = { version = "24.0.0", path = "./wgpu-types", default-features = false }

wgpu-core-deps-windows-linux-android = { version = "24.0.0", path = "./wgpu-core/platform-deps/windows-linux-android" }
wgpu-core-deps-apple = { version = "24.0.0", path = "./wgpu-core/platform-deps/apple" }
Expand Down
1 change: 1 addition & 0 deletions deno_webgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ path = "lib.rs"
# so the whole workspace can built as wasm.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
wgpu-core = { workspace = true, features = [
"std",
"raw-window-handle",
"trace",
"replay",
Expand Down
10 changes: 9 additions & 1 deletion wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ignored = ["cfg_aliases"]
[lib]

[features]
default = ["dx12", "metal", "gles", "vulkan", "wgsl", "webgpu"]
default = ["std", "dx12", "metal", "gles", "vulkan", "wgsl", "webgpu"]

#! ### Backends
# --------------------------------------------------------------------
Expand Down Expand Up @@ -131,6 +131,13 @@ static-dxc = ["wgpu-core?/static-dxc"]
#! ### Other
# --------------------------------------------------------------------

## Enables functionality dependent on `std::sync` and `std::thread`:
##
## * `Send + Sync` for wgpu types
## * `wgpu::util::StagingBelt`
## * `*_from_env()` functions
std = ["wgpu-core?/std", "wgpu-types/std"]

## Internally count resources and events for debugging purposes. If the counters
## feature is disabled, the counting infrastructure is removed from the build and
## the exposed counters always return 0.
Expand All @@ -144,6 +151,7 @@ counters = ["wgpu-core?/counters"]
## This is technically *very* unsafe in a multithreaded environment,
## but on a wasm binary compiled without atomics is a definitionally single-threaded environment.
fragile-send-sync-non-atomic-wasm = [
"std",
"wgpu-core?/fragile-send-sync-non-atomic-wasm",
"wgpu-types/fragile-send-sync-non-atomic-wasm",
]
Expand Down
9 changes: 6 additions & 3 deletions wgpu/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ fn main() {
native: { not(target_arch = "wasm32") },
Emscripten: { all(target_arch = "wasm32", target_os = "emscripten") },

send_sync: { any(
native,
all(feature = "fragile-send-sync-non-atomic-wasm", not(target_feature = "atomics"))
send_sync: { all(
feature = "std",
any(
native,
all(feature = "fragile-send-sync-non-atomic-wasm", not(target_feature = "atomics"))
)
) },

// Backends - keep this in sync with `wgpu-core/Cargo.toml` & docs in `wgpu/Cargo.toml`
Expand Down
4 changes: 4 additions & 0 deletions wgpu/src/api/blas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,15 @@ pub struct BlasTriangleGeometry<'a> {
/// Transform buffer offset in bytes (optional, required if transform buffer is present).
pub transform_buffer_offset: Option<wgt::BufferAddress>,
}
#[cfg(send_sync)]
static_assertions::assert_impl_all!(BlasTriangleGeometry<'_>: WasmNotSendSync);

/// Contains the sets of geometry that go into a [Blas].
pub enum BlasGeometries<'a> {
/// Triangle geometry variant.
TriangleGeometries(Vec<BlasTriangleGeometry<'a>>),
}
#[cfg(send_sync)]
static_assertions::assert_impl_all!(BlasGeometries<'_>: WasmNotSendSync);

/// Builds the given sets of geometry into the given [Blas].
Expand All @@ -127,6 +129,7 @@ pub struct BlasBuildEntry<'a> {
/// Geometries.
pub geometry: BlasGeometries<'a>,
}
#[cfg(send_sync)]
static_assertions::assert_impl_all!(BlasBuildEntry<'_>: WasmNotSendSync);

#[derive(Debug, Clone)]
Expand All @@ -141,6 +144,7 @@ pub struct Blas {
pub(crate) handle: Option<u64>,
pub(crate) inner: dispatch::DispatchBlas,
}
#[cfg(send_sync)]
static_assertions::assert_impl_all!(Blas: WasmNotSendSync);

crate::cmp::impl_eq_ord_hash_proxy!(Blas => .inner);
Expand Down
14 changes: 12 additions & 2 deletions wgpu/src/api/surface_texture.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::{error, fmt};
use std::thread;

use crate::*;

Expand Down Expand Up @@ -42,7 +41,7 @@ impl SurfaceTexture {

impl Drop for SurfaceTexture {
fn drop(&mut self) {
if !self.presented && !thread::panicking() {
if !self.presented && !definitely_panicking() {
self.detail.texture_discard();
}
}
Expand Down Expand Up @@ -77,3 +76,14 @@ impl fmt::Display for SurfaceError {
}

impl error::Error for SurfaceError {}

/// Returns `true` if the current thread is unwinding and `false` if it is not or the `std` feature
/// is disabled.
fn definitely_panicking() -> bool {
#[cfg(feature = "std")]
if std::thread::panicking() {
return true;
}

false
}
3 changes: 3 additions & 0 deletions wgpu/src/api/tlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub(crate) struct TlasShared {
pub struct Tlas {
pub(crate) shared: Arc<TlasShared>,
}
#[cfg(send_sync)]
static_assertions::assert_impl_all!(Tlas: WasmNotSendSync);

crate::cmp::impl_eq_ord_hash_proxy!(Tlas => .shared.inner);
Expand All @@ -43,6 +44,7 @@ pub struct TlasBuildEntry<'a> {
/// Number of instances in the instance buffer.
pub instance_count: u32,
}
#[cfg(send_sync)]
static_assertions::assert_impl_all!(TlasBuildEntry<'_>: WasmNotSendSync);

/// The safe version of [`TlasBuildEntry`], containing [`TlasInstance`]s instead of a raw buffer.
Expand All @@ -51,6 +53,7 @@ pub struct TlasPackage {
pub(crate) instances: Vec<Option<TlasInstance>>,
pub(crate) lowest_unmodified: u32,
}
#[cfg(send_sync)]
static_assertions::assert_impl_all!(TlasPackage: WasmNotSendSync);

impl TlasPackage {
Expand Down
1 change: 1 addition & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#![cfg_attr(not(any(wgpu_core, webgpu)), allow(unused))]

extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
#[cfg(wgpu_core)]
pub extern crate wgpu_core as wgc;
Expand Down
6 changes: 4 additions & 2 deletions wgpu/src/util/init.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#[cfg(feature = "std")]
use crate::{Adapter, Instance, RequestAdapterOptions, Surface};

#[cfg(doc)]
use crate::Backends;

/// Initialize the adapter obeying the `WGPU_ADAPTER_NAME` environment variable.
#[cfg(native)]
#[cfg(all(feature = "std", native))]
pub fn initialize_adapter_from_env(
instance: &Instance,
compatible_surface: Option<&Surface<'_>>,
Expand Down Expand Up @@ -36,14 +37,15 @@ pub fn initialize_adapter_from_env(
}

/// Initialize the adapter obeying the `WGPU_ADAPTER_NAME` environment variable.
#[cfg(not(native))]
#[cfg(all(feature = "std", not(native)))]
pub fn initialize_adapter_from_env(
_instance: &Instance,
_compatible_surface: Option<&Surface<'_>>,
) -> Result<Adapter, wgt::RequestAdapterError> {
Err(wgt::RequestAdapterError::EnvNotSet)
}

#[cfg(feature = "std")]
/// Initialize the adapter obeying the `WGPU_ADAPTER_NAME` environment variable and if it doesn't exist fall back on a default adapter.
pub async fn initialize_adapter_from_env_or_default(
instance: &Instance,
Expand Down
2 changes: 2 additions & 0 deletions wgpu/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! Nothing in this module is a part of the WebGPU API specification;
//! they are unique to the `wgpu` library.

#[cfg(feature = "std")]
mod belt;
mod device;
mod encoder;
Expand All @@ -12,6 +13,7 @@ mod texture_blitter;
use alloc::{borrow::Cow, format, string::String, sync::Arc, vec};
use core::ptr::copy_nonoverlapping;

#[cfg(feature = "std")]
pub use belt::StagingBelt;
pub use device::{BufferInitDescriptor, DeviceExt};
pub use encoder::RenderEncoder;
Expand Down
Loading