Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix game of life example on macos #1829

Merged
merged 4 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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. You can do that by calling `vulkano_win::set_ca_metal_layer_to_winit`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create_surface_from_handle is not used with Winit, and it doesn't make sense to tell the user to use a function that requires Winit, if they aren't using Winit.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I updated once more...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although... One could if they want to retain the ownership of the window else where? Though that's probably not necessary...

/// before using this function.
pub fn create_surface_from_handle<W>(
window: W,
instance: Arc<Instance>,
Expand Down
14 changes: 10 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> {
pub 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,14 @@ 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> {
set_ca_metal_layer_to_winit(win.borrow());
Surface::from_mac_os(instance, win.borrow().ns_view() as *const (), win)
}

Expand Down