Atlas - SDL_cocoavulkan.m

Home / ext / SDL2 / src / video / cocoa Lines: 1 | Size: 8026 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2018 Sam Lantinga <[email protected]> 4 5 This software is provided 'as-is', without any express or implied 6 warranty. In no event will the authors be held liable for any damages 7 arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, 10 including commercial applications, and to alter it and redistribute it 11 freely, subject to the following restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not 14 claim that you wrote the original software. If you use this software 15 in a product, an acknowledgment in the product documentation would be 16 appreciated but is not required. 17 2. Altered source versions must be plainly marked as such, and must not be 18 misrepresented as being the original software. 19 3. This notice may not be removed or altered from any source distribution. 20*/ 21 22/* 23 * @author Mark Callow, www.edgewise-consulting.com. Based on Jacob Lifshay's 24 * SDL_x11vulkan.c. 25 */ 26 27#include "../../SDL_internal.h" 28 29#if SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_COCOA 30 31#include "SDL_cocoavideo.h" 32#include "SDL_cocoawindow.h" 33#include "SDL_assert.h" 34 35#include "SDL_loadso.h" 36#include "SDL_cocoametalview.h" 37#include "SDL_cocoavulkan.h" 38#include "SDL_syswm.h" 39 40#include <dlfcn.h> 41 42const char* defaultPaths[] = { 43 "vulkan.framework/vulkan", 44 "libvulkan.1.dylib", 45 "MoltenVK.framework/MoltenVK", 46 "libMoltenVK.dylib" 47}; 48 49/* Since libSDL is most likely a .dylib, need RTLD_DEFAULT not RTLD_SELF. */ 50#define DEFAULT_HANDLE RTLD_DEFAULT 51 52int Cocoa_Vulkan_LoadLibrary(_THIS, const char *path) 53{ 54 VkExtensionProperties *extensions = NULL; 55 Uint32 extensionCount = 0; 56 SDL_bool hasSurfaceExtension = SDL_FALSE; 57 SDL_bool hasMacOSSurfaceExtension = SDL_FALSE; 58 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL; 59 60 if (_this->vulkan_config.loader_handle) { 61 SDL_SetError("Vulkan/MoltenVK already loaded"); 62 return -1; 63 } 64 65 /* Load the Vulkan loader library */ 66 if (!path) { 67 path = SDL_getenv("SDL_VULKAN_LIBRARY"); 68 } 69 70 if (!path) { 71 /* MoltenVK framework, currently, v0.17.0, has a static library and is 72 * the recommended way to use the package. There is likely no object to 73 * load. */ 74 vkGetInstanceProcAddr = 75 (PFN_vkGetInstanceProcAddr)dlsym(DEFAULT_HANDLE, 76 "vkGetInstanceProcAddr"); 77 } 78 79 if (vkGetInstanceProcAddr) { 80 _this->vulkan_config.loader_handle = DEFAULT_HANDLE; 81 } else { 82 const char** paths; 83 int numPaths; 84 int i; 85 86 if (path) { 87 paths = &path; 88 numPaths = 1; 89 } else { 90 /* Look for framework or .dylib packaged with the application 91 * instead. */ 92 paths = defaultPaths; 93 numPaths = SDL_arraysize(defaultPaths); 94 } 95 96 for (i=0; i < numPaths; i++) { 97 _this->vulkan_config.loader_handle = SDL_LoadObject(paths[i]); 98 if (_this->vulkan_config.loader_handle) 99 break; 100 else 101 continue; 102 } 103 if (i == numPaths) 104 return -1; 105 106 SDL_strlcpy(_this->vulkan_config.loader_path, paths[i], 107 SDL_arraysize(_this->vulkan_config.loader_path)); 108 vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction( 109 _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr"); 110 } 111 112 if (!vkGetInstanceProcAddr) { 113 SDL_SetError("Failed to find %s in either executable or %s: %s", 114 "vkGetInstanceProcAddr", 115 _this->vulkan_config.loader_path, 116 (const char *) dlerror()); 117 goto fail; 118 } 119 120 _this->vulkan_config.vkGetInstanceProcAddr = (void *)vkGetInstanceProcAddr; 121 _this->vulkan_config.vkEnumerateInstanceExtensionProperties = 122 (void *)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)( 123 VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties"); 124 if (!_this->vulkan_config.vkEnumerateInstanceExtensionProperties) { 125 goto fail; 126 } 127 extensions = SDL_Vulkan_CreateInstanceExtensionsList( 128 (PFN_vkEnumerateInstanceExtensionProperties) 129 _this->vulkan_config.vkEnumerateInstanceExtensionProperties, 130 &extensionCount); 131 if (!extensions) { 132 goto fail; 133 } 134 for (Uint32 i = 0; i < extensionCount; i++) { 135 if (SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) { 136 hasSurfaceExtension = SDL_TRUE; 137 } else if (SDL_strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) { 138 hasMacOSSurfaceExtension = SDL_TRUE; 139 } 140 } 141 SDL_free(extensions); 142 if (!hasSurfaceExtension) { 143 SDL_SetError("Installed MoltenVK/Vulkan doesn't implement the " 144 VK_KHR_SURFACE_EXTENSION_NAME " extension"); 145 goto fail; 146 } else if (!hasMacOSSurfaceExtension) { 147 SDL_SetError("Installed MoltenVK/Vulkan doesn't implement the " 148 VK_MVK_MACOS_SURFACE_EXTENSION_NAME "extension"); 149 goto fail; 150 } 151 return 0; 152 153fail: 154 SDL_UnloadObject(_this->vulkan_config.loader_handle); 155 _this->vulkan_config.loader_handle = NULL; 156 return -1; 157} 158 159void Cocoa_Vulkan_UnloadLibrary(_THIS) 160{ 161 if (_this->vulkan_config.loader_handle) { 162 if (_this->vulkan_config.loader_handle != DEFAULT_HANDLE) { 163 SDL_UnloadObject(_this->vulkan_config.loader_handle); 164 } 165 _this->vulkan_config.loader_handle = NULL; 166 } 167} 168 169SDL_bool Cocoa_Vulkan_GetInstanceExtensions(_THIS, 170 SDL_Window *window, 171 unsigned *count, 172 const char **names) 173{ 174 static const char *const extensionsForCocoa[] = { 175 VK_KHR_SURFACE_EXTENSION_NAME, VK_MVK_MACOS_SURFACE_EXTENSION_NAME 176 }; 177 if (!_this->vulkan_config.loader_handle) { 178 SDL_SetError("Vulkan is not loaded"); 179 return SDL_FALSE; 180 } 181 return SDL_Vulkan_GetInstanceExtensions_Helper( 182 count, names, SDL_arraysize(extensionsForCocoa), 183 extensionsForCocoa); 184} 185 186SDL_bool Cocoa_Vulkan_CreateSurface(_THIS, 187 SDL_Window *window, 188 VkInstance instance, 189 VkSurfaceKHR *surface) 190{ 191 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = 192 (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr; 193 PFN_vkCreateMacOSSurfaceMVK vkCreateMacOSSurfaceMVK = 194 (PFN_vkCreateMacOSSurfaceMVK)vkGetInstanceProcAddr( 195 (VkInstance)instance, 196 "vkCreateMacOSSurfaceMVK"); 197 VkMacOSSurfaceCreateInfoMVK createInfo = {}; 198 VkResult result; 199 200 if (!_this->vulkan_config.loader_handle) { 201 SDL_SetError("Vulkan is not loaded"); 202 return SDL_FALSE; 203 } 204 205 if (!vkCreateMacOSSurfaceMVK) { 206 SDL_SetError(VK_MVK_MACOS_SURFACE_EXTENSION_NAME 207 " extension is not enabled in the Vulkan instance."); 208 return SDL_FALSE; 209 } 210 createInfo.sType = VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK; 211 createInfo.pNext = NULL; 212 createInfo.flags = 0; 213 createInfo.pView = Cocoa_Mtl_AddMetalView(window); 214 result = vkCreateMacOSSurfaceMVK(instance, &createInfo, 215 NULL, surface); 216 if (result != VK_SUCCESS) { 217 SDL_SetError("vkCreateMacOSSurfaceMVK failed: %s", 218 SDL_Vulkan_GetResultString(result)); 219 return SDL_FALSE; 220 } 221 return SDL_TRUE; 222} 223 224void Cocoa_Vulkan_GetDrawableSize(_THIS, SDL_Window *window, int *w, int *h) 225{ 226 Cocoa_Mtl_GetDrawableSize(window, w, h); 227} 228 229#endif 230 231/* vim: set ts=4 sw=4 expandtab: */ 232
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.