SDL_vulkan.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. #endif /* !NO_SDL_VULKAN_TYPEDEFS */
  62. /**
  63. * \name Vulkan support functions
  64. */
  65. /* @{ */
  66. /**
  67. * Dynamically load the Vulkan loader library.
  68. *
  69. * This should be called after initializing the video driver, but before
  70. * creating any Vulkan windows. If no Vulkan loader library is loaded, the
  71. * default library will be loaded upon creation of the first Vulkan window.
  72. *
  73. * It is fairly common for Vulkan applications to link with libvulkan instead
  74. * of explicitly loading it at run time. This will work with SDL provided the
  75. * application links to a dynamic library and both it and SDL use the same
  76. * search path.
  77. *
  78. * If you specify a non-NULL `path`, an application should retrieve all of the
  79. * Vulkan functions it uses from the dynamic library using
  80. * SDL_Vulkan_GetVkGetInstanceProcAddr unless you can guarantee `path` points
  81. * to the same vulkan loader library the application linked to.
  82. *
  83. * On Apple devices, if `path` is NULL, SDL will attempt to find the
  84. * `vkGetInstanceProcAddr` address within all the Mach-O images of the current
  85. * process. This is because it is fairly common for Vulkan applications to
  86. * link with libvulkan (and historically MoltenVK was provided as a static
  87. * library). If it is not found, on macOS, SDL will attempt to load
  88. * `vulkan.framework/vulkan`, `libvulkan.1.dylib`,
  89. * `MoltenVK.framework/MoltenVK`, and `libMoltenVK.dylib`, in that order. On
  90. * iOS, SDL will attempt to load `libMoltenVK.dylib`. Applications using a
  91. * dynamic framework or .dylib must ensure it is included in its application
  92. * bundle.
  93. *
  94. * On non-Apple devices, application linking with a static libvulkan is not
  95. * supported. Either do not link to the Vulkan loader or link to a dynamic
  96. * library version.
  97. *
  98. * \param path the platform dependent Vulkan loader library name or NULL.
  99. * \returns true on success or false on failure; call SDL_GetError() for more
  100. * information.
  101. *
  102. * \since This function is available since SDL 3.1.3.
  103. *
  104. * \sa SDL_Vulkan_GetVkGetInstanceProcAddr
  105. * \sa SDL_Vulkan_UnloadLibrary
  106. */
  107. extern SDL_DECLSPEC bool SDLCALL SDL_Vulkan_LoadLibrary(const char *path);
  108. /**
  109. * Get the address of the `vkGetInstanceProcAddr` function.
  110. *
  111. * This should be called after either calling SDL_Vulkan_LoadLibrary() or
  112. * creating an SDL_Window with the `SDL_WINDOW_VULKAN` flag.
  113. *
  114. * The actual type of the returned function pointer is
  115. * PFN_vkGetInstanceProcAddr, but that isn't available because the Vulkan
  116. * headers are not included here. You should cast the return value of this
  117. * function to that type, e.g.
  118. *
  119. * `vkGetInstanceProcAddr =
  120. * (PFN_vkGetInstanceProcAddr)SDL_Vulkan_GetVkGetInstanceProcAddr();`
  121. *
  122. * \returns the function pointer for `vkGetInstanceProcAddr` or NULL on
  123. * failure; call SDL_GetError() for more information.
  124. *
  125. * \since This function is available since SDL 3.1.3.
  126. */
  127. extern SDL_DECLSPEC SDL_FunctionPointer SDLCALL SDL_Vulkan_GetVkGetInstanceProcAddr(void);
  128. /**
  129. * Unload the Vulkan library previously loaded by SDL_Vulkan_LoadLibrary().
  130. *
  131. * \since This function is available since SDL 3.1.3.
  132. *
  133. * \sa SDL_Vulkan_LoadLibrary
  134. */
  135. extern SDL_DECLSPEC void SDLCALL SDL_Vulkan_UnloadLibrary(void);
  136. /**
  137. * Get the Vulkan instance extensions needed for vkCreateInstance.
  138. *
  139. * This should be called after either calling SDL_Vulkan_LoadLibrary() or
  140. * creating an SDL_Window with the `SDL_WINDOW_VULKAN` flag.
  141. *
  142. * On return, the variable pointed to by `count` will be set to the number of
  143. * elements returned, suitable for using with
  144. * VkInstanceCreateInfo::enabledExtensionCount, and the returned array can be
  145. * used with VkInstanceCreateInfo::ppEnabledExtensionNames, for calling
  146. * Vulkan's vkCreateInstance API.
  147. *
  148. * You should not free the returned array; it is owned by SDL.
  149. *
  150. * \param count a pointer filled in with the number of extensions returned.
  151. * \returns an array of extension name strings on success, NULL on failure;
  152. * call SDL_GetError() for more information.
  153. *
  154. * \since This function is available since SDL 3.1.3.
  155. *
  156. * \sa SDL_Vulkan_CreateSurface
  157. */
  158. extern SDL_DECLSPEC char const * const * SDLCALL SDL_Vulkan_GetInstanceExtensions(Uint32 *count);
  159. /**
  160. * Create a Vulkan rendering surface for a window.
  161. *
  162. * The `window` must have been created with the `SDL_WINDOW_VULKAN` flag and
  163. * `instance` must have been created with extensions returned by
  164. * SDL_Vulkan_GetInstanceExtensions() enabled.
  165. *
  166. * If `allocator` is NULL, Vulkan will use the system default allocator. This
  167. * argument is passed directly to Vulkan and isn't used by SDL itself.
  168. *
  169. * \param window the window to which to attach the Vulkan surface.
  170. * \param instance the Vulkan instance handle.
  171. * \param allocator a VkAllocationCallbacks struct, which lets the app set the
  172. * allocator that creates the surface. Can be NULL.
  173. * \param surface a pointer to a VkSurfaceKHR handle to output the newly
  174. * created surface.
  175. * \returns true on success or false on failure; call SDL_GetError() for more
  176. * information.
  177. *
  178. * \since This function is available since SDL 3.1.3.
  179. *
  180. * \sa SDL_Vulkan_GetInstanceExtensions
  181. * \sa SDL_Vulkan_DestroySurface
  182. */
  183. extern SDL_DECLSPEC bool SDLCALL SDL_Vulkan_CreateSurface(SDL_Window *window,
  184. VkInstance instance,
  185. const struct VkAllocationCallbacks *allocator,
  186. VkSurfaceKHR* surface);
  187. /**
  188. * Destroy the Vulkan rendering surface of a window.
  189. *
  190. * This should be called before SDL_DestroyWindow, if SDL_Vulkan_CreateSurface
  191. * was called after SDL_CreateWindow.
  192. *
  193. * The `instance` must have been created with extensions returned by
  194. * SDL_Vulkan_GetInstanceExtensions() enabled and `surface` must have been
  195. * created successfully by an SDL_Vulkan_CreateSurface() call.
  196. *
  197. * If `allocator` is NULL, Vulkan will use the system default allocator. This
  198. * argument is passed directly to Vulkan and isn't used by SDL itself.
  199. *
  200. * \param instance the Vulkan instance handle.
  201. * \param surface vkSurfaceKHR handle to destroy.
  202. * \param allocator a VkAllocationCallbacks struct, which lets the app set the
  203. * allocator that destroys the surface. Can be NULL.
  204. *
  205. * \since This function is available since SDL 3.1.3.
  206. *
  207. * \sa SDL_Vulkan_GetInstanceExtensions
  208. * \sa SDL_Vulkan_CreateSurface
  209. */
  210. extern SDL_DECLSPEC void SDLCALL SDL_Vulkan_DestroySurface(VkInstance instance,
  211. VkSurfaceKHR surface,
  212. const struct VkAllocationCallbacks *allocator);
  213. /**
  214. * Query support for presentation via a given physical device and queue
  215. * family.
  216. *
  217. * The `instance` must have been created with extensions returned by
  218. * SDL_Vulkan_GetInstanceExtensions() enabled.
  219. *
  220. * \param instance the Vulkan instance handle.
  221. * \param physicalDevice a valid Vulkan physical device handle.
  222. * \param queueFamilyIndex a valid queue family index for the given physical
  223. * device.
  224. * \returns true if supported, false if unsupported or an error occurred.
  225. *
  226. * \since This function is available since SDL 3.1.3.
  227. *
  228. * \sa SDL_Vulkan_GetInstanceExtensions
  229. */
  230. extern SDL_DECLSPEC bool SDLCALL SDL_Vulkan_GetPresentationSupport(VkInstance instance,
  231. VkPhysicalDevice physicalDevice,
  232. Uint32 queueFamilyIndex);
  233. /* @} *//* Vulkan support functions */
  234. /* Ends C function definitions when using C++ */
  235. #ifdef __cplusplus
  236. }
  237. #endif
  238. #include <SDL3/SDL_close_code.h>
  239. #endif /* SDL_vulkan_h_ */