vulkan_device.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  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. LOG_WARNING(Render_Vulkan, "ARM drivers have broken VK_EXT_extended_dynamic_state2");
  468. RemoveExtensionFeature(extensions.extended_dynamic_state2, features.extended_dynamic_state2,
  469. VK_EXT_EXTENDED_DYNAMIC_STATE_2_EXTENSION_NAME);
  470. }
  471. if (is_nvidia) {
  472. const u32 nv_major_version = (properties.properties.driverVersion >> 22) & 0x3ff;
  473. const auto arch = GetNvidiaArch();
  474. if (arch >= NvidiaArchitecture::Arch_AmpereOrNewer) {
  475. LOG_WARNING(Render_Vulkan, "Ampere and newer have broken float16 math");
  476. features.shader_float16_int8.shaderFloat16 = false;
  477. } else if (arch <= NvidiaArchitecture::Arch_Volta) {
  478. if (nv_major_version < 527) {
  479. LOG_WARNING(Render_Vulkan, "Volta and older have broken VK_KHR_push_descriptor");
  480. RemoveExtension(extensions.push_descriptor, VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
  481. }
  482. }
  483. if (nv_major_version >= 510) {
  484. LOG_WARNING(Render_Vulkan, "NVIDIA Drivers >= 510 do not support MSAA image blits");
  485. cant_blit_msaa = true;
  486. }
  487. }
  488. if (extensions.extended_dynamic_state && is_radv) {
  489. // Mask driver version variant
  490. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  491. if (version < VK_MAKE_API_VERSION(0, 21, 2, 0)) {
  492. LOG_WARNING(Render_Vulkan,
  493. "RADV versions older than 21.2 have broken VK_EXT_extended_dynamic_state");
  494. RemoveExtensionFeature(extensions.extended_dynamic_state,
  495. features.extended_dynamic_state,
  496. VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
  497. }
  498. }
  499. if (extensions.extended_dynamic_state2 && is_radv) {
  500. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  501. if (version < VK_MAKE_API_VERSION(0, 22, 3, 1)) {
  502. LOG_WARNING(
  503. Render_Vulkan,
  504. "RADV versions older than 22.3.1 have broken VK_EXT_extended_dynamic_state2");
  505. RemoveExtensionFeature(extensions.extended_dynamic_state2,
  506. features.extended_dynamic_state2,
  507. VK_EXT_EXTENDED_DYNAMIC_STATE_2_EXTENSION_NAME);
  508. }
  509. }
  510. if (extensions.extended_dynamic_state2 && is_qualcomm) {
  511. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  512. if (version >= VK_MAKE_API_VERSION(0, 0, 676, 0) &&
  513. version < VK_MAKE_API_VERSION(0, 0, 680, 0)) {
  514. // Qualcomm Adreno 7xx drivers do not properly support extended_dynamic_state2.
  515. LOG_WARNING(Render_Vulkan,
  516. "Qualcomm Adreno 7xx drivers have broken VK_EXT_extended_dynamic_state2");
  517. RemoveExtensionFeature(extensions.extended_dynamic_state2,
  518. features.extended_dynamic_state2,
  519. VK_EXT_EXTENDED_DYNAMIC_STATE_2_EXTENSION_NAME);
  520. }
  521. }
  522. if (extensions.extended_dynamic_state3 && is_radv) {
  523. LOG_WARNING(Render_Vulkan, "RADV has broken extendedDynamicState3ColorBlendEquation");
  524. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEnable = false;
  525. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEquation = false;
  526. dynamic_state3_blending = false;
  527. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  528. if (version < VK_MAKE_API_VERSION(0, 23, 1, 0)) {
  529. LOG_WARNING(Render_Vulkan,
  530. "RADV versions older than 23.1.0 have broken depth clamp dynamic state");
  531. features.extended_dynamic_state3.extendedDynamicState3DepthClampEnable = false;
  532. dynamic_state3_enables = false;
  533. }
  534. }
  535. if (extensions.extended_dynamic_state3 && is_amd_driver) {
  536. LOG_WARNING(Render_Vulkan,
  537. "AMD drivers have broken extendedDynamicState3ColorBlendEquation");
  538. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEnable = false;
  539. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEquation = false;
  540. dynamic_state3_blending = false;
  541. }
  542. if (extensions.vertex_input_dynamic_state && is_radv) {
  543. // TODO(ameerj): Blacklist only offending driver versions
  544. // TODO(ameerj): Confirm if RDNA1 is affected
  545. const bool is_rdna2 =
  546. supported_extensions.contains(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME);
  547. if (is_rdna2) {
  548. LOG_WARNING(Render_Vulkan,
  549. "RADV has broken VK_EXT_vertex_input_dynamic_state on RDNA2 hardware");
  550. RemoveExtensionFeature(extensions.vertex_input_dynamic_state,
  551. features.vertex_input_dynamic_state,
  552. VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
  553. }
  554. }
  555. if (extensions.vertex_input_dynamic_state && is_qualcomm) {
  556. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  557. if (version >= VK_MAKE_API_VERSION(0, 0, 676, 0) &&
  558. version < VK_MAKE_API_VERSION(0, 0, 680, 0)) {
  559. // Qualcomm Adreno 7xx drivers do not properly support vertex_input_dynamic_state.
  560. LOG_WARNING(
  561. Render_Vulkan,
  562. "Qualcomm Adreno 7xx drivers have broken VK_EXT_vertex_input_dynamic_state");
  563. RemoveExtensionFeature(extensions.vertex_input_dynamic_state,
  564. features.vertex_input_dynamic_state,
  565. VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
  566. }
  567. }
  568. sets_per_pool = 64;
  569. if (is_amd_driver) {
  570. // AMD drivers need a higher amount of Sets per Pool in certain circumstances like in XC2.
  571. sets_per_pool = 96;
  572. // Disable VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT on AMD GCN4 and lower as it is broken.
  573. if (!features.shader_float16_int8.shaderFloat16) {
  574. LOG_WARNING(Render_Vulkan,
  575. "AMD GCN4 and earlier have broken VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT");
  576. has_broken_cube_compatibility = true;
  577. }
  578. }
  579. if (is_qualcomm) {
  580. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  581. if (version < VK_MAKE_API_VERSION(0, 255, 615, 512)) {
  582. has_broken_parallel_compiling = true;
  583. }
  584. }
  585. if (extensions.sampler_filter_minmax && is_amd) {
  586. // Disable ext_sampler_filter_minmax on AMD GCN4 and lower as it is broken.
  587. if (!features.shader_float16_int8.shaderFloat16) {
  588. LOG_WARNING(Render_Vulkan,
  589. "AMD GCN4 and earlier have broken VK_EXT_sampler_filter_minmax");
  590. RemoveExtension(extensions.sampler_filter_minmax,
  591. VK_EXT_SAMPLER_FILTER_MINMAX_EXTENSION_NAME);
  592. }
  593. }
  594. if (extensions.vertex_input_dynamic_state && is_intel_windows) {
  595. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  596. if (version < VK_MAKE_API_VERSION(27, 20, 100, 0)) {
  597. LOG_WARNING(Render_Vulkan, "Intel has broken VK_EXT_vertex_input_dynamic_state");
  598. RemoveExtensionFeature(extensions.vertex_input_dynamic_state,
  599. features.vertex_input_dynamic_state,
  600. VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
  601. }
  602. }
  603. if (features.shader_float16_int8.shaderFloat16 && is_intel_windows) {
  604. // Intel's compiler crashes when using fp16 on Astral Chain, disable it for the time being.
  605. LOG_WARNING(Render_Vulkan, "Intel has broken float16 math");
  606. features.shader_float16_int8.shaderFloat16 = false;
  607. }
  608. if (is_intel_windows) {
  609. LOG_WARNING(Render_Vulkan, "Intel proprietary drivers do not support MSAA image blits");
  610. cant_blit_msaa = true;
  611. }
  612. has_broken_compute =
  613. CheckBrokenCompute(properties.driver.driverID, properties.properties.driverVersion) &&
  614. !Settings::values.enable_compute_pipelines.GetValue();
  615. if (is_intel_anv || (is_qualcomm && !is_s8gen2)) {
  616. LOG_WARNING(Render_Vulkan, "Driver does not support native BGR format");
  617. must_emulate_bgr565 = true;
  618. }
  619. if (extensions.push_descriptor && is_intel_anv) {
  620. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  621. if (version >= VK_MAKE_API_VERSION(0, 22, 3, 0) &&
  622. version < VK_MAKE_API_VERSION(0, 23, 2, 0)) {
  623. // Disable VK_KHR_push_descriptor due to
  624. // mesa/mesa/-/commit/ff91c5ca42bc80aa411cb3fd8f550aa6fdd16bdc
  625. LOG_WARNING(Render_Vulkan,
  626. "ANV drivers 22.3.0 to 23.1.0 have broken VK_KHR_push_descriptor");
  627. RemoveExtension(extensions.push_descriptor, VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
  628. }
  629. } else if (extensions.push_descriptor && is_nvidia) {
  630. const auto arch = GetNvidiaArch();
  631. if (arch <= NvidiaArchitecture::Arch_Pascal) {
  632. LOG_WARNING(Render_Vulkan,
  633. "Pascal and older architectures have broken VK_KHR_push_descriptor");
  634. RemoveExtension(extensions.push_descriptor, VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
  635. }
  636. }
  637. if (is_mvk) {
  638. LOG_WARNING(Render_Vulkan,
  639. "MVK driver breaks when using more than 16 vertex attributes/bindings");
  640. properties.properties.limits.maxVertexInputAttributes =
  641. std::min(properties.properties.limits.maxVertexInputAttributes, 16U);
  642. properties.properties.limits.maxVertexInputBindings =
  643. std::min(properties.properties.limits.maxVertexInputBindings, 16U);
  644. }
  645. logical = vk::Device::Create(physical, queue_cis, ExtensionListForVulkan(loaded_extensions),
  646. first_next, dld);
  647. graphics_queue = logical.GetQueue(graphics_family);
  648. present_queue = logical.GetQueue(present_family);
  649. VmaVulkanFunctions functions{};
  650. functions.vkGetInstanceProcAddr = dld.vkGetInstanceProcAddr;
  651. functions.vkGetDeviceProcAddr = dld.vkGetDeviceProcAddr;
  652. const VmaAllocatorCreateInfo allocator_info = {
  653. .flags = VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT,
  654. .physicalDevice = physical,
  655. .device = *logical,
  656. .preferredLargeHeapBlockSize = 0,
  657. .pAllocationCallbacks = nullptr,
  658. .pDeviceMemoryCallbacks = nullptr,
  659. .pHeapSizeLimit = nullptr,
  660. .pVulkanFunctions = &functions,
  661. .instance = instance,
  662. .vulkanApiVersion = VK_API_VERSION_1_1,
  663. .pTypeExternalMemoryHandleTypes = nullptr,
  664. };
  665. vk::Check(vmaCreateAllocator(&allocator_info, &allocator));
  666. }
  667. Device::~Device() {
  668. vmaDestroyAllocator(allocator);
  669. }
  670. VkFormat Device::GetSupportedFormat(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  671. FormatType format_type) const {
  672. if (IsFormatSupported(wanted_format, wanted_usage, format_type)) {
  673. return wanted_format;
  674. }
  675. // The wanted format is not supported by hardware, search for alternatives
  676. const VkFormat* alternatives = GetFormatAlternatives(wanted_format);
  677. if (alternatives == nullptr) {
  678. ASSERT_MSG(false,
  679. "Format={} with usage={} and type={} has no defined alternatives and host "
  680. "hardware does not support it",
  681. wanted_format, wanted_usage, format_type);
  682. return wanted_format;
  683. }
  684. std::size_t i = 0;
  685. for (VkFormat alternative = *alternatives; alternative; alternative = alternatives[++i]) {
  686. if (!IsFormatSupported(alternative, wanted_usage, format_type)) {
  687. continue;
  688. }
  689. LOG_DEBUG(Render_Vulkan,
  690. "Emulating format={} with alternative format={} with usage={} and type={}",
  691. wanted_format, alternative, wanted_usage, format_type);
  692. return alternative;
  693. }
  694. // No alternatives found, panic
  695. ASSERT_MSG(false,
  696. "Format={} with usage={} and type={} is not supported by the host hardware and "
  697. "doesn't support any of the alternatives",
  698. wanted_format, wanted_usage, format_type);
  699. return wanted_format;
  700. }
  701. void Device::ReportLoss() const {
  702. LOG_CRITICAL(Render_Vulkan, "Device loss occurred!");
  703. // Wait for the log to flush and for Nsight Aftermath to dump the results
  704. std::this_thread::sleep_for(std::chrono::seconds{15});
  705. }
  706. void Device::SaveShader(std::span<const u32> spirv) const {
  707. if (nsight_aftermath_tracker) {
  708. nsight_aftermath_tracker->SaveShader(spirv);
  709. }
  710. }
  711. bool Device::ComputeIsOptimalAstcSupported() const {
  712. // Disable for now to avoid converting ASTC twice.
  713. static constexpr std::array astc_formats = {
  714. VK_FORMAT_ASTC_4x4_UNORM_BLOCK, VK_FORMAT_ASTC_4x4_SRGB_BLOCK,
  715. VK_FORMAT_ASTC_5x4_UNORM_BLOCK, VK_FORMAT_ASTC_5x4_SRGB_BLOCK,
  716. VK_FORMAT_ASTC_5x5_UNORM_BLOCK, VK_FORMAT_ASTC_5x5_SRGB_BLOCK,
  717. VK_FORMAT_ASTC_6x5_UNORM_BLOCK, VK_FORMAT_ASTC_6x5_SRGB_BLOCK,
  718. VK_FORMAT_ASTC_6x6_UNORM_BLOCK, VK_FORMAT_ASTC_6x6_SRGB_BLOCK,
  719. VK_FORMAT_ASTC_8x5_UNORM_BLOCK, VK_FORMAT_ASTC_8x5_SRGB_BLOCK,
  720. VK_FORMAT_ASTC_8x6_UNORM_BLOCK, VK_FORMAT_ASTC_8x6_SRGB_BLOCK,
  721. VK_FORMAT_ASTC_8x8_UNORM_BLOCK, VK_FORMAT_ASTC_8x8_SRGB_BLOCK,
  722. VK_FORMAT_ASTC_10x5_UNORM_BLOCK, VK_FORMAT_ASTC_10x5_SRGB_BLOCK,
  723. VK_FORMAT_ASTC_10x6_UNORM_BLOCK, VK_FORMAT_ASTC_10x6_SRGB_BLOCK,
  724. VK_FORMAT_ASTC_10x8_UNORM_BLOCK, VK_FORMAT_ASTC_10x8_SRGB_BLOCK,
  725. VK_FORMAT_ASTC_10x10_UNORM_BLOCK, VK_FORMAT_ASTC_10x10_SRGB_BLOCK,
  726. VK_FORMAT_ASTC_12x10_UNORM_BLOCK, VK_FORMAT_ASTC_12x10_SRGB_BLOCK,
  727. VK_FORMAT_ASTC_12x12_UNORM_BLOCK, VK_FORMAT_ASTC_12x12_SRGB_BLOCK,
  728. };
  729. if (!features.features.textureCompressionASTC_LDR) {
  730. return false;
  731. }
  732. const auto format_feature_usage{
  733. VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_BLIT_SRC_BIT |
  734. VK_FORMAT_FEATURE_BLIT_DST_BIT | VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
  735. VK_FORMAT_FEATURE_TRANSFER_DST_BIT};
  736. for (const auto format : astc_formats) {
  737. const auto physical_format_properties{physical.GetFormatProperties(format)};
  738. if ((physical_format_properties.optimalTilingFeatures & format_feature_usage) == 0) {
  739. return false;
  740. }
  741. }
  742. return true;
  743. }
  744. bool Device::TestDepthStencilBlits(VkFormat format) const {
  745. static constexpr VkFormatFeatureFlags required_features =
  746. VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
  747. const auto test_features = [](VkFormatProperties props) {
  748. return (props.optimalTilingFeatures & required_features) == required_features;
  749. };
  750. return test_features(format_properties.at(format));
  751. }
  752. bool Device::IsFormatSupported(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  753. FormatType format_type) const {
  754. const auto it = format_properties.find(wanted_format);
  755. if (it == format_properties.end()) {
  756. UNIMPLEMENTED_MSG("Unimplemented format query={}", wanted_format);
  757. return true;
  758. }
  759. const auto supported_usage = GetFormatFeatures(it->second, format_type);
  760. return (supported_usage & wanted_usage) == wanted_usage;
  761. }
  762. std::string Device::GetDriverName() const {
  763. switch (properties.driver.driverID) {
  764. case VK_DRIVER_ID_AMD_PROPRIETARY:
  765. return "AMD";
  766. case VK_DRIVER_ID_AMD_OPEN_SOURCE:
  767. return "AMDVLK";
  768. case VK_DRIVER_ID_MESA_RADV:
  769. return "RADV";
  770. case VK_DRIVER_ID_NVIDIA_PROPRIETARY:
  771. return "NVIDIA";
  772. case VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS:
  773. return "Intel";
  774. case VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA:
  775. return "ANV";
  776. case VK_DRIVER_ID_IMAGINATION_PROPRIETARY:
  777. return "PowerVR";
  778. case VK_DRIVER_ID_QUALCOMM_PROPRIETARY:
  779. return "Qualcomm";
  780. case VK_DRIVER_ID_ARM_PROPRIETARY:
  781. return "Mali";
  782. case VK_DRIVER_ID_GOOGLE_SWIFTSHADER:
  783. return "SwiftShader";
  784. case VK_DRIVER_ID_BROADCOM_PROPRIETARY:
  785. return "Broadcom";
  786. case VK_DRIVER_ID_MESA_LLVMPIPE:
  787. return "Lavapipe";
  788. case VK_DRIVER_ID_MOLTENVK:
  789. return "MoltenVK";
  790. case VK_DRIVER_ID_VERISILICON_PROPRIETARY:
  791. return "Vivante";
  792. case VK_DRIVER_ID_MESA_TURNIP:
  793. return "Turnip";
  794. case VK_DRIVER_ID_MESA_V3DV:
  795. return "V3DV";
  796. case VK_DRIVER_ID_MESA_PANVK:
  797. return "PanVK";
  798. case VK_DRIVER_ID_MESA_VENUS:
  799. return "Venus";
  800. case VK_DRIVER_ID_MESA_DOZEN:
  801. return "Dozen";
  802. case VK_DRIVER_ID_MESA_NVK:
  803. return "NVK";
  804. case VK_DRIVER_ID_IMAGINATION_OPEN_SOURCE_MESA:
  805. return "PVR";
  806. // case VK_DRIVER_ID_MESA_AGXV:
  807. // return "Asahi";
  808. default:
  809. return properties.driver.driverName;
  810. }
  811. }
  812. bool Device::ShouldBoostClocks() const {
  813. const auto driver_id = properties.driver.driverID;
  814. const auto vendor_id = properties.properties.vendorID;
  815. const auto device_id = properties.properties.deviceID;
  816. const bool validated_driver =
  817. driver_id == VK_DRIVER_ID_AMD_PROPRIETARY || driver_id == VK_DRIVER_ID_AMD_OPEN_SOURCE ||
  818. driver_id == VK_DRIVER_ID_MESA_RADV || driver_id == VK_DRIVER_ID_NVIDIA_PROPRIETARY ||
  819. driver_id == VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS ||
  820. driver_id == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA ||
  821. driver_id == VK_DRIVER_ID_QUALCOMM_PROPRIETARY || driver_id == VK_DRIVER_ID_MESA_TURNIP;
  822. const bool is_steam_deck = (vendor_id == 0x1002 && device_id == 0x163F) ||
  823. (vendor_id == 0x1002 && device_id == 0x1435);
  824. const bool is_debugging = this->HasDebuggingToolAttached();
  825. return validated_driver && !is_steam_deck && !is_debugging;
  826. }
  827. bool Device::HasTimelineSemaphore() const {
  828. if (GetDriverID() == VK_DRIVER_ID_QUALCOMM_PROPRIETARY ||
  829. GetDriverID() == VK_DRIVER_ID_MESA_TURNIP) {
  830. // Timeline semaphores do not work properly on all Qualcomm drivers.
  831. // They generally work properly with Turnip drivers, but are problematic on some devices
  832. // (e.g. ZTE handsets with Snapdragon 870).
  833. return false;
  834. }
  835. return features.timeline_semaphore.timelineSemaphore;
  836. }
  837. bool Device::GetSuitability(bool requires_swapchain) {
  838. // Assume we will be suitable.
  839. bool suitable = true;
  840. // Configure properties.
  841. properties.properties = physical.GetProperties();
  842. // Set instance version.
  843. instance_version = properties.properties.apiVersion;
  844. // Minimum of API version 1.1 is required. (This is well-supported.)
  845. ASSERT(instance_version >= VK_API_VERSION_1_1);
  846. // Get available extensions.
  847. auto extension_properties = physical.EnumerateDeviceExtensionProperties();
  848. // Get the set of supported extensions.
  849. supported_extensions.clear();
  850. for (const VkExtensionProperties& property : extension_properties) {
  851. supported_extensions.insert(property.extensionName);
  852. }
  853. // Generate list of extensions to load.
  854. loaded_extensions.clear();
  855. #define EXTENSION(prefix, macro_name, var_name) \
  856. if (supported_extensions.contains(VK_##prefix##_##macro_name##_EXTENSION_NAME)) { \
  857. loaded_extensions.insert(VK_##prefix##_##macro_name##_EXTENSION_NAME); \
  858. extensions.var_name = true; \
  859. }
  860. #define FEATURE_EXTENSION(prefix, struct_name, macro_name, var_name) \
  861. if (supported_extensions.contains(VK_##prefix##_##macro_name##_EXTENSION_NAME)) { \
  862. loaded_extensions.insert(VK_##prefix##_##macro_name##_EXTENSION_NAME); \
  863. extensions.var_name = true; \
  864. }
  865. if (instance_version < VK_API_VERSION_1_2) {
  866. FOR_EACH_VK_FEATURE_1_2(FEATURE_EXTENSION);
  867. }
  868. if (instance_version < VK_API_VERSION_1_3) {
  869. FOR_EACH_VK_FEATURE_1_3(FEATURE_EXTENSION);
  870. }
  871. FOR_EACH_VK_FEATURE_EXT(FEATURE_EXTENSION);
  872. FOR_EACH_VK_EXTENSION(EXTENSION);
  873. #undef FEATURE_EXTENSION
  874. #undef EXTENSION
  875. // Some extensions are mandatory. Check those.
  876. #define CHECK_EXTENSION(extension_name) \
  877. if (!loaded_extensions.contains(extension_name)) { \
  878. LOG_ERROR(Render_Vulkan, "Missing required extension {}", extension_name); \
  879. suitable = false; \
  880. }
  881. #define LOG_EXTENSION(extension_name) \
  882. if (!loaded_extensions.contains(extension_name)) { \
  883. LOG_INFO(Render_Vulkan, "Device doesn't support extension {}", extension_name); \
  884. }
  885. FOR_EACH_VK_RECOMMENDED_EXTENSION(LOG_EXTENSION);
  886. FOR_EACH_VK_MANDATORY_EXTENSION(CHECK_EXTENSION);
  887. if (requires_swapchain) {
  888. CHECK_EXTENSION(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
  889. }
  890. #undef LOG_EXTENSION
  891. #undef CHECK_EXTENSION
  892. // Generate the linked list of features to test.
  893. features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  894. // Set next pointer.
  895. void** next = &features2.pNext;
  896. // Test all features we know about. If the feature is not available in core at our
  897. // current API version, and was not enabled by an extension, skip testing the feature.
  898. // We set the structure sType explicitly here as it is zeroed by the constructor.
  899. #define FEATURE(prefix, struct_name, macro_name, var_name) \
  900. features.var_name.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_##macro_name##_FEATURES; \
  901. SetNext(next, features.var_name);
  902. #define EXT_FEATURE(prefix, struct_name, macro_name, var_name) \
  903. if (extensions.var_name) { \
  904. features.var_name.sType = \
  905. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_##macro_name##_FEATURES_##prefix; \
  906. SetNext(next, features.var_name); \
  907. }
  908. FOR_EACH_VK_FEATURE_1_1(FEATURE);
  909. FOR_EACH_VK_FEATURE_EXT(EXT_FEATURE);
  910. if (instance_version >= VK_API_VERSION_1_2) {
  911. FOR_EACH_VK_FEATURE_1_2(FEATURE);
  912. } else {
  913. FOR_EACH_VK_FEATURE_1_2(EXT_FEATURE);
  914. }
  915. if (instance_version >= VK_API_VERSION_1_3) {
  916. FOR_EACH_VK_FEATURE_1_3(FEATURE);
  917. } else {
  918. FOR_EACH_VK_FEATURE_1_3(EXT_FEATURE);
  919. }
  920. #undef EXT_FEATURE
  921. #undef FEATURE
  922. // Perform the feature test.
  923. physical.GetFeatures2(features2);
  924. features.features = features2.features;
  925. // Some features are mandatory. Check those.
  926. #define CHECK_FEATURE(feature, name) \
  927. if (!features.feature.name) { \
  928. LOG_ERROR(Render_Vulkan, "Missing required feature {}", #name); \
  929. suitable = false; \
  930. }
  931. #define LOG_FEATURE(feature, name) \
  932. if (!features.feature.name) { \
  933. LOG_INFO(Render_Vulkan, "Device doesn't support feature {}", #name); \
  934. }
  935. FOR_EACH_VK_RECOMMENDED_FEATURE(LOG_FEATURE);
  936. FOR_EACH_VK_MANDATORY_FEATURE(CHECK_FEATURE);
  937. #undef LOG_FEATURE
  938. #undef CHECK_FEATURE
  939. // Generate linked list of properties.
  940. properties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
  941. // Set next pointer.
  942. next = &properties2.pNext;
  943. // Get driver info.
  944. properties.driver.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES;
  945. SetNext(next, properties.driver);
  946. // Retrieve subgroup properties.
  947. properties.subgroup_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES;
  948. SetNext(next, properties.subgroup_properties);
  949. // Retrieve relevant extension properties.
  950. if (extensions.shader_float_controls) {
  951. properties.float_controls.sType =
  952. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT_CONTROLS_PROPERTIES;
  953. SetNext(next, properties.float_controls);
  954. }
  955. if (extensions.push_descriptor) {
  956. properties.push_descriptor.sType =
  957. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR;
  958. SetNext(next, properties.push_descriptor);
  959. }
  960. if (extensions.subgroup_size_control || features.subgroup_size_control.subgroupSizeControl) {
  961. properties.subgroup_size_control.sType =
  962. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_PROPERTIES;
  963. SetNext(next, properties.subgroup_size_control);
  964. }
  965. if (extensions.transform_feedback) {
  966. properties.transform_feedback.sType =
  967. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT;
  968. SetNext(next, properties.transform_feedback);
  969. }
  970. // Perform the property fetch.
  971. physical.GetProperties2(properties2);
  972. properties.properties = properties2.properties;
  973. // Unload extensions if feature support is insufficient.
  974. RemoveUnsuitableExtensions();
  975. // Check limits.
  976. struct Limit {
  977. u32 minimum;
  978. u32 value;
  979. const char* name;
  980. };
  981. const VkPhysicalDeviceLimits& limits{properties.properties.limits};
  982. const std::array limits_report{
  983. Limit{65536, limits.maxUniformBufferRange, "maxUniformBufferRange"},
  984. Limit{16, limits.maxViewports, "maxViewports"},
  985. Limit{8, limits.maxColorAttachments, "maxColorAttachments"},
  986. Limit{8, limits.maxClipDistances, "maxClipDistances"},
  987. };
  988. for (const auto& [min, value, name] : limits_report) {
  989. if (value < min) {
  990. LOG_ERROR(Render_Vulkan, "{} has to be {} or greater but it is {}", name, min, value);
  991. suitable = false;
  992. }
  993. }
  994. // Return whether we were suitable.
  995. return suitable;
  996. }
  997. void Device::RemoveUnsuitableExtensions() {
  998. // VK_EXT_custom_border_color
  999. extensions.custom_border_color = features.custom_border_color.customBorderColors &&
  1000. features.custom_border_color.customBorderColorWithoutFormat;
  1001. RemoveExtensionFeatureIfUnsuitable(extensions.custom_border_color, features.custom_border_color,
  1002. VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME);
  1003. // VK_EXT_depth_bias_control
  1004. extensions.depth_bias_control =
  1005. features.depth_bias_control.depthBiasControl &&
  1006. features.depth_bias_control.leastRepresentableValueForceUnormRepresentation;
  1007. RemoveExtensionFeatureIfUnsuitable(extensions.depth_bias_control, features.depth_bias_control,
  1008. VK_EXT_DEPTH_BIAS_CONTROL_EXTENSION_NAME);
  1009. // VK_EXT_depth_clip_control
  1010. extensions.depth_clip_control = features.depth_clip_control.depthClipControl;
  1011. RemoveExtensionFeatureIfUnsuitable(extensions.depth_clip_control, features.depth_clip_control,
  1012. VK_EXT_DEPTH_CLIP_CONTROL_EXTENSION_NAME);
  1013. // VK_EXT_extended_dynamic_state
  1014. extensions.extended_dynamic_state = features.extended_dynamic_state.extendedDynamicState;
  1015. RemoveExtensionFeatureIfUnsuitable(extensions.extended_dynamic_state,
  1016. features.extended_dynamic_state,
  1017. VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
  1018. // VK_EXT_extended_dynamic_state2
  1019. extensions.extended_dynamic_state2 = features.extended_dynamic_state2.extendedDynamicState2;
  1020. RemoveExtensionFeatureIfUnsuitable(extensions.extended_dynamic_state2,
  1021. features.extended_dynamic_state2,
  1022. VK_EXT_EXTENDED_DYNAMIC_STATE_2_EXTENSION_NAME);
  1023. // VK_EXT_extended_dynamic_state3
  1024. dynamic_state3_blending =
  1025. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEnable &&
  1026. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEquation &&
  1027. features.extended_dynamic_state3.extendedDynamicState3ColorWriteMask;
  1028. dynamic_state3_enables =
  1029. features.extended_dynamic_state3.extendedDynamicState3DepthClampEnable &&
  1030. features.extended_dynamic_state3.extendedDynamicState3LogicOpEnable;
  1031. extensions.extended_dynamic_state3 = dynamic_state3_blending || dynamic_state3_enables;
  1032. dynamic_state3_blending = dynamic_state3_blending && extensions.extended_dynamic_state3;
  1033. dynamic_state3_enables = dynamic_state3_enables && extensions.extended_dynamic_state3;
  1034. RemoveExtensionFeatureIfUnsuitable(extensions.extended_dynamic_state3,
  1035. features.extended_dynamic_state3,
  1036. VK_EXT_EXTENDED_DYNAMIC_STATE_3_EXTENSION_NAME);
  1037. // VK_EXT_provoking_vertex
  1038. extensions.provoking_vertex =
  1039. features.provoking_vertex.provokingVertexLast &&
  1040. features.provoking_vertex.transformFeedbackPreservesProvokingVertex;
  1041. RemoveExtensionFeatureIfUnsuitable(extensions.provoking_vertex, features.provoking_vertex,
  1042. VK_EXT_PROVOKING_VERTEX_EXTENSION_NAME);
  1043. // VK_KHR_shader_atomic_int64
  1044. extensions.shader_atomic_int64 = features.shader_atomic_int64.shaderBufferInt64Atomics &&
  1045. features.shader_atomic_int64.shaderSharedInt64Atomics;
  1046. RemoveExtensionFeatureIfUnsuitable(extensions.shader_atomic_int64, features.shader_atomic_int64,
  1047. VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME);
  1048. // VK_EXT_shader_demote_to_helper_invocation
  1049. extensions.shader_demote_to_helper_invocation =
  1050. features.shader_demote_to_helper_invocation.shaderDemoteToHelperInvocation;
  1051. RemoveExtensionFeatureIfUnsuitable(extensions.shader_demote_to_helper_invocation,
  1052. features.shader_demote_to_helper_invocation,
  1053. VK_EXT_SHADER_DEMOTE_TO_HELPER_INVOCATION_EXTENSION_NAME);
  1054. // VK_EXT_subgroup_size_control
  1055. extensions.subgroup_size_control =
  1056. features.subgroup_size_control.subgroupSizeControl &&
  1057. properties.subgroup_size_control.minSubgroupSize <= GuestWarpSize &&
  1058. properties.subgroup_size_control.maxSubgroupSize >= GuestWarpSize;
  1059. RemoveExtensionFeatureIfUnsuitable(extensions.subgroup_size_control,
  1060. features.subgroup_size_control,
  1061. VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME);
  1062. // VK_EXT_transform_feedback
  1063. extensions.transform_feedback =
  1064. features.transform_feedback.transformFeedback &&
  1065. features.transform_feedback.geometryStreams &&
  1066. properties.transform_feedback.maxTransformFeedbackStreams >= 4 &&
  1067. properties.transform_feedback.maxTransformFeedbackBuffers > 0 &&
  1068. properties.transform_feedback.transformFeedbackQueries &&
  1069. properties.transform_feedback.transformFeedbackDraw;
  1070. RemoveExtensionFeatureIfUnsuitable(extensions.transform_feedback, features.transform_feedback,
  1071. VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME);
  1072. // VK_EXT_vertex_input_dynamic_state
  1073. extensions.vertex_input_dynamic_state =
  1074. features.vertex_input_dynamic_state.vertexInputDynamicState;
  1075. RemoveExtensionFeatureIfUnsuitable(extensions.vertex_input_dynamic_state,
  1076. features.vertex_input_dynamic_state,
  1077. VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
  1078. // VK_KHR_pipeline_executable_properties
  1079. if (Settings::values.renderer_shader_feedback.GetValue()) {
  1080. extensions.pipeline_executable_properties =
  1081. features.pipeline_executable_properties.pipelineExecutableInfo;
  1082. RemoveExtensionFeatureIfUnsuitable(extensions.pipeline_executable_properties,
  1083. features.pipeline_executable_properties,
  1084. VK_KHR_PIPELINE_EXECUTABLE_PROPERTIES_EXTENSION_NAME);
  1085. } else {
  1086. RemoveExtensionFeature(extensions.pipeline_executable_properties,
  1087. features.pipeline_executable_properties,
  1088. VK_KHR_PIPELINE_EXECUTABLE_PROPERTIES_EXTENSION_NAME);
  1089. }
  1090. // VK_KHR_workgroup_memory_explicit_layout
  1091. extensions.workgroup_memory_explicit_layout =
  1092. features.features.shaderInt16 &&
  1093. features.workgroup_memory_explicit_layout.workgroupMemoryExplicitLayout &&
  1094. features.workgroup_memory_explicit_layout.workgroupMemoryExplicitLayout8BitAccess &&
  1095. features.workgroup_memory_explicit_layout.workgroupMemoryExplicitLayout16BitAccess &&
  1096. features.workgroup_memory_explicit_layout.workgroupMemoryExplicitLayoutScalarBlockLayout;
  1097. RemoveExtensionFeatureIfUnsuitable(extensions.workgroup_memory_explicit_layout,
  1098. features.workgroup_memory_explicit_layout,
  1099. VK_KHR_WORKGROUP_MEMORY_EXPLICIT_LAYOUT_EXTENSION_NAME);
  1100. }
  1101. void Device::SetupFamilies(VkSurfaceKHR surface) {
  1102. const std::vector queue_family_properties = physical.GetQueueFamilyProperties();
  1103. std::optional<u32> graphics;
  1104. std::optional<u32> present;
  1105. for (u32 index = 0; index < static_cast<u32>(queue_family_properties.size()); ++index) {
  1106. if (graphics && (present || !surface)) {
  1107. break;
  1108. }
  1109. const VkQueueFamilyProperties& queue_family = queue_family_properties[index];
  1110. if (queue_family.queueCount == 0) {
  1111. continue;
  1112. }
  1113. if (queue_family.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
  1114. graphics = index;
  1115. }
  1116. if (surface && physical.GetSurfaceSupportKHR(index, surface)) {
  1117. present = index;
  1118. }
  1119. }
  1120. if (!graphics) {
  1121. LOG_ERROR(Render_Vulkan, "Device lacks a graphics queue");
  1122. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  1123. }
  1124. if (surface && !present) {
  1125. LOG_ERROR(Render_Vulkan, "Device lacks a present queue");
  1126. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  1127. }
  1128. if (graphics) {
  1129. graphics_family = *graphics;
  1130. }
  1131. if (present) {
  1132. present_family = *present;
  1133. }
  1134. }
  1135. u64 Device::GetDeviceMemoryUsage() const {
  1136. VkPhysicalDeviceMemoryBudgetPropertiesEXT budget;
  1137. budget.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT;
  1138. budget.pNext = nullptr;
  1139. physical.GetMemoryProperties(&budget);
  1140. u64 result{};
  1141. for (const size_t heap : valid_heap_memory) {
  1142. result += budget.heapUsage[heap];
  1143. }
  1144. return result;
  1145. }
  1146. void Device::CollectPhysicalMemoryInfo() {
  1147. // Account for resolution scaling in memory limits
  1148. const size_t normal_memory = 6_GiB;
  1149. const size_t scaler_memory = 1_GiB * Settings::values.resolution_info.ScaleUp(1);
  1150. // Calculate limits using memory budget
  1151. VkPhysicalDeviceMemoryBudgetPropertiesEXT budget{};
  1152. budget.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT;
  1153. const auto mem_info =
  1154. physical.GetMemoryProperties(extensions.memory_budget ? &budget : nullptr);
  1155. const auto& mem_properties = mem_info.memoryProperties;
  1156. const size_t num_properties = mem_properties.memoryHeapCount;
  1157. device_access_memory = 0;
  1158. u64 device_initial_usage = 0;
  1159. u64 local_memory = 0;
  1160. for (size_t element = 0; element < num_properties; ++element) {
  1161. const bool is_heap_local =
  1162. (mem_properties.memoryHeaps[element].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) != 0;
  1163. if (!is_integrated && !is_heap_local) {
  1164. continue;
  1165. }
  1166. valid_heap_memory.push_back(element);
  1167. if (is_heap_local) {
  1168. local_memory += mem_properties.memoryHeaps[element].size;
  1169. }
  1170. if (extensions.memory_budget) {
  1171. device_initial_usage += budget.heapUsage[element];
  1172. device_access_memory += budget.heapBudget[element];
  1173. continue;
  1174. }
  1175. device_access_memory += mem_properties.memoryHeaps[element].size;
  1176. }
  1177. if (!is_integrated) {
  1178. const u64 reserve_memory = std::min<u64>(device_access_memory / 8, 1_GiB);
  1179. device_access_memory -= reserve_memory;
  1180. device_access_memory = std::min<u64>(device_access_memory, normal_memory + scaler_memory);
  1181. return;
  1182. }
  1183. const s64 available_memory = static_cast<s64>(device_access_memory - device_initial_usage);
  1184. device_access_memory = static_cast<u64>(std::max<s64>(
  1185. std::min<s64>(available_memory - 8_GiB, 4_GiB), std::min<s64>(local_memory, 4_GiB)));
  1186. }
  1187. void Device::CollectToolingInfo() {
  1188. if (!extensions.tooling_info) {
  1189. return;
  1190. }
  1191. auto tools{physical.GetPhysicalDeviceToolProperties()};
  1192. for (const VkPhysicalDeviceToolProperties& tool : tools) {
  1193. const std::string_view name = tool.name;
  1194. LOG_INFO(Render_Vulkan, "Attached debugging tool: {}", name);
  1195. has_renderdoc = has_renderdoc || name == "RenderDoc";
  1196. has_nsight_graphics = has_nsight_graphics || name == "NVIDIA Nsight Graphics";
  1197. }
  1198. }
  1199. std::vector<VkDeviceQueueCreateInfo> Device::GetDeviceQueueCreateInfos() const {
  1200. static constexpr float QUEUE_PRIORITY = 1.0f;
  1201. std::unordered_set<u32> unique_queue_families{graphics_family, present_family};
  1202. std::vector<VkDeviceQueueCreateInfo> queue_cis;
  1203. queue_cis.reserve(unique_queue_families.size());
  1204. for (const u32 queue_family : unique_queue_families) {
  1205. auto& ci = queue_cis.emplace_back(VkDeviceQueueCreateInfo{
  1206. .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
  1207. .pNext = nullptr,
  1208. .flags = 0,
  1209. .queueFamilyIndex = queue_family,
  1210. .queueCount = 1,
  1211. .pQueuePriorities = nullptr,
  1212. });
  1213. ci.pQueuePriorities = &QUEUE_PRIORITY;
  1214. }
  1215. return queue_cis;
  1216. }
  1217. } // namespace Vulkan