diff options
Diffstat (limited to 'contrib/SDL-3.2.8/src/video/android/SDL_androidvulkan.c')
| -rw-r--r-- | contrib/SDL-3.2.8/src/video/android/SDL_androidvulkan.c | 171 |
1 files changed, 171 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/video/android/SDL_androidvulkan.c b/contrib/SDL-3.2.8/src/video/android/SDL_androidvulkan.c new file mode 100644 index 0000000..4d38388 --- /dev/null +++ b/contrib/SDL-3.2.8/src/video/android/SDL_androidvulkan.c | |||
| @@ -0,0 +1,171 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 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_ANDROID) | ||
| 30 | |||
| 31 | #include "../SDL_vulkan_internal.h" | ||
| 32 | |||
| 33 | #include "SDL_androidvideo.h" | ||
| 34 | #include "SDL_androidwindow.h" | ||
| 35 | |||
| 36 | #include "SDL_androidvulkan.h" | ||
| 37 | |||
| 38 | |||
| 39 | bool Android_Vulkan_LoadLibrary(SDL_VideoDevice *_this, const char *path) | ||
| 40 | { | ||
| 41 | VkExtensionProperties *extensions = NULL; | ||
| 42 | Uint32 i, extensionCount = 0; | ||
| 43 | bool hasSurfaceExtension = false; | ||
| 44 | bool hasAndroidSurfaceExtension = 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 = "libvulkan.so"; | ||
| 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 = (void *)vkGetInstanceProcAddr; | ||
| 69 | _this->vulkan_config.vkEnumerateInstanceExtensionProperties = | ||
| 70 | (void *)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.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_ANDROID_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) { | ||
| 86 | hasAndroidSurfaceExtension = 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 (!hasAndroidSurfaceExtension) { | ||
| 94 | SDL_SetError("Installed Vulkan doesn't implement the " VK_KHR_ANDROID_SURFACE_EXTENSION_NAME "extension"); | ||
| 95 | goto fail; | ||
| 96 | } | ||
| 97 | return true; | ||
| 98 | |||
| 99 | fail: | ||
| 100 | SDL_UnloadObject(_this->vulkan_config.loader_handle); | ||
| 101 | _this->vulkan_config.loader_handle = NULL; | ||
| 102 | return false; | ||
| 103 | } | ||
| 104 | |||
| 105 | void Android_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 | |||
| 113 | char const* const* Android_Vulkan_GetInstanceExtensions(SDL_VideoDevice *_this, | ||
| 114 | Uint32 *count) | ||
| 115 | { | ||
| 116 | static const char *const extensionsForAndroid[] = { | ||
| 117 | VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_ANDROID_SURFACE_EXTENSION_NAME | ||
| 118 | }; | ||
| 119 | if (count) { | ||
| 120 | *count = SDL_arraysize(extensionsForAndroid); | ||
| 121 | } | ||
| 122 | return extensionsForAndroid; | ||
| 123 | } | ||
| 124 | |||
| 125 | bool Android_Vulkan_CreateSurface(SDL_VideoDevice *_this, | ||
| 126 | SDL_Window *window, | ||
| 127 | VkInstance instance, | ||
| 128 | const struct VkAllocationCallbacks *allocator, | ||
| 129 | VkSurfaceKHR *surface) | ||
| 130 | { | ||
| 131 | SDL_WindowData *windowData = window->internal; | ||
| 132 | PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = | ||
| 133 | (PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr; | ||
| 134 | PFN_vkCreateAndroidSurfaceKHR vkCreateAndroidSurfaceKHR = | ||
| 135 | (PFN_vkCreateAndroidSurfaceKHR)vkGetInstanceProcAddr( | ||
| 136 | instance, | ||
| 137 | "vkCreateAndroidSurfaceKHR"); | ||
| 138 | VkAndroidSurfaceCreateInfoKHR createInfo; | ||
| 139 | VkResult result; | ||
| 140 | |||
| 141 | if (!_this->vulkan_config.loader_handle) { | ||
| 142 | return SDL_SetError("Vulkan is not loaded"); | ||
| 143 | } | ||
| 144 | |||
| 145 | if (!vkCreateAndroidSurfaceKHR) { | ||
| 146 | return SDL_SetError(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME | ||
| 147 | " extension is not enabled in the Vulkan instance."); | ||
| 148 | } | ||
| 149 | SDL_zero(createInfo); | ||
| 150 | createInfo.sType = VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR; | ||
| 151 | createInfo.pNext = NULL; | ||
| 152 | createInfo.flags = 0; | ||
| 153 | createInfo.window = windowData->native_window; | ||
| 154 | result = vkCreateAndroidSurfaceKHR(instance, &createInfo, allocator, surface); | ||
| 155 | if (result != VK_SUCCESS) { | ||
| 156 | return SDL_SetError("vkCreateAndroidSurfaceKHR failed: %s", SDL_Vulkan_GetResultString(result)); | ||
| 157 | } | ||
| 158 | return true; | ||
| 159 | } | ||
| 160 | |||
| 161 | void Android_Vulkan_DestroySurface(SDL_VideoDevice *_this, | ||
| 162 | VkInstance instance, | ||
| 163 | VkSurfaceKHR surface, | ||
| 164 | const struct VkAllocationCallbacks *allocator) | ||
| 165 | { | ||
| 166 | if (_this->vulkan_config.loader_handle) { | ||
| 167 | SDL_Vulkan_DestroySurface_Internal(_this->vulkan_config.vkGetInstanceProcAddr, instance, surface, allocator); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | #endif | ||
