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

Make all backends controlled by features (using a very ugly workaround) #6949

Draft
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Comment on lines 22 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we consider shorter names for these, perhaps with slight reordering?

Suggested change
"wgpu",
"wgpu-core-target-feature-conditionals/arch-wasm32",
"wgpu-core-target-feature-conditionals/native-non-apple",
"wgpu-core-target-feature-conditionals/vendor-apple",
"wgpu-core-target-features/arch-wasm32",
"wgpu-core-target-features/native-non-apple",
"wgpu-core-target-features/vendor-apple",
"wgpu",

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah those names are definitely too crazy right now

]
exclude = []
default-members = [
Expand All @@ -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]
Expand Down Expand Up @@ -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"
Expand Down
18 changes: 18 additions & 0 deletions wgpu-core-target-feature-conditionals/arch-wasm32/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! No code. See README.md for details.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! No code. See README.md for details.
22 changes: 22 additions & 0 deletions wgpu-core-target-feature-conditionals/vendor-apple/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//! No code. See README.md for details.
1 change: 1 addition & 0 deletions wgpu-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
79 changes: 35 additions & 44 deletions wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 12 additions & 1 deletion wgpu/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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") },
Expand Down
52 changes: 15 additions & 37 deletions wgpu/src/api/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/gfx-rs/wgpu/issues/3514>
/// * 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 <https://github.com/gfx-rs/wgpu/issues/3514>
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);
}
Comment on lines -69 to -99
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3 always love for removing large blocks of ugly code :)

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
}

Expand Down Expand Up @@ -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<Adapter> {
let Some(core_instance) = self.inner.as_core_opt() else {
return Vec::new();
Expand Down
4 changes: 3 additions & 1 deletion wgpu/src/backend/wgpu_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
Loading
Loading