vulkan_device.h 31 KB

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