vulkan_device.h 33 KB

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