vulkan_device.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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 = false,
  209. .dualSrcBlend = false,
  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.occlusionQueryPrecise, "occlusionQueryPrecise"),
  627. std::make_pair(features.fragmentStoresAndAtomics, "fragmentStoresAndAtomics"),
  628. std::make_pair(features.shaderImageGatherExtended, "shaderImageGatherExtended"),
  629. std::make_pair(features.shaderStorageImageWriteWithoutFormat,
  630. "shaderStorageImageWriteWithoutFormat"),
  631. std::make_pair(demote.shaderDemoteToHelperInvocation, "shaderDemoteToHelperInvocation"),
  632. std::make_pair(variable_pointers.variablePointers, "variablePointers"),
  633. std::make_pair(variable_pointers.variablePointersStorageBuffer,
  634. "variablePointersStorageBuffer"),
  635. std::make_pair(robustness2.robustBufferAccess2, "robustBufferAccess2"),
  636. std::make_pair(robustness2.robustImageAccess2, "robustImageAccess2"),
  637. std::make_pair(robustness2.nullDescriptor, "nullDescriptor"),
  638. };
  639. for (const auto& [is_supported, name] : feature_report) {
  640. if (is_supported) {
  641. continue;
  642. }
  643. LOG_ERROR(Render_Vulkan, "Missing required feature: {}", name);
  644. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  645. }
  646. }
  647. std::vector<const char*> Device::LoadExtensions(bool requires_surface) {
  648. std::vector<const char*> extensions;
  649. extensions.reserve(8 + REQUIRED_EXTENSIONS.size());
  650. extensions.insert(extensions.begin(), REQUIRED_EXTENSIONS.begin(), REQUIRED_EXTENSIONS.end());
  651. if (requires_surface) {
  652. extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
  653. }
  654. bool has_khr_shader_float16_int8{};
  655. bool has_khr_workgroup_memory_explicit_layout{};
  656. bool has_ext_subgroup_size_control{};
  657. bool has_ext_transform_feedback{};
  658. bool has_ext_custom_border_color{};
  659. bool has_ext_extended_dynamic_state{};
  660. bool has_ext_shader_atomic_int64{};
  661. for (const VkExtensionProperties& extension : physical.EnumerateDeviceExtensionProperties()) {
  662. const auto test = [&](std::optional<std::reference_wrapper<bool>> status, const char* name,
  663. bool push) {
  664. if (extension.extensionName != std::string_view(name)) {
  665. return;
  666. }
  667. if (push) {
  668. extensions.push_back(name);
  669. }
  670. if (status) {
  671. status->get() = true;
  672. }
  673. };
  674. test(nv_viewport_swizzle, VK_NV_VIEWPORT_SWIZZLE_EXTENSION_NAME, true);
  675. test(nv_viewport_array2, VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME, true);
  676. test(khr_uniform_buffer_standard_layout,
  677. VK_KHR_UNIFORM_BUFFER_STANDARD_LAYOUT_EXTENSION_NAME, true);
  678. test(khr_spirv_1_4, VK_KHR_SPIRV_1_4_EXTENSION_NAME, true);
  679. test(has_khr_shader_float16_int8, VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME, false);
  680. test(ext_depth_range_unrestricted, VK_EXT_DEPTH_RANGE_UNRESTRICTED_EXTENSION_NAME, true);
  681. test(ext_index_type_uint8, VK_EXT_INDEX_TYPE_UINT8_EXTENSION_NAME, true);
  682. test(ext_sampler_filter_minmax, VK_EXT_SAMPLER_FILTER_MINMAX_EXTENSION_NAME, true);
  683. test(ext_shader_viewport_index_layer, VK_EXT_SHADER_VIEWPORT_INDEX_LAYER_EXTENSION_NAME,
  684. true);
  685. test(ext_tooling_info, VK_EXT_TOOLING_INFO_EXTENSION_NAME, true);
  686. test(ext_shader_stencil_export, VK_EXT_SHADER_STENCIL_EXPORT_EXTENSION_NAME, true);
  687. test(has_ext_transform_feedback, VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME, false);
  688. test(has_ext_custom_border_color, VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME, false);
  689. test(has_ext_extended_dynamic_state, VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME, false);
  690. test(has_ext_subgroup_size_control, VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME, false);
  691. test(has_ext_shader_atomic_int64, VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME, false);
  692. test(has_khr_workgroup_memory_explicit_layout,
  693. VK_KHR_WORKGROUP_MEMORY_EXPLICIT_LAYOUT_EXTENSION_NAME, false);
  694. if (Settings::values.renderer_debug) {
  695. test(nv_device_diagnostics_config, VK_NV_DEVICE_DIAGNOSTICS_CONFIG_EXTENSION_NAME,
  696. true);
  697. }
  698. }
  699. VkPhysicalDeviceFeatures2KHR features{};
  700. features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2_KHR;
  701. VkPhysicalDeviceProperties2KHR physical_properties;
  702. physical_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR;
  703. if (has_khr_shader_float16_int8) {
  704. VkPhysicalDeviceFloat16Int8FeaturesKHR float16_int8_features;
  705. float16_int8_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT16_INT8_FEATURES_KHR;
  706. float16_int8_features.pNext = nullptr;
  707. features.pNext = &float16_int8_features;
  708. physical.GetFeatures2KHR(features);
  709. is_float16_supported = float16_int8_features.shaderFloat16;
  710. extensions.push_back(VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME);
  711. }
  712. if (has_ext_subgroup_size_control) {
  713. VkPhysicalDeviceSubgroupSizeControlFeaturesEXT subgroup_features;
  714. subgroup_features.sType =
  715. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT;
  716. subgroup_features.pNext = nullptr;
  717. features.pNext = &subgroup_features;
  718. physical.GetFeatures2KHR(features);
  719. VkPhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_properties;
  720. subgroup_properties.sType =
  721. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_PROPERTIES_EXT;
  722. subgroup_properties.pNext = nullptr;
  723. physical_properties.pNext = &subgroup_properties;
  724. physical.GetProperties2KHR(physical_properties);
  725. is_warp_potentially_bigger = subgroup_properties.maxSubgroupSize > GuestWarpSize;
  726. if (subgroup_features.subgroupSizeControl &&
  727. subgroup_properties.minSubgroupSize <= GuestWarpSize &&
  728. subgroup_properties.maxSubgroupSize >= GuestWarpSize) {
  729. extensions.push_back(VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME);
  730. guest_warp_stages = subgroup_properties.requiredSubgroupSizeStages;
  731. ext_subgroup_size_control = true;
  732. }
  733. } else {
  734. is_warp_potentially_bigger = true;
  735. }
  736. if (has_ext_shader_atomic_int64) {
  737. VkPhysicalDeviceShaderAtomicInt64Features atomic_int64;
  738. atomic_int64.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_FEATURES_EXT;
  739. atomic_int64.pNext = nullptr;
  740. features.pNext = &atomic_int64;
  741. physical.GetFeatures2KHR(features);
  742. if (atomic_int64.shaderBufferInt64Atomics && atomic_int64.shaderSharedInt64Atomics) {
  743. extensions.push_back(VK_KHR_SHADER_ATOMIC_INT64_EXTENSION_NAME);
  744. ext_shader_atomic_int64 = true;
  745. }
  746. }
  747. if (has_ext_transform_feedback) {
  748. VkPhysicalDeviceTransformFeedbackFeaturesEXT tfb_features;
  749. tfb_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT;
  750. tfb_features.pNext = nullptr;
  751. features.pNext = &tfb_features;
  752. physical.GetFeatures2KHR(features);
  753. VkPhysicalDeviceTransformFeedbackPropertiesEXT tfb_properties;
  754. tfb_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT;
  755. tfb_properties.pNext = nullptr;
  756. physical_properties.pNext = &tfb_properties;
  757. physical.GetProperties2KHR(physical_properties);
  758. if (tfb_features.transformFeedback && tfb_features.geometryStreams &&
  759. tfb_properties.maxTransformFeedbackStreams >= 4 &&
  760. tfb_properties.maxTransformFeedbackBuffers && tfb_properties.transformFeedbackQueries &&
  761. tfb_properties.transformFeedbackDraw) {
  762. extensions.push_back(VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME);
  763. ext_transform_feedback = true;
  764. }
  765. }
  766. if (has_ext_custom_border_color) {
  767. VkPhysicalDeviceCustomBorderColorFeaturesEXT border_features;
  768. border_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CUSTOM_BORDER_COLOR_FEATURES_EXT;
  769. border_features.pNext = nullptr;
  770. features.pNext = &border_features;
  771. physical.GetFeatures2KHR(features);
  772. if (border_features.customBorderColors && border_features.customBorderColorWithoutFormat) {
  773. extensions.push_back(VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME);
  774. ext_custom_border_color = true;
  775. }
  776. }
  777. if (has_ext_extended_dynamic_state) {
  778. VkPhysicalDeviceExtendedDynamicStateFeaturesEXT dynamic_state;
  779. dynamic_state.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_FEATURES_EXT;
  780. dynamic_state.pNext = nullptr;
  781. features.pNext = &dynamic_state;
  782. physical.GetFeatures2KHR(features);
  783. if (dynamic_state.extendedDynamicState) {
  784. extensions.push_back(VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
  785. ext_extended_dynamic_state = true;
  786. }
  787. }
  788. if (has_khr_workgroup_memory_explicit_layout) {
  789. VkPhysicalDeviceWorkgroupMemoryExplicitLayoutFeaturesKHR layout;
  790. layout.sType =
  791. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_WORKGROUP_MEMORY_EXPLICIT_LAYOUT_FEATURES_KHR;
  792. layout.pNext = nullptr;
  793. features.pNext = &layout;
  794. physical.GetFeatures2KHR(features);
  795. if (layout.workgroupMemoryExplicitLayout &&
  796. layout.workgroupMemoryExplicitLayout8BitAccess &&
  797. layout.workgroupMemoryExplicitLayout16BitAccess &&
  798. layout.workgroupMemoryExplicitLayoutScalarBlockLayout) {
  799. extensions.push_back(VK_KHR_WORKGROUP_MEMORY_EXPLICIT_LAYOUT_EXTENSION_NAME);
  800. khr_workgroup_memory_explicit_layout = true;
  801. }
  802. }
  803. return extensions;
  804. }
  805. void Device::SetupFamilies(VkSurfaceKHR surface) {
  806. const std::vector queue_family_properties = physical.GetQueueFamilyProperties();
  807. std::optional<u32> graphics;
  808. std::optional<u32> present;
  809. for (u32 index = 0; index < static_cast<u32>(queue_family_properties.size()); ++index) {
  810. if (graphics && (present || !surface)) {
  811. break;
  812. }
  813. const VkQueueFamilyProperties& queue_family = queue_family_properties[index];
  814. if (queue_family.queueCount == 0) {
  815. continue;
  816. }
  817. if (queue_family.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
  818. graphics = index;
  819. }
  820. if (surface && physical.GetSurfaceSupportKHR(index, surface)) {
  821. present = index;
  822. }
  823. }
  824. if (!graphics) {
  825. LOG_ERROR(Render_Vulkan, "Device lacks a graphics queue");
  826. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  827. }
  828. if (surface && !present) {
  829. LOG_ERROR(Render_Vulkan, "Device lacks a present queue");
  830. throw vk::Exception(VK_ERROR_FEATURE_NOT_PRESENT);
  831. }
  832. graphics_family = *graphics;
  833. present_family = *present;
  834. }
  835. void Device::SetupFeatures() {
  836. const VkPhysicalDeviceFeatures features{physical.GetFeatures()};
  837. is_formatless_image_load_supported = features.shaderStorageImageReadWithoutFormat;
  838. is_shader_storage_image_multisample = features.shaderStorageImageMultisample;
  839. is_blit_depth_stencil_supported = TestDepthStencilBlits();
  840. is_optimal_astc_supported = IsOptimalAstcSupported(features);
  841. }
  842. void Device::SetupProperties() {
  843. float_controls.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT_CONTROLS_PROPERTIES_KHR;
  844. VkPhysicalDeviceProperties2KHR properties2{};
  845. properties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR;
  846. properties2.pNext = &float_controls;
  847. physical.GetProperties2KHR(properties2);
  848. }
  849. void Device::CollectTelemetryParameters() {
  850. VkPhysicalDeviceDriverPropertiesKHR driver{
  851. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR,
  852. .pNext = nullptr,
  853. .driverID = {},
  854. .driverName = {},
  855. .driverInfo = {},
  856. .conformanceVersion = {},
  857. };
  858. VkPhysicalDeviceProperties2KHR device_properties{
  859. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
  860. .pNext = &driver,
  861. .properties = {},
  862. };
  863. physical.GetProperties2KHR(device_properties);
  864. driver_id = driver.driverID;
  865. vendor_name = driver.driverName;
  866. const std::vector extensions = physical.EnumerateDeviceExtensionProperties();
  867. reported_extensions.reserve(std::size(extensions));
  868. for (const auto& extension : extensions) {
  869. reported_extensions.emplace_back(extension.extensionName);
  870. }
  871. }
  872. void Device::CollectPhysicalMemoryInfo() {
  873. const auto mem_properties = physical.GetMemoryProperties();
  874. const size_t num_properties = mem_properties.memoryHeapCount;
  875. device_access_memory = 0;
  876. for (size_t element = 0; element < num_properties; ++element) {
  877. if ((mem_properties.memoryHeaps[element].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) != 0) {
  878. device_access_memory += mem_properties.memoryHeaps[element].size;
  879. }
  880. }
  881. }
  882. void Device::CollectToolingInfo() {
  883. if (!ext_tooling_info) {
  884. return;
  885. }
  886. const auto vkGetPhysicalDeviceToolPropertiesEXT =
  887. reinterpret_cast<PFN_vkGetPhysicalDeviceToolPropertiesEXT>(
  888. dld.vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceToolPropertiesEXT"));
  889. if (!vkGetPhysicalDeviceToolPropertiesEXT) {
  890. return;
  891. }
  892. u32 tool_count = 0;
  893. if (vkGetPhysicalDeviceToolPropertiesEXT(physical, &tool_count, nullptr) != VK_SUCCESS) {
  894. return;
  895. }
  896. std::vector<VkPhysicalDeviceToolPropertiesEXT> tools(tool_count);
  897. if (vkGetPhysicalDeviceToolPropertiesEXT(physical, &tool_count, tools.data()) != VK_SUCCESS) {
  898. return;
  899. }
  900. for (const VkPhysicalDeviceToolPropertiesEXT& tool : tools) {
  901. const std::string_view name = tool.name;
  902. LOG_INFO(Render_Vulkan, "{}", name);
  903. has_renderdoc = has_renderdoc || name == "RenderDoc";
  904. has_nsight_graphics = has_nsight_graphics || name == "NVIDIA Nsight Graphics";
  905. }
  906. }
  907. std::vector<VkDeviceQueueCreateInfo> Device::GetDeviceQueueCreateInfos() const {
  908. static constexpr float QUEUE_PRIORITY = 1.0f;
  909. std::unordered_set<u32> unique_queue_families{graphics_family, present_family};
  910. std::vector<VkDeviceQueueCreateInfo> queue_cis;
  911. queue_cis.reserve(unique_queue_families.size());
  912. for (const u32 queue_family : unique_queue_families) {
  913. auto& ci = queue_cis.emplace_back(VkDeviceQueueCreateInfo{
  914. .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
  915. .pNext = nullptr,
  916. .flags = 0,
  917. .queueFamilyIndex = queue_family,
  918. .queueCount = 1,
  919. .pQueuePriorities = nullptr,
  920. });
  921. ci.pQueuePriorities = &QUEUE_PRIORITY;
  922. }
  923. return queue_cis;
  924. }
  925. } // namespace Vulkan