vulkan_device.cpp 48 KB

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