vulkan_device.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <bitset>
  5. #include <chrono>
  6. #include <optional>
  7. #include <thread>
  8. #include <unordered_set>
  9. #include <utility>
  10. #include <vector>
  11. #include "common/assert.h"
  12. #include "common/literals.h"
  13. #include "common/polyfill_ranges.h"
  14. #include "common/settings.h"
  15. #include "video_core/vulkan_common/nsight_aftermath_tracker.h"
  16. #include "video_core/vulkan_common/vma.h"
  17. #include "video_core/vulkan_common/vulkan_device.h"
  18. #include "video_core/vulkan_common/vulkan_wrapper.h"
  19. #if defined(ANDROID) && defined(ARCHITECTURE_arm64)
  20. #include <adrenotools/bcenabler.h>
  21. #endif
  22. namespace Vulkan {
  23. using namespace Common::Literals;
  24. namespace {
  25. namespace Alternatives {
  26. constexpr std::array STENCIL8_UINT{
  27. VK_FORMAT_D16_UNORM_S8_UINT,
  28. VK_FORMAT_D24_UNORM_S8_UINT,
  29. VK_FORMAT_D32_SFLOAT_S8_UINT,
  30. VK_FORMAT_UNDEFINED,
  31. };
  32. constexpr std::array DEPTH24_UNORM_STENCIL8_UINT{
  33. VK_FORMAT_D32_SFLOAT_S8_UINT,
  34. VK_FORMAT_D16_UNORM_S8_UINT,
  35. VK_FORMAT_UNDEFINED,
  36. };
  37. constexpr std::array DEPTH16_UNORM_STENCIL8_UINT{
  38. VK_FORMAT_D24_UNORM_S8_UINT,
  39. VK_FORMAT_D32_SFLOAT_S8_UINT,
  40. VK_FORMAT_UNDEFINED,
  41. };
  42. constexpr std::array B5G6R5_UNORM_PACK16{
  43. VK_FORMAT_R5G6B5_UNORM_PACK16,
  44. VK_FORMAT_UNDEFINED,
  45. };
  46. constexpr std::array R4G4_UNORM_PACK8{
  47. VK_FORMAT_R8_UNORM,
  48. VK_FORMAT_UNDEFINED,
  49. };
  50. constexpr std::array R16G16B16_SFLOAT{
  51. VK_FORMAT_R16G16B16A16_SFLOAT,
  52. VK_FORMAT_UNDEFINED,
  53. };
  54. constexpr std::array R16G16B16_SSCALED{
  55. VK_FORMAT_R16G16B16A16_SSCALED,
  56. VK_FORMAT_UNDEFINED,
  57. };
  58. constexpr std::array R8G8B8_SSCALED{
  59. VK_FORMAT_R8G8B8A8_SSCALED,
  60. VK_FORMAT_UNDEFINED,
  61. };
  62. } // namespace Alternatives
  63. enum class NvidiaArchitecture {
  64. AmpereOrNewer,
  65. Turing,
  66. VoltaOrOlder,
  67. };
  68. template <typename T>
  69. void SetNext(void**& next, T& data) {
  70. *next = &data;
  71. next = &data.pNext;
  72. }
  73. constexpr const VkFormat* GetFormatAlternatives(VkFormat format) {
  74. switch (format) {
  75. case VK_FORMAT_S8_UINT:
  76. return Alternatives::STENCIL8_UINT.data();
  77. case VK_FORMAT_D24_UNORM_S8_UINT:
  78. return Alternatives::DEPTH24_UNORM_STENCIL8_UINT.data();
  79. case VK_FORMAT_D16_UNORM_S8_UINT:
  80. return Alternatives::DEPTH16_UNORM_STENCIL8_UINT.data();
  81. case VK_FORMAT_B5G6R5_UNORM_PACK16:
  82. return Alternatives::B5G6R5_UNORM_PACK16.data();
  83. case VK_FORMAT_R4G4_UNORM_PACK8:
  84. return Alternatives::R4G4_UNORM_PACK8.data();
  85. case VK_FORMAT_R16G16B16_SFLOAT:
  86. return Alternatives::R16G16B16_SFLOAT.data();
  87. case VK_FORMAT_R16G16B16_SSCALED:
  88. return Alternatives::R16G16B16_SSCALED.data();
  89. case VK_FORMAT_R8G8B8_SSCALED:
  90. return Alternatives::R8G8B8_SSCALED.data();
  91. default:
  92. return nullptr;
  93. }
  94. }
  95. VkFormatFeatureFlags GetFormatFeatures(VkFormatProperties properties, FormatType format_type) {
  96. switch (format_type) {
  97. case FormatType::Linear:
  98. return properties.linearTilingFeatures;
  99. case FormatType::Optimal:
  100. return properties.optimalTilingFeatures;
  101. case FormatType::Buffer:
  102. return properties.bufferFeatures;
  103. default:
  104. return {};
  105. }
  106. }
  107. std::unordered_map<VkFormat, VkFormatProperties> GetFormatProperties(vk::PhysicalDevice physical) {
  108. static constexpr std::array formats{
  109. VK_FORMAT_A1R5G5B5_UNORM_PACK16,
  110. VK_FORMAT_A2B10G10R10_SINT_PACK32,
  111. VK_FORMAT_A2B10G10R10_SNORM_PACK32,
  112. VK_FORMAT_A2B10G10R10_SSCALED_PACK32,
  113. VK_FORMAT_A2B10G10R10_UINT_PACK32,
  114. VK_FORMAT_A2B10G10R10_UNORM_PACK32,
  115. VK_FORMAT_A2B10G10R10_USCALED_PACK32,
  116. VK_FORMAT_A8B8G8R8_SINT_PACK32,
  117. VK_FORMAT_A8B8G8R8_SNORM_PACK32,
  118. VK_FORMAT_A8B8G8R8_SRGB_PACK32,
  119. VK_FORMAT_A8B8G8R8_UINT_PACK32,
  120. VK_FORMAT_A8B8G8R8_UNORM_PACK32,
  121. VK_FORMAT_ASTC_10x10_SRGB_BLOCK,
  122. VK_FORMAT_ASTC_10x10_UNORM_BLOCK,
  123. VK_FORMAT_ASTC_10x5_SRGB_BLOCK,
  124. VK_FORMAT_ASTC_10x5_UNORM_BLOCK,
  125. VK_FORMAT_ASTC_10x6_SRGB_BLOCK,
  126. VK_FORMAT_ASTC_10x6_UNORM_BLOCK,
  127. VK_FORMAT_ASTC_10x8_SRGB_BLOCK,
  128. VK_FORMAT_ASTC_10x8_UNORM_BLOCK,
  129. VK_FORMAT_ASTC_12x10_SRGB_BLOCK,
  130. VK_FORMAT_ASTC_12x10_UNORM_BLOCK,
  131. VK_FORMAT_ASTC_12x12_SRGB_BLOCK,
  132. VK_FORMAT_ASTC_12x12_UNORM_BLOCK,
  133. VK_FORMAT_ASTC_4x4_SRGB_BLOCK,
  134. VK_FORMAT_ASTC_4x4_UNORM_BLOCK,
  135. VK_FORMAT_ASTC_5x4_SRGB_BLOCK,
  136. VK_FORMAT_ASTC_5x4_UNORM_BLOCK,
  137. VK_FORMAT_ASTC_5x5_SRGB_BLOCK,
  138. VK_FORMAT_ASTC_5x5_UNORM_BLOCK,
  139. VK_FORMAT_ASTC_6x5_SRGB_BLOCK,
  140. VK_FORMAT_ASTC_6x5_UNORM_BLOCK,
  141. VK_FORMAT_ASTC_6x6_SRGB_BLOCK,
  142. VK_FORMAT_ASTC_6x6_UNORM_BLOCK,
  143. VK_FORMAT_ASTC_8x5_SRGB_BLOCK,
  144. VK_FORMAT_ASTC_8x5_UNORM_BLOCK,
  145. VK_FORMAT_ASTC_8x6_SRGB_BLOCK,
  146. VK_FORMAT_ASTC_8x6_UNORM_BLOCK,
  147. VK_FORMAT_ASTC_8x8_SRGB_BLOCK,
  148. VK_FORMAT_ASTC_8x8_UNORM_BLOCK,
  149. VK_FORMAT_B10G11R11_UFLOAT_PACK32,
  150. VK_FORMAT_B4G4R4A4_UNORM_PACK16,
  151. VK_FORMAT_B5G5R5A1_UNORM_PACK16,
  152. VK_FORMAT_B5G6R5_UNORM_PACK16,
  153. VK_FORMAT_B8G8R8A8_SRGB,
  154. VK_FORMAT_B8G8R8A8_UNORM,
  155. VK_FORMAT_BC1_RGBA_SRGB_BLOCK,
  156. VK_FORMAT_BC1_RGBA_UNORM_BLOCK,
  157. VK_FORMAT_BC2_SRGB_BLOCK,
  158. VK_FORMAT_BC2_UNORM_BLOCK,
  159. VK_FORMAT_BC3_SRGB_BLOCK,
  160. VK_FORMAT_BC3_UNORM_BLOCK,
  161. VK_FORMAT_BC4_SNORM_BLOCK,
  162. VK_FORMAT_BC4_UNORM_BLOCK,
  163. VK_FORMAT_BC5_SNORM_BLOCK,
  164. VK_FORMAT_BC5_UNORM_BLOCK,
  165. VK_FORMAT_BC6H_SFLOAT_BLOCK,
  166. VK_FORMAT_BC6H_UFLOAT_BLOCK,
  167. VK_FORMAT_BC7_SRGB_BLOCK,
  168. VK_FORMAT_BC7_UNORM_BLOCK,
  169. VK_FORMAT_D16_UNORM,
  170. VK_FORMAT_D16_UNORM_S8_UINT,
  171. VK_FORMAT_D24_UNORM_S8_UINT,
  172. VK_FORMAT_D32_SFLOAT,
  173. VK_FORMAT_D32_SFLOAT_S8_UINT,
  174. VK_FORMAT_E5B9G9R9_UFLOAT_PACK32,
  175. VK_FORMAT_R16G16B16A16_SFLOAT,
  176. VK_FORMAT_R16G16B16A16_SINT,
  177. VK_FORMAT_R16G16B16A16_SNORM,
  178. VK_FORMAT_R16G16B16A16_SSCALED,
  179. VK_FORMAT_R16G16B16A16_UINT,
  180. VK_FORMAT_R16G16B16A16_UNORM,
  181. VK_FORMAT_R16G16B16A16_USCALED,
  182. VK_FORMAT_R16G16B16_SFLOAT,
  183. VK_FORMAT_R16G16B16_SINT,
  184. VK_FORMAT_R16G16B16_SNORM,
  185. VK_FORMAT_R16G16B16_SSCALED,
  186. VK_FORMAT_R16G16B16_UINT,
  187. VK_FORMAT_R16G16B16_UNORM,
  188. VK_FORMAT_R16G16B16_USCALED,
  189. VK_FORMAT_R16G16_SFLOAT,
  190. VK_FORMAT_R16G16_SINT,
  191. VK_FORMAT_R16G16_SNORM,
  192. VK_FORMAT_R16G16_SSCALED,
  193. VK_FORMAT_R16G16_UINT,
  194. VK_FORMAT_R16G16_UNORM,
  195. VK_FORMAT_R16G16_USCALED,
  196. VK_FORMAT_R16_SFLOAT,
  197. VK_FORMAT_R16_SINT,
  198. VK_FORMAT_R16_SNORM,
  199. VK_FORMAT_R16_SSCALED,
  200. VK_FORMAT_R16_UINT,
  201. VK_FORMAT_R16_UNORM,
  202. VK_FORMAT_R16_USCALED,
  203. VK_FORMAT_R32G32B32A32_SFLOAT,
  204. VK_FORMAT_R32G32B32A32_SINT,
  205. VK_FORMAT_R32G32B32A32_UINT,
  206. VK_FORMAT_R32G32B32_SFLOAT,
  207. VK_FORMAT_R32G32B32_SINT,
  208. VK_FORMAT_R32G32B32_UINT,
  209. VK_FORMAT_R32G32_SFLOAT,
  210. VK_FORMAT_R32G32_SINT,
  211. VK_FORMAT_R32G32_UINT,
  212. VK_FORMAT_R32_SFLOAT,
  213. VK_FORMAT_R32_SINT,
  214. VK_FORMAT_R32_UINT,
  215. VK_FORMAT_R4G4B4A4_UNORM_PACK16,
  216. VK_FORMAT_R4G4_UNORM_PACK8,
  217. VK_FORMAT_R5G5B5A1_UNORM_PACK16,
  218. VK_FORMAT_R5G6B5_UNORM_PACK16,
  219. VK_FORMAT_R8G8B8A8_SINT,
  220. VK_FORMAT_R8G8B8A8_SNORM,
  221. VK_FORMAT_R8G8B8A8_SRGB,
  222. VK_FORMAT_R8G8B8A8_SSCALED,
  223. VK_FORMAT_R8G8B8A8_UINT,
  224. VK_FORMAT_R8G8B8A8_UNORM,
  225. VK_FORMAT_R8G8B8A8_USCALED,
  226. VK_FORMAT_R8G8B8_SINT,
  227. VK_FORMAT_R8G8B8_SNORM,
  228. VK_FORMAT_R8G8B8_SSCALED,
  229. VK_FORMAT_R8G8B8_UINT,
  230. VK_FORMAT_R8G8B8_UNORM,
  231. VK_FORMAT_R8G8B8_USCALED,
  232. VK_FORMAT_R8G8_SINT,
  233. VK_FORMAT_R8G8_SNORM,
  234. VK_FORMAT_R8G8_SSCALED,
  235. VK_FORMAT_R8G8_UINT,
  236. VK_FORMAT_R8G8_UNORM,
  237. VK_FORMAT_R8G8_USCALED,
  238. VK_FORMAT_R8_SINT,
  239. VK_FORMAT_R8_SNORM,
  240. VK_FORMAT_R8_SSCALED,
  241. VK_FORMAT_R8_UINT,
  242. VK_FORMAT_R8_UNORM,
  243. VK_FORMAT_R8_USCALED,
  244. VK_FORMAT_S8_UINT,
  245. };
  246. std::unordered_map<VkFormat, VkFormatProperties> format_properties;
  247. for (const auto format : formats) {
  248. format_properties.emplace(format, physical.GetFormatProperties(format));
  249. }
  250. return format_properties;
  251. }
  252. #if defined(ANDROID) && defined(ARCHITECTURE_arm64)
  253. void OverrideBcnFormats(std::unordered_map<VkFormat, VkFormatProperties>& format_properties) {
  254. // These properties are extracted from Adreno driver 512.687.0
  255. constexpr VkFormatFeatureFlags tiling_features{
  256. VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_BLIT_SRC_BIT |
  257. VK_FORMAT_FEATURE_SAMPLED_IMAGE_FILTER_LINEAR_BIT | VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
  258. VK_FORMAT_FEATURE_TRANSFER_DST_BIT};
  259. constexpr VkFormatFeatureFlags buffer_features{VK_FORMAT_FEATURE_UNIFORM_TEXEL_BUFFER_BIT};
  260. static constexpr std::array bcn_formats{
  261. VK_FORMAT_BC1_RGBA_SRGB_BLOCK, VK_FORMAT_BC1_RGBA_UNORM_BLOCK, VK_FORMAT_BC2_SRGB_BLOCK,
  262. VK_FORMAT_BC2_UNORM_BLOCK, VK_FORMAT_BC3_SRGB_BLOCK, VK_FORMAT_BC3_UNORM_BLOCK,
  263. VK_FORMAT_BC4_SNORM_BLOCK, VK_FORMAT_BC4_UNORM_BLOCK, VK_FORMAT_BC5_SNORM_BLOCK,
  264. VK_FORMAT_BC5_UNORM_BLOCK, VK_FORMAT_BC6H_SFLOAT_BLOCK, VK_FORMAT_BC6H_UFLOAT_BLOCK,
  265. VK_FORMAT_BC7_SRGB_BLOCK, VK_FORMAT_BC7_UNORM_BLOCK,
  266. };
  267. for (const auto format : bcn_formats) {
  268. format_properties[format].linearTilingFeatures = tiling_features;
  269. format_properties[format].optimalTilingFeatures = tiling_features;
  270. format_properties[format].bufferFeatures = buffer_features;
  271. }
  272. }
  273. #endif
  274. NvidiaArchitecture GetNvidiaArchitecture(vk::PhysicalDevice physical,
  275. const std::set<std::string, std::less<>>& exts) {
  276. if (exts.contains(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME)) {
  277. VkPhysicalDeviceFragmentShadingRatePropertiesKHR shading_rate_props{};
  278. shading_rate_props.sType =
  279. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADING_RATE_PROPERTIES_KHR;
  280. VkPhysicalDeviceProperties2 physical_properties{};
  281. physical_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
  282. physical_properties.pNext = &shading_rate_props;
  283. physical.GetProperties2(physical_properties);
  284. if (shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports) {
  285. // Only Ampere and newer support this feature
  286. return NvidiaArchitecture::AmpereOrNewer;
  287. }
  288. }
  289. if (exts.contains(VK_NV_SHADING_RATE_IMAGE_EXTENSION_NAME)) {
  290. return NvidiaArchitecture::Turing;
  291. }
  292. return NvidiaArchitecture::VoltaOrOlder;
  293. }
  294. std::vector<const char*> ExtensionListForVulkan(
  295. const std::set<std::string, std::less<>>& extensions) {
  296. std::vector<const char*> output;
  297. output.reserve(extensions.size());
  298. for (const auto& extension : extensions) {
  299. output.push_back(extension.c_str());
  300. }
  301. return output;
  302. }
  303. } // Anonymous namespace
  304. Device::Device(VkInstance instance_, vk::PhysicalDevice physical_, VkSurfaceKHR surface,
  305. const vk::InstanceDispatch& dld_)
  306. : instance{instance_}, dld{dld_}, physical{physical_},
  307. format_properties(GetFormatProperties(physical)) {
  308. // Get suitability and device properties.
  309. const bool is_suitable = GetSuitability(surface != nullptr);
  310. const VkDriverId driver_id = properties.driver.driverID;
  311. const auto device_id = properties.properties.deviceID;
  312. const bool is_radv = driver_id == VK_DRIVER_ID_MESA_RADV;
  313. const bool is_amd_driver =
  314. driver_id == VK_DRIVER_ID_AMD_PROPRIETARY || driver_id == VK_DRIVER_ID_AMD_OPEN_SOURCE;
  315. const bool is_amd = is_amd_driver || is_radv;
  316. const bool is_intel_windows = driver_id == VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS;
  317. const bool is_intel_anv = driver_id == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA;
  318. const bool is_nvidia = driver_id == VK_DRIVER_ID_NVIDIA_PROPRIETARY;
  319. const bool is_mvk = driver_id == VK_DRIVER_ID_MOLTENVK;
  320. const bool is_qualcomm = driver_id == VK_DRIVER_ID_QUALCOMM_PROPRIETARY;
  321. const bool is_turnip = driver_id == VK_DRIVER_ID_MESA_TURNIP;
  322. const bool is_s8gen2 = device_id == 0x43050a01;
  323. const bool is_arm = driver_id == VK_DRIVER_ID_ARM_PROPRIETARY;
  324. if ((is_mvk || is_qualcomm || is_turnip || is_arm) && !is_suitable) {
  325. LOG_WARNING(Render_Vulkan, "Unsuitable driver, continuing anyway");
  326. } else if (!is_suitable) {
  327. throw vk::Exception(VK_ERROR_INCOMPATIBLE_DRIVER);
  328. }
  329. SetupFamilies(surface);
  330. const auto queue_cis = GetDeviceQueueCreateInfos();
  331. // GetSuitability has already configured the linked list of features for us.
  332. // Reuse it here.
  333. const void* first_next = &features2;
  334. VkDeviceDiagnosticsConfigCreateInfoNV diagnostics_nv{};
  335. if (Settings::values.enable_nsight_aftermath && extensions.device_diagnostics_config) {
  336. nsight_aftermath_tracker = std::make_unique<NsightAftermathTracker>();
  337. diagnostics_nv = {
  338. .sType = VK_STRUCTURE_TYPE_DEVICE_DIAGNOSTICS_CONFIG_CREATE_INFO_NV,
  339. .pNext = &features2,
  340. .flags = VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_SHADER_DEBUG_INFO_BIT_NV |
  341. VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_RESOURCE_TRACKING_BIT_NV |
  342. VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_AUTOMATIC_CHECKPOINTS_BIT_NV,
  343. };
  344. first_next = &diagnostics_nv;
  345. }
  346. is_blit_depth_stencil_supported = TestDepthStencilBlits();
  347. is_optimal_astc_supported = ComputeIsOptimalAstcSupported();
  348. is_warp_potentially_bigger = !extensions.subgroup_size_control ||
  349. properties.subgroup_size_control.maxSubgroupSize > GuestWarpSize;
  350. is_integrated = properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;
  351. is_virtual = properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU;
  352. is_non_gpu = properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_OTHER ||
  353. properties.properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU;
  354. supports_d24_depth =
  355. IsFormatSupported(VK_FORMAT_D24_UNORM_S8_UINT,
  356. VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT, FormatType::Optimal);
  357. supports_conditional_barriers = !(is_intel_anv || is_intel_windows);
  358. CollectPhysicalMemoryInfo();
  359. CollectToolingInfo();
  360. if (is_qualcomm || is_turnip) {
  361. LOG_WARNING(Render_Vulkan,
  362. "Qualcomm and Turnip drivers have broken VK_EXT_custom_border_color");
  363. extensions.custom_border_color = false;
  364. loaded_extensions.erase(VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME);
  365. }
  366. if (is_qualcomm) {
  367. must_emulate_scaled_formats = true;
  368. LOG_WARNING(Render_Vulkan, "Qualcomm drivers have broken VK_EXT_extended_dynamic_state");
  369. extensions.extended_dynamic_state = false;
  370. loaded_extensions.erase(VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
  371. LOG_WARNING(Render_Vulkan,
  372. "Qualcomm drivers have a slow VK_KHR_push_descriptor implementation");
  373. extensions.push_descriptor = false;
  374. loaded_extensions.erase(VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
  375. #if defined(ANDROID) && defined(ARCHITECTURE_arm64)
  376. // Patch the driver to enable BCn textures.
  377. const auto major = (properties.properties.driverVersion >> 24) << 2;
  378. const auto minor = (properties.properties.driverVersion >> 12) & 0xFFFU;
  379. const auto vendor = properties.properties.vendorID;
  380. const auto patch_status = adrenotools_get_bcn_type(major, minor, vendor);
  381. if (patch_status == ADRENOTOOLS_BCN_PATCH) {
  382. LOG_INFO(Render_Vulkan, "Patching Adreno driver to support BCn texture formats");
  383. if (adrenotools_patch_bcn(
  384. reinterpret_cast<void*>(dld.vkGetPhysicalDeviceFormatProperties))) {
  385. OverrideBcnFormats(format_properties);
  386. } else {
  387. LOG_ERROR(Render_Vulkan, "Patch failed! Driver code may now crash");
  388. }
  389. } else if (patch_status == ADRENOTOOLS_BCN_BLOB) {
  390. LOG_INFO(Render_Vulkan, "Adreno driver supports BCn textures without patches");
  391. } else {
  392. LOG_WARNING(Render_Vulkan, "Adreno driver can't be patched to enable BCn textures");
  393. }
  394. #endif
  395. }
  396. if (is_arm) {
  397. must_emulate_scaled_formats = true;
  398. LOG_WARNING(Render_Vulkan, "ARM drivers have broken VK_EXT_extended_dynamic_state");
  399. extensions.extended_dynamic_state = false;
  400. loaded_extensions.erase(VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
  401. LOG_WARNING(Render_Vulkan, "ARM drivers have broken VK_EXT_extended_dynamic_state2");
  402. features.extended_dynamic_state2.extendedDynamicState2 = false;
  403. features.extended_dynamic_state2.extendedDynamicState2LogicOp = false;
  404. features.extended_dynamic_state2.extendedDynamicState2PatchControlPoints = false;
  405. extensions.extended_dynamic_state2 = false;
  406. loaded_extensions.erase(VK_EXT_EXTENDED_DYNAMIC_STATE_2_EXTENSION_NAME);
  407. }
  408. if (is_nvidia) {
  409. const u32 nv_major_version = (properties.properties.driverVersion >> 22) & 0x3ff;
  410. const auto arch = GetNvidiaArchitecture(physical, supported_extensions);
  411. switch (arch) {
  412. case NvidiaArchitecture::AmpereOrNewer:
  413. LOG_WARNING(Render_Vulkan, "Ampere and newer have broken float16 math");
  414. features.shader_float16_int8.shaderFloat16 = false;
  415. break;
  416. case NvidiaArchitecture::Turing:
  417. break;
  418. case NvidiaArchitecture::VoltaOrOlder:
  419. if (nv_major_version < 527) {
  420. LOG_WARNING(Render_Vulkan, "Volta and older have broken VK_KHR_push_descriptor");
  421. extensions.push_descriptor = false;
  422. loaded_extensions.erase(VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
  423. }
  424. break;
  425. }
  426. if (nv_major_version >= 510) {
  427. LOG_WARNING(Render_Vulkan, "NVIDIA Drivers >= 510 do not support MSAA image blits");
  428. cant_blit_msaa = true;
  429. }
  430. }
  431. if (extensions.extended_dynamic_state && is_radv) {
  432. // Mask driver version variant
  433. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  434. if (version < VK_MAKE_API_VERSION(0, 21, 2, 0)) {
  435. LOG_WARNING(Render_Vulkan,
  436. "RADV versions older than 21.2 have broken VK_EXT_extended_dynamic_state");
  437. extensions.extended_dynamic_state = false;
  438. loaded_extensions.erase(VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
  439. }
  440. }
  441. if (extensions.extended_dynamic_state2 && is_radv) {
  442. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  443. if (version < VK_MAKE_API_VERSION(0, 22, 3, 1)) {
  444. LOG_WARNING(
  445. Render_Vulkan,
  446. "RADV versions older than 22.3.1 have broken VK_EXT_extended_dynamic_state2");
  447. features.extended_dynamic_state2.extendedDynamicState2 = false;
  448. features.extended_dynamic_state2.extendedDynamicState2LogicOp = false;
  449. features.extended_dynamic_state2.extendedDynamicState2PatchControlPoints = false;
  450. extensions.extended_dynamic_state2 = false;
  451. loaded_extensions.erase(VK_EXT_EXTENDED_DYNAMIC_STATE_2_EXTENSION_NAME);
  452. }
  453. }
  454. if (extensions.extended_dynamic_state2 && is_qualcomm) {
  455. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  456. if (version >= VK_MAKE_API_VERSION(0, 0, 676, 0) &&
  457. version < VK_MAKE_API_VERSION(0, 0, 680, 0)) {
  458. // Qualcomm Adreno 7xx drivers do not properly support extended_dynamic_state2.
  459. LOG_WARNING(Render_Vulkan,
  460. "Qualcomm Adreno 7xx drivers have broken VK_EXT_extended_dynamic_state2");
  461. features.extended_dynamic_state2.extendedDynamicState2 = false;
  462. features.extended_dynamic_state2.extendedDynamicState2LogicOp = false;
  463. features.extended_dynamic_state2.extendedDynamicState2PatchControlPoints = false;
  464. extensions.extended_dynamic_state2 = false;
  465. loaded_extensions.erase(VK_EXT_EXTENDED_DYNAMIC_STATE_2_EXTENSION_NAME);
  466. }
  467. }
  468. if (extensions.extended_dynamic_state3 && is_radv) {
  469. LOG_WARNING(Render_Vulkan, "RADV has broken extendedDynamicState3ColorBlendEquation");
  470. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEnable = false;
  471. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEquation = false;
  472. dynamic_state3_blending = false;
  473. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  474. if (version < VK_MAKE_API_VERSION(0, 23, 1, 0)) {
  475. LOG_WARNING(Render_Vulkan,
  476. "RADV versions older than 23.1.0 have broken depth clamp dynamic state");
  477. features.extended_dynamic_state3.extendedDynamicState3DepthClampEnable = false;
  478. dynamic_state3_enables = false;
  479. }
  480. }
  481. if (extensions.extended_dynamic_state3 && is_amd_driver) {
  482. LOG_WARNING(Render_Vulkan,
  483. "AMD drivers have broken extendedDynamicState3ColorBlendEquation");
  484. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEnable = false;
  485. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEquation = false;
  486. dynamic_state3_blending = false;
  487. }
  488. if (extensions.vertex_input_dynamic_state && is_radv) {
  489. // TODO(ameerj): Blacklist only offending driver versions
  490. // TODO(ameerj): Confirm if RDNA1 is affected
  491. const bool is_rdna2 =
  492. supported_extensions.contains(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME);
  493. if (is_rdna2) {
  494. LOG_WARNING(Render_Vulkan,
  495. "RADV has broken VK_EXT_vertex_input_dynamic_state on RDNA2 hardware");
  496. features.vertex_input_dynamic_state.vertexInputDynamicState = false;
  497. extensions.vertex_input_dynamic_state = false;
  498. loaded_extensions.erase(VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
  499. }
  500. }
  501. if (extensions.vertex_input_dynamic_state && is_qualcomm) {
  502. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  503. if (version >= VK_MAKE_API_VERSION(0, 0, 676, 0) &&
  504. version < VK_MAKE_API_VERSION(0, 0, 680, 0)) {
  505. // Qualcomm Adreno 7xx drivers do not properly support vertex_input_dynamic_state.
  506. LOG_WARNING(
  507. Render_Vulkan,
  508. "Qualcomm Adreno 7xx drivers have broken VK_EXT_vertex_input_dynamic_state");
  509. features.vertex_input_dynamic_state.vertexInputDynamicState = false;
  510. extensions.vertex_input_dynamic_state = false;
  511. loaded_extensions.erase(VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
  512. }
  513. }
  514. sets_per_pool = 64;
  515. if (is_amd_driver) {
  516. // AMD drivers need a higher amount of Sets per Pool in certain circumstances like in XC2.
  517. sets_per_pool = 96;
  518. // Disable VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT on AMD GCN4 and lower as it is broken.
  519. if (!features.shader_float16_int8.shaderFloat16) {
  520. LOG_WARNING(Render_Vulkan,
  521. "AMD GCN4 and earlier have broken VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT");
  522. has_broken_cube_compatibility = true;
  523. }
  524. }
  525. if (extensions.sampler_filter_minmax && is_amd) {
  526. // Disable ext_sampler_filter_minmax on AMD GCN4 and lower as it is broken.
  527. if (!features.shader_float16_int8.shaderFloat16) {
  528. LOG_WARNING(Render_Vulkan,
  529. "AMD GCN4 and earlier have broken VK_EXT_sampler_filter_minmax");
  530. extensions.sampler_filter_minmax = false;
  531. loaded_extensions.erase(VK_EXT_SAMPLER_FILTER_MINMAX_EXTENSION_NAME);
  532. }
  533. }
  534. if (extensions.vertex_input_dynamic_state && is_intel_windows) {
  535. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  536. if (version < VK_MAKE_API_VERSION(27, 20, 100, 0)) {
  537. LOG_WARNING(Render_Vulkan, "Intel has broken VK_EXT_vertex_input_dynamic_state");
  538. extensions.vertex_input_dynamic_state = false;
  539. loaded_extensions.erase(VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
  540. }
  541. }
  542. if (features.shader_float16_int8.shaderFloat16 && is_intel_windows) {
  543. // Intel's compiler crashes when using fp16 on Astral Chain, disable it for the time being.
  544. LOG_WARNING(Render_Vulkan, "Intel has broken float16 math");
  545. features.shader_float16_int8.shaderFloat16 = false;
  546. }
  547. if (is_intel_windows) {
  548. LOG_WARNING(Render_Vulkan, "Intel proprietary drivers do not support MSAA image blits");
  549. cant_blit_msaa = true;
  550. }
  551. has_broken_compute =
  552. CheckBrokenCompute(properties.driver.driverID, properties.properties.driverVersion) &&
  553. !Settings::values.enable_compute_pipelines.GetValue();
  554. if (is_intel_anv || (is_qualcomm && !is_s8gen2)) {
  555. LOG_WARNING(Render_Vulkan, "Driver does not support native BGR format");
  556. must_emulate_bgr565 = true;
  557. }
  558. if (extensions.push_descriptor && is_intel_anv) {
  559. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  560. if (version >= VK_MAKE_API_VERSION(0, 22, 3, 0) &&
  561. version < VK_MAKE_API_VERSION(0, 23, 2, 0)) {
  562. // Disable VK_KHR_push_descriptor due to
  563. // mesa/mesa/-/commit/ff91c5ca42bc80aa411cb3fd8f550aa6fdd16bdc
  564. LOG_WARNING(Render_Vulkan,
  565. "ANV drivers 22.3.0 to 23.1.0 have broken VK_KHR_push_descriptor");
  566. extensions.push_descriptor = false;
  567. loaded_extensions.erase(VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
  568. }
  569. }
  570. if (is_mvk) {
  571. LOG_WARNING(Render_Vulkan,
  572. "MVK driver breaks when using more than 16 vertex attributes/bindings");
  573. properties.properties.limits.maxVertexInputAttributes =
  574. std::min(properties.properties.limits.maxVertexInputAttributes, 16U);
  575. properties.properties.limits.maxVertexInputBindings =
  576. std::min(properties.properties.limits.maxVertexInputBindings, 16U);
  577. }
  578. logical = vk::Device::Create(physical, queue_cis, ExtensionListForVulkan(loaded_extensions),
  579. first_next, dld);
  580. graphics_queue = logical.GetQueue(graphics_family);
  581. present_queue = logical.GetQueue(present_family);
  582. VmaVulkanFunctions functions{};
  583. functions.vkGetInstanceProcAddr = dld.vkGetInstanceProcAddr;
  584. functions.vkGetDeviceProcAddr = dld.vkGetDeviceProcAddr;
  585. const VmaAllocatorCreateInfo allocator_info = {
  586. .flags = VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT,
  587. .physicalDevice = physical,
  588. .device = *logical,
  589. .preferredLargeHeapBlockSize = 0,
  590. .pAllocationCallbacks = nullptr,
  591. .pDeviceMemoryCallbacks = nullptr,
  592. .pHeapSizeLimit = nullptr,
  593. .pVulkanFunctions = &functions,
  594. .instance = instance,
  595. .vulkanApiVersion = VK_API_VERSION_1_1,
  596. .pTypeExternalMemoryHandleTypes = nullptr,
  597. };
  598. vk::Check(vmaCreateAllocator(&allocator_info, &allocator));
  599. }
  600. Device::~Device() {
  601. vmaDestroyAllocator(allocator);
  602. }
  603. VkFormat Device::GetSupportedFormat(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  604. FormatType format_type) const {
  605. if (IsFormatSupported(wanted_format, wanted_usage, format_type)) {
  606. return wanted_format;
  607. }
  608. // The wanted format is not supported by hardware, search for alternatives
  609. const VkFormat* alternatives = GetFormatAlternatives(wanted_format);
  610. if (alternatives == nullptr) {
  611. ASSERT_MSG(false,
  612. "Format={} with usage={} and type={} has no defined alternatives and host "
  613. "hardware does not support it",
  614. wanted_format, wanted_usage, format_type);
  615. return wanted_format;
  616. }
  617. std::size_t i = 0;
  618. for (VkFormat alternative = *alternatives; alternative; alternative = alternatives[++i]) {
  619. if (!IsFormatSupported(alternative, wanted_usage, format_type)) {
  620. continue;
  621. }
  622. LOG_DEBUG(Render_Vulkan,
  623. "Emulating format={} with alternative format={} with usage={} and type={}",
  624. wanted_format, alternative, wanted_usage, format_type);
  625. return alternative;
  626. }
  627. // No alternatives found, panic
  628. ASSERT_MSG(false,
  629. "Format={} with usage={} and type={} is not supported by the host hardware and "
  630. "doesn't support any of the alternatives",
  631. wanted_format, wanted_usage, format_type);
  632. return wanted_format;
  633. }
  634. void Device::ReportLoss() const {
  635. LOG_CRITICAL(Render_Vulkan, "Device loss occurred!");
  636. // Wait for the log to flush and for Nsight Aftermath to dump the results
  637. std::this_thread::sleep_for(std::chrono::seconds{15});
  638. }
  639. void Device::SaveShader(std::span<const u32> spirv) const {
  640. if (nsight_aftermath_tracker) {
  641. nsight_aftermath_tracker->SaveShader(spirv);
  642. }
  643. }
  644. bool Device::ComputeIsOptimalAstcSupported() const {
  645. // Disable for now to avoid converting ASTC twice.
  646. static constexpr std::array astc_formats = {
  647. VK_FORMAT_ASTC_4x4_UNORM_BLOCK, VK_FORMAT_ASTC_4x4_SRGB_BLOCK,
  648. VK_FORMAT_ASTC_5x4_UNORM_BLOCK, VK_FORMAT_ASTC_5x4_SRGB_BLOCK,
  649. VK_FORMAT_ASTC_5x5_UNORM_BLOCK, VK_FORMAT_ASTC_5x5_SRGB_BLOCK,
  650. VK_FORMAT_ASTC_6x5_UNORM_BLOCK, VK_FORMAT_ASTC_6x5_SRGB_BLOCK,
  651. VK_FORMAT_ASTC_6x6_UNORM_BLOCK, VK_FORMAT_ASTC_6x6_SRGB_BLOCK,
  652. VK_FORMAT_ASTC_8x5_UNORM_BLOCK, VK_FORMAT_ASTC_8x5_SRGB_BLOCK,
  653. VK_FORMAT_ASTC_8x6_UNORM_BLOCK, VK_FORMAT_ASTC_8x6_SRGB_BLOCK,
  654. VK_FORMAT_ASTC_8x8_UNORM_BLOCK, VK_FORMAT_ASTC_8x8_SRGB_BLOCK,
  655. VK_FORMAT_ASTC_10x5_UNORM_BLOCK, VK_FORMAT_ASTC_10x5_SRGB_BLOCK,
  656. VK_FORMAT_ASTC_10x6_UNORM_BLOCK, VK_FORMAT_ASTC_10x6_SRGB_BLOCK,
  657. VK_FORMAT_ASTC_10x8_UNORM_BLOCK, VK_FORMAT_ASTC_10x8_SRGB_BLOCK,
  658. VK_FORMAT_ASTC_10x10_UNORM_BLOCK, VK_FORMAT_ASTC_10x10_SRGB_BLOCK,
  659. VK_FORMAT_ASTC_12x10_UNORM_BLOCK, VK_FORMAT_ASTC_12x10_SRGB_BLOCK,
  660. VK_FORMAT_ASTC_12x12_UNORM_BLOCK, VK_FORMAT_ASTC_12x12_SRGB_BLOCK,
  661. };
  662. if (!features.features.textureCompressionASTC_LDR) {
  663. return false;
  664. }
  665. const auto format_feature_usage{
  666. VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_BLIT_SRC_BIT |
  667. VK_FORMAT_FEATURE_BLIT_DST_BIT | VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
  668. VK_FORMAT_FEATURE_TRANSFER_DST_BIT};
  669. for (const auto format : astc_formats) {
  670. const auto physical_format_properties{physical.GetFormatProperties(format)};
  671. if ((physical_format_properties.optimalTilingFeatures & format_feature_usage) == 0) {
  672. return false;
  673. }
  674. }
  675. return true;
  676. }
  677. bool Device::TestDepthStencilBlits() const {
  678. static constexpr VkFormatFeatureFlags required_features =
  679. VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
  680. const auto test_features = [](VkFormatProperties props) {
  681. return (props.optimalTilingFeatures & required_features) == required_features;
  682. };
  683. return test_features(format_properties.at(VK_FORMAT_D32_SFLOAT_S8_UINT)) &&
  684. test_features(format_properties.at(VK_FORMAT_D24_UNORM_S8_UINT));
  685. }
  686. bool Device::IsFormatSupported(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  687. FormatType format_type) const {
  688. const auto it = format_properties.find(wanted_format);
  689. if (it == format_properties.end()) {
  690. UNIMPLEMENTED_MSG("Unimplemented format query={}", wanted_format);
  691. return true;
  692. }
  693. const auto supported_usage = GetFormatFeatures(it->second, format_type);
  694. return (supported_usage & wanted_usage) == wanted_usage;
  695. }
  696. std::string Device::GetDriverName() const {
  697. switch (properties.driver.driverID) {
  698. case VK_DRIVER_ID_AMD_PROPRIETARY:
  699. return "AMD";
  700. case VK_DRIVER_ID_AMD_OPEN_SOURCE:
  701. return "AMDVLK";
  702. case VK_DRIVER_ID_MESA_RADV:
  703. return "RADV";
  704. case VK_DRIVER_ID_NVIDIA_PROPRIETARY:
  705. return "NVIDIA";
  706. case VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS:
  707. return "INTEL";
  708. case VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA:
  709. return "ANV";
  710. case VK_DRIVER_ID_MESA_LLVMPIPE:
  711. return "LAVAPIPE";
  712. default:
  713. return properties.driver.driverName;
  714. }
  715. }
  716. bool Device::ShouldBoostClocks() const {
  717. const auto driver_id = properties.driver.driverID;
  718. const auto vendor_id = properties.properties.vendorID;
  719. const auto device_id = properties.properties.deviceID;
  720. const bool validated_driver =
  721. driver_id == VK_DRIVER_ID_AMD_PROPRIETARY || driver_id == VK_DRIVER_ID_AMD_OPEN_SOURCE ||
  722. driver_id == VK_DRIVER_ID_MESA_RADV || driver_id == VK_DRIVER_ID_NVIDIA_PROPRIETARY ||
  723. driver_id == VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS ||
  724. driver_id == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA ||
  725. driver_id == VK_DRIVER_ID_QUALCOMM_PROPRIETARY || driver_id == VK_DRIVER_ID_MESA_TURNIP;
  726. const bool is_steam_deck = vendor_id == 0x1002 && device_id == 0x163F;
  727. const bool is_debugging = this->HasDebuggingToolAttached();
  728. return validated_driver && !is_steam_deck && !is_debugging;
  729. }
  730. bool Device::HasTimelineSemaphore() const {
  731. if (GetDriverID() == VK_DRIVER_ID_QUALCOMM_PROPRIETARY ||
  732. GetDriverID() == VK_DRIVER_ID_MESA_TURNIP) {
  733. // Timeline semaphores do not work properly on all Qualcomm drivers.
  734. // They generally work properly with Turnip drivers, but are problematic on some devices
  735. // (e.g. ZTE handsets with Snapdragon 870).
  736. return false;
  737. }
  738. return features.timeline_semaphore.timelineSemaphore;
  739. }
  740. bool Device::GetSuitability(bool requires_swapchain) {
  741. // Assume we will be suitable.
  742. bool suitable = true;
  743. // Configure properties.
  744. properties.properties = physical.GetProperties();
  745. // Set instance version.
  746. instance_version = properties.properties.apiVersion;
  747. // Minimum of API version 1.1 is required. (This is well-supported.)
  748. ASSERT(instance_version >= VK_API_VERSION_1_1);
  749. // Get available extensions.
  750. auto extension_properties = physical.EnumerateDeviceExtensionProperties();
  751. // Get the set of supported extensions.
  752. supported_extensions.clear();
  753. for (const VkExtensionProperties& property : extension_properties) {
  754. supported_extensions.insert(property.extensionName);
  755. }
  756. // Generate list of extensions to load.
  757. loaded_extensions.clear();
  758. #define EXTENSION(prefix, macro_name, var_name) \
  759. if (supported_extensions.contains(VK_##prefix##_##macro_name##_EXTENSION_NAME)) { \
  760. loaded_extensions.insert(VK_##prefix##_##macro_name##_EXTENSION_NAME); \
  761. extensions.var_name = true; \
  762. }
  763. #define FEATURE_EXTENSION(prefix, struct_name, macro_name, var_name) \
  764. if (supported_extensions.contains(VK_##prefix##_##macro_name##_EXTENSION_NAME)) { \
  765. loaded_extensions.insert(VK_##prefix##_##macro_name##_EXTENSION_NAME); \
  766. extensions.var_name = true; \
  767. }
  768. if (instance_version < VK_API_VERSION_1_2) {
  769. FOR_EACH_VK_FEATURE_1_2(FEATURE_EXTENSION);
  770. }
  771. if (instance_version < VK_API_VERSION_1_3) {
  772. FOR_EACH_VK_FEATURE_1_3(FEATURE_EXTENSION);
  773. }
  774. FOR_EACH_VK_FEATURE_EXT(FEATURE_EXTENSION);
  775. FOR_EACH_VK_EXTENSION(EXTENSION);
  776. #undef FEATURE_EXTENSION
  777. #undef EXTENSION
  778. // Some extensions are mandatory. Check those.
  779. #define CHECK_EXTENSION(extension_name) \
  780. if (!loaded_extensions.contains(extension_name)) { \
  781. LOG_ERROR(Render_Vulkan, "Missing required extension {}", extension_name); \
  782. suitable = false; \
  783. }
  784. #define LOG_EXTENSION(extension_name) \
  785. if (!loaded_extensions.contains(extension_name)) { \
  786. LOG_INFO(Render_Vulkan, "Device doesn't support extension {}", extension_name); \
  787. }
  788. FOR_EACH_VK_RECOMMENDED_EXTENSION(LOG_EXTENSION);
  789. FOR_EACH_VK_MANDATORY_EXTENSION(CHECK_EXTENSION);
  790. if (requires_swapchain) {
  791. CHECK_EXTENSION(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
  792. }
  793. #undef LOG_EXTENSION
  794. #undef CHECK_EXTENSION
  795. // Generate the linked list of features to test.
  796. features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  797. // Set next pointer.
  798. void** next = &features2.pNext;
  799. // Test all features we know about. If the feature is not available in core at our
  800. // current API version, and was not enabled by an extension, skip testing the feature.
  801. // We set the structure sType explicitly here as it is zeroed by the constructor.
  802. #define FEATURE(prefix, struct_name, macro_name, var_name) \
  803. features.var_name.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_##macro_name##_FEATURES; \
  804. SetNext(next, features.var_name);
  805. #define EXT_FEATURE(prefix, struct_name, macro_name, var_name) \
  806. if (extensions.var_name) { \
  807. features.var_name.sType = \
  808. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_##macro_name##_FEATURES_##prefix; \
  809. SetNext(next, features.var_name); \
  810. }
  811. FOR_EACH_VK_FEATURE_1_1(FEATURE);
  812. FOR_EACH_VK_FEATURE_EXT(EXT_FEATURE);
  813. if (instance_version >= VK_API_VERSION_1_2) {
  814. FOR_EACH_VK_FEATURE_1_2(FEATURE);
  815. } else {
  816. FOR_EACH_VK_FEATURE_1_2(EXT_FEATURE);
  817. }
  818. if (instance_version >= VK_API_VERSION_1_3) {
  819. FOR_EACH_VK_FEATURE_1_3(FEATURE);
  820. } else {
  821. FOR_EACH_VK_FEATURE_1_3(EXT_FEATURE);
  822. }
  823. #undef EXT_FEATURE
  824. #undef FEATURE
  825. // Perform the feature test.
  826. physical.GetFeatures2(features2);
  827. features.features = features2.features;
  828. // Some features are mandatory. Check those.
  829. #define CHECK_FEATURE(feature, name) \
  830. if (!features.feature.name) { \
  831. LOG_ERROR(Render_Vulkan, "Missing required feature {}", #name); \
  832. suitable = false; \
  833. }
  834. #define LOG_FEATURE(feature, name) \
  835. if (!features.feature.name) { \
  836. LOG_INFO(Render_Vulkan, "Device doesn't support feature {}", #name); \
  837. }
  838. FOR_EACH_VK_RECOMMENDED_FEATURE(LOG_FEATURE);
  839. FOR_EACH_VK_MANDATORY_FEATURE(CHECK_FEATURE);
  840. #undef LOG_FEATURE
  841. #undef CHECK_FEATURE
  842. // Generate linked list of properties.
  843. properties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
  844. // Set next pointer.
  845. next = &properties2.pNext;
  846. // Get driver info.
  847. properties.driver.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES;
  848. SetNext(next, properties.driver);
  849. // Retrieve subgroup properties.
  850. properties.subgroup_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES;
  851. SetNext(next, properties.subgroup_properties);
  852. // Retrieve relevant extension properties.
  853. if (extensions.shader_float_controls) {
  854. properties.float_controls.sType =
  855. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT_CONTROLS_PROPERTIES;
  856. SetNext(next, properties.float_controls);
  857. }
  858. if (extensions.push_descriptor) {
  859. properties.push_descriptor.sType =
  860. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR;
  861. SetNext(next, properties.push_descriptor);
  862. }
  863. if (extensions.subgroup_size_control) {
  864. properties.subgroup_size_control.sType =
  865. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_PROPERTIES;
  866. SetNext(next, properties.subgroup_size_control);
  867. }
  868. if (extensions.transform_feedback) {
  869. properties.transform_feedback.sType =
  870. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT;
  871. SetNext(next, properties.transform_feedback);
  872. }
  873. // Perform the property fetch.
  874. physical.GetProperties2(properties2);
  875. properties.properties = properties2.properties;
  876. // Unload extensions if feature support is insufficient.
  877. RemoveUnsuitableExtensions();
  878. // Check limits.
  879. struct Limit {
  880. u32 minimum;
  881. u32 value;
  882. const char* name;
  883. };
  884. const VkPhysicalDeviceLimits& limits{properties.properties.limits};
  885. const std::array limits_report{
  886. Limit{65536, limits.maxUniformBufferRange, "maxUniformBufferRange"},
  887. Limit{16, limits.maxViewports, "maxViewports"},
  888. Limit{8, limits.maxColorAttachments, "maxColorAttachments"},
  889. Limit{8, limits.maxClipDistances, "maxClipDistances"},
  890. };
  891. for (const auto& [min, value, name] : limits_report) {
  892. if (value < min) {
  893. LOG_ERROR(Render_Vulkan, "{} has to be {} or greater but it is {}", name, min, value);
  894. suitable = false;
  895. }
  896. }
  897. // Return whether we were suitable.
  898. return suitable;
  899. }
  900. void Device::RemoveExtensionIfUnsuitable(bool is_suitable, const std::string& extension_name) {
  901. if (loaded_extensions.contains(extension_name) && !is_suitable) {
  902. LOG_WARNING(Render_Vulkan, "Removing unsuitable extension {}", extension_name);
  903. loaded_extensions.erase(extension_name);
  904. }
  905. }
  906. void Device::RemoveUnsuitableExtensions() {
  907. // VK_EXT_custom_border_color
  908. extensions.custom_border_color = features.custom_border_color.customBorderColors &&
  909. features.custom_border_color.customBorderColorWithoutFormat;
  910. RemoveExtensionIfUnsuitable(extensions.custom_border_color,
  911. VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME);
  912. // VK_EXT_depth_clip_control
  913. extensions.depth_clip_control = features.depth_clip_control.depthClipControl;
  914. RemoveExtensionIfUnsuitable(extensions.depth_clip_control,
  915. VK_EXT_DEPTH_CLIP_CONTROL_EXTENSION_NAME);
  916. // VK_EXT_extended_dynamic_state
  917. extensions.extended_dynamic_state = features.extended_dynamic_state.extendedDynamicState;
  918. RemoveExtensionIfUnsuitable(extensions.extended_dynamic_state,
  919. VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
  920. // VK_EXT_extended_dynamic_state2
  921. extensions.extended_dynamic_state2 = features.extended_dynamic_state2.extendedDynamicState2;
  922. RemoveExtensionIfUnsuitable(extensions.extended_dynamic_state2,
  923. VK_EXT_EXTENDED_DYNAMIC_STATE_2_EXTENSION_NAME);
  924. // VK_EXT_extended_dynamic_state3
  925. dynamic_state3_blending =
  926. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEnable &&
  927. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEquation &&
  928. features.extended_dynamic_state3.extendedDynamicState3ColorWriteMask;
  929. dynamic_state3_enables =
  930. features.extended_dynamic_state3.extendedDynamicState3DepthClampEnable &&
  931. features.extended_dynamic_state3.extendedDynamicState3LogicOpEnable;
  932. extensions.extended_dynamic_state3 = dynamic_state3_blending || dynamic_state3_enables;
  933. dynamic_state3_blending = dynamic_state3_blending && extensions.extended_dynamic_state3;
  934. dynamic_state3_enables = dynamic_state3_enables && extensions.extended_dynamic_state3;
  935. RemoveExtensionIfUnsuitable(extensions.extended_dynamic_state3,
  936. VK_EXT_EXTENDED_DYNAMIC_STATE_3_EXTENSION_NAME);
  937. // VK_EXT_provoking_vertex
  938. extensions.provoking_vertex =
  939. features.provoking_vertex.provokingVertexLast &&
  940. features.provoking_vertex.transformFeedbackPreservesProvokingVertex;
  941. RemoveExtensionIfUnsuitable(extensions.provoking_vertex,
  942. VK_EXT_PROVOKING_VERTEX_EXTENSION_NAME);
  943. // VK_KHR_shader_atomic_int64
  944. extensions.shader_atomic_int64 = features.shader_atomic_int64.shaderBufferInt64Atomics &&
  945. features.shader_atomic_int64.shaderSharedInt64Atomics;
  946. RemoveExtensionIfUnsuitable(extensions.shader_atomic_int64,
  947. VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME);
  948. // VK_EXT_shader_demote_to_helper_invocation
  949. extensions.shader_demote_to_helper_invocation =
  950. features.shader_demote_to_helper_invocation.shaderDemoteToHelperInvocation;
  951. RemoveExtensionIfUnsuitable(extensions.shader_demote_to_helper_invocation,
  952. VK_EXT_SHADER_DEMOTE_TO_HELPER_INVOCATION_EXTENSION_NAME);
  953. // VK_EXT_subgroup_size_control
  954. extensions.subgroup_size_control =
  955. features.subgroup_size_control.subgroupSizeControl &&
  956. properties.subgroup_size_control.minSubgroupSize <= GuestWarpSize &&
  957. properties.subgroup_size_control.maxSubgroupSize >= GuestWarpSize;
  958. RemoveExtensionIfUnsuitable(extensions.subgroup_size_control,
  959. VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME);
  960. // VK_EXT_transform_feedback
  961. extensions.transform_feedback =
  962. features.transform_feedback.transformFeedback &&
  963. features.transform_feedback.geometryStreams &&
  964. properties.transform_feedback.maxTransformFeedbackStreams >= 4 &&
  965. properties.transform_feedback.maxTransformFeedbackBuffers > 0 &&
  966. properties.transform_feedback.transformFeedbackQueries &&
  967. properties.transform_feedback.transformFeedbackDraw;
  968. RemoveExtensionIfUnsuitable(extensions.transform_feedback,
  969. VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME);
  970. // VK_EXT_vertex_input_dynamic_state
  971. extensions.vertex_input_dynamic_state =
  972. features.vertex_input_dynamic_state.vertexInputDynamicState;
  973. RemoveExtensionIfUnsuitable(extensions.vertex_input_dynamic_state,
  974. VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
  975. // VK_KHR_pipeline_executable_properties
  976. if (Settings::values.renderer_shader_feedback.GetValue()) {
  977. extensions.pipeline_executable_properties =
  978. features.pipeline_executable_properties.pipelineExecutableInfo;
  979. RemoveExtensionIfUnsuitable(extensions.pipeline_executable_properties,
  980. VK_KHR_PIPELINE_EXECUTABLE_PROPERTIES_EXTENSION_NAME);
  981. } else {
  982. extensions.pipeline_executable_properties = false;
  983. loaded_extensions.erase(VK_KHR_PIPELINE_EXECUTABLE_PROPERTIES_EXTENSION_NAME);
  984. }
  985. // VK_KHR_workgroup_memory_explicit_layout
  986. extensions.workgroup_memory_explicit_layout =
  987. features.features.shaderInt16 &&
  988. features.workgroup_memory_explicit_layout.workgroupMemoryExplicitLayout &&
  989. features.workgroup_memory_explicit_layout.workgroupMemoryExplicitLayout8BitAccess &&
  990. features.workgroup_memory_explicit_layout.workgroupMemoryExplicitLayout16BitAccess &&
  991. features.workgroup_memory_explicit_layout.workgroupMemoryExplicitLayoutScalarBlockLayout;
  992. RemoveExtensionIfUnsuitable(extensions.workgroup_memory_explicit_layout,
  993. VK_KHR_WORKGROUP_MEMORY_EXPLICIT_LAYOUT_EXTENSION_NAME);
  994. }
  995. void Device::SetupFamilies(VkSurfaceKHR surface) {
  996. const std::vector queue_family_properties = physical.GetQueueFamilyProperties();
  997. std::optional<u32> graphics;
  998. std::optional<u32> present;
  999. for (u32 index = 0; index < static_cast<u32>(queue_family_properties.size()); ++index) {
  1000. if (graphics && (present || !surface)) {
  1001. break;
  1002. }
  1003. const VkQueueFamilyProperties& queue_family = queue_family_properties[index];
  1004. if (queue_family.queueCount == 0) {
  1005. continue;
  1006. }
  1007. if (queue_family.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
  1008. graphics = index;
  1009. }
  1010. if (surface && physical.GetSurfaceSupportKHR(index, surface)) {
  1011. present = index;
  1012. }
  1013. }
  1014. if (!graphics) {
  1015. LOG_ERROR(Render_Vulkan, "Device lacks a graphics queue");
  1016. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  1017. }
  1018. if (surface && !present) {
  1019. LOG_ERROR(Render_Vulkan, "Device lacks a present queue");
  1020. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  1021. }
  1022. if (graphics) {
  1023. graphics_family = *graphics;
  1024. }
  1025. if (present) {
  1026. present_family = *present;
  1027. }
  1028. }
  1029. u64 Device::GetDeviceMemoryUsage() const {
  1030. VkPhysicalDeviceMemoryBudgetPropertiesEXT budget;
  1031. budget.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT;
  1032. budget.pNext = nullptr;
  1033. physical.GetMemoryProperties(&budget);
  1034. u64 result{};
  1035. for (const size_t heap : valid_heap_memory) {
  1036. result += budget.heapUsage[heap];
  1037. }
  1038. return result;
  1039. }
  1040. void Device::CollectPhysicalMemoryInfo() {
  1041. // Account for resolution scaling in memory limits
  1042. const size_t normal_memory = 6_GiB;
  1043. const size_t scaler_memory = 1_GiB * Settings::values.resolution_info.ScaleUp(1);
  1044. // Calculate limits using memory budget
  1045. VkPhysicalDeviceMemoryBudgetPropertiesEXT budget{};
  1046. budget.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT;
  1047. const auto mem_info =
  1048. physical.GetMemoryProperties(extensions.memory_budget ? &budget : nullptr);
  1049. const auto& mem_properties = mem_info.memoryProperties;
  1050. const size_t num_properties = mem_properties.memoryHeapCount;
  1051. device_access_memory = 0;
  1052. u64 device_initial_usage = 0;
  1053. u64 local_memory = 0;
  1054. for (size_t element = 0; element < num_properties; ++element) {
  1055. const bool is_heap_local =
  1056. (mem_properties.memoryHeaps[element].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) != 0;
  1057. if (!is_integrated && !is_heap_local) {
  1058. continue;
  1059. }
  1060. valid_heap_memory.push_back(element);
  1061. if (is_heap_local) {
  1062. local_memory += mem_properties.memoryHeaps[element].size;
  1063. }
  1064. if (extensions.memory_budget) {
  1065. device_initial_usage += budget.heapUsage[element];
  1066. device_access_memory += budget.heapBudget[element];
  1067. continue;
  1068. }
  1069. device_access_memory += mem_properties.memoryHeaps[element].size;
  1070. }
  1071. if (!is_integrated) {
  1072. const u64 reserve_memory = std::min<u64>(device_access_memory / 8, 1_GiB);
  1073. device_access_memory -= reserve_memory;
  1074. device_access_memory = std::min<u64>(device_access_memory, normal_memory + scaler_memory);
  1075. return;
  1076. }
  1077. const s64 available_memory = static_cast<s64>(device_access_memory - device_initial_usage);
  1078. device_access_memory = static_cast<u64>(std::max<s64>(
  1079. std::min<s64>(available_memory - 8_GiB, 4_GiB), std::min<s64>(local_memory, 4_GiB)));
  1080. }
  1081. void Device::CollectToolingInfo() {
  1082. if (!extensions.tooling_info) {
  1083. return;
  1084. }
  1085. auto tools{physical.GetPhysicalDeviceToolProperties()};
  1086. for (const VkPhysicalDeviceToolProperties& tool : tools) {
  1087. const std::string_view name = tool.name;
  1088. LOG_INFO(Render_Vulkan, "Attached debugging tool: {}", name);
  1089. has_renderdoc = has_renderdoc || name == "RenderDoc";
  1090. has_nsight_graphics = has_nsight_graphics || name == "NVIDIA Nsight Graphics";
  1091. }
  1092. }
  1093. std::vector<VkDeviceQueueCreateInfo> Device::GetDeviceQueueCreateInfos() const {
  1094. static constexpr float QUEUE_PRIORITY = 1.0f;
  1095. std::unordered_set<u32> unique_queue_families{graphics_family, present_family};
  1096. std::vector<VkDeviceQueueCreateInfo> queue_cis;
  1097. queue_cis.reserve(unique_queue_families.size());
  1098. for (const u32 queue_family : unique_queue_families) {
  1099. auto& ci = queue_cis.emplace_back(VkDeviceQueueCreateInfo{
  1100. .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
  1101. .pNext = nullptr,
  1102. .flags = 0,
  1103. .queueFamilyIndex = queue_family,
  1104. .queueCount = 1,
  1105. .pQueuePriorities = nullptr,
  1106. });
  1107. ci.pQueuePriorities = &QUEUE_PRIORITY;
  1108. }
  1109. return queue_cis;
  1110. }
  1111. } // namespace Vulkan