Atlas - SDL_waylandvulkan.c

Home / ext / SDL / src / video / wayland Lines: 1 | Size: 7915 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_WAYLAND) 30 31#include "../SDL_vulkan_internal.h" 32 33#include "SDL_waylandvideo.h" 34#include "SDL_waylandwindow.h" 35 36#include "SDL_waylandvulkan.h" 37 38#ifdef SDL_PLATFORM_OPENBSD 39#define DEFAULT_VULKAN "libvulkan.so" 40#else 41#define DEFAULT_VULKAN "libvulkan.so.1" 42#endif 43 44SDL_ELF_NOTE_DLOPEN( 45 "wayland-vulkan", 46 "Support for Vulkan on wayland backend", 47 SDL_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED, 48 DEFAULT_VULKAN 49) 50 51bool Wayland_Vulkan_LoadLibrary(SDL_VideoDevice *_this, const char *path) 52{ 53 VkExtensionProperties *extensions = NULL; 54 Uint32 i, extensionCount = 0; 55 bool hasSurfaceExtension = false; 56 bool hasWaylandSurfaceExtension = false; 57 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL; 58 if (_this->vulkan_config.loader_handle) { 59 return SDL_SetError("Vulkan already loaded"); 60 } 61 62 // Load the Vulkan loader library 63 if (!path) { 64 path = SDL_GetHint(SDL_HINT_VULKAN_LIBRARY); 65 } 66 if (!path) { 67 path = DEFAULT_VULKAN; 68 } 69 _this->vulkan_config.loader_handle = SDL_LoadObject(path); 70 if (!_this->vulkan_config.loader_handle) { 71 return false; 72 } 73 SDL_strlcpy(_this->vulkan_config.loader_path, path, 74 SDL_arraysize(_this->vulkan_config.loader_path)); 75 vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction( 76 _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr"); 77 if (!vkGetInstanceProcAddr) { 78 goto fail; 79 } 80 _this->vulkan_config.vkGetInstanceProcAddr = (void *)vkGetInstanceProcAddr; 81 _this->vulkan_config.vkEnumerateInstanceExtensionProperties = 82 (void *)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)( 83 VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties"); 84 if (!_this->vulkan_config.vkEnumerateInstanceExtensionProperties) { 85 goto fail; 86 } 87 extensions = SDL_Vulkan_CreateInstanceExtensionsList( 88 (PFN_vkEnumerateInstanceExtensionProperties) 89 _this->vulkan_config.vkEnumerateInstanceExtensionProperties, 90 &extensionCount); 91 if (!extensions) { 92 goto fail; 93 } 94 for (i = 0; i < extensionCount; i++) { 95 if (SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) { 96 hasSurfaceExtension = true; 97 } else if (SDL_strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) { 98 hasWaylandSurfaceExtension = true; 99 } 100 } 101 SDL_free(extensions); 102 if (!hasSurfaceExtension) { 103 SDL_SetError("Installed Vulkan doesn't implement the " VK_KHR_SURFACE_EXTENSION_NAME " extension"); 104 goto fail; 105 } else if (!hasWaylandSurfaceExtension) { 106 SDL_SetError("Installed Vulkan doesn't implement the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME " extension"); 107 goto fail; 108 } 109 return true; 110 111fail: 112 SDL_UnloadObject(_this->vulkan_config.loader_handle); 113 _this->vulkan_config.loader_handle = NULL; 114 return false; 115} 116 117void Wayland_Vulkan_UnloadLibrary(SDL_VideoDevice *_this) 118{ 119 if (_this->vulkan_config.loader_handle) { 120 SDL_UnloadObject(_this->vulkan_config.loader_handle); 121 _this->vulkan_config.loader_handle = NULL; 122 } 123} 124 125char const * const *Wayland_Vulkan_GetInstanceExtensions(SDL_VideoDevice *_this, Uint32 *count) 126{ 127 static const char *const extensionsForWayland[] = { 128 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME 129 }; 130 131 if (count) { 132 *count = SDL_arraysize(extensionsForWayland); 133 } 134 135 return extensionsForWayland; 136} 137 138bool Wayland_Vulkan_CreateSurface(SDL_VideoDevice *_this, 139 SDL_Window *window, 140 VkInstance instance, 141 const struct VkAllocationCallbacks *allocator, 142 VkSurfaceKHR *surface) 143{ 144 SDL_WindowData *windowData = window->internal; 145 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = 146 (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr; 147 PFN_vkCreateWaylandSurfaceKHR vkCreateWaylandSurfaceKHR = 148 (PFN_vkCreateWaylandSurfaceKHR)vkGetInstanceProcAddr( 149 instance, 150 "vkCreateWaylandSurfaceKHR"); 151 VkWaylandSurfaceCreateInfoKHR createInfo; 152 VkResult result; 153 154 if (!_this->vulkan_config.loader_handle) { 155 return SDL_SetError("Vulkan is not loaded"); 156 } 157 158 if (!vkCreateWaylandSurfaceKHR) { 159 return SDL_SetError(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME 160 " extension is not enabled in the Vulkan instance."); 161 } 162 SDL_zero(createInfo); 163 createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR; 164 createInfo.pNext = NULL; 165 createInfo.flags = 0; 166 createInfo.display = windowData->waylandData->display; 167 createInfo.surface = windowData->surface; 168 result = vkCreateWaylandSurfaceKHR(instance, &createInfo, allocator, surface); 169 if (result != VK_SUCCESS) { 170 return SDL_SetError("vkCreateWaylandSurfaceKHR failed: %s", SDL_Vulkan_GetResultString(result)); 171 } 172 return true; 173} 174 175void Wayland_Vulkan_DestroySurface(SDL_VideoDevice *_this, 176 VkInstance instance, 177 VkSurfaceKHR surface, 178 const struct VkAllocationCallbacks *allocator) 179{ 180 if (_this->vulkan_config.loader_handle) { 181 SDL_Vulkan_DestroySurface_Internal(_this->vulkan_config.vkGetInstanceProcAddr, instance, surface, allocator); 182 } 183} 184 185bool Wayland_Vulkan_GetPresentationSupport(SDL_VideoDevice *_this, 186 VkInstance instance, 187 VkPhysicalDevice physicalDevice, 188 Uint32 queueFamilyIndex) 189{ 190 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = 191 (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr; 192 PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR vkGetPhysicalDeviceWaylandPresentationSupportKHR = 193 (PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR)vkGetInstanceProcAddr( 194 instance, 195 "vkGetPhysicalDeviceWaylandPresentationSupportKHR"); 196 197 if (!_this->vulkan_config.loader_handle) { 198 return SDL_SetError("Vulkan is not loaded"); 199 } 200 201 if (!vkGetPhysicalDeviceWaylandPresentationSupportKHR) { 202 return SDL_SetError(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME " extension is not enabled in the Vulkan instance."); 203 } 204 205 return vkGetPhysicalDeviceWaylandPresentationSupportKHR(physicalDevice, 206 queueFamilyIndex, 207 _this->internal->display); 208} 209 210#endif 211
[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.