diff options
| author | 2020-12-24 22:05:48 -0300 | |
|---|---|---|
| committer | 2020-12-31 02:07:33 -0300 | |
| commit | 25f88d99cead2f7f6fdbf5e36e7578472aaa65bd (patch) | |
| tree | f53fb29591c7a9ab73220b29431337ad2d5596bd /src/video_core/vulkan_common | |
| parent | vulkan_common: Rename renderer_vulkan/wrapper.h to vulkan_common/vulkan_wrapp... (diff) | |
| download | yuzu-25f88d99cead2f7f6fdbf5e36e7578472aaa65bd.tar.gz yuzu-25f88d99cead2f7f6fdbf5e36e7578472aaa65bd.tar.xz yuzu-25f88d99cead2f7f6fdbf5e36e7578472aaa65bd.zip | |
renderer_vulkan: Move instance initialization to a separate file
Simplify Vulkan's backend initialization code by moving it to a separate
file, allowing us to initialize a Vulkan instance from different
backends.
Diffstat (limited to 'src/video_core/vulkan_common')
| -rw-r--r-- | src/video_core/vulkan_common/vulkan_instance.cpp | 152 | ||||
| -rw-r--r-- | src/video_core/vulkan_common/vulkan_instance.h | 21 |
2 files changed, 173 insertions, 0 deletions
diff --git a/src/video_core/vulkan_common/vulkan_instance.cpp b/src/video_core/vulkan_common/vulkan_instance.cpp new file mode 100644 index 000000000..c19f93e0a --- /dev/null +++ b/src/video_core/vulkan_common/vulkan_instance.cpp | |||
| @@ -0,0 +1,152 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <optional> | ||
| 7 | #include <span> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | #include "common/common_types.h" | ||
| 12 | #include "common/dynamic_library.h" | ||
| 13 | #include "common/logging/log.h" | ||
| 14 | #include "core/frontend/emu_window.h" | ||
| 15 | #include "video_core/vulkan_common/vulkan_instance.h" | ||
| 16 | #include "video_core/vulkan_common/vulkan_wrapper.h" | ||
| 17 | |||
| 18 | // Include these late to avoid polluting previous headers | ||
| 19 | #ifdef _WIN32 | ||
| 20 | #include <windows.h> | ||
| 21 | // ensure include order | ||
| 22 | #include <vulkan/vulkan_win32.h> | ||
| 23 | #endif | ||
| 24 | |||
| 25 | #if !defined(_WIN32) && !defined(__APPLE__) | ||
| 26 | #include <X11/Xlib.h> | ||
| 27 | #include <vulkan/vulkan_wayland.h> | ||
| 28 | #include <vulkan/vulkan_xlib.h> | ||
| 29 | #endif | ||
| 30 | |||
| 31 | namespace Vulkan { | ||
| 32 | namespace { | ||
| 33 | [[nodiscard]] std::vector<const char*> RequiredExtensions( | ||
| 34 | Core::Frontend::WindowSystemType window_type, bool enable_debug_utils) { | ||
| 35 | std::vector<const char*> extensions; | ||
| 36 | extensions.reserve(6); | ||
| 37 | switch (window_type) { | ||
| 38 | case Core::Frontend::WindowSystemType::Headless: | ||
| 39 | break; | ||
| 40 | #ifdef _WIN32 | ||
| 41 | case Core::Frontend::WindowSystemType::Windows: | ||
| 42 | extensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME); | ||
| 43 | break; | ||
| 44 | #endif | ||
| 45 | #if !defined(_WIN32) && !defined(__APPLE__) | ||
| 46 | case Core::Frontend::WindowSystemType::X11: | ||
| 47 | extensions.push_back(VK_KHR_XLIB_SURFACE_EXTENSION_NAME); | ||
| 48 | break; | ||
| 49 | case Core::Frontend::WindowSystemType::Wayland: | ||
| 50 | extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME); | ||
| 51 | break; | ||
| 52 | #endif | ||
| 53 | default: | ||
| 54 | LOG_ERROR(Render_Vulkan, "Presentation not supported on this platform"); | ||
| 55 | break; | ||
| 56 | } | ||
| 57 | if (window_type != Core::Frontend::WindowSystemType::Headless) { | ||
| 58 | extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME); | ||
| 59 | } | ||
| 60 | if (enable_debug_utils) { | ||
| 61 | extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); | ||
| 62 | } | ||
| 63 | extensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME); | ||
| 64 | return extensions; | ||
| 65 | } | ||
| 66 | |||
| 67 | [[nodiscard]] bool AreExtensionsSupported(const vk::InstanceDispatch& dld, | ||
| 68 | std::span<const char* const> extensions) { | ||
| 69 | const std::optional properties = vk::EnumerateInstanceExtensionProperties(dld); | ||
| 70 | if (!properties) { | ||
| 71 | LOG_ERROR(Render_Vulkan, "Failed to query extension properties"); | ||
| 72 | return false; | ||
| 73 | } | ||
| 74 | for (const char* extension : extensions) { | ||
| 75 | const auto it = std::ranges::find_if(*properties, [extension](const auto& prop) { | ||
| 76 | return std::strcmp(extension, prop.extensionName) == 0; | ||
| 77 | }); | ||
| 78 | if (it == properties->end()) { | ||
| 79 | LOG_ERROR(Render_Vulkan, "Required instance extension {} is not available", extension); | ||
| 80 | return false; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | return true; | ||
| 84 | } | ||
| 85 | |||
| 86 | [[nodiscard]] std::vector<const char*> Layers(bool enable_layers) { | ||
| 87 | std::vector<const char*> layers; | ||
| 88 | if (enable_layers) { | ||
| 89 | layers.push_back("VK_LAYER_KHRONOS_validation"); | ||
| 90 | } | ||
| 91 | return layers; | ||
| 92 | } | ||
| 93 | |||
| 94 | void RemoveUnavailableLayers(const vk::InstanceDispatch& dld, std::vector<const char*>& layers) { | ||
| 95 | const std::optional layer_properties = vk::EnumerateInstanceLayerProperties(dld); | ||
| 96 | if (!layer_properties) { | ||
| 97 | LOG_ERROR(Render_Vulkan, "Failed to query layer properties, disabling layers"); | ||
| 98 | layers.clear(); | ||
| 99 | } | ||
| 100 | std::erase_if(layers, [&layer_properties](const char* layer) { | ||
| 101 | const auto comp = [layer](const VkLayerProperties& layer_property) { | ||
| 102 | return std::strcmp(layer, layer_property.layerName) == 0; | ||
| 103 | }; | ||
| 104 | const auto it = std::ranges::find_if(*layer_properties, comp); | ||
| 105 | if (it == layer_properties->end()) { | ||
| 106 | LOG_ERROR(Render_Vulkan, "Layer {} not available, removing it", layer); | ||
| 107 | return true; | ||
| 108 | } | ||
| 109 | return false; | ||
| 110 | }); | ||
| 111 | } | ||
| 112 | } // Anonymous namespace | ||
| 113 | |||
| 114 | std::pair<vk::Instance, u32> CreateInstance(Common::DynamicLibrary& library, | ||
| 115 | vk::InstanceDispatch& dld, | ||
| 116 | Core::Frontend::WindowSystemType window_type, | ||
| 117 | bool enable_debug_utils, bool enable_layers) { | ||
| 118 | if (!library.IsOpen()) { | ||
| 119 | LOG_ERROR(Render_Vulkan, "Vulkan library not available"); | ||
| 120 | return {}; | ||
| 121 | } | ||
| 122 | if (!library.GetSymbol("vkGetInstanceProcAddr", &dld.vkGetInstanceProcAddr)) { | ||
| 123 | LOG_ERROR(Render_Vulkan, "vkGetInstanceProcAddr not present in Vulkan"); | ||
| 124 | return {}; | ||
| 125 | } | ||
| 126 | if (!vk::Load(dld)) { | ||
| 127 | LOG_ERROR(Render_Vulkan, "Failed to load Vulkan function pointers"); | ||
| 128 | return {}; | ||
| 129 | } | ||
| 130 | const std::vector<const char*> extensions = RequiredExtensions(window_type, enable_debug_utils); | ||
| 131 | if (!AreExtensionsSupported(dld, extensions)) { | ||
| 132 | return {}; | ||
| 133 | } | ||
| 134 | |||
| 135 | std::vector<const char*> layers = Layers(enable_layers); | ||
| 136 | RemoveUnavailableLayers(dld, layers); | ||
| 137 | |||
| 138 | // Limit the maximum version of Vulkan to avoid using untested version. | ||
| 139 | const u32 version = std::min(vk::AvailableVersion(dld), VK_API_VERSION_1_1); | ||
| 140 | |||
| 141 | vk::Instance instance = vk::Instance::Create(version, layers, extensions, dld); | ||
| 142 | if (!instance) { | ||
| 143 | LOG_ERROR(Render_Vulkan, "Failed to create Vulkan instance"); | ||
| 144 | return {}; | ||
| 145 | } | ||
| 146 | if (!vk::Load(*instance, dld)) { | ||
| 147 | LOG_ERROR(Render_Vulkan, "Failed to load Vulkan instance function pointers"); | ||
| 148 | } | ||
| 149 | return std::make_pair(std::move(instance), version); | ||
| 150 | } | ||
| 151 | |||
| 152 | } // namespace Vulkan | ||
diff --git a/src/video_core/vulkan_common/vulkan_instance.h b/src/video_core/vulkan_common/vulkan_instance.h new file mode 100644 index 000000000..ff2be0a48 --- /dev/null +++ b/src/video_core/vulkan_common/vulkan_instance.h | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <utility> | ||
| 8 | |||
| 9 | #include "common/common_types.h" | ||
| 10 | #include "common/dynamic_library.h" | ||
| 11 | #include "core/frontend/emu_window.h" | ||
| 12 | #include "video_core/vulkan_common/vulkan_wrapper.h" | ||
| 13 | |||
| 14 | namespace Vulkan { | ||
| 15 | |||
| 16 | [[nodiscard]] std::pair<vk::Instance, u32> CreateInstance( | ||
| 17 | Common::DynamicLibrary& library, vk::InstanceDispatch& dld, | ||
| 18 | Core::Frontend::WindowSystemType window_type = Core::Frontend::WindowSystemType::Headless, | ||
| 19 | bool enable_debug_utils = false, bool enable_layers = false); | ||
| 20 | |||
| 21 | } // namespace Vulkan | ||