| 1 | // Copyright 2016 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #define VMA_IMPLEMENTATION |
| 5 | #include "VideoBackends/Vulkan/VulkanLoader.h" |
| 6 | |
| 7 | #if defined(ANDROID) |
| 8 | #include <adrenotools/driver.h> |
| 9 | #include <dlfcn.h> |
| 10 | #endif |
| 11 | |
| 12 | #include "Common/DynamicLibrary.h" |
| 13 | #if defined(__APPLE__) |
| 14 | #include "Common/FileUtil.h" |
| 15 | #elif defined(ANDROID) && _M_ARM_64 |
| 16 | #include "Common/FileUtil.h" |
| 17 | |
| 18 | #include "VideoCommon/VideoConfig.h" |
| 19 | #endif |
| 20 | |
| 21 | #define VULKAN_MODULE_ENTRY_POINT(name, required) PFN_##name name; |
| 22 | #define VULKAN_INSTANCE_ENTRY_POINT(name, required) PFN_##name name; |
| 23 | #define VULKAN_DEVICE_ENTRY_POINT(name, required) PFN_##name name; |
| 24 | #include "VideoBackends/Vulkan/VulkanEntryPoints.inl" |
| 25 | #undef VULKAN_DEVICE_ENTRY_POINT |
| 26 | #undef VULKAN_INSTANCE_ENTRY_POINT |
| 27 | #undef VULKAN_MODULE_ENTRY_POINT |
| 28 | |
| 29 | namespace Vulkan |
| 30 | { |
| 31 | static void ResetVulkanLibraryFunctionPointers() |
| 32 | { |
| 33 | #define VULKAN_MODULE_ENTRY_POINT(name, required) name = nullptr; |
| 34 | #define VULKAN_INSTANCE_ENTRY_POINT(name, required) name = nullptr; |
| 35 | #define VULKAN_DEVICE_ENTRY_POINT(name, required) name = nullptr; |
| 36 | #include "VideoBackends/Vulkan/VulkanEntryPoints.inl" |
| 37 | #undef VULKAN_DEVICE_ENTRY_POINT |
| 38 | #undef VULKAN_INSTANCE_ENTRY_POINT |
| 39 | #undef VULKAN_MODULE_ENTRY_POINT |
| 40 | } |
| 41 | |
| 42 | static Common::DynamicLibrary s_vulkan_module; |
| 43 | |
| 44 | static bool OpenVulkanLibrary(bool force_system_library) |
| 45 | { |
| 46 | #if defined(__APPLE__) |
| 47 | // Check if a path to a specific Vulkan library has been specified. |
| 48 | char* libvulkan_env = getenv("LIBVULKAN_PATH"); |
| 49 | if (libvulkan_env && s_vulkan_module.Open(libvulkan_env)) |
| 50 | return true; |
| 51 | |
| 52 | // Use the libMoltenVK.dylib from the application bundle. |
| 53 | std::string filename = File::GetBundleDirectory() + "/Contents/Frameworks/libMoltenVK.dylib"; |
| 54 | return s_vulkan_module.Open(filename.c_str()); |
| 55 | #else |
| 56 | |
| 57 | #if defined(ANDROID) && _M_ARM_64 |
| 58 | const std::string& driver_lib_name = g_Config.customDriverLibraryName; |
| 59 | |
| 60 | if (!force_system_library && !driver_lib_name.empty() && SupportsCustomDriver()) |
| 61 | { |
| 62 | std::string tmp_dir = File::GetGpuDriverDirectory(D_GPU_DRIVERS_TMP); |
| 63 | std::string hook_dir = File::GetGpuDriverDirectory(D_GPU_DRIVERS_HOOKS); |
| 64 | std::string file_redirect_dir = File::GetGpuDriverDirectory(D_GPU_DRIVERS_FILE_REDIRECT); |
| 65 | std::string driver_dir = File::GetGpuDriverDirectory(D_GPU_DRIVERS_EXTRACTED); |
| 66 | INFO_LOG_FMT(HOST_GPU, "Loading driver: {}", driver_lib_name); |
| 67 | |
| 68 | s_vulkan_module = adrenotools_open_libvulkan( |
| 69 | RTLD_NOW, ADRENOTOOLS_DRIVER_FILE_REDIRECT | ADRENOTOOLS_DRIVER_CUSTOM, tmp_dir.c_str(), |
| 70 | hook_dir.c_str(), driver_dir.c_str(), driver_lib_name.c_str(), file_redirect_dir.c_str(), |
| 71 | nullptr); |
| 72 | if (s_vulkan_module.IsOpen()) |
| 73 | { |
| 74 | INFO_LOG_FMT(HOST_GPU, "Successfully loaded driver: {}", driver_lib_name); |
| 75 | return true; |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | WARN_LOG_FMT(HOST_GPU, "Loading driver {} failed.", driver_lib_name); |
| 80 | } |
| 81 | } |
| 82 | #endif |
| 83 | |
| 84 | WARN_LOG_FMT(HOST_GPU, "Loading system driver"); |
| 85 | std::string filename = Common::DynamicLibrary::GetVersionedFilename("vulkan", 1); |
| 86 | if (s_vulkan_module.Open(filename.c_str())) |
| 87 | return true; |
| 88 | |
| 89 | // Android devices may not have libvulkan.so.1, only libvulkan.so. |
| 90 | filename = Common::DynamicLibrary::GetVersionedFilename("vulkan"); |
| 91 | return s_vulkan_module.Open(filename.c_str()); |
| 92 | #endif |
| 93 | } |
| 94 | |
| 95 | bool LoadVulkanLibrary(bool force_system_library) |
| 96 | { |
| 97 | if (!s_vulkan_module.IsOpen() && !OpenVulkanLibrary(force_system_library)) |
| 98 | return false; |
| 99 | |
| 100 | #define VULKAN_MODULE_ENTRY_POINT(name, required) \ |
| 101 | if (!s_vulkan_module.GetSymbol(#name, &name) && required) \ |
| 102 | { \ |
| 103 | ERROR_LOG_FMT(VIDEO, "Vulkan: Failed to load required module function {}", #name); \ |
| 104 | ResetVulkanLibraryFunctionPointers(); \ |
| 105 | s_vulkan_module.Close(); \ |
| 106 | return false; \ |
| 107 | } |
| 108 | #include "VideoBackends/Vulkan/VulkanEntryPoints.inl" |
| 109 | #undef VULKAN_MODULE_ENTRY_POINT |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | void UnloadVulkanLibrary() |
| 115 | { |
| 116 | s_vulkan_module.Close(); |
| 117 | if (!s_vulkan_module.IsOpen()) |
| 118 | ResetVulkanLibraryFunctionPointers(); |
| 119 | } |
| 120 | |
| 121 | bool LoadVulkanInstanceFunctions(VkInstance instance) |
| 122 | { |
| 123 | bool required_functions_missing = false; |
| 124 | auto LoadFunction = [&](PFN_vkVoidFunction* func_ptr, const char* name, bool is_required) { |
| 125 | *func_ptr = vkGetInstanceProcAddr(instance, name); |
| 126 | if (!(*func_ptr) && is_required) |
| 127 | { |
| 128 | ERROR_LOG_FMT(HOST_GPU, "Vulkan: Failed to load required instance function {}", name); |
| 129 | required_functions_missing = true; |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | #define VULKAN_INSTANCE_ENTRY_POINT(name, required) \ |
| 134 | LoadFunction(reinterpret_cast<PFN_vkVoidFunction*>(&name), #name, required); |
| 135 | #include "VideoBackends/Vulkan/VulkanEntryPoints.inl" |
| 136 | #undef VULKAN_INSTANCE_ENTRY_POINT |
| 137 | |
| 138 | return !required_functions_missing; |
| 139 | } |
| 140 | |
| 141 | bool LoadVulkanDeviceFunctions(VkDevice device) |
| 142 | { |
| 143 | bool required_functions_missing = false; |
| 144 | auto LoadFunction = [&](PFN_vkVoidFunction* func_ptr, const char* name, bool is_required) { |
| 145 | *func_ptr = vkGetDeviceProcAddr(device, name); |
| 146 | if (!(*func_ptr) && is_required) |
| 147 | { |
| 148 | ERROR_LOG_FMT(HOST_GPU, "Vulkan: Failed to load required device function {}", name); |
| 149 | required_functions_missing = true; |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | #define VULKAN_DEVICE_ENTRY_POINT(name, required) \ |
| 154 | LoadFunction(reinterpret_cast<PFN_vkVoidFunction*>(&name), #name, required); |
| 155 | #include "VideoBackends/Vulkan/VulkanEntryPoints.inl" |
| 156 | #undef VULKAN_DEVICE_ENTRY_POINT |
| 157 | |
| 158 | return !required_functions_missing; |
| 159 | } |
| 160 | |
| 161 | const char* VkResultToString(VkResult res) |
| 162 | { |
| 163 | switch (res) |
| 164 | { |
| 165 | case VK_SUCCESS: |
| 166 | return "VK_SUCCESS"; |
| 167 | |
| 168 | case VK_NOT_READY: |
| 169 | return "VK_NOT_READY"; |
| 170 | |
| 171 | case VK_TIMEOUT: |
| 172 | return "VK_TIMEOUT"; |
| 173 | |
| 174 | case VK_EVENT_SET: |
| 175 | return "VK_EVENT_SET"; |
| 176 | |
| 177 | case VK_EVENT_RESET: |
| 178 | return "VK_EVENT_RESET"; |
| 179 | |
| 180 | case VK_INCOMPLETE: |
| 181 | return "VK_INCOMPLETE"; |
| 182 | |
| 183 | case VK_ERROR_OUT_OF_HOST_MEMORY: |
| 184 | return "VK_ERROR_OUT_OF_HOST_MEMORY"; |
| 185 | |
| 186 | case VK_ERROR_OUT_OF_DEVICE_MEMORY: |
| 187 | return "VK_ERROR_OUT_OF_DEVICE_MEMORY"; |
| 188 | |
| 189 | case VK_ERROR_INITIALIZATION_FAILED: |
| 190 | return "VK_ERROR_INITIALIZATION_FAILED"; |
| 191 | |
| 192 | case VK_ERROR_DEVICE_LOST: |
| 193 | return "VK_ERROR_DEVICE_LOST"; |
| 194 | |
| 195 | case VK_ERROR_MEMORY_MAP_FAILED: |
| 196 | return "VK_ERROR_MEMORY_MAP_FAILED"; |
| 197 | |
| 198 | case VK_ERROR_LAYER_NOT_PRESENT: |
| 199 | return "VK_ERROR_LAYER_NOT_PRESENT"; |
| 200 | |
| 201 | case VK_ERROR_EXTENSION_NOT_PRESENT: |
| 202 | return "VK_ERROR_EXTENSION_NOT_PRESENT"; |
| 203 | |
| 204 | case VK_ERROR_FEATURE_NOT_PRESENT: |
| 205 | return "VK_ERROR_FEATURE_NOT_PRESENT"; |
| 206 | |
| 207 | case VK_ERROR_INCOMPATIBLE_DRIVER: |
| 208 | return "VK_ERROR_INCOMPATIBLE_DRIVER"; |
| 209 | |
| 210 | case VK_ERROR_TOO_MANY_OBJECTS: |
| 211 | return "VK_ERROR_TOO_MANY_OBJECTS"; |
| 212 | |
| 213 | case VK_ERROR_FORMAT_NOT_SUPPORTED: |
| 214 | return "VK_ERROR_FORMAT_NOT_SUPPORTED"; |
| 215 | |
| 216 | case VK_ERROR_SURFACE_LOST_KHR: |
| 217 | return "VK_ERROR_SURFACE_LOST_KHR"; |
| 218 | |
| 219 | case VK_ERROR_NATIVE_WINDOW_IN_USE_KHR: |
| 220 | return "VK_ERROR_NATIVE_WINDOW_IN_USE_KHR"; |
| 221 | |
| 222 | case VK_SUBOPTIMAL_KHR: |
| 223 | return "VK_SUBOPTIMAL_KHR"; |
| 224 | |
| 225 | case VK_ERROR_OUT_OF_DATE_KHR: |
| 226 | return "VK_ERROR_OUT_OF_DATE_KHR"; |
| 227 | |
| 228 | case VK_ERROR_INCOMPATIBLE_DISPLAY_KHR: |
| 229 | return "VK_ERROR_INCOMPATIBLE_DISPLAY_KHR"; |
| 230 | |
| 231 | case VK_ERROR_VALIDATION_FAILED_EXT: |
| 232 | return "VK_ERROR_VALIDATION_FAILED_EXT"; |
| 233 | |
| 234 | case VK_ERROR_INVALID_SHADER_NV: |
| 235 | return "VK_ERROR_INVALID_SHADER_NV"; |
| 236 | |
| 237 | default: |
| 238 | return "UNKNOWN_VK_RESULT"; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | void LogVulkanResult(Common::Log::LogLevel level, const char* func_name, const int line, |
| 243 | VkResult res, const char* msg) |
| 244 | { |
| 245 | GENERIC_LOG_FMT(Common::Log::LogType::VIDEO, level, "({}:{}) {} ({}: {})", func_name, line, msg, |
| 246 | static_cast<int>(res), VkResultToString(res)); |
| 247 | } |
| 248 | |
| 249 | #ifdef ANDROID |
| 250 | static bool CheckKgslPresent() |
| 251 | { |
| 252 | constexpr auto KgslPath{"/dev/kgsl-3d0"}; |
| 253 | |
| 254 | return access(KgslPath, F_OK) == 0; |
| 255 | } |
| 256 | |
| 257 | bool SupportsCustomDriver() |
| 258 | { |
| 259 | return android_get_device_api_level() >= 28 && CheckKgslPresent(); |
| 260 | } |
| 261 | #endif |
| 262 | |
| 263 | } // namespace Vulkan |