SDL_vulkan.h 9.6 KB

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