Atlas - SDL_x11vulkan.c

Home / ext / SDL / src / video / x11 Lines: 1 | Size: 12224 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#include "SDL_internal.h" 22 23#if defined(SDL_VIDEO_VULKAN) && defined(SDL_VIDEO_DRIVER_X11) 24 25#include "../SDL_vulkan_internal.h" 26 27#include "SDL_x11video.h" 28 29#include "SDL_x11vulkan.h" 30 31#include <X11/Xlib.h> 32// #include <xcb/xcb.h> 33 34#ifdef SDL_PLATFORM_OPENBSD 35#define DEFAULT_VULKAN "libvulkan.so" 36#define DEFAULT_X11_XCB "libX11-xcb.so" 37#else 38#define DEFAULT_VULKAN "libvulkan.so.1" 39#define DEFAULT_X11_XCB "libX11-xcb.so.1" 40#endif 41 42SDL_ELF_NOTE_DLOPEN( 43 "x11-vulkan", 44 "Support for vulkan on X11", 45 SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED, 46 DEFAULT_VULKAN 47) 48 49SDL_ELF_NOTE_DLOPEN( 50 "x11-vulkan", 51 "Support for vulkan on X11", 52 SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED, 53 DEFAULT_X11_XCB 54) 55 56/* 57typedef uint32_t xcb_window_t; 58typedef uint32_t xcb_visualid_t; 59*/ 60 61bool X11_Vulkan_LoadLibrary(SDL_VideoDevice *_this, const char *path) 62{ 63 SDL_VideoData *videoData = _this->internal; 64 VkExtensionProperties *extensions = NULL; 65 Uint32 extensionCount = 0; 66 bool hasSurfaceExtension = false; 67 bool hasXlibSurfaceExtension = false; 68 bool hasXCBSurfaceExtension = false; 69 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL; 70 Uint32 i; 71 if (_this->vulkan_config.loader_handle) { 72 return SDL_SetError("Vulkan already loaded"); 73 } 74 75 // Load the Vulkan loader library 76 if (!path) { 77 path = SDL_GetHint(SDL_HINT_VULKAN_LIBRARY); 78 } 79 if (!path) { 80 path = DEFAULT_VULKAN; 81 } 82 _this->vulkan_config.loader_handle = SDL_LoadObject(path); 83 if (!_this->vulkan_config.loader_handle) { 84 return false; 85 } 86 SDL_strlcpy(_this->vulkan_config.loader_path, path, SDL_arraysize(_this->vulkan_config.loader_path)); 87 vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction( 88 _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr"); 89 if (!vkGetInstanceProcAddr) { 90 goto fail; 91 } 92 _this->vulkan_config.vkGetInstanceProcAddr = (void *)vkGetInstanceProcAddr; 93 _this->vulkan_config.vkEnumerateInstanceExtensionProperties = 94 (void *)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)( 95 VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties"); 96 if (!_this->vulkan_config.vkEnumerateInstanceExtensionProperties) { 97 goto fail; 98 } 99 extensions = SDL_Vulkan_CreateInstanceExtensionsList( 100 (PFN_vkEnumerateInstanceExtensionProperties) 101 _this->vulkan_config.vkEnumerateInstanceExtensionProperties, 102 &extensionCount); 103 if (!extensions) { 104 goto fail; 105 } 106 for (i = 0; i < extensionCount; i++) { 107 if (SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) { 108 hasSurfaceExtension = true; 109 } else if (SDL_strcmp(VK_KHR_XCB_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) { 110 hasXCBSurfaceExtension = true; 111 } else if (SDL_strcmp(VK_KHR_XLIB_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) { 112 hasXlibSurfaceExtension = true; 113 } 114 } 115 SDL_free(extensions); 116 if (!hasSurfaceExtension) { 117 SDL_SetError("Installed Vulkan doesn't implement the " VK_KHR_SURFACE_EXTENSION_NAME " extension"); 118 goto fail; 119 } 120 if (hasXlibSurfaceExtension) { 121 videoData->vulkan_xlib_xcb_library = NULL; 122 } else if (!hasXCBSurfaceExtension) { 123 SDL_SetError("Installed Vulkan doesn't implement either the " VK_KHR_XCB_SURFACE_EXTENSION_NAME " extension or the " VK_KHR_XLIB_SURFACE_EXTENSION_NAME " extension"); 124 goto fail; 125 } else { 126 const char *libX11XCBLibraryName = SDL_GetHint(SDL_HINT_X11_XCB_LIBRARY); 127 if (!libX11XCBLibraryName || !*libX11XCBLibraryName) { 128 libX11XCBLibraryName = DEFAULT_X11_XCB; 129 } 130 videoData->vulkan_xlib_xcb_library = SDL_LoadObject(libX11XCBLibraryName); 131 if (!videoData->vulkan_xlib_xcb_library) { 132 goto fail; 133 } 134 videoData->vulkan_XGetXCBConnection = 135 (PFN_XGetXCBConnection)SDL_LoadFunction(videoData->vulkan_xlib_xcb_library, "XGetXCBConnection"); 136 if (!videoData->vulkan_XGetXCBConnection) { 137 SDL_UnloadObject(videoData->vulkan_xlib_xcb_library); 138 goto fail; 139 } 140 } 141 return true; 142 143fail: 144 SDL_UnloadObject(_this->vulkan_config.loader_handle); 145 _this->vulkan_config.loader_handle = NULL; 146 return false; 147} 148 149void X11_Vulkan_UnloadLibrary(SDL_VideoDevice *_this) 150{ 151 SDL_VideoData *videoData = _this->internal; 152 if (_this->vulkan_config.loader_handle) { 153 if (videoData->vulkan_xlib_xcb_library) { 154 SDL_UnloadObject(videoData->vulkan_xlib_xcb_library); 155 } 156 SDL_UnloadObject(_this->vulkan_config.loader_handle); 157 _this->vulkan_config.loader_handle = NULL; 158 } 159} 160 161char const * const *X11_Vulkan_GetInstanceExtensions(SDL_VideoDevice *_this, Uint32 *count) 162{ 163 SDL_VideoData *videoData = _this->internal; 164 if (videoData->vulkan_xlib_xcb_library) { 165 static const char *const extensionsForXCB[] = { 166 VK_KHR_SURFACE_EXTENSION_NAME, 167 VK_KHR_XCB_SURFACE_EXTENSION_NAME, 168 }; 169 if(count) { 170 *count = SDL_arraysize(extensionsForXCB); 171 } 172 return extensionsForXCB; 173 } else { 174 static const char *const extensionsForXlib[] = { 175 VK_KHR_SURFACE_EXTENSION_NAME, 176 VK_KHR_XLIB_SURFACE_EXTENSION_NAME, 177 }; 178 if(count) { 179 *count = SDL_arraysize(extensionsForXlib); 180 } 181 return extensionsForXlib; 182 } 183} 184 185bool X11_Vulkan_CreateSurface(SDL_VideoDevice *_this, 186 SDL_Window *window, 187 VkInstance instance, 188 const struct VkAllocationCallbacks *allocator, 189 VkSurfaceKHR *surface) 190{ 191 SDL_VideoData *videoData = _this->internal; 192 SDL_WindowData *windowData = window->internal; 193 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr; 194 if (!_this->vulkan_config.loader_handle) { 195 return SDL_SetError("Vulkan is not loaded"); 196 } 197 vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr; 198 if (videoData->vulkan_xlib_xcb_library) { 199 PFN_vkCreateXcbSurfaceKHR vkCreateXcbSurfaceKHR = 200 (PFN_vkCreateXcbSurfaceKHR)vkGetInstanceProcAddr(instance, 201 "vkCreateXcbSurfaceKHR"); 202 VkXcbSurfaceCreateInfoKHR createInfo; 203 VkResult result; 204 if (!vkCreateXcbSurfaceKHR) { 205 return SDL_SetError(VK_KHR_XCB_SURFACE_EXTENSION_NAME " extension is not enabled in the Vulkan instance."); 206 } 207 SDL_zero(createInfo); 208 createInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR; 209 createInfo.connection = videoData->vulkan_XGetXCBConnection(videoData->display); 210 if (!createInfo.connection) { 211 return SDL_SetError("XGetXCBConnection failed"); 212 } 213 createInfo.window = (xcb_window_t)windowData->xwindow; 214 result = vkCreateXcbSurfaceKHR(instance, &createInfo, allocator, surface); 215 if (result != VK_SUCCESS) { 216 return SDL_SetError("vkCreateXcbSurfaceKHR failed: %s", SDL_Vulkan_GetResultString(result)); 217 } 218 } else { 219 PFN_vkCreateXlibSurfaceKHR vkCreateXlibSurfaceKHR = 220 (PFN_vkCreateXlibSurfaceKHR)vkGetInstanceProcAddr(instance, 221 "vkCreateXlibSurfaceKHR"); 222 VkXlibSurfaceCreateInfoKHR createInfo; 223 VkResult result; 224 if (!vkCreateXlibSurfaceKHR) { 225 return SDL_SetError(VK_KHR_XLIB_SURFACE_EXTENSION_NAME " extension is not enabled in the Vulkan instance."); 226 } 227 SDL_zero(createInfo); 228 createInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR; 229 createInfo.dpy = videoData->display; 230 createInfo.window = (xcb_window_t)windowData->xwindow; 231 result = vkCreateXlibSurfaceKHR(instance, &createInfo, allocator, surface); 232 if (result != VK_SUCCESS) { 233 return SDL_SetError("vkCreateXlibSurfaceKHR failed: %s", SDL_Vulkan_GetResultString(result)); 234 } 235 } 236 237 return true; // success! 238} 239 240void X11_Vulkan_DestroySurface(SDL_VideoDevice *_this, 241 VkInstance instance, 242 VkSurfaceKHR surface, 243 const struct VkAllocationCallbacks *allocator) 244{ 245 if (_this->vulkan_config.loader_handle) { 246 SDL_Vulkan_DestroySurface_Internal(_this->vulkan_config.vkGetInstanceProcAddr, instance, surface, allocator); 247 } 248} 249 250bool X11_Vulkan_GetPresentationSupport(SDL_VideoDevice *_this, 251 VkInstance instance, 252 VkPhysicalDevice physicalDevice, 253 Uint32 queueFamilyIndex) 254{ 255 SDL_VideoData *videoData = _this->internal; 256 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr; 257 const char *forced_visual_id; 258 VisualID visualid; 259 260 if (!_this->vulkan_config.loader_handle) { 261 return SDL_SetError("Vulkan is not loaded"); 262 } 263 vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr; 264 265 forced_visual_id = SDL_GetHint(SDL_HINT_VIDEO_X11_WINDOW_VISUALID); 266 if (forced_visual_id) { 267 visualid = SDL_strtol(forced_visual_id, NULL, 0); 268 } else { 269 visualid = X11_XVisualIDFromVisual(DefaultVisual(videoData->display, DefaultScreen(videoData->display))); 270 } 271 272 if (videoData->vulkan_xlib_xcb_library) { 273 PFN_vkGetPhysicalDeviceXcbPresentationSupportKHR vkGetPhysicalDeviceXcbPresentationSupportKHR = 274 (PFN_vkGetPhysicalDeviceXcbPresentationSupportKHR)vkGetInstanceProcAddr( 275 instance, 276 "vkGetPhysicalDeviceXcbPresentationSupportKHR"); 277 278 if (!vkGetPhysicalDeviceXcbPresentationSupportKHR) { 279 return SDL_SetError(VK_KHR_XCB_SURFACE_EXTENSION_NAME " extension is not enabled in the Vulkan instance."); 280 } 281 282 return vkGetPhysicalDeviceXcbPresentationSupportKHR(physicalDevice, 283 queueFamilyIndex, 284 videoData->vulkan_XGetXCBConnection(videoData->display), 285 visualid); 286 } else { 287 PFN_vkGetPhysicalDeviceXlibPresentationSupportKHR vkGetPhysicalDeviceXlibPresentationSupportKHR = 288 (PFN_vkGetPhysicalDeviceXlibPresentationSupportKHR)vkGetInstanceProcAddr( 289 instance, 290 "vkGetPhysicalDeviceXlibPresentationSupportKHR"); 291 292 if (!vkGetPhysicalDeviceXlibPresentationSupportKHR) { 293 return SDL_SetError(VK_KHR_XLIB_SURFACE_EXTENSION_NAME " extension is not enabled in the Vulkan instance."); 294 } 295 296 return vkGetPhysicalDeviceXlibPresentationSupportKHR(physicalDevice, 297 queueFamilyIndex, 298 videoData->display, 299 visualid); 300 } 301} 302 303#endif 304
[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.