Skip to content

Commit df354a9

Browse files
committed
Use crate indirections to enable feature depending on platforms
1 parent b233d20 commit df354a9

File tree

15 files changed

+169
-92
lines changed

15 files changed

+169
-92
lines changed

Cargo.lock

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ members = [
2121
"wgpu-macros",
2222
"wgpu-types",
2323
"wgpu",
24+
"wgpu-core-conditional/arch-wasm32",
25+
"wgpu-core-conditional/native-non-apple",
26+
"wgpu-core-conditional/vendor-apple",
2427
]
2528
exclude = []
2629
default-members = [
@@ -40,6 +43,9 @@ default-members = [
4043
"wgpu-macros",
4144
"wgpu-types",
4245
"wgpu",
46+
"wgpu-core-conditional/arch-wasm32",
47+
"wgpu-core-conditional/native-non-apple",
48+
"wgpu-core-conditional/vendor-apple",
4349
]
4450

4551
[workspace.lints.clippy]
@@ -66,6 +72,9 @@ wgpu = { version = "24.0.0", path = "./wgpu", default-features = false, features
6672
"static-dxc",
6773
] }
6874
wgpu-core = { version = "24.0.0", path = "./wgpu-core" }
75+
wgpu-core-if-arch-wasm32 = { version = "24.0.0", path = "./wgpu-core-conditional/arch-wasm32" }
76+
wgpu-core-if-native-non-apple = { version = "24.0.0", path = "./wgpu-core-conditional/native-non-apple" }
77+
wgpu-core-if-vendor-apple = { version = "24.0.0", path = "./wgpu-core-conditional/vendor-apple" }
6978
wgpu-hal = { version = "24.0.0", path = "./wgpu-hal" }
7079
wgpu-macros = { version = "24.0.0", path = "./wgpu-macros" }
7180
wgpu-test = { version = "24.0.0", path = "./tests" }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "wgpu-core-if-arch-wasm32"
3+
version.workspace = true
4+
authors.workspace = true
5+
edition.workspace = true
6+
description = "Workaround crate for enabling features in wgpu-core when targeting wasm32"
7+
homepage.workspace = true
8+
repository.workspace = true
9+
keywords.workspace = true
10+
license.workspace = true
11+
12+
[lib]
13+
14+
[features]
15+
gles = ["wgpu-core/gles"]
16+
17+
[target.'cfg(target_arch = "wasm32")'.dependencies]
18+
wgpu-core.workspace = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//! No code. See README.md for details.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "wgpu-core-if-native-non-apple"
3+
version.workspace = true
4+
authors.workspace = true
5+
edition.workspace = true
6+
description = "Workaround crate for enabling features in wgpu-core when targeting macOS/iOS"
7+
homepage.workspace = true
8+
repository.workspace = true
9+
keywords.workspace = true
10+
license.workspace = true
11+
12+
[lib]
13+
14+
[features]
15+
gles = ["wgpu-core/gles"]
16+
renderdoc = ["wgpu-core/renderdoc"]
17+
vulkan = ["wgpu-core/vulkan"]
18+
19+
raw-window-handle = ["wgpu-core/raw-window-handle"]
20+
21+
[target.'cfg(any(windows, all(unix, not(target_arch = "wasm32"), not(target_vendor = "apple"))))'.dependencies]
22+
wgpu-core.workspace = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//! No code. See README.md for details.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "wgpu-core-if-vendor-apple"
3+
version.workspace = true
4+
authors.workspace = true
5+
edition.workspace = true
6+
description = "Workaround crate for enabling features in wgpu-core when targeting macOS/iOS"
7+
homepage.workspace = true
8+
repository.workspace = true
9+
keywords.workspace = true
10+
license.workspace = true
11+
12+
[lib]
13+
14+
[features]
15+
gles = ["wgpu-core/gles"]
16+
metal = ["wgpu-core/metal"]
17+
vulkan = ["wgpu-core/vulkan"]
18+
19+
raw-window-handle = ["wgpu-core/raw-window-handle"]
20+
21+
[target.'cfg(target_vendor = "apple")'.dependencies]
22+
wgpu-core.workspace = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//! No code. See README.md for details.

wgpu-core/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
// (the only reason to use wgpu-core on the web in the first place) that have atomics enabled.
5757
#![cfg_attr(not(send_sync), allow(clippy::arc_with_non_send_sync))]
5858

59-
extern crate wgpu_hal as hal;
60-
extern crate wgpu_types as wgt;
59+
pub extern crate wgpu_hal as hal;
60+
pub extern crate wgpu_types as wgt;
6161

6262
pub mod binding_model;
6363
pub mod command;

wgpu/Cargo.toml

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,41 @@ ignored = ["cfg_aliases"]
2828
[lib]
2929

3030
[features]
31-
default = ["wgsl", "dx12", "metal", "webgpu"]
31+
# TODO: should we disable GLES by default? keeping it for now since that's what we did effectively so far.
32+
default = ["wgsl", "dx12", "metal", "vulkan", "gles", "webgpu"]
3233

3334
#! ### Backends
3435
# --------------------------------------------------------------------
35-
#! ⚠️ WIP: Not all backends can be manually configured today.
36-
#! On Windows, Linux & Android the Vulkan & GLES backends are always enabled.
37-
#! See [#3514](https://github.com/gfx-rs/wgpu/issues/3514) for more details.
3836

3937
## Enables the DX12 backend on Windows.
40-
dx12 = ["wgpu-core?/dx12"]
38+
# Dx12 doesn't do anything on non-windows wgpu-core & wgpu-hal, so we don't need a proxy crate here.
39+
dx12 = ["wgpu-core/dx12"]
4140

4241
## Enables the Metal backend on macOS & iOS.
43-
metal = ["wgpu-core?/metal"]
42+
# Metal doesn't do anything on non-Apple wgpu-core & wgpu-hal, but we already have a proxy crate for Apple platforms anyways.
43+
metal = ["wgpu-core-if-vendor-apple/metal"]
4444

45-
## Enables the WebGPU backend on Wasm. Disabled when targeting `emscripten`.
46-
webgpu = ["naga?/wgsl-out"]
47-
48-
## Enables the GLES backend via [ANGLE](https://github.com/google/angle) on macOS using.
49-
angle = ["wgpu-core?/gles"]
45+
## Enables the Vulkan backend on Windows, Linux, and Android.
46+
##
47+
## For enabling Vulkan on macOS & iOS, use the `vulkan-portability` feature.
48+
vulkan = ["wgpu-core-if-native-non-apple/vulkan"]
5049

5150
## Enables the Vulkan backend on macOS & iOS.
52-
vulkan-portability = ["wgpu-core?/vulkan"]
51+
vulkan-portability = ["wgpu-core-if-vendor-apple/vulkan"]
5352

54-
## Enables the GLES backend on Wasm
53+
## Enables the OpenGL/GLES backend on Windows, Linux, and Android.
5554
##
56-
## * ⚠️ WIP: Currently will also enable GLES dependencies on any other targets.
57-
webgl = ["dep:wgpu-hal", "wgpu-core/gles"]
55+
## For enabling WebGL use the `webgl` feature, for enabling OpenGL via ANGLE on macOS use the `angle` feature.
56+
gles = ["wgpu-core-if-native-non-apple/gles"]
57+
58+
## Enables the GLES backend via [ANGLE](https://github.com/google/angle) on macOS using.
59+
angle = ["wgpu-core-if-vendor-apple/gles"]
60+
61+
## Enables GLES backend (WebGL) on Wasm
62+
webgl = ["wgpu-core-if-arch-wasm32/gles"]
63+
64+
## Enables the WebGPU backend on Wasm. Disabled when targeting `emscripten`.
65+
webgpu = ["naga?/wgsl-out"]
5866

5967
#! **Note:** In the documentation, if you see that an item depends on a backend,
6068
#! it means that the item is only available when that backend is enabled _and_ the backend
@@ -134,8 +142,11 @@ static-dxc = ["wgpu-hal/static-dxc"]
134142

135143
[dependencies]
136144
naga = { workspace = true, optional = true }
137-
wgpu-core = { workspace = true, optional = true }
145+
wgpu-core = { workspace = true, optional = true, features = [
146+
"raw-window-handle",
147+
] }
138148
wgpu-types = { workspace = true, features = ["serde"] }
149+
wgpu-hal = { workspace = true, optional = true, features = ["renderdoc"] }
139150

140151
arrayvec.workspace = true
141152
bitflags.workspace = true
@@ -148,40 +159,16 @@ serde = { workspace = true, features = ["default", "derive"], optional = true }
148159
smallvec.workspace = true
149160
static_assertions.workspace = true
150161

162+
wgpu-core-if-arch-wasm32 = { workspace = true, optional = true }
163+
wgpu-core-if-native-non-apple = { workspace = true, optional = true }
164+
wgpu-core-if-vendor-apple = { workspace = true, optional = true }
165+
166+
151167
########################################
152168
# Target Specific Feature Dependencies #
153169
########################################
154-
155-
# Windows
156-
[target.'cfg(windows)'.dependencies]
157-
wgpu-core = { workspace = true, features = [
158-
"raw-window-handle",
159-
"vulkan",
160-
"gles",
161-
] }
162-
wgpu-hal = { workspace = true, features = ["renderdoc"] }
163-
164-
# Apple Platforms
165-
[target.'cfg(target_vendor = "apple")'.dependencies]
166-
wgpu-core = { workspace = true, features = ["raw-window-handle"] }
167-
wgpu-hal = { workspace = true, features = [] }
168-
169-
# Linux + Android
170-
[target.'cfg(any(target_os = "linux", target_os = "android"))'.dependencies]
171-
wgpu-core = { workspace = true, features = [
172-
"raw-window-handle",
173-
"vulkan",
174-
"gles",
175-
] }
176-
wgpu-hal = { workspace = true, features = ["renderdoc"] }
177-
178170
# Webassembly
179171
[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
180-
wgpu-core = { workspace = true, optional = true, features = [
181-
"raw-window-handle",
182-
] }
183-
wgpu-hal = { workspace = true, optional = true }
184-
185172
js-sys = { workspace = true, features = ["default"] }
186173
parking_lot.workspace = true
187174
wasm-bindgen-futures.workspace = true

wgpu/build.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
fn main() {
22
cfg_aliases::cfg_aliases! {
33
native: { not(target_arch = "wasm32") },
4-
webgl: { all(target_arch = "wasm32", not(target_os = "emscripten"), feature = "webgl") },
5-
webgpu: { all(target_arch = "wasm32", not(target_os = "emscripten"), feature = "webgpu") },
64
Emscripten: { all(target_arch = "wasm32", target_os = "emscripten") },
7-
wgpu_core: { any(native, webgl, Emscripten) },
85
send_sync: { any(
96
not(target_arch = "wasm32"),
107
all(feature = "fragile-send-sync-non-atomic-wasm", not(target_feature = "atomics"))
118
) },
9+
10+
webgl: { all(target_arch = "wasm32", not(target_os = "emscripten"), feature = "webgl") },
11+
webgpu: { all(target_arch = "wasm32", not(target_os = "emscripten"), feature = "webgpu") },
1212
dx12: { all(target_os = "windows", feature = "dx12") },
1313
metal: { all(target_vendor = "apple", feature = "metal") },
14+
vulkan: {
15+
any(all(feature = "vulkan-portability", target_vendor = "apple"),
16+
all(feature = "vulkan", native, not(target_vendor = "apple"))
17+
)},
18+
gles: {
19+
any(
20+
webgl,
21+
all(feature = "angle", target_vendor = "apple"),
22+
all(feature = "gles", native, not(target_vendor = "apple"))
23+
)
24+
},
25+
26+
wgpu_core: { any(dx12, webgl, metal, vulkan) },
27+
1428
// This alias is _only_ if _we_ need naga in the wrapper. wgpu-core provides
1529
// its own re-export of naga, which can be used in other situations
1630
naga: { any(feature = "naga-ir", feature = "spirv", feature = "glsl") },

wgpu/src/api/instance.rs

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -60,45 +60,23 @@ impl Instance {
6060
///
6161
/// `InstanceDescriptor::backends` does not need to be a subset of this,
6262
/// but any backend that is not in this set, will not be picked.
63-
///
64-
/// TODO: Right now it's otherwise not possible yet to opt-out of all features on some platforms.
65-
/// See <https://github.com/gfx-rs/wgpu/issues/3514>
66-
/// * Windows/Linux/Android: always enables Vulkan and GLES with no way to opt out
6763
pub const fn enabled_backend_features() -> Backends {
6864
let mut backends = Backends::empty();
69-
70-
if cfg!(native) {
71-
if cfg!(metal) {
72-
backends = backends.union(Backends::METAL);
73-
}
74-
if cfg!(dx12) {
75-
backends = backends.union(Backends::DX12);
76-
}
77-
78-
// Windows, Android, Linux currently always enable Vulkan and OpenGL.
79-
// See <https://github.com/gfx-rs/wgpu/issues/3514>
80-
if cfg!(target_os = "windows") || cfg!(unix) {
81-
backends = backends.union(Backends::VULKAN).union(Backends::GL);
82-
}
83-
84-
// Vulkan on Mac/iOS is only available through vulkan-portability.
85-
if cfg!(target_vendor = "apple") && cfg!(feature = "vulkan-portability") {
86-
backends = backends.union(Backends::VULKAN);
87-
}
88-
89-
// GL on Mac is only available through angle.
90-
if cfg!(target_os = "macos") && cfg!(feature = "angle") {
91-
backends = backends.union(Backends::GL);
92-
}
93-
} else {
94-
if cfg!(webgpu) {
95-
backends = backends.union(Backends::BROWSER_WEBGPU);
96-
}
97-
if cfg!(webgl) {
98-
backends = backends.union(Backends::GL);
99-
}
65+
if cfg!(metal) {
66+
backends = backends.union(Backends::METAL);
67+
}
68+
if cfg!(dx12) {
69+
backends = backends.union(Backends::DX12);
70+
}
71+
if cfg!(vulkan) {
72+
backends = backends.union(Backends::VULKAN);
73+
}
74+
if cfg!(gles) {
75+
backends = backends.union(Backends::GL);
76+
}
77+
if cfg!(webgpu) {
78+
backends = backends.union(Backends::BROWSER_WEBGPU);
10079
}
101-
10280
backends
10381
}
10482

@@ -221,7 +199,7 @@ impl Instance {
221199
/// # Arguments
222200
///
223201
/// - `backends` - Backends from which to enumerate adapters.
224-
#[cfg(native)]
202+
#[cfg(all(native, wgpu_core))]
225203
pub fn enumerate_adapters(&self, backends: Backends) -> Vec<Adapter> {
226204
let Some(core_instance) = self.inner.as_core_opt() else {
227205
return Vec::new();

wgpu/src/backend/wgpu_core.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl ContextWgpuCore {
6161

6262
pub unsafe fn create_adapter_from_hal<A: wgc::hal_api::HalApi>(
6363
&self,
64-
hal_adapter: hal::ExposedAdapter<A>,
64+
hal_adapter: crate::hal::ExposedAdapter<A>,
6565
) -> wgc::id::AdapterId {
6666
unsafe { self.0.create_adapter_from_hal(hal_adapter.into(), None) }
6767
}
@@ -95,7 +95,7 @@ impl ContextWgpuCore {
9595
pub unsafe fn create_device_from_hal<A: wgc::hal_api::HalApi>(
9696
&self,
9797
adapter: &CoreAdapter,
98-
hal_device: hal::OpenDevice<A>,
98+
hal_device: crate::hal::OpenDevice<A>,
9999
desc: &crate::DeviceDescriptor<'_>,
100100
trace_dir: Option<&std::path::Path>,
101101
) -> Result<(CoreDevice, CoreQueue), crate::RequestDeviceError> {

wgpu/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#[cfg(wgpu_core)]
2929
pub extern crate wgpu_core as wgc;
3030
#[cfg(wgpu_core)]
31-
pub extern crate wgpu_hal as hal;
31+
pub use wgpu_core::hal;
3232
pub extern crate wgpu_types as wgt;
3333

3434
//

0 commit comments

Comments
 (0)