diff --git a/Cargo.lock b/Cargo.lock index 41d277851d..f7fa5f88b1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4018,6 +4018,9 @@ dependencies = [ "wasm-bindgen-futures", "web-sys", "wgpu-core", + "wgpu-core-feature-conditional-arch-wasm32", + "wgpu-core-feature-conditional-native-non-apple", + "wgpu-core-feature-conditional-vendor-apple", "wgpu-hal", "wgpu-types", ] @@ -4065,6 +4068,27 @@ dependencies = [ "wgpu-types", ] +[[package]] +name = "wgpu-core-feature-conditional-arch-wasm32" +version = "24.0.0" +dependencies = [ + "wgpu-core", +] + +[[package]] +name = "wgpu-core-feature-conditional-native-non-apple" +version = "24.0.0" +dependencies = [ + "wgpu-core", +] + +[[package]] +name = "wgpu-core-feature-conditional-vendor-apple" +version = "24.0.0" +dependencies = [ + "wgpu-core", +] + [[package]] name = "wgpu-examples" version = "24.0.0" diff --git a/Cargo.toml b/Cargo.toml index fc5c0e0b85..44e20e6076 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,9 @@ members = [ "wgpu-macros", "wgpu-types", "wgpu", + "wgpu-core-target-feature-conditionals/arch-wasm32", + "wgpu-core-target-feature-conditionals/native-non-apple", + "wgpu-core-target-feature-conditionals/vendor-apple", ] exclude = [] default-members = [ @@ -38,6 +41,9 @@ default-members = [ "wgpu-macros", "wgpu-types", "wgpu", + "wgpu-core-target-feature-conditionals/arch-wasm32", + "wgpu-core-target-feature-conditionals/native-non-apple", + "wgpu-core-target-feature-conditionals/vendor-apple", ] [workspace.lints.clippy] @@ -73,6 +79,18 @@ version = "24.0.0" path = "./naga" version = "24.0.0" +[workspace.dependencies.wgpu-core-feature-conditional-arch-wasm32] +path = "./wgpu-core-target-feature-conditionals/arch-wasm32" +version = "24.0.0" + +[workspace.dependencies.wgpu-core-feature-conditional-native-non-apple] +path = "./wgpu-core-target-feature-conditionals/native-non-apple" +version = "24.0.0" + +[workspace.dependencies.wgpu-core-feature-conditional-vendor-apple] +path = "./wgpu-core-target-feature-conditionals/vendor-apple" +version = "24.0.0" + [workspace.dependencies] anyhow = "1.0.95" argh = "0.1.13" diff --git a/wgpu-core-target-feature-conditionals/arch-wasm32/Cargo.toml b/wgpu-core-target-feature-conditionals/arch-wasm32/Cargo.toml new file mode 100644 index 0000000000..f25b5c10f9 --- /dev/null +++ b/wgpu-core-target-feature-conditionals/arch-wasm32/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "wgpu-core-feature-conditional-arch-wasm32" +version.workspace = true +authors.workspace = true +edition.workspace = true +description = "Workaround crate for enabling features in wgpu-core when targeting wasm32" +homepage.workspace = true +repository.workspace = true +keywords.workspace = true +license.workspace = true + +[lib] + +[features] +gles = ["wgc/gles"] + +[target.'cfg(target_arch = "wasm32")'.dependencies.wgc] +workspace = true diff --git a/wgpu-core-target-feature-conditionals/arch-wasm32/src/lib.rs b/wgpu-core-target-feature-conditionals/arch-wasm32/src/lib.rs new file mode 100644 index 0000000000..174c77f96a --- /dev/null +++ b/wgpu-core-target-feature-conditionals/arch-wasm32/src/lib.rs @@ -0,0 +1 @@ +//! No code. See README.md for details. diff --git a/wgpu-core-target-feature-conditionals/native-non-apple/Cargo.toml b/wgpu-core-target-feature-conditionals/native-non-apple/Cargo.toml new file mode 100644 index 0000000000..81d8e89b13 --- /dev/null +++ b/wgpu-core-target-feature-conditionals/native-non-apple/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "wgpu-core-feature-conditional-native-non-apple" +version.workspace = true +authors.workspace = true +edition.workspace = true +description = "Workaround crate for enabling features in wgpu-core when targeting macOS/iOS" +homepage.workspace = true +repository.workspace = true +keywords.workspace = true +license.workspace = true + +[lib] + +[features] +gles = ["wgc/gles"] +renderdoc = ["wgc/renderdoc"] +vulkan = ["wgc/vulkan"] + +raw-window-handle = ["wgc/raw-window-handle"] + +[target.'cfg(any(windows, all(unix, not(target_arch = "wasm32"), not(target_vendor = "apple"))))'.dependencies.wgc] +workspace = true diff --git a/wgpu-core-target-feature-conditionals/native-non-apple/src/lib.rs b/wgpu-core-target-feature-conditionals/native-non-apple/src/lib.rs new file mode 100644 index 0000000000..174c77f96a --- /dev/null +++ b/wgpu-core-target-feature-conditionals/native-non-apple/src/lib.rs @@ -0,0 +1 @@ +//! No code. See README.md for details. diff --git a/wgpu-core-target-feature-conditionals/vendor-apple/Cargo.toml b/wgpu-core-target-feature-conditionals/vendor-apple/Cargo.toml new file mode 100644 index 0000000000..507cb949ca --- /dev/null +++ b/wgpu-core-target-feature-conditionals/vendor-apple/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "wgpu-core-feature-conditional-vendor-apple" +version.workspace = true +authors.workspace = true +edition.workspace = true +description = "Workaround crate for enabling features in wgpu-core when targeting macOS/iOS" +homepage.workspace = true +repository.workspace = true +keywords.workspace = true +license.workspace = true + +[lib] + +[features] +gles = ["wgc/gles"] +metal = ["wgc/metal"] +vulkan = ["wgc/vulkan"] + +raw-window-handle = ["wgc/raw-window-handle"] + +[target.'cfg(target_vendor = "apple")'.dependencies.wgc] +workspace = true diff --git a/wgpu-core-target-feature-conditionals/vendor-apple/src/lib.rs b/wgpu-core-target-feature-conditionals/vendor-apple/src/lib.rs new file mode 100644 index 0000000000..174c77f96a --- /dev/null +++ b/wgpu-core-target-feature-conditionals/vendor-apple/src/lib.rs @@ -0,0 +1 @@ +//! No code. See README.md for details. diff --git a/wgpu-core/src/lib.rs b/wgpu-core/src/lib.rs index 4c2ea81490..3cdd65012e 100644 --- a/wgpu-core/src/lib.rs +++ b/wgpu-core/src/lib.rs @@ -92,6 +92,7 @@ pub mod validation; pub use validation::{map_storage_format_from_naga, map_storage_format_to_naga}; +pub use ::hal; pub use hal::{api, MAX_BIND_GROUPS, MAX_COLOR_ATTACHMENTS, MAX_VERTEX_BUFFERS}; pub use naga; diff --git a/wgpu/Cargo.toml b/wgpu/Cargo.toml index 8df1295852..52e920481f 100644 --- a/wgpu/Cargo.toml +++ b/wgpu/Cargo.toml @@ -29,33 +29,44 @@ ignored = ["cfg_aliases"] [lib] [features] -default = ["wgsl", "dx12", "metal", "webgpu"] +# TODO: should we disable GLES by default? keeping it for now since that's what we did effectively so far. +default = ["wgsl", "dx12", "metal", "vulkan", "gles", "webgpu"] #! ### Backends # -------------------------------------------------------------------- -#! ⚠️ WIP: Not all backends can be manually configured today. -#! On Windows, Linux & Android the Vulkan & GLES backends are always enabled. -#! See [#3514](https://github.com/gfx-rs/wgpu/issues/3514) for more details. ## Enables the DX12 backend on Windows. -dx12 = ["wgc?/dx12"] +dx12 = [ + "wgc/dx12", +] # Dx12 doesn't do anything on non-windows wgpu-core & wgpu-hal, so we don't need a proxy crate here. ## Enables the Metal backend on macOS & iOS. -metal = ["wgc?/metal"] +metal = [ + "wgpu-core-feature-conditional-vendor-apple/metal", +] # Metal doesn't do anything on non-Apple wgpu-core & wgpu-hal, but we already have a proxy crate for Apple platforms anyways. -## Enables the WebGPU backend on Wasm. Disabled when targeting `emscripten`. -webgpu = ["naga?/wgsl-out"] - -## Enables the GLES backend via [ANGLE](https://github.com/google/angle) on macOS using. -angle = ["wgc?/gles"] +## Enables the Vulkan backend on Windows, Linux, and Android. +## +## For enabling Vulkan on macOS & iOS, use the `vulkan-portability` feature. +vulkan = ["wgpu-core-feature-conditional-native-non-apple/vulkan"] ## Enables the Vulkan backend on macOS & iOS. -vulkan-portability = ["wgc?/vulkan"] +vulkan-portability = ["wgpu-core-feature-conditional-vendor-apple/vulkan"] -## Enables the GLES backend on Wasm +## Enables the OpenGL/GLES backend on Windows, Linux, and Android. ## -## * ⚠️ WIP: Currently will also enable GLES dependencies on any other targets. -webgl = ["dep:hal", "wgc/gles"] +## For enabling WebGL use the `webgl` feature, for enabling OpenGL via ANGLE on macOS use the `angle` feature. +gles = ["wgpu-core-feature-conditional-native-non-apple/gles"] + +## Enables the GLES backend via [ANGLE](https://github.com/google/angle) on macOS using. +angle = ["wgpu-core-feature-conditional-vendor-apple/gles"] + +## Enables GLES backend (WebGL) on Wasm +webgl = ["wgpu-core-feature-conditional-arch-wasm32/gles"] + +## Enables the WebGPU backend on Wasm. Disabled when targeting `emscripten`. +webgpu = ["naga?/wgsl-out"] + #! **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 @@ -140,46 +151,22 @@ features = ["raw-window-handle"] # Whenever wgpu-core is selected, we want raw window handle support. [target.'cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))'.dependencies.wgc] workspace = true +optional = true features = ["raw-window-handle"] # If we are not targeting WebGL, enable indirect-validation. # WebGL doesn't support indirect execution so this is not needed. [target.'cfg(not(target_arch = "wasm32"))'.dependencies.wgc] workspace = true +optional = true features = ["indirect-validation"] -# Enable `wgc` by default on macOS and iOS to allow the `metal` crate feature to -# enable the Metal backend while being no-op on other targets. -[target.'cfg(target_vendor = "apple")'.dependencies.wgc] -workspace = true - -# We want the wgpu-core Direct3D backend and OpenGL (via WGL) on Windows. -[target.'cfg(windows)'.dependencies.wgc] -workspace = true -features = ["gles"] - -# We want the wgpu-core Vulkan backend on Unix (but not emscripten, macOS, iOS) and Windows. -[target.'cfg(any(windows, all(unix, not(target_os = "emscripten"), not(target_vendor = "apple"))))'.dependencies.wgc] -workspace = true -features = ["vulkan"] - -# We want the wgpu-core GLES backend on Unix (but not macOS, iOS). -[target.'cfg(all(unix, not(target_vendor = "apple")))'.dependencies.wgc] -workspace = true -features = ["gles"] - [dependencies.wgt] workspace = true -# We need wgpu-hal unless we're targeting the web APIs. -[target.'cfg(any(not(target_arch = "wasm32"), target_os = "emscripten"))'.dependencies] -hal = { workspace = true } - -[target.'cfg(all(not(target_arch = "wasm32"), unix, not(target_vendor = "apple")))'.dependencies] -hal = { workspace = true, features = ["renderdoc"] } - -[target.'cfg(windows)'.dependencies] -hal = { workspace = true, features = ["renderdoc"] } +# If hal is dragged in via wgc, enable renderdoc if we're native & not on apple. +[target.'cfg(all(not(target_arch = "wasm32"), any(unix, windows), not(target_vendor = "apple")))'.dependencies] +hal = { workspace = true, optional = true, features = ["renderdoc"] } [target.'cfg(target_arch = "wasm32")'.dependencies.hal] workspace = true @@ -197,6 +184,10 @@ serde = { workspace = true, features = ["default", "derive"], optional = true } smallvec.workspace = true static_assertions.workspace = true +wgpu-core-feature-conditional-arch-wasm32 = { workspace = true, optional = true } +wgpu-core-feature-conditional-native-non-apple = { workspace = true, optional = true } +wgpu-core-feature-conditional-vendor-apple = { workspace = true, optional = true } + [dependencies.naga] workspace = true optional = true diff --git a/wgpu/build.rs b/wgpu/build.rs index d853281dfa..604b3bd5e1 100644 --- a/wgpu/build.rs +++ b/wgpu/build.rs @@ -4,13 +4,24 @@ fn main() { webgl: { all(target_arch = "wasm32", not(target_os = "emscripten"), feature = "webgl") }, webgpu: { all(target_arch = "wasm32", not(target_os = "emscripten"), feature = "webgpu") }, Emscripten: { all(target_arch = "wasm32", target_os = "emscripten") }, - wgpu_core: { any(native, webgl, Emscripten) }, send_sync: { any( not(target_arch = "wasm32"), all(feature = "fragile-send-sync-non-atomic-wasm", not(target_feature = "atomics")) ) }, dx12: { all(target_os = "windows", feature = "dx12") }, metal: { all(target_vendor = "apple", feature = "metal") }, + vulkan: { + any(all(feature = "vulkan-portability", target_vendor = "apple"), + all(feature = "vulkan", native, not(target_vendor = "apple")) + )}, + gles: { + any( + webgl, + all(feature = "angle", target_vendor = "apple"), + all(feature = "gles", native, not(target_vendor = "apple")) + ) + }, + wgpu_core: { any(dx12, webgl, metal, vulkan) }, // This alias is _only_ if _we_ need naga in the wrapper. wgpu-core provides // its own re-export of naga, which can be used in other situations naga: { any(feature = "naga-ir", feature = "spirv", feature = "glsl") }, diff --git a/wgpu/src/api/instance.rs b/wgpu/src/api/instance.rs index a8daf37fdc..3b54ec4658 100644 --- a/wgpu/src/api/instance.rs +++ b/wgpu/src/api/instance.rs @@ -60,45 +60,23 @@ impl Instance { /// /// `InstanceDescriptor::backends` does not need to be a subset of this, /// but any backend that is not in this set, will not be picked. - /// - /// TODO: Right now it's otherwise not possible yet to opt-out of all features on some platforms. - /// See - /// * Windows/Linux/Android: always enables Vulkan and GLES with no way to opt out pub const fn enabled_backend_features() -> Backends { let mut backends = Backends::empty(); - - if cfg!(native) { - if cfg!(metal) { - backends = backends.union(Backends::METAL); - } - if cfg!(dx12) { - backends = backends.union(Backends::DX12); - } - - // Windows, Android, Linux currently always enable Vulkan and OpenGL. - // See - if cfg!(target_os = "windows") || cfg!(unix) { - backends = backends.union(Backends::VULKAN).union(Backends::GL); - } - - // Vulkan on Mac/iOS is only available through vulkan-portability. - if cfg!(target_vendor = "apple") && cfg!(feature = "vulkan-portability") { - backends = backends.union(Backends::VULKAN); - } - - // GL on Mac is only available through angle. - if cfg!(target_os = "macos") && cfg!(feature = "angle") { - backends = backends.union(Backends::GL); - } - } else { - if cfg!(webgpu) { - backends = backends.union(Backends::BROWSER_WEBGPU); - } - if cfg!(webgl) { - backends = backends.union(Backends::GL); - } + if cfg!(metal) { + backends = backends.union(Backends::METAL); + } + if cfg!(dx12) { + backends = backends.union(Backends::DX12); + } + if cfg!(vulkan) { + backends = backends.union(Backends::VULKAN); + } + if cfg!(gles) { + backends = backends.union(Backends::GL); + } + if cfg!(webgpu) { + backends = backends.union(Backends::BROWSER_WEBGPU); } - backends } @@ -221,7 +199,7 @@ impl Instance { /// # Arguments /// /// - `backends` - Backends from which to enumerate adapters. - #[cfg(native)] + #[cfg(all(native, wgpu_core))] pub fn enumerate_adapters(&self, backends: Backends) -> Vec { let Some(core_instance) = self.inner.as_core_opt() else { return Vec::new(); diff --git a/wgpu/src/backend/wgpu_core.rs b/wgpu/src/backend/wgpu_core.rs index ad750860bc..edb7efa6fd 100644 --- a/wgpu/src/backend/wgpu_core.rs +++ b/wgpu/src/backend/wgpu_core.rs @@ -13,7 +13,9 @@ use std::{ borrow::Cow::Borrowed, error::Error, fmt, future::ready, ops::Range, pin::Pin, ptr::NonNull, slice, sync::Arc, }; -use wgc::{command::bundle_ffi::*, error::ContextErrorSource, pipeline::CreateShaderModuleError}; +use wgc::{ + command::bundle_ffi::*, error::ContextErrorSource, hal, pipeline::CreateShaderModuleError, +}; use wgt::WasmNotSendSync; #[derive(Clone)] diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index f94be6bdf1..32d82888f3 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -97,7 +97,7 @@ pub use ::wgc as core; /// /// #[cfg(wgpu_core)] -pub use ::hal; +pub use ::wgc::hal; /// Re-export of our `naga` dependency. /// diff --git a/wgpu/src/util/init.rs b/wgpu/src/util/init.rs index 54de26da65..d48047eb03 100644 --- a/wgpu/src/util/init.rs +++ b/wgpu/src/util/init.rs @@ -4,7 +4,7 @@ use crate::{Adapter, Instance, RequestAdapterOptions, Surface}; use crate::Backends; /// Initialize the adapter obeying the WGPU_ADAPTER_NAME environment variable. -#[cfg(native)] +#[cfg(all(native, wgpu_core))] pub fn initialize_adapter_from_env( instance: &Instance, compatible_surface: Option<&Surface<'_>>, @@ -36,7 +36,7 @@ pub fn initialize_adapter_from_env( } /// Initialize the adapter obeying the WGPU_ADAPTER_NAME environment variable. -#[cfg(not(native))] +#[cfg(not(all(native, wgpu_core)))] pub fn initialize_adapter_from_env( _instance: &Instance, _compatible_surface: Option<&Surface<'_>>,