vk_device.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 <cstdlib>
  7. #include <optional>
  8. #include <set>
  9. #include <string_view>
  10. #include <thread>
  11. #include <vector>
  12. #include "common/assert.h"
  13. #include "core/settings.h"
  14. #include "video_core/renderer_vulkan/declarations.h"
  15. #include "video_core/renderer_vulkan/vk_device.h"
  16. namespace Vulkan {
  17. namespace {
  18. namespace Alternatives {
  19. constexpr std::array Depth24UnormS8Uint = {vk::Format::eD32SfloatS8Uint,
  20. vk::Format::eD16UnormS8Uint, vk::Format{}};
  21. constexpr std::array Depth16UnormS8Uint = {vk::Format::eD24UnormS8Uint,
  22. vk::Format::eD32SfloatS8Uint, vk::Format{}};
  23. } // namespace Alternatives
  24. template <typename T>
  25. void SetNext(void**& next, T& data) {
  26. *next = &data;
  27. next = &data.pNext;
  28. }
  29. template <typename T>
  30. T GetFeatures(vk::PhysicalDevice physical, const vk::DispatchLoaderDynamic& dldi) {
  31. vk::PhysicalDeviceFeatures2 features;
  32. T extension_features;
  33. features.pNext = &extension_features;
  34. physical.getFeatures2(&features, dldi);
  35. return extension_features;
  36. }
  37. template <typename T>
  38. T GetProperties(vk::PhysicalDevice physical, const vk::DispatchLoaderDynamic& dldi) {
  39. vk::PhysicalDeviceProperties2 properties;
  40. T extension_properties;
  41. properties.pNext = &extension_properties;
  42. physical.getProperties2(&properties, dldi);
  43. return extension_properties;
  44. }
  45. constexpr const vk::Format* GetFormatAlternatives(vk::Format format) {
  46. switch (format) {
  47. case vk::Format::eD24UnormS8Uint:
  48. return Alternatives::Depth24UnormS8Uint.data();
  49. case vk::Format::eD16UnormS8Uint:
  50. return Alternatives::Depth16UnormS8Uint.data();
  51. default:
  52. return nullptr;
  53. }
  54. }
  55. vk::FormatFeatureFlags GetFormatFeatures(vk::FormatProperties properties, FormatType format_type) {
  56. switch (format_type) {
  57. case FormatType::Linear:
  58. return properties.linearTilingFeatures;
  59. case FormatType::Optimal:
  60. return properties.optimalTilingFeatures;
  61. case FormatType::Buffer:
  62. return properties.bufferFeatures;
  63. default:
  64. return {};
  65. }
  66. }
  67. } // Anonymous namespace
  68. VKDevice::VKDevice(const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDevice physical,
  69. vk::SurfaceKHR surface)
  70. : physical{physical}, properties{physical.getProperties(dldi)},
  71. format_properties{GetFormatProperties(dldi, physical)} {
  72. SetupFamilies(dldi, surface);
  73. SetupFeatures(dldi);
  74. }
  75. VKDevice::~VKDevice() = default;
  76. bool VKDevice::Create(const vk::DispatchLoaderDynamic& dldi, vk::Instance instance) {
  77. const auto queue_cis = GetDeviceQueueCreateInfos();
  78. const std::vector extensions = LoadExtensions(dldi);
  79. vk::PhysicalDeviceFeatures2 features2;
  80. void** next = &features2.pNext;
  81. auto& features = features2.features;
  82. features.vertexPipelineStoresAndAtomics = true;
  83. features.independentBlend = true;
  84. features.depthClamp = true;
  85. features.samplerAnisotropy = true;
  86. features.largePoints = true;
  87. features.multiViewport = true;
  88. features.depthBiasClamp = true;
  89. features.geometryShader = true;
  90. features.tessellationShader = true;
  91. features.occlusionQueryPrecise = true;
  92. features.fragmentStoresAndAtomics = true;
  93. features.shaderImageGatherExtended = true;
  94. features.shaderStorageImageReadWithoutFormat = is_formatless_image_load_supported;
  95. features.shaderStorageImageWriteWithoutFormat = true;
  96. features.textureCompressionASTC_LDR = is_optimal_astc_supported;
  97. vk::PhysicalDevice16BitStorageFeaturesKHR bit16_storage;
  98. bit16_storage.uniformAndStorageBuffer16BitAccess = true;
  99. SetNext(next, bit16_storage);
  100. vk::PhysicalDevice8BitStorageFeaturesKHR bit8_storage;
  101. bit8_storage.uniformAndStorageBuffer8BitAccess = true;
  102. SetNext(next, bit8_storage);
  103. vk::PhysicalDeviceHostQueryResetFeaturesEXT host_query_reset;
  104. host_query_reset.hostQueryReset = true;
  105. SetNext(next, host_query_reset);
  106. vk::PhysicalDeviceFloat16Int8FeaturesKHR float16_int8;
  107. if (is_float16_supported) {
  108. float16_int8.shaderFloat16 = true;
  109. SetNext(next, float16_int8);
  110. } else {
  111. LOG_INFO(Render_Vulkan, "Device doesn't support float16 natively");
  112. }
  113. vk::PhysicalDeviceUniformBufferStandardLayoutFeaturesKHR std430_layout;
  114. if (khr_uniform_buffer_standard_layout) {
  115. std430_layout.uniformBufferStandardLayout = true;
  116. SetNext(next, std430_layout);
  117. } else {
  118. LOG_INFO(Render_Vulkan, "Device doesn't support packed UBOs");
  119. }
  120. vk::PhysicalDeviceIndexTypeUint8FeaturesEXT index_type_uint8;
  121. if (ext_index_type_uint8) {
  122. index_type_uint8.indexTypeUint8 = true;
  123. SetNext(next, index_type_uint8);
  124. } else {
  125. LOG_INFO(Render_Vulkan, "Device doesn't support uint8 indexes");
  126. }
  127. vk::PhysicalDeviceTransformFeedbackFeaturesEXT transform_feedback;
  128. if (ext_transform_feedback) {
  129. transform_feedback.transformFeedback = true;
  130. transform_feedback.geometryStreams = true;
  131. SetNext(next, transform_feedback);
  132. } else {
  133. LOG_INFO(Render_Vulkan, "Device doesn't support transform feedbacks");
  134. }
  135. if (!ext_depth_range_unrestricted) {
  136. LOG_INFO(Render_Vulkan, "Device doesn't support depth range unrestricted");
  137. }
  138. vk::DeviceCreateInfo device_ci({}, static_cast<u32>(queue_cis.size()), queue_cis.data(), 0,
  139. nullptr, static_cast<u32>(extensions.size()), extensions.data(),
  140. nullptr);
  141. device_ci.pNext = &features2;
  142. vk::Device dummy_logical;
  143. if (physical.createDevice(&device_ci, nullptr, &dummy_logical, dldi) != vk::Result::eSuccess) {
  144. LOG_CRITICAL(Render_Vulkan, "Logical device failed to be created!");
  145. return false;
  146. }
  147. dld.init(instance, dldi.vkGetInstanceProcAddr, dummy_logical, dldi.vkGetDeviceProcAddr);
  148. logical = UniqueDevice(
  149. dummy_logical, vk::ObjectDestroy<vk::NoParent, vk::DispatchLoaderDynamic>(nullptr, dld));
  150. CollectTelemetryParameters();
  151. graphics_queue = logical->getQueue(graphics_family, 0, dld);
  152. present_queue = logical->getQueue(present_family, 0, dld);
  153. return true;
  154. }
  155. vk::Format VKDevice::GetSupportedFormat(vk::Format wanted_format,
  156. vk::FormatFeatureFlags wanted_usage,
  157. FormatType format_type) const {
  158. if (IsFormatSupported(wanted_format, wanted_usage, format_type)) {
  159. return wanted_format;
  160. }
  161. // The wanted format is not supported by hardware, search for alternatives
  162. const vk::Format* alternatives = GetFormatAlternatives(wanted_format);
  163. if (alternatives == nullptr) {
  164. UNREACHABLE_MSG("Format={} with usage={} and type={} has no defined alternatives and host "
  165. "hardware does not support it",
  166. vk::to_string(wanted_format), vk::to_string(wanted_usage),
  167. static_cast<u32>(format_type));
  168. return wanted_format;
  169. }
  170. std::size_t i = 0;
  171. for (vk::Format alternative = alternatives[0]; alternative != vk::Format{};
  172. alternative = alternatives[++i]) {
  173. if (!IsFormatSupported(alternative, wanted_usage, format_type)) {
  174. continue;
  175. }
  176. LOG_WARNING(Render_Vulkan,
  177. "Emulating format={} with alternative format={} with usage={} and type={}",
  178. static_cast<u32>(wanted_format), static_cast<u32>(alternative),
  179. static_cast<u32>(wanted_usage), static_cast<u32>(format_type));
  180. return alternative;
  181. }
  182. // No alternatives found, panic
  183. UNREACHABLE_MSG("Format={} with usage={} and type={} is not supported by the host hardware and "
  184. "doesn't support any of the alternatives",
  185. static_cast<u32>(wanted_format), static_cast<u32>(wanted_usage),
  186. static_cast<u32>(format_type));
  187. return wanted_format;
  188. }
  189. void VKDevice::ReportLoss() const {
  190. LOG_CRITICAL(Render_Vulkan, "Device loss occured!");
  191. // Wait some time to let the log flush
  192. std::this_thread::sleep_for(std::chrono::seconds{1});
  193. if (!nv_device_diagnostic_checkpoints) {
  194. return;
  195. }
  196. [[maybe_unused]] const std::vector data = graphics_queue.getCheckpointDataNV(dld);
  197. // Catch here in debug builds (or with optimizations disabled) the last graphics pipeline to be
  198. // executed. It can be done on a debugger by evaluating the expression:
  199. // *(VKGraphicsPipeline*)data[0]
  200. }
  201. bool VKDevice::IsOptimalAstcSupported(const vk::PhysicalDeviceFeatures& features,
  202. const vk::DispatchLoaderDynamic& dldi) const {
  203. // Disable for now to avoid converting ASTC twice.
  204. return false;
  205. static constexpr std::array astc_formats = {
  206. vk::Format::eAstc4x4SrgbBlock, vk::Format::eAstc8x8SrgbBlock,
  207. vk::Format::eAstc8x5SrgbBlock, vk::Format::eAstc5x4SrgbBlock,
  208. vk::Format::eAstc5x5UnormBlock, vk::Format::eAstc5x5SrgbBlock,
  209. vk::Format::eAstc10x8UnormBlock, vk::Format::eAstc10x8SrgbBlock,
  210. vk::Format::eAstc6x6UnormBlock, vk::Format::eAstc6x6SrgbBlock,
  211. vk::Format::eAstc10x10UnormBlock, vk::Format::eAstc10x10SrgbBlock,
  212. vk::Format::eAstc12x12UnormBlock, vk::Format::eAstc12x12SrgbBlock,
  213. vk::Format::eAstc8x6UnormBlock, vk::Format::eAstc8x6SrgbBlock,
  214. vk::Format::eAstc6x5UnormBlock, vk::Format::eAstc6x5SrgbBlock};
  215. if (!features.textureCompressionASTC_LDR) {
  216. return false;
  217. }
  218. const auto format_feature_usage{
  219. vk::FormatFeatureFlagBits::eSampledImage | vk::FormatFeatureFlagBits::eBlitSrc |
  220. vk::FormatFeatureFlagBits::eBlitDst | vk::FormatFeatureFlagBits::eTransferSrc |
  221. vk::FormatFeatureFlagBits::eTransferDst};
  222. for (const auto format : astc_formats) {
  223. const auto format_properties{physical.getFormatProperties(format, dldi)};
  224. if (!(format_properties.optimalTilingFeatures & format_feature_usage)) {
  225. return false;
  226. }
  227. }
  228. return true;
  229. }
  230. bool VKDevice::IsFormatSupported(vk::Format wanted_format, vk::FormatFeatureFlags wanted_usage,
  231. FormatType format_type) const {
  232. const auto it = format_properties.find(wanted_format);
  233. if (it == format_properties.end()) {
  234. UNIMPLEMENTED_MSG("Unimplemented format query={}", vk::to_string(wanted_format));
  235. return true;
  236. }
  237. const auto supported_usage = GetFormatFeatures(it->second, format_type);
  238. return (supported_usage & wanted_usage) == wanted_usage;
  239. }
  240. bool VKDevice::IsSuitable(const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDevice physical,
  241. vk::SurfaceKHR surface) {
  242. bool is_suitable = true;
  243. constexpr std::array required_extensions = {
  244. VK_KHR_SWAPCHAIN_EXTENSION_NAME,
  245. VK_KHR_16BIT_STORAGE_EXTENSION_NAME,
  246. VK_KHR_8BIT_STORAGE_EXTENSION_NAME,
  247. VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME,
  248. VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME,
  249. VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME,
  250. VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME,
  251. VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME,
  252. };
  253. std::bitset<required_extensions.size()> available_extensions{};
  254. for (const auto& prop : physical.enumerateDeviceExtensionProperties(nullptr, dldi)) {
  255. for (std::size_t i = 0; i < required_extensions.size(); ++i) {
  256. if (available_extensions[i]) {
  257. continue;
  258. }
  259. available_extensions[i] =
  260. required_extensions[i] == std::string_view{prop.extensionName};
  261. }
  262. }
  263. if (!available_extensions.all()) {
  264. for (std::size_t i = 0; i < required_extensions.size(); ++i) {
  265. if (available_extensions[i]) {
  266. continue;
  267. }
  268. LOG_ERROR(Render_Vulkan, "Missing required extension: {}", required_extensions[i]);
  269. is_suitable = false;
  270. }
  271. }
  272. bool has_graphics{}, has_present{};
  273. const auto queue_family_properties = physical.getQueueFamilyProperties(dldi);
  274. for (u32 i = 0; i < static_cast<u32>(queue_family_properties.size()); ++i) {
  275. const auto& family = queue_family_properties[i];
  276. if (family.queueCount == 0) {
  277. continue;
  278. }
  279. has_graphics |=
  280. (family.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast<vk::QueueFlagBits>(0);
  281. has_present |= physical.getSurfaceSupportKHR(i, surface, dldi) != 0;
  282. }
  283. if (!has_graphics || !has_present) {
  284. LOG_ERROR(Render_Vulkan, "Device lacks a graphics and present queue");
  285. is_suitable = false;
  286. }
  287. // TODO(Rodrigo): Check if the device matches all requeriments.
  288. const auto properties{physical.getProperties(dldi)};
  289. const auto& limits{properties.limits};
  290. constexpr u32 required_ubo_size = 65536;
  291. if (limits.maxUniformBufferRange < required_ubo_size) {
  292. LOG_ERROR(Render_Vulkan, "Device UBO size {} is too small, {} is required",
  293. limits.maxUniformBufferRange, required_ubo_size);
  294. is_suitable = false;
  295. }
  296. constexpr u32 required_num_viewports = 16;
  297. if (limits.maxViewports < required_num_viewports) {
  298. LOG_INFO(Render_Vulkan, "Device number of viewports {} is too small, {} is required",
  299. limits.maxViewports, required_num_viewports);
  300. is_suitable = false;
  301. }
  302. const auto features{physical.getFeatures(dldi)};
  303. const std::array feature_report = {
  304. std::make_pair(features.vertexPipelineStoresAndAtomics, "vertexPipelineStoresAndAtomics"),
  305. std::make_pair(features.independentBlend, "independentBlend"),
  306. std::make_pair(features.depthClamp, "depthClamp"),
  307. std::make_pair(features.samplerAnisotropy, "samplerAnisotropy"),
  308. std::make_pair(features.largePoints, "largePoints"),
  309. std::make_pair(features.multiViewport, "multiViewport"),
  310. std::make_pair(features.depthBiasClamp, "depthBiasClamp"),
  311. std::make_pair(features.geometryShader, "geometryShader"),
  312. std::make_pair(features.tessellationShader, "tessellationShader"),
  313. std::make_pair(features.occlusionQueryPrecise, "occlusionQueryPrecise"),
  314. std::make_pair(features.fragmentStoresAndAtomics, "fragmentStoresAndAtomics"),
  315. std::make_pair(features.shaderImageGatherExtended, "shaderImageGatherExtended"),
  316. std::make_pair(features.shaderStorageImageWriteWithoutFormat,
  317. "shaderStorageImageWriteWithoutFormat"),
  318. };
  319. for (const auto& [supported, name] : feature_report) {
  320. if (supported) {
  321. continue;
  322. }
  323. LOG_ERROR(Render_Vulkan, "Missing required feature: {}", name);
  324. is_suitable = false;
  325. }
  326. if (!is_suitable) {
  327. LOG_ERROR(Render_Vulkan, "{} is not suitable", properties.deviceName);
  328. }
  329. return is_suitable;
  330. }
  331. std::vector<const char*> VKDevice::LoadExtensions(const vk::DispatchLoaderDynamic& dldi) {
  332. std::vector<const char*> extensions;
  333. const auto Test = [&](const vk::ExtensionProperties& extension,
  334. std::optional<std::reference_wrapper<bool>> status, const char* name,
  335. bool push) {
  336. if (extension.extensionName != std::string_view(name)) {
  337. return;
  338. }
  339. if (push) {
  340. extensions.push_back(name);
  341. }
  342. if (status) {
  343. status->get() = true;
  344. }
  345. };
  346. extensions.reserve(15);
  347. extensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
  348. extensions.push_back(VK_KHR_16BIT_STORAGE_EXTENSION_NAME);
  349. extensions.push_back(VK_KHR_8BIT_STORAGE_EXTENSION_NAME);
  350. extensions.push_back(VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME);
  351. extensions.push_back(VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME);
  352. extensions.push_back(VK_EXT_SHADER_SUBGROUP_BALLOT_EXTENSION_NAME);
  353. extensions.push_back(VK_EXT_SHADER_SUBGROUP_VOTE_EXTENSION_NAME);
  354. extensions.push_back(VK_EXT_HOST_QUERY_RESET_EXTENSION_NAME);
  355. [[maybe_unused]] const bool nsight =
  356. std::getenv("NVTX_INJECTION64_PATH") || std::getenv("NSIGHT_LAUNCHED");
  357. bool has_khr_shader_float16_int8{};
  358. bool has_ext_subgroup_size_control{};
  359. bool has_ext_transform_feedback{};
  360. for (const auto& extension : physical.enumerateDeviceExtensionProperties(nullptr, dldi)) {
  361. Test(extension, khr_uniform_buffer_standard_layout,
  362. VK_KHR_UNIFORM_BUFFER_STANDARD_LAYOUT_EXTENSION_NAME, true);
  363. Test(extension, has_khr_shader_float16_int8, VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME,
  364. false);
  365. Test(extension, ext_depth_range_unrestricted,
  366. VK_EXT_DEPTH_RANGE_UNRESTRICTED_EXTENSION_NAME, true);
  367. Test(extension, ext_index_type_uint8, VK_EXT_INDEX_TYPE_UINT8_EXTENSION_NAME, true);
  368. Test(extension, ext_shader_viewport_index_layer,
  369. VK_EXT_SHADER_VIEWPORT_INDEX_LAYER_EXTENSION_NAME, true);
  370. Test(extension, has_ext_subgroup_size_control, VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME,
  371. false);
  372. Test(extension, has_ext_transform_feedback, VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME,
  373. false);
  374. if (Settings::values.renderer_debug) {
  375. Test(extension, nv_device_diagnostic_checkpoints,
  376. VK_NV_DEVICE_DIAGNOSTIC_CHECKPOINTS_EXTENSION_NAME, true);
  377. }
  378. }
  379. if (has_khr_shader_float16_int8) {
  380. is_float16_supported =
  381. GetFeatures<vk::PhysicalDeviceFloat16Int8FeaturesKHR>(physical, dldi).shaderFloat16;
  382. extensions.push_back(VK_KHR_SHADER_FLOAT16_INT8_EXTENSION_NAME);
  383. }
  384. if (has_ext_subgroup_size_control) {
  385. const auto features =
  386. GetFeatures<vk::PhysicalDeviceSubgroupSizeControlFeaturesEXT>(physical, dldi);
  387. const auto properties =
  388. GetProperties<vk::PhysicalDeviceSubgroupSizeControlPropertiesEXT>(physical, dldi);
  389. is_warp_potentially_bigger = properties.maxSubgroupSize > GuestWarpSize;
  390. if (features.subgroupSizeControl && properties.minSubgroupSize <= GuestWarpSize &&
  391. properties.maxSubgroupSize >= GuestWarpSize) {
  392. extensions.push_back(VK_EXT_SUBGROUP_SIZE_CONTROL_EXTENSION_NAME);
  393. guest_warp_stages = properties.requiredSubgroupSizeStages;
  394. }
  395. } else {
  396. is_warp_potentially_bigger = true;
  397. }
  398. if (has_ext_transform_feedback) {
  399. const auto features =
  400. GetFeatures<vk::PhysicalDeviceTransformFeedbackFeaturesEXT>(physical, dldi);
  401. const auto properties =
  402. GetProperties<vk::PhysicalDeviceTransformFeedbackPropertiesEXT>(physical, dldi);
  403. if (features.transformFeedback && features.geometryStreams &&
  404. properties.maxTransformFeedbackStreams >= 4 && properties.maxTransformFeedbackBuffers &&
  405. properties.transformFeedbackQueries && properties.transformFeedbackDraw) {
  406. extensions.push_back(VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME);
  407. ext_transform_feedback = true;
  408. }
  409. }
  410. return extensions;
  411. }
  412. void VKDevice::SetupFamilies(const vk::DispatchLoaderDynamic& dldi, vk::SurfaceKHR surface) {
  413. std::optional<u32> graphics_family_, present_family_;
  414. const auto queue_family_properties = physical.getQueueFamilyProperties(dldi);
  415. for (u32 i = 0; i < static_cast<u32>(queue_family_properties.size()); ++i) {
  416. if (graphics_family_ && present_family_)
  417. break;
  418. const auto& queue_family = queue_family_properties[i];
  419. if (queue_family.queueCount == 0)
  420. continue;
  421. if (queue_family.queueFlags & vk::QueueFlagBits::eGraphics)
  422. graphics_family_ = i;
  423. if (physical.getSurfaceSupportKHR(i, surface, dldi))
  424. present_family_ = i;
  425. }
  426. ASSERT(graphics_family_ && present_family_);
  427. graphics_family = *graphics_family_;
  428. present_family = *present_family_;
  429. }
  430. void VKDevice::SetupFeatures(const vk::DispatchLoaderDynamic& dldi) {
  431. const auto supported_features{physical.getFeatures(dldi)};
  432. is_formatless_image_load_supported = supported_features.shaderStorageImageReadWithoutFormat;
  433. is_optimal_astc_supported = IsOptimalAstcSupported(supported_features, dldi);
  434. }
  435. void VKDevice::CollectTelemetryParameters() {
  436. const auto driver = GetProperties<vk::PhysicalDeviceDriverPropertiesKHR>(physical, dld);
  437. driver_id = driver.driverID;
  438. vendor_name = driver.driverName;
  439. const auto extensions = physical.enumerateDeviceExtensionProperties(nullptr, dld);
  440. reported_extensions.reserve(std::size(extensions));
  441. for (const auto& extension : extensions) {
  442. reported_extensions.push_back(extension.extensionName);
  443. }
  444. }
  445. std::vector<vk::DeviceQueueCreateInfo> VKDevice::GetDeviceQueueCreateInfos() const {
  446. static const float QUEUE_PRIORITY = 1.0f;
  447. std::set<u32> unique_queue_families = {graphics_family, present_family};
  448. std::vector<vk::DeviceQueueCreateInfo> queue_cis;
  449. for (u32 queue_family : unique_queue_families)
  450. queue_cis.push_back({{}, queue_family, 1, &QUEUE_PRIORITY});
  451. return queue_cis;
  452. }
  453. std::unordered_map<vk::Format, vk::FormatProperties> VKDevice::GetFormatProperties(
  454. const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDevice physical) {
  455. static constexpr std::array formats{vk::Format::eA8B8G8R8UnormPack32,
  456. vk::Format::eA8B8G8R8UintPack32,
  457. vk::Format::eA8B8G8R8SnormPack32,
  458. vk::Format::eA8B8G8R8SrgbPack32,
  459. vk::Format::eB5G6R5UnormPack16,
  460. vk::Format::eA2B10G10R10UnormPack32,
  461. vk::Format::eA1R5G5B5UnormPack16,
  462. vk::Format::eR32G32B32A32Sfloat,
  463. vk::Format::eR32G32B32A32Uint,
  464. vk::Format::eR32G32Sfloat,
  465. vk::Format::eR32G32Uint,
  466. vk::Format::eR16G16B16A16Uint,
  467. vk::Format::eR16G16B16A16Unorm,
  468. vk::Format::eR16G16Unorm,
  469. vk::Format::eR16G16Snorm,
  470. vk::Format::eR16G16Sfloat,
  471. vk::Format::eR16Unorm,
  472. vk::Format::eR8G8B8A8Srgb,
  473. vk::Format::eR8G8Unorm,
  474. vk::Format::eR8G8Snorm,
  475. vk::Format::eR8Unorm,
  476. vk::Format::eR8Uint,
  477. vk::Format::eB10G11R11UfloatPack32,
  478. vk::Format::eR32Sfloat,
  479. vk::Format::eR32Uint,
  480. vk::Format::eR32Sint,
  481. vk::Format::eR16Sfloat,
  482. vk::Format::eR16G16B16A16Sfloat,
  483. vk::Format::eB8G8R8A8Unorm,
  484. vk::Format::eR4G4B4A4UnormPack16,
  485. vk::Format::eD32Sfloat,
  486. vk::Format::eD16Unorm,
  487. vk::Format::eD16UnormS8Uint,
  488. vk::Format::eD24UnormS8Uint,
  489. vk::Format::eD32SfloatS8Uint,
  490. vk::Format::eBc1RgbaUnormBlock,
  491. vk::Format::eBc2UnormBlock,
  492. vk::Format::eBc3UnormBlock,
  493. vk::Format::eBc4UnormBlock,
  494. vk::Format::eBc5UnormBlock,
  495. vk::Format::eBc5SnormBlock,
  496. vk::Format::eBc7UnormBlock,
  497. vk::Format::eBc6HUfloatBlock,
  498. vk::Format::eBc6HSfloatBlock,
  499. vk::Format::eBc1RgbaSrgbBlock,
  500. vk::Format::eBc2SrgbBlock,
  501. vk::Format::eBc3SrgbBlock,
  502. vk::Format::eBc7SrgbBlock,
  503. vk::Format::eAstc4x4SrgbBlock,
  504. vk::Format::eAstc8x8SrgbBlock,
  505. vk::Format::eAstc8x5SrgbBlock,
  506. vk::Format::eAstc5x4SrgbBlock,
  507. vk::Format::eAstc5x5UnormBlock,
  508. vk::Format::eAstc5x5SrgbBlock,
  509. vk::Format::eAstc10x8UnormBlock,
  510. vk::Format::eAstc10x8SrgbBlock,
  511. vk::Format::eAstc6x6UnormBlock,
  512. vk::Format::eAstc6x6SrgbBlock,
  513. vk::Format::eAstc10x10UnormBlock,
  514. vk::Format::eAstc10x10SrgbBlock,
  515. vk::Format::eAstc12x12UnormBlock,
  516. vk::Format::eAstc12x12SrgbBlock,
  517. vk::Format::eAstc8x6UnormBlock,
  518. vk::Format::eAstc8x6SrgbBlock,
  519. vk::Format::eAstc6x5UnormBlock,
  520. vk::Format::eAstc6x5SrgbBlock,
  521. vk::Format::eE5B9G9R9UfloatPack32};
  522. std::unordered_map<vk::Format, vk::FormatProperties> format_properties;
  523. for (const auto format : formats) {
  524. format_properties.emplace(format, physical.getFormatProperties(format, dldi));
  525. }
  526. return format_properties;
  527. }
  528. } // namespace Vulkan