vulkan_device.cpp 54 KB

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