diff --git a/ash-window/src/lib.rs b/ash-window/src/lib.rs index d6cb0f301..a43933dfb 100644 --- a/ash-window/src/lib.rs +++ b/ash-window/src/lib.rs @@ -2,7 +2,7 @@ use std::os::raw::c_char; -use ash::{extensions::khr, prelude::*, vk, Entry, Instance}; +use ash::{extensions::ext, extensions::khr, prelude::*, vk, Entry, Instance}; use raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; #[cfg(any(target_os = "macos", target_os = "ios"))] @@ -118,6 +118,59 @@ pub unsafe fn create_surface( } } +/// Extensions necessary for creating a surface on windows. +#[allow(dead_code)] +const WINDOWS_SURFACE_EXTENSIONS: &'static [*const c_char] = &[ + khr::Surface::name().as_ptr(), + khr::Win32Surface::name().as_ptr(), +]; +/// Extensions necessary for creating a surface on unix. +/// Note that this is not equal to the return value of [`enumerate_required_extensions`] on Unix due to the multiple window types. +#[allow(dead_code)] +const UNIX_SURFACE_EXTENSIONS: &'static [*const c_char] = &[ + khr::Surface::name().as_ptr(), + khr::WaylandSurface::name().as_ptr(), + khr::XlibSurface::name().as_ptr(), + khr::XcbSurface::name().as_ptr(), +]; +/// Extensions necessary for creating a surface on android. +#[allow(dead_code)] +const ANDROID_SURFACE_EXTENSIONS: &'static [*const c_char] = &[ + khr::Surface::name().as_ptr(), + khr::AndroidSurface::name().as_ptr(), +]; +/// Extensions necessary for creating a surface on macos. +#[allow(dead_code)] +const MACOS_SURFACE_EXTENSIONS: &'static [*const c_char] = &[ + khr::Surface::name().as_ptr(), + ext::MetalSurface::name().as_ptr(), +]; +/// Extensions necessary for creating a surface on ios. +#[allow(dead_code)] +const IOS_SURFACE_EXTENSIONS: &'static [*const c_char] = MACOS_SURFACE_EXTENSIONS; + +/// Extensions necessary for creating a surface on the current target platform. +/// (Note that on Unix, this is not equal to the return value of [`enumerate_required_extensions`]) +pub const TARGET_EXTENSIONS: &'static [*const c_char] = { + #[cfg(target_os = "windows")] + let out = WINDOWS_SURFACE_EXTENSIONS; + #[cfg(any( + target_os = "linux", + target_os = "dragonfly", + target_os = "freebsd", + target_os = "netbsd", + target_os = "openbsd" + ))] + let out = UNIX_SURFACE_EXTENSIONS; + #[cfg(target_os = "android")] + let out = ANDROID_SURFACE_EXTENSIONS; + #[cfg(target_os = "macos")] + let out = MACOS_SURFACE_EXTENSIONS; + #[cfg(target_os = "ios")] + let out = IOS_SURFACE_EXTENSIONS; + out +}; + /// Query the required instance extensions for creating a surface from a window handle. /// /// The returned extensions will include all extension dependencies. @@ -126,13 +179,7 @@ pub fn enumerate_required_extensions( ) -> VkResult<&'static [*const c_char]> { let extensions = match window_handle.raw_window_handle() { #[cfg(target_os = "windows")] - RawWindowHandle::Windows(_) => { - const WINDOWS_EXTS: [*const c_char; 2] = [ - khr::Surface::name().as_ptr(), - khr::Win32Surface::name().as_ptr(), - ]; - &WINDOWS_EXTS - } + RawWindowHandle::Windows(_) => WINDOWS_SURFACE_EXTENSIONS, #[cfg(any( target_os = "linux", @@ -179,32 +226,14 @@ pub fn enumerate_required_extensions( &XCB_EXTS } - #[cfg(any(target_os = "android"))] - RawWindowHandle::Android(_) => { - const ANDROID_EXTS: [*const c_char; 2] = [ - khr::Surface::name().as_ptr(), - khr::AndroidSurface::name().as_ptr(), - ]; - &ANDROID_EXTS - } + #[cfg(target_os = "android")] + RawWindowHandle::Android(_) => ANDROID_SURFACE_EXTENSIONS, - #[cfg(any(target_os = "macos"))] - RawWindowHandle::MacOS(_) => { - const MACOS_EXTS: [*const c_char; 2] = [ - khr::Surface::name().as_ptr(), - ext::MetalSurface::name().as_ptr(), - ]; - &MACOS_EXTS - } + #[cfg(target_os = "macos")] + RawWindowHandle::MacOS(_) => METAL_SURFACE_EXTENSIONS, - #[cfg(any(target_os = "ios"))] - RawWindowHandle::IOS(_) => { - const IOS_EXTS: [*const c_char; 2] = [ - khr::Surface::name().as_ptr(), - ext::MetalSurface::name().as_ptr(), - ]; - &IOS_EXTS - } + #[cfg(target_os = "ios")] + RawWindowHandle::IOS(_) => IOS_SURFACE_EXTENSIONS, _ => return Err(vk::Result::ERROR_EXTENSION_NOT_PRESENT), };