vk_device.cpp 29 KB

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