SDL_vulkan.h 10 KB

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