vulkan_device.cpp 57 KB

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