vulkan_device.cpp 58 KB

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