SDL_waylandvulkan.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /*
  19. * @author Mark Callow, www.edgewise-consulting.com. Based on Jacob Lifshay's
  20. * SDL_x11vulkan.c.
  21. */
  22. #include "SDL_internal.h"
  23. #if SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_WAYLAND
  24. #include "SDL_waylandvideo.h"
  25. #include "SDL_waylandwindow.h"
  26. #include "SDL_waylandvulkan.h"
  27. #include <SDL3/SDL_syswm.h>
  28. #if defined(__OpenBSD__)
  29. #define DEFAULT_VULKAN "libvulkan.so"
  30. #else
  31. #define DEFAULT_VULKAN "libvulkan.so.1"
  32. #endif
  33. int Wayland_Vulkan_LoadLibrary(_THIS, const char *path)
  34. {
  35. VkExtensionProperties *extensions = NULL;
  36. Uint32 i, extensionCount = 0;
  37. SDL_bool hasSurfaceExtension = SDL_FALSE;
  38. SDL_bool hasWaylandSurfaceExtension = SDL_FALSE;
  39. PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL;
  40. if (_this->vulkan_config.loader_handle) {
  41. return SDL_SetError("Vulkan already loaded");
  42. }
  43. /* Load the Vulkan loader library */
  44. if (path == NULL) {
  45. path = SDL_getenv("SDL_VULKAN_LIBRARY");
  46. }
  47. if (path == NULL) {
  48. path = DEFAULT_VULKAN;
  49. }
  50. _this->vulkan_config.loader_handle = SDL_LoadObject(path);
  51. if (!_this->vulkan_config.loader_handle) {
  52. return -1;
  53. }
  54. SDL_strlcpy(_this->vulkan_config.loader_path, path,
  55. SDL_arraysize(_this->vulkan_config.loader_path));
  56. vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction(
  57. _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr");
  58. if (!vkGetInstanceProcAddr) {
  59. goto fail;
  60. }
  61. _this->vulkan_config.vkGetInstanceProcAddr = (void *)vkGetInstanceProcAddr;
  62. _this->vulkan_config.vkEnumerateInstanceExtensionProperties =
  63. (void *)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)(
  64. VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
  65. if (!_this->vulkan_config.vkEnumerateInstanceExtensionProperties) {
  66. goto fail;
  67. }
  68. extensions = SDL_Vulkan_CreateInstanceExtensionsList(
  69. (PFN_vkEnumerateInstanceExtensionProperties)
  70. _this->vulkan_config.vkEnumerateInstanceExtensionProperties,
  71. &extensionCount);
  72. if (extensions == NULL) {
  73. goto fail;
  74. }
  75. for (i = 0; i < extensionCount; i++) {
  76. if (SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) {
  77. hasSurfaceExtension = SDL_TRUE;
  78. } else if (SDL_strcmp(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) {
  79. hasWaylandSurfaceExtension = SDL_TRUE;
  80. }
  81. }
  82. SDL_free(extensions);
  83. if (!hasSurfaceExtension) {
  84. SDL_SetError("Installed Vulkan doesn't implement the " VK_KHR_SURFACE_EXTENSION_NAME " extension");
  85. goto fail;
  86. } else if (!hasWaylandSurfaceExtension) {
  87. SDL_SetError("Installed Vulkan doesn't implement the " VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME "extension");
  88. goto fail;
  89. }
  90. return 0;
  91. fail:
  92. SDL_UnloadObject(_this->vulkan_config.loader_handle);
  93. _this->vulkan_config.loader_handle = NULL;
  94. return -1;
  95. }
  96. void Wayland_Vulkan_UnloadLibrary(_THIS)
  97. {
  98. if (_this->vulkan_config.loader_handle) {
  99. SDL_UnloadObject(_this->vulkan_config.loader_handle);
  100. _this->vulkan_config.loader_handle = NULL;
  101. }
  102. }
  103. SDL_bool Wayland_Vulkan_GetInstanceExtensions(_THIS,
  104. unsigned *count,
  105. const char **names)
  106. {
  107. static const char *const extensionsForWayland[] = {
  108. VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
  109. };
  110. if (!_this->vulkan_config.loader_handle) {
  111. SDL_SetError("Vulkan is not loaded");
  112. return SDL_FALSE;
  113. }
  114. return SDL_Vulkan_GetInstanceExtensions_Helper(
  115. count, names, SDL_arraysize(extensionsForWayland),
  116. extensionsForWayland);
  117. }
  118. SDL_bool Wayland_Vulkan_CreateSurface(_THIS,
  119. SDL_Window *window,
  120. VkInstance instance,
  121. VkSurfaceKHR *surface)
  122. {
  123. SDL_WindowData *windowData = window->driverdata;
  124. PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr =
  125. (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr;
  126. PFN_vkCreateWaylandSurfaceKHR vkCreateWaylandSurfaceKHR =
  127. (PFN_vkCreateWaylandSurfaceKHR)vkGetInstanceProcAddr(
  128. instance,
  129. "vkCreateWaylandSurfaceKHR");
  130. VkWaylandSurfaceCreateInfoKHR createInfo;
  131. VkResult result;
  132. if (!_this->vulkan_config.loader_handle) {
  133. SDL_SetError("Vulkan is not loaded");
  134. return SDL_FALSE;
  135. }
  136. if (!vkCreateWaylandSurfaceKHR) {
  137. SDL_SetError(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME
  138. " extension is not enabled in the Vulkan instance.");
  139. return SDL_FALSE;
  140. }
  141. SDL_zero(createInfo);
  142. createInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
  143. createInfo.pNext = NULL;
  144. createInfo.flags = 0;
  145. createInfo.display = windowData->waylandData->display;
  146. createInfo.surface = windowData->surface;
  147. result = vkCreateWaylandSurfaceKHR(instance, &createInfo,
  148. NULL, surface);
  149. if (result != VK_SUCCESS) {
  150. SDL_SetError("vkCreateWaylandSurfaceKHR failed: %s",
  151. SDL_Vulkan_GetResultString(result));
  152. return SDL_FALSE;
  153. }
  154. return SDL_TRUE;
  155. }
  156. #endif