From fe89c5283496a923828bda593eb9529d35016922 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Sat, 7 Sep 2019 21:26:28 -0400 Subject: [PATCH] Make Vulkan optional on Apple --- .travis.yml | 1 + wgpu-native/Cargo.toml | 6 +++++- wgpu-native/src/hub.rs | 2 ++ wgpu-native/src/instance.rs | 11 +++++++++-- wgpu-native/src/lib.rs | 2 ++ 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index f0c1aac09..6157b297b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -59,6 +59,7 @@ script: - cargo test # TODO: enable GL backend #- (cd wgpu-native && cargo check --features local,glutin) + - if [[ $TRAVIS_OS_NAME == "osx" ]]; then (cd wgpu-native && cargo check --features gfx-backend-vulkan); fi - if [[ $TRAVIS_OS_NAME == "linux" ]]; then cargo check --release; fi - if [[ $TRAVIS_RUST_VERSION == "nightly" ]]; then cargo +nightly install cbindgen; fi - if [[ $TRAVIS_RUST_VERSION == "nightly" ]] && [[ $TRAVIS_OS_NAME == "windows" ]]; then diff --git a/wgpu-native/Cargo.toml b/wgpu-native/Cargo.toml index dbc61f855..a57f8d4b7 100644 --- a/wgpu-native/Cargo.toml +++ b/wgpu-native/Cargo.toml @@ -30,7 +30,6 @@ lazy_static = "1.1.0" log = "0.4" hal = { package = "gfx-hal", git = "https://github.com/gfx-rs/gfx", rev = "a084a1f2fec6c9ed928c53e2dc8c1761782b9019" } gfx-backend-empty = { git = "https://github.com/gfx-rs/gfx", rev = "a084a1f2fec6c9ed928c53e2dc8c1761782b9019" } -gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", rev = "a084a1f2fec6c9ed928c53e2dc8c1761782b9019", features = ["x11"] } parking_lot = "0.9" raw-window-handle = "0.1" rendy-memory = { git = "https://github.com/amethyst/rendy", rev = "a8ac0de977a28d09592d615f8857461622833443" } @@ -40,7 +39,12 @@ vec_map = "0.8" [target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies] gfx-backend-metal = { git = "https://github.com/gfx-rs/gfx", rev = "a084a1f2fec6c9ed928c53e2dc8c1761782b9019" } +gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", rev = "a084a1f2fec6c9ed928c53e2dc8c1761782b9019", optional = true } + +[target.'cfg(all(unix, not(target_os = "ios"), not(target_os = "macos")))'.dependencies] +gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", rev = "a084a1f2fec6c9ed928c53e2dc8c1761782b9019", features = ["x11"] } [target.'cfg(windows)'.dependencies] gfx-backend-dx12 = { git = "https://github.com/gfx-rs/gfx", rev = "a084a1f2fec6c9ed928c53e2dc8c1761782b9019" } gfx-backend-dx11 = { git = "https://github.com/gfx-rs/gfx", rev = "a084a1f2fec6c9ed928c53e2dc8c1761782b9019" } +gfx-backend-vulkan = { git = "https://github.com/gfx-rs/gfx", rev = "a084a1f2fec6c9ed928c53e2dc8c1761782b9019" } diff --git a/wgpu-native/src/hub.rs b/wgpu-native/src/hub.rs index ca42563bc..3477e5ef6 100644 --- a/wgpu-native/src/hub.rs +++ b/wgpu-native/src/hub.rs @@ -378,6 +378,7 @@ impl Default for Hub { #[derive(Debug, Default)] pub struct Hubs { + #[cfg(any(not(any(target_os = "ios", target_os = "macos")), feature = "gfx-backend-vulkan"))] vulkan: Hub, #[cfg(any(target_os = "ios", target_os = "macos"))] metal: Hub, @@ -408,6 +409,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"))] impl GfxBackend for backend::Vulkan { const VARIANT: Backend = Backend::Vulkan; fn hub() -> &'static Hub { diff --git a/wgpu-native/src/instance.rs b/wgpu-native/src/instance.rs index 883c1c217..d7d7d76cd 100644 --- a/wgpu-native/src/instance.rs +++ b/wgpu-native/src/instance.rs @@ -32,6 +32,7 @@ use std::marker::PhantomData; #[derive(Debug)] pub struct Instance { + #[cfg(any(not(any(target_os = "ios", target_os = "macos")), feature = "gfx-backend-vulkan"))] vulkan: Option, #[cfg(any(target_os = "ios", target_os = "macos"))] metal: gfx_backend_metal::Instance, @@ -44,7 +45,7 @@ pub struct Instance { impl Instance { pub(crate) fn new(name: &str, version: u32) -> Self { Instance { - //TODO: reconsider once `create` returns a `Result` + #[cfg(any(not(any(target_os = "ios", target_os = "macos")), feature = "gfx-backend-vulkan"))] vulkan: gfx_backend_vulkan::Instance::create(name, version).ok(), #[cfg(any(target_os = "ios", target_os = "macos"))] metal: gfx_backend_metal::Instance::create(name, version).unwrap(), @@ -62,6 +63,7 @@ type GfxSurface = ::Surface; pub struct Surface { pub(crate) swap_chain: Option, pub(crate) ref_count: RefCount, + #[cfg(any(not(any(target_os = "ios", target_os = "macos")), feature = "gfx-backend-vulkan"))] pub(crate) vulkan: Option>, #[cfg(any(target_os = "ios", target_os = "macos"))] pub(crate) metal: GfxSurface, @@ -167,6 +169,7 @@ pub fn wgpu_create_surface(raw_handle: raw_window_handle::RawWindowHandle) -> Su Rwh::IOS(h) => Surface { swap_chain: None, ref_count, + #[cfg(feature = "gfx-backend-vulkan")] vulkan: None, metal: instance .metal @@ -176,6 +179,7 @@ pub fn wgpu_create_surface(raw_handle: raw_window_handle::RawWindowHandle) -> Su Rwh::MacOS(h) => Surface { swap_chain: None, ref_count, + #[cfg(feature = "gfx-backend-vulkan")] vulkan: instance .vulkan .as_ref() @@ -245,6 +249,7 @@ pub extern "C" fn wgpu_create_surface_from_metal_layer(layer: *mut std::ffi::c_v let surface = Surface { swap_chain: None, ref_count: LifeGuard::new().ref_count, + #[cfg(feature = "gfx-backend-vulkan")] vulkan: None, //TODO: currently requires `NSView` metal: GLOBAL .instance @@ -296,6 +301,7 @@ pub fn request_adapter( let id_dx12 = find_input(Backend::Dx12); let id_dx11 = find_input(Backend::Dx11); + #[cfg(any(not(any(target_os = "ios", target_os = "macos")), feature = "gfx-backend-vulkan"))] let mut adapters_vk = match instance.vulkan { Some(ref inst) if id_vulkan.is_some() => { let adapters = inst.enumerate_adapters(); @@ -361,6 +367,7 @@ pub fn request_adapter( let mut token = Token::root(); let mut selected = preferred_gpu.unwrap_or(0); + #[cfg(any(not(any(target_os = "ios", target_os = "macos")), feature = "gfx-backend-vulkan"))] { if selected < adapters_vk.len() { let adapter = Adapter { @@ -421,7 +428,7 @@ pub fn request_adapter( } selected -= adapters_dx11.len(); } - let _ = (selected, id_metal, id_dx12, id_dx11); + let _ = (selected, id_vulkan, id_metal, id_dx12, id_dx11); None } diff --git a/wgpu-native/src/lib.rs b/wgpu-native/src/lib.rs index de10e2a27..fc7ecf908 100644 --- a/wgpu-native/src/lib.rs +++ b/wgpu-native/src/lib.rs @@ -6,6 +6,7 @@ pub mod backend { pub use gfx_backend_empty::Backend as Empty; #[cfg(any(target_os = "ios", target_os = "macos"))] pub use gfx_backend_metal::Backend as Metal; + #[cfg(any(not(any(target_os = "ios", target_os = "macos")), feature = "gfx-backend-vulkan"))] pub use gfx_backend_vulkan::Backend as Vulkan; } @@ -202,6 +203,7 @@ pub enum InputState {} macro_rules! gfx_select { ($id:expr => $function:ident( $($param:expr),+ )) => { match $id.backend() { + #[cfg(any(not(any(target_os = "ios", target_os = "macos")), feature = "gfx-backend-vulkan"))] $crate::Backend::Vulkan => $function::<$crate::backend::Vulkan>( $($param),+ ), #[cfg(any(target_os = "ios", target_os = "macos"))] $crate::Backend::Metal => $function::<$crate::backend::Metal>( $($param),+ ),