Skip to content

Commit

Permalink
Fix game of life example on macos (#1829)
Browse files Browse the repository at this point in the history
  • Loading branch information
hakolao authored Mar 28, 2022
1 parent 407d99a commit d1ab3f6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
4 changes: 2 additions & 2 deletions examples/src/bin/multi_window_game_of_life/vulkano_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(),
Expand Down
5 changes: 4 additions & 1 deletion vulkano-win/src/raw_window_handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
16 changes: 12 additions & 4 deletions vulkano-win/src/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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)
}

Expand Down

0 comments on commit d1ab3f6

Please sign in to comment.