Atlas - SDL_windowsvulkan.c

Home / ext / SDL / src / video / windows Lines: 1 | Size: 7381 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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 * @author Mark Callow, www.edgewise-consulting.com. Based on Jacob Lifshay's 24 * SDL_x11vulkan.c. 25 */ 26 27#include "SDL_internal.h" 28 29#if defined(SDL_VIDEO_VULKAN) && defined(SDL_VIDEO_DRIVER_WINDOWS) 30 31#include "../SDL_vulkan_internal.h" 32 33#include "SDL_windowsvideo.h" 34#include "SDL_windowswindow.h" 35 36#include "SDL_windowsvulkan.h" 37 38bool WIN_Vulkan_LoadLibrary(SDL_VideoDevice *_this, const char *path) 39{ 40 VkExtensionProperties *extensions = NULL; 41 Uint32 extensionCount = 0; 42 Uint32 i; 43 bool hasSurfaceExtension = false; 44 bool hasWin32SurfaceExtension = false; 45 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL; 46 if (_this->vulkan_config.loader_handle) { 47 return SDL_SetError("Vulkan already loaded"); 48 } 49 50 // Load the Vulkan loader library 51 if (!path) { 52 path = SDL_GetHint(SDL_HINT_VULKAN_LIBRARY); 53 } 54 if (!path) { 55 path = "vulkan-1.dll"; 56 } 57 _this->vulkan_config.loader_handle = SDL_LoadObject(path); 58 if (!_this->vulkan_config.loader_handle) { 59 return false; 60 } 61 SDL_strlcpy(_this->vulkan_config.loader_path, path, 62 SDL_arraysize(_this->vulkan_config.loader_path)); 63 vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction( 64 _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr"); 65 if (!vkGetInstanceProcAddr) { 66 goto fail; 67 } 68 _this->vulkan_config.vkGetInstanceProcAddr = (SDL_FunctionPointer)vkGetInstanceProcAddr; 69 _this->vulkan_config.vkEnumerateInstanceExtensionProperties = 70 (SDL_FunctionPointer)vkGetInstanceProcAddr( 71 VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties"); 72 if (!_this->vulkan_config.vkEnumerateInstanceExtensionProperties) { 73 goto fail; 74 } 75 extensions = SDL_Vulkan_CreateInstanceExtensionsList( 76 (PFN_vkEnumerateInstanceExtensionProperties) 77 _this->vulkan_config.vkEnumerateInstanceExtensionProperties, 78 &extensionCount); 79 if (!extensions) { 80 goto fail; 81 } 82 for (i = 0; i < extensionCount; i++) { 83 if (SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) { 84 hasSurfaceExtension = true; 85 } else if (SDL_strcmp(VK_KHR_WIN32_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) { 86 hasWin32SurfaceExtension = true; 87 } 88 } 89 SDL_free(extensions); 90 if (!hasSurfaceExtension) { 91 SDL_SetError("Installed Vulkan doesn't implement the " VK_KHR_SURFACE_EXTENSION_NAME " extension"); 92 goto fail; 93 } else if (!hasWin32SurfaceExtension) { 94 SDL_SetError("Installed Vulkan doesn't implement the " VK_KHR_WIN32_SURFACE_EXTENSION_NAME " extension"); 95 goto fail; 96 } 97 return true; 98 99fail: 100 SDL_UnloadObject(_this->vulkan_config.loader_handle); 101 _this->vulkan_config.loader_handle = NULL; 102 return false; 103} 104 105void WIN_Vulkan_UnloadLibrary(SDL_VideoDevice *_this) 106{ 107 if (_this->vulkan_config.loader_handle) { 108 SDL_UnloadObject(_this->vulkan_config.loader_handle); 109 _this->vulkan_config.loader_handle = NULL; 110 } 111} 112 113char const * const *WIN_Vulkan_GetInstanceExtensions(SDL_VideoDevice *_this, Uint32 *count) 114{ 115 static const char *const extensionsForWin32[] = { 116 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_WIN32_SURFACE_EXTENSION_NAME 117 }; 118 if (count) { 119 *count = SDL_arraysize(extensionsForWin32); 120 } 121 return extensionsForWin32; 122} 123 124bool WIN_Vulkan_CreateSurface(SDL_VideoDevice *_this, 125 SDL_Window *window, 126 VkInstance instance, 127 const struct VkAllocationCallbacks *allocator, 128 VkSurfaceKHR *surface) 129{ 130 SDL_WindowData *windowData = window->internal; 131 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = 132 (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr; 133 PFN_vkCreateWin32SurfaceKHR vkCreateWin32SurfaceKHR = 134 (PFN_vkCreateWin32SurfaceKHR)vkGetInstanceProcAddr( 135 instance, 136 "vkCreateWin32SurfaceKHR"); 137 VkWin32SurfaceCreateInfoKHR createInfo; 138 VkResult result; 139 140 if (!_this->vulkan_config.loader_handle) { 141 return SDL_SetError("Vulkan is not loaded"); 142 } 143 144 if (!vkCreateWin32SurfaceKHR) { 145 return SDL_SetError(VK_KHR_WIN32_SURFACE_EXTENSION_NAME 146 " extension is not enabled in the Vulkan instance."); 147 } 148 createInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR; 149 createInfo.pNext = NULL; 150 createInfo.flags = 0; 151 createInfo.hinstance = windowData->hinstance; 152 createInfo.hwnd = windowData->hwnd; 153 result = vkCreateWin32SurfaceKHR(instance, &createInfo, allocator, surface); 154 if (result != VK_SUCCESS) { 155 return SDL_SetError("vkCreateWin32SurfaceKHR failed: %s", SDL_Vulkan_GetResultString(result)); 156 } 157 return true; 158} 159 160void WIN_Vulkan_DestroySurface(SDL_VideoDevice *_this, 161 VkInstance instance, 162 VkSurfaceKHR surface, 163 const struct VkAllocationCallbacks *allocator) 164{ 165 if (_this->vulkan_config.loader_handle) { 166 SDL_Vulkan_DestroySurface_Internal(_this->vulkan_config.vkGetInstanceProcAddr, instance, surface, allocator); 167 } 168} 169 170bool WIN_Vulkan_GetPresentationSupport(SDL_VideoDevice *_this, 171 VkInstance instance, 172 VkPhysicalDevice physicalDevice, 173 Uint32 queueFamilyIndex) 174{ 175 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = 176 (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr; 177 PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR vkGetPhysicalDeviceWin32PresentationSupportKHR = 178 (PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR)vkGetInstanceProcAddr( 179 instance, 180 "vkGetPhysicalDeviceWin32PresentationSupportKHR"); 181 182 if (!_this->vulkan_config.loader_handle) { 183 return SDL_SetError("Vulkan is not loaded"); 184 } 185 186 if (!vkGetPhysicalDeviceWin32PresentationSupportKHR) { 187 return SDL_SetError(VK_KHR_WIN32_SURFACE_EXTENSION_NAME " extension is not enabled in the Vulkan instance."); 188 } 189 190 return vkGetPhysicalDeviceWin32PresentationSupportKHR(physicalDevice, 191 queueFamilyIndex); 192} 193 194#endif 195
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.