|
| 1 | +{ |
| 2 | + This file is part of: |
| 3 | +
|
| 4 | + SDL3 for Pascal |
| 5 | + (https://github.com/PascalGameDevelopment/SDL3-for-Pascal) |
| 6 | + SPDX-License-Identifier: Zlib |
| 7 | +} |
| 8 | + |
| 9 | +{* |
| 10 | + * # CategoryVulkan |
| 11 | + * |
| 12 | + * Functions for creating Vulkan surfaces on SDL windows. |
| 13 | + * |
| 14 | + * For the most part, Vulkan operates independent of SDL, but it benefits from |
| 15 | + * a little support during setup. |
| 16 | + * |
| 17 | + * Use SDL_Vulkan_GetInstanceExtensions() to get platform-specific bits for |
| 18 | + * creating a VkInstance, then SDL_Vulkan_GetVkGetInstanceProcAddr() to get |
| 19 | + * the appropriate function for querying Vulkan entry points. Then |
| 20 | + * SDL_Vulkan_CreateSurface() will get you the final pieces you need to |
| 21 | + * prepare for rendering into an SDL_Window with Vulkan. |
| 22 | + * |
| 23 | + * Unlike OpenGL, most of the details of "context" creation and window buffer |
| 24 | + * swapping are handled by the Vulkan API directly, so SDL doesn't provide |
| 25 | + * Vulkan equivalents of SDL_GL_SwapWindow(), etc; they aren't necessary. |
| 26 | + } |
| 27 | + |
| 28 | +{ #note : SDL3-for-Pascal: The following comment includes the original C code. } |
| 29 | +{ Avoid including vulkan.h, don't define VkInstance if it's already included } |
| 30 | +{ |
| 31 | + #ifdef VULKAN_H_ |
| 32 | + #define NO_SDL_VULKAN_TYPEDEFS |
| 33 | + #endif |
| 34 | + #ifndef NO_SDL_VULKAN_TYPEDEFS |
| 35 | + #define VK_DEFINE_HANDLE(object) typedef struct object##_T* object; |
| 36 | +
|
| 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 | +
|
| 43 | + VK_DEFINE_HANDLE(VkInstance) |
| 44 | + VK_DEFINE_HANDLE(VkPhysicalDevice) |
| 45 | + VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkSurfaceKHR) |
| 46 | + struct VkAllocationCallbacks; |
| 47 | +} |
| 48 | +{ #todo : Is there a common compiler flag in the Vulkan bindings for Pascal? Couldn't find any common one. Examine. } |
| 49 | +{$IFDEF VULKAN} |
| 50 | + {$DEFINE NO_SDL_VULKAN_TYPEDEFS} |
| 51 | +{$ENDIF} |
| 52 | +{$IFNDEF NO_SDL_VULKAN_TYPEDEFS} |
| 53 | +type |
| 54 | + PVkInstance = ^TVkInstance; |
| 55 | + TVkInstance = ^TVkInstance_T; |
| 56 | + TVkInstance_T = record |
| 57 | + end; |
| 58 | + PVkPhysicalDevice = ^TVkPhysicalDevice; |
| 59 | + TVkPhysicalDevice = ^TVkPhysicalDevice_T; |
| 60 | + TVkPhysicalDevice_T = record |
| 61 | + end; |
| 62 | + PVkSurfaceKHR = ^TVkSurfaceKHR; |
| 63 | + TVkSurfaceKHR = ^TVkSurfaceKHR_T; |
| 64 | + TVkSurfaceKHR_T = record |
| 65 | + end; |
| 66 | +{$ENDIF} |
| 67 | +type |
| 68 | + PPVkAllocationCallbacks = ^PVkAllocationCallbacks; |
| 69 | + PVkAllocationCallbacks = ^TVkAllocationCallbacks; |
| 70 | + TVkAllocationCallbacks = record |
| 71 | + {undefined structure} |
| 72 | + end; |
| 73 | + |
| 74 | +{* |
| 75 | + * \name Vulkan support functions |
| 76 | + } |
| 77 | + |
| 78 | +{* |
| 79 | + * Dynamically load the Vulkan loader library. |
| 80 | + * |
| 81 | + * This should be called after initializing the video driver, but before |
| 82 | + * creating any Vulkan windows. If no Vulkan loader library is loaded, the |
| 83 | + * default library will be loaded upon creation of the first Vulkan window. |
| 84 | + * |
| 85 | + * SDL keeps a counter of how many times this function has been successfully |
| 86 | + * called, so it is safe to call this function multiple times, so long as it |
| 87 | + * is eventually paired with an equivalent number of calls to |
| 88 | + * SDL_Vulkan_UnloadLibrary. The `path` argument is ignored unless there is no |
| 89 | + * library currently loaded, and and the library isn't actually unloaded until |
| 90 | + * there have been an equivalent number of calls to SDL_Vulkan_UnloadLibrary. |
| 91 | + * |
| 92 | + * It is fairly common for Vulkan applications to link with libvulkan instead |
| 93 | + * of explicitly loading it at run time. This will work with SDL provided the |
| 94 | + * application links to a dynamic library and both it and SDL use the same |
| 95 | + * search path. |
| 96 | + * |
| 97 | + * If you specify a non-nil `path`, an application should retrieve all of the |
| 98 | + * Vulkan functions it uses from the dynamic library using |
| 99 | + * SDL_Vulkan_GetVkGetInstanceProcAddr unless you can guarantee `path` points |
| 100 | + * to the same vulkan loader library the application linked to. |
| 101 | + * |
| 102 | + * On Apple devices, if `path` is nil, SDL will attempt to find the |
| 103 | + * `vkGetInstanceProcAddr` address within all the Mach-O images of the current |
| 104 | + * process. This is because it is fairly common for Vulkan applications to |
| 105 | + * link with libvulkan (and historically MoltenVK was provided as a static |
| 106 | + * library). If it is not found, on macOS, SDL will attempt to load |
| 107 | + * `vulkan.framework/vulkan`, `libvulkan.1.dylib`, |
| 108 | + * `MoltenVK.framework/MoltenVK`, and `libMoltenVK.dylib`, in that order. On |
| 109 | + * iOS, SDL will attempt to load `libMoltenVK.dylib`. Applications using a |
| 110 | + * dynamic framework or .dylib must ensure it is included in its application |
| 111 | + * bundle. |
| 112 | + * |
| 113 | + * On non-Apple devices, application linking with a static libvulkan is not |
| 114 | + * supported. Either do not link to the Vulkan loader or link to a dynamic |
| 115 | + * library version. |
| 116 | + * |
| 117 | + * \param path the platform dependent Vulkan loader library name or nil. |
| 118 | + * \returns true on success or false on failure; call SDL_GetError() for more |
| 119 | + * information. |
| 120 | + * |
| 121 | + * \threadsafety This function is not thread safe. |
| 122 | + * |
| 123 | + * \since This function is available since SDL 3.2.0. |
| 124 | + * |
| 125 | + * \sa SDL_Vulkan_GetVkGetInstanceProcAddr |
| 126 | + * \sa SDL_Vulkan_UnloadLibrary |
| 127 | + } |
| 128 | +function SDL_Vulkan_LoadLibrary(path: PAnsiChar): cbool; cdecl; |
| 129 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_Vulkan_LoadLibrary' {$ENDIF} {$ENDIF}; |
| 130 | + |
| 131 | +{* |
| 132 | + * Get the address of the `vkGetInstanceProcAddr` function. |
| 133 | + * |
| 134 | + * This should be called after either calling SDL_Vulkan_LoadLibrary() or |
| 135 | + * creating an SDL_Window with the `SDL_WINDOW_VULKAN` flag. |
| 136 | + * |
| 137 | + * The actual type of the returned function Pointer is |
| 138 | + * PFN_vkGetInstanceProcAddr, but that isn't available because the Vulkan |
| 139 | + * headers are not included here. You should cast the return value of this |
| 140 | + * function to that type, e.g. |
| 141 | + * |
| 142 | + * `vkGetInstanceProcAddr = |
| 143 | + * (PFN_vkGetInstanceProcAddr)SDL_Vulkan_GetVkGetInstanceProcAddr();` |
| 144 | + * |
| 145 | + * \returns the function Pointer for `vkGetInstanceProcAddr` or nil on |
| 146 | + * failure; call SDL_GetError() for more information. |
| 147 | + * |
| 148 | + * \since This function is available since SDL 3.2.0. |
| 149 | + } |
| 150 | +function SDL_Vulkan_GetVkGetInstanceProcAddr: TSDL_FunctionPointer; cdecl; |
| 151 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_Vulkan_GetVkGetInstanceProcAddr' {$ENDIF} {$ENDIF}; |
| 152 | + |
| 153 | +{* |
| 154 | + * Unload the Vulkan library previously loaded by SDL_Vulkan_LoadLibrary(). |
| 155 | + * |
| 156 | + * SDL keeps a counter of how many times this function has been called, so it |
| 157 | + * is safe to call this function multiple times, so long as it is paired with |
| 158 | + * an equivalent number of calls to SDL_Vulkan_LoadLibrary. The library isn't |
| 159 | + * actually unloaded until there have been an equivalent number of calls to |
| 160 | + * SDL_Vulkan_UnloadLibrary. |
| 161 | + * |
| 162 | + * Once the library has actually been unloaded, if any Vulkan instances |
| 163 | + * remain, they will likely crash the program. Clean up any existing Vulkan |
| 164 | + * resources, and destroy appropriate windows, renderers and GPU devices |
| 165 | + * before calling this function. |
| 166 | + * |
| 167 | + * \threadsafety This function is not thread safe. |
| 168 | + * |
| 169 | + * \since This function is available since SDL 3.2.0. |
| 170 | + * |
| 171 | + * \sa SDL_Vulkan_LoadLibrary |
| 172 | + } |
| 173 | +procedure SDL_Vulkan_UnloadLibrary; cdecl; |
| 174 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_Vulkan_UnloadLibrary' {$ENDIF} {$ENDIF}; |
| 175 | + |
| 176 | +{* |
| 177 | + * Get the Vulkan instance extensions needed for vkCreateInstance. |
| 178 | + * |
| 179 | + * This should be called after either calling SDL_Vulkan_LoadLibrary() or |
| 180 | + * creating an SDL_Window with the `SDL_WINDOW_VULKAN` flag. |
| 181 | + * |
| 182 | + * On return, the variable pointed to by `count` will be set to the number of |
| 183 | + * elements returned, suitable for using with |
| 184 | + * VkInstanceCreateInfo:: enabledExtensionCount, and the returned array can be |
| 185 | + * used with VkInstanceCreateInfo:: ppEnabledExtensionNames, for calling |
| 186 | + * Vulkan's vkCreateInstance API. |
| 187 | + * |
| 188 | + * You should not free the returned array; it is owned by SDL. |
| 189 | + * |
| 190 | + * \param count a Pointer filled in with the number of extensions returned. |
| 191 | + * \returns an array of extension name strings on success, nil on failure; |
| 192 | + * call SDL_GetError() for more information. |
| 193 | + * |
| 194 | + * \since This function is available since SDL 3.2.0. |
| 195 | + * |
| 196 | + * \sa SDL_Vulkan_CreateSurface |
| 197 | + } |
| 198 | +function SDL_Vulkan_GetInstanceExtensions(count: pcuint32):PPAnsiChar; cdecl; |
| 199 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_Vulkan_GetInstanceExtensions' {$ENDIF} {$ENDIF}; |
| 200 | + |
| 201 | +{* |
| 202 | + * Create a Vulkan rendering surface for a window. |
| 203 | + * |
| 204 | + * The `window` must have been created with the `SDL_WINDOW_VULKAN` flag and |
| 205 | + * `instance` must have been created with extensions returned by |
| 206 | + * SDL_Vulkan_GetInstanceExtensions() enabled. |
| 207 | + * |
| 208 | + * If `allocator` is nil, Vulkan will use the system default allocator. This |
| 209 | + * argument is passed directly to Vulkan and isn't used by SDL itself. |
| 210 | + * |
| 211 | + * \param window the window to which to attach the Vulkan surface. |
| 212 | + * \param instance the Vulkan instance handle. |
| 213 | + * \param allocator a VkAllocationCallbacks struct, which lets the app set the |
| 214 | + * allocator that creates the surface. Can be nil. |
| 215 | + * \param surface a Pointer to a VkSurfaceKHR handle to output the newly |
| 216 | + * created surface. |
| 217 | + * \returns true on success or false on failure; call SDL_GetError() for more |
| 218 | + * information. |
| 219 | + * |
| 220 | + * \since This function is available since SDL 3.2.0. |
| 221 | + * |
| 222 | + * \sa SDL_Vulkan_GetInstanceExtensions |
| 223 | + * \sa SDL_Vulkan_DestroySurface |
| 224 | + } |
| 225 | +function SDL_Vulkan_CreateSurface(window: PSDL_Window; instance: TVkInstance; allocator: PVkAllocationCallbacks; surface: PVkSurfaceKHR): cbool; cdecl; |
| 226 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_Vulkan_CreateSurface' {$ENDIF} {$ENDIF}; |
| 227 | + |
| 228 | +{* |
| 229 | + * Destroy the Vulkan rendering surface of a window. |
| 230 | + * |
| 231 | + * This should be called before SDL_DestroyWindow, if SDL_Vulkan_CreateSurface |
| 232 | + * was called after SDL_CreateWindow. |
| 233 | + * |
| 234 | + * The `instance` must have been created with extensions returned by |
| 235 | + * SDL_Vulkan_GetInstanceExtensions() enabled and `surface` must have been |
| 236 | + * created successfully by an SDL_Vulkan_CreateSurface() call. |
| 237 | + * |
| 238 | + * If `allocator` is nil, Vulkan will use the system default allocator. This |
| 239 | + * argument is passed directly to Vulkan and isn't used by SDL itself. |
| 240 | + * |
| 241 | + * \param instance the Vulkan instance handle. |
| 242 | + * \param surface vkSurfaceKHR handle to destroy. |
| 243 | + * \param allocator a VkAllocationCallbacks struct, which lets the app set the |
| 244 | + * allocator that destroys the surface. Can be nil. |
| 245 | + * |
| 246 | + * \since This function is available since SDL 3.2.0. |
| 247 | + * |
| 248 | + * \sa SDL_Vulkan_GetInstanceExtensions |
| 249 | + * \sa SDL_Vulkan_CreateSurface |
| 250 | + } |
| 251 | +procedure SDL_Vulkan_DestroySurface(instance: TVkInstance; surface: TVkSurfaceKHR; allocator: PVkAllocationCallbacks); cdecl; |
| 252 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_Vulkan_DestroySurface' {$ENDIF} {$ENDIF}; |
| 253 | + |
| 254 | +{* |
| 255 | + * Query support for presentation via a given physical device and queue |
| 256 | + * family. |
| 257 | + * |
| 258 | + * The `instance` must have been created with extensions returned by |
| 259 | + * SDL_Vulkan_GetInstanceExtensions() enabled. |
| 260 | + * |
| 261 | + * \param instance the Vulkan instance handle. |
| 262 | + * \param physicalDevice a valid Vulkan physical device handle. |
| 263 | + * \param queueFamilyIndex a valid queue family index for the given physical |
| 264 | + * device. |
| 265 | + * \returns true if supported, false if unsupported or an error occurred. |
| 266 | + * |
| 267 | + * \since This function is available since SDL 3.2.0. |
| 268 | + * |
| 269 | + * \sa SDL_Vulkan_GetInstanceExtensions |
| 270 | + } |
| 271 | +function SDL_Vulkan_GetPresentationSupport(instance: TVkInstance; physicalDevice: TVkPhysicalDevice; queueFamilyIndex: cuint32): cbool; cdecl; |
| 272 | + external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_Vulkan_GetPresentationSupport' {$ENDIF} {$ENDIF}; |
0 commit comments