vk_device.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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 "core/settings.h"
  14. #include "video_core/renderer_vulkan/vk_device.h"
  15. #include "video_core/renderer_vulkan/wrapper.h"
  16. namespace Vulkan {
  17. namespace {
  18. namespace Alternatives {
  19. constexpr std::array Depth24UnormS8_UINT{
  20. VK_FORMAT_D32_SFLOAT_S8_UINT,
  21. VK_FORMAT_D16_UNORM_S8_UINT,
  22. VkFormat{},
  23. };
  24. constexpr std::array Depth16UnormS8_UINT{
  25. VK_FORMAT_D24_UNORM_S8_UINT,
  26. VK_FORMAT_D32_SFLOAT_S8_UINT,
  27. VkFormat{},
  28. };
  29. } // namespace Alternatives
  30. constexpr std::array REQUIRED_EXTENSIONS{
  31. VK_KHR_SWAPCHAIN_EXTENSION_NAME,
  32. VK_KHR_16BIT_STORAGE_EXTENSION_NAME,
  33. VK_KHR_8BIT_STORAGE_EXTENSION_NAME,
  34. VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME,
  35. VK_KHR_DESCRIPTOR_UPDATE_TEMPLATE_EXTENSION_NAME,
  36. VK_KHR_TIMELINE_SEMAPHORE_EXTENSION_NAME,
  37. VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME,
  38. VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME,
  39. VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME,
  40. VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME,
  41. };
  42. template <typename T>
  43. void SetNext(void**& next, T& data) {
  44. *next = &data;
  45. next = &data.pNext;
  46. }
  47. constexpr const VkFormat* GetFormatAlternatives(VkFormat format) {
  48. switch (format) {
  49. case VK_FORMAT_D24_UNORM_S8_UINT:
  50. return Alternatives::Depth24UnormS8_UINT.data();
  51. case VK_FORMAT_D16_UNORM_S8_UINT:
  52. return Alternatives::Depth16UnormS8_UINT.data();
  53. default:
  54. return nullptr;
  55. }
  56. }
  57. VkFormatFeatureFlags GetFormatFeatures(VkFormatProperties properties, FormatType format_type) {
  58. switch (format_type) {
  59. case FormatType::Linear:
  60. return properties.linearTilingFeatures;
  61. case FormatType::Optimal:
  62. return properties.optimalTilingFeatures;
  63. case FormatType::Buffer:
  64. return properties.bufferFeatures;
  65. default:
  66. return {};
  67. }
  68. }
  69. [[nodiscard]] bool IsRDNA(std::string_view device_name, VkDriverIdKHR driver_id) {
  70. static constexpr std::array RDNA_DEVICES{
  71. "5700",
  72. "5600",
  73. "5500",
  74. "5300",
  75. };
  76. if (driver_id != VK_DRIVER_ID_AMD_PROPRIETARY_KHR) {
  77. return false;
  78. }
  79. return std::any_of(RDNA_DEVICES.begin(), RDNA_DEVICES.end(), [device_name](const char* name) {
  80. return device_name.find(name) != std::string_view::npos;
  81. });
  82. }
  83. std::unordered_map<VkFormat, VkFormatProperties> GetFormatProperties(
  84. vk::PhysicalDevice physical, const vk::InstanceDispatch& dld) {
  85. static constexpr std::array formats{
  86. VK_FORMAT_A8B8G8R8_UNORM_PACK32,
  87. VK_FORMAT_A8B8G8R8_UINT_PACK32,
  88. VK_FORMAT_A8B8G8R8_SNORM_PACK32,
  89. VK_FORMAT_A8B8G8R8_SINT_PACK32,
  90. VK_FORMAT_A8B8G8R8_SRGB_PACK32,
  91. VK_FORMAT_B5G6R5_UNORM_PACK16,
  92. VK_FORMAT_A2B10G10R10_UNORM_PACK32,
  93. VK_FORMAT_A2B10G10R10_UINT_PACK32,
  94. VK_FORMAT_A1R5G5B5_UNORM_PACK16,
  95. VK_FORMAT_R32G32B32A32_SFLOAT,
  96. VK_FORMAT_R32G32B32A32_SINT,
  97. VK_FORMAT_R32G32B32A32_UINT,
  98. VK_FORMAT_R32G32_SFLOAT,
  99. VK_FORMAT_R32G32_SINT,
  100. VK_FORMAT_R32G32_UINT,
  101. VK_FORMAT_R16G16B16A16_SINT,
  102. VK_FORMAT_R16G16B16A16_UINT,
  103. VK_FORMAT_R16G16B16A16_SNORM,
  104. VK_FORMAT_R16G16B16A16_UNORM,
  105. VK_FORMAT_R16G16_UNORM,
  106. VK_FORMAT_R16G16_SNORM,
  107. VK_FORMAT_R16G16_SFLOAT,
  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_SRGB_BLOCK,
  148. VK_FORMAT_ASTC_8x8_SRGB_BLOCK,
  149. VK_FORMAT_ASTC_8x5_SRGB_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_10x8_UNORM_BLOCK,
  154. VK_FORMAT_ASTC_10x8_SRGB_BLOCK,
  155. VK_FORMAT_ASTC_6x6_UNORM_BLOCK,
  156. VK_FORMAT_ASTC_6x6_SRGB_BLOCK,
  157. VK_FORMAT_ASTC_10x10_UNORM_BLOCK,
  158. VK_FORMAT_ASTC_10x10_SRGB_BLOCK,
  159. VK_FORMAT_ASTC_12x12_UNORM_BLOCK,
  160. VK_FORMAT_ASTC_12x12_SRGB_BLOCK,
  161. VK_FORMAT_ASTC_8x6_UNORM_BLOCK,
  162. VK_FORMAT_ASTC_8x6_SRGB_BLOCK,
  163. VK_FORMAT_ASTC_6x5_UNORM_BLOCK,
  164. VK_FORMAT_ASTC_6x5_SRGB_BLOCK,
  165. VK_FORMAT_E5B9G9R9_UFLOAT_PACK32,
  166. };
  167. std::unordered_map<VkFormat, VkFormatProperties> format_properties;
  168. for (const auto format : formats) {
  169. format_properties.emplace(format, physical.GetFormatProperties(format));
  170. }
  171. return format_properties;
  172. }
  173. } // Anonymous namespace
  174. VKDevice::VKDevice(VkInstance instance, vk::PhysicalDevice physical, VkSurfaceKHR surface,
  175. const vk::InstanceDispatch& dld)
  176. : dld{dld}, physical{physical}, properties{physical.GetProperties()},
  177. format_properties{GetFormatProperties(physical, dld)} {
  178. SetupFamilies(surface);
  179. SetupFeatures();
  180. }
  181. VKDevice::~VKDevice() = default;
  182. bool VKDevice::Create() {
  183. const auto queue_cis = GetDeviceQueueCreateInfos();
  184. const std::vector extensions = LoadExtensions();
  185. VkPhysicalDeviceFeatures2 features2{
  186. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2,
  187. .pNext = nullptr,
  188. };
  189. const void* first_next = &features2;
  190. void** next = &features2.pNext;
  191. features2.features = {
  192. .robustBufferAccess = false,
  193. .fullDrawIndexUint32 = false,
  194. .imageCubeArray = false,
  195. .independentBlend = true,
  196. .geometryShader = true,
  197. .tessellationShader = true,
  198. .sampleRateShading = false,
  199. .dualSrcBlend = false,
  200. .logicOp = false,
  201. .multiDrawIndirect = false,
  202. .drawIndirectFirstInstance = false,
  203. .depthClamp = true,
  204. .depthBiasClamp = true,
  205. .fillModeNonSolid = false,
  206. .depthBounds = false,
  207. .wideLines = false,
  208. .largePoints = true,
  209. .alphaToOne = false,
  210. .multiViewport = true,
  211. .samplerAnisotropy = true,
  212. .textureCompressionETC2 = false,
  213. .textureCompressionASTC_LDR = is_optimal_astc_supported,
  214. .textureCompressionBC = false,
  215. .occlusionQueryPrecise = true,
  216. .pipelineStatisticsQuery = false,
  217. .vertexPipelineStoresAndAtomics = true,
  218. .fragmentStoresAndAtomics = true,
  219. .shaderTessellationAndGeometryPointSize = false,
  220. .shaderImageGatherExtended = true,
  221. .shaderStorageImageExtendedFormats = false,
  222. .shaderStorageImageMultisample = false,
  223. .shaderStorageImageReadWithoutFormat = is_formatless_image_load_supported,
  224. .shaderStorageImageWriteWithoutFormat = true,
  225. .shaderUniformBufferArrayDynamicIndexing = false,
  226. .shaderSampledImageArrayDynamicIndexing = false,
  227. .shaderStorageBufferArrayDynamicIndexing = false,
  228. .shaderStorageImageArrayDynamicIndexing = false,
  229. .shaderClipDistance = false,
  230. .shaderCullDistance = false,
  231. .shaderFloat64 = false,
  232. .shaderInt64 = false,
  233. .shaderInt16 = false,
  234. .shaderResourceResidency = false,
  235. .shaderResourceMinLod = false,
  236. .sparseBinding = false,
  237. .sparseResidencyBuffer = false,
  238. .sparseResidencyImage2D = false,
  239. .sparseResidencyImage3D = false,
  240. .sparseResidency2Samples = false,
  241. .sparseResidency4Samples = false,
  242. .sparseResidency8Samples = false,
  243. .sparseResidency16Samples = false,
  244. .sparseResidencyAliased = false,
  245. .variableMultisampleRate = false,
  246. .inheritedQueries = false,
  247. };
  248. VkPhysicalDeviceTimelineSemaphoreFeaturesKHR timeline_semaphore{
  249. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TIMELINE_SEMAPHORE_FEATURES_KHR,
  250. .pNext = nullptr,
  251. .timelineSemaphore = true,
  252. };
  253. SetNext(next, timeline_semaphore);
  254. VkPhysicalDevice16BitStorageFeaturesKHR bit16_storage{
  255. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES_KHR,
  256. .pNext = nullptr,
  257. .storageBuffer16BitAccess = false,
  258. .uniformAndStorageBuffer16BitAccess = true,
  259. .storagePushConstant16 = false,
  260. .storageInputOutput16 = false,
  261. };
  262. SetNext(next, bit16_storage);
  263. VkPhysicalDevice8BitStorageFeaturesKHR bit8_storage{
  264. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES_KHR,
  265. .pNext = nullptr,
  266. .storageBuffer8BitAccess = false,
  267. .uniformAndStorageBuffer8BitAccess = true,
  268. .storagePushConstant8 = false,
  269. };
  270. SetNext(next, bit8_storage);
  271. VkPhysicalDeviceHostQueryResetFeaturesEXT host_query_reset{
  272. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_HOST_QUERY_RESET_FEATURES_EXT,
  273. .hostQueryReset = true,
  274. };
  275. SetNext(next, host_query_reset);
  276. VkPhysicalDeviceFloat16Int8FeaturesKHR float16_int8;
  277. if (is_float16_supported) {
  278. float16_int8 = {
  279. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT16_INT8_FEATURES_KHR,
  280. .pNext = nullptr,
  281. .shaderFloat16 = true,
  282. .shaderInt8 = false,
  283. };
  284. SetNext(next, float16_int8);
  285. } else {
  286. LOG_INFO(Render_Vulkan, "Device doesn't support float16 natively");
  287. }
  288. if (!nv_viewport_swizzle) {
  289. LOG_INFO(Render_Vulkan, "Device doesn't support viewport swizzles");
  290. }
  291. VkPhysicalDeviceUniformBufferStandardLayoutFeaturesKHR std430_layout;
  292. if (khr_uniform_buffer_standard_layout) {
  293. std430_layout = {
  294. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_UNIFORM_BUFFER_STANDARD_LAYOUT_FEATURES_KHR,
  295. .pNext = nullptr,
  296. .uniformBufferStandardLayout = true,
  297. };
  298. SetNext(next, std430_layout);
  299. } else {
  300. LOG_INFO(Render_Vulkan, "Device doesn't support packed UBOs");
  301. }
  302. VkPhysicalDeviceIndexTypeUint8FeaturesEXT index_type_uint8;
  303. if (ext_index_type_uint8) {
  304. index_type_uint8 = {
  305. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_INDEX_TYPE_UINT8_FEATURES_EXT,
  306. .pNext = nullptr,
  307. .indexTypeUint8 = true,
  308. };
  309. SetNext(next, index_type_uint8);
  310. } else {
  311. LOG_INFO(Render_Vulkan, "Device doesn't support uint8 indexes");
  312. }
  313. VkPhysicalDeviceTransformFeedbackFeaturesEXT transform_feedback;
  314. if (ext_transform_feedback) {
  315. transform_feedback = {
  316. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT,
  317. .pNext = nullptr,
  318. .transformFeedback = true,
  319. .geometryStreams = true,
  320. };
  321. SetNext(next, transform_feedback);
  322. } else {
  323. LOG_INFO(Render_Vulkan, "Device doesn't support transform feedbacks");
  324. }
  325. VkPhysicalDeviceCustomBorderColorFeaturesEXT custom_border;
  326. if (ext_custom_border_color) {
  327. custom_border = {
  328. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CUSTOM_BORDER_COLOR_FEATURES_EXT,
  329. .pNext = nullptr,
  330. .customBorderColors = VK_TRUE,
  331. .customBorderColorWithoutFormat = VK_TRUE,
  332. };
  333. SetNext(next, custom_border);
  334. } else {
  335. LOG_INFO(Render_Vulkan, "Device doesn't support custom border colors");
  336. }
  337. VkPhysicalDeviceExtendedDynamicStateFeaturesEXT dynamic_state;
  338. if (ext_extended_dynamic_state) {
  339. dynamic_state = {
  340. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_FEATURES_EXT,
  341. .pNext = nullptr,
  342. .extendedDynamicState = VK_TRUE,
  343. };
  344. SetNext(next, dynamic_state);
  345. } else {
  346. LOG_INFO(Render_Vulkan, "Device doesn't support extended dynamic state");
  347. }
  348. if (!ext_depth_range_unrestricted) {
  349. LOG_INFO(Render_Vulkan, "Device doesn't support depth range unrestricted");
  350. }
  351. VkDeviceDiagnosticsConfigCreateInfoNV diagnostics_nv;
  352. if (nv_device_diagnostics_config) {
  353. nsight_aftermath_tracker.Initialize();
  354. diagnostics_nv = {
  355. .sType = VK_STRUCTURE_TYPE_DEVICE_DIAGNOSTICS_CONFIG_CREATE_INFO_NV,
  356. .pNext = &features2,
  357. .flags = VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_SHADER_DEBUG_INFO_BIT_NV |
  358. VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_RESOURCE_TRACKING_BIT_NV |
  359. VK_DEVICE_DIAGNOSTICS_CONFIG_ENABLE_AUTOMATIC_CHECKPOINTS_BIT_NV,
  360. };
  361. first_next = &diagnostics_nv;
  362. }
  363. logical = vk::Device::Create(physical, queue_cis, extensions, first_next, dld);
  364. if (!logical) {
  365. LOG_ERROR(Render_Vulkan, "Failed to create logical device");
  366. return false;
  367. }
  368. CollectTelemetryParameters();
  369. if (ext_extended_dynamic_state && IsRDNA(properties.deviceName, driver_id)) {
  370. // AMD's proprietary driver supports VK_EXT_extended_dynamic_state but on RDNA devices it
  371. // seems to cause stability issues
  372. LOG_WARNING(
  373. Render_Vulkan,
  374. "Blacklisting AMD proprietary on RDNA devices from VK_EXT_extended_dynamic_state");
  375. ext_extended_dynamic_state = false;
  376. }
  377. graphics_queue = logical.GetQueue(graphics_family);
  378. present_queue = logical.GetQueue(present_family);
  379. use_asynchronous_shaders = Settings::values.use_asynchronous_shaders.GetValue();
  380. return true;
  381. }
  382. VkFormat VKDevice::GetSupportedFormat(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  383. FormatType format_type) const {
  384. if (IsFormatSupported(wanted_format, wanted_usage, format_type)) {
  385. return wanted_format;
  386. }
  387. // The wanted format is not supported by hardware, search for alternatives
  388. const VkFormat* alternatives = GetFormatAlternatives(wanted_format);
  389. if (alternatives == nullptr) {
  390. UNREACHABLE_MSG("Format={} with usage={} and type={} has no defined alternatives and host "
  391. "hardware does not support it",
  392. wanted_format, wanted_usage, format_type);
  393. return wanted_format;
  394. }
  395. std::size_t i = 0;
  396. for (VkFormat alternative = *alternatives; alternative; alternative = alternatives[++i]) {
  397. if (!IsFormatSupported(alternative, wanted_usage, format_type)) {
  398. continue;
  399. }
  400. LOG_WARNING(Render_Vulkan,
  401. "Emulating format={} with alternative format={} with usage={} and type={}",
  402. wanted_format, alternative, wanted_usage, format_type);
  403. return alternative;
  404. }
  405. // No alternatives found, panic
  406. UNREACHABLE_MSG("Format={} with usage={} and type={} is not supported by the host hardware and "
  407. "doesn't support any of the alternatives",
  408. wanted_format, wanted_usage, format_type);
  409. return wanted_format;
  410. }
  411. void VKDevice::ReportLoss() const {
  412. LOG_CRITICAL(Render_Vulkan, "Device loss occured!");
  413. // Wait for the log to flush and for Nsight Aftermath to dump the results
  414. std::this_thread::sleep_for(std::chrono::seconds{3});
  415. }
  416. void VKDevice::SaveShader(const std::vector<u32>& spirv) const {
  417. nsight_aftermath_tracker.SaveShader(spirv);
  418. }
  419. bool VKDevice::IsOptimalAstcSupported(const VkPhysicalDeviceFeatures& features) const {
  420. // Disable for now to avoid converting ASTC twice.
  421. static constexpr std::array astc_formats = {
  422. VK_FORMAT_ASTC_4x4_UNORM_BLOCK, VK_FORMAT_ASTC_4x4_SRGB_BLOCK,
  423. VK_FORMAT_ASTC_5x4_UNORM_BLOCK, VK_FORMAT_ASTC_5x4_SRGB_BLOCK,
  424. VK_FORMAT_ASTC_5x5_UNORM_BLOCK, VK_FORMAT_ASTC_5x5_SRGB_BLOCK,
  425. VK_FORMAT_ASTC_6x5_UNORM_BLOCK, VK_FORMAT_ASTC_6x5_SRGB_BLOCK,
  426. VK_FORMAT_ASTC_6x6_UNORM_BLOCK, VK_FORMAT_ASTC_6x6_SRGB_BLOCK,
  427. VK_FORMAT_ASTC_8x5_UNORM_BLOCK, VK_FORMAT_ASTC_8x5_SRGB_BLOCK,
  428. VK_FORMAT_ASTC_8x6_UNORM_BLOCK, VK_FORMAT_ASTC_8x6_SRGB_BLOCK,
  429. VK_FORMAT_ASTC_8x8_UNORM_BLOCK, VK_FORMAT_ASTC_8x8_SRGB_BLOCK,
  430. VK_FORMAT_ASTC_10x5_UNORM_BLOCK, VK_FORMAT_ASTC_10x5_SRGB_BLOCK,
  431. VK_FORMAT_ASTC_10x6_UNORM_BLOCK, VK_FORMAT_ASTC_10x6_SRGB_BLOCK,
  432. VK_FORMAT_ASTC_10x8_UNORM_BLOCK, VK_FORMAT_ASTC_10x8_SRGB_BLOCK,
  433. VK_FORMAT_ASTC_10x10_UNORM_BLOCK, VK_FORMAT_ASTC_10x10_SRGB_BLOCK,
  434. VK_FORMAT_ASTC_12x10_UNORM_BLOCK, VK_FORMAT_ASTC_12x10_SRGB_BLOCK,
  435. VK_FORMAT_ASTC_12x12_UNORM_BLOCK, VK_FORMAT_ASTC_12x12_SRGB_BLOCK,
  436. };
  437. if (!features.textureCompressionASTC_LDR) {
  438. return false;
  439. }
  440. const auto format_feature_usage{
  441. VK_FORMAT_FEATURE_SAMPLED_IMAGE_BIT | VK_FORMAT_FEATURE_BLIT_SRC_BIT |
  442. VK_FORMAT_FEATURE_BLIT_DST_BIT | VK_FORMAT_FEATURE_TRANSFER_SRC_BIT |
  443. VK_FORMAT_FEATURE_TRANSFER_DST_BIT};
  444. for (const auto format : astc_formats) {
  445. const auto format_properties{physical.GetFormatProperties(format)};
  446. if (!(format_properties.optimalTilingFeatures & format_feature_usage)) {
  447. return false;
  448. }
  449. }
  450. return true;
  451. }
  452. bool VKDevice::IsFormatSupported(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  453. FormatType format_type) const {
  454. const auto it = format_properties.find(wanted_format);
  455. if (it == format_properties.end()) {
  456. UNIMPLEMENTED_MSG("Unimplemented format query={}", wanted_format);
  457. return true;
  458. }
  459. const auto supported_usage = GetFormatFeatures(it->second, format_type);
  460. return (supported_usage & wanted_usage) == wanted_usage;
  461. }
  462. bool VKDevice::IsSuitable(vk::PhysicalDevice physical, VkSurfaceKHR surface) {
  463. bool is_suitable = true;
  464. std::bitset<REQUIRED_EXTENSIONS.size()> available_extensions;
  465. for (const auto& prop : physical.EnumerateDeviceExtensionProperties()) {
  466. for (std::size_t i = 0; i < REQUIRED_EXTENSIONS.size(); ++i) {
  467. if (available_extensions[i]) {
  468. continue;
  469. }
  470. const std::string_view name{prop.extensionName};
  471. available_extensions[i] = name == REQUIRED_EXTENSIONS[i];
  472. }
  473. }
  474. if (!available_extensions.all()) {
  475. for (std::size_t i = 0; i < REQUIRED_EXTENSIONS.size(); ++i) {
  476. if (available_extensions[i]) {
  477. continue;
  478. }
  479. LOG_ERROR(Render_Vulkan, "Missing required extension: {}", REQUIRED_EXTENSIONS[i]);
  480. is_suitable = false;
  481. }
  482. }
  483. bool has_graphics{}, has_present{};
  484. const std::vector queue_family_properties = physical.GetQueueFamilyProperties();
  485. for (u32 i = 0; i < static_cast<u32>(queue_family_properties.size()); ++i) {
  486. const auto& family = queue_family_properties[i];
  487. if (family.queueCount == 0) {
  488. continue;
  489. }
  490. has_graphics |= family.queueFlags & VK_QUEUE_GRAPHICS_BIT;
  491. has_present |= physical.GetSurfaceSupportKHR(i, surface);
  492. }
  493. if (!has_graphics || !has_present) {
  494. LOG_ERROR(Render_Vulkan, "Device lacks a graphics and present queue");
  495. is_suitable = false;
  496. }
  497. // TODO(Rodrigo): Check if the device matches all requeriments.
  498. const auto properties{physical.GetProperties()};
  499. const auto& limits{properties.limits};
  500. constexpr u32 required_ubo_size = 65536;
  501. if (limits.maxUniformBufferRange < required_ubo_size) {
  502. LOG_ERROR(Render_Vulkan, "Device UBO size {} is too small, {} is required",
  503. limits.maxUniformBufferRange, required_ubo_size);
  504. is_suitable = false;
  505. }
  506. constexpr u32 required_num_viewports = 16;
  507. if (limits.maxViewports < required_num_viewports) {
  508. LOG_INFO(Render_Vulkan, "Device number of viewports {} is too small, {} is required",
  509. limits.maxViewports, required_num_viewports);
  510. is_suitable = false;
  511. }
  512. const auto features{physical.GetFeatures()};
  513. const std::array feature_report = {
  514. std::make_pair(features.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics"),
  515. std::make_pair(features.independentBlend, "independentBlend"),
  516. std::make_pair(features.depthClamp, "depthClamp"),
  517. std::make_pair(features.samplerAnisotropy, "samplerAnisotropy"),
  518. std::make_pair(features.largePoints, "largePoints"),
  519. std::make_pair(features.multiViewport, "multiViewport"),
  520. std::make_pair(features.depthBiasClamp, "depthBiasClamp"),
  521. std::make_pair(features.geometryShader, "geometryShader"),
  522. std::make_pair(features.tessellationShader, "tessellationShader"),
  523. std::make_pair(features.occlusionQueryPrecise, "occlusionQueryPrecise"),
  524. std::make_pair(features.fragmentStoresAndAtomics, "fragmentStoresAndAtomics"),
  525. std::make_pair(features.shaderImageGatherExtended, "shaderImageGatherExtended"),
  526. std::make_pair(features.shaderStorageImageWriteWithoutFormat,
  527. "shaderStorageImageWriteWithoutFormat"),
  528. };
  529. for (const auto& [supported, name] : feature_report) {
  530. if (supported) {
  531. continue;
  532. }
  533. LOG_ERROR(Render_Vulkan, "Missing required feature: {}", name);
  534. is_suitable = false;
  535. }
  536. if (!is_suitable) {
  537. LOG_ERROR(Render_Vulkan, "{} is not suitable", properties.deviceName);
  538. }
  539. return is_suitable;
  540. }
  541. std::vector<const char*> VKDevice::LoadExtensions() {
  542. std::vector<const char*> extensions;
  543. const auto Test = [&](const VkExtensionProperties& extension,
  544. std::optional<std::reference_wrapper<bool>> status, const char* name,
  545. bool push) {
  546. if (extension.extensionName != std::string_view(name)) {
  547. return;
  548. }
  549. if (push) {
  550. extensions.push_back(name);
  551. }
  552. if (status) {
  553. status->get() = true;
  554. }
  555. };
  556. extensions.reserve(7 + REQUIRED_EXTENSIONS.size());
  557. extensions.insert(extensions.begin(), REQUIRED_EXTENSIONS.begin(), REQUIRED_EXTENSIONS.end());
  558. bool has_khr_shader_float16_int8{};
  559. bool has_ext_subgroup_size_control{};
  560. bool has_ext_transform_feedback{};
  561. bool has_ext_custom_border_color{};
  562. bool has_ext_extended_dynamic_state{};
  563. for (const auto& extension : physical.EnumerateDeviceExtensionProperties()) {
  564. Test(extension, nv_viewport_swizzle, VK_NV_VIEWPORT_SWIZZLE_EXTENSION_NAME, true);
  565. Test(extension, khr_uniform_buffer_standard_layout,
  566. VK_KHR_UNIFORM_BUFFER_STANDARD_LAYOUT_EXTENSION_NAME, true);
  567. Test(extension, has_khr_shader_float16_int8, VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME,
  568. false);
  569. Test(extension, ext_depth_range_unrestricted,
  570. VK_EXT_DEPTH_RANGE_UNRESTRICTED_EXTENSION_NAME, true);
  571. Test(extension, ext_index_type_uint8, VK_EXT_INDEX_TYPE_UINT8_EXTENSION_NAME, true);
  572. Test(extension, ext_shader_viewport_index_layer,
  573. VK_EXT_SHADER_VIEWPORT_INDEX_LAYER_EXTENSION_NAME, true);
  574. Test(extension, has_ext_subgroup_size_control, VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME,
  575. false);
  576. Test(extension, has_ext_transform_feedback, VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME,
  577. false);
  578. Test(extension, has_ext_custom_border_color, VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME,
  579. false);
  580. Test(extension, has_ext_extended_dynamic_state,
  581. VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME, false);
  582. if (Settings::values.renderer_debug) {
  583. Test(extension, nv_device_diagnostics_config,
  584. VK_NV_DEVICE_DIAGNOSTICS_CONFIG_EXTENSION_NAME, true);
  585. }
  586. }
  587. VkPhysicalDeviceFeatures2KHR features;
  588. features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2_KHR;
  589. VkPhysicalDeviceProperties2KHR properties;
  590. properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR;
  591. if (has_khr_shader_float16_int8) {
  592. VkPhysicalDeviceFloat16Int8FeaturesKHR float16_int8_features;
  593. float16_int8_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FLOAT16_INT8_FEATURES_KHR;
  594. float16_int8_features.pNext = nullptr;
  595. features.pNext = &float16_int8_features;
  596. physical.GetFeatures2KHR(features);
  597. is_float16_supported = float16_int8_features.shaderFloat16;
  598. extensions.push_back(VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME);
  599. }
  600. if (has_ext_subgroup_size_control) {
  601. VkPhysicalDeviceSubgroupSizeControlFeaturesEXT subgroup_features;
  602. subgroup_features.sType =
  603. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT;
  604. subgroup_features.pNext = nullptr;
  605. features.pNext = &subgroup_features;
  606. physical.GetFeatures2KHR(features);
  607. VkPhysicalDeviceSubgroupSizeControlPropertiesEXT subgroup_properties;
  608. subgroup_properties.sType =
  609. VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_PROPERTIES_EXT;
  610. subgroup_properties.pNext = nullptr;
  611. properties.pNext = &subgroup_properties;
  612. physical.GetProperties2KHR(properties);
  613. is_warp_potentially_bigger = subgroup_properties.maxSubgroupSize > GuestWarpSize;
  614. if (subgroup_features.subgroupSizeControl &&
  615. subgroup_properties.minSubgroupSize <= GuestWarpSize &&
  616. subgroup_properties.maxSubgroupSize >= GuestWarpSize) {
  617. extensions.push_back(VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME);
  618. guest_warp_stages = subgroup_properties.requiredSubgroupSizeStages;
  619. }
  620. } else {
  621. is_warp_potentially_bigger = true;
  622. }
  623. if (has_ext_transform_feedback) {
  624. VkPhysicalDeviceTransformFeedbackFeaturesEXT tfb_features;
  625. tfb_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_FEATURES_EXT;
  626. tfb_features.pNext = nullptr;
  627. features.pNext = &tfb_features;
  628. physical.GetFeatures2KHR(features);
  629. VkPhysicalDeviceTransformFeedbackPropertiesEXT tfb_properties;
  630. tfb_properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_TRANSFORM_FEEDBACK_PROPERTIES_EXT;
  631. tfb_properties.pNext = nullptr;
  632. properties.pNext = &tfb_properties;
  633. physical.GetProperties2KHR(properties);
  634. if (tfb_features.transformFeedback && tfb_features.geometryStreams &&
  635. tfb_properties.maxTransformFeedbackStreams >= 4 &&
  636. tfb_properties.maxTransformFeedbackBuffers && tfb_properties.transformFeedbackQueries &&
  637. tfb_properties.transformFeedbackDraw) {
  638. extensions.push_back(VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME);
  639. ext_transform_feedback = true;
  640. }
  641. }
  642. if (has_ext_custom_border_color) {
  643. VkPhysicalDeviceCustomBorderColorFeaturesEXT border_features;
  644. border_features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_CUSTOM_BORDER_COLOR_FEATURES_EXT;
  645. border_features.pNext = nullptr;
  646. features.pNext = &border_features;
  647. physical.GetFeatures2KHR(features);
  648. if (border_features.customBorderColors && border_features.customBorderColorWithoutFormat) {
  649. extensions.push_back(VK_EXT_CUSTOM_BORDER_COLOR_EXTENSION_NAME);
  650. ext_custom_border_color = true;
  651. }
  652. }
  653. if (has_ext_extended_dynamic_state) {
  654. VkPhysicalDeviceExtendedDynamicStateFeaturesEXT dynamic_state;
  655. dynamic_state.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_FEATURES_EXT;
  656. dynamic_state.pNext = nullptr;
  657. features.pNext = &dynamic_state;
  658. physical.GetFeatures2KHR(features);
  659. if (dynamic_state.extendedDynamicState) {
  660. extensions.push_back(VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME);
  661. ext_extended_dynamic_state = true;
  662. }
  663. }
  664. return extensions;
  665. }
  666. void VKDevice::SetupFamilies(VkSurfaceKHR surface) {
  667. std::optional<u32> graphics_family_, present_family_;
  668. const std::vector queue_family_properties = physical.GetQueueFamilyProperties();
  669. for (u32 i = 0; i < static_cast<u32>(queue_family_properties.size()); ++i) {
  670. if (graphics_family_ && present_family_)
  671. break;
  672. const auto& queue_family = queue_family_properties[i];
  673. if (queue_family.queueCount == 0)
  674. continue;
  675. if (queue_family.queueFlags & VK_QUEUE_GRAPHICS_BIT) {
  676. graphics_family_ = i;
  677. }
  678. if (physical.GetSurfaceSupportKHR(i, surface)) {
  679. present_family_ = i;
  680. }
  681. }
  682. ASSERT(graphics_family_ && present_family_);
  683. graphics_family = *graphics_family_;
  684. present_family = *present_family_;
  685. }
  686. void VKDevice::SetupFeatures() {
  687. const auto supported_features{physical.GetFeatures()};
  688. is_formatless_image_load_supported = supported_features.shaderStorageImageReadWithoutFormat;
  689. is_optimal_astc_supported = IsOptimalAstcSupported(supported_features);
  690. }
  691. void VKDevice::CollectTelemetryParameters() {
  692. VkPhysicalDeviceDriverPropertiesKHR driver{
  693. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR,
  694. .pNext = nullptr,
  695. };
  696. VkPhysicalDeviceProperties2KHR properties{
  697. .sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR,
  698. .pNext = &driver,
  699. };
  700. physical.GetProperties2KHR(properties);
  701. driver_id = driver.driverID;
  702. vendor_name = driver.driverName;
  703. const std::vector extensions = physical.EnumerateDeviceExtensionProperties();
  704. reported_extensions.reserve(std::size(extensions));
  705. for (const auto& extension : extensions) {
  706. reported_extensions.emplace_back(extension.extensionName);
  707. }
  708. }
  709. std::vector<VkDeviceQueueCreateInfo> VKDevice::GetDeviceQueueCreateInfos() const {
  710. static constexpr float QUEUE_PRIORITY = 1.0f;
  711. std::unordered_set<u32> unique_queue_families{graphics_family, present_family};
  712. std::vector<VkDeviceQueueCreateInfo> queue_cis;
  713. queue_cis.reserve(unique_queue_families.size());
  714. for (const u32 queue_family : unique_queue_families) {
  715. auto& ci = queue_cis.emplace_back(VkDeviceQueueCreateInfo{
  716. .sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO,
  717. .pNext = nullptr,
  718. .flags = 0,
  719. .queueFamilyIndex = queue_family,
  720. .queueCount = 1,
  721. .pQueuePriorities = nullptr,
  722. });
  723. ci.pQueuePriorities = &QUEUE_PRIORITY;
  724. }
  725. return queue_cis;
  726. }
  727. } // namespace Vulkan