vulkan_instance.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <future>
  6. #include <optional>
  7. #include <span>
  8. #include <utility>
  9. #include <vector>
  10. #include "common/common_types.h"
  11. #include "common/dynamic_library.h"
  12. #include "common/logging/log.h"
  13. #include "core/frontend/emu_window.h"
  14. #include "video_core/vulkan_common/vulkan_instance.h"
  15. #include "video_core/vulkan_common/vulkan_wrapper.h"
  16. // Include these late to avoid polluting previous headers
  17. #ifdef _WIN32
  18. #include <windows.h>
  19. // ensure include order
  20. #include <vulkan/vulkan_win32.h>
  21. #endif
  22. #if !defined(_WIN32) && !defined(__APPLE__)
  23. #include <X11/Xlib.h>
  24. #include <vulkan/vulkan_wayland.h>
  25. #include <vulkan/vulkan_xlib.h>
  26. #endif
  27. namespace Vulkan {
  28. namespace {
  29. [[nodiscard]] std::vector<const char*> RequiredExtensions(
  30. Core::Frontend::WindowSystemType window_type, bool enable_debug_utils) {
  31. std::vector<const char*> extensions;
  32. extensions.reserve(6);
  33. switch (window_type) {
  34. case Core::Frontend::WindowSystemType::Headless:
  35. break;
  36. #ifdef _WIN32
  37. case Core::Frontend::WindowSystemType::Windows:
  38. extensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
  39. break;
  40. #endif
  41. #if !defined(_WIN32) && !defined(__APPLE__)
  42. case Core::Frontend::WindowSystemType::X11:
  43. extensions.push_back(VK_KHR_XLIB_SURFACE_EXTENSION_NAME);
  44. break;
  45. case Core::Frontend::WindowSystemType::Wayland:
  46. extensions.push_back(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME);
  47. break;
  48. #endif
  49. default:
  50. LOG_ERROR(Render_Vulkan, "Presentation not supported on this platform");
  51. break;
  52. }
  53. if (window_type != Core::Frontend::WindowSystemType::Headless) {
  54. extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
  55. }
  56. if (enable_debug_utils) {
  57. extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
  58. }
  59. extensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
  60. return extensions;
  61. }
  62. [[nodiscard]] bool AreExtensionsSupported(const vk::InstanceDispatch& dld,
  63. std::span<const char* const> extensions) {
  64. const std::optional properties = vk::EnumerateInstanceExtensionProperties(dld);
  65. if (!properties) {
  66. LOG_ERROR(Render_Vulkan, "Failed to query extension properties");
  67. return false;
  68. }
  69. for (const char* extension : extensions) {
  70. const auto it = std::ranges::find_if(*properties, [extension](const auto& prop) {
  71. return std::strcmp(extension, prop.extensionName) == 0;
  72. });
  73. if (it == properties->end()) {
  74. LOG_ERROR(Render_Vulkan, "Required instance extension {} is not available", extension);
  75. return false;
  76. }
  77. }
  78. return true;
  79. }
  80. [[nodiscard]] std::vector<const char*> Layers(bool enable_layers) {
  81. std::vector<const char*> layers;
  82. if (enable_layers) {
  83. layers.push_back("VK_LAYER_KHRONOS_validation");
  84. }
  85. return layers;
  86. }
  87. void RemoveUnavailableLayers(const vk::InstanceDispatch& dld, std::vector<const char*>& layers) {
  88. const std::optional layer_properties = vk::EnumerateInstanceLayerProperties(dld);
  89. if (!layer_properties) {
  90. LOG_ERROR(Render_Vulkan, "Failed to query layer properties, disabling layers");
  91. layers.clear();
  92. }
  93. std::erase_if(layers, [&layer_properties](const char* layer) {
  94. const auto comp = [layer](const VkLayerProperties& layer_property) {
  95. return std::strcmp(layer, layer_property.layerName) == 0;
  96. };
  97. const auto it = std::ranges::find_if(*layer_properties, comp);
  98. if (it == layer_properties->end()) {
  99. LOG_ERROR(Render_Vulkan, "Layer {} not available, removing it", layer);
  100. return true;
  101. }
  102. return false;
  103. });
  104. }
  105. } // Anonymous namespace
  106. vk::Instance CreateInstance(const Common::DynamicLibrary& library, vk::InstanceDispatch& dld,
  107. u32 required_version, Core::Frontend::WindowSystemType window_type,
  108. bool enable_debug_utils, bool enable_layers) {
  109. if (!library.IsOpen()) {
  110. LOG_ERROR(Render_Vulkan, "Vulkan library not available");
  111. throw vk::Exception(VK_ERROR_INITIALIZATION_FAILED);
  112. }
  113. if (!library.GetSymbol("vkGetInstanceProcAddr", &dld.vkGetInstanceProcAddr)) {
  114. LOG_ERROR(Render_Vulkan, "vkGetInstanceProcAddr not present in Vulkan");
  115. throw vk::Exception(VK_ERROR_INITIALIZATION_FAILED);
  116. }
  117. if (!vk::Load(dld)) {
  118. LOG_ERROR(Render_Vulkan, "Failed to load Vulkan function pointers");
  119. throw vk::Exception(VK_ERROR_INITIALIZATION_FAILED);
  120. }
  121. const std::vector<const char*> extensions = RequiredExtensions(window_type, enable_debug_utils);
  122. if (!AreExtensionsSupported(dld, extensions)) {
  123. throw vk::Exception(VK_ERROR_EXTENSION_NOT_PRESENT);
  124. }
  125. std::vector<const char*> layers = Layers(enable_layers);
  126. RemoveUnavailableLayers(dld, layers);
  127. const u32 available_version = vk::AvailableVersion(dld);
  128. if (available_version < required_version) {
  129. LOG_ERROR(Render_Vulkan, "Vulkan {}.{} is not supported, {}.{} is required",
  130. VK_VERSION_MAJOR(available_version), VK_VERSION_MINOR(available_version),
  131. VK_VERSION_MAJOR(required_version), VK_VERSION_MINOR(required_version));
  132. throw vk::Exception(VK_ERROR_INCOMPATIBLE_DRIVER);
  133. }
  134. vk::Instance instance =
  135. std::async([&] {
  136. return vk::Instance::Create(required_version, layers, extensions, dld);
  137. }).get();
  138. if (!vk::Load(*instance, dld)) {
  139. LOG_ERROR(Render_Vulkan, "Failed to load Vulkan instance function pointers");
  140. throw vk::Exception(VK_ERROR_INITIALIZATION_FAILED);
  141. }
  142. return instance;
  143. }
  144. } // namespace Vulkan