This commit is contained in:
Richard Knight 2022-08-26 09:12:16 +09:00 committed by GitHub
commit d9a8b03354
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 71 additions and 31 deletions

View File

@ -281,6 +281,20 @@ GLFWAPI CGDirectDisplayID glfwGetCocoaMonitor(GLFWmonitor* monitor);
* @ingroup native * @ingroup native
*/ */
GLFWAPI id glfwGetCocoaWindow(GLFWwindow* window); GLFWAPI id glfwGetCocoaWindow(GLFWwindow* window);
/*! @brief Returns the `CAMetalLayer` for the specified window.
*
* @return The `CAMetalLayer` of the specified window, or `nil` if an
* [error](@ref error_handling) occurred.
*
* @thread_safety This function may be called from any thread. Access is not
* synchronized.
*
* @since Added in version 3.0.
*
* @ingroup native
*/
GLFWAPI id glfwGetMetalLayer(GLFWwindow* window);
#endif #endif
#if defined(GLFW_EXPOSE_NATIVE_NSGL) #if defined(GLFW_EXPOSE_NATIVE_NSGL)

View File

@ -894,6 +894,48 @@ static GLFWbool createNativeWindow(_GLFWwindow* window,
return GLFW_TRUE; return GLFW_TRUE;
} }
static GLFWbool createMetalLayer(_GLFWwindow* window)
{
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101100
@autoreleasepool {
// HACK: Dynamically load Core Animation to avoid adding an extra
// dependency for the majority who don't use MoltenVK
NSBundle* bundle = [NSBundle bundleWithPath:@"/System/Library/Frameworks/QuartzCore.framework"];
if (!bundle)
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Cocoa: Failed to find QuartzCore.framework");
return GLFW_FALSE;
}
// NOTE: Create the layer here as makeBackingLayer should not return nil
window->ns.layer = [[bundle classNamed:@"CAMetalLayer"] layer];
if (!window->ns.layer)
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Cocoa: Failed to create layer for view");
return GLFW_FALSE;
}
if (window->ns.retina)
[window->ns.layer setContentsScale:[window->ns.object backingScaleFactor]];
[window->ns.view setLayer:window->ns.layer];
[window->ns.view setWantsLayer:YES];
} // autoreleasepool
return GLFW_TRUE;
#else
return GLFW_FALSE;
#endif
}
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
////// GLFW internal API ////// ////// GLFW internal API //////
@ -1941,33 +1983,8 @@ VkResult _glfwCreateWindowSurfaceCocoa(VkInstance instance,
const VkAllocationCallbacks* allocator, const VkAllocationCallbacks* allocator,
VkSurfaceKHR* surface) VkSurfaceKHR* surface)
{ {
@autoreleasepool { if (!createMetalLayer(window))
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101100
// HACK: Dynamically load Core Animation to avoid adding an extra
// dependency for the majority who don't use MoltenVK
NSBundle* bundle = [NSBundle bundleWithPath:@"/System/Library/Frameworks/QuartzCore.framework"];
if (!bundle)
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Cocoa: Failed to find QuartzCore.framework");
return VK_ERROR_EXTENSION_NOT_PRESENT; return VK_ERROR_EXTENSION_NOT_PRESENT;
}
// NOTE: Create the layer here as makeBackingLayer should not return nil
window->ns.layer = [[bundle classNamed:@"CAMetalLayer"] layer];
if (!window->ns.layer)
{
_glfwInputError(GLFW_PLATFORM_ERROR,
"Cocoa: Failed to create layer for view");
return VK_ERROR_EXTENSION_NOT_PRESENT;
}
if (window->ns.retina)
[window->ns.layer setContentsScale:[window->ns.object backingScaleFactor]];
[window->ns.view setLayer:window->ns.layer];
[window->ns.view setWantsLayer:YES];
VkResult err; VkResult err;
@ -2020,11 +2037,6 @@ VkResult _glfwCreateWindowSurfaceCocoa(VkInstance instance,
} }
return err; return err;
#else
return VK_ERROR_EXTENSION_NOT_PRESENT;
#endif
} // autoreleasepool
} }
@ -2047,3 +2059,17 @@ GLFWAPI id glfwGetCocoaWindow(GLFWwindow* handle)
return window->ns.object; return window->ns.object;
} }
GLFWAPI id glfwGetMetalLayer(GLFWwindow* handle)
{
_GLFWwindow* window = (_GLFWwindow*) handle;
_GLFW_REQUIRE_INIT_OR_RETURN(nil);
if (!window->ns.layer)
{
if (!createMetalLayer(window))
return nil;
}
return window->ns.layer;
}