vulkan_device.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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. constexpr std::array REQUIRED_EXTENSIONS{
  65. VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME,
  66. VK_EXT_ROBUSTNESS_2_EXTENSION_NAME,
  67. #ifdef _WIN32
  68. VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME,
  69. #endif
  70. #ifdef __unix__
  71. VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
  72. #endif
  73. };
  74. constexpr std::array REQUIRED_EXTENSIONS_BEFORE_1_2{
  75. VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME,
  76. VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME,
  77. VK_KHR_8BIT_STORAGE_EXTENSION_NAME,
  78. VK_KHR_SHADER_FLOAT_CONTROLS_EXTENSION_NAME,
  79. VK_KHR_SAMPLER_MIRROR_CLAMP_TO_EDGE_EXTENSION_NAME,
  80. VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME,
  81. };
  82. constexpr std::array REQUIRED_EXTENSIONS_BEFORE_1_3{
  83. VK_EXT_SHADER_DEMOTE_TO_HELPER_INVOCATION_EXTENSION_NAME,
  84. };
  85. template <typename T>
  86. void SetNext(void**& next, T& data) {
  87. *next = &data;
  88. next = &data.pNext;
  89. }
  90. constexpr const VkFormat* GetFormatAlternatives(VkFormat format) {
  91. switch (format) {
  92. case VK_FORMAT_S8_UINT:
  93. return Alternatives::STENCIL8_UINT.data();
  94. case VK_FORMAT_D24_UNORM_S8_UINT:
  95. return Alternatives::DEPTH24_UNORM_STENCIL8_UINT.data();
  96. case VK_FORMAT_D16_UNORM_S8_UINT:
  97. return Alternatives::DEPTH16_UNORM_STENCIL8_UINT.data();
  98. case VK_FORMAT_B5G6R5_UNORM_PACK16:
  99. return Alternatives::B5G6R5_UNORM_PACK16.data();
  100. case VK_FORMAT_R4G4_UNORM_PACK8:
  101. return Alternatives::R4G4_UNORM_PACK8.data();
  102. case VK_FORMAT_R16G16B16_SFLOAT:
  103. return Alternatives::R16G16B16_SFLOAT.data();
  104. case VK_FORMAT_R16G16B16_SSCALED:
  105. return Alternatives::R16G16B16_SSCALED.data();
  106. case VK_FORMAT_R8G8B8_SSCALED:
  107. return Alternatives::R8G8B8_SSCALED.data();
  108. default:
  109. return nullptr;
  110. }
  111. }
  112. VkFormatFeatureFlags GetFormatFeatures(VkFormatProperties properties, FormatType format_type) {
  113. switch (format_type) {
  114. case FormatType::Linear:
  115. return properties.linearTilingFeatures;
  116. case FormatType::Optimal:
  117. return properties.optimalTilingFeatures;
  118. case FormatType::Buffer:
  119. return properties.bufferFeatures;
  120. default:
  121. return {};
  122. }
  123. }
  124. std::unordered_map<VkFormat, VkFormatProperties> GetFormatProperties(vk::PhysicalDevice physical) {
  125. static constexpr std::array formats{
  126. VK_FORMAT_A1R5G5B5_UNORM_PACK16,
  127. VK_FORMAT_A2B10G10R10_SINT_PACK32,
  128. VK_FORMAT_A2B10G10R10_SNORM_PACK32,
  129. VK_FORMAT_A2B10G10R10_SSCALED_PACK32,
  130. VK_FORMAT_A2B10G10R10_UINT_PACK32,
  131. VK_FORMAT_A2B10G10R10_UNORM_PACK32,
  132. VK_FORMAT_A2B10G10R10_USCALED_PACK32,
  133. VK_FORMAT_A8B8G8R8_SINT_PACK32,
  134. VK_FORMAT_A8B8G8R8_SNORM_PACK32,
  135. VK_FORMAT_A8B8G8R8_SRGB_PACK32,
  136. VK_FORMAT_A8B8G8R8_UINT_PACK32,
  137. VK_FORMAT_A8B8G8R8_UNORM_PACK32,
  138. VK_FORMAT_ASTC_10x10_SRGB_BLOCK,
  139. VK_FORMAT_ASTC_10x10_UNORM_BLOCK,
  140. VK_FORMAT_ASTC_10x5_SRGB_BLOCK,
  141. VK_FORMAT_ASTC_10x5_UNORM_BLOCK,
  142. VK_FORMAT_ASTC_10x6_SRGB_BLOCK,
  143. VK_FORMAT_ASTC_10x6_UNORM_BLOCK,
  144. VK_FORMAT_ASTC_10x8_SRGB_BLOCK,
  145. VK_FORMAT_ASTC_10x8_UNORM_BLOCK,
  146. VK_FORMAT_ASTC_12x10_SRGB_BLOCK,
  147. VK_FORMAT_ASTC_12x10_UNORM_BLOCK,
  148. VK_FORMAT_ASTC_12x12_SRGB_BLOCK,
  149. VK_FORMAT_ASTC_12x12_UNORM_BLOCK,
  150. VK_FORMAT_ASTC_4x4_SRGB_BLOCK,
  151. VK_FORMAT_ASTC_4x4_UNORM_BLOCK,
  152. VK_FORMAT_ASTC_5x4_SRGB_BLOCK,
  153. VK_FORMAT_ASTC_5x4_UNORM_BLOCK,
  154. VK_FORMAT_ASTC_5x5_SRGB_BLOCK,
  155. VK_FORMAT_ASTC_5x5_UNORM_BLOCK,
  156. VK_FORMAT_ASTC_6x5_SRGB_BLOCK,
  157. VK_FORMAT_ASTC_6x5_UNORM_BLOCK,
  158. VK_FORMAT_ASTC_6x6_SRGB_BLOCK,
  159. VK_FORMAT_ASTC_6x6_UNORM_BLOCK,
  160. VK_FORMAT_ASTC_8x5_SRGB_BLOCK,
  161. VK_FORMAT_ASTC_8x5_UNORM_BLOCK,
  162. VK_FORMAT_ASTC_8x6_SRGB_BLOCK,
  163. VK_FORMAT_ASTC_8x6_UNORM_BLOCK,
  164. VK_FORMAT_ASTC_8x8_SRGB_BLOCK,
  165. VK_FORMAT_ASTC_8x8_UNORM_BLOCK,
  166. VK_FORMAT_B10G11R11_UFLOAT_PACK32,
  167. VK_FORMAT_B4G4R4A4_UNORM_PACK16,
  168. VK_FORMAT_B5G5R5A1_UNORM_PACK16,
  169. VK_FORMAT_B5G6R5_UNORM_PACK16,
  170. VK_FORMAT_B8G8R8A8_SRGB,
  171. VK_FORMAT_B8G8R8A8_UNORM,
  172. VK_FORMAT_BC1_RGBA_SRGB_BLOCK,
  173. VK_FORMAT_BC1_RGBA_UNORM_BLOCK,
  174. VK_FORMAT_BC2_SRGB_BLOCK,
  175. VK_FORMAT_BC2_UNORM_BLOCK,
  176. VK_FORMAT_BC3_SRGB_BLOCK,
  177. VK_FORMAT_BC3_UNORM_BLOCK,
  178. VK_FORMAT_BC4_SNORM_BLOCK,
  179. VK_FORMAT_BC4_UNORM_BLOCK,
  180. VK_FORMAT_BC5_SNORM_BLOCK,
  181. VK_FORMAT_BC5_UNORM_BLOCK,
  182. VK_FORMAT_BC6H_SFLOAT_BLOCK,
  183. VK_FORMAT_BC6H_UFLOAT_BLOCK,
  184. VK_FORMAT_BC7_SRGB_BLOCK,
  185. VK_FORMAT_BC7_UNORM_BLOCK,
  186. VK_FORMAT_D16_UNORM,
  187. VK_FORMAT_D16_UNORM_S8_UINT,
  188. VK_FORMAT_D24_UNORM_S8_UINT,
  189. VK_FORMAT_D32_SFLOAT,
  190. VK_FORMAT_D32_SFLOAT_S8_UINT,
  191. VK_FORMAT_E5B9G9R9_UFLOAT_PACK32,
  192. VK_FORMAT_R16G16B16A16_SFLOAT,
  193. VK_FORMAT_R16G16B16A16_SINT,
  194. VK_FORMAT_R16G16B16A16_SNORM,
  195. VK_FORMAT_R16G16B16A16_SSCALED,
  196. VK_FORMAT_R16G16B16A16_UINT,
  197. VK_FORMAT_R16G16B16A16_UNORM,
  198. VK_FORMAT_R16G16B16A16_USCALED,
  199. VK_FORMAT_R16G16B16_SFLOAT,
  200. VK_FORMAT_R16G16B16_SINT,
  201. VK_FORMAT_R16G16B16_SNORM,
  202. VK_FORMAT_R16G16B16_SSCALED,
  203. VK_FORMAT_R16G16B16_UINT,
  204. VK_FORMAT_R16G16B16_UNORM,
  205. VK_FORMAT_R16G16B16_USCALED,
  206. VK_FORMAT_R16G16_SFLOAT,
  207. VK_FORMAT_R16G16_SINT,
  208. VK_FORMAT_R16G16_SNORM,
  209. VK_FORMAT_R16G16_SSCALED,
  210. VK_FORMAT_R16G16_UINT,
  211. VK_FORMAT_R16G16_UNORM,
  212. VK_FORMAT_R16G16_USCALED,
  213. VK_FORMAT_R16_SFLOAT,
  214. VK_FORMAT_R16_SINT,
  215. VK_FORMAT_R16_SNORM,
  216. VK_FORMAT_R16_SSCALED,
  217. VK_FORMAT_R16_UINT,
  218. VK_FORMAT_R16_UNORM,
  219. VK_FORMAT_R16_USCALED,
  220. VK_FORMAT_R32G32B32A32_SFLOAT,
  221. VK_FORMAT_R32G32B32A32_SINT,
  222. VK_FORMAT_R32G32B32A32_UINT,
  223. VK_FORMAT_R32G32B32_SFLOAT,
  224. VK_FORMAT_R32G32B32_SINT,
  225. VK_FORMAT_R32G32B32_UINT,
  226. VK_FORMAT_R32G32_SFLOAT,
  227. VK_FORMAT_R32G32_SINT,
  228. VK_FORMAT_R32G32_UINT,
  229. VK_FORMAT_R32_SFLOAT,
  230. VK_FORMAT_R32_SINT,
  231. VK_FORMAT_R32_UINT,
  232. VK_FORMAT_R4G4B4A4_UNORM_PACK16,
  233. VK_FORMAT_R4G4_UNORM_PACK8,
  234. VK_FORMAT_R5G5B5A1_UNORM_PACK16,
  235. VK_FORMAT_R5G6B5_UNORM_PACK16,
  236. VK_FORMAT_R8G8B8A8_SINT,
  237. VK_FORMAT_R8G8B8A8_SNORM,
  238. VK_FORMAT_R8G8B8A8_SRGB,
  239. VK_FORMAT_R8G8B8A8_SSCALED,
  240. VK_FORMAT_R8G8B8A8_UINT,
  241. VK_FORMAT_R8G8B8A8_UNORM,
  242. VK_FORMAT_R8G8B8A8_USCALED,
  243. VK_FORMAT_R8G8B8_SINT,
  244. VK_FORMAT_R8G8B8_SNORM,
  245. VK_FORMAT_R8G8B8_SSCALED,
  246. VK_FORMAT_R8G8B8_UINT,
  247. VK_FORMAT_R8G8B8_UNORM,
  248. VK_FORMAT_R8G8B8_USCALED,
  249. VK_FORMAT_R8G8_SINT,
  250. VK_FORMAT_R8G8_SNORM,
  251. VK_FORMAT_R8G8_SSCALED,
  252. VK_FORMAT_R8G8_UINT,
  253. VK_FORMAT_R8G8_UNORM,
  254. VK_FORMAT_R8G8_USCALED,
  255. VK_FORMAT_R8_SINT,
  256. VK_FORMAT_R8_SNORM,
  257. VK_FORMAT_R8_SSCALED,
  258. VK_FORMAT_R8_UINT,
  259. VK_FORMAT_R8_UNORM,
  260. VK_FORMAT_R8_USCALED,
  261. VK_FORMAT_S8_UINT,
  262. };
  263. std::unordered_map<VkFormat, VkFormatProperties> format_properties;
  264. for (const auto format : formats) {
  265. format_properties.emplace(format, physical.GetFormatProperties(format));
  266. }
  267. return format_properties;
  268. }
  269. std::vector<std::string> GetSupportedExtensions(vk::PhysicalDevice physical) {
  270. const std::vector extensions = physical.EnumerateDeviceExtensionProperties();
  271. std::vector<std::string> supported_extensions;
  272. supported_extensions.reserve(extensions.size());
  273. for (const auto& extension : extensions) {
  274. supported_extensions.emplace_back(extension.extensionName);
  275. }
  276. return supported_extensions;
  277. }
  278. bool IsExtensionSupported(std::span<const std::string> supported_extensions,
  279. std::string_view extension) {
  280. return std::ranges::find(supported_extensions, extension) != supported_extensions.end();
  281. }
  282. NvidiaArchitecture GetNvidiaArchitecture(vk::PhysicalDevice physical,
  283. std::span<const std::string> exts) {
  284. if (IsExtensionSupported(exts, VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME)) {
  285. VkPhysicalDeviceFragmentShadingRatePropertiesKHR shading_rate_props{};
  286. shading_rate_props.sType =
  287. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADING_RATE_PROPERTIES_KHR;
  288. VkPhysicalDeviceProperties2 physical_properties{};
  289. physical_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
  290. physical_properties.pNext = &shading_rate_props;
  291. physical.GetProperties2(physical_properties);
  292. if (shading_rate_props.primitiveFragmentShadingRateWithMultipleViewports) {
  293. // Only Ampere and newer support this feature
  294. return NvidiaArchitecture::AmpereOrNewer;
  295. }
  296. }
  297. if (IsExtensionSupported(exts, VK_NV_SHADING_RATE_IMAGE_EXTENSION_NAME)) {
  298. return NvidiaArchitecture::Turing;
  299. }
  300. return NvidiaArchitecture::VoltaOrOlder;
  301. }
  302. } // Anonymous namespace
  303. Device::Device(VkInstance instance_, vk::PhysicalDevice physical_, VkSurfaceKHR surface,
  304. const vk::InstanceDispatch& dld_)
  305. : instance{instance_}, dld{dld_}, physical{physical_}, properties{physical.GetProperties()},
  306. instance_version{properties.apiVersion}, supported_extensions{GetSupportedExtensions(
  307. physical)},
  308. format_properties(GetFormatProperties(physical)) {
  309. CheckSuitability(surface != nullptr);
  310. SetupFamilies(surface);
  311. SetupFeatures();
  312. SetupProperties();
  313. const auto queue_cis = GetDeviceQueueCreateInfos();
  314. const std::vector extensions = LoadExtensions(surface != nullptr);
  315. VkPhysicalDeviceFeatures2 features2{
  316. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
  317. .pNext = nullptr,
  318. .features{
  319. .robustBufferAccess = true,
  320. .fullDrawIndexUint32 = false,
  321. .imageCubeArray = true,
  322. .independentBlend = true,
  323. .geometryShader = true,
  324. .tessellationShader = true,
  325. .sampleRateShading = true,
  326. .dualSrcBlend = true,
  327. .logicOp = true,
  328. .multiDrawIndirect = false,
  329. .drawIndirectFirstInstance = false,
  330. .depthClamp = true,
  331. .depthBiasClamp = true,
  332. .fillModeNonSolid = true,
  333. .depthBounds = is_depth_bounds_supported,
  334. .wideLines = true,
  335. .largePoints = true,
  336. .alphaToOne = false,
  337. .multiViewport = true,
  338. .samplerAnisotropy = true,
  339. .textureCompressionETC2 = false,
  340. .textureCompressionASTC_LDR = is_optimal_astc_supported,
  341. .textureCompressionBC = false,
  342. .occlusionQueryPrecise = true,
  343. .pipelineStatisticsQuery = false,
  344. .vertexPipelineStoresAndAtomics = true,
  345. .fragmentStoresAndAtomics = true,
  346. .shaderTessellationAndGeometryPointSize = false,
  347. .shaderImageGatherExtended = true,
  348. .shaderStorageImageExtendedFormats = false,
  349. .shaderStorageImageMultisample = is_shader_storage_image_multisample,
  350. .shaderStorageImageReadWithoutFormat = is_formatless_image_load_supported,
  351. .shaderStorageImageWriteWithoutFormat = true,
  352. .shaderUniformBufferArrayDynamicIndexing = false,
  353. .shaderSampledImageArrayDynamicIndexing = false,
  354. .shaderStorageBufferArrayDynamicIndexing = false,
  355. .shaderStorageImageArrayDynamicIndexing = false,
  356. .shaderClipDistance = true,
  357. .shaderCullDistance = true,
  358. .shaderFloat64 = is_shader_float64_supported,
  359. .shaderInt64 = is_shader_int64_supported,
  360. .shaderInt16 = is_shader_int16_supported,
  361. .shaderResourceResidency = false,
  362. .shaderResourceMinLod = false,
  363. .sparseBinding = false,
  364. .sparseResidencyBuffer = false,
  365. .sparseResidencyImage2D = false,
  366. .sparseResidencyImage3D = false,
  367. .sparseResidency2Samples = false,
  368. .sparseResidency4Samples = false,
  369. .sparseResidency8Samples = false,
  370. .sparseResidency16Samples = false,
  371. .sparseResidencyAliased = false,
  372. .variableMultisampleRate = false,
  373. .inheritedQueries = false,
  374. },
  375. };
  376. const void* first_next = &features2;
  377. void** next = &features2.pNext;
  378. VkPhysicalDeviceTimelineSemaphoreFeatures timeline_semaphore{
  379. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_FEATURES,
  380. .pNext = nullptr,
  381. .timelineSemaphore = true,
  382. };
  383. SetNext(next, timeline_semaphore);
  384. VkPhysicalDevice16BitStorageFeatures bit16_storage{
  385. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES,
  386. .pNext = nullptr,
  387. .storageBuffer16BitAccess = true,
  388. .uniformAndStorageBuffer16BitAccess = true,
  389. .storagePushConstant16 = false,
  390. .storageInputOutput16 = false,
  391. };
  392. SetNext(next, bit16_storage);
  393. VkPhysicalDevice8BitStorageFeatures bit8_storage{
  394. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES,
  395. .pNext = nullptr,
  396. .storageBuffer8BitAccess = false,
  397. .uniformAndStorageBuffer8BitAccess = true,
  398. .storagePushConstant8 = false,
  399. };
  400. SetNext(next, bit8_storage);
  401. VkPhysicalDeviceRobustness2FeaturesEXT robustness2{
  402. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT,
  403. .pNext = nullptr,
  404. .robustBufferAccess2 = true,
  405. .robustImageAccess2 = true,
  406. .nullDescriptor = true,
  407. };
  408. SetNext(next, robustness2);
  409. VkPhysicalDeviceHostQueryResetFeatures host_query_reset{
  410. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_QUERY_RESET_FEATURES,
  411. .pNext = nullptr,
  412. .hostQueryReset = true,
  413. };
  414. SetNext(next, host_query_reset);
  415. VkPhysicalDeviceVariablePointerFeatures variable_pointers{
  416. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTERS_FEATURES,
  417. .pNext = nullptr,
  418. .variablePointersStorageBuffer = VK_TRUE,
  419. .variablePointers = VK_TRUE,
  420. };
  421. SetNext(next, variable_pointers);
  422. VkPhysicalDeviceShaderDemoteToHelperInvocationFeatures demote{
  423. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DEMOTE_TO_HELPER_INVOCATION_FEATURES,
  424. .pNext = nullptr,
  425. .shaderDemoteToHelperInvocation = true,
  426. };
  427. SetNext(next, demote);
  428. VkPhysicalDeviceShaderDrawParametersFeatures draw_parameters{
  429. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETER_FEATURES,
  430. .pNext = nullptr,
  431. .shaderDrawParameters = true,
  432. };
  433. SetNext(next, draw_parameters);
  434. VkPhysicalDeviceShaderFloat16Int8Features float16_int8;
  435. if (is_int8_supported || is_float16_supported) {
  436. float16_int8 = {
  437. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FLOAT16_INT8_FEATURES,
  438. .pNext = nullptr,
  439. .shaderFloat16 = is_float16_supported,
  440. .shaderInt8 = is_int8_supported,
  441. };
  442. SetNext(next, float16_int8);
  443. }
  444. if (!is_float16_supported) {
  445. LOG_INFO(Render_Vulkan, "Device doesn't support float16 natively");
  446. }
  447. if (!is_int8_supported) {
  448. LOG_INFO(Render_Vulkan, "Device doesn't support int8 natively");
  449. }
  450. if (!nv_viewport_swizzle) {
  451. LOG_INFO(Render_Vulkan, "Device doesn't support viewport swizzles");
  452. }
  453. if (!nv_viewport_array2) {
  454. LOG_INFO(Render_Vulkan, "Device doesn't support viewport masks");
  455. }
  456. if (!nv_geometry_shader_passthrough) {
  457. LOG_INFO(Render_Vulkan, "Device doesn't support passthrough geometry shaders");
  458. }
  459. VkPhysicalDeviceUniformBufferStandardLayoutFeatures std430_layout;
  460. if (khr_uniform_buffer_standard_layout) {
  461. std430_layout = {
  462. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_UNIFORM_BUFFER_STANDARD_LAYOUT_FEATURES,
  463. .pNext = nullptr,
  464. .uniformBufferStandardLayout = true,
  465. };
  466. SetNext(next, std430_layout);
  467. } else {
  468. LOG_INFO(Render_Vulkan, "Device doesn't support packed UBOs");
  469. }
  470. VkPhysicalDeviceIndexTypeUint8FeaturesEXT index_type_uint8;
  471. if (ext_index_type_uint8) {
  472. index_type_uint8 = {
  473. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INDEX_TYPE_UINT8_FEATURES_EXT,
  474. .pNext = nullptr,
  475. .indexTypeUint8 = true,
  476. };
  477. SetNext(next, index_type_uint8);
  478. } else {
  479. LOG_INFO(Render_Vulkan, "Device doesn't support uint8 indexes");
  480. }
  481. VkPhysicalDevicePrimitiveTopologyListRestartFeaturesEXT primitive_topology_list_restart;
  482. if (is_topology_list_restart_supported || is_patch_list_restart_supported) {
  483. primitive_topology_list_restart = {
  484. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRIMITIVE_TOPOLOGY_LIST_RESTART_FEATURES_EXT,
  485. .pNext = nullptr,
  486. .primitiveTopologyListRestart = is_topology_list_restart_supported,
  487. .primitiveTopologyPatchListRestart = is_patch_list_restart_supported,
  488. };
  489. SetNext(next, primitive_topology_list_restart);
  490. } else {
  491. LOG_INFO(Render_Vulkan, "Device doesn't support list topology primitive restart");
  492. }
  493. VkPhysicalDeviceTransformFeedbackFeaturesEXT transform_feedback;
  494. if (ext_transform_feedback) {
  495. transform_feedback = {
  496. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT,
  497. .pNext = nullptr,
  498. .transformFeedback = true,
  499. .geometryStreams = true,
  500. };
  501. SetNext(next, transform_feedback);
  502. } else {
  503. LOG_INFO(Render_Vulkan, "Device doesn't support transform feedbacks");
  504. }
  505. VkPhysicalDeviceCustomBorderColorFeaturesEXT custom_border;
  506. if (ext_custom_border_color) {
  507. custom_border = {
  508. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CUSTOM_BORDER_COLOR_FEATURES_EXT,
  509. .pNext = nullptr,
  510. .customBorderColors = VK_TRUE,
  511. .customBorderColorWithoutFormat = VK_TRUE,
  512. };
  513. SetNext(next, custom_border);
  514. } else {
  515. LOG_INFO(Render_Vulkan, "Device doesn't support custom border colors");
  516. }
  517. VkPhysicalDeviceExtendedDynamicStateFeaturesEXT dynamic_state;
  518. if (ext_extended_dynamic_state) {
  519. dynamic_state = {
  520. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_FEATURES_EXT,
  521. .pNext = nullptr,
  522. .extendedDynamicState = VK_TRUE,
  523. };
  524. SetNext(next, dynamic_state);
  525. } else {
  526. LOG_INFO(Render_Vulkan, "Device doesn't support extended dynamic state");
  527. }
  528. VkPhysicalDeviceLineRasterizationFeaturesEXT line_raster;
  529. if (ext_line_rasterization) {
  530. line_raster = {
  531. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_LINE_RASTERIZATION_FEATURES_EXT,
  532. .pNext = nullptr,
  533. .rectangularLines = VK_TRUE,
  534. .bresenhamLines = VK_FALSE,
  535. .smoothLines = VK_TRUE,
  536. .stippledRectangularLines = VK_FALSE,
  537. .stippledBresenhamLines = VK_FALSE,
  538. .stippledSmoothLines = VK_FALSE,
  539. };
  540. SetNext(next, line_raster);
  541. } else {
  542. LOG_INFO(Render_Vulkan, "Device doesn't support smooth lines");
  543. }
  544. if (!ext_conservative_rasterization) {
  545. LOG_INFO(Render_Vulkan, "Device doesn't support conservative rasterization");
  546. }
  547. VkPhysicalDeviceProvokingVertexFeaturesEXT provoking_vertex;
  548. if (ext_provoking_vertex) {
  549. provoking_vertex = {
  550. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROVOKING_VERTEX_FEATURES_EXT,
  551. .pNext = nullptr,
  552. .provokingVertexLast = VK_TRUE,
  553. .transformFeedbackPreservesProvokingVertex = VK_TRUE,
  554. };
  555. SetNext(next, provoking_vertex);
  556. } else {
  557. LOG_INFO(Render_Vulkan, "Device doesn't support provoking vertex last");
  558. }
  559. VkPhysicalDeviceVertexInputDynamicStateFeaturesEXT vertex_input_dynamic;
  560. if (ext_vertex_input_dynamic_state) {
  561. vertex_input_dynamic = {
  562. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_INPUT_DYNAMIC_STATE_FEATURES_EXT,
  563. .pNext = nullptr,
  564. .vertexInputDynamicState = VK_TRUE,
  565. };
  566. SetNext(next, vertex_input_dynamic);
  567. } else {
  568. LOG_INFO(Render_Vulkan, "Device doesn't support vertex input dynamic state");
  569. }
  570. VkPhysicalDeviceShaderAtomicInt64Features atomic_int64;
  571. if (ext_shader_atomic_int64) {
  572. atomic_int64 = {
  573. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_ATOMIC_INT64_FEATURES,
  574. .pNext = nullptr,
  575. .shaderBufferInt64Atomics = VK_TRUE,
  576. .shaderSharedInt64Atomics = VK_TRUE,
  577. };
  578. SetNext(next, atomic_int64);
  579. }
  580. VkPhysicalDeviceWorkgroupMemoryExplicitLayoutFeaturesKHR workgroup_layout;
  581. if (khr_workgroup_memory_explicit_layout && is_shader_int16_supported) {
  582. workgroup_layout = {
  583. .sType =
  584. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_WORKGROUP_MEMORY_EXPLICIT_LAYOUT_FEATURES_KHR,
  585. .pNext = nullptr,
  586. .workgroupMemoryExplicitLayout = VK_TRUE,
  587. .workgroupMemoryExplicitLayoutScalarBlockLayout = VK_TRUE,
  588. .workgroupMemoryExplicitLayout8BitAccess = VK_TRUE,
  589. .workgroupMemoryExplicitLayout16BitAccess = VK_TRUE,
  590. };
  591. SetNext(next, workgroup_layout);
  592. } else if (khr_workgroup_memory_explicit_layout) {
  593. // TODO(lat9nq): Find a proper fix for this
  594. LOG_WARNING(Render_Vulkan, "Disabling VK_KHR_workgroup_memory_explicit_layout due to a "
  595. "yuzu bug when host driver does not support 16-bit integers");
  596. khr_workgroup_memory_explicit_layout = false;
  597. }
  598. VkPhysicalDevicePipelineExecutablePropertiesFeaturesKHR executable_properties;
  599. if (khr_pipeline_executable_properties) {
  600. LOG_INFO(Render_Vulkan, "Enabling shader feedback, expect slower shader build times");
  601. executable_properties = {
  602. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_EXECUTABLE_PROPERTIES_FEATURES_KHR,
  603. .pNext = nullptr,
  604. .pipelineExecutableInfo = VK_TRUE,
  605. };
  606. SetNext(next, executable_properties);
  607. }
  608. if (!ext_depth_range_unrestricted) {
  609. LOG_INFO(Render_Vulkan, "Device doesn't support depth range unrestricted");
  610. }
  611. VkDeviceDiagnosticsConfigCreateInfoNV diagnostics_nv;
  612. if (Settings::values.enable_nsight_aftermath && nv_device_diagnostics_config) {
  613. nsight_aftermath_tracker = std::make_unique<NsightAftermathTracker>();
  614. diagnostics_nv = {
  615. .sType = VK_STRUCTURE_TYPE_DEVICE_DIAGNOSTICS_CONFIG_CREATE_INFO_NV,
  616. .pNext = &features2,
  617. .flags = VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_SHADER_DEBUG_INFO_BIT_NV |
  618. VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_RESOURCE_TRACKING_BIT_NV |
  619. VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_AUTOMATIC_CHECKPOINTS_BIT_NV,
  620. };
  621. first_next = &diagnostics_nv;
  622. }
  623. logical = vk::Device::Create(physical, queue_cis, extensions, first_next, dld);
  624. is_integrated = properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;
  625. is_virtual = properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU;
  626. is_non_gpu = properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_OTHER ||
  627. properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_CPU;
  628. CollectPhysicalMemoryInfo();
  629. CollectTelemetryParameters();
  630. CollectToolingInfo();
  631. if (driver_id == VK_DRIVER_ID_NVIDIA_PROPRIETARY_KHR) {
  632. const auto arch = GetNvidiaArchitecture(physical, supported_extensions);
  633. switch (arch) {
  634. case NvidiaArchitecture::AmpereOrNewer:
  635. LOG_WARNING(Render_Vulkan, "Blacklisting Ampere devices from float16 math");
  636. is_float16_supported = false;
  637. break;
  638. case NvidiaArchitecture::Turing:
  639. break;
  640. case NvidiaArchitecture::VoltaOrOlder:
  641. LOG_WARNING(Render_Vulkan, "Blacklisting Volta and older from VK_KHR_push_descriptor");
  642. khr_push_descriptor = false;
  643. break;
  644. }
  645. const u32 nv_major_version = (properties.driverVersion >> 22) & 0x3ff;
  646. if (nv_major_version >= 510) {
  647. LOG_WARNING(Render_Vulkan, "NVIDIA Drivers >= 510 do not support MSAA image blits");
  648. cant_blit_msaa = true;
  649. }
  650. }
  651. const bool is_radv = driver_id == VK_DRIVER_ID_MESA_RADV;
  652. if (ext_extended_dynamic_state && is_radv) {
  653. // Mask driver version variant
  654. const u32 version = (properties.driverVersion << 3) >> 3;
  655. if (version < VK_MAKE_API_VERSION(0, 21, 2, 0)) {
  656. LOG_WARNING(Render_Vulkan,
  657. "RADV versions older than 21.2 have broken VK_EXT_extended_dynamic_state");
  658. ext_extended_dynamic_state = false;
  659. }
  660. }
  661. if (ext_vertex_input_dynamic_state && is_radv) {
  662. // TODO(ameerj): Blacklist only offending driver versions
  663. // TODO(ameerj): Confirm if RDNA1 is affected
  664. const bool is_rdna2 =
  665. IsExtensionSupported(supported_extensions, VK_KHR_FRAGMENT_SHADING_RATE_EXTENSION_NAME);
  666. if (is_rdna2) {
  667. LOG_WARNING(Render_Vulkan,
  668. "RADV has broken VK_EXT_vertex_input_dynamic_state on RDNA2 hardware");
  669. ext_vertex_input_dynamic_state = false;
  670. }
  671. }
  672. sets_per_pool = 64;
  673. const bool is_amd =
  674. driver_id == VK_DRIVER_ID_AMD_PROPRIETARY || driver_id == VK_DRIVER_ID_AMD_OPEN_SOURCE;
  675. if (is_amd) {
  676. // AMD drivers need a higher amount of Sets per Pool in certain circunstances like in XC2.
  677. sets_per_pool = 96;
  678. // Disable VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT on AMD GCN4 and lower as it is broken.
  679. if (!is_float16_supported) {
  680. LOG_WARNING(
  681. Render_Vulkan,
  682. "AMD GCN4 and earlier do not properly support VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT");
  683. has_broken_cube_compatibility = true;
  684. }
  685. }
  686. const bool is_amd_or_radv = is_amd || is_radv;
  687. if (ext_sampler_filter_minmax && is_amd_or_radv) {
  688. // Disable ext_sampler_filter_minmax on AMD GCN4 and lower as it is broken.
  689. if (!is_float16_supported) {
  690. LOG_WARNING(Render_Vulkan,
  691. "Blacklisting AMD GCN4 and earlier for VK_EXT_sampler_filter_minmax");
  692. ext_sampler_filter_minmax = false;
  693. }
  694. }
  695. const bool is_intel_windows = driver_id == VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS;
  696. const bool is_intel_anv = driver_id == VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA;
  697. if (ext_vertex_input_dynamic_state && is_intel_windows) {
  698. LOG_WARNING(Render_Vulkan, "Blacklisting Intel for VK_EXT_vertex_input_dynamic_state");
  699. ext_vertex_input_dynamic_state = false;
  700. }
  701. if (is_float16_supported && is_intel_windows) {
  702. // Intel's compiler crashes when using fp16 on Astral Chain, disable it for the time being.
  703. LOG_WARNING(Render_Vulkan, "Blacklisting Intel proprietary from float16 math");
  704. is_float16_supported = false;
  705. }
  706. if (is_intel_windows) {
  707. LOG_WARNING(Render_Vulkan, "Intel proprietary drivers do not support MSAA image blits");
  708. cant_blit_msaa = true;
  709. }
  710. if (is_intel_anv) {
  711. LOG_WARNING(Render_Vulkan, "ANV driver does not support native BGR format");
  712. must_emulate_bgr565 = true;
  713. }
  714. supports_d24_depth =
  715. IsFormatSupported(VK_FORMAT_D24_UNORM_S8_UINT,
  716. VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT, FormatType::Optimal);
  717. graphics_queue = logical.GetQueue(graphics_family);
  718. present_queue = logical.GetQueue(present_family);
  719. }
  720. Device::~Device() = default;
  721. VkFormat Device::GetSupportedFormat(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  722. FormatType format_type) const {
  723. if (IsFormatSupported(wanted_format, wanted_usage, format_type)) {
  724. return wanted_format;
  725. }
  726. // The wanted format is not supported by hardware, search for alternatives
  727. const VkFormat* alternatives = GetFormatAlternatives(wanted_format);
  728. if (alternatives == nullptr) {
  729. ASSERT_MSG(false,
  730. "Format={} with usage={} and type={} has no defined alternatives and host "
  731. "hardware does not support it",
  732. wanted_format, wanted_usage, format_type);
  733. return wanted_format;
  734. }
  735. std::size_t i = 0;
  736. for (VkFormat alternative = *alternatives; alternative; alternative = alternatives[++i]) {
  737. if (!IsFormatSupported(alternative, wanted_usage, format_type)) {
  738. continue;
  739. }
  740. LOG_DEBUG(Render_Vulkan,
  741. "Emulating format={} with alternative format={} with usage={} and type={}",
  742. wanted_format, alternative, wanted_usage, format_type);
  743. return alternative;
  744. }
  745. // No alternatives found, panic
  746. ASSERT_MSG(false,
  747. "Format={} with usage={} and type={} is not supported by the host hardware and "
  748. "doesn't support any of the alternatives",
  749. wanted_format, wanted_usage, format_type);
  750. return wanted_format;
  751. }
  752. void Device::ReportLoss() const {
  753. LOG_CRITICAL(Render_Vulkan, "Device loss occurred!");
  754. // Wait for the log to flush and for Nsight Aftermath to dump the results
  755. std::this_thread::sleep_for(std::chrono::seconds{15});
  756. }
  757. void Device::SaveShader(std::span<const u32> spirv) const {
  758. if (nsight_aftermath_tracker) {
  759. nsight_aftermath_tracker->SaveShader(spirv);
  760. }
  761. }
  762. bool Device::IsOptimalAstcSupported(const VkPhysicalDeviceFeatures& features) const {
  763. // Disable for now to avoid converting ASTC twice.
  764. static constexpr std::array astc_formats = {
  765. VK_FORMAT_ASTC_4x4_UNORM_BLOCK, VK_FORMAT_ASTC_4x4_SRGB_BLOCK,
  766. VK_FORMAT_ASTC_5x4_UNORM_BLOCK, VK_FORMAT_ASTC_5x4_SRGB_BLOCK,
  767. VK_FORMAT_ASTC_5x5_UNORM_BLOCK, VK_FORMAT_ASTC_5x5_SRGB_BLOCK,
  768. VK_FORMAT_ASTC_6x5_UNORM_BLOCK, VK_FORMAT_ASTC_6x5_SRGB_BLOCK,
  769. VK_FORMAT_ASTC_6x6_UNORM_BLOCK, VK_FORMAT_ASTC_6x6_SRGB_BLOCK,
  770. VK_FORMAT_ASTC_8x5_UNORM_BLOCK, VK_FORMAT_ASTC_8x5_SRGB_BLOCK,
  771. VK_FORMAT_ASTC_8x6_UNORM_BLOCK, VK_FORMAT_ASTC_8x6_SRGB_BLOCK,
  772. VK_FORMAT_ASTC_8x8_UNORM_BLOCK, VK_FORMAT_ASTC_8x8_SRGB_BLOCK,
  773. VK_FORMAT_ASTC_10x5_UNORM_BLOCK, VK_FORMAT_ASTC_10x5_SRGB_BLOCK,
  774. VK_FORMAT_ASTC_10x6_UNORM_BLOCK, VK_FORMAT_ASTC_10x6_SRGB_BLOCK,
  775. VK_FORMAT_ASTC_10x8_UNORM_BLOCK, VK_FORMAT_ASTC_10x8_SRGB_BLOCK,
  776. VK_FORMAT_ASTC_10x10_UNORM_BLOCK, VK_FORMAT_ASTC_10x10_SRGB_BLOCK,
  777. VK_FORMAT_ASTC_12x10_UNORM_BLOCK, VK_FORMAT_ASTC_12x10_SRGB_BLOCK,
  778. VK_FORMAT_ASTC_12x12_UNORM_BLOCK, VK_FORMAT_ASTC_12x12_SRGB_BLOCK,
  779. };
  780. if (!features.textureCompressionASTC_LDR) {
  781. return false;
  782. }
  783. const auto format_feature_usage{
  784. VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_BLIT_SRC_BIT |
  785. VK_FORMAT_FEATURE_BLIT_DST_BIT | VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
  786. VK_FORMAT_FEATURE_TRANSFER_DST_BIT};
  787. for (const auto format : astc_formats) {
  788. const auto physical_format_properties{physical.GetFormatProperties(format)};
  789. if ((physical_format_properties.optimalTilingFeatures & format_feature_usage) == 0) {
  790. return false;
  791. }
  792. }
  793. return true;
  794. }
  795. bool Device::TestDepthStencilBlits() const {
  796. static constexpr VkFormatFeatureFlags required_features =
  797. VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
  798. const auto test_features = [](VkFormatProperties props) {
  799. return (props.optimalTilingFeatures & required_features) == required_features;
  800. };
  801. return test_features(format_properties.at(VK_FORMAT_D32_SFLOAT_S8_UINT)) &&
  802. test_features(format_properties.at(VK_FORMAT_D24_UNORM_S8_UINT));
  803. }
  804. bool Device::IsFormatSupported(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  805. FormatType format_type) const {
  806. const auto it = format_properties.find(wanted_format);
  807. if (it == format_properties.end()) {
  808. UNIMPLEMENTED_MSG("Unimplemented format query={}", wanted_format);
  809. return true;
  810. }
  811. const auto supported_usage = GetFormatFeatures(it->second, format_type);
  812. return (supported_usage & wanted_usage) == wanted_usage;
  813. }
  814. std::string Device::GetDriverName() const {
  815. switch (driver_id) {
  816. case VK_DRIVER_ID_AMD_PROPRIETARY:
  817. return "AMD";
  818. case VK_DRIVER_ID_AMD_OPEN_SOURCE:
  819. return "AMDVLK";
  820. case VK_DRIVER_ID_MESA_RADV:
  821. return "RADV";
  822. case VK_DRIVER_ID_NVIDIA_PROPRIETARY:
  823. return "NVIDIA";
  824. case VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS:
  825. return "INTEL";
  826. case VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA:
  827. return "ANV";
  828. case VK_DRIVER_ID_MESA_LLVMPIPE:
  829. return "LAVAPIPE";
  830. default:
  831. return vendor_name;
  832. }
  833. }
  834. static std::vector<const char*> ExtensionsRequiredForInstanceVersion(u32 available_version) {
  835. std::vector<const char*> extensions{REQUIRED_EXTENSIONS.begin(), REQUIRED_EXTENSIONS.end()};
  836. if (available_version < VK_API_VERSION_1_2) {
  837. extensions.insert(extensions.end(), REQUIRED_EXTENSIONS_BEFORE_1_2.begin(),
  838. REQUIRED_EXTENSIONS_BEFORE_1_2.end());
  839. }
  840. if (available_version < VK_API_VERSION_1_3) {
  841. extensions.insert(extensions.end(), REQUIRED_EXTENSIONS_BEFORE_1_3.begin(),
  842. REQUIRED_EXTENSIONS_BEFORE_1_3.end());
  843. }
  844. return extensions;
  845. }
  846. void Device::CheckSuitability(bool requires_swapchain) const {
  847. std::vector<const char*> required_extensions =
  848. ExtensionsRequiredForInstanceVersion(instance_version);
  849. std::vector<const char*> available_extensions;
  850. if (requires_swapchain) {
  851. required_extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
  852. }
  853. auto extension_properties = physical.EnumerateDeviceExtensionProperties();
  854. for (const VkExtensionProperties& property : extension_properties) {
  855. available_extensions.push_back(property.extensionName);
  856. }
  857. bool has_all_required_extensions = true;
  858. for (const char* requirement_name : required_extensions) {
  859. const bool found =
  860. std::ranges::any_of(available_extensions, [&](const char* extension_name) {
  861. return std::strcmp(requirement_name, extension_name) == 0;
  862. });
  863. if (!found) {
  864. LOG_ERROR(Render_Vulkan, "Missing required extension: {}", requirement_name);
  865. has_all_required_extensions = false;
  866. }
  867. }
  868. if (!has_all_required_extensions) {
  869. throw vk::Exception(VK_ERROR_EXTENSION_NOT_PRESENT);
  870. }
  871. struct LimitTuple {
  872. u32 minimum;
  873. u32 value;
  874. const char* name;
  875. };
  876. const VkPhysicalDeviceLimits& limits{properties.limits};
  877. const std::array limits_report{
  878. LimitTuple{65536, limits.maxUniformBufferRange, "maxUniformBufferRange"},
  879. LimitTuple{16, limits.maxViewports, "maxViewports"},
  880. LimitTuple{8, limits.maxColorAttachments, "maxColorAttachments"},
  881. LimitTuple{8, limits.maxClipDistances, "maxClipDistances"},
  882. };
  883. for (const auto& tuple : limits_report) {
  884. if (tuple.value < tuple.minimum) {
  885. LOG_ERROR(Render_Vulkan, "{} has to be {} or greater but it is {}", tuple.name,
  886. tuple.minimum, tuple.value);
  887. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  888. }
  889. }
  890. VkPhysicalDeviceShaderDemoteToHelperInvocationFeatures demote{};
  891. demote.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DEMOTE_TO_HELPER_INVOCATION_FEATURES;
  892. demote.pNext = nullptr;
  893. VkPhysicalDeviceVariablePointerFeatures variable_pointers{};
  894. variable_pointers.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTERS_FEATURES;
  895. variable_pointers.pNext = &demote;
  896. VkPhysicalDeviceRobustness2FeaturesEXT robustness2{};
  897. robustness2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT;
  898. robustness2.pNext = &variable_pointers;
  899. VkPhysicalDeviceTimelineSemaphoreFeatures timeline_semaphore{};
  900. timeline_semaphore.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_FEATURES;
  901. timeline_semaphore.pNext = &robustness2;
  902. VkPhysicalDevice16BitStorageFeatures bit16_storage{};
  903. bit16_storage.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES;
  904. bit16_storage.pNext = &timeline_semaphore;
  905. VkPhysicalDevice8BitStorageFeatures bit8_storage{};
  906. bit8_storage.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES;
  907. bit8_storage.pNext = &bit16_storage;
  908. VkPhysicalDeviceHostQueryResetFeatures host_query_reset{};
  909. host_query_reset.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_QUERY_RESET_FEATURES;
  910. host_query_reset.pNext = &bit8_storage;
  911. VkPhysicalDeviceShaderDrawParametersFeatures draw_parameters{};
  912. draw_parameters.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETER_FEATURES;
  913. draw_parameters.pNext = &host_query_reset;
  914. VkPhysicalDeviceFeatures2 features2{};
  915. features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  916. features2.pNext = &draw_parameters;
  917. physical.GetFeatures2(features2);
  918. const VkPhysicalDeviceFeatures& features{features2.features};
  919. std::array feature_report{
  920. std::make_pair(features.robustBufferAccess, "robustBufferAccess"),
  921. std::make_pair(features.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics"),
  922. std::make_pair(features.imageCubeArray, "imageCubeArray"),
  923. std::make_pair(features.independentBlend, "independentBlend"),
  924. std::make_pair(features.depthClamp, "depthClamp"),
  925. std::make_pair(features.samplerAnisotropy, "samplerAnisotropy"),
  926. std::make_pair(features.largePoints, "largePoints"),
  927. std::make_pair(features.multiViewport, "multiViewport"),
  928. std::make_pair(features.depthBiasClamp, "depthBiasClamp"),
  929. std::make_pair(features.fillModeNonSolid, "fillModeNonSolid"),
  930. std::make_pair(features.wideLines, "wideLines"),
  931. std::make_pair(features.geometryShader, "geometryShader"),
  932. std::make_pair(features.tessellationShader, "tessellationShader"),
  933. std::make_pair(features.sampleRateShading, "sampleRateShading"),
  934. std::make_pair(features.dualSrcBlend, "dualSrcBlend"),
  935. std::make_pair(features.logicOp, "logicOp"),
  936. std::make_pair(features.occlusionQueryPrecise, "occlusionQueryPrecise"),
  937. std::make_pair(features.fragmentStoresAndAtomics, "fragmentStoresAndAtomics"),
  938. std::make_pair(features.shaderImageGatherExtended, "shaderImageGatherExtended"),
  939. std::make_pair(features.shaderStorageImageWriteWithoutFormat,
  940. "shaderStorageImageWriteWithoutFormat"),
  941. std::make_pair(features.shaderClipDistance, "shaderClipDistance"),
  942. std::make_pair(features.shaderCullDistance, "shaderCullDistance"),
  943. std::make_pair(variable_pointers.variablePointers, "variablePointers"),
  944. std::make_pair(variable_pointers.variablePointersStorageBuffer,
  945. "variablePointersStorageBuffer"),
  946. std::make_pair(robustness2.robustBufferAccess2, "robustBufferAccess2"),
  947. std::make_pair(robustness2.robustImageAccess2, "robustImageAccess2"),
  948. std::make_pair(robustness2.nullDescriptor, "nullDescriptor"),
  949. std::make_pair(demote.shaderDemoteToHelperInvocation, "shaderDemoteToHelperInvocation"),
  950. std::make_pair(timeline_semaphore.timelineSemaphore, "timelineSemaphore"),
  951. std::make_pair(bit16_storage.storageBuffer16BitAccess, "storageBuffer16BitAccess"),
  952. std::make_pair(bit16_storage.uniformAndStorageBuffer16BitAccess,
  953. "uniformAndStorageBuffer16BitAccess"),
  954. std::make_pair(bit8_storage.uniformAndStorageBuffer8BitAccess,
  955. "uniformAndStorageBuffer8BitAccess"),
  956. std::make_pair(host_query_reset.hostQueryReset, "hostQueryReset"),
  957. std::make_pair(draw_parameters.shaderDrawParameters, "shaderDrawParameters"),
  958. };
  959. bool has_all_required_features = true;
  960. for (const auto& [is_supported, name] : feature_report) {
  961. if (!is_supported) {
  962. LOG_ERROR(Render_Vulkan, "Missing required feature: {}", name);
  963. has_all_required_features = false;
  964. }
  965. }
  966. if (!has_all_required_features) {
  967. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  968. }
  969. }
  970. std::vector<const char*> Device::LoadExtensions(bool requires_surface) {
  971. std::vector<const char*> extensions = ExtensionsRequiredForInstanceVersion(instance_version);
  972. if (requires_surface) {
  973. extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
  974. }
  975. bool has_khr_shader_float16_int8{};
  976. bool has_khr_workgroup_memory_explicit_layout{};
  977. bool has_khr_pipeline_executable_properties{};
  978. bool has_khr_image_format_list{};
  979. bool has_khr_swapchain_mutable_format{};
  980. bool has_ext_subgroup_size_control{};
  981. bool has_ext_transform_feedback{};
  982. bool has_ext_custom_border_color{};
  983. bool has_ext_extended_dynamic_state{};
  984. bool has_ext_shader_atomic_int64{};
  985. bool has_ext_provoking_vertex{};
  986. bool has_ext_vertex_input_dynamic_state{};
  987. bool has_ext_line_rasterization{};
  988. bool has_ext_primitive_topology_list_restart{};
  989. for (const std::string& extension : supported_extensions) {
  990. const auto test = [&](std::optional<std::reference_wrapper<bool>> status, const char* name,
  991. bool push) {
  992. if (extension != name) {
  993. return;
  994. }
  995. if (push) {
  996. extensions.push_back(name);
  997. }
  998. if (status) {
  999. status->get() = true;
  1000. }
  1001. };
  1002. test(nv_viewport_swizzle, VK_NV_VIEWPORT_SWIZZLE_EXTENSION_NAME, true);
  1003. test(nv_viewport_array2, VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME, true);
  1004. test(nv_geometry_shader_passthrough, VK_NV_GEOMETRY_SHADER_PASSTHROUGH_EXTENSION_NAME,
  1005. true);
  1006. test(khr_uniform_buffer_standard_layout,
  1007. VK_KHR_UNIFORM_BUFFER_STANDARD_LAYOUT_EXTENSION_NAME, true);
  1008. test(khr_spirv_1_4, VK_KHR_SPIRV_1_4_EXTENSION_NAME, true);
  1009. test(khr_push_descriptor, VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME, true);
  1010. test(has_khr_shader_float16_int8, VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME, false);
  1011. test(ext_depth_range_unrestricted, VK_EXT_DEPTH_RANGE_UNRESTRICTED_EXTENSION_NAME, true);
  1012. test(ext_index_type_uint8, VK_EXT_INDEX_TYPE_UINT8_EXTENSION_NAME, true);
  1013. test(has_ext_primitive_topology_list_restart,
  1014. VK_EXT_PRIMITIVE_TOPOLOGY_LIST_RESTART_EXTENSION_NAME, true);
  1015. test(ext_sampler_filter_minmax, VK_EXT_SAMPLER_FILTER_MINMAX_EXTENSION_NAME, true);
  1016. test(ext_shader_viewport_index_layer, VK_EXT_SHADER_VIEWPORT_INDEX_LAYER_EXTENSION_NAME,
  1017. true);
  1018. test(ext_tooling_info, VK_EXT_TOOLING_INFO_EXTENSION_NAME, true);
  1019. test(ext_shader_stencil_export, VK_EXT_SHADER_STENCIL_EXPORT_EXTENSION_NAME, true);
  1020. test(ext_conservative_rasterization, VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME,
  1021. true);
  1022. test(has_ext_transform_feedback, VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME, false);
  1023. test(has_ext_custom_border_color, VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME, false);
  1024. test(has_ext_extended_dynamic_state, VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME, false);
  1025. test(has_ext_subgroup_size_control, VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME, true);
  1026. test(has_ext_provoking_vertex, VK_EXT_PROVOKING_VERTEX_EXTENSION_NAME, false);
  1027. test(has_ext_vertex_input_dynamic_state, VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME,
  1028. false);
  1029. test(has_ext_shader_atomic_int64, VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME, false);
  1030. test(has_khr_workgroup_memory_explicit_layout,
  1031. VK_KHR_WORKGROUP_MEMORY_EXPLICIT_LAYOUT_EXTENSION_NAME, false);
  1032. test(has_khr_image_format_list, VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME, false);
  1033. test(has_khr_swapchain_mutable_format, VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_EXTENSION_NAME,
  1034. false);
  1035. test(has_ext_line_rasterization, VK_EXT_LINE_RASTERIZATION_EXTENSION_NAME, false);
  1036. test(ext_memory_budget, VK_EXT_MEMORY_BUDGET_EXTENSION_NAME, true);
  1037. if (Settings::values.enable_nsight_aftermath) {
  1038. test(nv_device_diagnostics_config, VK_NV_DEVICE_DIAGNOSTICS_CONFIG_EXTENSION_NAME,
  1039. true);
  1040. }
  1041. if (Settings::values.renderer_shader_feedback) {
  1042. test(has_khr_pipeline_executable_properties,
  1043. VK_KHR_PIPELINE_EXECUTABLE_PROPERTIES_EXTENSION_NAME, false);
  1044. }
  1045. }
  1046. VkPhysicalDeviceFeatures2 features{};
  1047. features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  1048. VkPhysicalDeviceProperties2 physical_properties{};
  1049. physical_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
  1050. if (has_khr_shader_float16_int8) {
  1051. VkPhysicalDeviceShaderFloat16Int8Features float16_int8_features;
  1052. float16_int8_features.sType =
  1053. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_FLOAT16_INT8_FEATURES;
  1054. float16_int8_features.pNext = nullptr;
  1055. features.pNext = &float16_int8_features;
  1056. physical.GetFeatures2(features);
  1057. is_float16_supported = float16_int8_features.shaderFloat16;
  1058. is_int8_supported = float16_int8_features.shaderInt8;
  1059. extensions.push_back(VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME);
  1060. }
  1061. if (has_ext_subgroup_size_control) {
  1062. VkPhysicalDeviceSubgroupSizeControlFeatures subgroup_features;
  1063. subgroup_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES;
  1064. subgroup_features.pNext = nullptr;
  1065. features.pNext = &subgroup_features;
  1066. physical.GetFeatures2(features);
  1067. VkPhysicalDeviceSubgroupSizeControlProperties subgroup_properties;
  1068. subgroup_properties.sType =
  1069. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_PROPERTIES;
  1070. subgroup_properties.pNext = nullptr;
  1071. physical_properties.pNext = &subgroup_properties;
  1072. physical.GetProperties2(physical_properties);
  1073. is_warp_potentially_bigger = subgroup_properties.maxSubgroupSize > GuestWarpSize;
  1074. if (subgroup_features.subgroupSizeControl &&
  1075. subgroup_properties.minSubgroupSize <= GuestWarpSize &&
  1076. subgroup_properties.maxSubgroupSize >= GuestWarpSize) {
  1077. extensions.push_back(VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME);
  1078. guest_warp_stages = subgroup_properties.requiredSubgroupSizeStages;
  1079. ext_subgroup_size_control = true;
  1080. }
  1081. } else {
  1082. is_warp_potentially_bigger = true;
  1083. }
  1084. if (has_ext_provoking_vertex) {
  1085. VkPhysicalDeviceProvokingVertexFeaturesEXT provoking_vertex;
  1086. provoking_vertex.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROVOKING_VERTEX_FEATURES_EXT;
  1087. provoking_vertex.pNext = nullptr;
  1088. features.pNext = &provoking_vertex;
  1089. physical.GetFeatures2(features);
  1090. if (provoking_vertex.provokingVertexLast &&
  1091. provoking_vertex.transformFeedbackPreservesProvokingVertex) {
  1092. extensions.push_back(VK_EXT_PROVOKING_VERTEX_EXTENSION_NAME);
  1093. ext_provoking_vertex = true;
  1094. }
  1095. }
  1096. if (has_ext_vertex_input_dynamic_state) {
  1097. VkPhysicalDeviceVertexInputDynamicStateFeaturesEXT vertex_input;
  1098. vertex_input.sType =
  1099. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_INPUT_DYNAMIC_STATE_FEATURES_EXT;
  1100. vertex_input.pNext = nullptr;
  1101. features.pNext = &vertex_input;
  1102. physical.GetFeatures2(features);
  1103. if (vertex_input.vertexInputDynamicState) {
  1104. extensions.push_back(VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME);
  1105. ext_vertex_input_dynamic_state = true;
  1106. }
  1107. }
  1108. if (has_ext_shader_atomic_int64) {
  1109. VkPhysicalDeviceShaderAtomicInt64Features atomic_int64;
  1110. atomic_int64.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_ATOMIC_INT64_FEATURES;
  1111. atomic_int64.pNext = nullptr;
  1112. features.pNext = &atomic_int64;
  1113. physical.GetFeatures2(features);
  1114. if (atomic_int64.shaderBufferInt64Atomics && atomic_int64.shaderSharedInt64Atomics) {
  1115. extensions.push_back(VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME);
  1116. ext_shader_atomic_int64 = true;
  1117. }
  1118. }
  1119. if (has_ext_transform_feedback) {
  1120. VkPhysicalDeviceTransformFeedbackFeaturesEXT tfb_features;
  1121. tfb_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT;
  1122. tfb_features.pNext = nullptr;
  1123. features.pNext = &tfb_features;
  1124. physical.GetFeatures2(features);
  1125. VkPhysicalDeviceTransformFeedbackPropertiesEXT tfb_properties;
  1126. tfb_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT;
  1127. tfb_properties.pNext = nullptr;
  1128. physical_properties.pNext = &tfb_properties;
  1129. physical.GetProperties2(physical_properties);
  1130. if (tfb_features.transformFeedback && tfb_features.geometryStreams &&
  1131. tfb_properties.maxTransformFeedbackStreams >= 4 &&
  1132. tfb_properties.maxTransformFeedbackBuffers && tfb_properties.transformFeedbackQueries &&
  1133. tfb_properties.transformFeedbackDraw) {
  1134. extensions.push_back(VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME);
  1135. ext_transform_feedback = true;
  1136. }
  1137. }
  1138. if (has_ext_custom_border_color) {
  1139. VkPhysicalDeviceCustomBorderColorFeaturesEXT border_features;
  1140. border_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CUSTOM_BORDER_COLOR_FEATURES_EXT;
  1141. border_features.pNext = nullptr;
  1142. features.pNext = &border_features;
  1143. physical.GetFeatures2(features);
  1144. if (border_features.customBorderColors && border_features.customBorderColorWithoutFormat) {
  1145. extensions.push_back(VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME);
  1146. ext_custom_border_color = true;
  1147. }
  1148. }
  1149. if (has_ext_extended_dynamic_state) {
  1150. VkPhysicalDeviceExtendedDynamicStateFeaturesEXT extended_dynamic_state;
  1151. extended_dynamic_state.sType =
  1152. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_FEATURES_EXT;
  1153. extended_dynamic_state.pNext = nullptr;
  1154. features.pNext = &extended_dynamic_state;
  1155. physical.GetFeatures2(features);
  1156. if (extended_dynamic_state.extendedDynamicState) {
  1157. extensions.push_back(VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
  1158. ext_extended_dynamic_state = true;
  1159. }
  1160. }
  1161. if (has_ext_line_rasterization) {
  1162. VkPhysicalDeviceLineRasterizationFeaturesEXT line_raster;
  1163. line_raster.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_LINE_RASTERIZATION_FEATURES_EXT;
  1164. line_raster.pNext = nullptr;
  1165. features.pNext = &line_raster;
  1166. physical.GetFeatures2(features);
  1167. if (line_raster.rectangularLines && line_raster.smoothLines) {
  1168. extensions.push_back(VK_EXT_LINE_RASTERIZATION_EXTENSION_NAME);
  1169. ext_line_rasterization = true;
  1170. }
  1171. }
  1172. if (has_khr_workgroup_memory_explicit_layout) {
  1173. VkPhysicalDeviceWorkgroupMemoryExplicitLayoutFeaturesKHR layout;
  1174. layout.sType =
  1175. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_WORKGROUP_MEMORY_EXPLICIT_LAYOUT_FEATURES_KHR;
  1176. layout.pNext = nullptr;
  1177. features.pNext = &layout;
  1178. physical.GetFeatures2(features);
  1179. if (layout.workgroupMemoryExplicitLayout &&
  1180. layout.workgroupMemoryExplicitLayout8BitAccess &&
  1181. layout.workgroupMemoryExplicitLayout16BitAccess &&
  1182. layout.workgroupMemoryExplicitLayoutScalarBlockLayout) {
  1183. extensions.push_back(VK_KHR_WORKGROUP_MEMORY_EXPLICIT_LAYOUT_EXTENSION_NAME);
  1184. khr_workgroup_memory_explicit_layout = true;
  1185. }
  1186. }
  1187. if (has_khr_pipeline_executable_properties) {
  1188. VkPhysicalDevicePipelineExecutablePropertiesFeaturesKHR executable_properties;
  1189. executable_properties.sType =
  1190. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PIPELINE_EXECUTABLE_PROPERTIES_FEATURES_KHR;
  1191. executable_properties.pNext = nullptr;
  1192. features.pNext = &executable_properties;
  1193. physical.GetFeatures2(features);
  1194. if (executable_properties.pipelineExecutableInfo) {
  1195. extensions.push_back(VK_KHR_PIPELINE_EXECUTABLE_PROPERTIES_EXTENSION_NAME);
  1196. khr_pipeline_executable_properties = true;
  1197. }
  1198. }
  1199. if (has_ext_primitive_topology_list_restart) {
  1200. VkPhysicalDevicePrimitiveTopologyListRestartFeaturesEXT primitive_topology_list_restart{};
  1201. primitive_topology_list_restart.sType =
  1202. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PRIMITIVE_TOPOLOGY_LIST_RESTART_FEATURES_EXT;
  1203. primitive_topology_list_restart.pNext = nullptr;
  1204. features.pNext = &primitive_topology_list_restart;
  1205. physical.GetFeatures2(features);
  1206. is_topology_list_restart_supported =
  1207. primitive_topology_list_restart.primitiveTopologyListRestart;
  1208. is_patch_list_restart_supported =
  1209. primitive_topology_list_restart.primitiveTopologyPatchListRestart;
  1210. }
  1211. if (has_khr_image_format_list && has_khr_swapchain_mutable_format) {
  1212. extensions.push_back(VK_KHR_IMAGE_FORMAT_LIST_EXTENSION_NAME);
  1213. extensions.push_back(VK_KHR_SWAPCHAIN_MUTABLE_FORMAT_EXTENSION_NAME);
  1214. khr_swapchain_mutable_format = true;
  1215. }
  1216. if (khr_push_descriptor) {
  1217. VkPhysicalDevicePushDescriptorPropertiesKHR push_descriptor;
  1218. push_descriptor.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR;
  1219. push_descriptor.pNext = nullptr;
  1220. physical_properties.pNext = &push_descriptor;
  1221. physical.GetProperties2(physical_properties);
  1222. max_push_descriptors = push_descriptor.maxPushDescriptors;
  1223. }
  1224. return extensions;
  1225. }
  1226. void Device::SetupFamilies(VkSurfaceKHR surface) {
  1227. const std::vector queue_family_properties = physical.GetQueueFamilyProperties();
  1228. std::optional<u32> graphics;
  1229. std::optional<u32> present;
  1230. for (u32 index = 0; index < static_cast<u32>(queue_family_properties.size()); ++index) {
  1231. if (graphics && (present || !surface)) {
  1232. break;
  1233. }
  1234. const VkQueueFamilyProperties& queue_family = queue_family_properties[index];
  1235. if (queue_family.queueCount == 0) {
  1236. continue;
  1237. }
  1238. if (queue_family.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
  1239. graphics = index;
  1240. }
  1241. if (surface && physical.GetSurfaceSupportKHR(index, surface)) {
  1242. present = index;
  1243. }
  1244. }
  1245. if (!graphics) {
  1246. LOG_ERROR(Render_Vulkan, "Device lacks a graphics queue");
  1247. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  1248. }
  1249. if (surface && !present) {
  1250. LOG_ERROR(Render_Vulkan, "Device lacks a present queue");
  1251. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  1252. }
  1253. graphics_family = *graphics;
  1254. present_family = *present;
  1255. }
  1256. void Device::SetupFeatures() {
  1257. const VkPhysicalDeviceFeatures features{physical.GetFeatures()};
  1258. is_depth_bounds_supported = features.depthBounds;
  1259. is_formatless_image_load_supported = features.shaderStorageImageReadWithoutFormat;
  1260. is_shader_float64_supported = features.shaderFloat64;
  1261. is_shader_int64_supported = features.shaderInt64;
  1262. is_shader_int16_supported = features.shaderInt16;
  1263. is_shader_storage_image_multisample = features.shaderStorageImageMultisample;
  1264. is_blit_depth_stencil_supported = TestDepthStencilBlits();
  1265. is_optimal_astc_supported = IsOptimalAstcSupported(features);
  1266. }
  1267. void Device::SetupProperties() {
  1268. float_controls.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT_CONTROLS_PROPERTIES;
  1269. VkPhysicalDeviceProperties2KHR properties2{};
  1270. properties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
  1271. properties2.pNext = &float_controls;
  1272. physical.GetProperties2(properties2);
  1273. }
  1274. void Device::CollectTelemetryParameters() {
  1275. VkPhysicalDeviceDriverProperties driver{
  1276. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES,
  1277. .pNext = nullptr,
  1278. .driverID = {},
  1279. .driverName = {},
  1280. .driverInfo = {},
  1281. .conformanceVersion = {},
  1282. };
  1283. VkPhysicalDeviceProperties2 device_properties{
  1284. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2,
  1285. .pNext = &driver,
  1286. .properties = {},
  1287. };
  1288. physical.GetProperties2(device_properties);
  1289. driver_id = driver.driverID;
  1290. vendor_name = driver.driverName;
  1291. }
  1292. u64 Device::GetDeviceMemoryUsage() const {
  1293. VkPhysicalDeviceMemoryBudgetPropertiesEXT budget;
  1294. budget.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT;
  1295. budget.pNext = nullptr;
  1296. physical.GetMemoryProperties(&budget);
  1297. u64 result{};
  1298. for (const size_t heap : valid_heap_memory) {
  1299. result += budget.heapUsage[heap];
  1300. }
  1301. return result;
  1302. }
  1303. void Device::CollectPhysicalMemoryInfo() {
  1304. VkPhysicalDeviceMemoryBudgetPropertiesEXT budget{};
  1305. budget.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT;
  1306. const auto mem_info = physical.GetMemoryProperties(ext_memory_budget ? &budget : nullptr);
  1307. const auto& mem_properties = mem_info.memoryProperties;
  1308. const size_t num_properties = mem_properties.memoryHeapCount;
  1309. device_access_memory = 0;
  1310. u64 device_initial_usage = 0;
  1311. u64 local_memory = 0;
  1312. for (size_t element = 0; element < num_properties; ++element) {
  1313. const bool is_heap_local =
  1314. (mem_properties.memoryHeaps[element].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) != 0;
  1315. if (!is_integrated && !is_heap_local) {
  1316. continue;
  1317. }
  1318. valid_heap_memory.push_back(element);
  1319. if (is_heap_local) {
  1320. local_memory += mem_properties.memoryHeaps[element].size;
  1321. }
  1322. if (ext_memory_budget) {
  1323. device_initial_usage += budget.heapUsage[element];
  1324. device_access_memory += budget.heapBudget[element];
  1325. continue;
  1326. }
  1327. device_access_memory += mem_properties.memoryHeaps[element].size;
  1328. }
  1329. if (!is_integrated) {
  1330. return;
  1331. }
  1332. const s64 available_memory = static_cast<s64>(device_access_memory - device_initial_usage);
  1333. device_access_memory = static_cast<u64>(std::max<s64>(
  1334. std::min<s64>(available_memory - 8_GiB, 4_GiB), static_cast<s64>(local_memory)));
  1335. }
  1336. void Device::CollectToolingInfo() {
  1337. if (!ext_tooling_info) {
  1338. return;
  1339. }
  1340. auto tools{physical.GetPhysicalDeviceToolProperties()};
  1341. for (const VkPhysicalDeviceToolProperties& tool : tools) {
  1342. const std::string_view name = tool.name;
  1343. LOG_INFO(Render_Vulkan, "Attached debugging tool: {}", name);
  1344. has_renderdoc = has_renderdoc || name == "RenderDoc";
  1345. has_nsight_graphics = has_nsight_graphics || name == "NVIDIA Nsight Graphics";
  1346. }
  1347. }
  1348. std::vector<VkDeviceQueueCreateInfo> Device::GetDeviceQueueCreateInfos() const {
  1349. static constexpr float QUEUE_PRIORITY = 1.0f;
  1350. std::unordered_set<u32> unique_queue_families{graphics_family, present_family};
  1351. std::vector<VkDeviceQueueCreateInfo> queue_cis;
  1352. queue_cis.reserve(unique_queue_families.size());
  1353. for (const u32 queue_family : unique_queue_families) {
  1354. auto& ci = queue_cis.emplace_back(VkDeviceQueueCreateInfo{
  1355. .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
  1356. .pNext = nullptr,
  1357. .flags = 0,
  1358. .queueFamilyIndex = queue_family,
  1359. .queueCount = 1,
  1360. .pQueuePriorities = nullptr,
  1361. });
  1362. ci.pQueuePriorities = &QUEUE_PRIORITY;
  1363. }
  1364. return queue_cis;
  1365. }
  1366. } // namespace Vulkan