vulkan_device.h 37 KB

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