Skip to content

Commit

Permalink
Make wgpu-core users responsible for choosing back ends.
Browse files Browse the repository at this point in the history
Before this change, backend selection is done by target dependencies in
`wgpu-core/Cargo.toml`, giving `wgpu-core` users no way to override those
choices. (Firefox doesn't want the gles back end, for example.)

There doesn't seem to be any way to have a crate select backends based on target
architecture and OS that users of that crate can still override. The default
features can't be selected based on the target, for example. That implies that
we should do the selection as late in the dependency DAG as feasible. Having
`wgpu` (and `wgpu-core`'s other dependents) choose backends seems good enough.
  • Loading branch information
jimblandy committed Dec 2, 2022
1 parent f64de4e commit 3eeef01
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 145 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

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

22 changes: 10 additions & 12 deletions wgpu-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ rustdoc-args = ["--cfg", "docsrs"]

[features]
default = []

# Backends, passed through to wgpu-hal
metal = ["hal/metal"]
vulkan = ["hal/vulkan"]
gles = ["hal/gles"]
dx11 = ["hal/dx11"]
dx12 = ["hal/dx12"]

# Apply run-time checks, even in release builds. These are in addition
# to the validation carried out at public APIs in all builds.
strict_asserts = []
Expand Down Expand Up @@ -60,21 +68,11 @@ workspace = true
[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
web-sys = { workspace = true, features = ["HtmlCanvasElement", "OffscreenCanvas"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
hal = { workspace = true, features = ["gles"] }

[target.'cfg(all(not(target_arch = "wasm32"), any(target_os = "ios", target_os = "macos")))'.dependencies]
hal = { workspace = true, features = ["metal"] }
#Note: could also enable "vulkan" for Vulkan Portability

[target.'cfg(all(not(target_arch = "wasm32"), unix, not(target_os = "ios"), not(target_os = "macos")))'.dependencies]
hal = { workspace = true, features = ["vulkan", "gles", "renderdoc"] }
hal = { workspace = true, features = ["renderdoc"] }

[target.'cfg(all(not(target_arch = "wasm32"), windows))'.dependencies]
hal = { workspace = true, features = ["vulkan", "dx12", "dx11", "renderdoc"] }
hal = { workspace = true, features = ["renderdoc"] }

[target.'cfg(target_os = "emscripten")'.dependencies]
hal = { workspace = true, features = ["emscripten"] }

[build-dependencies]
cfg_aliases.workspace = true
22 changes: 0 additions & 22 deletions wgpu-core/build.rs

This file was deleted.

10 changes: 5 additions & 5 deletions wgpu-core/src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5407,27 +5407,27 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
let mut closures = UserClosures::default();
let mut all_queue_empty = true;

#[cfg(vulkan)]
#[cfg(feature = "vulkan")]
{
all_queue_empty = self.poll_devices::<hal::api::Vulkan>(force_wait, &mut closures)?
&& all_queue_empty;
}
#[cfg(metal)]
#[cfg(feature = "metal")]
{
all_queue_empty =
self.poll_devices::<hal::api::Metal>(force_wait, &mut closures)? && all_queue_empty;
}
#[cfg(dx12)]
#[cfg(feature = "dx12")]
{
all_queue_empty =
self.poll_devices::<hal::api::Dx12>(force_wait, &mut closures)? && all_queue_empty;
}
#[cfg(dx11)]
#[cfg(feature = "dx11")]
{
all_queue_empty =
self.poll_devices::<hal::api::Dx11>(force_wait, &mut closures)? && all_queue_empty;
}
#[cfg(gl)]
#[cfg(feature = "gles")]
{
all_queue_empty =
self.poll_devices::<hal::api::Gles>(force_wait, &mut closures)? && all_queue_empty;
Expand Down
60 changes: 30 additions & 30 deletions wgpu-core/src/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,30 +1057,30 @@ impl<A: HalApi, F: GlobalIdentityHandlerFactory> Hub<A, F> {
}

pub struct Hubs<F: GlobalIdentityHandlerFactory> {
#[cfg(vulkan)]
#[cfg(feature = "vulkan")]
vulkan: Hub<hal::api::Vulkan, F>,
#[cfg(metal)]
#[cfg(feature = "metal")]
metal: Hub<hal::api::Metal, F>,
#[cfg(dx12)]
#[cfg(feature = "dx12")]
dx12: Hub<hal::api::Dx12, F>,
#[cfg(dx11)]
#[cfg(feature = "dx11")]
dx11: Hub<hal::api::Dx11, F>,
#[cfg(gl)]
#[cfg(feature = "gles")]
gl: Hub<hal::api::Gles, F>,
}

impl<F: GlobalIdentityHandlerFactory> Hubs<F> {
fn new(factory: &F) -> Self {
Self {
#[cfg(vulkan)]
#[cfg(feature = "vulkan")]
vulkan: Hub::new(factory),
#[cfg(metal)]
#[cfg(feature = "metal")]
metal: Hub::new(factory),
#[cfg(dx12)]
#[cfg(feature = "dx12")]
dx12: Hub::new(factory),
#[cfg(dx11)]
#[cfg(feature = "dx11")]
dx11: Hub::new(factory),
#[cfg(gl)]
#[cfg(feature = "gles")]
gl: Hub::new(factory),
}
}
Expand All @@ -1089,15 +1089,15 @@ impl<F: GlobalIdentityHandlerFactory> Hubs<F> {
#[derive(Debug)]
pub struct GlobalReport {
pub surfaces: StorageReport,
#[cfg(vulkan)]
#[cfg(feature = "vulkan")]
pub vulkan: Option<HubReport>,
#[cfg(metal)]
#[cfg(feature = "metal")]
pub metal: Option<HubReport>,
#[cfg(dx12)]
#[cfg(feature = "dx12")]
pub dx12: Option<HubReport>,
#[cfg(dx11)]
#[cfg(feature = "dx11")]
pub dx11: Option<HubReport>,
#[cfg(gl)]
#[cfg(feature = "gles")]
pub gl: Option<HubReport>,
}

Expand Down Expand Up @@ -1162,31 +1162,31 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
pub fn generate_report(&self) -> GlobalReport {
GlobalReport {
surfaces: self.surfaces.data.read().generate_report(),
#[cfg(vulkan)]
#[cfg(feature = "vulkan")]
vulkan: if self.instance.vulkan.is_some() {
Some(self.hubs.vulkan.generate_report())
} else {
None
},
#[cfg(metal)]
#[cfg(feature = "metal")]
metal: if self.instance.metal.is_some() {
Some(self.hubs.metal.generate_report())
} else {
None
},
#[cfg(dx12)]
#[cfg(feature = "dx12")]
dx12: if self.instance.dx12.is_some() {
Some(self.hubs.dx12.generate_report())
} else {
None
},
#[cfg(dx11)]
#[cfg(feature = "dx11")]
dx11: if self.instance.dx11.is_some() {
Some(self.hubs.dx11.generate_report())
} else {
None
},
#[cfg(gl)]
#[cfg(feature = "gles")]
gl: if self.instance.gl.is_some() {
Some(self.hubs.gl.generate_report())
} else {
Expand All @@ -1203,23 +1203,23 @@ impl<G: GlobalIdentityHandlerFactory> Drop for Global<G> {
let mut surface_guard = self.surfaces.data.write();

// destroy hubs before the instance gets dropped
#[cfg(vulkan)]
#[cfg(feature = "vulkan")]
{
self.hubs.vulkan.clear(&mut surface_guard, true);
}
#[cfg(metal)]
#[cfg(feature = "metal")]
{
self.hubs.metal.clear(&mut surface_guard, true);
}
#[cfg(dx12)]
#[cfg(feature = "dx12")]
{
self.hubs.dx12.clear(&mut surface_guard, true);
}
#[cfg(dx11)]
#[cfg(feature = "dx11")]
{
self.hubs.dx11.clear(&mut surface_guard, true);
}
#[cfg(gl)]
#[cfg(feature = "gles")]
{
self.hubs.gl.clear(&mut surface_guard, true);
}
Expand Down Expand Up @@ -1261,7 +1261,7 @@ impl HalApi for hal::api::Empty {
}
}

#[cfg(vulkan)]
#[cfg(feature = "vulkan")]
impl HalApi for hal::api::Vulkan {
const VARIANT: Backend = Backend::Vulkan;
fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
Expand All @@ -1285,7 +1285,7 @@ impl HalApi for hal::api::Vulkan {
}
}

#[cfg(metal)]
#[cfg(feature = "metal")]
impl HalApi for hal::api::Metal {
const VARIANT: Backend = Backend::Metal;
fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
Expand All @@ -1309,7 +1309,7 @@ impl HalApi for hal::api::Metal {
}
}

#[cfg(dx12)]
#[cfg(feature = "dx12")]
impl HalApi for hal::api::Dx12 {
const VARIANT: Backend = Backend::Dx12;
fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
Expand All @@ -1333,7 +1333,7 @@ impl HalApi for hal::api::Dx12 {
}
}

#[cfg(dx11)]
#[cfg(feature = "dx11")]
impl HalApi for hal::api::Dx11 {
const VARIANT: Backend = Backend::Dx11;
fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
Expand All @@ -1357,7 +1357,7 @@ impl HalApi for hal::api::Dx11 {
}
}

#[cfg(gl)]
#[cfg(feature = "gles")]
impl HalApi for hal::api::Gles {
const VARIANT: Backend = Backend::Gl;
fn create_instance_from_hal(name: &str, hal_instance: Self::Instance) -> Instance {
Expand Down
Loading

0 comments on commit 3eeef01

Please sign in to comment.