Fix game of life example on macos (#1829)

This commit is contained in:
Okko Hakola 2022-03-28 16:28:51 +03:00 committed by GitHub
parent 407d99a4a5
commit d1ab3f6726
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 7 deletions

View File

@ -23,7 +23,7 @@ use vulkano::{
sync,
sync::{FlushError, GpuFuture},
};
use vulkano_win::create_surface_from_handle;
use vulkano_win::create_surface_from_winit;
use winit::window::Window;
pub struct VulkanoWindow {
@ -52,7 +52,7 @@ impl VulkanoWindow {
vsync: bool,
) -> VulkanoWindow {
// Create rendering surface from window
let surface = create_surface_from_handle(window, vulkano_context.instance()).unwrap();
let surface = create_surface_from_winit(window, vulkano_context.instance()).unwrap();
// Create swap chain & frame(s) to which we'll render
let (swapchain, final_views) = vulkano_context.create_swapchain(
surface.clone(),

View File

@ -5,7 +5,10 @@ use vulkano::swapchain::Surface;
use vulkano::swapchain::SurfaceCreationError;
/// Creates a vulkan surface from a generic window
/// which implements HasRawWindowHandle and thus can reveal the os-dependent handle
/// which implements HasRawWindowHandle and thus can reveal the os-dependent handle.
/// - Note that if you wish to use this function with MacOS, you will need to ensure that the
/// `CAMetalLayer` is set to the ns_view. An example of how one might do that can be found in
/// `vulkano_win::set_ca_metal_layer_to_winit`
pub fn create_surface_from_handle<W>(
window: W,
instance: Arc<Instance>,

View File

@ -172,11 +172,10 @@ use objc::runtime::YES;
#[cfg(target_os = "macos")]
use std::mem;
/// Ensure `CAMetalLayer` (native rendering surface on MacOs) is used by the ns_view.
/// This is necessary to be able to render on Mac.
#[cfg(target_os = "macos")]
unsafe fn winit_to_surface<W: SafeBorrow<Window>>(
instance: Arc<Instance>,
win: W,
) -> Result<Arc<Surface<W>>, SurfaceCreationError> {
unsafe fn set_ca_metal_layer_to_winit<W: SafeBorrow<Window>>(win: W) {
use winit::platform::macos::WindowExtMacOS;
let wnd: cocoa_id = mem::transmute(win.borrow().ns_window());
@ -191,7 +190,16 @@ unsafe fn winit_to_surface<W: SafeBorrow<Window>>(
layer.set_contents_scale(view.backingScaleFactor());
view.setLayer(mem::transmute(layer.as_ref())); // Bombs here with out of memory
view.setWantsLayer(YES);
}
#[cfg(target_os = "macos")]
unsafe fn winit_to_surface<W: SafeBorrow<Window>>(
instance: Arc<Instance>,
win: W,
) -> Result<Arc<Surface<W>>, SurfaceCreationError> {
use winit::platform::macos::WindowExtMacOS;
set_ca_metal_layer_to_winit(win.borrow());
Surface::from_mac_os(instance, win.borrow().ns_view() as *const (), win)
}