1
0

SDL_androidvulkan.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_ANDROID
  24. #include "SDL_androidvideo.h"
  25. #include "SDL_androidwindow.h"
  26. #include "SDL_androidvulkan.h"
  27. #include <SDL3/SDL_syswm.h>
  28. int Android_Vulkan_LoadLibrary(_THIS, const char *path)
  29. {
  30. VkExtensionProperties *extensions = NULL;
  31. Uint32 i, extensionCount = 0;
  32. SDL_bool hasSurfaceExtension = SDL_FALSE;
  33. SDL_bool hasAndroidSurfaceExtension = SDL_FALSE;
  34. PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL;
  35. if (_this->vulkan_config.loader_handle) {
  36. return SDL_SetError("Vulkan already loaded");
  37. }
  38. /* Load the Vulkan loader library */
  39. if (path == NULL) {
  40. path = SDL_getenv("SDL_VULKAN_LIBRARY");
  41. }
  42. if (path == NULL) {
  43. path = "libvulkan.so";
  44. }
  45. _this->vulkan_config.loader_handle = SDL_LoadObject(path);
  46. if (!_this->vulkan_config.loader_handle) {
  47. return -1;
  48. }
  49. SDL_strlcpy(_this->vulkan_config.loader_path, path,
  50. SDL_arraysize(_this->vulkan_config.loader_path));
  51. vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction(
  52. _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr");
  53. if (!vkGetInstanceProcAddr) {
  54. goto fail;
  55. }
  56. _this->vulkan_config.vkGetInstanceProcAddr = (void *)vkGetInstanceProcAddr;
  57. _this->vulkan_config.vkEnumerateInstanceExtensionProperties =
  58. (void *)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)(
  59. VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties");
  60. if (!_this->vulkan_config.vkEnumerateInstanceExtensionProperties) {
  61. goto fail;
  62. }
  63. extensions = SDL_Vulkan_CreateInstanceExtensionsList(
  64. (PFN_vkEnumerateInstanceExtensionProperties)
  65. _this->vulkan_config.vkEnumerateInstanceExtensionProperties,
  66. &extensionCount);
  67. if (extensions == NULL) {
  68. goto fail;
  69. }
  70. for (i = 0; i < extensionCount; i++) {
  71. if (SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) {
  72. hasSurfaceExtension = SDL_TRUE;
  73. } else if (SDL_strcmp(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) {
  74. hasAndroidSurfaceExtension = SDL_TRUE;
  75. }
  76. }
  77. SDL_free(extensions);
  78. if (!hasSurfaceExtension) {
  79. SDL_SetError("Installed Vulkan doesn't implement the " VK_KHR_SURFACE_EXTENSION_NAME " extension");
  80. goto fail;
  81. } else if (!hasAndroidSurfaceExtension) {
  82. SDL_SetError("Installed Vulkan doesn't implement the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME "extension");
  83. goto fail;
  84. }
  85. return 0;
  86. fail:
  87. SDL_UnloadObject(_this->vulkan_config.loader_handle);
  88. _this->vulkan_config.loader_handle = NULL;
  89. return -1;
  90. }
  91. void Android_Vulkan_UnloadLibrary(_THIS)
  92. {
  93. if (_this->vulkan_config.loader_handle) {
  94. SDL_UnloadObject(_this->vulkan_config.loader_handle);
  95. _this->vulkan_config.loader_handle = NULL;
  96. }
  97. }
  98. SDL_bool Android_Vulkan_GetInstanceExtensions(_THIS,
  99. unsigned *count,
  100. const char **names)
  101. {
  102. static const char *const extensionsForAndroid[] = {
  103. VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
  104. };
  105. if (!_this->vulkan_config.loader_handle) {
  106. SDL_SetError("Vulkan is not loaded");
  107. return SDL_FALSE;
  108. }
  109. return SDL_Vulkan_GetInstanceExtensions_Helper(
  110. count, names, SDL_arraysize(extensionsForAndroid),
  111. extensionsForAndroid);
  112. }
  113. SDL_bool Android_Vulkan_CreateSurface(_THIS,
  114. SDL_Window *window,
  115. VkInstance instance,
  116. VkSurfaceKHR *surface)
  117. {
  118. SDL_WindowData *windowData = (SDL_WindowData *)window->driverdata;
  119. PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr =
  120. (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr;
  121. PFN_vkCreateAndroidSurfaceKHR vkCreateAndroidSurfaceKHR =
  122. (PFN_vkCreateAndroidSurfaceKHR)vkGetInstanceProcAddr(
  123. instance,
  124. "vkCreateAndroidSurfaceKHR");
  125. VkAndroidSurfaceCreateInfoKHR createInfo;
  126. VkResult result;
  127. if (!_this->vulkan_config.loader_handle) {
  128. SDL_SetError("Vulkan is not loaded");
  129. return SDL_FALSE;
  130. }
  131. if (!vkCreateAndroidSurfaceKHR) {
  132. SDL_SetError(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME
  133. " extension is not enabled in the Vulkan instance.");
  134. return SDL_FALSE;
  135. }
  136. SDL_zero(createInfo);
  137. createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR;
  138. createInfo.pNext = NULL;
  139. createInfo.flags = 0;
  140. createInfo.window = windowData->native_window;
  141. result = vkCreateAndroidSurfaceKHR(instance, &createInfo,
  142. NULL, surface);
  143. if (result != VK_SUCCESS) {
  144. SDL_SetError("vkCreateAndroidSurfaceKHR failed: %s",
  145. SDL_Vulkan_GetResultString(result));
  146. return SDL_FALSE;
  147. }
  148. return SDL_TRUE;
  149. }
  150. #endif