SDL_vivantevulkan.c (5643B)
1 /* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2020 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 Wladimir J. van der Laan. Based on Jacob Lifshay's 24 * SDL_x11vulkan.c, Mark Callow's SDL_androidvulkan.c, and 25 * the FSL demo framework. 26 */ 27 28 #include "../../SDL_internal.h" 29 30 #if SDL_VIDEO_VULKAN && SDL_VIDEO_DRIVER_VIVANTE 31 32 #include "SDL_vivantevideo.h" 33 34 #include "SDL_loadso.h" 35 #include "SDL_vivantevulkan.h" 36 #include "SDL_syswm.h" 37 38 int VIVANTE_Vulkan_LoadLibrary(_THIS, const char *path) 39 { 40 VkExtensionProperties *extensions = NULL; 41 Uint32 i, extensionCount = 0; 42 SDL_bool hasSurfaceExtension = SDL_FALSE; 43 SDL_bool hasDisplayExtension = SDL_FALSE; 44 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = NULL; 45 if(_this->vulkan_config.loader_handle) 46 return SDL_SetError("Vulkan already loaded"); 47 48 /* Load the Vulkan loader library */ 49 if(!path) 50 path = SDL_getenv("SDL_VULKAN_LIBRARY"); 51 if(!path) 52 { 53 /* If no path set, try Vivante fb vulkan driver explicitly */ 54 path = "libvulkan-fb.so"; 55 _this->vulkan_config.loader_handle = SDL_LoadObject(path); 56 if(!_this->vulkan_config.loader_handle) 57 { 58 /* If that couldn't be loaded, fall back to default name */ 59 path = "libvulkan.so"; 60 _this->vulkan_config.loader_handle = SDL_LoadObject(path); 61 } 62 } else { 63 _this->vulkan_config.loader_handle = SDL_LoadObject(path); 64 } 65 if(!_this->vulkan_config.loader_handle) 66 return -1; 67 SDL_strlcpy(_this->vulkan_config.loader_path, path, 68 SDL_arraysize(_this->vulkan_config.loader_path)); 69 SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "vivante: Loaded vulkan driver %s", path); 70 vkGetInstanceProcAddr = (PFN_vkGetInstanceProcAddr)SDL_LoadFunction( 71 _this->vulkan_config.loader_handle, "vkGetInstanceProcAddr"); 72 if(!vkGetInstanceProcAddr) 73 goto fail; 74 _this->vulkan_config.vkGetInstanceProcAddr = (void *)vkGetInstanceProcAddr; 75 _this->vulkan_config.vkEnumerateInstanceExtensionProperties = 76 (void *)((PFN_vkGetInstanceProcAddr)_this->vulkan_config.vkGetInstanceProcAddr)( 77 VK_NULL_HANDLE, "vkEnumerateInstanceExtensionProperties"); 78 if(!_this->vulkan_config.vkEnumerateInstanceExtensionProperties) 79 goto fail; 80 extensions = SDL_Vulkan_CreateInstanceExtensionsList( 81 (PFN_vkEnumerateInstanceExtensionProperties) 82 _this->vulkan_config.vkEnumerateInstanceExtensionProperties, 83 &extensionCount); 84 if(!extensions) 85 goto fail; 86 for(i = 0; i < extensionCount; i++) 87 { 88 if(SDL_strcmp(VK_KHR_SURFACE_EXTENSION_NAME, extensions[i].extensionName) == 0) 89 hasSurfaceExtension = SDL_TRUE; 90 else if(SDL_strcmp(VK_KHR_DISPLAY_EXTENSION_NAME, extensions[i].extensionName) == 0) 91 hasDisplayExtension = SDL_TRUE; 92 } 93 SDL_free(extensions); 94 if(!hasSurfaceExtension) 95 { 96 SDL_SetError("Installed Vulkan doesn't implement the " 97 VK_KHR_SURFACE_EXTENSION_NAME " extension"); 98 goto fail; 99 } 100 else if(!hasDisplayExtension) 101 { 102 SDL_SetError("Installed Vulkan doesn't implement the " 103 VK_KHR_DISPLAY_EXTENSION_NAME "extension"); 104 goto fail; 105 } 106 return 0; 107 108 fail: 109 SDL_UnloadObject(_this->vulkan_config.loader_handle); 110 _this->vulkan_config.loader_handle = NULL; 111 return -1; 112 } 113 114 void VIVANTE_Vulkan_UnloadLibrary(_THIS) 115 { 116 if(_this->vulkan_config.loader_handle) 117 { 118 SDL_UnloadObject(_this->vulkan_config.loader_handle); 119 _this->vulkan_config.loader_handle = NULL; 120 } 121 } 122 123 SDL_bool VIVANTE_Vulkan_GetInstanceExtensions(_THIS, 124 SDL_Window *window, 125 unsigned *count, 126 const char **names) 127 { 128 static const char *const extensionsForVivante[] = { 129 VK_KHR_SURFACE_EXTENSION_NAME, VK_KHR_DISPLAY_EXTENSION_NAME 130 }; 131 if(!_this->vulkan_config.loader_handle) 132 { 133 SDL_SetError("Vulkan is not loaded"); 134 return SDL_FALSE; 135 } 136 return SDL_Vulkan_GetInstanceExtensions_Helper( 137 count, names, SDL_arraysize(extensionsForVivante), 138 extensionsForVivante); 139 } 140 141 SDL_bool VIVANTE_Vulkan_CreateSurface(_THIS, 142 SDL_Window *window, 143 VkInstance instance, 144 VkSurfaceKHR *surface) 145 { 146 if(!_this->vulkan_config.loader_handle) 147 { 148 SDL_SetError("Vulkan is not loaded"); 149 return SDL_FALSE; 150 } 151 return SDL_Vulkan_Display_CreateSurface(_this->vulkan_config.vkGetInstanceProcAddr, instance, surface); 152 } 153 154 #endif 155 156 /* vi: set ts=4 sw=4 expandtab: */ 157