vulkan_device.h 34 KB

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