Skip to content

Commit 0d569d5

Browse files
Optional web-specific deps for wasm32 (#7565)
1 parent f34dfd9 commit 0d569d5

File tree

16 files changed

+93
-27
lines changed

16 files changed

+93
-27
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ jobs:
269269
cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} --tests --features glsl,spirv
270270
cargo doc --target ${{ matrix.target }} ${{ matrix.extra-flags }} --no-deps --features glsl,spirv
271271
272-
# check with no features
273-
cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} --no-default-features
272+
# check with only the web feature
273+
cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} --no-default-features --features=web
274274
275275
# all features
276276
cargo clippy --target ${{ matrix.target }} ${{ matrix.extra-flags }} --tests --all-features
@@ -480,7 +480,7 @@ jobs:
480480
- name: Execute tests
481481
run: |
482482
cd wgpu
483-
wasm-pack test --headless --chrome --no-default-features --features wgsl,webgl --workspace
483+
wasm-pack test --headless --chrome --no-default-features --features wgsl,webgl,web --workspace
484484
485485
gpu-test:
486486
# runtime is normally 5-15 minutes

examples/standalone/custom_backend/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ edition = "2021"
44
rust-version = "1.84"
55
publish = false
66

7+
[features]
8+
default = ["web"]
9+
web = ["wgpu/web"]
10+
711
[dependencies]
812
wgpu = { version = "25.0.0", features = [
913
"custom",

examples/standalone/custom_backend/src/custom.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ impl QueueInterface for CustomQueue {
337337
unimplemented!()
338338
}
339339

340-
#[cfg(target_arch = "wasm32")]
340+
#[cfg(all(target_arch = "wasm32", feature = "web"))]
341341
fn copy_external_image_to_texture(
342342
&self,
343343
_source: &wgpu::CopyExternalImageSourceInfo,

tests/tests/wgpu-dependency/main.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,54 @@ fn wasm32_with_webgl_depends_on_glow() {
139139
});
140140
}
141141

142+
#[test]
143+
fn wasm32_with_only_custom_backend_does_not_depend_on_web_specifics() {
144+
check_feature_dependency(Requirement {
145+
human_readable_name: "wasm32 with only the `custom` backend does not depend on web-specific bindings [`wasm-bindgen`, `js-sys`, `web-sys`]",
146+
target: "wasm32-unknown-unknown",
147+
packages: &["wgpu"],
148+
features: &["custom"],
149+
default_features: false,
150+
search_terms: &[Search::Negative("wasm-bindgen"), Search::Negative("js-sys"), Search::Negative("web-sys")],
151+
});
152+
}
153+
154+
#[test]
155+
fn wasm32_with_webgpu_backend_does_depend_on_web_specifics() {
156+
check_feature_dependency(Requirement {
157+
human_readable_name: "wasm32 with the `webgpu` backend depends on web-specific bindings [`wasm-bindgen`, `js-sys`, `web-sys`]",
158+
target: "wasm32-unknown-unknown",
159+
packages: &["wgpu"],
160+
features: &["webgpu"],
161+
default_features: false,
162+
search_terms: &[Search::Positive("wasm-bindgen"), Search::Positive("js-sys"), Search::Positive("web-sys")],
163+
});
164+
}
165+
166+
#[test]
167+
fn wasm32_with_webgl_backend_does_depend_on_web_specifics() {
168+
check_feature_dependency(Requirement {
169+
human_readable_name: "wasm32 with the `webgl` backend depends on web-specific bindings [`wasm-bindgen`, `js-sys`, `web-sys`]",
170+
target: "wasm32-unknown-unknown",
171+
packages: &["wgpu"],
172+
features: &["webgl"],
173+
default_features: false,
174+
search_terms: &[Search::Positive("wasm-bindgen"), Search::Positive("js-sys"), Search::Positive("web-sys")],
175+
});
176+
}
177+
178+
#[test]
179+
fn windows_with_webgpu_webgl_backend_does_not_depend_on_web_specifics() {
180+
check_feature_dependency(Requirement {
181+
human_readable_name: "windows with the `webgpu` and `webgl` backends enabled does not depend on web-specific bindings [`wasm-bindgen`, `js-sys`, `web-sys`]",
182+
target: "x86_64-pc-windows-msvc",
183+
packages: &["wgpu"],
184+
features: &["webgpu", "webgl"],
185+
default_features: false,
186+
search_terms: &[Search::Negative("wasm-bindgen"), Search::Negative("js-sys"), Search::Negative("web-sys")],
187+
});
188+
}
189+
142190
#[test]
143191
fn windows_with_webgl_does_not_depend_on_glow() {
144192
check_feature_dependency(Requirement {

wgpu-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ gles = [
147147
]
148148

149149
## WebGL backend, only available on Emscripten
150-
webgl = ["wgpu-core-deps-wasm/webgl"]
150+
webgl = ["wgpu-core-deps-wasm/webgl", "wgpu-types/web"]
151151
## OpenGL backend, on macOS only
152152
angle = ["wgpu-core-deps-apple/angle"]
153153
## Vulkan portability backend, only available on macOS

wgpu-hal/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ gles = [
118118
"dep:profiling",
119119
"dep:wasm-bindgen",
120120
"dep:web-sys",
121+
"wgpu-types/web",
121122
"windows/Win32_Graphics_OpenGL",
122123
"windows/Win32_Graphics_Gdi",
123124
"windows/Win32_System_LibraryLoader",

wgpu-types/Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ alloc_instead_of_core = "warn"
3737

3838
[features]
3939
default = ["std"]
40-
std = ["js-sys/std", "web-sys/std", "thiserror/std"]
40+
std = ["js-sys?/std", "web-sys?/std", "thiserror/std"]
4141
strict_asserts = []
4242
fragile-send-sync-non-atomic-wasm = []
4343
serde = ["dep:serde", "bitflags/serde"]
4444
# Enables some internal instrumentation for debugging purposes.
4545
counters = []
4646
# Enables variants of `Trace` other than `Trace::Off`
4747
trace = ["std"]
48+
# Enable web-specific dependencies for wasm.
49+
web = ["dep:js-sys", "dep:web-sys"]
4850

4951
[dependencies]
5052
bitflags = { workspace = true, features = ["serde"] }
@@ -57,8 +59,8 @@ serde = { workspace = true, default-features = false, features = [
5759
], optional = true }
5860

5961
[target.'cfg(target_arch = "wasm32")'.dependencies]
60-
js-sys = { workspace = true, default-features = false }
61-
web-sys = { workspace = true, default-features = false, features = [
62+
js-sys = { workspace = true, optional = true, default-features = false }
63+
web-sys = { workspace = true, optional = true, default-features = false, features = [
6264
"ImageBitmap",
6365
"ImageData",
6466
"HtmlImageElement",

wgpu-types/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6894,7 +6894,7 @@ pub type ImageCopyTexture<T> = TexelCopyTextureInfo<T>;
68946894
///
68956895
/// Corresponds to [WebGPU `GPUCopyExternalImageSourceInfo`](
68966896
/// https://gpuweb.github.io/gpuweb/#dictdef-gpuimagecopyexternalimage).
6897-
#[cfg(target_arch = "wasm32")]
6897+
#[cfg(all(target_arch = "wasm32", feature = "web"))]
68986898
#[derive(Clone, Debug)]
68996899
pub struct CopyExternalImageSourceInfo {
69006900
/// The texture to be copied from. The copy source data is captured at the moment
@@ -6918,14 +6918,14 @@ pub struct CopyExternalImageSourceInfo {
69186918
since = "24.0.0",
69196919
note = "This has been renamed to `CopyExternalImageSourceInfo`, and will be removed in 25.0.0."
69206920
)]
6921-
#[cfg(target_arch = "wasm32")]
6921+
#[cfg(all(target_arch = "wasm32", feature = "web"))]
69226922
pub type ImageCopyExternalImage = CopyExternalImageSourceInfo;
69236923

69246924
/// Source of an external texture copy.
69256925
///
69266926
/// Corresponds to the [implicit union type on WebGPU `GPUCopyExternalImageSourceInfo.source`](
69276927
/// https://gpuweb.github.io/gpuweb/#dom-gpuimagecopyexternalimage-source).
6928-
#[cfg(target_arch = "wasm32")]
6928+
#[cfg(all(target_arch = "wasm32", feature = "web"))]
69296929
#[derive(Clone, Debug)]
69306930
pub enum ExternalImageSource {
69316931
/// Copy from a previously-decoded image bitmap.
@@ -6947,7 +6947,7 @@ pub enum ExternalImageSource {
69476947
VideoFrame(web_sys::VideoFrame),
69486948
}
69496949

6950-
#[cfg(target_arch = "wasm32")]
6950+
#[cfg(all(target_arch = "wasm32", feature = "web"))]
69516951
impl ExternalImageSource {
69526952
/// Gets the pixel, not css, width of the source.
69536953
pub fn width(&self) -> u32 {
@@ -6978,7 +6978,7 @@ impl ExternalImageSource {
69786978
}
69796979
}
69806980

6981-
#[cfg(target_arch = "wasm32")]
6981+
#[cfg(all(target_arch = "wasm32", feature = "web"))]
69826982
impl core::ops::Deref for ExternalImageSource {
69836983
type Target = js_sys::Object;
69846984

@@ -6998,12 +6998,14 @@ impl core::ops::Deref for ExternalImageSource {
69986998

69996999
#[cfg(all(
70007000
target_arch = "wasm32",
7001+
feature = "web",
70017002
feature = "fragile-send-sync-non-atomic-wasm",
70027003
not(target_feature = "atomics")
70037004
))]
70047005
unsafe impl Send for ExternalImageSource {}
70057006
#[cfg(all(
70067007
target_arch = "wasm32",
7008+
feature = "web",
70077009
feature = "fragile-send-sync-non-atomic-wasm",
70087010
not(target_feature = "atomics")
70097011
))]

wgpu/Cargo.toml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ gles = ["wgpu-core?/gles"]
4848

4949
## Enables the WebGPU backend on WebAssembly.
5050
webgpu = [
51+
"web",
5152
"naga?/wgsl-out",
5253
"dep:wasm-bindgen-futures",
5354
"web-sys/Document",
@@ -68,7 +69,7 @@ angle = ["wgpu-core?/angle"]
6869
vulkan-portability = ["wgpu-core?/vulkan-portability"]
6970

7071
## Enables the GLES backend on WebAssembly only.
71-
webgl = ["wgpu-core/webgl", "dep:wgpu-hal", "dep:smallvec"]
72+
webgl = ["web", "wgpu-core/webgl", "dep:wgpu-hal", "dep:smallvec"]
7273

7374
## Enables the noop backend for testing.
7475
##
@@ -148,6 +149,13 @@ fragile-send-sync-non-atomic-wasm = [
148149
"wgpu-types/fragile-send-sync-non-atomic-wasm",
149150
]
150151

152+
## Use web-specific libraries on WASM
153+
##
154+
## Those libraties (wasm-bindgen, web-sys, js-sys) can only be used when there is a JavaScript
155+
## context around the WASM VM, e.g., when the WASM binary is used in a browser.
156+
web = ["dep:wasm-bindgen", "dep:js-sys", "dep:web-sys", "wgpu-types/web"]
157+
158+
151159
#########################
152160
# Standard Dependencies #
153161
#########################
@@ -192,9 +200,9 @@ smallvec.workspace = true
192200
###############
193201
[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
194202
# Needed for all backends
195-
js-sys = { workspace = true, features = ["default"] }
196-
wasm-bindgen.workspace = true
197-
web-sys = { workspace = true, features = [
203+
js-sys = { workspace = true, optional = true, features = ["default"] }
204+
wasm-bindgen = { workspace = true, optional = true }
205+
web-sys = { workspace = true, optional = true, features = [
198206
"HtmlCanvasElement",
199207
"OffscreenCanvas",
200208
] }

wgpu/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ fn main() {
22
cfg_aliases::cfg_aliases! {
33
native: { not(target_arch = "wasm32") },
44
Emscripten: { all(target_arch = "wasm32", target_os = "emscripten") },
5+
web: { all(target_arch = "wasm32", not(Emscripten), feature = "web") },
56

67
send_sync: { any(
78
native,

0 commit comments

Comments
 (0)