SDL_vulkan.h 9.6 KB

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