vk_device.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <map>
  5. #include <optional>
  6. #include <set>
  7. #include <vector>
  8. #include "common/assert.h"
  9. #include "video_core/renderer_vulkan/declarations.h"
  10. #include "video_core/renderer_vulkan/vk_device.h"
  11. namespace Vulkan {
  12. namespace Alternatives {
  13. constexpr std::array<vk::Format, 3> Depth24UnormS8Uint = {
  14. vk::Format::eD32SfloatS8Uint, vk::Format::eD16UnormS8Uint, {}};
  15. constexpr std::array<vk::Format, 3> Depth16UnormS8Uint = {
  16. vk::Format::eD24UnormS8Uint, vk::Format::eD32SfloatS8Uint, {}};
  17. constexpr std::array<vk::Format, 2> Astc = {vk::Format::eA8B8G8R8UnormPack32, {}};
  18. } // namespace Alternatives
  19. constexpr const vk::Format* GetFormatAlternatives(vk::Format format) {
  20. switch (format) {
  21. case vk::Format::eD24UnormS8Uint:
  22. return Alternatives::Depth24UnormS8Uint.data();
  23. case vk::Format::eD16UnormS8Uint:
  24. return Alternatives::Depth16UnormS8Uint.data();
  25. default:
  26. return nullptr;
  27. }
  28. }
  29. constexpr vk::FormatFeatureFlags GetFormatFeatures(vk::FormatProperties properties,
  30. FormatType format_type) {
  31. switch (format_type) {
  32. case FormatType::Linear:
  33. return properties.linearTilingFeatures;
  34. case FormatType::Optimal:
  35. return properties.optimalTilingFeatures;
  36. case FormatType::Buffer:
  37. return properties.bufferFeatures;
  38. default:
  39. return {};
  40. }
  41. }
  42. VKDevice::VKDevice(const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDevice physical,
  43. vk::SurfaceKHR surface)
  44. : physical{physical}, format_properties{GetFormatProperties(dldi, physical)} {
  45. SetupFamilies(dldi, surface);
  46. SetupProperties(dldi);
  47. SetupFeatures(dldi);
  48. }
  49. VKDevice::~VKDevice() = default;
  50. bool VKDevice::Create(const vk::DispatchLoaderDynamic& dldi, vk::Instance instance) {
  51. vk::PhysicalDeviceFeatures device_features;
  52. device_features.vertexPipelineStoresAndAtomics = true;
  53. device_features.independentBlend = true;
  54. device_features.textureCompressionASTC_LDR = is_optimal_astc_supported;
  55. const auto queue_cis = GetDeviceQueueCreateInfos();
  56. const std::vector<const char*> extensions = LoadExtensions(dldi);
  57. const vk::DeviceCreateInfo device_ci({}, static_cast<u32>(queue_cis.size()), queue_cis.data(),
  58. 0, nullptr, static_cast<u32>(extensions.size()),
  59. extensions.data(), &device_features);
  60. vk::Device dummy_logical;
  61. if (physical.createDevice(&device_ci, nullptr, &dummy_logical, dldi) != vk::Result::eSuccess) {
  62. LOG_CRITICAL(Render_Vulkan, "Logical device failed to be created!");
  63. return false;
  64. }
  65. dld.init(instance, dldi.vkGetInstanceProcAddr, dummy_logical, dldi.vkGetDeviceProcAddr);
  66. logical = UniqueDevice(
  67. dummy_logical, vk::ObjectDestroy<vk::NoParent, vk::DispatchLoaderDynamic>(nullptr, dld));
  68. graphics_queue = logical->getQueue(graphics_family, 0, dld);
  69. present_queue = logical->getQueue(present_family, 0, dld);
  70. return true;
  71. }
  72. vk::Format VKDevice::GetSupportedFormat(vk::Format wanted_format,
  73. vk::FormatFeatureFlags wanted_usage,
  74. FormatType format_type) const {
  75. if (IsFormatSupported(wanted_format, wanted_usage, format_type)) {
  76. return wanted_format;
  77. }
  78. // The wanted format is not supported by hardware, search for alternatives
  79. const vk::Format* alternatives = GetFormatAlternatives(wanted_format);
  80. if (alternatives == nullptr) {
  81. LOG_CRITICAL(Render_Vulkan,
  82. "Format={} with usage={} and type={} has no defined alternatives and host "
  83. "hardware does not support it",
  84. vk::to_string(wanted_format), vk::to_string(wanted_usage),
  85. static_cast<u32>(format_type));
  86. UNREACHABLE();
  87. return wanted_format;
  88. }
  89. std::size_t i = 0;
  90. for (vk::Format alternative = alternatives[0]; alternative != vk::Format{};
  91. alternative = alternatives[++i]) {
  92. if (!IsFormatSupported(alternative, wanted_usage, format_type))
  93. continue;
  94. LOG_WARNING(Render_Vulkan,
  95. "Emulating format={} with alternative format={} with usage={} and type={}",
  96. static_cast<u32>(wanted_format), static_cast<u32>(alternative),
  97. static_cast<u32>(wanted_usage), static_cast<u32>(format_type));
  98. return alternative;
  99. }
  100. // No alternatives found, panic
  101. LOG_CRITICAL(Render_Vulkan,
  102. "Format={} with usage={} and type={} is not supported by the host hardware and "
  103. "doesn't support any of the alternatives",
  104. static_cast<u32>(wanted_format), static_cast<u32>(wanted_usage),
  105. static_cast<u32>(format_type));
  106. UNREACHABLE();
  107. return wanted_format;
  108. }
  109. bool VKDevice::IsOptimalAstcSupported(const vk::PhysicalDeviceFeatures& features,
  110. const vk::DispatchLoaderDynamic& dldi) const {
  111. if (!features.textureCompressionASTC_LDR) {
  112. return false;
  113. }
  114. const auto format_feature_usage{
  115. vk::FormatFeatureFlagBits::eSampledImage | vk::FormatFeatureFlagBits::eBlitSrc |
  116. vk::FormatFeatureFlagBits::eBlitDst | vk::FormatFeatureFlagBits::eTransferSrc |
  117. vk::FormatFeatureFlagBits::eTransferDst};
  118. constexpr std::array<vk::Format, 9> astc_formats = {
  119. vk::Format::eAstc4x4UnormBlock, vk::Format::eAstc4x4SrgbBlock,
  120. vk::Format::eAstc8x8SrgbBlock, vk::Format::eAstc8x6SrgbBlock,
  121. vk::Format::eAstc5x4SrgbBlock, vk::Format::eAstc5x5UnormBlock,
  122. vk::Format::eAstc5x5SrgbBlock, vk::Format::eAstc10x8UnormBlock,
  123. vk::Format::eAstc10x8SrgbBlock};
  124. for (const auto format : astc_formats) {
  125. const auto format_properties{physical.getFormatProperties(format, dldi)};
  126. if (!(format_properties.optimalTilingFeatures & format_feature_usage)) {
  127. return false;
  128. }
  129. }
  130. return true;
  131. }
  132. bool VKDevice::IsFormatSupported(vk::Format wanted_format, vk::FormatFeatureFlags wanted_usage,
  133. FormatType format_type) const {
  134. const auto it = format_properties.find(wanted_format);
  135. if (it == format_properties.end()) {
  136. LOG_CRITICAL(Render_Vulkan, "Unimplemented format query={}", vk::to_string(wanted_format));
  137. UNREACHABLE();
  138. return true;
  139. }
  140. const vk::FormatFeatureFlags supported_usage = GetFormatFeatures(it->second, format_type);
  141. return (supported_usage & wanted_usage) == wanted_usage;
  142. }
  143. bool VKDevice::IsSuitable(const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDevice physical,
  144. vk::SurfaceKHR surface) {
  145. bool has_swapchain{};
  146. for (const auto& prop : physical.enumerateDeviceExtensionProperties(nullptr, dldi)) {
  147. has_swapchain |= prop.extensionName == std::string(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
  148. }
  149. if (!has_swapchain) {
  150. // The device doesn't support creating swapchains.
  151. return false;
  152. }
  153. bool has_graphics{}, has_present{};
  154. const auto queue_family_properties = physical.getQueueFamilyProperties(dldi);
  155. for (u32 i = 0; i < static_cast<u32>(queue_family_properties.size()); ++i) {
  156. const auto& family = queue_family_properties[i];
  157. if (family.queueCount == 0)
  158. continue;
  159. has_graphics |=
  160. (family.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast<vk::QueueFlagBits>(0);
  161. has_present |= physical.getSurfaceSupportKHR(i, surface, dldi) != 0;
  162. }
  163. if (!has_graphics || !has_present) {
  164. // The device doesn't have a graphics and present queue.
  165. return false;
  166. }
  167. // TODO(Rodrigo): Check if the device matches all requeriments.
  168. const auto properties{physical.getProperties(dldi)};
  169. const auto limits{properties.limits};
  170. if (limits.maxUniformBufferRange < 65536) {
  171. return false;
  172. }
  173. const vk::PhysicalDeviceFeatures features{physical.getFeatures(dldi)};
  174. if (!features.vertexPipelineStoresAndAtomics || !features.independentBlend) {
  175. return false;
  176. }
  177. // Device is suitable.
  178. return true;
  179. }
  180. std::vector<const char*> VKDevice::LoadExtensions(const vk::DispatchLoaderDynamic& dldi) {
  181. std::vector<const char*> extensions;
  182. extensions.reserve(2);
  183. extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
  184. const auto Test = [&](const vk::ExtensionProperties& extension,
  185. std::optional<std::reference_wrapper<bool>> status, const char* name,
  186. u32 revision) {
  187. if (extension.extensionName != std::string(name)) {
  188. return;
  189. }
  190. extensions.push_back(name);
  191. if (status) {
  192. status->get() = true;
  193. }
  194. };
  195. for (const auto& extension : physical.enumerateDeviceExtensionProperties(nullptr, dldi)) {
  196. Test(extension, ext_scalar_block_layout, VK_EXT_SCALAR_BLOCK_LAYOUT_EXTENSION_NAME, 1);
  197. }
  198. return extensions;
  199. }
  200. void VKDevice::SetupFamilies(const vk::DispatchLoaderDynamic& dldi, vk::SurfaceKHR surface) {
  201. std::optional<u32> graphics_family_, present_family_;
  202. const auto queue_family_properties = physical.getQueueFamilyProperties(dldi);
  203. for (u32 i = 0; i < static_cast<u32>(queue_family_properties.size()); ++i) {
  204. if (graphics_family_ && present_family_)
  205. break;
  206. const auto& queue_family = queue_family_properties[i];
  207. if (queue_family.queueCount == 0)
  208. continue;
  209. if (queue_family.queueFlags & vk::QueueFlagBits::eGraphics)
  210. graphics_family_ = i;
  211. if (physical.getSurfaceSupportKHR(i, surface, dldi))
  212. present_family_ = i;
  213. }
  214. ASSERT(graphics_family_ && present_family_);
  215. graphics_family = *graphics_family_;
  216. present_family = *present_family_;
  217. }
  218. void VKDevice::SetupProperties(const vk::DispatchLoaderDynamic& dldi) {
  219. const vk::PhysicalDeviceProperties props = physical.getProperties(dldi);
  220. device_type = props.deviceType;
  221. uniform_buffer_alignment = static_cast<u64>(props.limits.minUniformBufferOffsetAlignment);
  222. max_storage_buffer_range = static_cast<u64>(props.limits.maxStorageBufferRange);
  223. }
  224. void VKDevice::SetupFeatures(const vk::DispatchLoaderDynamic& dldi) {
  225. const auto supported_features{physical.getFeatures(dldi)};
  226. is_optimal_astc_supported = IsOptimalAstcSupported(supported_features, dldi);
  227. }
  228. std::vector<vk::DeviceQueueCreateInfo> VKDevice::GetDeviceQueueCreateInfos() const {
  229. static const float QUEUE_PRIORITY = 1.0f;
  230. std::set<u32> unique_queue_families = {graphics_family, present_family};
  231. std::vector<vk::DeviceQueueCreateInfo> queue_cis;
  232. for (u32 queue_family : unique_queue_families)
  233. queue_cis.push_back({{}, queue_family, 1, &QUEUE_PRIORITY});
  234. return queue_cis;
  235. }
  236. std::map<vk::Format, vk::FormatProperties> VKDevice::GetFormatProperties(
  237. const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDevice physical) {
  238. static constexpr std::array<vk::Format, 33> formats = {vk::Format::eA8B8G8R8UnormPack32,
  239. vk::Format::eB5G6R5UnormPack16,
  240. vk::Format::eA2B10G10R10UnormPack32,
  241. vk::Format::eR32G32B32A32Sfloat,
  242. vk::Format::eR16G16Unorm,
  243. vk::Format::eR16G16Snorm,
  244. vk::Format::eR8G8B8A8Srgb,
  245. vk::Format::eR8Unorm,
  246. vk::Format::eB10G11R11UfloatPack32,
  247. vk::Format::eR32Sfloat,
  248. vk::Format::eR16Sfloat,
  249. vk::Format::eR16G16B16A16Sfloat,
  250. vk::Format::eD32Sfloat,
  251. vk::Format::eD16Unorm,
  252. vk::Format::eD16UnormS8Uint,
  253. vk::Format::eD24UnormS8Uint,
  254. vk::Format::eD32SfloatS8Uint,
  255. vk::Format::eBc1RgbaUnormBlock,
  256. vk::Format::eBc2UnormBlock,
  257. vk::Format::eBc3UnormBlock,
  258. vk::Format::eBc4UnormBlock,
  259. vk::Format::eBc5UnormBlock,
  260. vk::Format::eBc5SnormBlock,
  261. vk::Format::eBc7UnormBlock,
  262. vk::Format::eAstc4x4UnormBlock,
  263. vk::Format::eAstc4x4SrgbBlock,
  264. vk::Format::eAstc8x8SrgbBlock,
  265. vk::Format::eAstc8x6SrgbBlock,
  266. vk::Format::eAstc5x4SrgbBlock,
  267. vk::Format::eAstc5x5UnormBlock,
  268. vk::Format::eAstc5x5SrgbBlock,
  269. vk::Format::eAstc10x8UnormBlock,
  270. vk::Format::eAstc10x8SrgbBlock};
  271. std::map<vk::Format, vk::FormatProperties> format_properties;
  272. for (const auto format : formats) {
  273. format_properties.emplace(format, physical.getFormatProperties(format, dldi));
  274. }
  275. return format_properties;
  276. }
  277. } // namespace Vulkan