SDL 3.0
SDL_vulkan.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 2017, Mark Callow
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/**
23 * # CategoryVulkan
24 *
25 * Functions for creating Vulkan surfaces on SDL windows.
26 *
27 * For the most part, Vulkan operates independent of SDL, but it benefits from
28 * a little support during setup.
29 *
30 * Use SDL_Vulkan_GetInstanceExtensions() to get platform-specific bits for
31 * creating a VkInstance, then SDL_Vulkan_GetVkGetInstanceProcAddr() to get
32 * the appropriate function for querying Vulkan entry points. Then
33 * SDL_Vulkan_CreateSurface() will get you the final pieces you need to
34 * prepare for rendering into an SDL_Window with Vulkan.
35 *
36 * Unlike OpenGL, most of the details of "context" creation and window buffer
37 * swapping are handled by the Vulkan API directly, so SDL doesn't provide
38 * Vulkan equivalents of SDL_GL_SwapWindow(), etc; they aren't necessary.
39 */
40
41#ifndef SDL_vulkan_h_
42#define SDL_vulkan_h_
43
44#include <SDL3/SDL_stdinc.h>
45#include <SDL3/SDL_error.h>
46#include <SDL3/SDL_video.h>
47
48#include <SDL3/SDL_begin_code.h>
49/* Set up for C function definitions, even when using C++ */
50#ifdef __cplusplus
51extern "C" {
52#endif
53
54/* Avoid including vulkan.h, don't define VkInstance if it's already included */
55#ifdef VULKAN_H_
56#define NO_SDL_VULKAN_TYPEDEFS
57#endif
58#ifndef NO_SDL_VULKAN_TYPEDEFS
59#define VK_DEFINE_HANDLE(object) typedef struct object##_T* object;
60
61#if defined(__LP64__) || defined(_WIN64) || defined(__x86_64__) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)
62#define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef struct object##_T *object;
63#else
64#define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef uint64_t object;
65#endif
66
67VK_DEFINE_HANDLE(VkInstance)
68VK_DEFINE_HANDLE(VkPhysicalDevice)
70struct VkAllocationCallbacks;
71
72#endif /* !NO_SDL_VULKAN_TYPEDEFS */
73
74/**
75 * \name Vulkan support functions
76 */
77/* @{ */
78
79/**
80 * Dynamically load the Vulkan loader library.
81 *
82 * This should be called after initializing the video driver, but before
83 * creating any Vulkan windows. If no Vulkan loader library is loaded, the
84 * default library will be loaded upon creation of the first Vulkan window.
85 *
86 * It is fairly common for Vulkan applications to link with libvulkan instead
87 * of explicitly loading it at run time. This will work with SDL provided the
88 * application links to a dynamic library and both it and SDL use the same
89 * search path.
90 *
91 * If you specify a non-NULL `path`, an application should retrieve all of the
92 * Vulkan functions it uses from the dynamic library using
93 * SDL_Vulkan_GetVkGetInstanceProcAddr unless you can guarantee `path` points
94 * to the same vulkan loader library the application linked to.
95 *
96 * On Apple devices, if `path` is NULL, SDL will attempt to find the
97 * `vkGetInstanceProcAddr` address within all the Mach-O images of the current
98 * process. This is because it is fairly common for Vulkan applications to
99 * link with libvulkan (and historically MoltenVK was provided as a static
100 * library). If it is not found, on macOS, SDL will attempt to load
101 * `vulkan.framework/vulkan`, `libvulkan.1.dylib`,
102 * `MoltenVK.framework/MoltenVK`, and `libMoltenVK.dylib`, in that order. On
103 * iOS, SDL will attempt to load `libMoltenVK.dylib`. Applications using a
104 * dynamic framework or .dylib must ensure it is included in its application
105 * bundle.
106 *
107 * On non-Apple devices, application linking with a static libvulkan is not
108 * supported. Either do not link to the Vulkan loader or link to a dynamic
109 * library version.
110 *
111 * \param path the platform dependent Vulkan loader library name or NULL.
112 * \returns true on success or false on failure; call SDL_GetError() for more
113 * information.
114 *
115 * \since This function is available since SDL 3.0.0.
116 *
117 * \sa SDL_Vulkan_GetVkGetInstanceProcAddr
118 * \sa SDL_Vulkan_UnloadLibrary
119 */
120extern SDL_DECLSPEC bool SDLCALL SDL_Vulkan_LoadLibrary(const char *path);
121
122/**
123 * Get the address of the `vkGetInstanceProcAddr` function.
124 *
125 * This should be called after either calling SDL_Vulkan_LoadLibrary() or
126 * creating an SDL_Window with the `SDL_WINDOW_VULKAN` flag.
127 *
128 * The actual type of the returned function pointer is
129 * PFN_vkGetInstanceProcAddr, but that isn't available because the Vulkan
130 * headers are not included here. You should cast the return value of this
131 * function to that type, e.g.
132 *
133 * `vkGetInstanceProcAddr =
134 * (PFN_vkGetInstanceProcAddr)SDL_Vulkan_GetVkGetInstanceProcAddr();`
135 *
136 * \returns the function pointer for `vkGetInstanceProcAddr` or NULL on
137 * failure; call SDL_GetError() for more information.
138 *
139 * \since This function is available since SDL 3.0.0.
140 */
142
143/**
144 * Unload the Vulkan library previously loaded by SDL_Vulkan_LoadLibrary().
145 *
146 * \since This function is available since SDL 3.0.0.
147 *
148 * \sa SDL_Vulkan_LoadLibrary
149 */
150extern SDL_DECLSPEC void SDLCALL SDL_Vulkan_UnloadLibrary(void);
151
152/**
153 * Get the Vulkan instance extensions needed for vkCreateInstance.
154 *
155 * This should be called after either calling SDL_Vulkan_LoadLibrary() or
156 * creating an SDL_Window with the `SDL_WINDOW_VULKAN` flag.
157 *
158 * On return, the variable pointed to by `count` will be set to the number of
159 * elements returned, suitable for using with
160 * VkInstanceCreateInfo::enabledExtensionCount, and the returned array can be
161 * used with VkInstanceCreateInfo::ppEnabledExtensionNames, for calling
162 * Vulkan's vkCreateInstance API.
163 *
164 * You should not free the returned array; it is owned by SDL.
165 *
166 * \param count a pointer filled in with the number of extensions returned.
167 * \returns an array of extension name strings on success, NULL on failure;
168 * call SDL_GetError() for more information.
169 *
170 * \since This function is available since SDL 3.0.0.
171 *
172 * \sa SDL_Vulkan_CreateSurface
173 */
174extern SDL_DECLSPEC char const * const * SDLCALL SDL_Vulkan_GetInstanceExtensions(Uint32 *count);
175
176/**
177 * Create a Vulkan rendering surface for a window.
178 *
179 * The `window` must have been created with the `SDL_WINDOW_VULKAN` flag and
180 * `instance` must have been created with extensions returned by
181 * SDL_Vulkan_GetInstanceExtensions() enabled.
182 *
183 * If `allocator` is NULL, Vulkan will use the system default allocator. This
184 * argument is passed directly to Vulkan and isn't used by SDL itself.
185 *
186 * \param window the window to which to attach the Vulkan surface.
187 * \param instance the Vulkan instance handle.
188 * \param allocator a VkAllocationCallbacks struct, which lets the app set the
189 * allocator that creates the surface. Can be NULL.
190 * \param surface a pointer to a VkSurfaceKHR handle to output the newly
191 * created surface.
192 * \returns true on success or false on failure; call SDL_GetError() for more
193 * information.
194 *
195 * \since This function is available since SDL 3.0.0.
196 *
197 * \sa SDL_Vulkan_GetInstanceExtensions
198 * \sa SDL_Vulkan_DestroySurface
199 */
200extern SDL_DECLSPEC bool SDLCALL SDL_Vulkan_CreateSurface(SDL_Window *window,
201 VkInstance instance,
202 const struct VkAllocationCallbacks *allocator,
203 VkSurfaceKHR* surface);
204
205/**
206 * Destroy the Vulkan rendering surface of a window.
207 *
208 * This should be called before SDL_DestroyWindow, if SDL_Vulkan_CreateSurface
209 * was called after SDL_CreateWindow.
210 *
211 * The `instance` must have been created with extensions returned by
212 * SDL_Vulkan_GetInstanceExtensions() enabled and `surface` must have been
213 * created successfully by an SDL_Vulkan_CreateSurface() call.
214 *
215 * If `allocator` is NULL, Vulkan will use the system default allocator. This
216 * argument is passed directly to Vulkan and isn't used by SDL itself.
217 *
218 * \param instance the Vulkan instance handle.
219 * \param surface vkSurfaceKHR handle to destroy.
220 * \param allocator a VkAllocationCallbacks struct, which lets the app set the
221 * allocator that destroys the surface. Can be NULL.
222 *
223 * \since This function is available since SDL 3.0.0.
224 *
225 * \sa SDL_Vulkan_GetInstanceExtensions
226 * \sa SDL_Vulkan_CreateSurface
227 */
228extern SDL_DECLSPEC void SDLCALL SDL_Vulkan_DestroySurface(VkInstance instance,
229 VkSurfaceKHR surface,
230 const struct VkAllocationCallbacks *allocator);
231
232/**
233 * Query support for presentation via a given physical device and queue
234 * family.
235 *
236 * The `instance` must have been created with extensions returned by
237 * SDL_Vulkan_GetInstanceExtensions() enabled.
238 *
239 * \param instance the Vulkan instance handle.
240 * \param physicalDevice a valid Vulkan physical device handle.
241 * \param queueFamilyIndex a valid queue family index for the given physical
242 * device.
243 * \returns true if supported, false if unsupported or an error occurred.
244 *
245 * \since This function is available since SDL 3.0.0.
246 *
247 * \sa SDL_Vulkan_GetInstanceExtensions
248 */
249extern SDL_DECLSPEC bool SDLCALL SDL_Vulkan_GetPresentationSupport(VkInstance instance,
250 VkPhysicalDevice physicalDevice,
251 Uint32 queueFamilyIndex);
252
253/* @} *//* Vulkan support functions */
254
255/* Ends C function definitions when using C++ */
256#ifdef __cplusplus
257}
258#endif
259#include <SDL3/SDL_close_code.h>
260
261#endif /* SDL_vulkan_h_ */
void(* SDL_FunctionPointer)(void)
uint32_t Uint32
Definition SDL_stdinc.h:356
struct SDL_Window SDL_Window
Definition SDL_video.h:165
#define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object)
Definition SDL_vulkan.h:64
SDL_FunctionPointer SDL_Vulkan_GetVkGetInstanceProcAddr(void)
#define VK_DEFINE_HANDLE(object)
Definition SDL_vulkan.h:59
bool SDL_Vulkan_LoadLibrary(const char *path)
bool SDL_Vulkan_GetPresentationSupport(VkInstance instance, VkPhysicalDevice physicalDevice, Uint32 queueFamilyIndex)
char const *const * SDL_Vulkan_GetInstanceExtensions(Uint32 *count)
bool SDL_Vulkan_CreateSurface(SDL_Window *window, VkInstance instance, const struct VkAllocationCallbacks *allocator, VkSurfaceKHR *surface)
void SDL_Vulkan_DestroySurface(VkInstance instance, VkSurfaceKHR surface, const struct VkAllocationCallbacks *allocator)
void SDL_Vulkan_UnloadLibrary(void)