Skip to content
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

Add no_std support to wgpu-types #6892

Merged
merged 19 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from 15 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
30 changes: 30 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ jobs:
os: ubuntu-22.04
target: wasm32-unknown-emscripten
kind: wgpu-only

# TODO: Uncomment once web-sys updates past 0.3.76
# See https://github.com/rustwasm/wasm-bindgen/pull/4378 for details
# - name: WebAssembly Core 1.0
# os: ubuntu-22.04
# target: wasm32v1-none
# kind: no-std
# toolchain: nightly

# Bare-metal x86-64
# TODO: Remove once web-sys updates past 0.3.76
# Included while wasm32v1-none is failing to ensure `no_std` does not regress
- name: Bare-metal x86_64
cwfitzgerald marked this conversation as resolved.
Show resolved Hide resolved
os: ubuntu-22.04
target: x86_64-unknown-none
kind: no-std
toolchain: nightly
cwfitzgerald marked this conversation as resolved.
Show resolved Hide resolved

name: Clippy ${{ matrix.name }}
runs-on: ${{ matrix.os }}
Expand Down Expand Up @@ -246,6 +263,19 @@ jobs:
# Check with all features.
cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p wgpu-hal --all-features
cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p wgpu --all-features

# Building for no_std platforms where every feature is enabled except "std".
- name: check no_std
if: matrix.kind == 'no_std'
cwfitzgerald marked this conversation as resolved.
Show resolved Hide resolved
shell: bash
run: |
set -e

# check with no features
cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} -p wgpu-types --no-default-features

# Check with all features except "std".
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

# Building for native platforms with standard tests.
- name: check native
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ By @wumpf in [#6849](https://github.com/gfx-rs/wgpu/pull/6849).
- `DeviceType` and `AdapterInfo` now impl `Hash` by @cwfitzgerald in [#6868](https://github.com/gfx-rs/wgpu/pull/6868)
- Add build support for Apple Vision Pro. By @guusw in [#6611](https://github.com/gfx-rs/wgpu/pull/6611).
- Add `wgsl_language_features` for obtaining available WGSL language feature by @sagudev in [#6814](https://github.com/gfx-rs/wgpu/pull/6814)
- Add `no_std` support to `wgpu-types`. By @bushrat011899 in [#6892](https://github.com/gfx-rs/wgpu/pull/6892).

##### Vulkan

Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ ron = "0.8"
# rustc-hash 2.0 is a completely different hasher with different performance characteristics
serde_json = "1.0.134"
rustc-hash = "1"
serde = "1"
serde = { version = "1", default-features = false }
smallvec = "1"
static_assertions = "1.1.0"
strum = { version = "0.26.0", features = ["derive"] }
Expand Down Expand Up @@ -173,11 +173,11 @@ windows = { version = "0.58", default-features = false }
# wasm32 dependencies
console_error_panic_hook = "0.1.7"
console_log = "1"
js-sys = "0.3.70"
js-sys = { version = "0.3.70", default-features = false }
wasm-bindgen = "0.2.97"
wasm-bindgen-futures = "0.4.45"
wasm-bindgen-test = "0.3"
web-sys = "0.3.74"
web-sys = { version = "0.3.74", default-features = false }
web-time = "0.2.4"

# deno dependencies
Expand Down
2 changes: 1 addition & 1 deletion lock-analyzer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ anyhow.workspace = true

[dependencies.serde]
workspace = true
features = ["serde_derive"]
features = ["default", "serde_derive"]
7 changes: 5 additions & 2 deletions naga/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ log = "0.4"
strum.workspace = true
spirv = { version = "0.3", optional = true }
thiserror.workspace = true
serde = { version = "1.0.217", features = ["derive"], optional = true }
serde = { version = "1.0.217", features = [
"default",
"derive",
], optional = true }
# Hold on updating to 0.7 until https://github.com/petgraph/petgraph/pull/714 is on crates.io
petgraph = { version = "0.6", optional = true }
pp-rs = { version = "0.2.1", optional = true }
Expand All @@ -101,5 +104,5 @@ hlsl-snapshots = { path = "./hlsl-snapshots" }
# incompatible with our tests because we do a syntactic diff and not a semantic one.
ron = "0.8.0"
rspirv = { version = "0.11", git = "https://github.com/gfx-rs/rspirv", rev = "b969f175d5663258b4891e44b76c1544da9661ab" }
serde = { workspace = true, features = ["derive"] }
serde = { workspace = true, features = ["default", "derive"] }
spirv = { version = "0.3", features = ["deserialize"] }
2 changes: 1 addition & 1 deletion wgpu-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ profiling = { workspace = true, default-features = false }
raw-window-handle = { workspace = true, optional = true }
ron = { workspace = true, optional = true }
rustc-hash.workspace = true
serde = { workspace = true, features = ["derive"], optional = true }
serde = { workspace = true, features = ["default", "derive"], optional = true }
smallvec.workspace = true
thiserror.workspace = true

Expand Down
3 changes: 2 additions & 1 deletion wgpu-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,13 @@ core-graphics-types.workspace = true
[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
wasm-bindgen.workspace = true
web-sys = { workspace = true, features = [
"default",
"Window",
"HtmlCanvasElement",
"WebGl2RenderingContext",
"OffscreenCanvas",
] }
js-sys.workspace = true
js-sys = { workspace = true, features = ["default"] }

[target.'cfg(unix)'.dependencies]
libc.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions wgpu-hal/examples/ray-traced-triangle/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ impl<A: hal::Api> Example<A> {
name: "example",
flags: wgt::InstanceFlags::default(),
dx12_shader_compiler: wgt::Dx12Compiler::DynamicDxc {
dxc_path: std::path::PathBuf::from("dxcompiler.dll"),
dxil_path: std::path::PathBuf::from("dxil.dll"),
dxc_path: "dxcompiler.dll".to_string(),
dxil_path: "dxil.dll".to_string(),
},
gles_minor_version: wgt::Gles3MinorVersion::default(),
};
Expand Down
15 changes: 7 additions & 8 deletions wgpu-hal/src/dx12/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,13 @@ impl crate::Instance for super::Instance {
dxil_path,
dxc_path,
} => {
let container =
super::shader_compilation::get_dynamic_dxc_container(dxc_path, dxil_path)
.map_err(|e| {
crate::InstanceError::with_source(
String::from("Failed to load dynamic DXC"),
e,
)
})?;
let container = super::shader_compilation::get_dynamic_dxc_container(
dxc_path.into(),
dxil_path.into(),
)
.map_err(|e| {
crate::InstanceError::with_source(String::from("Failed to load dynamic DXC"), e)
})?;

Some(Arc::new(container))
}
Expand Down
2 changes: 1 addition & 1 deletion wgpu-info/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ anyhow.workspace = true
bitflags.workspace = true
env_logger.workspace = true
pico-args.workspace = true
serde.workspace = true
serde = { workspace = true, features = ["default"] }
serde_json.workspace = true
wgpu = { workspace = true, features = ["serde", "dx12", "metal", "static-dxc"] }
16 changes: 13 additions & 3 deletions wgpu-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ targets = [
[lib]

[features]
default = ["std"]
std = ["js-sys/std", "web-sys/std"]
strict_asserts = []
fragile-send-sync-non-atomic-wasm = []
serde = ["dep:serde"]
Expand All @@ -39,11 +41,14 @@ unexpected_cfgs = { level = "warn", check-cfg = ['cfg(web_sys_unstable_apis)'] }

[dependencies]
bitflags = { workspace = true, features = ["serde"] }
serde = { workspace = true, features = ["derive"], optional = true }
serde = { workspace = true, default-features = false, features = [
"alloc",
"derive",
], optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys.workspace = true
web-sys = { workspace = true, features = [
js-sys = { workspace = true, default-features = false }
web-sys = { workspace = true, default-features = false, features = [
"ImageBitmap",
"ImageData",
"HtmlImageElement",
Expand All @@ -56,3 +61,8 @@ web-sys = { workspace = true, features = [
[dev-dependencies]
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true

[lints.clippy]
std_instead_of_core = "warn"
std_instead_of_alloc = "warn"
alloc_instead_of_core = "warn"
12 changes: 6 additions & 6 deletions wgpu-types/src/assertions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! `wgpu-core`'s `"strict_asserts"` feature enables that validation
//! in both debug and release builds.

/// This is equivalent to [`std::assert`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert`].
/// This is equivalent to [`core::assert`] if the `strict_asserts` feature is activated, otherwise equal to [`core::debug_assert`].
#[cfg(feature = "strict_asserts")]
#[macro_export]
macro_rules! strict_assert {
Expand All @@ -20,7 +20,7 @@ macro_rules! strict_assert {
}
}

/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert_eq`].
/// This is equivalent to [`core::assert_eq`] if the `strict_asserts` feature is activated, otherwise equal to [`core::debug_assert_eq`].
#[cfg(feature = "strict_asserts")]
#[macro_export]
macro_rules! strict_assert_eq {
Expand All @@ -29,7 +29,7 @@ macro_rules! strict_assert_eq {
}
}

/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert_ne`].
/// This is equivalent to [`core::assert_ne`] if the `strict_asserts` feature is activated, otherwise equal to [`core::debug_assert_ne`].
#[cfg(feature = "strict_asserts")]
#[macro_export]
macro_rules! strict_assert_ne {
Expand All @@ -38,7 +38,7 @@ macro_rules! strict_assert_ne {
}
}

/// This is equivalent to [`std::assert`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert`]
/// This is equivalent to [`core::assert`] if the `strict_asserts` feature is activated, otherwise equal to [`core::debug_assert`]
#[cfg(not(feature = "strict_asserts"))]
#[macro_export]
macro_rules! strict_assert {
Expand All @@ -47,7 +47,7 @@ macro_rules! strict_assert {
};
}

/// This is equivalent to [`std::assert_eq`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert_eq`]
/// This is equivalent to [`core::assert_eq`] if the `strict_asserts` feature is activated, otherwise equal to [`core::debug_assert_eq`]
#[cfg(not(feature = "strict_asserts"))]
#[macro_export]
macro_rules! strict_assert_eq {
Expand All @@ -56,7 +56,7 @@ macro_rules! strict_assert_eq {
};
}

/// This is equivalent to [`std::assert_ne`] if the `strict_asserts` feature is activated, otherwise equal to [`std::debug_assert_ne`]
/// This is equivalent to [`core::assert_ne`] if the `strict_asserts` feature is activated, otherwise equal to [`core::debug_assert_ne`]
#[cfg(not(feature = "strict_asserts"))]
#[macro_export]
macro_rules! strict_assert_ne {
Expand Down
13 changes: 7 additions & 6 deletions wgpu-types/src/counters.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::{string::String, vec::Vec};
#[cfg(feature = "counters")]
use std::sync::atomic::{AtomicIsize, Ordering};
use std::{fmt, ops::Range};
use core::sync::atomic::{AtomicIsize, Ordering};
use core::{fmt, ops::Range};

/// An internal counter for debugging purposes
///
Expand Down Expand Up @@ -95,8 +96,8 @@ impl Default for InternalCounter {
}
}

impl std::fmt::Debug for InternalCounter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl core::fmt::Debug for InternalCounter {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.read().fmt(f)
}
}
Expand Down Expand Up @@ -192,15 +193,15 @@ impl fmt::Debug for AllocationReport {
impl fmt::Debug for AllocatorReport {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut allocations = self.allocations.clone();
allocations.sort_by_key(|alloc| std::cmp::Reverse(alloc.size));
allocations.sort_by_key(|alloc| core::cmp::Reverse(alloc.size));

let max_num_allocations_to_print = f.precision().unwrap_or(usize::MAX);
allocations.truncate(max_num_allocations_to_print);

f.debug_struct("AllocatorReport")
.field(
"summary",
&std::format_args!(
&core::format_args!(
"{} / {}",
FmtBytes(self.total_allocated_bytes),
FmtBytes(self.total_reserved_bytes)
Expand Down
Loading
Loading