vulkan_device.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. // Copyright 2018 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <bitset>
  5. #include <chrono>
  6. #include <optional>
  7. #include <string_view>
  8. #include <thread>
  9. #include <unordered_set>
  10. #include <utility>
  11. #include <vector>
  12. #include "common/assert.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. namespace {
  19. namespace Alternatives {
  20. constexpr std::array DEPTH24_UNORM_STENCIL8_UINT{
  21. VK_FORMAT_D32_SFLOAT_S8_UINT,
  22. VK_FORMAT_D16_UNORM_S8_UINT,
  23. VK_FORMAT_UNDEFINED,
  24. };
  25. constexpr std::array DEPTH16_UNORM_STENCIL8_UINT{
  26. VK_FORMAT_D24_UNORM_S8_UINT,
  27. VK_FORMAT_D32_SFLOAT_S8_UINT,
  28. VK_FORMAT_UNDEFINED,
  29. };
  30. } // namespace Alternatives
  31. constexpr std::array REQUIRED_EXTENSIONS{
  32. VK_KHR_MAINTENANCE1_EXTENSION_NAME,
  33. VK_KHR_STORAGE_BUFFER_STORAGE_CLASS_EXTENSION_NAME,
  34. VK_KHR_SHADER_DRAW_PARAMETERS_EXTENSION_NAME,
  35. VK_KHR_16BIT_STORAGE_EXTENSION_NAME,
  36. VK_KHR_8BIT_STORAGE_EXTENSION_NAME,
  37. VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME,
  38. VK_KHR_DESCRIPTOR_UPDATE_TEMPLATE_EXTENSION_NAME,
  39. VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME,
  40. VK_KHR_SAMPLER_MIRROR_CLAMP_TO_EDGE_EXTENSION_NAME,
  41. VK_KHR_SHADER_FLOAT_CONTROLS_EXTENSION_NAME,
  42. VK_KHR_VARIABLE_POINTERS_EXTENSION_NAME,
  43. VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME,
  44. VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME,
  45. VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME,
  46. VK_EXT_ROBUSTNESS_2_EXTENSION_NAME,
  47. VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME,
  48. VK_EXT_SHADER_DEMOTE_TO_HELPER_INVOCATION_EXTENSION_NAME,
  49. #ifdef _WIN32
  50. VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME,
  51. #endif
  52. #ifdef __unix__
  53. VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME,
  54. #endif
  55. };
  56. template <typename T>
  57. void SetNext(void**& next, T& data) {
  58. *next = &data;
  59. next = &data.pNext;
  60. }
  61. constexpr const VkFormat* GetFormatAlternatives(VkFormat format) {
  62. switch (format) {
  63. case VK_FORMAT_D24_UNORM_S8_UINT:
  64. return Alternatives::DEPTH24_UNORM_STENCIL8_UINT.data();
  65. case VK_FORMAT_D16_UNORM_S8_UINT:
  66. return Alternatives::DEPTH16_UNORM_STENCIL8_UINT.data();
  67. default:
  68. return nullptr;
  69. }
  70. }
  71. VkFormatFeatureFlags GetFormatFeatures(VkFormatProperties properties, FormatType format_type) {
  72. switch (format_type) {
  73. case FormatType::Linear:
  74. return properties.linearTilingFeatures;
  75. case FormatType::Optimal:
  76. return properties.optimalTilingFeatures;
  77. case FormatType::Buffer:
  78. return properties.bufferFeatures;
  79. default:
  80. return {};
  81. }
  82. }
  83. std::unordered_map<VkFormat, VkFormatProperties> GetFormatProperties(vk::PhysicalDevice physical) {
  84. static constexpr std::array formats{
  85. VK_FORMAT_A8B8G8R8_UNORM_PACK32,
  86. VK_FORMAT_A8B8G8R8_UINT_PACK32,
  87. VK_FORMAT_A8B8G8R8_SNORM_PACK32,
  88. VK_FORMAT_A8B8G8R8_SINT_PACK32,
  89. VK_FORMAT_A8B8G8R8_SRGB_PACK32,
  90. VK_FORMAT_B5G6R5_UNORM_PACK16,
  91. VK_FORMAT_A2B10G10R10_UNORM_PACK32,
  92. VK_FORMAT_A2B10G10R10_UINT_PACK32,
  93. VK_FORMAT_A1R5G5B5_UNORM_PACK16,
  94. VK_FORMAT_R32G32B32A32_SFLOAT,
  95. VK_FORMAT_R32G32B32A32_SINT,
  96. VK_FORMAT_R32G32B32A32_UINT,
  97. VK_FORMAT_R32G32_SFLOAT,
  98. VK_FORMAT_R32G32_SINT,
  99. VK_FORMAT_R32G32_UINT,
  100. VK_FORMAT_R16G16B16A16_SINT,
  101. VK_FORMAT_R16G16B16A16_UINT,
  102. VK_FORMAT_R16G16B16A16_SNORM,
  103. VK_FORMAT_R16G16B16A16_UNORM,
  104. VK_FORMAT_R16G16_UNORM,
  105. VK_FORMAT_R16G16_SNORM,
  106. VK_FORMAT_R16G16_SFLOAT,
  107. VK_FORMAT_R16G16_SINT,
  108. VK_FORMAT_R16_UNORM,
  109. VK_FORMAT_R16_UINT,
  110. VK_FORMAT_R8G8B8A8_SRGB,
  111. VK_FORMAT_R8G8_UNORM,
  112. VK_FORMAT_R8G8_SNORM,
  113. VK_FORMAT_R8G8_SINT,
  114. VK_FORMAT_R8G8_UINT,
  115. VK_FORMAT_R8_UNORM,
  116. VK_FORMAT_R8_SNORM,
  117. VK_FORMAT_R8_SINT,
  118. VK_FORMAT_R8_UINT,
  119. VK_FORMAT_B10G11R11_UFLOAT_PACK32,
  120. VK_FORMAT_R32_SFLOAT,
  121. VK_FORMAT_R32_UINT,
  122. VK_FORMAT_R32_SINT,
  123. VK_FORMAT_R16_SFLOAT,
  124. VK_FORMAT_R16G16B16A16_SFLOAT,
  125. VK_FORMAT_B8G8R8A8_UNORM,
  126. VK_FORMAT_B8G8R8A8_SRGB,
  127. VK_FORMAT_R4G4B4A4_UNORM_PACK16,
  128. VK_FORMAT_D32_SFLOAT,
  129. VK_FORMAT_D16_UNORM,
  130. VK_FORMAT_D16_UNORM_S8_UINT,
  131. VK_FORMAT_D24_UNORM_S8_UINT,
  132. VK_FORMAT_D32_SFLOAT_S8_UINT,
  133. VK_FORMAT_BC1_RGBA_UNORM_BLOCK,
  134. VK_FORMAT_BC2_UNORM_BLOCK,
  135. VK_FORMAT_BC3_UNORM_BLOCK,
  136. VK_FORMAT_BC4_UNORM_BLOCK,
  137. VK_FORMAT_BC4_SNORM_BLOCK,
  138. VK_FORMAT_BC5_UNORM_BLOCK,
  139. VK_FORMAT_BC5_SNORM_BLOCK,
  140. VK_FORMAT_BC7_UNORM_BLOCK,
  141. VK_FORMAT_BC6H_UFLOAT_BLOCK,
  142. VK_FORMAT_BC6H_SFLOAT_BLOCK,
  143. VK_FORMAT_BC1_RGBA_SRGB_BLOCK,
  144. VK_FORMAT_BC2_SRGB_BLOCK,
  145. VK_FORMAT_BC3_SRGB_BLOCK,
  146. VK_FORMAT_BC7_SRGB_BLOCK,
  147. VK_FORMAT_ASTC_4x4_UNORM_BLOCK,
  148. VK_FORMAT_ASTC_4x4_SRGB_BLOCK,
  149. VK_FORMAT_ASTC_5x4_UNORM_BLOCK,
  150. VK_FORMAT_ASTC_5x4_SRGB_BLOCK,
  151. VK_FORMAT_ASTC_5x5_UNORM_BLOCK,
  152. VK_FORMAT_ASTC_5x5_SRGB_BLOCK,
  153. VK_FORMAT_ASTC_6x5_UNORM_BLOCK,
  154. VK_FORMAT_ASTC_6x5_SRGB_BLOCK,
  155. VK_FORMAT_ASTC_6x6_UNORM_BLOCK,
  156. VK_FORMAT_ASTC_6x6_SRGB_BLOCK,
  157. VK_FORMAT_ASTC_8x5_UNORM_BLOCK,
  158. VK_FORMAT_ASTC_8x5_SRGB_BLOCK,
  159. VK_FORMAT_ASTC_8x6_UNORM_BLOCK,
  160. VK_FORMAT_ASTC_8x6_SRGB_BLOCK,
  161. VK_FORMAT_ASTC_8x8_UNORM_BLOCK,
  162. VK_FORMAT_ASTC_8x8_SRGB_BLOCK,
  163. VK_FORMAT_ASTC_10x5_UNORM_BLOCK,
  164. VK_FORMAT_ASTC_10x5_SRGB_BLOCK,
  165. VK_FORMAT_ASTC_10x6_UNORM_BLOCK,
  166. VK_FORMAT_ASTC_10x6_SRGB_BLOCK,
  167. VK_FORMAT_ASTC_10x8_UNORM_BLOCK,
  168. VK_FORMAT_ASTC_10x8_SRGB_BLOCK,
  169. VK_FORMAT_ASTC_10x10_UNORM_BLOCK,
  170. VK_FORMAT_ASTC_10x10_SRGB_BLOCK,
  171. VK_FORMAT_ASTC_12x10_UNORM_BLOCK,
  172. VK_FORMAT_ASTC_12x10_SRGB_BLOCK,
  173. VK_FORMAT_ASTC_12x12_UNORM_BLOCK,
  174. VK_FORMAT_ASTC_12x12_SRGB_BLOCK,
  175. VK_FORMAT_ASTC_8x6_UNORM_BLOCK,
  176. VK_FORMAT_ASTC_8x6_SRGB_BLOCK,
  177. VK_FORMAT_ASTC_6x5_UNORM_BLOCK,
  178. VK_FORMAT_ASTC_6x5_SRGB_BLOCK,
  179. VK_FORMAT_E5B9G9R9_UFLOAT_PACK32,
  180. };
  181. std::unordered_map<VkFormat, VkFormatProperties> format_properties;
  182. for (const auto format : formats) {
  183. format_properties.emplace(format, physical.GetFormatProperties(format));
  184. }
  185. return format_properties;
  186. }
  187. } // Anonymous namespace
  188. Device::Device(VkInstance instance_, vk::PhysicalDevice physical_, VkSurfaceKHR surface,
  189. const vk::InstanceDispatch& dld_)
  190. : instance{instance_}, dld{dld_}, physical{physical_}, properties{physical.GetProperties()},
  191. format_properties{GetFormatProperties(physical)} {
  192. CheckSuitability(surface != nullptr);
  193. SetupFamilies(surface);
  194. SetupFeatures();
  195. SetupProperties();
  196. const auto queue_cis = GetDeviceQueueCreateInfos();
  197. const std::vector extensions = LoadExtensions(surface != nullptr);
  198. VkPhysicalDeviceFeatures2 features2{
  199. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
  200. .pNext = nullptr,
  201. .features{
  202. .robustBufferAccess = true,
  203. .fullDrawIndexUint32 = false,
  204. .imageCubeArray = true,
  205. .independentBlend = true,
  206. .geometryShader = true,
  207. .tessellationShader = true,
  208. .sampleRateShading = true,
  209. .dualSrcBlend = true,
  210. .logicOp = false,
  211. .multiDrawIndirect = false,
  212. .drawIndirectFirstInstance = false,
  213. .depthClamp = true,
  214. .depthBiasClamp = true,
  215. .fillModeNonSolid = true,
  216. .depthBounds = false,
  217. .wideLines = false,
  218. .largePoints = true,
  219. .alphaToOne = false,
  220. .multiViewport = true,
  221. .samplerAnisotropy = true,
  222. .textureCompressionETC2 = false,
  223. .textureCompressionASTC_LDR = is_optimal_astc_supported,
  224. .textureCompressionBC = false,
  225. .occlusionQueryPrecise = true,
  226. .pipelineStatisticsQuery = false,
  227. .vertexPipelineStoresAndAtomics = true,
  228. .fragmentStoresAndAtomics = true,
  229. .shaderTessellationAndGeometryPointSize = false,
  230. .shaderImageGatherExtended = true,
  231. .shaderStorageImageExtendedFormats = false,
  232. .shaderStorageImageMultisample = is_shader_storage_image_multisample,
  233. .shaderStorageImageReadWithoutFormat = is_formatless_image_load_supported,
  234. .shaderStorageImageWriteWithoutFormat = true,
  235. .shaderUniformBufferArrayDynamicIndexing = false,
  236. .shaderSampledImageArrayDynamicIndexing = false,
  237. .shaderStorageBufferArrayDynamicIndexing = false,
  238. .shaderStorageImageArrayDynamicIndexing = false,
  239. .shaderClipDistance = false,
  240. .shaderCullDistance = false,
  241. .shaderFloat64 = true,
  242. .shaderInt64 = true,
  243. .shaderInt16 = true,
  244. .shaderResourceResidency = false,
  245. .shaderResourceMinLod = false,
  246. .sparseBinding = false,
  247. .sparseResidencyBuffer = false,
  248. .sparseResidencyImage2D = false,
  249. .sparseResidencyImage3D = false,
  250. .sparseResidency2Samples = false,
  251. .sparseResidency4Samples = false,
  252. .sparseResidency8Samples = false,
  253. .sparseResidency16Samples = false,
  254. .sparseResidencyAliased = false,
  255. .variableMultisampleRate = false,
  256. .inheritedQueries = false,
  257. },
  258. };
  259. const void* first_next = &features2;
  260. void** next = &features2.pNext;
  261. VkPhysicalDeviceTimelineSemaphoreFeaturesKHR timeline_semaphore{
  262. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_FEATURES_KHR,
  263. .pNext = nullptr,
  264. .timelineSemaphore = true,
  265. };
  266. SetNext(next, timeline_semaphore);
  267. VkPhysicalDevice16BitStorageFeaturesKHR bit16_storage{
  268. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES_KHR,
  269. .pNext = nullptr,
  270. .storageBuffer16BitAccess = false,
  271. .uniformAndStorageBuffer16BitAccess = true,
  272. .storagePushConstant16 = false,
  273. .storageInputOutput16 = false,
  274. };
  275. SetNext(next, bit16_storage);
  276. VkPhysicalDevice8BitStorageFeaturesKHR bit8_storage{
  277. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES_KHR,
  278. .pNext = nullptr,
  279. .storageBuffer8BitAccess = false,
  280. .uniformAndStorageBuffer8BitAccess = true,
  281. .storagePushConstant8 = false,
  282. };
  283. SetNext(next, bit8_storage);
  284. VkPhysicalDeviceRobustness2FeaturesEXT robustness2{
  285. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT,
  286. .pNext = nullptr,
  287. .robustBufferAccess2 = true,
  288. .robustImageAccess2 = true,
  289. .nullDescriptor = true,
  290. };
  291. SetNext(next, robustness2);
  292. VkPhysicalDeviceHostQueryResetFeaturesEXT host_query_reset{
  293. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_QUERY_RESET_FEATURES_EXT,
  294. .pNext = nullptr,
  295. .hostQueryReset = true,
  296. };
  297. SetNext(next, host_query_reset);
  298. VkPhysicalDeviceVariablePointerFeaturesKHR variable_pointers{
  299. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTERS_FEATURES_KHR,
  300. .pNext = nullptr,
  301. .variablePointersStorageBuffer = VK_TRUE,
  302. .variablePointers = VK_TRUE,
  303. };
  304. SetNext(next, variable_pointers);
  305. VkPhysicalDeviceShaderDemoteToHelperInvocationFeaturesEXT demote{
  306. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DEMOTE_TO_HELPER_INVOCATION_FEATURES_EXT,
  307. .pNext = nullptr,
  308. .shaderDemoteToHelperInvocation = true,
  309. };
  310. SetNext(next, demote);
  311. VkPhysicalDeviceFloat16Int8FeaturesKHR float16_int8;
  312. if (is_float16_supported) {
  313. float16_int8 = {
  314. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT16_INT8_FEATURES_KHR,
  315. .pNext = nullptr,
  316. .shaderFloat16 = true,
  317. .shaderInt8 = false,
  318. };
  319. SetNext(next, float16_int8);
  320. } else {
  321. LOG_INFO(Render_Vulkan, "Device doesn't support float16 natively");
  322. }
  323. if (!nv_viewport_swizzle) {
  324. LOG_INFO(Render_Vulkan, "Device doesn't support viewport swizzles");
  325. }
  326. if (!nv_viewport_array2) {
  327. LOG_INFO(Render_Vulkan, "Device doesn't support viewport masks");
  328. }
  329. VkPhysicalDeviceUniformBufferStandardLayoutFeaturesKHR std430_layout;
  330. if (khr_uniform_buffer_standard_layout) {
  331. std430_layout = {
  332. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_UNIFORM_BUFFER_STANDARD_LAYOUT_FEATURES_KHR,
  333. .pNext = nullptr,
  334. .uniformBufferStandardLayout = true,
  335. };
  336. SetNext(next, std430_layout);
  337. } else {
  338. LOG_INFO(Render_Vulkan, "Device doesn't support packed UBOs");
  339. }
  340. VkPhysicalDeviceIndexTypeUint8FeaturesEXT index_type_uint8;
  341. if (ext_index_type_uint8) {
  342. index_type_uint8 = {
  343. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INDEX_TYPE_UINT8_FEATURES_EXT,
  344. .pNext = nullptr,
  345. .indexTypeUint8 = true,
  346. };
  347. SetNext(next, index_type_uint8);
  348. } else {
  349. LOG_INFO(Render_Vulkan, "Device doesn't support uint8 indexes");
  350. }
  351. VkPhysicalDeviceTransformFeedbackFeaturesEXT transform_feedback;
  352. if (ext_transform_feedback) {
  353. transform_feedback = {
  354. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT,
  355. .pNext = nullptr,
  356. .transformFeedback = true,
  357. .geometryStreams = true,
  358. };
  359. SetNext(next, transform_feedback);
  360. } else {
  361. LOG_INFO(Render_Vulkan, "Device doesn't support transform feedbacks");
  362. }
  363. VkPhysicalDeviceCustomBorderColorFeaturesEXT custom_border;
  364. if (ext_custom_border_color) {
  365. custom_border = {
  366. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CUSTOM_BORDER_COLOR_FEATURES_EXT,
  367. .pNext = nullptr,
  368. .customBorderColors = VK_TRUE,
  369. .customBorderColorWithoutFormat = VK_TRUE,
  370. };
  371. SetNext(next, custom_border);
  372. } else {
  373. LOG_INFO(Render_Vulkan, "Device doesn't support custom border colors");
  374. }
  375. VkPhysicalDeviceExtendedDynamicStateFeaturesEXT dynamic_state;
  376. if (ext_extended_dynamic_state) {
  377. dynamic_state = {
  378. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_FEATURES_EXT,
  379. .pNext = nullptr,
  380. .extendedDynamicState = VK_TRUE,
  381. };
  382. SetNext(next, dynamic_state);
  383. } else {
  384. LOG_INFO(Render_Vulkan, "Device doesn't support extended dynamic state");
  385. }
  386. VkPhysicalDeviceShaderAtomicInt64FeaturesKHR atomic_int64;
  387. if (ext_shader_atomic_int64) {
  388. atomic_int64 = {
  389. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_ATOMIC_INT64_FEATURES_KHR,
  390. .pNext = nullptr,
  391. .shaderBufferInt64Atomics = VK_TRUE,
  392. .shaderSharedInt64Atomics = VK_TRUE,
  393. };
  394. SetNext(next, atomic_int64);
  395. }
  396. VkPhysicalDeviceWorkgroupMemoryExplicitLayoutFeaturesKHR workgroup_layout;
  397. if (khr_workgroup_memory_explicit_layout) {
  398. workgroup_layout = {
  399. .sType =
  400. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_WORKGROUP_MEMORY_EXPLICIT_LAYOUT_FEATURES_KHR,
  401. .pNext = nullptr,
  402. .workgroupMemoryExplicitLayout = VK_TRUE,
  403. .workgroupMemoryExplicitLayoutScalarBlockLayout = VK_TRUE,
  404. .workgroupMemoryExplicitLayout8BitAccess = VK_TRUE,
  405. .workgroupMemoryExplicitLayout16BitAccess = VK_TRUE,
  406. };
  407. SetNext(next, workgroup_layout);
  408. }
  409. if (!ext_depth_range_unrestricted) {
  410. LOG_INFO(Render_Vulkan, "Device doesn't support depth range unrestricted");
  411. }
  412. VkDeviceDiagnosticsConfigCreateInfoNV diagnostics_nv;
  413. if (nv_device_diagnostics_config) {
  414. nsight_aftermath_tracker = std::make_unique<NsightAftermathTracker>();
  415. diagnostics_nv = {
  416. .sType = VK_STRUCTURE_TYPE_DEVICE_DIAGNOSTICS_CONFIG_CREATE_INFO_NV,
  417. .pNext = &features2,
  418. .flags = VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_SHADER_DEBUG_INFO_BIT_NV |
  419. VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_RESOURCE_TRACKING_BIT_NV |
  420. VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_AUTOMATIC_CHECKPOINTS_BIT_NV,
  421. };
  422. first_next = &diagnostics_nv;
  423. }
  424. logical = vk::Device::Create(physical, queue_cis, extensions, first_next, dld);
  425. CollectPhysicalMemoryInfo();
  426. CollectTelemetryParameters();
  427. CollectToolingInfo();
  428. if (ext_extended_dynamic_state && driver_id == VK_DRIVER_ID_MESA_RADV) {
  429. LOG_WARNING(
  430. Render_Vulkan,
  431. "Blacklisting RADV for VK_EXT_extended_dynamic state, likely due to a bug in yuzu");
  432. ext_extended_dynamic_state = false;
  433. }
  434. if (is_float16_supported && driver_id == VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS) {
  435. // Intel's compiler crashes when using fp16 on Astral Chain, disable it for the time being.
  436. // LOG_WARNING(Render_Vulkan, "Blacklisting Intel proprietary from float16 math");
  437. // is_float16_supported = false;
  438. }
  439. graphics_queue = logical.GetQueue(graphics_family);
  440. present_queue = logical.GetQueue(present_family);
  441. }
  442. Device::~Device() = default;
  443. VkFormat Device::GetSupportedFormat(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  444. FormatType format_type) const {
  445. if (IsFormatSupported(wanted_format, wanted_usage, format_type)) {
  446. return wanted_format;
  447. }
  448. // The wanted format is not supported by hardware, search for alternatives
  449. const VkFormat* alternatives = GetFormatAlternatives(wanted_format);
  450. if (alternatives == nullptr) {
  451. UNREACHABLE_MSG("Format={} with usage={} and type={} has no defined alternatives and host "
  452. "hardware does not support it",
  453. wanted_format, wanted_usage, format_type);
  454. return wanted_format;
  455. }
  456. std::size_t i = 0;
  457. for (VkFormat alternative = *alternatives; alternative; alternative = alternatives[++i]) {
  458. if (!IsFormatSupported(alternative, wanted_usage, format_type)) {
  459. continue;
  460. }
  461. LOG_WARNING(Render_Vulkan,
  462. "Emulating format={} with alternative format={} with usage={} and type={}",
  463. wanted_format, alternative, wanted_usage, format_type);
  464. return alternative;
  465. }
  466. // No alternatives found, panic
  467. UNREACHABLE_MSG("Format={} with usage={} and type={} is not supported by the host hardware and "
  468. "doesn't support any of the alternatives",
  469. wanted_format, wanted_usage, format_type);
  470. return wanted_format;
  471. }
  472. void Device::ReportLoss() const {
  473. LOG_CRITICAL(Render_Vulkan, "Device loss occured!");
  474. // Wait for the log to flush and for Nsight Aftermath to dump the results
  475. std::this_thread::sleep_for(std::chrono::seconds{15});
  476. }
  477. void Device::SaveShader(std::span<const u32> spirv) const {
  478. if (nsight_aftermath_tracker) {
  479. nsight_aftermath_tracker->SaveShader(spirv);
  480. }
  481. }
  482. bool Device::IsOptimalAstcSupported(const VkPhysicalDeviceFeatures& features) const {
  483. // Disable for now to avoid converting ASTC twice.
  484. static constexpr std::array astc_formats = {
  485. VK_FORMAT_ASTC_4x4_UNORM_BLOCK, VK_FORMAT_ASTC_4x4_SRGB_BLOCK,
  486. VK_FORMAT_ASTC_5x4_UNORM_BLOCK, VK_FORMAT_ASTC_5x4_SRGB_BLOCK,
  487. VK_FORMAT_ASTC_5x5_UNORM_BLOCK, VK_FORMAT_ASTC_5x5_SRGB_BLOCK,
  488. VK_FORMAT_ASTC_6x5_UNORM_BLOCK, VK_FORMAT_ASTC_6x5_SRGB_BLOCK,
  489. VK_FORMAT_ASTC_6x6_UNORM_BLOCK, VK_FORMAT_ASTC_6x6_SRGB_BLOCK,
  490. VK_FORMAT_ASTC_8x5_UNORM_BLOCK, VK_FORMAT_ASTC_8x5_SRGB_BLOCK,
  491. VK_FORMAT_ASTC_8x6_UNORM_BLOCK, VK_FORMAT_ASTC_8x6_SRGB_BLOCK,
  492. VK_FORMAT_ASTC_8x8_UNORM_BLOCK, VK_FORMAT_ASTC_8x8_SRGB_BLOCK,
  493. VK_FORMAT_ASTC_10x5_UNORM_BLOCK, VK_FORMAT_ASTC_10x5_SRGB_BLOCK,
  494. VK_FORMAT_ASTC_10x6_UNORM_BLOCK, VK_FORMAT_ASTC_10x6_SRGB_BLOCK,
  495. VK_FORMAT_ASTC_10x8_UNORM_BLOCK, VK_FORMAT_ASTC_10x8_SRGB_BLOCK,
  496. VK_FORMAT_ASTC_10x10_UNORM_BLOCK, VK_FORMAT_ASTC_10x10_SRGB_BLOCK,
  497. VK_FORMAT_ASTC_12x10_UNORM_BLOCK, VK_FORMAT_ASTC_12x10_SRGB_BLOCK,
  498. VK_FORMAT_ASTC_12x12_UNORM_BLOCK, VK_FORMAT_ASTC_12x12_SRGB_BLOCK,
  499. };
  500. if (!features.textureCompressionASTC_LDR) {
  501. return false;
  502. }
  503. const auto format_feature_usage{
  504. VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_BLIT_SRC_BIT |
  505. VK_FORMAT_FEATURE_BLIT_DST_BIT | VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
  506. VK_FORMAT_FEATURE_TRANSFER_DST_BIT};
  507. for (const auto format : astc_formats) {
  508. const auto physical_format_properties{physical.GetFormatProperties(format)};
  509. if ((physical_format_properties.optimalTilingFeatures & format_feature_usage) == 0) {
  510. return false;
  511. }
  512. }
  513. return true;
  514. }
  515. bool Device::TestDepthStencilBlits() const {
  516. static constexpr VkFormatFeatureFlags required_features =
  517. VK_FORMAT_FEATURE_BLIT_SRC_BIT | VK_FORMAT_FEATURE_BLIT_DST_BIT;
  518. const auto test_features = [](VkFormatProperties props) {
  519. return (props.optimalTilingFeatures & required_features) == required_features;
  520. };
  521. return test_features(format_properties.at(VK_FORMAT_D32_SFLOAT_S8_UINT)) &&
  522. test_features(format_properties.at(VK_FORMAT_D24_UNORM_S8_UINT));
  523. }
  524. bool Device::IsFormatSupported(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  525. FormatType format_type) const {
  526. const auto it = format_properties.find(wanted_format);
  527. if (it == format_properties.end()) {
  528. UNIMPLEMENTED_MSG("Unimplemented format query={}", wanted_format);
  529. return true;
  530. }
  531. const auto supported_usage = GetFormatFeatures(it->second, format_type);
  532. return (supported_usage & wanted_usage) == wanted_usage;
  533. }
  534. std::string Device::GetDriverName() const {
  535. switch (driver_id) {
  536. case VK_DRIVER_ID_AMD_PROPRIETARY:
  537. return "AMD";
  538. case VK_DRIVER_ID_AMD_OPEN_SOURCE:
  539. return "AMDVLK";
  540. case VK_DRIVER_ID_MESA_RADV:
  541. return "RADV";
  542. case VK_DRIVER_ID_NVIDIA_PROPRIETARY:
  543. return "NVIDIA";
  544. case VK_DRIVER_ID_INTEL_PROPRIETARY_WINDOWS:
  545. return "INTEL";
  546. case VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA:
  547. return "ANV";
  548. case VK_DRIVER_ID_MESA_LLVMPIPE:
  549. return "LAVAPIPE";
  550. default:
  551. return vendor_name;
  552. }
  553. }
  554. void Device::CheckSuitability(bool requires_swapchain) const {
  555. std::bitset<REQUIRED_EXTENSIONS.size()> available_extensions;
  556. bool has_swapchain = false;
  557. for (const VkExtensionProperties& property : physical.EnumerateDeviceExtensionProperties()) {
  558. const std::string_view name{property.extensionName};
  559. for (size_t i = 0; i < REQUIRED_EXTENSIONS.size(); ++i) {
  560. if (available_extensions[i]) {
  561. continue;
  562. }
  563. available_extensions[i] = name == REQUIRED_EXTENSIONS[i];
  564. }
  565. has_swapchain = has_swapchain || name == VK_KHR_SWAPCHAIN_EXTENSION_NAME;
  566. }
  567. for (size_t i = 0; i < REQUIRED_EXTENSIONS.size(); ++i) {
  568. if (available_extensions[i]) {
  569. continue;
  570. }
  571. LOG_ERROR(Render_Vulkan, "Missing required extension: {}", REQUIRED_EXTENSIONS[i]);
  572. throw vk::Exception(VK_ERROR_EXTENSION_NOT_PRESENT);
  573. }
  574. if (requires_swapchain && !has_swapchain) {
  575. LOG_ERROR(Render_Vulkan, "Missing required extension: VK_KHR_swapchain");
  576. throw vk::Exception(VK_ERROR_EXTENSION_NOT_PRESENT);
  577. }
  578. struct LimitTuple {
  579. u32 minimum;
  580. u32 value;
  581. const char* name;
  582. };
  583. const VkPhysicalDeviceLimits& limits{properties.limits};
  584. const std::array limits_report{
  585. LimitTuple{65536, limits.maxUniformBufferRange, "maxUniformBufferRange"},
  586. LimitTuple{16, limits.maxViewports, "maxViewports"},
  587. LimitTuple{8, limits.maxColorAttachments, "maxColorAttachments"},
  588. LimitTuple{8, limits.maxClipDistances, "maxClipDistances"},
  589. };
  590. for (const auto& tuple : limits_report) {
  591. if (tuple.value < tuple.minimum) {
  592. LOG_ERROR(Render_Vulkan, "{} has to be {} or greater but it is {}", tuple.name,
  593. tuple.minimum, tuple.value);
  594. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  595. }
  596. }
  597. VkPhysicalDeviceShaderDemoteToHelperInvocationFeaturesEXT demote{};
  598. demote.sType =
  599. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DEMOTE_TO_HELPER_INVOCATION_FEATURES_EXT;
  600. demote.pNext = nullptr;
  601. VkPhysicalDeviceVariablePointerFeaturesKHR variable_pointers{};
  602. variable_pointers.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTERS_FEATURES_KHR;
  603. variable_pointers.pNext = &demote;
  604. VkPhysicalDeviceRobustness2FeaturesEXT robustness2{};
  605. robustness2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ROBUSTNESS_2_FEATURES_EXT;
  606. robustness2.pNext = &variable_pointers;
  607. VkPhysicalDeviceFeatures2KHR features2{};
  608. features2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
  609. features2.pNext = &robustness2;
  610. physical.GetFeatures2KHR(features2);
  611. const VkPhysicalDeviceFeatures& features{features2.features};
  612. const std::array feature_report{
  613. std::make_pair(features.robustBufferAccess, "robustBufferAccess"),
  614. std::make_pair(features.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics"),
  615. std::make_pair(features.robustBufferAccess, "robustBufferAccess"),
  616. std::make_pair(features.imageCubeArray, "imageCubeArray"),
  617. std::make_pair(features.independentBlend, "independentBlend"),
  618. std::make_pair(features.depthClamp, "depthClamp"),
  619. std::make_pair(features.samplerAnisotropy, "samplerAnisotropy"),
  620. std::make_pair(features.largePoints, "largePoints"),
  621. std::make_pair(features.multiViewport, "multiViewport"),
  622. std::make_pair(features.depthBiasClamp, "depthBiasClamp"),
  623. std::make_pair(features.fillModeNonSolid, "fillModeNonSolid"),
  624. std::make_pair(features.geometryShader, "geometryShader"),
  625. std::make_pair(features.tessellationShader, "tessellationShader"),
  626. std::make_pair(features.sampleRateShading, "sampleRateShading"),
  627. std::make_pair(features.dualSrcBlend, "dualSrcBlend"),
  628. std::make_pair(features.occlusionQueryPrecise, "occlusionQueryPrecise"),
  629. std::make_pair(features.fragmentStoresAndAtomics, "fragmentStoresAndAtomics"),
  630. std::make_pair(features.shaderImageGatherExtended, "shaderImageGatherExtended"),
  631. std::make_pair(features.shaderStorageImageWriteWithoutFormat,
  632. "shaderStorageImageWriteWithoutFormat"),
  633. std::make_pair(demote.shaderDemoteToHelperInvocation, "shaderDemoteToHelperInvocation"),
  634. std::make_pair(variable_pointers.variablePointers, "variablePointers"),
  635. std::make_pair(variable_pointers.variablePointersStorageBuffer,
  636. "variablePointersStorageBuffer"),
  637. std::make_pair(robustness2.robustBufferAccess2, "robustBufferAccess2"),
  638. std::make_pair(robustness2.robustImageAccess2, "robustImageAccess2"),
  639. std::make_pair(robustness2.nullDescriptor, "nullDescriptor"),
  640. };
  641. for (const auto& [is_supported, name] : feature_report) {
  642. if (is_supported) {
  643. continue;
  644. }
  645. LOG_ERROR(Render_Vulkan, "Missing required feature: {}", name);
  646. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  647. }
  648. }
  649. std::vector<const char*> Device::LoadExtensions(bool requires_surface) {
  650. std::vector<const char*> extensions;
  651. extensions.reserve(8 + REQUIRED_EXTENSIONS.size());
  652. extensions.insert(extensions.begin(), REQUIRED_EXTENSIONS.begin(), REQUIRED_EXTENSIONS.end());
  653. if (requires_surface) {
  654. extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
  655. }
  656. bool has_khr_shader_float16_int8{};
  657. bool has_khr_workgroup_memory_explicit_layout{};
  658. bool has_ext_subgroup_size_control{};
  659. bool has_ext_transform_feedback{};
  660. bool has_ext_custom_border_color{};
  661. bool has_ext_extended_dynamic_state{};
  662. bool has_ext_shader_atomic_int64{};
  663. for (const VkExtensionProperties& extension : physical.EnumerateDeviceExtensionProperties()) {
  664. const auto test = [&](std::optional<std::reference_wrapper<bool>> status, const char* name,
  665. bool push) {
  666. if (extension.extensionName != std::string_view(name)) {
  667. return;
  668. }
  669. if (push) {
  670. extensions.push_back(name);
  671. }
  672. if (status) {
  673. status->get() = true;
  674. }
  675. };
  676. test(nv_viewport_swizzle, VK_NV_VIEWPORT_SWIZZLE_EXTENSION_NAME, true);
  677. test(nv_viewport_array2, VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME, true);
  678. test(khr_uniform_buffer_standard_layout,
  679. VK_KHR_UNIFORM_BUFFER_STANDARD_LAYOUT_EXTENSION_NAME, true);
  680. test(khr_spirv_1_4, VK_KHR_SPIRV_1_4_EXTENSION_NAME, true);
  681. test(has_khr_shader_float16_int8, VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME, false);
  682. test(ext_depth_range_unrestricted, VK_EXT_DEPTH_RANGE_UNRESTRICTED_EXTENSION_NAME, true);
  683. test(ext_index_type_uint8, VK_EXT_INDEX_TYPE_UINT8_EXTENSION_NAME, true);
  684. test(ext_sampler_filter_minmax, VK_EXT_SAMPLER_FILTER_MINMAX_EXTENSION_NAME, true);
  685. test(ext_shader_viewport_index_layer, VK_EXT_SHADER_VIEWPORT_INDEX_LAYER_EXTENSION_NAME,
  686. true);
  687. test(ext_tooling_info, VK_EXT_TOOLING_INFO_EXTENSION_NAME, true);
  688. test(ext_shader_stencil_export, VK_EXT_SHADER_STENCIL_EXPORT_EXTENSION_NAME, true);
  689. test(has_ext_transform_feedback, VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME, false);
  690. test(has_ext_custom_border_color, VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME, false);
  691. test(has_ext_extended_dynamic_state, VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME, false);
  692. test(has_ext_subgroup_size_control, VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME, false);
  693. test(has_ext_shader_atomic_int64, VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME, false);
  694. test(has_khr_workgroup_memory_explicit_layout,
  695. VK_KHR_WORKGROUP_MEMORY_EXPLICIT_LAYOUT_EXTENSION_NAME, false);
  696. if (Settings::values.renderer_debug) {
  697. test(nv_device_diagnostics_config, VK_NV_DEVICE_DIAGNOSTICS_CONFIG_EXTENSION_NAME,
  698. true);
  699. }
  700. }
  701. VkPhysicalDeviceFeatures2KHR features{};
  702. features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2_KHR;
  703. VkPhysicalDeviceProperties2KHR physical_properties;
  704. physical_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR;
  705. if (has_khr_shader_float16_int8) {
  706. VkPhysicalDeviceFloat16Int8FeaturesKHR float16_int8_features;
  707. float16_int8_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT16_INT8_FEATURES_KHR;
  708. float16_int8_features.pNext = nullptr;
  709. features.pNext = &float16_int8_features;
  710. physical.GetFeatures2KHR(features);
  711. is_float16_supported = float16_int8_features.shaderFloat16;
  712. extensions.push_back(VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME);
  713. }
  714. if (has_ext_subgroup_size_control) {
  715. VkPhysicalDeviceSubgroupSizeControlFeaturesEXT subgroup_features;
  716. subgroup_features.sType =
  717. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT;
  718. subgroup_features.pNext = nullptr;
  719. features.pNext = &subgroup_features;
  720. physical.GetFeatures2KHR(features);
  721. VkPhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_properties;
  722. subgroup_properties.sType =
  723. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_PROPERTIES_EXT;
  724. subgroup_properties.pNext = nullptr;
  725. physical_properties.pNext = &subgroup_properties;
  726. physical.GetProperties2KHR(physical_properties);
  727. is_warp_potentially_bigger = subgroup_properties.maxSubgroupSize > GuestWarpSize;
  728. if (subgroup_features.subgroupSizeControl &&
  729. subgroup_properties.minSubgroupSize <= GuestWarpSize &&
  730. subgroup_properties.maxSubgroupSize >= GuestWarpSize) {
  731. extensions.push_back(VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME);
  732. guest_warp_stages = subgroup_properties.requiredSubgroupSizeStages;
  733. ext_subgroup_size_control = true;
  734. }
  735. } else {
  736. is_warp_potentially_bigger = true;
  737. }
  738. if (has_ext_shader_atomic_int64) {
  739. VkPhysicalDeviceShaderAtomicInt64Features atomic_int64;
  740. atomic_int64.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_FEATURES_EXT;
  741. atomic_int64.pNext = nullptr;
  742. features.pNext = &atomic_int64;
  743. physical.GetFeatures2KHR(features);
  744. if (atomic_int64.shaderBufferInt64Atomics && atomic_int64.shaderSharedInt64Atomics) {
  745. extensions.push_back(VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME);
  746. ext_shader_atomic_int64 = true;
  747. }
  748. }
  749. if (has_ext_transform_feedback) {
  750. VkPhysicalDeviceTransformFeedbackFeaturesEXT tfb_features;
  751. tfb_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT;
  752. tfb_features.pNext = nullptr;
  753. features.pNext = &tfb_features;
  754. physical.GetFeatures2KHR(features);
  755. VkPhysicalDeviceTransformFeedbackPropertiesEXT tfb_properties;
  756. tfb_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT;
  757. tfb_properties.pNext = nullptr;
  758. physical_properties.pNext = &tfb_properties;
  759. physical.GetProperties2KHR(physical_properties);
  760. if (tfb_features.transformFeedback && tfb_features.geometryStreams &&
  761. tfb_properties.maxTransformFeedbackStreams >= 4 &&
  762. tfb_properties.maxTransformFeedbackBuffers && tfb_properties.transformFeedbackQueries &&
  763. tfb_properties.transformFeedbackDraw) {
  764. extensions.push_back(VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME);
  765. ext_transform_feedback = true;
  766. }
  767. }
  768. if (has_ext_custom_border_color) {
  769. VkPhysicalDeviceCustomBorderColorFeaturesEXT border_features;
  770. border_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CUSTOM_BORDER_COLOR_FEATURES_EXT;
  771. border_features.pNext = nullptr;
  772. features.pNext = &border_features;
  773. physical.GetFeatures2KHR(features);
  774. if (border_features.customBorderColors && border_features.customBorderColorWithoutFormat) {
  775. extensions.push_back(VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME);
  776. ext_custom_border_color = true;
  777. }
  778. }
  779. if (has_ext_extended_dynamic_state) {
  780. VkPhysicalDeviceExtendedDynamicStateFeaturesEXT dynamic_state;
  781. dynamic_state.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_FEATURES_EXT;
  782. dynamic_state.pNext = nullptr;
  783. features.pNext = &dynamic_state;
  784. physical.GetFeatures2KHR(features);
  785. if (dynamic_state.extendedDynamicState) {
  786. extensions.push_back(VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
  787. ext_extended_dynamic_state = true;
  788. }
  789. }
  790. if (has_khr_workgroup_memory_explicit_layout) {
  791. VkPhysicalDeviceWorkgroupMemoryExplicitLayoutFeaturesKHR layout;
  792. layout.sType =
  793. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_WORKGROUP_MEMORY_EXPLICIT_LAYOUT_FEATURES_KHR;
  794. layout.pNext = nullptr;
  795. features.pNext = &layout;
  796. physical.GetFeatures2KHR(features);
  797. if (layout.workgroupMemoryExplicitLayout &&
  798. layout.workgroupMemoryExplicitLayout8BitAccess &&
  799. layout.workgroupMemoryExplicitLayout16BitAccess &&
  800. layout.workgroupMemoryExplicitLayoutScalarBlockLayout) {
  801. extensions.push_back(VK_KHR_WORKGROUP_MEMORY_EXPLICIT_LAYOUT_EXTENSION_NAME);
  802. khr_workgroup_memory_explicit_layout = true;
  803. }
  804. }
  805. return extensions;
  806. }
  807. void Device::SetupFamilies(VkSurfaceKHR surface) {
  808. const std::vector queue_family_properties = physical.GetQueueFamilyProperties();
  809. std::optional<u32> graphics;
  810. std::optional<u32> present;
  811. for (u32 index = 0; index < static_cast<u32>(queue_family_properties.size()); ++index) {
  812. if (graphics && (present || !surface)) {
  813. break;
  814. }
  815. const VkQueueFamilyProperties& queue_family = queue_family_properties[index];
  816. if (queue_family.queueCount == 0) {
  817. continue;
  818. }
  819. if (queue_family.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
  820. graphics = index;
  821. }
  822. if (surface && physical.GetSurfaceSupportKHR(index, surface)) {
  823. present = index;
  824. }
  825. }
  826. if (!graphics) {
  827. LOG_ERROR(Render_Vulkan, "Device lacks a graphics queue");
  828. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  829. }
  830. if (surface && !present) {
  831. LOG_ERROR(Render_Vulkan, "Device lacks a present queue");
  832. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  833. }
  834. graphics_family = *graphics;
  835. present_family = *present;
  836. }
  837. void Device::SetupFeatures() {
  838. const VkPhysicalDeviceFeatures features{physical.GetFeatures()};
  839. is_formatless_image_load_supported = features.shaderStorageImageReadWithoutFormat;
  840. is_shader_storage_image_multisample = features.shaderStorageImageMultisample;
  841. is_blit_depth_stencil_supported = TestDepthStencilBlits();
  842. is_optimal_astc_supported = IsOptimalAstcSupported(features);
  843. }
  844. void Device::SetupProperties() {
  845. float_controls.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT_CONTROLS_PROPERTIES_KHR;
  846. VkPhysicalDeviceProperties2KHR properties2{};
  847. properties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR;
  848. properties2.pNext = &float_controls;
  849. physical.GetProperties2KHR(properties2);
  850. }
  851. void Device::CollectTelemetryParameters() {
  852. VkPhysicalDeviceDriverPropertiesKHR driver{
  853. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR,
  854. .pNext = nullptr,
  855. .driverID = {},
  856. .driverName = {},
  857. .driverInfo = {},
  858. .conformanceVersion = {},
  859. };
  860. VkPhysicalDeviceProperties2KHR device_properties{
  861. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
  862. .pNext = &driver,
  863. .properties = {},
  864. };
  865. physical.GetProperties2KHR(device_properties);
  866. driver_id = driver.driverID;
  867. vendor_name = driver.driverName;
  868. const std::vector extensions = physical.EnumerateDeviceExtensionProperties();
  869. reported_extensions.reserve(std::size(extensions));
  870. for (const auto& extension : extensions) {
  871. reported_extensions.emplace_back(extension.extensionName);
  872. }
  873. }
  874. void Device::CollectPhysicalMemoryInfo() {
  875. const auto mem_properties = physical.GetMemoryProperties();
  876. const size_t num_properties = mem_properties.memoryHeapCount;
  877. device_access_memory = 0;
  878. for (size_t element = 0; element < num_properties; ++element) {
  879. if ((mem_properties.memoryHeaps[element].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) != 0) {
  880. device_access_memory += mem_properties.memoryHeaps[element].size;
  881. }
  882. }
  883. }
  884. void Device::CollectToolingInfo() {
  885. if (!ext_tooling_info) {
  886. return;
  887. }
  888. const auto vkGetPhysicalDeviceToolPropertiesEXT =
  889. reinterpret_cast<PFN_vkGetPhysicalDeviceToolPropertiesEXT>(
  890. dld.vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceToolPropertiesEXT"));
  891. if (!vkGetPhysicalDeviceToolPropertiesEXT) {
  892. return;
  893. }
  894. u32 tool_count = 0;
  895. if (vkGetPhysicalDeviceToolPropertiesEXT(physical, &tool_count, nullptr) != VK_SUCCESS) {
  896. return;
  897. }
  898. std::vector<VkPhysicalDeviceToolPropertiesEXT> tools(tool_count);
  899. if (vkGetPhysicalDeviceToolPropertiesEXT(physical, &tool_count, tools.data()) != VK_SUCCESS) {
  900. return;
  901. }
  902. for (const VkPhysicalDeviceToolPropertiesEXT& tool : tools) {
  903. const std::string_view name = tool.name;
  904. LOG_INFO(Render_Vulkan, "{}", name);
  905. has_renderdoc = has_renderdoc || name == "RenderDoc";
  906. has_nsight_graphics = has_nsight_graphics || name == "NVIDIA Nsight Graphics";
  907. }
  908. }
  909. std::vector<VkDeviceQueueCreateInfo> Device::GetDeviceQueueCreateInfos() const {
  910. static constexpr float QUEUE_PRIORITY = 1.0f;
  911. std::unordered_set<u32> unique_queue_families{graphics_family, present_family};
  912. std::vector<VkDeviceQueueCreateInfo> queue_cis;
  913. queue_cis.reserve(unique_queue_families.size());
  914. for (const u32 queue_family : unique_queue_families) {
  915. auto& ci = queue_cis.emplace_back(VkDeviceQueueCreateInfo{
  916. .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
  917. .pNext = nullptr,
  918. .flags = 0,
  919. .queueFamilyIndex = queue_family,
  920. .queueCount = 1,
  921. .pQueuePriorities = nullptr,
  922. });
  923. ci.pQueuePriorities = &QUEUE_PRIORITY;
  924. }
  925. return queue_cis;
  926. }
  927. } // namespace Vulkan