From fc33169c39bff98e1ee8f287aca04bdcac0d03dc Mon Sep 17 00:00:00 2001 From: hakolao Date: Thu, 17 Feb 2022 13:46:22 +0200 Subject: [PATCH 1/4] Fix game of life on macos --- examples/src/bin/multi_window_game_of_life/vulkano_window.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/src/bin/multi_window_game_of_life/vulkano_window.rs b/examples/src/bin/multi_window_game_of_life/vulkano_window.rs index 7a652122a4..77f08bc118 100644 --- a/examples/src/bin/multi_window_game_of_life/vulkano_window.rs +++ b/examples/src/bin/multi_window_game_of_life/vulkano_window.rs @@ -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(), From c513aeca47b1240982a73f7fb0d499594a8554b2 Mon Sep 17 00:00:00 2001 From: hakolao Date: Mon, 28 Mar 2022 14:27:41 +0300 Subject: [PATCH 2/4] Exract ca metal layer setting to its own function --- vulkano-win/src/raw_window_handle.rs | 5 ++++- vulkano-win/src/winit.rs | 14 ++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/vulkano-win/src/raw_window_handle.rs b/vulkano-win/src/raw_window_handle.rs index 015fdd3463..93a05f5097 100644 --- a/vulkano-win/src/raw_window_handle.rs +++ b/vulkano-win/src/raw_window_handle.rs @@ -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. You can do that by calling `vulkano_win::set_ca_metal_layer_to_winit` +/// before using this function. pub fn create_surface_from_handle( window: W, instance: Arc, diff --git a/vulkano-win/src/winit.rs b/vulkano-win/src/winit.rs index 61380d6abe..5ccd779d71 100644 --- a/vulkano-win/src/winit.rs +++ b/vulkano-win/src/winit.rs @@ -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>( - instance: Arc, - win: W, -) -> Result>, SurfaceCreationError> { +pub unsafe fn set_ca_metal_layer_to_winit>(win: W) { use winit::platform::macos::WindowExtMacOS; let wnd: cocoa_id = mem::transmute(win.borrow().ns_window()); @@ -191,7 +190,14 @@ unsafe fn winit_to_surface>( 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>( + instance: Arc, + win: W, +) -> Result>, SurfaceCreationError> { + set_ca_metal_layer_to_winit(win.borrow()); Surface::from_mac_os(instance, win.borrow().ns_view() as *const (), win) } From 41c4e5376fd3fa6a1e66beba91a061774dbc7d17 Mon Sep 17 00:00:00 2001 From: hakolao Date: Mon, 28 Mar 2022 14:31:23 +0300 Subject: [PATCH 3/4] Fix trait use mistake --- vulkano-win/src/winit.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vulkano-win/src/winit.rs b/vulkano-win/src/winit.rs index 5ccd779d71..47502bed5e 100644 --- a/vulkano-win/src/winit.rs +++ b/vulkano-win/src/winit.rs @@ -197,6 +197,8 @@ unsafe fn winit_to_surface>( instance: Arc, win: W, ) -> Result>, 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) } From 0152ca62963f90a9e723918314461a104c91e425 Mon Sep 17 00:00:00 2001 From: hakolao Date: Mon, 28 Mar 2022 16:26:20 +0300 Subject: [PATCH 4/4] Make ca metal layer setting private & update doc to be non-winit specific --- vulkano-win/src/raw_window_handle.rs | 4 ++-- vulkano-win/src/winit.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/vulkano-win/src/raw_window_handle.rs b/vulkano-win/src/raw_window_handle.rs index 93a05f5097..5c55d8c848 100644 --- a/vulkano-win/src/raw_window_handle.rs +++ b/vulkano-win/src/raw_window_handle.rs @@ -7,8 +7,8 @@ use vulkano::swapchain::SurfaceCreationError; /// Creates a vulkan surface from a generic window /// 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. You can do that by calling `vulkano_win::set_ca_metal_layer_to_winit` -/// before using this function. +/// `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( window: W, instance: Arc, diff --git a/vulkano-win/src/winit.rs b/vulkano-win/src/winit.rs index 47502bed5e..0fc905a30a 100644 --- a/vulkano-win/src/winit.rs +++ b/vulkano-win/src/winit.rs @@ -175,7 +175,7 @@ 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")] -pub unsafe fn set_ca_metal_layer_to_winit>(win: W) { +unsafe fn set_ca_metal_layer_to_winit>(win: W) { use winit::platform::macos::WindowExtMacOS; let wnd: cocoa_id = mem::transmute(win.borrow().ns_window());