vulkan_device.cpp 59 KB

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