vulkan_device.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <set>
  5. #include <span>
  6. #include <string>
  7. #include <unordered_map>
  8. #include <vector>
  9. #include "common/common_types.h"
  10. #include "common/settings.h"
  11. #include "video_core/vulkan_common/vulkan_wrapper.h"
  12. VK_DEFINE_HANDLE(VmaAllocator)
  13. // Define all features which may be used by the implementation here.
  14. // Vulkan version in the macro describes the minimum version required for feature availability.
  15. // If the Vulkan version is lower than the required version, the named extension is required.
  16. #define FOR_EACH_VK_FEATURE_1_1(FEATURE) \
  17. FEATURE(EXT, SubgroupSizeControl, SUBGROUP_SIZE_CONTROL, subgroup_size_control) \
  18. FEATURE(KHR, 16BitStorage, 16BIT_STORAGE, bit16_storage) \
  19. FEATURE(KHR, ShaderAtomicInt64, SHADER_ATOMIC_INT64, shader_atomic_int64) \
  20. FEATURE(KHR, ShaderDrawParameters, SHADER_DRAW_PARAMETERS, shader_draw_parameters) \
  21. FEATURE(KHR, ShaderFloat16Int8, SHADER_FLOAT16_INT8, shader_float16_int8) \
  22. FEATURE(KHR, UniformBufferStandardLayout, UNIFORM_BUFFER_STANDARD_LAYOUT, \
  23. uniform_buffer_standard_layout) \
  24. FEATURE(KHR, VariablePointer, VARIABLE_POINTERS, variable_pointer)
  25. #define FOR_EACH_VK_FEATURE_1_2(FEATURE) \
  26. FEATURE(EXT, HostQueryReset, HOST_QUERY_RESET, host_query_reset) \
  27. FEATURE(KHR, 8BitStorage, 8BIT_STORAGE, bit8_storage) \
  28. FEATURE(KHR, TimelineSemaphore, TIMELINE_SEMAPHORE, timeline_semaphore)
  29. #define FOR_EACH_VK_FEATURE_1_3(FEATURE) \
  30. FEATURE(EXT, ShaderDemoteToHelperInvocation, SHADER_DEMOTE_TO_HELPER_INVOCATION, \
  31. shader_demote_to_helper_invocation)
  32. // Define all features which may be used by the implementation and require an extension here.
  33. #define FOR_EACH_VK_FEATURE_EXT(FEATURE) \
  34. FEATURE(EXT, CustomBorderColor, CUSTOM_BORDER_COLOR, custom_border_color) \
  35. FEATURE(EXT, DepthClipControl, DEPTH_CLIP_CONTROL, depth_clip_control) \
  36. FEATURE(EXT, ExtendedDynamicState, EXTENDED_DYNAMIC_STATE, extended_dynamic_state) \
  37. FEATURE(EXT, ExtendedDynamicState2, EXTENDED_DYNAMIC_STATE_2, extended_dynamic_state2) \
  38. FEATURE(EXT, ExtendedDynamicState3, EXTENDED_DYNAMIC_STATE_3, extended_dynamic_state3) \
  39. FEATURE(EXT, IndexTypeUint8, INDEX_TYPE_UINT8, index_type_uint8) \
  40. FEATURE(EXT, LineRasterization, LINE_RASTERIZATION, line_rasterization) \
  41. FEATURE(EXT, PrimitiveTopologyListRestart, PRIMITIVE_TOPOLOGY_LIST_RESTART, \
  42. primitive_topology_list_restart) \
  43. FEATURE(EXT, ProvokingVertex, PROVOKING_VERTEX, provoking_vertex) \
  44. FEATURE(EXT, Robustness2, ROBUSTNESS_2, robustness2) \
  45. FEATURE(EXT, TransformFeedback, TRANSFORM_FEEDBACK, transform_feedback) \
  46. FEATURE(EXT, VertexInputDynamicState, VERTEX_INPUT_DYNAMIC_STATE, vertex_input_dynamic_state) \
  47. FEATURE(KHR, PipelineExecutableProperties, PIPELINE_EXECUTABLE_PROPERTIES, \
  48. pipeline_executable_properties) \
  49. FEATURE(KHR, WorkgroupMemoryExplicitLayout, WORKGROUP_MEMORY_EXPLICIT_LAYOUT, \
  50. workgroup_memory_explicit_layout)
  51. // Define miscellaneous extensions which may be used by the implementation here.
  52. #define FOR_EACH_VK_EXTENSION(EXTENSION) \
  53. EXTENSION(EXT, CONSERVATIVE_RASTERIZATION, conservative_rasterization) \
  54. EXTENSION(EXT, DEPTH_RANGE_UNRESTRICTED, depth_range_unrestricted) \
  55. EXTENSION(EXT, MEMORY_BUDGET, memory_budget) \
  56. EXTENSION(EXT, ROBUSTNESS_2, robustness_2) \
  57. EXTENSION(EXT, SAMPLER_FILTER_MINMAX, sampler_filter_minmax) \
  58. EXTENSION(EXT, SHADER_STENCIL_EXPORT, shader_stencil_export) \
  59. EXTENSION(EXT, SHADER_VIEWPORT_INDEX_LAYER, shader_viewport_index_layer) \
  60. EXTENSION(EXT, TOOLING_INFO, tooling_info) \
  61. EXTENSION(EXT, VERTEX_ATTRIBUTE_DIVISOR, vertex_attribute_divisor) \
  62. EXTENSION(KHR, DRAW_INDIRECT_COUNT, draw_indirect_count) \
  63. EXTENSION(KHR, DRIVER_PROPERTIES, driver_properties) \
  64. EXTENSION(KHR, EXTERNAL_MEMORY_FD, external_memory_fd) \
  65. EXTENSION(KHR, PUSH_DESCRIPTOR, push_descriptor) \
  66. EXTENSION(KHR, SAMPLER_MIRROR_CLAMP_TO_EDGE, sampler_mirror_clamp_to_edge) \
  67. EXTENSION(KHR, SHADER_FLOAT_CONTROLS, shader_float_controls) \
  68. EXTENSION(KHR, SPIRV_1_4, spirv_1_4) \
  69. EXTENSION(KHR, SWAPCHAIN, swapchain) \
  70. EXTENSION(KHR, SWAPCHAIN_MUTABLE_FORMAT, swapchain_mutable_format) \
  71. EXTENSION(NV, DEVICE_DIAGNOSTICS_CONFIG, device_diagnostics_config) \
  72. EXTENSION(NV, GEOMETRY_SHADER_PASSTHROUGH, geometry_shader_passthrough) \
  73. EXTENSION(NV, VIEWPORT_ARRAY2, viewport_array2) \
  74. EXTENSION(NV, VIEWPORT_SWIZZLE, viewport_swizzle)
  75. #define FOR_EACH_VK_EXTENSION_WIN32(EXTENSION) \
  76. EXTENSION(KHR, EXTERNAL_MEMORY_WIN32, external_memory_win32)
  77. // Define extensions which must be supported.
  78. #define FOR_EACH_VK_MANDATORY_EXTENSION(EXTENSION_NAME) \
  79. EXTENSION_NAME(VK_EXT_VERTEX_ATTRIBUTE_DIVISOR_EXTENSION_NAME) \
  80. EXTENSION_NAME(VK_KHR_DRIVER_PROPERTIES_EXTENSION_NAME) \
  81. EXTENSION_NAME(VK_KHR_SAMPLER_MIRROR_CLAMP_TO_EDGE_EXTENSION_NAME) \
  82. EXTENSION_NAME(VK_KHR_SHADER_FLOAT_CONTROLS_EXTENSION_NAME)
  83. #define FOR_EACH_VK_MANDATORY_EXTENSION_GENERIC(EXTENSION_NAME) \
  84. EXTENSION_NAME(VK_KHR_EXTERNAL_MEMORY_FD_EXTENSION_NAME)
  85. #define FOR_EACH_VK_MANDATORY_EXTENSION_WIN32(EXTENSION_NAME) \
  86. EXTENSION_NAME(VK_KHR_EXTERNAL_MEMORY_WIN32_EXTENSION_NAME)
  87. // Define extensions where the absence of the extension may result in a degraded experience.
  88. #define FOR_EACH_VK_RECOMMENDED_EXTENSION(EXTENSION_NAME) \
  89. EXTENSION_NAME(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME) \
  90. EXTENSION_NAME(VK_EXT_DEPTH_RANGE_UNRESTRICTED_EXTENSION_NAME) \
  91. EXTENSION_NAME(VK_EXT_EXTENDED_DYNAMIC_STATE_EXTENSION_NAME) \
  92. EXTENSION_NAME(VK_EXT_EXTENDED_DYNAMIC_STATE_2_EXTENSION_NAME) \
  93. EXTENSION_NAME(VK_EXT_EXTENDED_DYNAMIC_STATE_3_EXTENSION_NAME) \
  94. EXTENSION_NAME(VK_EXT_LINE_RASTERIZATION_EXTENSION_NAME) \
  95. EXTENSION_NAME(VK_EXT_ROBUSTNESS_2_EXTENSION_NAME) \
  96. EXTENSION_NAME(VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME) \
  97. EXTENSION_NAME(VK_NV_GEOMETRY_SHADER_PASSTHROUGH_EXTENSION_NAME) \
  98. EXTENSION_NAME(VK_NV_VIEWPORT_ARRAY2_EXTENSION_NAME) \
  99. EXTENSION_NAME(VK_NV_VIEWPORT_SWIZZLE_EXTENSION_NAME)
  100. // Define features which must be supported.
  101. #define FOR_EACH_VK_MANDATORY_FEATURE(FEATURE_NAME) \
  102. FEATURE_NAME(bit16_storage, storageBuffer16BitAccess) \
  103. FEATURE_NAME(bit16_storage, uniformAndStorageBuffer16BitAccess) \
  104. FEATURE_NAME(bit8_storage, storageBuffer8BitAccess) \
  105. FEATURE_NAME(bit8_storage, uniformAndStorageBuffer8BitAccess) \
  106. FEATURE_NAME(features, depthBiasClamp) \
  107. FEATURE_NAME(features, depthClamp) \
  108. FEATURE_NAME(features, drawIndirectFirstInstance) \
  109. FEATURE_NAME(features, dualSrcBlend) \
  110. FEATURE_NAME(features, fillModeNonSolid) \
  111. FEATURE_NAME(features, fragmentStoresAndAtomics) \
  112. FEATURE_NAME(features, geometryShader) \
  113. FEATURE_NAME(features, imageCubeArray) \
  114. FEATURE_NAME(features, independentBlend) \
  115. FEATURE_NAME(features, largePoints) \
  116. FEATURE_NAME(features, logicOp) \
  117. FEATURE_NAME(features, multiDrawIndirect) \
  118. FEATURE_NAME(features, multiViewport) \
  119. FEATURE_NAME(features, occlusionQueryPrecise) \
  120. FEATURE_NAME(features, robustBufferAccess) \
  121. FEATURE_NAME(features, samplerAnisotropy) \
  122. FEATURE_NAME(features, sampleRateShading) \
  123. FEATURE_NAME(features, shaderClipDistance) \
  124. FEATURE_NAME(features, shaderCullDistance) \
  125. FEATURE_NAME(features, shaderImageGatherExtended) \
  126. FEATURE_NAME(features, shaderStorageImageWriteWithoutFormat) \
  127. FEATURE_NAME(features, tessellationShader) \
  128. FEATURE_NAME(features, vertexPipelineStoresAndAtomics) \
  129. FEATURE_NAME(features, wideLines) \
  130. FEATURE_NAME(host_query_reset, hostQueryReset) \
  131. FEATURE_NAME(shader_demote_to_helper_invocation, shaderDemoteToHelperInvocation) \
  132. FEATURE_NAME(shader_draw_parameters, shaderDrawParameters) \
  133. FEATURE_NAME(variable_pointer, variablePointers) \
  134. FEATURE_NAME(variable_pointer, variablePointersStorageBuffer)
  135. // Define features where the absence of the feature may result in a degraded experience.
  136. #define FOR_EACH_VK_RECOMMENDED_FEATURE(FEATURE_NAME) \
  137. FEATURE_NAME(custom_border_color, customBorderColors) \
  138. FEATURE_NAME(extended_dynamic_state, extendedDynamicState) \
  139. FEATURE_NAME(index_type_uint8, indexTypeUint8) \
  140. FEATURE_NAME(primitive_topology_list_restart, primitiveTopologyListRestart) \
  141. FEATURE_NAME(provoking_vertex, provokingVertexLast) \
  142. FEATURE_NAME(robustness2, nullDescriptor) \
  143. FEATURE_NAME(robustness2, robustBufferAccess2) \
  144. FEATURE_NAME(robustness2, robustImageAccess2) \
  145. FEATURE_NAME(shader_float16_int8, shaderFloat16) \
  146. FEATURE_NAME(shader_float16_int8, shaderInt8) \
  147. FEATURE_NAME(timeline_semaphore, timelineSemaphore) \
  148. FEATURE_NAME(transform_feedback, transformFeedback) \
  149. FEATURE_NAME(uniform_buffer_standard_layout, uniformBufferStandardLayout) \
  150. FEATURE_NAME(vertex_input_dynamic_state, vertexInputDynamicState)
  151. namespace Vulkan {
  152. class NsightAftermathTracker;
  153. /// Format usage descriptor.
  154. enum class FormatType { Linear, Optimal, Buffer };
  155. /// Subgroup size of the guest emulated hardware (Nvidia has 32 threads per subgroup).
  156. const u32 GuestWarpSize = 32;
  157. /// Handles data specific to a physical device.
  158. class Device {
  159. public:
  160. explicit Device(VkInstance instance, vk::PhysicalDevice physical, VkSurfaceKHR surface,
  161. const vk::InstanceDispatch& dld);
  162. ~Device();
  163. /**
  164. * Returns a format supported by the device for the passed requirements.
  165. * @param wanted_format The ideal format to be returned. It may not be the returned format.
  166. * @param wanted_usage The usage that must be fulfilled even if the format is not supported.
  167. * @param format_type Format type usage.
  168. * @returns A format supported by the device.
  169. */
  170. VkFormat GetSupportedFormat(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  171. FormatType format_type) const;
  172. /// Returns true if a format is supported.
  173. bool IsFormatSupported(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  174. FormatType format_type) const;
  175. /// Reports a device loss.
  176. void ReportLoss() const;
  177. /// Reports a shader to Nsight Aftermath.
  178. void SaveShader(std::span<const u32> spirv) const;
  179. /// Returns the name of the VkDriverId reported from Vulkan.
  180. std::string GetDriverName() const;
  181. /// Returns the dispatch loader with direct function pointers of the device.
  182. const vk::DeviceDispatch& GetDispatchLoader() const {
  183. return dld;
  184. }
  185. /// Returns the VMA allocator.
  186. VmaAllocator GetAllocator() const {
  187. return allocator;
  188. }
  189. /// Returns the logical device.
  190. const vk::Device& GetLogical() const {
  191. return logical;
  192. }
  193. /// Returns the physical device.
  194. vk::PhysicalDevice GetPhysical() const {
  195. return physical;
  196. }
  197. /// Returns the main graphics queue.
  198. vk::Queue GetGraphicsQueue() const {
  199. return graphics_queue;
  200. }
  201. /// Returns the main present queue.
  202. vk::Queue GetPresentQueue() const {
  203. return present_queue;
  204. }
  205. /// Returns main graphics queue family index.
  206. u32 GetGraphicsFamily() const {
  207. return graphics_family;
  208. }
  209. /// Returns main present queue family index.
  210. u32 GetPresentFamily() const {
  211. return present_family;
  212. }
  213. /// Returns the current Vulkan API version provided in Vulkan-formatted version numbers.
  214. u32 ApiVersion() const {
  215. return properties.properties.apiVersion;
  216. }
  217. /// Returns the current driver version provided in Vulkan-formatted version numbers.
  218. u32 GetDriverVersion() const {
  219. return properties.properties.driverVersion;
  220. }
  221. /// Returns the device name.
  222. std::string_view GetModelName() const {
  223. return properties.properties.deviceName;
  224. }
  225. /// Returns the driver ID.
  226. VkDriverIdKHR GetDriverID() const {
  227. return properties.driver.driverID;
  228. }
  229. bool ShouldBoostClocks() const;
  230. /// Returns uniform buffer alignment requirement.
  231. VkDeviceSize GetUniformBufferAlignment() const {
  232. return properties.properties.limits.minUniformBufferOffsetAlignment;
  233. }
  234. /// Returns storage alignment requirement.
  235. VkDeviceSize GetStorageBufferAlignment() const {
  236. return properties.properties.limits.minStorageBufferOffsetAlignment;
  237. }
  238. /// Returns the maximum range for storage buffers.
  239. VkDeviceSize GetMaxStorageBufferRange() const {
  240. return properties.properties.limits.maxStorageBufferRange;
  241. }
  242. /// Returns the maximum size for push constants.
  243. VkDeviceSize GetMaxPushConstantsSize() const {
  244. return properties.properties.limits.maxPushConstantsSize;
  245. }
  246. /// Returns the maximum size for shared memory.
  247. u32 GetMaxComputeSharedMemorySize() const {
  248. return properties.properties.limits.maxComputeSharedMemorySize;
  249. }
  250. /// Returns float control properties of the device.
  251. const VkPhysicalDeviceFloatControlsPropertiesKHR& FloatControlProperties() const {
  252. return properties.float_controls;
  253. }
  254. /// Returns true if ASTC is natively supported.
  255. bool IsOptimalAstcSupported() const {
  256. return features.features.textureCompressionASTC_LDR;
  257. }
  258. /// Returns true if descriptor aliasing is natively supported.
  259. bool IsDescriptorAliasingSupported() const {
  260. return GetDriverID() != VK_DRIVER_ID_QUALCOMM_PROPRIETARY;
  261. }
  262. /// Returns true if the device suppors float64 natively.
  263. bool IsFloat64Supported() const {
  264. return features.features.shaderFloat64;
  265. }
  266. /// Returns true if the device supports float16 natively.
  267. bool IsFloat16Supported() const {
  268. return features.shader_float16_int8.shaderFloat16;
  269. }
  270. /// Returns true if the device supports int8 natively.
  271. bool IsInt8Supported() const {
  272. return features.shader_float16_int8.shaderInt8;
  273. }
  274. /// Returns true if the device warp size can potentially be bigger than guest's warp size.
  275. bool IsWarpSizePotentiallyBiggerThanGuest() const {
  276. return is_warp_potentially_bigger;
  277. }
  278. /// Returns true if the device can be forced to use the guest warp size.
  279. bool IsGuestWarpSizeSupported(VkShaderStageFlagBits stage) const {
  280. return properties.subgroup_size_control.requiredSubgroupSizeStages & stage;
  281. }
  282. /// Returns the maximum number of push descriptors.
  283. u32 MaxPushDescriptors() const {
  284. return properties.push_descriptor.maxPushDescriptors;
  285. }
  286. /// Returns true if formatless image load is supported.
  287. bool IsFormatlessImageLoadSupported() const {
  288. return features.features.shaderStorageImageReadWithoutFormat;
  289. }
  290. /// Returns true if shader int64 is supported.
  291. bool IsShaderInt64Supported() const {
  292. return features.features.shaderInt64;
  293. }
  294. /// Returns true if shader int16 is supported.
  295. bool IsShaderInt16Supported() const {
  296. return features.features.shaderInt16;
  297. }
  298. // Returns true if depth bounds is supported.
  299. bool IsDepthBoundsSupported() const {
  300. return features.features.depthBounds;
  301. }
  302. /// Returns true when blitting from and to depth stencil images is supported.
  303. bool IsBlitDepthStencilSupported() const {
  304. return is_blit_depth_stencil_supported;
  305. }
  306. /// Returns true if the device supports VK_NV_viewport_swizzle.
  307. bool IsNvViewportSwizzleSupported() const {
  308. return extensions.viewport_swizzle;
  309. }
  310. /// Returns true if the device supports VK_NV_viewport_array2.
  311. bool IsNvViewportArray2Supported() const {
  312. return extensions.viewport_array2;
  313. }
  314. /// Returns true if the device supports VK_NV_geometry_shader_passthrough.
  315. bool IsNvGeometryShaderPassthroughSupported() const {
  316. return extensions.geometry_shader_passthrough;
  317. }
  318. /// Returns true if the device supports VK_KHR_uniform_buffer_standard_layout.
  319. bool IsKhrUniformBufferStandardLayoutSupported() const {
  320. return extensions.uniform_buffer_standard_layout;
  321. }
  322. /// Returns true if the device supports VK_KHR_push_descriptor.
  323. bool IsKhrPushDescriptorSupported() const {
  324. return extensions.push_descriptor;
  325. }
  326. /// Returns true if VK_KHR_pipeline_executable_properties is enabled.
  327. bool IsKhrPipelineExecutablePropertiesEnabled() const {
  328. return extensions.pipeline_executable_properties;
  329. }
  330. /// Returns true if VK_KHR_swapchain_mutable_format is enabled.
  331. bool IsKhrSwapchainMutableFormatEnabled() const {
  332. return extensions.swapchain_mutable_format;
  333. }
  334. /// Returns true if the device supports VK_KHR_workgroup_memory_explicit_layout.
  335. bool IsKhrWorkgroupMemoryExplicitLayoutSupported() const {
  336. return extensions.workgroup_memory_explicit_layout;
  337. }
  338. /// Returns true if the device supports VK_EXT_primitive_topology_list_restart.
  339. bool IsTopologyListPrimitiveRestartSupported() const {
  340. return features.primitive_topology_list_restart.primitiveTopologyListRestart;
  341. }
  342. /// Returns true if the device supports VK_EXT_primitive_topology_list_restart.
  343. bool IsPatchListPrimitiveRestartSupported() const {
  344. return features.primitive_topology_list_restart.primitiveTopologyPatchListRestart;
  345. }
  346. /// Returns true if the device supports VK_EXT_index_type_uint8.
  347. bool IsExtIndexTypeUint8Supported() const {
  348. return extensions.index_type_uint8;
  349. }
  350. /// Returns true if the device supports VK_EXT_sampler_filter_minmax.
  351. bool IsExtSamplerFilterMinmaxSupported() const {
  352. return extensions.sampler_filter_minmax;
  353. }
  354. /// Returns true if the device supports VK_EXT_depth_range_unrestricted.
  355. bool IsExtDepthRangeUnrestrictedSupported() const {
  356. return extensions.depth_range_unrestricted;
  357. }
  358. /// Returns true if the device supports VK_EXT_depth_clip_control.
  359. bool IsExtDepthClipControlSupported() const {
  360. return extensions.depth_clip_control;
  361. }
  362. /// Returns true if the device supports VK_EXT_shader_viewport_index_layer.
  363. bool IsExtShaderViewportIndexLayerSupported() const {
  364. return extensions.shader_viewport_index_layer;
  365. }
  366. /// Returns true if the device supports VK_EXT_subgroup_size_control.
  367. bool IsExtSubgroupSizeControlSupported() const {
  368. return extensions.subgroup_size_control;
  369. }
  370. /// Returns true if the device supports VK_EXT_transform_feedback.
  371. bool IsExtTransformFeedbackSupported() const {
  372. return extensions.transform_feedback;
  373. }
  374. /// Returns true if the device supports VK_EXT_custom_border_color.
  375. bool IsExtCustomBorderColorSupported() const {
  376. return extensions.custom_border_color;
  377. }
  378. /// Returns true if the device supports VK_EXT_extended_dynamic_state.
  379. bool IsExtExtendedDynamicStateSupported() const {
  380. return extensions.extended_dynamic_state;
  381. }
  382. /// Returns true if the device supports VK_EXT_extended_dynamic_state2.
  383. bool IsExtExtendedDynamicState2Supported() const {
  384. return extensions.extended_dynamic_state2;
  385. }
  386. bool IsExtExtendedDynamicState2ExtrasSupported() const {
  387. return features.extended_dynamic_state2.extendedDynamicState2LogicOp;
  388. }
  389. /// Returns true if the device supports VK_EXT_extended_dynamic_state3.
  390. bool IsExtExtendedDynamicState3Supported() const {
  391. return extensions.extended_dynamic_state3;
  392. }
  393. /// Returns true if the device supports VK_EXT_extended_dynamic_state3.
  394. bool IsExtExtendedDynamicState3BlendingSupported() const {
  395. return dynamic_state3_blending;
  396. }
  397. /// Returns true if the device supports VK_EXT_extended_dynamic_state3.
  398. bool IsExtExtendedDynamicState3EnablesSupported() const {
  399. return dynamic_state3_enables;
  400. }
  401. /// Returns true if the device supports VK_EXT_line_rasterization.
  402. bool IsExtLineRasterizationSupported() const {
  403. return extensions.line_rasterization;
  404. }
  405. /// Returns true if the device supports VK_EXT_vertex_input_dynamic_state.
  406. bool IsExtVertexInputDynamicStateSupported() const {
  407. return extensions.vertex_input_dynamic_state;
  408. }
  409. /// Returns true if the device supports VK_EXT_shader_stencil_export.
  410. bool IsExtShaderStencilExportSupported() const {
  411. return extensions.shader_stencil_export;
  412. }
  413. /// Returns true if the device supports VK_EXT_conservative_rasterization.
  414. bool IsExtConservativeRasterizationSupported() const {
  415. return extensions.conservative_rasterization;
  416. }
  417. /// Returns true if the device supports VK_EXT_provoking_vertex.
  418. bool IsExtProvokingVertexSupported() const {
  419. return extensions.provoking_vertex;
  420. }
  421. /// Returns true if the device supports VK_KHR_shader_atomic_int64.
  422. bool IsExtShaderAtomicInt64Supported() const {
  423. return extensions.shader_atomic_int64;
  424. }
  425. bool HasTimelineSemaphore() const {
  426. if (GetDriverID() == VK_DRIVER_ID_QUALCOMM_PROPRIETARY) {
  427. // Timeline semaphores do not work properly on all Qualcomm drivers.
  428. return false;
  429. }
  430. return features.timeline_semaphore.timelineSemaphore;
  431. }
  432. /// Returns the minimum supported version of SPIR-V.
  433. u32 SupportedSpirvVersion() const {
  434. if (instance_version >= VK_API_VERSION_1_3) {
  435. return 0x00010600U;
  436. }
  437. if (extensions.spirv_1_4) {
  438. return 0x00010400U;
  439. }
  440. return 0x00010000U;
  441. }
  442. /// Returns true when a known debugging tool is attached.
  443. bool HasDebuggingToolAttached() const {
  444. return has_renderdoc || has_nsight_graphics || Settings::values.renderer_debug.GetValue();
  445. }
  446. /// Returns true when the device does not properly support cube compatibility.
  447. bool HasBrokenCubeImageCompability() const {
  448. return has_broken_cube_compatibility;
  449. }
  450. /// Returns the vendor name reported from Vulkan.
  451. std::string_view GetVendorName() const {
  452. return properties.driver.driverName;
  453. }
  454. /// Returns the list of available extensions.
  455. const std::set<std::string, std::less<>>& GetAvailableExtensions() const {
  456. return supported_extensions;
  457. }
  458. u64 GetDeviceLocalMemory() const {
  459. return device_access_memory;
  460. }
  461. bool CanReportMemoryUsage() const {
  462. return extensions.memory_budget;
  463. }
  464. u64 GetDeviceMemoryUsage() const;
  465. u32 GetSetsPerPool() const {
  466. return sets_per_pool;
  467. }
  468. bool SupportsD24DepthBuffer() const {
  469. return supports_d24_depth;
  470. }
  471. bool CantBlitMSAA() const {
  472. return cant_blit_msaa;
  473. }
  474. bool MustEmulateScaledFormats() const {
  475. return must_emulate_scaled_formats;
  476. }
  477. bool MustEmulateBGR565() const {
  478. return must_emulate_bgr565;
  479. }
  480. bool HasNullDescriptor() const {
  481. return features.robustness2.nullDescriptor;
  482. }
  483. u32 GetMaxVertexInputAttributes() const {
  484. return properties.properties.limits.maxVertexInputAttributes;
  485. }
  486. u32 GetMaxVertexInputBindings() const {
  487. return properties.properties.limits.maxVertexInputBindings;
  488. }
  489. bool SupportsConditionalBarriers() const {
  490. return supports_conditional_barriers;
  491. }
  492. private:
  493. /// Checks if the physical device is suitable and configures the object state
  494. /// with all necessary info about its properties.
  495. bool GetSuitability(bool requires_swapchain);
  496. // Remove extensions which have incomplete feature support.
  497. void RemoveUnsuitableExtensions();
  498. void RemoveExtensionIfUnsuitable(bool is_suitable, const std::string& extension_name);
  499. /// Sets up queue families.
  500. void SetupFamilies(VkSurfaceKHR surface);
  501. /// Collects information about attached tools.
  502. void CollectToolingInfo();
  503. /// Collects information about the device's local memory.
  504. void CollectPhysicalMemoryInfo();
  505. /// Returns a list of queue initialization descriptors.
  506. std::vector<VkDeviceQueueCreateInfo> GetDeviceQueueCreateInfos() const;
  507. /// Returns true if ASTC textures are natively supported.
  508. bool ComputeIsOptimalAstcSupported() const;
  509. /// Returns true if the device natively supports blitting depth stencil images.
  510. bool TestDepthStencilBlits() const;
  511. private:
  512. VkInstance instance; ///< Vulkan instance.
  513. VmaAllocator allocator; ///< VMA allocator.
  514. vk::DeviceDispatch dld; ///< Device function pointers.
  515. vk::PhysicalDevice physical; ///< Physical device.
  516. vk::Device logical; ///< Logical device.
  517. vk::Queue graphics_queue; ///< Main graphics queue.
  518. vk::Queue present_queue; ///< Main present queue.
  519. u32 instance_version{}; ///< Vulkan instance version.
  520. u32 graphics_family{}; ///< Main graphics queue family index.
  521. u32 present_family{}; ///< Main present queue family index.
  522. struct Extensions {
  523. #define EXTENSION(prefix, macro_name, var_name) bool var_name{};
  524. #define FEATURE(prefix, struct_name, macro_name, var_name) bool var_name{};
  525. FOR_EACH_VK_FEATURE_1_1(FEATURE);
  526. FOR_EACH_VK_FEATURE_1_2(FEATURE);
  527. FOR_EACH_VK_FEATURE_1_3(FEATURE);
  528. FOR_EACH_VK_FEATURE_EXT(FEATURE);
  529. FOR_EACH_VK_EXTENSION(EXTENSION);
  530. FOR_EACH_VK_EXTENSION_WIN32(EXTENSION);
  531. #undef EXTENSION
  532. #undef FEATURE
  533. };
  534. struct Features {
  535. #define FEATURE_CORE(prefix, struct_name, macro_name, var_name) \
  536. VkPhysicalDevice##struct_name##Features var_name{};
  537. #define FEATURE_EXT(prefix, struct_name, macro_name, var_name) \
  538. VkPhysicalDevice##struct_name##Features##prefix var_name{};
  539. FOR_EACH_VK_FEATURE_1_1(FEATURE_CORE);
  540. FOR_EACH_VK_FEATURE_1_2(FEATURE_CORE);
  541. FOR_EACH_VK_FEATURE_1_3(FEATURE_CORE);
  542. FOR_EACH_VK_FEATURE_EXT(FEATURE_EXT);
  543. #undef FEATURE_CORE
  544. #undef FEATURE_EXT
  545. VkPhysicalDeviceFeatures features{};
  546. };
  547. struct Properties {
  548. VkPhysicalDeviceDriverProperties driver{};
  549. VkPhysicalDeviceFloatControlsProperties float_controls{};
  550. VkPhysicalDevicePushDescriptorPropertiesKHR push_descriptor{};
  551. VkPhysicalDeviceSubgroupSizeControlProperties subgroup_size_control{};
  552. VkPhysicalDeviceTransformFeedbackPropertiesEXT transform_feedback{};
  553. VkPhysicalDeviceProperties properties{};
  554. };
  555. Extensions extensions{};
  556. Features features{};
  557. Properties properties{};
  558. VkPhysicalDeviceFeatures2 features2{};
  559. VkPhysicalDeviceProperties2 properties2{};
  560. // Misc features
  561. bool is_optimal_astc_supported{}; ///< Support for all guest ASTC formats.
  562. bool is_blit_depth_stencil_supported{}; ///< Support for blitting from and to depth stencil.
  563. bool is_warp_potentially_bigger{}; ///< Host warp size can be bigger than guest.
  564. bool is_integrated{}; ///< Is GPU an iGPU.
  565. bool is_virtual{}; ///< Is GPU a virtual GPU.
  566. bool is_non_gpu{}; ///< Is SoftwareRasterizer, FPGA, non-GPU device.
  567. bool has_broken_cube_compatibility{}; ///< Has broken cube compatibility bit
  568. bool has_renderdoc{}; ///< Has RenderDoc attached
  569. bool has_nsight_graphics{}; ///< Has Nsight Graphics attached
  570. bool supports_d24_depth{}; ///< Supports D24 depth buffers.
  571. bool cant_blit_msaa{}; ///< Does not support MSAA<->MSAA blitting.
  572. bool must_emulate_scaled_formats{}; ///< Requires scaled vertex format emulation
  573. bool must_emulate_bgr565{}; ///< Emulates BGR565 by swizzling RGB565 format.
  574. bool dynamic_state3_blending{}; ///< Has all blending features of dynamic_state3.
  575. bool dynamic_state3_enables{}; ///< Has all enables features of dynamic_state3.
  576. bool supports_conditional_barriers{}; ///< Allows barriers in conditional control flow.
  577. u64 device_access_memory{}; ///< Total size of device local memory in bytes.
  578. u32 sets_per_pool{}; ///< Sets per Description Pool
  579. // Telemetry parameters
  580. std::set<std::string, std::less<>> supported_extensions; ///< Reported Vulkan extensions.
  581. std::set<std::string, std::less<>> loaded_extensions; ///< Loaded Vulkan extensions.
  582. std::vector<size_t> valid_heap_memory; ///< Heaps used.
  583. /// Format properties dictionary.
  584. std::unordered_map<VkFormat, VkFormatProperties> format_properties;
  585. /// Nsight Aftermath GPU crash tracker
  586. std::unique_ptr<NsightAftermathTracker> nsight_aftermath_tracker;
  587. };
  588. } // namespace Vulkan