SDL  2.0
SDL_uikitvulkan.m
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2020 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_UIKIT
30 
31 #include "SDL_uikitvideo.h"
32 #include "SDL_uikitwindow.h"
33 
34 #include "SDL_loadso.h"
35 #include "SDL_uikitvulkan.h"
36 #include "SDL_uikitmetalview.h"
37 #include "SDL_syswm.h"
38 
39 #include <dlfcn.h>
40 
41 const char* defaultPaths[] = {
42  "libvulkan.dylib",
43 };
44 
45 /* Since libSDL is static, could use RTLD_SELF. Using RTLD_DEFAULT is future
46  * proofing. */
47 #define DEFAULT_HANDLE RTLD_DEFAULT
48 
49 int UIKit_Vulkan_LoadLibrary(_THIS, const char *path)
50 {
51  VkExtensionProperties *extensions = NULL;
52  Uint32 extensionCount = 0;
53  SDL_bool hasSurfaceExtension = SDL_FALSE;
54  SDL_bool hasIOSSurfaceExtension = SDL_FALSE;
56 
58  return SDL_SetError("Vulkan Portability library is already loaded.");
59  }
60 
61  /* Load the Vulkan loader library */
62  if (!path) {
63  path = SDL_getenv("SDL_VULKAN_LIBRARY");
64  }
65 
66  if (!path) {
67  /* Handle the case where Vulkan Portability is linked statically. */
69  (PFN_vkGetInstanceProcAddr)dlsym(DEFAULT_HANDLE,
70  "vkGetInstanceProcAddr");
71  }
72 
74  _this->vulkan_config.loader_handle = DEFAULT_HANDLE;
75  } else {
76  const char** paths;
77  const char *foundPath = NULL;
78  int numPaths;
79  int i;
80 
81  if (path) {
82  paths = &path;
83  numPaths = 1;
84  } else {
85  /* Look for the .dylib packaged with the application instead. */
86  paths = defaultPaths;
87  numPaths = SDL_arraysize(defaultPaths);
88  }
89 
90  for (i = 0; i < numPaths && _this->vulkan_config.loader_handle == NULL; i++) {
91  foundPath = paths[i];
93  }
94 
96  return SDL_SetError("Failed to load Vulkan Portability library");
97  }
98 
104  "vkGetInstanceProcAddr");
105  }
106 
107  if (!vkGetInstanceProcAddr) {
108  SDL_SetError("Failed to find %s in either executable or %s: %s",
109  "vkGetInstanceProcAddr",
110  "linked Vulkan Portability library",
111  (const char *) dlerror());
112  goto fail;
113  }
114 
118  VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
119 
121  SDL_SetError("No vkEnumerateInstanceExtensionProperties found.");
122  goto fail;
123  }
124 
125  extensions = SDL_Vulkan_CreateInstanceExtensionsList(
128  &extensionCount);
129 
130  if (!extensions) {
131  goto fail;
132  }
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_IOS_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) {
138  hasIOSSurfaceExtension = SDL_TRUE;
139  }
140  }
141 
142  SDL_free(extensions);
143 
144  if (!hasSurfaceExtension) {
145  SDL_SetError("Installed Vulkan Portability doesn't implement the "
146  VK_KHR_SURFACE_EXTENSION_NAME " extension");
147  goto fail;
148  } else if (!hasIOSSurfaceExtension) {
149  SDL_SetError("Installed Vulkan Portability doesn't implement the "
151  goto fail;
152  }
153 
154  return 0;
155 
156 fail:
158  return -1;
159 }
160 
161 void UIKit_Vulkan_UnloadLibrary(_THIS)
162 {
164  if (_this->vulkan_config.loader_handle != DEFAULT_HANDLE) {
166  }
168  }
169 }
170 
171 SDL_bool UIKit_Vulkan_GetInstanceExtensions(_THIS,
173  unsigned *count,
174  const char **names)
175 {
176  static const char *const extensionsForUIKit[] = {
178  };
180  SDL_SetError("Vulkan is not loaded");
181  return SDL_FALSE;
182  }
183 
184  return SDL_Vulkan_GetInstanceExtensions_Helper(
185  count, names, SDL_arraysize(extensionsForUIKit),
186  extensionsForUIKit);
187 }
188 
189 SDL_bool UIKit_Vulkan_CreateSurface(_THIS,
191  VkInstance instance,
192  VkSurfaceKHR *surface)
193 {
198  (VkInstance)instance,
199  "vkCreateIOSSurfaceMVK");
200  VkIOSSurfaceCreateInfoMVK createInfo = {};
202  SDL_MetalView metalview;
203 
205  SDL_SetError("Vulkan is not loaded");
206  return SDL_FALSE;
207  }
208 
209  if (!vkCreateIOSSurfaceMVK) {
211  " extension is not enabled in the Vulkan instance.");
212  return SDL_FALSE;
213  }
214 
215  metalview = UIKit_Metal_CreateView(_this, window);
216  if (metalview == NULL) {
217  return SDL_FALSE;
218  }
219 
221  createInfo.pNext = NULL;
222  createInfo.flags = 0;
223  createInfo.pView = (const void *)metalview;
224  result = vkCreateIOSSurfaceMVK(instance, &createInfo,
225  NULL, surface);
226  if (result != VK_SUCCESS) {
227  UIKit_Metal_DestroyView(_this, metalview);
228  SDL_SetError("vkCreateIOSSurfaceMVK failed: %s",
229  SDL_Vulkan_GetResultString(result));
230  return SDL_FALSE;
231  }
232 
233  /* Unfortunately there's no SDL_Vulkan_DestroySurface function we can call
234  * Metal_DestroyView from. Right now the metal view's ref count is +2 (one
235  * from returning a new view object in CreateView, and one because it's
236  * a subview of the window.) If we release the view here to make it +1, it
237  * will be destroyed when the window is destroyed. */
238  CFBridgingRelease(metalview);
239 
240  return SDL_TRUE;
241 }
242 
243 void UIKit_Vulkan_GetDrawableSize(_THIS, SDL_Window *window, int *w, int *h)
244 {
245  UIKit_Metal_GetDrawableSize(_this, window, w, h);
246 }
247 
248 #endif
249 
250 /* vi: set ts=4 sw=4 expandtab: */
#define _THIS
#define SDL_SetError
#define SDL_LoadObject
#define SDL_UnloadObject
#define SDL_getenv
#define SDL_strlcpy
#define SDL_free
#define SDL_strcmp
void * SDL_LoadFunction(void *handle, const char *name)
void * SDL_MetalView
A handle to a CAMetalLayer-backed NSView (macOS) or UIView (iOS/tvOS).
Definition: SDL_metal.h:44
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1571
GLuint64EXT * result
GLuint GLuint * names
GLsizei numPaths
GLsizei const GLuint * paths
GLsizei const GLchar *const * path
GLfloat GLfloat GLfloat GLfloat h
GLubyte GLubyte GLubyte GLubyte w
SDL_bool
Definition: SDL_stdinc.h:168
@ SDL_TRUE
Definition: SDL_stdinc.h:170
@ SDL_FALSE
Definition: SDL_stdinc.h:169
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:121
uint32_t Uint32
Definition: SDL_stdinc.h:209
static SDL_VideoDevice * _this
Definition: SDL_video.c:126
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
#define NULL
Definition: begin_code.h:163
EGLSurface surface
Definition: eglext.h:248
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
struct SDL_VideoDevice::@441 vulkan_config
PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr
Definition: SDL_sysvideo.h:387
PFN_vkEnumerateInstanceExtensionProperties vkEnumerateInstanceExtensionProperties
Definition: SDL_sysvideo.h:388
char loader_path[256]
Definition: SDL_sysvideo.h:390
void * loader_handle
Definition: SDL_sysvideo.h:391
The type used to identify a window.
Definition: SDL_sysvideo.h:75
VkIOSSurfaceCreateFlagsMVK flags
Definition: vulkan_ios.h:29
VkStructureType sType
Definition: vulkan_ios.h:27
VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char *pName)
#define VK_KHR_SURFACE_EXTENSION_NAME
Definition: vulkan_core.h:5654
VkResult
Definition: vulkan_core.h:103
@ VK_SUCCESS
Definition: vulkan_core.h:104
VkResult(VKAPI_PTR * PFN_vkEnumerateInstanceExtensionProperties)(const char *pLayerName, uint32_t *pPropertyCount, VkExtensionProperties *pProperties)
Definition: vulkan_core.h:3121
#define VK_NULL_HANDLE
Definition: vulkan_core.h:55
PFN_vkVoidFunction(VKAPI_PTR * PFN_vkGetInstanceProcAddr)(VkInstance instance, const char *pName)
Definition: vulkan_core.h:3117
@ VK_STRUCTURE_TYPE_IOS_SURFACE_CREATE_INFO_MVK
Definition: vulkan_core.h:415
#define VK_MVK_IOS_SURFACE_EXTENSION_NAME
Definition: vulkan_ios.h:24
VkResult(VKAPI_PTR * PFN_vkCreateIOSSurfaceMVK)(VkInstance instance, const VkIOSSurfaceCreateInfoMVK *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface)
Definition: vulkan_ios.h:33
VKAPI_ATTR VkResult VKAPI_CALL vkCreateIOSSurfaceMVK(VkInstance instance, const VkIOSSurfaceCreateInfoMVK *pCreateInfo, const VkAllocationCallbacks *pAllocator, VkSurfaceKHR *pSurface)