vk_device.cpp 30 KB

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