Skip to content

Commit 727c216

Browse files
committed
Optional web-specific deps for wasm32
1 parent c7330a8 commit 727c216

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

wgpu-core/Cargo.toml

+1-1
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-types/Cargo.toml

+5-3
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

+7-5
Original file line numberDiff line numberDiff line change
@@ -6838,7 +6838,7 @@ pub type ImageCopyTexture<T> = TexelCopyTextureInfo<T>;
68386838
///
68396839
/// Corresponds to [WebGPU `GPUCopyExternalImageSourceInfo`](
68406840
/// https://gpuweb.github.io/gpuweb/#dictdef-gpuimagecopyexternalimage).
6841-
#[cfg(target_arch = "wasm32")]
6841+
#[cfg(all(target_arch = "wasm32", feature = "web"))]
68426842
#[derive(Clone, Debug)]
68436843
pub struct CopyExternalImageSourceInfo {
68446844
/// The texture to be copied from. The copy source data is captured at the moment
@@ -6862,14 +6862,14 @@ pub struct CopyExternalImageSourceInfo {
68626862
since = "24.0.0",
68636863
note = "This has been renamed to `CopyExternalImageSourceInfo`, and will be removed in 25.0.0."
68646864
)]
6865-
#[cfg(target_arch = "wasm32")]
6865+
#[cfg(all(target_arch = "wasm32", feature = "web"))]
68666866
pub type ImageCopyExternalImage = CopyExternalImageSourceInfo;
68676867

68686868
/// Source of an external texture copy.
68696869
///
68706870
/// Corresponds to the [implicit union type on WebGPU `GPUCopyExternalImageSourceInfo.source`](
68716871
/// https://gpuweb.github.io/gpuweb/#dom-gpuimagecopyexternalimage-source).
6872-
#[cfg(target_arch = "wasm32")]
6872+
#[cfg(all(target_arch = "wasm32", feature = "web"))]
68736873
#[derive(Clone, Debug)]
68746874
pub enum ExternalImageSource {
68756875
/// Copy from a previously-decoded image bitmap.
@@ -6891,7 +6891,7 @@ pub enum ExternalImageSource {
68916891
VideoFrame(web_sys::VideoFrame),
68926892
}
68936893

6894-
#[cfg(target_arch = "wasm32")]
6894+
#[cfg(all(target_arch = "wasm32", feature = "web"))]
68956895
impl ExternalImageSource {
68966896
/// Gets the pixel, not css, width of the source.
68976897
pub fn width(&self) -> u32 {
@@ -6922,7 +6922,7 @@ impl ExternalImageSource {
69226922
}
69236923
}
69246924

6925-
#[cfg(target_arch = "wasm32")]
6925+
#[cfg(all(target_arch = "wasm32", feature = "web"))]
69266926
impl core::ops::Deref for ExternalImageSource {
69276927
type Target = js_sys::Object;
69286928

@@ -6942,12 +6942,14 @@ impl core::ops::Deref for ExternalImageSource {
69426942

69436943
#[cfg(all(
69446944
target_arch = "wasm32",
6945+
feature = "web",
69456946
feature = "fragile-send-sync-non-atomic-wasm",
69466947
not(target_feature = "atomics")
69476948
))]
69486949
unsafe impl Send for ExternalImageSource {}
69496950
#[cfg(all(
69506951
target_arch = "wasm32",
6952+
feature = "web",
69516953
feature = "fragile-send-sync-non-atomic-wasm",
69526954
not(target_feature = "atomics")
69536955
))]

wgpu/Cargo.toml

+15-4
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,17 @@ gles = ["wgpu-core?/gles"]
5050
webgpu = [
5151
"naga?/wgsl-out",
5252
"dep:wasm-bindgen-futures",
53+
"dep:wasm-bindgen",
54+
"dep:js-sys",
55+
"dep:web-sys",
5356
"web-sys/Document",
5457
"web-sys/Event",
5558
"web-sys/Navigator",
5659
"web-sys/NodeList",
5760
"web-sys/Window",
5861
"web-sys/WorkerGlobalScope",
5962
"web-sys/WorkerNavigator",
63+
"wgpu-types/web",
6064
]
6165

6266
#! ### Conditional Backends
@@ -68,7 +72,14 @@ angle = ["wgpu-core?/angle"]
6872
vulkan-portability = ["wgpu-core?/vulkan-portability"]
6973

7074
## Enables the GLES backend on WebAssembly only.
71-
webgl = ["wgpu-core/webgl", "dep:wgpu-hal", "dep:smallvec"]
75+
webgl = [
76+
"wgpu-core/webgl",
77+
"dep:wgpu-hal",
78+
"dep:smallvec",
79+
"dep:wasm-bindgen",
80+
"dep:js-sys",
81+
"dep:web-sys",
82+
]
7283

7384
## Enables the noop backend for testing.
7485
##
@@ -192,9 +203,9 @@ smallvec.workspace = true
192203
###############
193204
[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
194205
# Needed for all backends
195-
js-sys = { workspace = true, features = ["default"] }
196-
wasm-bindgen.workspace = true
197-
web-sys = { workspace = true, features = [
206+
js-sys = { workspace = true, optional = true, features = ["default"] }
207+
wasm-bindgen = { workspace = true, optional = true }
208+
web-sys = { workspace = true, optional = true, features = [
198209
"HtmlCanvasElement",
199210
"OffscreenCanvas",
200211
] }

0 commit comments

Comments
 (0)