From 7572dbeaf9a072f9ab5a230558d059d59fc126e0 Mon Sep 17 00:00:00 2001 From: Zicklag Date: Sat, 29 Aug 2020 21:40:07 -0500 Subject: [PATCH] Use CFG Aliases to Replace the backends! Macro Uses the `cfg_aliases` crate to replace the `backends!` macro and provide a more natural way to gate backend specific code. --- Cargo.lock | 7 ++ wgpu-core/Cargo.toml | 3 + wgpu-core/build.rs | 14 +++ wgpu-core/src/device/mod.rs | 28 +++-- wgpu-core/src/hub.rs | 70 +++++------ wgpu-core/src/instance.rs | 70 +++++------ wgpu-core/src/lib.rs | 11 +- wgpu-core/src/macros.rs | 238 +++++++++++------------------------- 8 files changed, 181 insertions(+), 260 deletions(-) create mode 100644 wgpu-core/build.rs diff --git a/Cargo.lock b/Cargo.lock index c040531f8..731105fb9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -141,6 +141,12 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "cfg_aliases" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6da2b592f5a2e590c3d94c44313bab369f2286cfe1e4134c830bf3317814866" + [[package]] name = "chrono" version = "0.4.11" @@ -1658,6 +1664,7 @@ version = "0.6.0" dependencies = [ "arrayvec", "bitflags", + "cfg_aliases", "copyless", "fxhash", "gfx-backend-dx11", diff --git a/wgpu-core/Cargo.toml b/wgpu-core/Cargo.toml index 8c0bf5e4a..4aa49f9ef 100644 --- a/wgpu-core/Cargo.toml +++ b/wgpu-core/Cargo.toml @@ -62,3 +62,6 @@ gfx-backend-vulkan = { version = "0.6" } [dev-dependencies] loom = "0.3" + +[build-dependencies] +cfg_aliases = "0.1" \ No newline at end of file diff --git a/wgpu-core/build.rs b/wgpu-core/build.rs new file mode 100644 index 000000000..85b31c28d --- /dev/null +++ b/wgpu-core/build.rs @@ -0,0 +1,14 @@ +fn main() { + // Setup cfg aliases + cfg_aliases::cfg_aliases! { + // Vendors/systems + apple: { any(target_os = "ios", target_os = "macos") }, + + // Backends + vulkan: { any(windows, all(unix, not(apple)), feature = "gfx-backend-vulkan") }, + metal: { apple }, + dx12: { windows }, + dx11: { windows }, + gl: { unix }, + } +} diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 9e09d6d5c..bc8aa18e5 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -3515,19 +3515,21 @@ impl Global { use crate::backend; let mut callbacks = Vec::new(); - backends! { - #[vulkan] { - self.poll_devices::(force_wait, &mut callbacks)?; - } - #[metal] { - self.poll_devices::(force_wait, &mut callbacks)?; - } - #[dx12] { - self.poll_devices::(force_wait, &mut callbacks)?; - } - #[dx11] { - self.poll_devices::(force_wait, &mut callbacks)?; - } + #[cfg(vulkan)] + { + self.poll_devices::(force_wait, &mut callbacks)?; + } + #[cfg(metal)] + { + self.poll_devices::(force_wait, &mut callbacks)?; + } + #[cfg(dx12)] + { + self.poll_devices::(force_wait, &mut callbacks)?; + } + #[cfg(dx11)] + { + self.poll_devices::(force_wait, &mut callbacks)?; } fire_map_callbacks(callbacks); diff --git a/wgpu-core/src/hub.rs b/wgpu-core/src/hub.rs index 208345b8b..584b4508a 100644 --- a/wgpu-core/src/hub.rs +++ b/wgpu-core/src/hub.rs @@ -647,32 +647,27 @@ impl Hub { #[derive(Debug)] pub struct Hubs { - #[cfg(any( - not(any(target_os = "ios", target_os = "macos")), - feature = "gfx-backend-vulkan" - ))] + #[cfg(vulkan)] vulkan: Hub, - #[cfg(any(target_os = "ios", target_os = "macos"))] + #[cfg(metal)] metal: Hub, - #[cfg(windows)] + #[cfg(dx12)] dx12: Hub, - #[cfg(windows)] + #[cfg(dx11)] dx11: Hub, } impl Hubs { fn new(factory: &F) -> Self { - backends! { - Hubs { - #[vulkan] - vulkan: Hub::new(factory), - #[metal] - metal: Hub::new(factory), - #[dx12] - dx12: Hub::new(factory), - #[dx11] - dx11: Hub::new(factory), - } + Hubs { + #[cfg(vulkan)] + vulkan: Hub::new(factory), + #[cfg(metal)] + metal: Hub::new(factory), + #[cfg(dx12)] + dx12: Hub::new(factory), + #[cfg(dx11)] + dx11: Hub::new(factory), } } } @@ -702,19 +697,21 @@ impl Drop for Global { let mut surface_guard = self.surfaces.data.write(); // destroy hubs - backends! { - #[vulkan] { - self.hubs.vulkan.clear(&mut *surface_guard); - } - #[metal] { - self.hubs.metal.clear(&mut *surface_guard); - } - #[dx12] { - self.hubs.dx12.clear(&mut *surface_guard); - } - #[dx11] { - self.hubs.dx11.clear(&mut *surface_guard); - } + #[cfg(vulkan)] + { + self.hubs.vulkan.clear(&mut *surface_guard); + } + #[cfg(metal)] + { + self.hubs.metal.clear(&mut *surface_guard); + } + #[cfg(dx12)] + { + self.hubs.dx12.clear(&mut *surface_guard); + } + #[cfg(dx11)] + { + self.hubs.dx11.clear(&mut *surface_guard); } // destroy surfaces @@ -733,10 +730,7 @@ pub trait GfxBackend: hal::Backend { fn get_surface_mut(surface: &mut Surface) -> &mut Self::Surface; } -#[cfg(any( - not(any(target_os = "ios", target_os = "macos")), - feature = "gfx-backend-vulkan" -))] +#[cfg(vulkan)] impl GfxBackend for backend::Vulkan { const VARIANT: Backend = Backend::Vulkan; fn hub(global: &Global) -> &Hub { @@ -747,7 +741,7 @@ impl GfxBackend for backend::Vulkan { } } -#[cfg(any(target_os = "ios", target_os = "macos"))] +#[cfg(metal)] impl GfxBackend for backend::Metal { const VARIANT: Backend = Backend::Metal; fn hub(global: &Global) -> &Hub { @@ -758,7 +752,7 @@ impl GfxBackend for backend::Metal { } } -#[cfg(windows)] +#[cfg(dx12)] impl GfxBackend for backend::Dx12 { const VARIANT: Backend = Backend::Dx12; fn hub(global: &Global) -> &Hub { @@ -769,7 +763,7 @@ impl GfxBackend for backend::Dx12 { } } -#[cfg(windows)] +#[cfg(dx11)] impl GfxBackend for backend::Dx11 { const VARIANT: Backend = Backend::Dx11; fn hub(global: &Global) -> &Hub { diff --git a/wgpu-core/src/instance.rs b/wgpu-core/src/instance.rs index 932fbbc30..4fa27e3d3 100644 --- a/wgpu-core/src/instance.rs +++ b/wgpu-core/src/instance.rs @@ -30,16 +30,13 @@ pub type RequestAdapterOptions = wgt::RequestAdapterOptions; #[derive(Debug)] pub struct Instance { - #[cfg(any( - not(any(target_os = "ios", target_os = "macos")), - feature = "gfx-backend-vulkan" - ))] + #[cfg(vulkan)] pub vulkan: Option, - #[cfg(any(target_os = "ios", target_os = "macos"))] + #[cfg(metal)] pub metal: Option, - #[cfg(windows)] + #[cfg(dx12)] pub dx12: Option, - #[cfg(windows)] + #[cfg(dx11)] pub dx11: Option, } @@ -54,13 +51,13 @@ impl Instance { } }; Instance { - #[vulkan] + #[cfg(vulkan)] vulkan: map((Backend::Vulkan, gfx_backend_vulkan::Instance::create)), - #[metal] + #[cfg(metal)] metal: map((Backend::Metal, gfx_backend_metal::Instance::create)), - #[dx12] + #[cfg(dx12)] dx12: map((Backend::Dx12, gfx_backend_dx12::Instance::create)), - #[dx11] + #[cfg(dx11)] dx11: map((Backend::Dx11, gfx_backend_dx11::Instance::create)), } } @@ -76,13 +73,13 @@ impl Instance { } }; - #[vulkan] + #[cfg(vulkan)] map((surface.vulkan, &mut self.vulkan)), - #[metal] + #[cfg(metal)] map((surface.metal, &mut self.metal)), - #[dx12] + #[cfg(dx12)] map((surface.dx12, &mut self.dx12)), - #[dx11] + #[cfg(dx11)] map((surface.dx11, &mut self.dx11)), } } @@ -92,16 +89,13 @@ type GfxSurface = ::Surface; #[derive(Debug)] pub struct Surface { - #[cfg(any( - not(any(target_os = "ios", target_os = "macos")), - feature = "gfx-backend-vulkan" - ))] + #[cfg(vulkan)] pub vulkan: Option>, - #[cfg(any(target_os = "ios", target_os = "macos"))] + #[cfg(metal)] pub metal: Option>, - #[cfg(windows)] + #[cfg(dx12)] pub dx12: Option>, - #[cfg(windows)] + #[cfg(dx11)] pub dx11: Option>, } @@ -346,13 +340,13 @@ impl Global { }; Surface { - #[vulkan] + #[cfg(vulkan)] vulkan: map(&self.instance.vulkan), - #[metal] + #[cfg(metal)] metal: map(&self.instance.metal), - #[dx12] + #[cfg(dx12)] dx12: map(&self.instance.dx12), - #[dx11] + #[cfg(dx11)] dx11: map(&self.instance.dx11), } } @@ -389,13 +383,13 @@ impl Global { } }; - #[vulkan] + #[cfg(vulkan)] map((&instance.vulkan, Backend::Vulkan, "Vulkan", backend::Vulkan::hub)), - #[metal] + #[cfg(metal)] map((&instance.metal, Backend::Metal, "Metal", backend::Metal::hub)), - #[dx12] + #[cfg(dx12)] map((&instance.dx12, Backend::Dx12, "Dx12", backend::Dx12::hub)), - #[dx11] + #[cfg(dx11)] map((&instance.dx11, Backend::Dx11, "Dx11", backend::Dx11::hub)), } @@ -449,28 +443,28 @@ impl Global { // NB: The internal function definitions are a workaround for Rust // being weird with lifetimes for closure literals... - #[vulkan] + #[cfg(vulkan)] let adapters_vk = map((&instance.vulkan, &id_vulkan, { fn surface_vulkan(surf: &Surface) -> Option<&GfxSurface> { surf.vulkan.as_ref() } surface_vulkan })); - #[metal] + #[cfg(metal)] let adapters_mtl = map((&instance.metal, &id_metal, { fn surface_metal(surf: &Surface) -> Option<&GfxSurface> { surf.metal.as_ref() } surface_metal })); - #[dx12] + #[cfg(dx12)] let adapters_dx12 = map((&instance.dx12, &id_dx12, { fn surface_dx12(surf: &Surface) -> Option<&GfxSurface> { surf.dx12.as_ref() } surface_dx12 })); - #[dx11] + #[cfg(dx11)] let adapters_dx11 = map((&instance.dx11, &id_dx11, { fn surface_dx11(surf: &Surface) -> Option<&GfxSurface> { surf.dx11.as_ref() @@ -526,13 +520,13 @@ impl Global { selected -= adapters_backend.len(); }; - #[vulkan] + #[cfg(vulkan)] map(("Vulkan", &mut id_vulkan, adapters_vk, backend::Vulkan::hub)), - #[metal] + #[cfg(metal)] map(("Metal", &mut id_metal, adapters_mtl, backend::Metal::hub)), - #[dx12] + #[cfg(dx12)] map(("Dx12", &mut id_dx12, adapters_dx12, backend::Dx12::hub)), - #[dx11] + #[cfg(dx11)] map(("Dx11", &mut id_dx11, adapters_dx11, backend::Dx11::hub)), } diff --git a/wgpu-core/src/lib.rs b/wgpu-core/src/lib.rs index 30ea6a252..2bfbc0c3e 100644 --- a/wgpu-core/src/lib.rs +++ b/wgpu-core/src/lib.rs @@ -15,16 +15,13 @@ mod macros; pub mod backend { pub use gfx_backend_empty::Backend as Empty; - #[cfg(windows)] + #[cfg(dx11)] pub use gfx_backend_dx11::Backend as Dx11; - #[cfg(windows)] + #[cfg(dx12)] pub use gfx_backend_dx12::Backend as Dx12; - #[cfg(any(target_os = "ios", target_os = "macos"))] + #[cfg(metal)] pub use gfx_backend_metal::Backend as Metal; - #[cfg(any( - not(any(target_os = "ios", target_os = "macos")), - feature = "gfx-backend-vulkan" - ))] + #[cfg(vulkan)] pub use gfx_backend_vulkan::Backend as Vulkan; } diff --git a/wgpu-core/src/macros.rs b/wgpu-core/src/macros.rs index 4a0d0aaea..47485e6d4 100644 --- a/wgpu-core/src/macros.rs +++ b/wgpu-core/src/macros.rs @@ -2,140 +2,56 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -macro_rules! backends { - // one let statement per backend - ( - #[vulkan] let $vk_field:pat = $vk_expr:expr; - #[metal] let $mtl_field:pat = $mtl_expr:expr; - #[dx12] let $dx12_field:pat = $dx12_expr:expr; - #[dx11] let $dx11_field:pat = $dx11_expr:expr; - ) => { - #[cfg(any( - windows, - all(unix, not(any(target_os = "ios", target_os = "macos"))), - feature = "gfx-backend-vulkan", - ))] - let $vk_field = $vk_expr; - - #[cfg(any(target_os = "ios", target_os = "macos"))] - let $mtl_field = $mtl_expr; - - #[cfg(windows)] - let $dx12_field = $dx12_expr; - - #[cfg(windows)] - let $dx11_field = $dx11_expr; - }; - - // one block statement per backend - ( - #[vulkan] $vk_block:block - #[metal] $mtl_block:block - #[dx12] $dx12_block:block - #[dx11] $dx11_block:block - ) => { - #[cfg(any( - windows, - all(unix, not(any(target_os = "ios", target_os = "macos"))), - feature = "gfx-backend-vulkan", - ))] - $vk_block - - #[cfg(any(target_os = "ios", target_os = "macos"))] - $mtl_block - - #[cfg(windows)] - $dx12_block - - #[cfg(windows)] - $dx11_block - }; - - // a struct constructor with one field per backend - ( - $Struct:path { - #[vulkan] $vk_field:ident: $vk_expr:expr, - #[metal] $mtl_field:ident: $mtl_expr:expr, - #[dx12] $dx12_field:ident: $dx12_expr:expr, - #[dx11] $dx11_field:ident: $dx11_expr:expr, - } - ) => {{ - $Struct { - #[cfg(any( - windows, - all(unix, not(any(target_os = "ios", target_os = "macos"))), - feature = "gfx-backend-vulkan", - ))] - $vk_field: $vk_expr, - - #[cfg(any(target_os = "ios", target_os = "macos"))] - $mtl_field: $mtl_expr, - - #[cfg(windows)] - $dx12_field: $dx12_expr, - - #[cfg(windows)] - $dx11_field: $dx11_expr, - } - }}; -} - macro_rules! backends_map { // one let statement per backend with mapped data ( let map = |$backend:pat| $map:block; $( - #[$backend_attr:ident] let $pat:pat = map($expr:expr); + #[cfg($backend_cfg:meta)] let $pat:pat = map($expr:expr); )* ) => { - backends! { - $( - #[$backend_attr] - let $pat = { - let $backend = $expr; - $map - }; - )* - } + $( + #[cfg($backend_cfg)] + let $pat = { + let $backend = $expr; + $map + }; + )* }; // one block statement per backend with mapped data ( let map = |$backend:pat| $map:block; $( - #[$backend_attr:ident] map($expr:expr), + #[cfg($backend_cfg:meta)] map($expr:expr), )* ) => { - backends! { - $( - #[$backend_attr] - { - let $backend = $expr; - $map - } - )* - } + $( + #[cfg($backend_cfg)] + { + let $backend = $expr; + $map + } + )* }; // a struct constructor with one field per backend with mapped data ( let map = |$backend:pat| $map:block; - $Struct:path { + $Struct:ident { $( - #[$backend_attr:ident] $ident:ident : map($expr:expr), + #[cfg($backend_cfg:meta)] $ident:ident : map($expr:expr), )* } ) => { - backends! { - $Struct { - $( - #[$backend_attr] - $ident: { - let $backend = $expr; - $map - }, - )* - } + $Struct { + $( + #[cfg($backend_cfg)] + $ident: { + let $backend = $expr; + $map + }, + )* } }; } @@ -164,10 +80,10 @@ fn test_backend_macro() { let test_foo: Foo = backends_map! { let map = |init| { init - 100 }; Foo { - #[vulkan] vulkan: map(101), - #[metal] metal: map(102), - #[dx12] dx12: map(103), - #[dx11] dx11: map(104), + #[cfg(vulkan)] vulkan: map(101), + #[cfg(metal)] metal: map(102), + #[cfg(dx12)] dx12: map(103), + #[cfg(dx11)] dx11: map(104), } }; @@ -179,16 +95,16 @@ fn test_backend_macro() { vec.push((id, chr)); }; - #[vulkan] + #[cfg(vulkan)] map((test_foo.vulkan, 'a')), - #[metal] + #[cfg(metal)] map((test_foo.metal, 'b')), - #[dx12] + #[cfg(dx12)] map((test_foo.dx12, 'c')), - #[dx11] + #[cfg(dx11)] map((test_foo.dx11, 'd')), } @@ -216,86 +132,80 @@ fn test_backend_macro() { } }; - #[vulkan] + #[cfg(vulkan)] map((test_foo.vulkan, |v| v == 1, || println!("vulkan"))), - #[metal] + #[cfg(metal)] map((test_foo.metal, |v| v == 2, || println!("metal"))), - #[dx12] + #[cfg(dx12)] map((test_foo.dx12, |v| v == 3, || println!("dx12"))), - #[dx11] + #[cfg(dx11)] map((test_foo.dx11, |v| v == 4, || println!("dx11"))), } // test struct construction 2 - let test_foo_2: Foo = backends! { - Foo { - #[vulkan] - vulkan: 1, + let test_foo_2: Foo = Foo { + #[cfg(vulkan)] + vulkan: 1, - #[metal] - metal: 2, + #[cfg(metal)] + metal: 2, - #[dx12] - dx12: 3, + #[cfg(dx12)] + dx12: 3, - #[dx11] - dx11: 4, - } + #[cfg(dx11)] + dx11: 4, }; - backends! { - #[vulkan] - let var_vulkan = test_foo_2.vulkan; + #[cfg(vulkan)] + let var_vulkan = test_foo_2.vulkan; - #[metal] - let var_metal = test_foo_2.metal; + #[cfg(metal)] + let var_metal = test_foo_2.metal; - #[dx12] - let var_dx12 = test_foo_2.dx12; + #[cfg(dx12)] + let var_dx12 = test_foo_2.dx12; - #[dx11] - let var_dx11 = test_foo_2.dx11; - } + #[cfg(dx11)] + let var_dx11 = test_foo_2.dx11; backends_map! { let map = |(id, chr, var)| { (chr, id, var) }; - #[vulkan] + #[cfg(vulkan)] let var_vulkan = map((test_foo_2.vulkan, 'a', var_vulkan)); - #[metal] + #[cfg(metal)] let var_metal = map((test_foo_2.metal, 'b', var_metal)); - #[dx12] + #[cfg(dx12)] let var_dx12 = map((test_foo_2.dx12, 'c', var_dx12)); - #[dx11] + #[cfg(dx11)] let var_dx11 = map((test_foo_2.dx11, 'd', var_dx11)); } - backends! { - #[vulkan] - { - println!("backend int: {:?}", var_vulkan); - } + #[cfg(vulkan)] + { + println!("backend int: {:?}", var_vulkan); + } - #[metal] - { - println!("backend int: {:?}", var_metal); - } + #[cfg(metal)] + { + println!("backend int: {:?}", var_metal); + } - #[dx12] - { - println!("backend int: {:?}", var_dx12); - } + #[cfg(dx12)] + { + println!("backend int: {:?}", var_dx12); + } - #[dx11] - { - println!("backend int: {:?}", var_dx11); - } + #[cfg(dx11)] + { + println!("backend int: {:?}", var_dx11); } #[cfg(any(