vk_device.cpp 31 KB

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