From a9279de79328d28773742afa12294391970eeac0 Mon Sep 17 00:00:00 2001 From: Connor Fitzgerald Date: Mon, 31 Mar 2025 22:02:21 -0400 Subject: [PATCH] Fix assorted issues with WebGL (#7448) * Fix validation error when configuring the surface on WebGL * Remove unneeded `webgl` feature * Fix compilation of the `noop` backend on `wasm32` * Prevent `webgpu` examples from incorrectly falling back to WebGL * Reduce dependency set when building wasm examples * Fix various warnings --- Cargo.toml | 1 - examples/features/Cargo.toml | 3 ++- wgpu-core/src/conv.rs | 5 +---- wgpu-core/src/device/global.rs | 5 +++++ wgpu/Cargo.toml | 2 +- wgpu/src/api/queue.rs | 9 +-------- wgpu/src/backend/wgpu_core.rs | 9 +++++---- xtask/src/run_wasm.rs | 4 ++-- 8 files changed, 17 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 559e281c0..64877fc6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,7 +68,6 @@ wgpu = { version = "24.0.0", path = "./wgpu", default-features = false, features "dx12", "metal", "static-dxc", - "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" } diff --git a/examples/features/Cargo.toml b/examples/features/Cargo.toml index ec79438af..2df3ca318 100644 --- a/examples/features/Cargo.toml +++ b/examples/features/Cargo.toml @@ -44,7 +44,6 @@ obj.workspace = true png.workspace = true pollster.workspace = true web-time.workspace = true -wgpu.workspace = true wgpu-types = { workspace = true, features = [ "trace", # TODO(#5974): this should be a dep on wgpu/trace and not wgpu-types at all ] } @@ -55,6 +54,7 @@ wgpu-test.workspace = true [target.'cfg(not(target_arch = "wasm32"))'.dependencies] env_logger.workspace = true +wgpu.workspace = true [target.'cfg(target_arch = "wasm32")'.dependencies] console_error_panic_hook.workspace = true @@ -62,6 +62,7 @@ console_log.workspace = true fern.workspace = true wasm-bindgen.workspace = true wasm-bindgen-futures.workspace = true +wgpu = { path = "../../wgpu", default-features = false, features = ["wgsl"] } # We need these features in the framework examples and tests web-sys = { workspace = true, features = [ "Location", diff --git a/wgpu-core/src/conv.rs b/wgpu-core/src/conv.rs index 5216ad0f6..5a5a27279 100644 --- a/wgpu-core/src/conv.rs +++ b/wgpu-core/src/conv.rs @@ -27,10 +27,7 @@ pub fn is_valid_copy_dst_texture_format( } } -#[cfg_attr( - any(not(target_arch = "wasm32"), target_os = "emscripten"), - allow(unused) -)] +#[cfg_attr(any(not(webgl)), expect(unused))] pub fn is_valid_external_image_copy_dst_texture_format(format: wgt::TextureFormat) -> bool { use wgt::TextureFormat as Tf; match format { diff --git a/wgpu-core/src/device/global.rs b/wgpu-core/src/device/global.rs index af928dfb0..d7c83d0b3 100644 --- a/wgpu-core/src/device/global.rs +++ b/wgpu-core/src/device/global.rs @@ -1880,6 +1880,11 @@ impl Global { Ok(wgt::PollStatus::Poll) => { unreachable!("Cannot get a Poll result from a Wait action.") } + Err(WaitIdleError::Timeout) if cfg!(target_arch = "wasm32") => { + // On wasm, you cannot actually successfully wait for the surface. + // However WebGL does not actually require you do this, so ignoring + // the failure is totally fine. See https://github.com/gfx-rs/wgpu/issues/7363 + } Err(e) => { break 'error e.into(); } diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index d9f0783e5..17c95e971 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -76,7 +76,7 @@ webgl = ["wgpu-core/webgl", "dep:wgpu-hal", "dep:smallvec"] ## but performs no computation. ## Because it lacks basic functionality, it is only actually used if explicitly enabled ## through `NoopBackendOptions`. -noop = ["wgpu-core/noop"] +noop = ["wgpu-core/noop", "dep:wgpu-hal", "dep:smallvec"] #! **Note:** In the documentation, if you see that an item depends on a backend, #! it means that the item is only available when that backend is enabled _and_ the backend diff --git a/wgpu/src/api/queue.rs b/wgpu/src/api/queue.rs index 7a03f8c0b..bf7ec4741 100644 --- a/wgpu/src/api/queue.rs +++ b/wgpu/src/api/queue.rs @@ -27,14 +27,7 @@ crate::cmp::impl_eq_ord_hash_proxy!(Queue => .inner); /// There is no analogue in the WebGPU specification. #[derive(Debug, Clone)] pub struct SubmissionIndex { - #[cfg_attr( - all( - target_arch = "wasm32", - not(target_os = "emscripten"), - not(feature = "webgl"), - ), - expect(dead_code) - )] + #[cfg_attr(not(wgpu_core), expect(dead_code))] pub(crate) index: u64, } #[cfg(send_sync)] diff --git a/wgpu/src/backend/wgpu_core.rs b/wgpu/src/backend/wgpu_core.rs index 926d7ddc2..2b9030ce3 100644 --- a/wgpu/src/backend/wgpu_core.rs +++ b/wgpu/src/backend/wgpu_core.rs @@ -399,10 +399,7 @@ fn map_texture_copy_view( } } -#[cfg_attr( - any(not(target_arch = "wasm32"), target_os = "emscripten"), - expect(unused) -)] +#[cfg_attr(not(webgl), expect(unused))] fn map_texture_tagged_copy_view( view: crate::CopyExternalImageDestInfo<&api::Texture>, ) -> wgc::command::CopyExternalImageDestInfo { @@ -1815,13 +1812,17 @@ impl dispatch::QueueInterface for CoreQueue { } } + // This method needs to exist if either webgpu or webgl is enabled, + // but we only actually have an implementation if webgl is enabled. #[cfg(any(webgpu, webgl))] + #[cfg_attr(not(webgl), expect(unused_variables))] fn copy_external_image_to_texture( &self, source: &crate::CopyExternalImageSourceInfo, dest: crate::CopyExternalImageDestInfo<&crate::api::Texture>, size: crate::Extent3d, ) { + #[cfg(webgl)] match self.context.0.queue_copy_external_image_to_texture( self.id, source, diff --git a/xtask/src/run_wasm.rs b/xtask/src/run_wasm.rs index d8048b3ca..eace62f2e 100644 --- a/xtask/src/run_wasm.rs +++ b/xtask/src/run_wasm.rs @@ -32,7 +32,7 @@ pub(crate) fn run_wasm(shell: Shell, mut args: Arguments) -> anyhow::Result<()> xshell::cmd!( shell, - "cargo build --target wasm32-unknown-unknown --bin wgpu-examples --no-default-features --features webgpu {release_flag...}" + "cargo build --target wasm32-unknown-unknown -p wgpu-examples --no-default-features --features webgpu {release_flag...}" ) .args(&cargo_args) .quiet() @@ -53,7 +53,7 @@ pub(crate) fn run_wasm(shell: Shell, mut args: Arguments) -> anyhow::Result<()> xshell::cmd!( shell, - "cargo build --target wasm32-unknown-unknown --bin wgpu-examples --no-default-features --features webgl {release_flag...}" + "cargo build --target wasm32-unknown-unknown -p wgpu-examples --no-default-features --features webgl {release_flag...}" ) .args(&cargo_args) .quiet()