From 15d91801b7ac3d52779ec7330f2a1c0128fb57df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Thu, 16 Jan 2020 01:52:50 +0100 Subject: [PATCH] Cocoa: Select Vulkan surface extension at runtime This mostly just follows the pattern established by X11. Related to #1619. --- CMakeLists.txt | 2 - README.md | 2 + src/cocoa_platform.h | 20 +++------ src/cocoa_window.m | 104 +++++++++++++++++++++---------------------- src/glfw_config.h.in | 1 - src/internal.h | 3 -- src/vulkan.c | 3 -- 7 files changed, 58 insertions(+), 77 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d5439f12..8d8719da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -145,8 +145,6 @@ elseif (WIN32) elseif (APPLE) set(_GLFW_COCOA 1) message(STATUS "Using Cocoa for window creation") - set(VK_USE_PLATFORM_METAL_EXT 1) - message(STATUS "Using VK_EXT_metal_surface") elseif (UNIX) set(_GLFW_X11 1) message(STATUS "Using X11 for window creation") diff --git a/README.md b/README.md index 51d0c668..0fea46e9 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,7 @@ information on what to include when reporting a bug. - [Win32] Bugfix: The window hint `GLFW_MAXIMIZED` did not move or resize the window (#1499) - [Win32] Bugfix: Disabled cursor mode interfered with some non-client actions + - [Cocoa] Added support for `VK_EXT_metal_surface` (#1619) - [Cocoa] Removed dependency on the CoreVideo framework - [Cocoa] Bugfix: `glfwSetWindowSize` used a bottom-left anchor point (#1553) - [Cocoa] Bugfix: Window remained on screen after destruction until event poll @@ -260,6 +261,7 @@ skills. - Cem Karan - Osman Keskin - Josh Kilmer + - Byunghoon Kim - Cameron King - Peter Knut - Christoph Kubisch diff --git a/src/cocoa_platform.h b/src/cocoa_platform.h index d1b51d6d..e9cd5843 100644 --- a/src/cocoa_platform.h +++ b/src/cocoa_platform.h @@ -60,8 +60,8 @@ typedef void* id; #define NSWindowStyleMaskTitled NSTitledWindowMask #endif -#if defined(VK_USE_PLATFORM_MACOS_MVK) typedef VkFlags VkMacOSSurfaceCreateFlagsMVK; +typedef VkFlags VkMetalSurfaceCreateFlagsEXT; typedef struct VkMacOSSurfaceCreateInfoMVK { @@ -71,24 +71,16 @@ typedef struct VkMacOSSurfaceCreateInfoMVK const void* pView; } VkMacOSSurfaceCreateInfoMVK; -typedef VkResult (APIENTRY *PFN_vkCreateMacOSSurfaceMVK)(VkInstance,const VkMacOSSurfaceCreateInfoMVK*,const VkAllocationCallbacks*,VkSurfaceKHR*); - -#elif defined(VK_USE_PLATFORM_METAL_EXT) -#define VK_EXT_metal_surface 1 -typedef void CAMetalLayer; - -#define VK_EXT_METAL_SURFACE_SPEC_VERSION 1 -#define VK_EXT_METAL_SURFACE_EXTENSION_NAME "VK_EXT_metal_surface" -typedef VkFlags VkMetalSurfaceCreateFlagsEXT; -typedef struct VkMetalSurfaceCreateInfoEXT { +typedef struct VkMetalSurfaceCreateInfoEXT +{ VkStructureType sType; const void* pNext; VkMetalSurfaceCreateFlagsEXT flags; - const CAMetalLayer* pLayer; + const void* pLayer; } VkMetalSurfaceCreateInfoEXT; -typedef VkResult (APIENTRY *PFN_vkCreateMetalSurfaceEXT)(VkInstance, const VkMetalSurfaceCreateInfoEXT*, const VkAllocationCallbacks*, VkSurfaceKHR*); -#endif +typedef VkResult (APIENTRY *PFN_vkCreateMacOSSurfaceMVK)(VkInstance,const VkMacOSSurfaceCreateInfoMVK*,const VkAllocationCallbacks*,VkSurfaceKHR*); +typedef VkResult (APIENTRY *PFN_vkCreateMetalSurfaceEXT)(VkInstance,const VkMetalSurfaceCreateInfoEXT*,const VkAllocationCallbacks*,VkSurfaceKHR*); #include "posix_thread.h" #include "cocoa_joystick.h" diff --git a/src/cocoa_window.m b/src/cocoa_window.m index c8f084d5..2a24f812 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -1715,19 +1715,16 @@ const char* _glfwPlatformGetClipboardString(void) void _glfwPlatformGetRequiredInstanceExtensions(char** extensions) { -#if defined(VK_USE_PLATFORM_MACOS_MVK) - if (!_glfw.vk.KHR_surface || !_glfw.vk.MVK_macos_surface) - return; - - extensions[0] = "VK_KHR_surface"; - extensions[1] = "VK_MVK_macos_surface"; - -#elif defined(VK_USE_PLATFORM_METAL_EXT) - if (!_glfw.vk.KHR_surface || !_glfw.vk.EXT_metal_surface) - return; - extensions[0] = "VK_KHR_surface"; - extensions[1] = "VK_EXT_metal_surface"; -#endif + if (_glfw.vk.KHR_surface && _glfw.vk.EXT_metal_surface) + { + extensions[0] = "VK_KHR_surface"; + extensions[1] = "VK_EXT_metal_surface"; + } + else if (_glfw.vk.KHR_surface && _glfw.vk.MVK_macos_surface) + { + extensions[0] = "VK_KHR_surface"; + extensions[1] = "VK_MVK_macos_surface"; + } } int _glfwPlatformGetPhysicalDevicePresentationSupport(VkInstance instance, @@ -1745,35 +1742,6 @@ VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, @autoreleasepool { #if MAC_OS_X_VERSION_MAX_ALLOWED >= 101100 - VkResult err; - -#if defined(VK_USE_PLATFORM_MACOS_MVK) - VkMacOSSurfaceCreateInfoMVK sci; - - PFN_vkCreateMacOSSurfaceMVK vkCreateMacOSSurfaceMVK; - vkCreateMacOSSurfaceMVK = (PFN_vkCreateMacOSSurfaceMVK) - vkGetInstanceProcAddr(instance, "vkCreateMacOSSurfaceMVK"); - if (!vkCreateMacOSSurfaceMVK) - { - _glfwInputError(GLFW_API_UNAVAILABLE, - "Cocoa: Vulkan instance missing VK_MVK_macos_surface extension"); - return VK_ERROR_EXTENSION_NOT_PRESENT; - } - -#elif defined(VK_USE_PLATFORM_METAL_EXT) - VkMetalSurfaceCreateInfoEXT sci; - - PFN_vkCreateMetalSurfaceEXT vkCreateMetalSurfaceEXT; - vkCreateMetalSurfaceEXT = (PFN_vkCreateMetalSurfaceEXT) - vkGetInstanceProcAddr(instance, "vkCreateMetalSurfaceEXT"); - if (!vkCreateMetalSurfaceEXT) - { - _glfwInputError(GLFW_API_UNAVAILABLE, - "Cocoa: Vulkan instance missing VK_EXT_metal_surface extension"); - return VK_ERROR_EXTENSION_NOT_PRESENT; - } -#endif - // 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"]; @@ -1799,21 +1767,49 @@ VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, [window->ns.view setLayer:window->ns.layer]; [window->ns.view setWantsLayer:YES]; - memset(&sci, 0, sizeof(sci)); -#if defined(VK_USE_PLATFORM_MACOS_MVK) - sci.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK; - sci.pView = window->ns.view; + VkResult err; - err = vkCreateMacOSSurfaceMVK(instance, &sci, allocator, surface); + if (_glfw.vk.EXT_metal_surface) + { + VkMetalSurfaceCreateInfoEXT sci; -#elif defined(VK_USE_PLATFORM_METAL_EXT) - sci.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT; - sci.pNext = NULL; - sci.flags = 0; - sci.pLayer = window->ns.layer; + PFN_vkCreateMetalSurfaceEXT vkCreateMetalSurfaceEXT; + vkCreateMetalSurfaceEXT = (PFN_vkCreateMetalSurfaceEXT) + vkGetInstanceProcAddr(instance, "vkCreateMetalSurfaceEXT"); + if (!vkCreateMetalSurfaceEXT) + { + _glfwInputError(GLFW_API_UNAVAILABLE, + "Cocoa: Vulkan instance missing VK_EXT_metal_surface extension"); + return VK_ERROR_EXTENSION_NOT_PRESENT; + } + + memset(&sci, 0, sizeof(sci)); + sci.sType = VK_STRUCTURE_TYPE_METAL_SURFACE_CREATE_INFO_EXT; + sci.pLayer = window->ns.layer; + + err = vkCreateMetalSurfaceEXT(instance, &sci, allocator, surface); + } + else + { + VkMacOSSurfaceCreateInfoMVK sci; + + PFN_vkCreateMacOSSurfaceMVK vkCreateMacOSSurfaceMVK; + vkCreateMacOSSurfaceMVK = (PFN_vkCreateMacOSSurfaceMVK) + vkGetInstanceProcAddr(instance, "vkCreateMacOSSurfaceMVK"); + if (!vkCreateMacOSSurfaceMVK) + { + _glfwInputError(GLFW_API_UNAVAILABLE, + "Cocoa: Vulkan instance missing VK_MVK_macos_surface extension"); + return VK_ERROR_EXTENSION_NOT_PRESENT; + } + + memset(&sci, 0, sizeof(sci)); + sci.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK; + sci.pView = window->ns.view; + + err = vkCreateMacOSSurfaceMVK(instance, &sci, allocator, surface); + } - err = vkCreateMetalSurfaceEXT(instance, &sci, allocator, surface); -#endif if (err) { _glfwInputError(GLFW_PLATFORM_ERROR, diff --git a/src/glfw_config.h.in b/src/glfw_config.h.in index ae058965..f4876da2 100644 --- a/src/glfw_config.h.in +++ b/src/glfw_config.h.in @@ -40,7 +40,6 @@ #cmakedefine _GLFW_WIN32 // Define this to 1 if building GLFW for Cocoa #cmakedefine _GLFW_COCOA -#cmakedefine VK_USE_PLATFORM_METAL_EXT // Define this to 1 if building GLFW for Wayland #cmakedefine _GLFW_WAYLAND // Define this to 1 if building GLFW for OSMesa diff --git a/src/internal.h b/src/internal.h index 33342d9f..6d7587c8 100644 --- a/src/internal.h +++ b/src/internal.h @@ -559,11 +559,8 @@ struct _GLFWlibrary #if defined(_GLFW_WIN32) GLFWbool KHR_win32_surface; #elif defined(_GLFW_COCOA) - #if defined(VK_USE_PLATFORM_MACOS_MVK) GLFWbool MVK_macos_surface; - #elif defined(VK_USE_PLATFORM_METAL_EXT) GLFWbool EXT_metal_surface; - #endif #elif defined(_GLFW_X11) GLFWbool KHR_xlib_surface; GLFWbool KHR_xcb_surface; diff --git a/src/vulkan.c b/src/vulkan.c index f6b07ec8..0bbcb83d 100644 --- a/src/vulkan.c +++ b/src/vulkan.c @@ -128,13 +128,10 @@ GLFWbool _glfwInitVulkan(int mode) else if (strcmp(ep[i].extensionName, "VK_KHR_win32_surface") == 0) _glfw.vk.KHR_win32_surface = GLFW_TRUE; #elif defined(_GLFW_COCOA) - #if defined(VK_USE_PLATFORM_MACOS_MVK) else if (strcmp(ep[i].extensionName, "VK_MVK_macos_surface") == 0) _glfw.vk.MVK_macos_surface = GLFW_TRUE; - #elif defined(VK_USE_PLATFORM_METAL_EXT) else if (strcmp(ep[i].extensionName, "VK_EXT_metal_surface") == 0) _glfw.vk.EXT_metal_surface = GLFW_TRUE; - #endif #elif defined(_GLFW_X11) else if (strcmp(ep[i].extensionName, "VK_KHR_xlib_surface") == 0) _glfw.vk.KHR_xlib_surface = GLFW_TRUE;