vulkan_device.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  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. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  379. if (version < VK_MAKE_API_VERSION(0, 23, 1, 0)) {
  380. LOG_WARNING(Render_Vulkan,
  381. "RADV versions older than 23.1.0 have broken depth clamp dynamic state");
  382. features.extended_dynamic_state3.extendedDynamicState3DepthClampEnable = false;
  383. dynamic_state3_enables = false;
  384. }
  385. }
  386. if (extensions.vertex_input_dynamic_state && is_radv) {
  387. // TODO(ameerj): Blacklist only offending driver versions
  388. // TODO(ameerj): Confirm if RDNA1 is affected
  389. const bool is_rdna2 =
  390. supported_extensions.contains(VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME);
  391. if (is_rdna2) {
  392. LOG_WARNING(Render_Vulkan,
  393. "RADV has broken VK_EXT_vertex_input_dynamic_state on RDNA2 hardware");
  394. features.vertex_input_dynamic_state.vertexInputDynamicState = false;
  395. extensions.vertex_input_dynamic_state = false;
  396. loaded_extensions.erase(VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
  397. }
  398. }
  399. sets_per_pool = 64;
  400. if (is_amd_driver) {
  401. // AMD drivers need a higher amount of Sets per Pool in certain circumstances like in XC2.
  402. sets_per_pool = 96;
  403. // Disable VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT on AMD GCN4 and lower as it is broken.
  404. if (!features.shader_float16_int8.shaderFloat16) {
  405. LOG_WARNING(Render_Vulkan,
  406. "AMD GCN4 and earlier have broken VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT");
  407. has_broken_cube_compatibility = true;
  408. }
  409. }
  410. if (extensions.sampler_filter_minmax && is_amd) {
  411. // Disable ext_sampler_filter_minmax on AMD GCN4 and lower as it is broken.
  412. if (!features.shader_float16_int8.shaderFloat16) {
  413. LOG_WARNING(Render_Vulkan,
  414. "AMD GCN4 and earlier have broken VK_EXT_sampler_filter_minmax");
  415. extensions.sampler_filter_minmax = false;
  416. loaded_extensions.erase(VK_EXT_SAMPLER_FILTER_MINMAX_EXTENSION_NAME);
  417. }
  418. }
  419. if (extensions.vertex_input_dynamic_state && is_intel_windows) {
  420. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  421. if (version < VK_MAKE_API_VERSION(27, 20, 100, 0)) {
  422. LOG_WARNING(Render_Vulkan, "Intel has broken VK_EXT_vertex_input_dynamic_state");
  423. extensions.vertex_input_dynamic_state = false;
  424. loaded_extensions.erase(VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
  425. }
  426. }
  427. if (features.shader_float16_int8.shaderFloat16 && is_intel_windows) {
  428. // Intel's compiler crashes when using fp16 on Astral Chain, disable it for the time being.
  429. LOG_WARNING(Render_Vulkan, "Intel has broken float16 math");
  430. features.shader_float16_int8.shaderFloat16 = false;
  431. }
  432. if (is_intel_windows) {
  433. LOG_WARNING(Render_Vulkan, "Intel proprietary drivers do not support MSAA image blits");
  434. cant_blit_msaa = true;
  435. }
  436. if (is_intel_anv) {
  437. LOG_WARNING(Render_Vulkan, "ANV driver does not support native BGR format");
  438. must_emulate_bgr565 = true;
  439. }
  440. if (extensions.push_descriptor && is_intel_anv) {
  441. const u32 version = (properties.properties.driverVersion << 3) >> 3;
  442. if (version >= VK_MAKE_API_VERSION(0, 22, 3, 0)) {
  443. // Disable VK_KHR_push_descriptor due to
  444. // mesa/mesa/-/commit/ff91c5ca42bc80aa411cb3fd8f550aa6fdd16bdc
  445. LOG_WARNING(Render_Vulkan,
  446. "ANV drivers 22.3.0 and later have broken VK_KHR_push_descriptor");
  447. extensions.push_descriptor = false;
  448. loaded_extensions.erase(VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
  449. }
  450. }
  451. if (is_mvk) {
  452. LOG_WARNING(Render_Vulkan,
  453. "MVK driver breaks when using more than 16 vertex attributes/bindings");
  454. properties.properties.limits.maxVertexInputAttributes =
  455. std::min(properties.properties.limits.maxVertexInputAttributes, 16U);
  456. properties.properties.limits.maxVertexInputBindings =
  457. std::min(properties.properties.limits.maxVertexInputBindings, 16U);
  458. }
  459. logical = vk::Device::Create(physical, queue_cis, ExtensionListForVulkan(loaded_extensions),
  460. first_next, dld);
  461. graphics_queue = logical.GetQueue(graphics_family);
  462. present_queue = logical.GetQueue(present_family);
  463. }
  464. Device::~Device() = default;
  465. VkFormat Device::GetSupportedFormat(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  466. FormatType format_type) const {
  467. if (IsFormatSupported(wanted_format, wanted_usage, format_type)) {
  468. return wanted_format;
  469. }
  470. // The wanted format is not supported by hardware, search for alternatives
  471. const VkFormat* alternatives = GetFormatAlternatives(wanted_format);
  472. if (alternatives == nullptr) {
  473. ASSERT_MSG(false,
  474. "Format={} with usage={} and type={} has no defined alternatives and host "
  475. "hardware does not support it",
  476. wanted_format, wanted_usage, format_type);
  477. return wanted_format;
  478. }
  479. std::size_t i = 0;
  480. for (VkFormat alternative = *alternatives; alternative; alternative = alternatives[++i]) {
  481. if (!IsFormatSupported(alternative, wanted_usage, format_type)) {
  482. continue;
  483. }
  484. LOG_DEBUG(Render_Vulkan,
  485. "Emulating format={} with alternative format={} with usage={} and type={}",
  486. wanted_format, alternative, wanted_usage, format_type);
  487. return alternative;
  488. }
  489. // No alternatives found, panic
  490. ASSERT_MSG(false,
  491. "Format={} with usage={} and type={} is not supported by the host hardware and "
  492. "doesn't support any of the alternatives",
  493. wanted_format, wanted_usage, format_type);
  494. return wanted_format;
  495. }
  496. void Device::ReportLoss() const {
  497. LOG_CRITICAL(Render_Vulkan, "Device loss occurred!");
  498. // Wait for the log to flush and for Nsight Aftermath to dump the results
  499. std::this_thread::sleep_for(std::chrono::seconds{15});
  500. }
  501. void Device::SaveShader(std::span<const u32> spirv) const {
  502. if (nsight_aftermath_tracker) {
  503. nsight_aftermath_tracker->SaveShader(spirv);
  504. }
  505. }
  506. bool Device::ComputeIsOptimalAstcSupported() const {
  507. // Disable for now to avoid converting ASTC twice.
  508. static constexpr std::array astc_formats = {
  509. VK_FORMAT_ASTC_4x4_UNORM_BLOCK, VK_FORMAT_ASTC_4x4_SRGB_BLOCK,
  510. VK_FORMAT_ASTC_5x4_UNORM_BLOCK, VK_FORMAT_ASTC_5x4_SRGB_BLOCK,
  511. VK_FORMAT_ASTC_5x5_UNORM_BLOCK, VK_FORMAT_ASTC_5x5_SRGB_BLOCK,
  512. VK_FORMAT_ASTC_6x5_UNORM_BLOCK, VK_FORMAT_ASTC_6x5_SRGB_BLOCK,
  513. VK_FORMAT_ASTC_6x6_UNORM_BLOCK, VK_FORMAT_ASTC_6x6_SRGB_BLOCK,
  514. VK_FORMAT_ASTC_8x5_UNORM_BLOCK, VK_FORMAT_ASTC_8x5_SRGB_BLOCK,
  515. VK_FORMAT_ASTC_8x6_UNORM_BLOCK, VK_FORMAT_ASTC_8x6_SRGB_BLOCK,
  516. VK_FORMAT_ASTC_8x8_UNORM_BLOCK, VK_FORMAT_ASTC_8x8_SRGB_BLOCK,
  517. VK_FORMAT_ASTC_10x5_UNORM_BLOCK, VK_FORMAT_ASTC_10x5_SRGB_BLOCK,
  518. VK_FORMAT_ASTC_10x6_UNORM_BLOCK, VK_FORMAT_ASTC_10x6_SRGB_BLOCK,
  519. VK_FORMAT_ASTC_10x8_UNORM_BLOCK, VK_FORMAT_ASTC_10x8_SRGB_BLOCK,
  520. VK_FORMAT_ASTC_10x10_UNORM_BLOCK, VK_FORMAT_ASTC_10x10_SRGB_BLOCK,
  521. VK_FORMAT_ASTC_12x10_UNORM_BLOCK, VK_FORMAT_ASTC_12x10_SRGB_BLOCK,
  522. VK_FORMAT_ASTC_12x12_UNORM_BLOCK, VK_FORMAT_ASTC_12x12_SRGB_BLOCK,
  523. };
  524. if (!features.features.textureCompressionASTC_LDR) {
  525. return false;
  526. }
  527. const auto format_feature_usage{
  528. VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_BLIT_SRC_BIT |
  529. VK_FORMAT_FEATURE_BLIT_DST_BIT | VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
  530. VK_FORMAT_FEATURE_TRANSFER_DST_BIT};
  531. for (const auto format : astc_formats) {
  532. const auto physical_format_properties{physical.GetFormatProperties(format)};
  533. if ((physical_format_properties.optimalTilingFeatures & format_feature_usage) == 0) {
  534. return false;
  535. }
  536. }
  537. return true;
  538. }
  539. bool Device::TestDepthStencilBlits() const {
  540. static constexpr VkFormatFeatureFlags required_features =
  541. VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
  542. const auto test_features = [](VkFormatProperties props) {
  543. return (props.optimalTilingFeatures & required_features) == required_features;
  544. };
  545. return test_features(format_properties.at(VK_FORMAT_D32_SFLOAT_S8_UINT)) &&
  546. test_features(format_properties.at(VK_FORMAT_D24_UNORM_S8_UINT));
  547. }
  548. bool Device::IsFormatSupported(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  549. FormatType format_type) const {
  550. const auto it = format_properties.find(wanted_format);
  551. if (it == format_properties.end()) {
  552. UNIMPLEMENTED_MSG("Unimplemented format query={}", wanted_format);
  553. return true;
  554. }
  555. const auto supported_usage = GetFormatFeatures(it->second, format_type);
  556. return (supported_usage & wanted_usage) == wanted_usage;
  557. }
  558. std::string Device::GetDriverName() const {
  559. switch (properties.driver.driverID) {
  560. case VK_DRIVER_ID_AMD_PROPRIETARY:
  561. return "AMD";
  562. case VK_DRIVER_ID_AMD_OPEN_SOURCE:
  563. return "AMDVLK";
  564. case VK_DRIVER_ID_MESA_RADV:
  565. return "RADV";
  566. case VK_DRIVER_ID_NVIDIA_PROPRIETARY:
  567. return "NVIDIA";
  568. case VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS:
  569. return "INTEL";
  570. case VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA:
  571. return "ANV";
  572. case VK_DRIVER_ID_MESA_LLVMPIPE:
  573. return "LAVAPIPE";
  574. default:
  575. return properties.driver.driverName;
  576. }
  577. }
  578. bool Device::ShouldBoostClocks() const {
  579. const auto driver_id = properties.driver.driverID;
  580. const auto vendor_id = properties.properties.vendorID;
  581. const auto device_id = properties.properties.deviceID;
  582. const bool validated_driver =
  583. driver_id == VK_DRIVER_ID_AMD_PROPRIETARY || driver_id == VK_DRIVER_ID_AMD_OPEN_SOURCE ||
  584. driver_id == VK_DRIVER_ID_MESA_RADV || driver_id == VK_DRIVER_ID_NVIDIA_PROPRIETARY ||
  585. driver_id == VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS ||
  586. driver_id == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA;
  587. const bool is_steam_deck = vendor_id == 0x1002 && device_id == 0x163F;
  588. const bool is_debugging = this->HasDebuggingToolAttached();
  589. return validated_driver && !is_steam_deck && !is_debugging;
  590. }
  591. bool Device::GetSuitability(bool requires_swapchain) {
  592. // Assume we will be suitable.
  593. bool suitable = true;
  594. // Configure properties.
  595. properties.properties = physical.GetProperties();
  596. // Set instance version.
  597. instance_version = properties.properties.apiVersion;
  598. // Minimum of API version 1.1 is required. (This is well-supported.)
  599. ASSERT(instance_version >= VK_API_VERSION_1_1);
  600. // Get available extensions.
  601. auto extension_properties = physical.EnumerateDeviceExtensionProperties();
  602. // Get the set of supported extensions.
  603. supported_extensions.clear();
  604. for (const VkExtensionProperties& property : extension_properties) {
  605. supported_extensions.insert(property.extensionName);
  606. }
  607. // Generate list of extensions to load.
  608. loaded_extensions.clear();
  609. #define EXTENSION(prefix, macro_name, var_name) \
  610. if (supported_extensions.contains(VK_##prefix##_##macro_name##_EXTENSION_NAME)) { \
  611. loaded_extensions.insert(VK_##prefix##_##macro_name##_EXTENSION_NAME); \
  612. extensions.var_name = true; \
  613. }
  614. #define FEATURE_EXTENSION(prefix, struct_name, macro_name, var_name) \
  615. if (supported_extensions.contains(VK_##prefix##_##macro_name##_EXTENSION_NAME)) { \
  616. loaded_extensions.insert(VK_##prefix##_##macro_name##_EXTENSION_NAME); \
  617. extensions.var_name = true; \
  618. }
  619. if (instance_version < VK_API_VERSION_1_2) {
  620. FOR_EACH_VK_FEATURE_1_2(FEATURE_EXTENSION);
  621. }
  622. if (instance_version < VK_API_VERSION_1_3) {
  623. FOR_EACH_VK_FEATURE_1_3(FEATURE_EXTENSION);
  624. }
  625. FOR_EACH_VK_FEATURE_EXT(FEATURE_EXTENSION);
  626. FOR_EACH_VK_EXTENSION(EXTENSION);
  627. #ifdef _WIN32
  628. FOR_EACH_VK_EXTENSION_WIN32(EXTENSION);
  629. #endif
  630. #undef FEATURE_EXTENSION
  631. #undef EXTENSION
  632. // Some extensions are mandatory. Check those.
  633. #define CHECK_EXTENSION(extension_name) \
  634. if (!loaded_extensions.contains(extension_name)) { \
  635. LOG_ERROR(Render_Vulkan, "Missing required extension {}", extension_name); \
  636. suitable = false; \
  637. }
  638. #define LOG_EXTENSION(extension_name) \
  639. if (!loaded_extensions.contains(extension_name)) { \
  640. LOG_INFO(Render_Vulkan, "Device doesn't support extension {}", extension_name); \
  641. }
  642. FOR_EACH_VK_RECOMMENDED_EXTENSION(LOG_EXTENSION);
  643. FOR_EACH_VK_MANDATORY_EXTENSION(CHECK_EXTENSION);
  644. #ifdef _WIN32
  645. FOR_EACH_VK_MANDATORY_EXTENSION_WIN32(CHECK_EXTENSION);
  646. #else
  647. FOR_EACH_VK_MANDATORY_EXTENSION_GENERIC(CHECK_EXTENSION);
  648. #endif
  649. if (requires_swapchain) {
  650. CHECK_EXTENSION(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
  651. }
  652. #undef LOG_EXTENSION
  653. #undef CHECK_EXTENSION
  654. // Generate the linked list of features to test.
  655. features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  656. // Set next pointer.
  657. void** next = &features2.pNext;
  658. // Test all features we know about. If the feature is not available in core at our
  659. // current API version, and was not enabled by an extension, skip testing the feature.
  660. // We set the structure sType explicitly here as it is zeroed by the constructor.
  661. #define FEATURE(prefix, struct_name, macro_name, var_name) \
  662. features.var_name.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_##macro_name##_FEATURES; \
  663. SetNext(next, features.var_name);
  664. #define EXT_FEATURE(prefix, struct_name, macro_name, var_name) \
  665. if (extensions.var_name) { \
  666. features.var_name.sType = \
  667. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_##macro_name##_FEATURES_##prefix; \
  668. SetNext(next, features.var_name); \
  669. }
  670. FOR_EACH_VK_FEATURE_1_1(FEATURE);
  671. FOR_EACH_VK_FEATURE_EXT(EXT_FEATURE);
  672. if (instance_version >= VK_API_VERSION_1_2) {
  673. FOR_EACH_VK_FEATURE_1_2(FEATURE);
  674. } else {
  675. FOR_EACH_VK_FEATURE_1_2(EXT_FEATURE);
  676. }
  677. if (instance_version >= VK_API_VERSION_1_3) {
  678. FOR_EACH_VK_FEATURE_1_3(FEATURE);
  679. } else {
  680. FOR_EACH_VK_FEATURE_1_3(EXT_FEATURE);
  681. }
  682. #undef EXT_FEATURE
  683. #undef FEATURE
  684. // Perform the feature test.
  685. physical.GetFeatures2(features2);
  686. features.features = features2.features;
  687. // Some features are mandatory. Check those.
  688. #define CHECK_FEATURE(feature, name) \
  689. if (!features.feature.name) { \
  690. LOG_ERROR(Render_Vulkan, "Missing required feature {}", #name); \
  691. suitable = false; \
  692. }
  693. #define LOG_FEATURE(feature, name) \
  694. if (!features.feature.name) { \
  695. LOG_INFO(Render_Vulkan, "Device doesn't support feature {}", #name); \
  696. }
  697. FOR_EACH_VK_RECOMMENDED_FEATURE(LOG_FEATURE);
  698. FOR_EACH_VK_MANDATORY_FEATURE(CHECK_FEATURE);
  699. #undef LOG_FEATURE
  700. #undef CHECK_FEATURE
  701. // Generate linked list of properties.
  702. properties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
  703. // Set next pointer.
  704. next = &properties2.pNext;
  705. // Get driver info.
  706. properties.driver.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES;
  707. SetNext(next, properties.driver);
  708. // Retrieve relevant extension properties.
  709. if (extensions.shader_float_controls) {
  710. properties.float_controls.sType =
  711. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT_CONTROLS_PROPERTIES;
  712. SetNext(next, properties.float_controls);
  713. }
  714. if (extensions.push_descriptor) {
  715. properties.push_descriptor.sType =
  716. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR;
  717. SetNext(next, properties.push_descriptor);
  718. }
  719. if (extensions.subgroup_size_control) {
  720. properties.subgroup_size_control.sType =
  721. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_PROPERTIES;
  722. SetNext(next, properties.subgroup_size_control);
  723. }
  724. if (extensions.transform_feedback) {
  725. properties.transform_feedback.sType =
  726. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT;
  727. SetNext(next, properties.transform_feedback);
  728. }
  729. // Perform the property fetch.
  730. physical.GetProperties2(properties2);
  731. properties.properties = properties2.properties;
  732. // Unload extensions if feature support is insufficient.
  733. RemoveUnsuitableExtensions();
  734. // Check limits.
  735. struct Limit {
  736. u32 minimum;
  737. u32 value;
  738. const char* name;
  739. };
  740. const VkPhysicalDeviceLimits& limits{properties.properties.limits};
  741. const std::array limits_report{
  742. Limit{65536, limits.maxUniformBufferRange, "maxUniformBufferRange"},
  743. Limit{16, limits.maxViewports, "maxViewports"},
  744. Limit{8, limits.maxColorAttachments, "maxColorAttachments"},
  745. Limit{8, limits.maxClipDistances, "maxClipDistances"},
  746. };
  747. for (const auto& [min, value, name] : limits_report) {
  748. if (value < min) {
  749. LOG_ERROR(Render_Vulkan, "{} has to be {} or greater but it is {}", name, min, value);
  750. suitable = false;
  751. }
  752. }
  753. // Return whether we were suitable.
  754. return suitable;
  755. }
  756. void Device::RemoveExtensionIfUnsuitable(bool is_suitable, const std::string& extension_name) {
  757. if (loaded_extensions.contains(extension_name) && !is_suitable) {
  758. LOG_WARNING(Render_Vulkan, "Removing unsuitable extension {}", extension_name);
  759. loaded_extensions.erase(extension_name);
  760. }
  761. }
  762. void Device::RemoveUnsuitableExtensions() {
  763. // VK_EXT_custom_border_color
  764. extensions.custom_border_color = features.custom_border_color.customBorderColors &&
  765. features.custom_border_color.customBorderColorWithoutFormat;
  766. RemoveExtensionIfUnsuitable(extensions.custom_border_color,
  767. VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME);
  768. // VK_EXT_depth_clip_control
  769. extensions.depth_clip_control = features.depth_clip_control.depthClipControl;
  770. RemoveExtensionIfUnsuitable(extensions.depth_clip_control,
  771. VK_EXT_DEPTH_CLIP_CONTROL_EXTENSION_NAME);
  772. // VK_EXT_extended_dynamic_state
  773. extensions.extended_dynamic_state = features.extended_dynamic_state.extendedDynamicState;
  774. RemoveExtensionIfUnsuitable(extensions.extended_dynamic_state,
  775. VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
  776. // VK_EXT_extended_dynamic_state2
  777. extensions.extended_dynamic_state2 = features.extended_dynamic_state2.extendedDynamicState2;
  778. RemoveExtensionIfUnsuitable(extensions.extended_dynamic_state2,
  779. VK_EXT_EXTENDED_DYNAMIC_STATE_2_EXTENSION_NAME);
  780. // VK_EXT_extended_dynamic_state3
  781. dynamic_state3_blending =
  782. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEnable &&
  783. features.extended_dynamic_state3.extendedDynamicState3ColorBlendEquation &&
  784. features.extended_dynamic_state3.extendedDynamicState3ColorWriteMask;
  785. dynamic_state3_enables =
  786. features.extended_dynamic_state3.extendedDynamicState3DepthClampEnable &&
  787. features.extended_dynamic_state3.extendedDynamicState3LogicOpEnable;
  788. extensions.extended_dynamic_state3 = dynamic_state3_blending || dynamic_state3_enables;
  789. dynamic_state3_blending = dynamic_state3_blending && extensions.extended_dynamic_state3;
  790. dynamic_state3_enables = dynamic_state3_enables && extensions.extended_dynamic_state3;
  791. RemoveExtensionIfUnsuitable(extensions.extended_dynamic_state3,
  792. VK_EXT_EXTENDED_DYNAMIC_STATE_3_EXTENSION_NAME);
  793. // VK_EXT_provoking_vertex
  794. extensions.provoking_vertex =
  795. features.provoking_vertex.provokingVertexLast &&
  796. features.provoking_vertex.transformFeedbackPreservesProvokingVertex;
  797. RemoveExtensionIfUnsuitable(extensions.provoking_vertex,
  798. VK_EXT_PROVOKING_VERTEX_EXTENSION_NAME);
  799. // VK_KHR_shader_atomic_int64
  800. extensions.shader_atomic_int64 = features.shader_atomic_int64.shaderBufferInt64Atomics &&
  801. features.shader_atomic_int64.shaderSharedInt64Atomics;
  802. RemoveExtensionIfUnsuitable(extensions.shader_atomic_int64,
  803. VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME);
  804. // VK_EXT_shader_demote_to_helper_invocation
  805. extensions.shader_demote_to_helper_invocation =
  806. features.shader_demote_to_helper_invocation.shaderDemoteToHelperInvocation;
  807. RemoveExtensionIfUnsuitable(extensions.shader_demote_to_helper_invocation,
  808. VK_EXT_SHADER_DEMOTE_TO_HELPER_INVOCATION_EXTENSION_NAME);
  809. // VK_EXT_subgroup_size_control
  810. extensions.subgroup_size_control =
  811. features.subgroup_size_control.subgroupSizeControl &&
  812. properties.subgroup_size_control.minSubgroupSize <= GuestWarpSize &&
  813. properties.subgroup_size_control.maxSubgroupSize >= GuestWarpSize;
  814. RemoveExtensionIfUnsuitable(extensions.subgroup_size_control,
  815. VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME);
  816. // VK_EXT_transform_feedback
  817. extensions.transform_feedback =
  818. features.transform_feedback.transformFeedback &&
  819. features.transform_feedback.geometryStreams &&
  820. properties.transform_feedback.maxTransformFeedbackStreams >= 4 &&
  821. properties.transform_feedback.maxTransformFeedbackBuffers > 0 &&
  822. properties.transform_feedback.transformFeedbackQueries &&
  823. properties.transform_feedback.transformFeedbackDraw;
  824. RemoveExtensionIfUnsuitable(extensions.transform_feedback,
  825. VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME);
  826. // VK_EXT_vertex_input_dynamic_state
  827. extensions.vertex_input_dynamic_state =
  828. features.vertex_input_dynamic_state.vertexInputDynamicState;
  829. RemoveExtensionIfUnsuitable(extensions.vertex_input_dynamic_state,
  830. VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
  831. // VK_KHR_pipeline_executable_properties
  832. if (Settings::values.renderer_shader_feedback.GetValue()) {
  833. extensions.pipeline_executable_properties =
  834. features.pipeline_executable_properties.pipelineExecutableInfo;
  835. RemoveExtensionIfUnsuitable(extensions.pipeline_executable_properties,
  836. VK_KHR_PIPELINE_EXECUTABLE_PROPERTIES_EXTENSION_NAME);
  837. } else {
  838. extensions.pipeline_executable_properties = false;
  839. loaded_extensions.erase(VK_KHR_PIPELINE_EXECUTABLE_PROPERTIES_EXTENSION_NAME);
  840. }
  841. // VK_KHR_workgroup_memory_explicit_layout
  842. extensions.workgroup_memory_explicit_layout =
  843. features.features.shaderInt16 &&
  844. features.workgroup_memory_explicit_layout.workgroupMemoryExplicitLayout &&
  845. features.workgroup_memory_explicit_layout.workgroupMemoryExplicitLayout8BitAccess &&
  846. features.workgroup_memory_explicit_layout.workgroupMemoryExplicitLayout16BitAccess &&
  847. features.workgroup_memory_explicit_layout.workgroupMemoryExplicitLayoutScalarBlockLayout;
  848. RemoveExtensionIfUnsuitable(extensions.workgroup_memory_explicit_layout,
  849. VK_KHR_WORKGROUP_MEMORY_EXPLICIT_LAYOUT_EXTENSION_NAME);
  850. }
  851. void Device::SetupFamilies(VkSurfaceKHR surface) {
  852. const std::vector queue_family_properties = physical.GetQueueFamilyProperties();
  853. std::optional<u32> graphics;
  854. std::optional<u32> present;
  855. for (u32 index = 0; index < static_cast<u32>(queue_family_properties.size()); ++index) {
  856. if (graphics && (present || !surface)) {
  857. break;
  858. }
  859. const VkQueueFamilyProperties& queue_family = queue_family_properties[index];
  860. if (queue_family.queueCount == 0) {
  861. continue;
  862. }
  863. if (queue_family.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
  864. graphics = index;
  865. }
  866. if (surface && physical.GetSurfaceSupportKHR(index, surface)) {
  867. present = index;
  868. }
  869. }
  870. if (!graphics) {
  871. LOG_ERROR(Render_Vulkan, "Device lacks a graphics queue");
  872. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  873. }
  874. if (surface && !present) {
  875. LOG_ERROR(Render_Vulkan, "Device lacks a present queue");
  876. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  877. }
  878. if (graphics) {
  879. graphics_family = *graphics;
  880. }
  881. if (present) {
  882. present_family = *present;
  883. }
  884. }
  885. u64 Device::GetDeviceMemoryUsage() const {
  886. VkPhysicalDeviceMemoryBudgetPropertiesEXT budget;
  887. budget.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT;
  888. budget.pNext = nullptr;
  889. physical.GetMemoryProperties(&budget);
  890. u64 result{};
  891. for (const size_t heap : valid_heap_memory) {
  892. result += budget.heapUsage[heap];
  893. }
  894. return result;
  895. }
  896. void Device::CollectPhysicalMemoryInfo() {
  897. // Account for resolution scaling in memory limits
  898. const size_t normal_memory = 6_GiB;
  899. const size_t scaler_memory = 1_GiB * Settings::values.resolution_info.ScaleUp(1);
  900. // Calculate limits using memory budget
  901. VkPhysicalDeviceMemoryBudgetPropertiesEXT budget{};
  902. budget.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT;
  903. const auto mem_info =
  904. physical.GetMemoryProperties(extensions.memory_budget ? &budget : nullptr);
  905. const auto& mem_properties = mem_info.memoryProperties;
  906. const size_t num_properties = mem_properties.memoryHeapCount;
  907. device_access_memory = 0;
  908. u64 device_initial_usage = 0;
  909. u64 local_memory = 0;
  910. for (size_t element = 0; element < num_properties; ++element) {
  911. const bool is_heap_local =
  912. (mem_properties.memoryHeaps[element].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) != 0;
  913. if (!is_integrated && !is_heap_local) {
  914. continue;
  915. }
  916. valid_heap_memory.push_back(element);
  917. if (is_heap_local) {
  918. local_memory += mem_properties.memoryHeaps[element].size;
  919. }
  920. if (extensions.memory_budget) {
  921. device_initial_usage += budget.heapUsage[element];
  922. device_access_memory += budget.heapBudget[element];
  923. continue;
  924. }
  925. device_access_memory += mem_properties.memoryHeaps[element].size;
  926. }
  927. if (!is_integrated) {
  928. const u64 reserve_memory = std::min<u64>(device_access_memory / 8, 1_GiB);
  929. device_access_memory -= reserve_memory;
  930. device_access_memory = std::min<u64>(device_access_memory, normal_memory + scaler_memory);
  931. return;
  932. }
  933. const s64 available_memory = static_cast<s64>(device_access_memory - device_initial_usage);
  934. device_access_memory = static_cast<u64>(std::max<s64>(
  935. std::min<s64>(available_memory - 8_GiB, 4_GiB), std::min<s64>(local_memory, 4_GiB)));
  936. }
  937. void Device::CollectToolingInfo() {
  938. if (!extensions.tooling_info) {
  939. return;
  940. }
  941. auto tools{physical.GetPhysicalDeviceToolProperties()};
  942. for (const VkPhysicalDeviceToolProperties& tool : tools) {
  943. const std::string_view name = tool.name;
  944. LOG_INFO(Render_Vulkan, "Attached debugging tool: {}", name);
  945. has_renderdoc = has_renderdoc || name == "RenderDoc";
  946. has_nsight_graphics = has_nsight_graphics || name == "NVIDIA Nsight Graphics";
  947. }
  948. }
  949. std::vector<VkDeviceQueueCreateInfo> Device::GetDeviceQueueCreateInfos() const {
  950. static constexpr float QUEUE_PRIORITY = 1.0f;
  951. std::unordered_set<u32> unique_queue_families{graphics_family, present_family};
  952. std::vector<VkDeviceQueueCreateInfo> queue_cis;
  953. queue_cis.reserve(unique_queue_families.size());
  954. for (const u32 queue_family : unique_queue_families) {
  955. auto& ci = queue_cis.emplace_back(VkDeviceQueueCreateInfo{
  956. .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
  957. .pNext = nullptr,
  958. .flags = 0,
  959. .queueFamilyIndex = queue_family,
  960. .queueCount = 1,
  961. .pQueuePriorities = nullptr,
  962. });
  963. ci.pQueuePriorities = &QUEUE_PRIORITY;
  964. }
  965. return queue_cis;
  966. }
  967. } // namespace Vulkan