vulkan_device.h 38 KB

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