SDL_vulkan.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 2017, Mark Callow
  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. /**
  19. * # CategoryVulkan
  20. *
  21. * Functions for creating Vulkan surfaces on SDL windows.
  22. *
  23. * For the most part, Vulkan operates independent of SDL, but it benefits from
  24. * a little support during setup.
  25. *
  26. * Use SDL_Vulkan_GetInstanceExtensions() to get platform-specific bits for
  27. * creating a VkInstance, then SDL_Vulkan_GetVkGetInstanceProcAddr() to get
  28. * the appropriate function for querying Vulkan entry points. Then
  29. * SDL_Vulkan_CreateSurface() will get you the final pieces you need to
  30. * prepare for rendering into an SDL_Window with Vulkan.
  31. *
  32. * Unlike OpenGL, most of the details of "context" creation and window buffer
  33. * swapping are handled by the Vulkan API directly, so SDL doesn't provide
  34. * Vulkan equivalents of SDL_GL_SwapWindow(), etc; they aren't necessary.
  35. */
  36. #ifndef SDL_vulkan_h_
  37. #define SDL_vulkan_h_
  38. #include <SDL3/SDL_stdinc.h>
  39. #include <SDL3/SDL_error.h>
  40. #include <SDL3/SDL_video.h>
  41. #include <SDL3/SDL_begin_code.h>
  42. /* Set up for C function definitions, even when using C++ */
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /* Avoid including vulkan.h, don't define VkInstance if it's already included */
  47. #ifdef VULKAN_H_
  48. #define NO_SDL_VULKAN_TYPEDEFS
  49. #endif
  50. #ifndef NO_SDL_VULKAN_TYPEDEFS
  51. #define VK_DEFINE_HANDLE(object) typedef struct object##_T* object;
  52. #if defined(__LP64__) || defined(_WIN64) || defined(__x86_64__) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)
  53. #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef struct object##_T *object;
  54. #else
  55. #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef uint64_t object;
  56. #endif
  57. VK_DEFINE_HANDLE(VkInstance)
  58. VK_DEFINE_HANDLE(VkPhysicalDevice)
  59. VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkSurfaceKHR)
  60. struct VkAllocationCallbacks;
  61. /* Make sure to undef to avoid issues in case of later vulkan include */
  62. #undef VK_DEFINE_HANDLE
  63. #undef VK_DEFINE_NON_DISPATCHABLE_HANDLE
  64. #endif /* !NO_SDL_VULKAN_TYPEDEFS */
  65. /**
  66. * \name Vulkan support functions
  67. */
  68. /* @{ */
  69. /**
  70. * Dynamically load the Vulkan loader library.
  71. *
  72. * This should be called after initializing the video driver, but before
  73. * creating any Vulkan windows. If no Vulkan loader library is loaded, the
  74. * default library will be loaded upon creation of the first Vulkan window.
  75. *
  76. * SDL keeps a counter of how many times this function has been successfully
  77. * called, so it is safe to call this function multiple times, so long as it
  78. * is eventually paired with an equivalent number of calls to
  79. * SDL_Vulkan_UnloadLibrary. The `path` argument is ignored unless there is no
  80. * library currently loaded, and and the library isn't actually unloaded until
  81. * there have been an equivalent number of calls to SDL_Vulkan_UnloadLibrary.
  82. *
  83. * It is fairly common for Vulkan applications to link with libvulkan instead
  84. * of explicitly loading it at run time. This will work with SDL provided the
  85. * application links to a dynamic library and both it and SDL use the same
  86. * search path.
  87. *
  88. * If you specify a non-NULL `path`, an application should retrieve all of the
  89. * Vulkan functions it uses from the dynamic library using
  90. * SDL_Vulkan_GetVkGetInstanceProcAddr unless you can guarantee `path` points
  91. * to the same vulkan loader library the application linked to.
  92. *
  93. * On Apple devices, if `path` is NULL, SDL will attempt to find the
  94. * `vkGetInstanceProcAddr` address within all the Mach-O images of the current
  95. * process. This is because it is fairly common for Vulkan applications to
  96. * link with libvulkan (and historically MoltenVK was provided as a static
  97. * library). If it is not found, on macOS, SDL will attempt to load
  98. * `vulkan.framework/vulkan`, `libvulkan.1.dylib`,
  99. * `MoltenVK.framework/MoltenVK`, and `libMoltenVK.dylib`, in that order. On
  100. * iOS, SDL will attempt to load `libMoltenVK.dylib`. Applications using a
  101. * dynamic framework or .dylib must ensure it is included in its application
  102. * bundle.
  103. *
  104. * On non-Apple devices, application linking with a static libvulkan is not
  105. * supported. Either do not link to the Vulkan loader or link to a dynamic
  106. * library version.
  107. *
  108. * \param path the platform dependent Vulkan loader library name or NULL.
  109. * \returns true on success or false on failure; call SDL_GetError() for more
  110. * information.
  111. *
  112. * \threadsafety This function is not thread safe.
  113. *
  114. * \since This function is available since SDL 3.1.3.
  115. *
  116. * \sa SDL_Vulkan_GetVkGetInstanceProcAddr
  117. * \sa SDL_Vulkan_UnloadLibrary
  118. */
  119. extern SDL_DECLSPEC bool SDLCALL SDL_Vulkan_LoadLibrary(const char *path);
  120. /**
  121. * Get the address of the `vkGetInstanceProcAddr` function.
  122. *
  123. * This should be called after either calling SDL_Vulkan_LoadLibrary() or
  124. * creating an SDL_Window with the `SDL_WINDOW_VULKAN` flag.
  125. *
  126. * The actual type of the returned function pointer is
  127. * PFN_vkGetInstanceProcAddr, but that isn't available because the Vulkan
  128. * headers are not included here. You should cast the return value of this
  129. * function to that type, e.g.
  130. *
  131. * `vkGetInstanceProcAddr =
  132. * (PFN_vkGetInstanceProcAddr)SDL_Vulkan_GetVkGetInstanceProcAddr();`
  133. *
  134. * \returns the function pointer for `vkGetInstanceProcAddr` or NULL on
  135. * failure; call SDL_GetError() for more information.
  136. *
  137. * \since This function is available since SDL 3.1.3.
  138. */
  139. extern SDL_DECLSPEC SDL_FunctionPointer SDLCALL SDL_Vulkan_GetVkGetInstanceProcAddr(void);
  140. /**
  141. * Unload the Vulkan library previously loaded by SDL_Vulkan_LoadLibrary().
  142. *
  143. * SDL keeps a counter of how many times this function has been called, so it
  144. * is safe to call this function multiple times, so long as it is paired with
  145. * an equivalent number of calls to SDL_Vulkan_LoadLibrary. The library isn't
  146. * actually unloaded until there have been an equivalent number of calls to
  147. * SDL_Vulkan_UnloadLibrary.
  148. *
  149. * Once the library has actually been unloaded, if any Vulkan instances
  150. * remain, they will likely crash the program. Clean up any existing Vulkan
  151. * resources, and destroy appropriate windows, renderers and GPU devices
  152. * before calling this function.
  153. *
  154. * \threadsafety This function is not thread safe.
  155. *
  156. * \since This function is available since SDL 3.1.3.
  157. *
  158. * \sa SDL_Vulkan_LoadLibrary
  159. */
  160. extern SDL_DECLSPEC void SDLCALL SDL_Vulkan_UnloadLibrary(void);
  161. /**
  162. * Get the Vulkan instance extensions needed for vkCreateInstance.
  163. *
  164. * This should be called after either calling SDL_Vulkan_LoadLibrary() or
  165. * creating an SDL_Window with the `SDL_WINDOW_VULKAN` flag.
  166. *
  167. * On return, the variable pointed to by `count` will be set to the number of
  168. * elements returned, suitable for using with
  169. * VkInstanceCreateInfo::enabledExtensionCount, and the returned array can be
  170. * used with VkInstanceCreateInfo::ppEnabledExtensionNames, for calling
  171. * Vulkan's vkCreateInstance API.
  172. *
  173. * You should not free the returned array; it is owned by SDL.
  174. *
  175. * \param count a pointer filled in with the number of extensions returned.
  176. * \returns an array of extension name strings on success, NULL on failure;
  177. * call SDL_GetError() for more information.
  178. *
  179. * \since This function is available since SDL 3.1.3.
  180. *
  181. * \sa SDL_Vulkan_CreateSurface
  182. */
  183. extern SDL_DECLSPEC char const * const * SDLCALL SDL_Vulkan_GetInstanceExtensions(Uint32 *count);
  184. /**
  185. * Create a Vulkan rendering surface for a window.
  186. *
  187. * The `window` must have been created with the `SDL_WINDOW_VULKAN` flag and
  188. * `instance` must have been created with extensions returned by
  189. * SDL_Vulkan_GetInstanceExtensions() enabled.
  190. *
  191. * If `allocator` is NULL, Vulkan will use the system default allocator. This
  192. * argument is passed directly to Vulkan and isn't used by SDL itself.
  193. *
  194. * \param window the window to which to attach the Vulkan surface.
  195. * \param instance the Vulkan instance handle.
  196. * \param allocator a VkAllocationCallbacks struct, which lets the app set the
  197. * allocator that creates the surface. Can be NULL.
  198. * \param surface a pointer to a VkSurfaceKHR handle to output the newly
  199. * created surface.
  200. * \returns true on success or false on failure; call SDL_GetError() for more
  201. * information.
  202. *
  203. * \since This function is available since SDL 3.1.3.
  204. *
  205. * \sa SDL_Vulkan_GetInstanceExtensions
  206. * \sa SDL_Vulkan_DestroySurface
  207. */
  208. extern SDL_DECLSPEC bool SDLCALL SDL_Vulkan_CreateSurface(SDL_Window *window,
  209. VkInstance instance,
  210. const struct VkAllocationCallbacks *allocator,
  211. VkSurfaceKHR* surface);
  212. /**
  213. * Destroy the Vulkan rendering surface of a window.
  214. *
  215. * This should be called before SDL_DestroyWindow, if SDL_Vulkan_CreateSurface
  216. * was called after SDL_CreateWindow.
  217. *
  218. * The `instance` must have been created with extensions returned by
  219. * SDL_Vulkan_GetInstanceExtensions() enabled and `surface` must have been
  220. * created successfully by an SDL_Vulkan_CreateSurface() call.
  221. *
  222. * If `allocator` is NULL, Vulkan will use the system default allocator. This
  223. * argument is passed directly to Vulkan and isn't used by SDL itself.
  224. *
  225. * \param instance the Vulkan instance handle.
  226. * \param surface vkSurfaceKHR handle to destroy.
  227. * \param allocator a VkAllocationCallbacks struct, which lets the app set the
  228. * allocator that destroys the surface. Can be NULL.
  229. *
  230. * \since This function is available since SDL 3.1.3.
  231. *
  232. * \sa SDL_Vulkan_GetInstanceExtensions
  233. * \sa SDL_Vulkan_CreateSurface
  234. */
  235. extern SDL_DECLSPEC void SDLCALL SDL_Vulkan_DestroySurface(VkInstance instance,
  236. VkSurfaceKHR surface,
  237. const struct VkAllocationCallbacks *allocator);
  238. /**
  239. * Query support for presentation via a given physical device and queue
  240. * family.
  241. *
  242. * The `instance` must have been created with extensions returned by
  243. * SDL_Vulkan_GetInstanceExtensions() enabled.
  244. *
  245. * \param instance the Vulkan instance handle.
  246. * \param physicalDevice a valid Vulkan physical device handle.
  247. * \param queueFamilyIndex a valid queue family index for the given physical
  248. * device.
  249. * \returns true if supported, false if unsupported or an error occurred.
  250. *
  251. * \since This function is available since SDL 3.1.3.
  252. *
  253. * \sa SDL_Vulkan_GetInstanceExtensions
  254. */
  255. extern SDL_DECLSPEC bool SDLCALL SDL_Vulkan_GetPresentationSupport(VkInstance instance,
  256. VkPhysicalDevice physicalDevice,
  257. Uint32 queueFamilyIndex);
  258. /* @} *//* Vulkan support functions */
  259. /* Ends C function definitions when using C++ */
  260. #ifdef __cplusplus
  261. }
  262. #endif
  263. #include <SDL3/SDL_close_code.h>
  264. #endif /* SDL_vulkan_h_ */