vulkan_device.cpp 59 KB

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