vulkan_device.cpp 43 KB

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