Skip to content

Commit

Permalink
ash-window: Upgrade to raw-window-handle 0.4.2
Browse files Browse the repository at this point in the history
The match arms are not guarded by `cfg` anymore, allowing us to
compile-test these simple arms on every system whenever our Ash
extension helpers and types are not guarded by `cfg` attributes either.
This applies to every platform except Mac/IOS where the symbols and
external raw-window-metal crate are themselves guarded by cfg's.
  • Loading branch information
MarijnS95 committed Dec 1, 2021
1 parent 0cddbad commit 9e69591
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 61 deletions.
2 changes: 1 addition & 1 deletion ash-window/Cargo.toml
Expand Up @@ -15,7 +15,7 @@ workspace = ".."

[dependencies]
ash = { path = "../ash", version = "0.33" }
raw-window-handle = "0.3"
raw-window-handle = "0.4.2"

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
raw-window-metal = "0.1"
Expand Down
74 changes: 14 additions & 60 deletions ash-window/src/lib.rs
Expand Up @@ -21,22 +21,14 @@ pub unsafe fn create_surface(
allocation_callbacks: Option<&vk::AllocationCallbacks>,
) -> VkResult<vk::SurfaceKHR> {
match window_handle.raw_window_handle() {
#[cfg(target_os = "windows")]
RawWindowHandle::Windows(handle) => {
RawWindowHandle::Win32(handle) => {
let surface_desc = vk::Win32SurfaceCreateInfoKHR::builder()
.hinstance(handle.hinstance)
.hwnd(handle.hwnd);
let surface_fn = khr::Win32Surface::new(entry, instance);
surface_fn.create_win32_surface(&surface_desc, allocation_callbacks)
}

#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
RawWindowHandle::Wayland(handle) => {
let surface_desc = vk::WaylandSurfaceCreateInfoKHR::builder()
.display(handle.display)
Expand All @@ -45,13 +37,6 @@ pub unsafe fn create_surface(
surface_fn.create_wayland_surface(&surface_desc, allocation_callbacks)
}

#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
RawWindowHandle::Xlib(handle) => {
let surface_desc = vk::XlibSurfaceCreateInfoKHR::builder()
.dpy(handle.display as *mut _)
Expand All @@ -60,13 +45,6 @@ pub unsafe fn create_surface(
surface_fn.create_xlib_surface(&surface_desc, allocation_callbacks)
}

#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
RawWindowHandle::Xcb(handle) => {
let surface_desc = vk::XcbSurfaceCreateInfoKHR::builder()
.connection(handle.connection as *mut _)
Expand All @@ -75,16 +53,15 @@ pub unsafe fn create_surface(
surface_fn.create_xcb_surface(&surface_desc, allocation_callbacks)
}

#[cfg(any(target_os = "android"))]
RawWindowHandle::Android(handle) => {
RawWindowHandle::AndroidNdk(handle) => {
let surface_desc =
vk::AndroidSurfaceCreateInfoKHR::builder().window(handle.a_native_window as _);
let surface_fn = khr::AndroidSurface::new(entry, instance);
surface_fn.create_android_surface(&surface_desc, allocation_callbacks)
}

#[cfg(any(target_os = "macos"))]
RawWindowHandle::MacOS(handle) => {
#[cfg(target_os = "macos")]
RawWindowHandle::AppKit(handle) => {
use raw_window_metal::{macos, Layer};

let layer = match macos::metal_layer_from_handle(handle) {
Expand All @@ -97,8 +74,8 @@ pub unsafe fn create_surface(
surface_fn.create_metal_surface(&surface_desc, allocation_callbacks)
}

#[cfg(any(target_os = "ios"))]
RawWindowHandle::IOS(handle) => {
#[cfg(target_os = "ios")]
RawWindowHandle::UiKit(handle) => {
use raw_window_metal::{ios, Layer};

let layer = match ios::metal_layer_from_handle(handle) {
Expand All @@ -111,7 +88,7 @@ pub unsafe fn create_surface(
surface_fn.create_metal_surface(&surface_desc, allocation_callbacks)
}

_ => Err(vk::Result::ERROR_EXTENSION_NOT_PRESENT), // not supported
_ => Err(vk::Result::ERROR_EXTENSION_NOT_PRESENT),
}
}

Expand All @@ -122,44 +99,21 @@ pub fn enumerate_required_extensions(
window_handle: &dyn HasRawWindowHandle,
) -> VkResult<Vec<&'static CStr>> {
let extensions = match window_handle.raw_window_handle() {
#[cfg(target_os = "windows")]
RawWindowHandle::Windows(_) => vec![khr::Surface::name(), khr::Win32Surface::name()],

#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
RawWindowHandle::Win32(_) => vec![khr::Surface::name(), khr::Win32Surface::name()],

RawWindowHandle::Wayland(_) => vec![khr::Surface::name(), khr::WaylandSurface::name()],

#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
RawWindowHandle::Xlib(_) => vec![khr::Surface::name(), khr::XlibSurface::name()],

#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
RawWindowHandle::Xcb(_) => vec![khr::Surface::name(), khr::XcbSurface::name()],

#[cfg(any(target_os = "android"))]
RawWindowHandle::Android(_) => vec![khr::Surface::name(), khr::AndroidSurface::name()],
RawWindowHandle::AndroidNdk(_) => vec![khr::Surface::name(), khr::AndroidSurface::name()],

#[cfg(any(target_os = "macos"))]
RawWindowHandle::MacOS(_) => vec![khr::Surface::name(), ext::MetalSurface::name()],
#[cfg(target_os = "macos")]
RawWindowHandle::AppKit(_) => vec![khr::Surface::name(), ext::MetalSurface::name()],

#[cfg(any(target_os = "ios"))]
RawWindowHandle::IOS(_) => vec![khr::Surface::name(), ext::MetalSurface::name()],
#[cfg(target_os = "ios")]
RawWindowHandle::UiKit(_) => vec![khr::Surface::name(), ext::MetalSurface::name()],

_ => return Err(vk::Result::ERROR_EXTENSION_NOT_PRESENT),
};
Expand Down

0 comments on commit 9e69591

Please sign in to comment.