SDL_vulkan.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. * \file SDL_vulkan.h
  20. *
  21. * Header file for functions to creating Vulkan surfaces on SDL windows.
  22. */
  23. #ifndef SDL_vulkan_h_
  24. #define SDL_vulkan_h_
  25. #include "SDL_video.h"
  26. #include "begin_code.h"
  27. /* Set up for C function definitions, even when using C++ */
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /* Avoid including vulkan.h */
  32. #define VK_DEFINE_HANDLE(object) typedef struct object##_T* object;
  33. #if defined(__LP64__) || defined(_WIN64) || defined(__x86_64__) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)
  34. #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef struct object##_T *object;
  35. #else
  36. #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef uint64_t object;
  37. #endif
  38. VK_DEFINE_HANDLE(VkInstance)
  39. VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkSurfaceKHR)
  40. typedef VkInstance SDL_vulkanInstance;
  41. typedef VkSurfaceKHR SDL_vulkanSurface; /* for compatibility with Tizen */
  42. /**
  43. * \name Vulkan support functions
  44. *
  45. * \note SDL_Vulkan_GetInstanceExtensions & SDL_Vulkan_CreateSurface API
  46. * is compatable with Tizen's implementation of Vulkan in SDL.
  47. */
  48. /* @{ */
  49. /**
  50. * \brief Dynamically load a Vulkan loader library.
  51. *
  52. * \param [in] path The platform dependent Vulkan loader library name, or
  53. * \c NULL to open the default Vulkan loader library.
  54. *
  55. * \return \c 0 on success, or \c -1 if the library couldn't be loaded.
  56. *
  57. * This should be done after initializing the video driver, but before
  58. * creating any Vulkan windows. If no Vulkan loader library is loaded, the
  59. * default library will be loaded upon creation of the first Vulkan window.
  60. *
  61. * \note If you specify a non-NULL \a path, you should retrieve all of the
  62. * Vulkan functions used in your program from the dynamic library using
  63. * \c SDL_Vulkan_GetVkGetInstanceProcAddr() unless you can guarantee
  64. * \a path points to the same vulkan loader library that you linked to.
  65. *
  66. * \note On Apple devices, if \a path is NULL, SDL will attempt to find
  67. * the vkGetInstanceProcAddr address within all the mach-o images of
  68. * the current process. This is because the currently (v0.17.0)
  69. * recommended MoltenVK (Vulkan on Metal) usage is as a static library.
  70. * If it is not found then SDL will attempt to load \c libMoltenVK.dylib.
  71. * Applications using the dylib alternative therefore do not need to do
  72. * anything special when calling SDL.
  73. *
  74. * \note On non-Apple devices, SDL requires you to either not link to the
  75. * Vulkan loader or link to a dynamic library version. This limitation
  76. * may be removed in a future version of SDL.
  77. *
  78. * \note This function will fail if there are no working Vulkan drivers
  79. * installed.
  80. *
  81. * \sa SDL_Vulkan_GetVkGetInstanceProcAddr()
  82. * \sa SDL_Vulkan_UnloadLibrary()
  83. */
  84. extern DECLSPEC int SDLCALL SDL_Vulkan_LoadLibrary(const char *path);
  85. /**
  86. * \brief Get the address of the \c vkGetInstanceProcAddr function.
  87. *
  88. * \note This should be called after either calling SDL_Vulkan_LoadLibrary
  89. * or creating an SDL_Window with the SDL_WINDOW_VULKAN flag.
  90. */
  91. extern DECLSPEC void *SDLCALL SDL_Vulkan_GetVkGetInstanceProcAddr(void);
  92. /**
  93. * \brief Unload the Vulkan loader library previously loaded by
  94. * \c SDL_Vulkan_LoadLibrary().
  95. *
  96. * \sa SDL_Vulkan_LoadLibrary()
  97. */
  98. extern DECLSPEC void SDLCALL SDL_Vulkan_UnloadLibrary(void);
  99. /**
  100. * \brief Get the names of the Vulkan instance extensions needed to create
  101. * a surface with \c SDL_Vulkan_CreateSurface().
  102. *
  103. * \param [in] window Window for which the required Vulkan instance
  104. * extensions should be retrieved
  105. * \param [in,out] count pointer to an \c unsigned related to the number of
  106. * required Vulkan instance extensions
  107. * \param [out] names \c NULL or a pointer to an array to be filled with the
  108. * required Vulkan instance extensions
  109. *
  110. * \return \c SDL_TRUE on success, \c SDL_FALSE on error.
  111. *
  112. * If \a pNames is \c NULL, then the number of required Vulkan instance
  113. * extensions is returned in pCount. Otherwise, \a pCount must point to a
  114. * variable set to the number of elements in the \a pNames array, and on
  115. * return the variable is overwritten with the number of names actually
  116. * written to \a pNames. If \a pCount is less than the number of required
  117. * extensions, at most \a pCount structures will be written. If \a pCount
  118. * is smaller than the number of required extensions, \c SDL_FALSE will be
  119. * returned instead of \c SDL_TRUE, to indicate that not all the required
  120. * extensions were returned.
  121. *
  122. * \note The returned list of extensions will contain \c VK_KHR_surface
  123. * and zero or more platform specific extensions
  124. *
  125. * \note The extension names queried here must be enabled when calling
  126. * VkCreateInstance, otherwise surface creation will fail.
  127. *
  128. * \note \c window should have been created with the \c SDL_WINDOW_VULKAN flag.
  129. *
  130. * \code
  131. * unsigned count;
  132. * // get count of required extensions
  133. * if(!SDL_Vulkan_GetInstanceExtensions(window, &count, NULL))
  134. * handle_error();
  135. *
  136. * static const char *const additionalExtensions[] =
  137. * {
  138. * VK_EXT_DEBUG_REPORT_EXTENSION_NAME, // example additional extension
  139. * };
  140. * size_t additionalExtensionsCount = sizeof(additionalExtensions) / sizeof(additionalExtensions[0]);
  141. * size_t extensionCount = count + additionalExtensionsCount;
  142. * const char **names = malloc(sizeof(const char *) * extensionCount);
  143. * if(!names)
  144. * handle_error();
  145. *
  146. * // get names of required extensions
  147. * if(!SDL_Vulkan_GetInstanceExtensions(window, &count, names))
  148. * handle_error();
  149. *
  150. * // copy additional extensions after required extensions
  151. * for(size_t i = 0; i < additionalExtensionsCount; i++)
  152. * names[i + count] = additionalExtensions[i];
  153. *
  154. * VkInstanceCreateInfo instanceCreateInfo = {};
  155. * instanceCreateInfo.enabledExtensionCount = extensionCount;
  156. * instanceCreateInfo.ppEnabledExtensionNames = names;
  157. * // fill in rest of instanceCreateInfo
  158. *
  159. * VkInstance instance;
  160. * // create the Vulkan instance
  161. * VkResult result = vkCreateInstance(&instanceCreateInfo, NULL, &instance);
  162. * free(names);
  163. * \endcode
  164. *
  165. * \sa SDL_Vulkan_CreateSurface()
  166. */
  167. extern DECLSPEC SDL_bool SDLCALL SDL_Vulkan_GetInstanceExtensions(
  168. SDL_Window *window,
  169. unsigned *pCount,
  170. const char **pNames);
  171. /**
  172. * \brief Create a Vulkan rendering surface for a window.
  173. *
  174. * \param [in] window SDL_Window to which to attach the rendering surface.
  175. * \param [in] instance handle to the Vulkan instance to use.
  176. * \param [out] surface pointer to a VkSurfaceKHR handle to receive the
  177. * handle of the newly created surface.
  178. *
  179. * \return \c SDL_TRUE on success, \c SDL_FALSE on error.
  180. *
  181. * \code
  182. * VkInstance instance;
  183. * SDL_Window *window;
  184. *
  185. * // create instance and window
  186. *
  187. * // create the Vulkan surface
  188. * VkSurfaceKHR surface;
  189. * if(!SDL_Vulkan_CreateSurface(window, instance, &surface))
  190. * handle_error();
  191. * \endcode
  192. *
  193. * \note \a window should have been created with the \c SDL_WINDOW_VULKAN flag.
  194. *
  195. * \note \a instance should have been created with the extensions returned
  196. * by \c SDL_Vulkan_CreateSurface() enabled.
  197. *
  198. * \sa SDL_Vulkan_GetInstanceExtensions()
  199. */
  200. extern DECLSPEC SDL_bool SDLCALL SDL_Vulkan_CreateSurface(
  201. SDL_Window *window,
  202. VkInstance instance,
  203. VkSurfaceKHR* surface);
  204. /**
  205. * \brief Get the size of a window's underlying drawable in pixels (for use
  206. * with setting viewport, scissor & etc).
  207. *
  208. * \param window SDL_Window from which the drawable size should be queried
  209. * \param w Pointer to variable for storing the width in pixels,
  210. * may be NULL
  211. * \param h Pointer to variable for storing the height in pixels,
  212. * may be NULL
  213. *
  214. * This may differ from SDL_GetWindowSize() if we're rendering to a high-DPI
  215. * drawable, i.e. the window was created with SDL_WINDOW_ALLOW_HIGHDPI on a
  216. * platform with high-DPI support (Apple calls this "Retina"), and not disabled
  217. * by the \c SDL_HINT_VIDEO_HIGHDPI_DISABLED hint.
  218. *
  219. * \sa SDL_GetWindowSize()
  220. * \sa SDL_CreateWindow()
  221. */
  222. extern DECLSPEC void SDLCALL SDL_Vulkan_GetDrawableSize(SDL_Window * window,
  223. int *w, int *h);
  224. /* @} *//* Vulkan support functions */
  225. /* Ends C function definitions when using C++ */
  226. #ifdef __cplusplus
  227. }
  228. #endif
  229. #include "close_code.h"
  230. #endif /* SDL_vulkan_h_ */