Update example to use ash-window

This commit is contained in:
Adrien Bennadji 2020-09-03 09:07:09 +02:00 committed by Benjamin Saunders
parent 00c7eea860
commit df504f8121
2 changed files with 8 additions and 138 deletions

View file

@ -8,11 +8,4 @@ edition = "2018"
winit = "0.19.5"
image = "0.10.4"
ash = { path = "../ash" }
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3.4", features = ["windef", "winuser"] }
[target.'cfg(target_os = "macos")'.dependencies]
metal = "0.17.1"
cocoa = "0.20.0"
objc = "0.2.7"
ash-window = { path = "../ash-window" }

View file

@ -1,36 +1,11 @@
extern crate ash;
#[cfg(target_os = "windows")]
extern crate winapi;
#[cfg(target_os = "macos")]
extern crate cocoa;
#[cfg(target_os = "macos")]
extern crate metal;
#[cfg(target_os = "macos")]
extern crate objc;
extern crate winit;
#[cfg(target_os = "macos")]
use cocoa::appkit::{NSView, NSWindow};
#[cfg(target_os = "macos")]
use cocoa::base::id as cocoa_id;
#[cfg(target_os = "macos")]
use metal::CoreAnimationLayer;
#[cfg(target_os = "macos")]
use objc::runtime::YES;
#[cfg(target_os = "macos")]
use std::mem;
#[cfg(all(unix, not(target_os = "android"), not(target_os = "macos")))]
use ash::extensions::khr::XlibSurface;
use ash::extensions::{
ext::DebugUtils,
khr::{Surface, Swapchain},
};
#[cfg(target_os = "windows")]
use ash::extensions::khr::Win32Surface;
#[cfg(target_os = "macos")]
use ash::extensions::mvk::MacOSSurface;
pub use ash::version::{DeviceV1_0, EntryV1_0, InstanceV1_0};
use ash::{vk, Device, Entry, Instance};
use std::borrow::Cow;
@ -101,109 +76,6 @@ pub fn record_submit_commandbuffer<D: DeviceV1_0, F: FnOnce(&D, vk::CommandBuffe
}
}
#[cfg(all(unix, not(target_os = "android"), not(target_os = "macos")))]
unsafe fn create_surface<E: EntryV1_0, I: InstanceV1_0>(
entry: &E,
instance: &I,
window: &winit::Window,
) -> Result<vk::SurfaceKHR, vk::Result> {
use winit::os::unix::WindowExt;
let x11_display = window.get_xlib_display().unwrap();
let x11_window = window.get_xlib_window().unwrap();
let x11_create_info = vk::XlibSurfaceCreateInfoKHR::builder()
.window(x11_window)
.dpy(x11_display as *mut vk::Display);
let xlib_surface_loader = XlibSurface::new(entry, instance);
xlib_surface_loader.create_xlib_surface(&x11_create_info, None)
}
#[cfg(target_os = "macos")]
unsafe fn create_surface<E: EntryV1_0, I: InstanceV1_0>(
entry: &E,
instance: &I,
window: &winit::Window,
) -> Result<vk::SurfaceKHR, vk::Result> {
use std::ptr;
use winit::os::macos::WindowExt;
let wnd: cocoa_id = mem::transmute(window.get_nswindow());
let layer = CoreAnimationLayer::new();
layer.set_edge_antialiasing_mask(0);
layer.set_presents_with_transaction(false);
layer.remove_all_animations();
let view = wnd.contentView();
layer.set_contents_scale(view.backingScaleFactor());
view.setLayer(mem::transmute(layer.as_ref()));
view.setWantsLayer(YES);
let create_info = vk::MacOSSurfaceCreateInfoMVK {
s_type: vk::StructureType::MACOS_SURFACE_CREATE_INFO_M,
p_next: ptr::null(),
flags: Default::default(),
p_view: window.get_nsview() as *const c_void,
};
let macos_surface_loader = MacOSSurface::new(entry, instance);
macos_surface_loader.create_mac_os_surface_mvk(&create_info, None)
}
#[cfg(target_os = "windows")]
unsafe fn create_surface<E: EntryV1_0, I: InstanceV1_0>(
entry: &E,
instance: &I,
window: &winit::Window,
) -> Result<vk::SurfaceKHR, vk::Result> {
use std::ffi::c_void;
use std::ptr;
use winapi::shared::windef::HWND;
use winapi::um::libloaderapi::GetModuleHandleW;
use winit::os::windows::WindowExt;
let hwnd = window.get_hwnd() as HWND;
let hinstance = GetModuleHandleW(ptr::null()) as *const c_void;
let win32_create_info = vk::Win32SurfaceCreateInfoKHR {
s_type: vk::StructureType::WIN32_SURFACE_CREATE_INFO_KHR,
p_next: ptr::null(),
flags: Default::default(),
hinstance,
hwnd: hwnd as *const c_void,
};
let win32_surface_loader = Win32Surface::new(entry, instance);
win32_surface_loader.create_win32_surface(&win32_create_info, None)
}
#[cfg(all(unix, not(target_os = "android"), not(target_os = "macos")))]
fn extension_names() -> Vec<*const i8> {
vec![
Surface::name().as_ptr(),
XlibSurface::name().as_ptr(),
DebugUtils::name().as_ptr(),
]
}
#[cfg(target_os = "macos")]
fn extension_names() -> Vec<*const i8> {
vec![
Surface::name().as_ptr(),
MacOSSurface::name().as_ptr(),
DebugUtils::name().as_ptr(),
]
}
#[cfg(all(windows))]
fn extension_names() -> Vec<*const i8> {
vec![
Surface::name().as_ptr(),
Win32Surface::name().as_ptr(),
DebugUtils::name().as_ptr(),
]
}
unsafe extern "system" fn vulkan_debug_callback(
message_severity: vk::DebugUtilsMessageSeverityFlagsEXT,
message_type: vk::DebugUtilsMessageTypeFlagsEXT,
@ -350,7 +222,12 @@ impl ExampleBase {
.map(|raw_name| raw_name.as_ptr())
.collect();
let extension_names_raw = extension_names();
let surface_extensions = ash_window::enumerate_required_extensions(&window).unwrap();
let mut extension_names_raw = surface_extensions
.iter()
.map(|ext| ext.as_ptr())
.collect::<Vec<_>>();
extension_names_raw.push(DebugUtils::name().as_ptr());
let appinfo = vk::ApplicationInfo::builder()
.application_name(&app_name)
@ -381,7 +258,7 @@ impl ExampleBase {
let debug_call_back = debug_utils_loader
.create_debug_utils_messenger(&debug_info, None)
.unwrap();
let surface = create_surface(&entry, &instance, &window).unwrap();
let surface = ash_window::create_surface(&entry, &instance, &window, None).unwrap();
let pdevices = instance
.enumerate_physical_devices()
.expect("Physical device error");