SDL_offscreenvulkan.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #if defined(SDL_VIDEO_VULKAN) && defined(SDL_VIDEO_DRIVER_OFFSCREEN)
  20. #include "../SDL_vulkan_internal.h"
  21. #include "../SDL_sysvideo.h"
  22. static const char *s_defaultPaths[] = {
  23. #if defined(SDL_PLATFORM_WINDOWS)
  24. "vulkan-1.dll"
  25. #elif defined(SDL_PLATFORM_APPLE)
  26. "vulkan.framework/vulkan",
  27. "libvulkan.1.dylib",
  28. "libvulkan.dylib",
  29. "MoltenVK.framework/MoltenVK",
  30. "libMoltenVK.dylib"
  31. #elif defined(SDL_PLATFORM_OPENBSD)
  32. "libvulkan.so"
  33. #else
  34. "libvulkan.so.1"
  35. #endif
  36. };
  37. #if defined( SDL_PLATFORM_APPLE )
  38. #include <dlfcn.h>
  39. // Since libSDL is most likely a .dylib, need RTLD_DEFAULT not RTLD_SELF.
  40. #define DEFAULT_HANDLE RTLD_DEFAULT
  41. #endif
  42. /*Should the whole driver fail if it can't create a surface? Rendering to an offscreen buffer is still possible without a surface.
  43. At the time of writing. I need the driver to minimally work even if the surface extension isn't present.
  44. And account for the inability to create a surface on the consumer side.
  45. So for now I'm targeting my specific use case -Dave Kircher*/
  46. #define HEADLESS_SURFACE_EXTENSION_REQUIRED_TO_LOAD 0
  47. bool OFFSCREEN_Vulkan_LoadLibrary(SDL_VideoDevice *_this, const char *path)
  48. {
  49. VkExtensionProperties *extensions = NULL;
  50. Uint32 extensionCount = 0;
  51. bool hasSurfaceExtension = false;
  52. bool hasHeadlessSurfaceExtension = false;
  53. PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL;
  54. Uint32 i;
  55. const char **paths;
  56. const char *foundPath = NULL;
  57. Uint32 numPaths;
  58. if (_this->vulkan_config.loader_handle) {
  59. return SDL_SetError("Vulkan already loaded");
  60. }
  61. // Load the Vulkan loader library
  62. if (!path) {
  63. path = SDL_GetHint(SDL_HINT_VULKAN_LIBRARY);
  64. }
  65. #if defined(SDL_PLATFORM_APPLE)
  66. if (!path) {
  67. // Handle the case where Vulkan Portability is linked statically.
  68. vkGetInstanceProcAddr =
  69. (PFN_vkGetInstanceProcAddr)dlsym(DEFAULT_HANDLE,
  70. "vkGetInstanceProcAddr");
  71. }
  72. if (vkGetInstanceProcAddr) {
  73. _this->vulkan_config.loader_handle = DEFAULT_HANDLE;
  74. } else
  75. #endif
  76. {
  77. if (path) {
  78. paths = &path;
  79. numPaths = 1;
  80. } else {
  81. paths = s_defaultPaths;
  82. numPaths = SDL_arraysize(s_defaultPaths);
  83. }
  84. for (i = 0; i < numPaths && _this->vulkan_config.loader_handle == NULL; i++) {
  85. foundPath = paths[i];
  86. _this->vulkan_config.loader_handle = SDL_LoadObject(foundPath);
  87. }
  88. if (_this->vulkan_config.loader_handle == NULL) {
  89. return SDL_SetError("Failed to load Vulkan Portability library");
  90. }
  91. SDL_strlcpy(_this->vulkan_config.loader_path, foundPath,
  92. SDL_arraysize(_this->vulkan_config.loader_path));
  93. vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction(
  94. _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr");
  95. if (!vkGetInstanceProcAddr) {
  96. SDL_SetError("Failed to load vkGetInstanceProcAddr from Vulkan Portability library");
  97. goto fail;
  98. }
  99. }
  100. _this->vulkan_config.vkGetInstanceProcAddr = (SDL_FunctionPointer)vkGetInstanceProcAddr;
  101. _this->vulkan_config.vkEnumerateInstanceExtensionProperties =
  102. (SDL_FunctionPointer)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)(
  103. VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
  104. if (!_this->vulkan_config.vkEnumerateInstanceExtensionProperties) {
  105. goto fail;
  106. }
  107. extensions = SDL_Vulkan_CreateInstanceExtensionsList(
  108. (PFN_vkEnumerateInstanceExtensionProperties)
  109. _this->vulkan_config.vkEnumerateInstanceExtensionProperties,
  110. &extensionCount);
  111. if (!extensions) {
  112. goto fail;
  113. }
  114. for (i = 0; i < extensionCount; i++) {
  115. if (SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) {
  116. hasSurfaceExtension = true;
  117. } else if (SDL_strcmp(VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) {
  118. hasHeadlessSurfaceExtension = true;
  119. }
  120. }
  121. SDL_free(extensions);
  122. if (!hasSurfaceExtension) {
  123. SDL_SetError("Installed Vulkan doesn't implement the " VK_KHR_SURFACE_EXTENSION_NAME " extension");
  124. goto fail;
  125. }
  126. if (!hasHeadlessSurfaceExtension) {
  127. #if (HEADLESS_SURFACE_EXTENSION_REQUIRED_TO_LOAD != 0)
  128. SDL_SetError("Installed Vulkan doesn't implement the " VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME " extension");
  129. goto fail;
  130. #else
  131. // Let's at least leave a breadcrumb for people to find if they have issues
  132. SDL_Log("Installed Vulkan doesn't implement the " VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME " extension");
  133. #endif
  134. }
  135. return true;
  136. fail:
  137. SDL_UnloadObject(_this->vulkan_config.loader_handle);
  138. _this->vulkan_config.loader_handle = NULL;
  139. return false;
  140. }
  141. void OFFSCREEN_Vulkan_UnloadLibrary(SDL_VideoDevice *_this)
  142. {
  143. if (_this->vulkan_config.loader_handle) {
  144. SDL_UnloadObject(_this->vulkan_config.loader_handle);
  145. _this->vulkan_config.loader_handle = NULL;
  146. }
  147. }
  148. char const *const *OFFSCREEN_Vulkan_GetInstanceExtensions(SDL_VideoDevice *_this,
  149. Uint32 *count)
  150. {
  151. #if (HEADLESS_SURFACE_EXTENSION_REQUIRED_TO_LOAD == 0)
  152. VkExtensionProperties *enumerateExtensions = NULL;
  153. Uint32 enumerateExtensionCount = 0;
  154. bool hasHeadlessSurfaceExtension = false;
  155. Uint32 i;
  156. #endif
  157. static const char *const returnExtensions[] = { VK_KHR_SURFACE_EXTENSION_NAME, VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME };
  158. if (count) {
  159. # if (HEADLESS_SURFACE_EXTENSION_REQUIRED_TO_LOAD == 0)
  160. {
  161. /* In optional mode, only return VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME if it's already supported by the instance
  162. There's probably a better way to cache the presence of the extension during OFFSCREEN_Vulkan_LoadLibrary().
  163. But both SDL_VideoData and SDL_VideoDevice::vulkan_config seem like I'd need to touch a bunch of code to do properly.
  164. And I want a smaller footprint for the first pass*/
  165. if ( _this->vulkan_config.vkEnumerateInstanceExtensionProperties ) {
  166. enumerateExtensions = SDL_Vulkan_CreateInstanceExtensionsList(
  167. (PFN_vkEnumerateInstanceExtensionProperties)
  168. _this->vulkan_config.vkEnumerateInstanceExtensionProperties,
  169. &enumerateExtensionCount);
  170. for (i = 0; i < enumerateExtensionCount; i++) {
  171. if (SDL_strcmp(VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME, enumerateExtensions[i].extensionName) == 0) {
  172. hasHeadlessSurfaceExtension = true;
  173. }
  174. }
  175. SDL_free(enumerateExtensions);
  176. }
  177. if ( hasHeadlessSurfaceExtension == true ) {
  178. *count = SDL_arraysize(returnExtensions);
  179. } else {
  180. *count = SDL_arraysize(returnExtensions) - 1; // assumes VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME is last
  181. }
  182. }
  183. # else
  184. {
  185. *count = SDL_arraysize(returnExtensions);
  186. }
  187. # endif
  188. }
  189. return returnExtensions;
  190. }
  191. bool OFFSCREEN_Vulkan_CreateSurface(SDL_VideoDevice *_this,
  192. SDL_Window *window,
  193. VkInstance instance,
  194. const struct VkAllocationCallbacks *allocator,
  195. VkSurfaceKHR *surface)
  196. {
  197. *surface = VK_NULL_HANDLE;
  198. if (!_this->vulkan_config.loader_handle) {
  199. return SDL_SetError("Vulkan is not loaded");
  200. }
  201. PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr;
  202. PFN_vkCreateHeadlessSurfaceEXT vkCreateHeadlessSurfaceEXT =
  203. (PFN_vkCreateHeadlessSurfaceEXT)vkGetInstanceProcAddr(instance, "vkCreateHeadlessSurfaceEXT");
  204. VkHeadlessSurfaceCreateInfoEXT createInfo;
  205. VkResult result;
  206. if (!vkCreateHeadlessSurfaceEXT) {
  207. /* This may be surprising to the consumer when HEADLESS_SURFACE_EXTENSION_REQUIRED_TO_LOAD == 0
  208. But this is the tradeoff for allowing offscreen rendering to a buffer to continue working without requiring the extension during driver load */
  209. return SDL_SetError(VK_EXT_HEADLESS_SURFACE_EXTENSION_NAME
  210. " extension is not enabled in the Vulkan instance.");
  211. }
  212. SDL_zero(createInfo);
  213. createInfo.sType = VK_STRUCTURE_TYPE_HEADLESS_SURFACE_CREATE_INFO_EXT;
  214. createInfo.pNext = NULL;
  215. createInfo.flags = 0;
  216. result = vkCreateHeadlessSurfaceEXT(instance, &createInfo, allocator, surface);
  217. if (result != VK_SUCCESS) {
  218. return SDL_SetError("vkCreateHeadlessSurfaceEXT failed: %s", SDL_Vulkan_GetResultString(result));
  219. }
  220. return true;
  221. }
  222. void OFFSCREEN_Vulkan_DestroySurface(SDL_VideoDevice *_this,
  223. VkInstance instance,
  224. VkSurfaceKHR surface,
  225. const struct VkAllocationCallbacks *allocator)
  226. {
  227. if (_this->vulkan_config.loader_handle) {
  228. SDL_Vulkan_DestroySurface_Internal(_this->vulkan_config.vkGetInstanceProcAddr, instance, surface, allocator);
  229. }
  230. }
  231. #endif