SDL  2.0
SDL_cocoavulkan.m
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
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 
42 const char* defaultPaths[] = {
43  "vulkan.framework/vulkan",
44  "libvulkan.1.dylib",
45  "libvulkan.dylib",
46  "MoltenVK.framework/MoltenVK",
47  "libMoltenVK.dylib"
48 };
49 
50 /* Since libSDL is most likely a .dylib, need RTLD_DEFAULT not RTLD_SELF. */
51 #define DEFAULT_HANDLE RTLD_DEFAULT
52 
53 int Cocoa_Vulkan_LoadLibrary(_THIS, const char *path)
54 {
55  VkExtensionProperties *extensions = NULL;
56  Uint32 extensionCount = 0;
57  SDL_bool hasSurfaceExtension = SDL_FALSE;
58  SDL_bool hasMacOSSurfaceExtension = SDL_FALSE;
60 
62  return SDL_SetError("Vulkan Portability library is already loaded.");
63  }
64 
65  /* Load the Vulkan loader library */
66  if (!path) {
67  path = SDL_getenv("SDL_VULKAN_LIBRARY");
68  }
69 
70  if (!path) {
71  /* Handle the case where Vulkan Portability is linked statically. */
73  (PFN_vkGetInstanceProcAddr)dlsym(DEFAULT_HANDLE,
74  "vkGetInstanceProcAddr");
75  }
76 
78  _this->vulkan_config.loader_handle = DEFAULT_HANDLE;
79  } else {
80  const char** paths;
81  const char *foundPath = NULL;
82  int numPaths;
83  int i;
84 
85  if (path) {
86  paths = &path;
87  numPaths = 1;
88  } else {
89  /* Look for framework or .dylib packaged with the application
90  * instead. */
91  paths = defaultPaths;
92  numPaths = SDL_arraysize(defaultPaths);
93  }
94 
95  for (i = 0; i < numPaths && _this->vulkan_config.loader_handle == NULL; i++) {
96  foundPath = paths[i];
98  }
99 
101  return SDL_SetError("Failed to load Vulkan Portability library");
102  }
103 
107  _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr");
108  }
109 
110  if (!vkGetInstanceProcAddr) {
111  SDL_SetError("Failed to find %s in either executable or %s: %s",
112  "vkGetInstanceProcAddr",
114  (const char *) dlerror());
115  goto fail;
116  }
117 
121  VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
123  goto fail;
124  }
125  extensions = SDL_Vulkan_CreateInstanceExtensionsList(
128  &extensionCount);
129  if (!extensions) {
130  goto fail;
131  }
132  for (Uint32 i = 0; i < extensionCount; i++) {
133  if (SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) {
134  hasSurfaceExtension = SDL_TRUE;
135  } else if (SDL_strcmp(VK_MVK_MACOS_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) {
136  hasMacOSSurfaceExtension = SDL_TRUE;
137  }
138  }
139  SDL_free(extensions);
140  if (!hasSurfaceExtension) {
141  SDL_SetError("Installed Vulkan Portability library doesn't implement the "
142  VK_KHR_SURFACE_EXTENSION_NAME " extension");
143  goto fail;
144  } else if (!hasMacOSSurfaceExtension) {
145  SDL_SetError("Installed Vulkan Portability library doesn't implement the "
147  goto fail;
148  }
149  return 0;
150 
151 fail:
154  return -1;
155 }
156 
157 void Cocoa_Vulkan_UnloadLibrary(_THIS)
158 {
160  if (_this->vulkan_config.loader_handle != DEFAULT_HANDLE) {
162  }
164  }
165 }
166 
167 SDL_bool Cocoa_Vulkan_GetInstanceExtensions(_THIS,
169  unsigned *count,
170  const char **names)
171 {
172  static const char *const extensionsForCocoa[] = {
174  };
176  SDL_SetError("Vulkan is not loaded");
177  return SDL_FALSE;
178  }
179  return SDL_Vulkan_GetInstanceExtensions_Helper(
180  count, names, SDL_arraysize(extensionsForCocoa),
181  extensionsForCocoa);
182 }
183 
184 SDL_bool Cocoa_Vulkan_CreateSurface(_THIS,
186  VkInstance instance,
187  VkSurfaceKHR *surface)
188 {
193  (VkInstance)instance,
194  "vkCreateMacOSSurfaceMVK");
195  VkMacOSSurfaceCreateInfoMVK createInfo = {};
197 
199  SDL_SetError("Vulkan is not loaded");
200  return SDL_FALSE;
201  }
202 
205  " extension is not enabled in the Vulkan instance.");
206  return SDL_FALSE;
207  }
209  createInfo.pNext = NULL;
210  createInfo.flags = 0;
211  createInfo.pView = Cocoa_Mtl_AddMetalView(window);
212  result = vkCreateMacOSSurfaceMVK(instance, &createInfo,
213  NULL, surface);
214  if (result != VK_SUCCESS) {
215  SDL_SetError("vkCreateMacOSSurfaceMVK failed: %s",
216  SDL_Vulkan_GetResultString(result));
217  return SDL_FALSE;
218  }
219  return SDL_TRUE;
220 }
221 
222 void Cocoa_Vulkan_GetDrawableSize(_THIS, SDL_Window *window, int *w, int *h)
223 {
224  Cocoa_Mtl_GetDrawableSize(window, w, h);
225 }
226 
227 #endif
228 
229 /* vim: set ts=4 sw=4 expandtab: */
VK_MVK_MACOS_SURFACE_EXTENSION_NAME
#define VK_MVK_MACOS_SURFACE_EXTENSION_NAME
Definition: vulkan_macos.h:32
VkMacOSSurfaceCreateInfoMVK::pView
const void * pView
Definition: vulkan_macos.h:40
SDL_strlcpy
#define SDL_strlcpy
Definition: SDL_dynapi_overrides.h:394
SDL_VideoDevice::vkGetInstanceProcAddr
PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr
Definition: SDL_sysvideo.h:372
PFN_vkGetInstanceProcAddr
PFN_vkVoidFunction(VKAPI_PTR * PFN_vkGetInstanceProcAddr)(VkInstance instance, const char *pName)
Definition: vulkan_core.h:2859
NULL
#define NULL
Definition: begin_code.h:167
surface
EGLSurface surface
Definition: eglext.h:248
VkMacOSSurfaceCreateInfoMVK::flags
VkMacOSSurfaceCreateFlagsMVK flags
Definition: vulkan_macos.h:39
vkCreateMacOSSurfaceMVK
VKAPI_ATTR VkResult VKAPI_CALL vkCreateMacOSSurfaceMVK(VkInstance instance, const VkMacOSSurfaceCreateInfoMVK *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface)
count
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1570
SDL_cocoawindow.h
vkGetInstanceProcAddr
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *pName)
VkMacOSSurfaceCreateInfoMVK::pNext
const void * pNext
Definition: vulkan_macos.h:38
SDL_UnloadObject
#define SDL_UnloadObject
Definition: SDL_dynapi_overrides.h:234
path
GLsizei const GLchar *const * path
Definition: SDL_opengl_glext.h:3730
numPaths
GLsizei numPaths
Definition: SDL_opengl_glext.h:9162
SDL_cocoavideo.h
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1946
result
GLuint64EXT * result
Definition: SDL_opengl_glext.h:9432
SDL_LoadObject
#define SDL_LoadObject
Definition: SDL_dynapi_overrides.h:232
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:73
SDL_cocoavulkan.h
VK_KHR_SURFACE_EXTENSION_NAME
#define VK_KHR_SURFACE_EXTENSION_NAME
Definition: vulkan_core.h:4669
VK_SUCCESS
Definition: vulkan_core.h:121
VK_NULL_HANDLE
#define VK_NULL_HANDLE
Definition: vulkan_core.h:49
_this
static SDL_VideoDevice * _this
Definition: SDL_video.c:118
SDL_VideoData::instance
PP_Instance instance
Definition: SDL_naclvideo.h:57
paths
GLsizei const GLuint * paths
Definition: SDL_opengl_glext.h:9162
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
VkResult
VkResult
Definition: vulkan_core.h:120
PFN_vkEnumerateInstanceExtensionProperties
VkResult(VKAPI_PTR * PFN_vkEnumerateInstanceExtensionProperties)(const char *pLayerName, uint32_t *pPropertyCount, VkExtensionProperties *pProperties)
Definition: vulkan_core.h:2863
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_VideoDevice::loader_handle
void * loader_handle
Definition: SDL_sysvideo.h:376
SDL_FALSE
Definition: SDL_stdinc.h:163
SDL_VideoDevice::loader_path
char loader_path[256]
Definition: SDL_sysvideo.h:375
SDL_assert.h
VkMacOSSurfaceCreateInfoMVK::sType
VkStructureType sType
Definition: vulkan_macos.h:37
_THIS
#define _THIS
Definition: SDL_alsa_audio.h:31
names
GLuint GLuint * names
Definition: SDL_opengl_glext.h:4956
SDL_VideoDevice::vkEnumerateInstanceExtensionProperties
PFN_vkEnumerateInstanceExtensionProperties vkEnumerateInstanceExtensionProperties
Definition: SDL_sysvideo.h:373
SDL_arraysize
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:115
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:161
SDL_getenv
#define SDL_getenv
Definition: SDL_dynapi_overrides.h:378
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
SDL_cocoametalview.h
SDL_LoadFunction
void * SDL_LoadFunction(void *handle, const char *name)
SDL_TRUE
Definition: SDL_stdinc.h:164
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
PFN_vkCreateMacOSSurfaceMVK
VkResult(VKAPI_PTR * PFN_vkCreateMacOSSurfaceMVK)(VkInstance instance, const VkMacOSSurfaceCreateInfoMVK *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface)
Definition: vulkan_macos.h:44
VK_STRUCTURE_TYPE_MACOS_SURFACE_CREATE_INFO_MVK
Definition: vulkan_core.h:373
VkExtensionProperties
Definition: vulkan_core.h:2047
VkMacOSSurfaceCreateInfoMVK
Definition: vulkan_macos.h:36
SDL_strcmp
#define SDL_strcmp
Definition: SDL_dynapi_overrides.h:417
SDL_loadso.h
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_VideoDevice::vulkan_config
struct SDL_VideoDevice::@263 vulkan_config
SDL_syswm.h
w
GLubyte GLubyte GLubyte GLubyte w
Definition: SDL_opengl_glext.h:731