SDL_vulkan.h 8.5 KB

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