vulkan_device.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <span>
  5. #include <string>
  6. #include <unordered_map>
  7. #include <vector>
  8. #include "common/common_types.h"
  9. #include "video_core/vulkan_common/vulkan_wrapper.h"
  10. namespace Vulkan {
  11. class NsightAftermathTracker;
  12. /// Format usage descriptor.
  13. enum class FormatType { Linear, Optimal, Buffer };
  14. /// Subgroup size of the guest emulated hardware (Nvidia has 32 threads per subgroup).
  15. const u32 GuestWarpSize = 32;
  16. /// Handles data specific to a physical device.
  17. class Device {
  18. public:
  19. explicit Device(VkInstance instance, vk::PhysicalDevice physical, VkSurfaceKHR surface,
  20. const vk::InstanceDispatch& dld);
  21. ~Device();
  22. /**
  23. * Returns a format supported by the device for the passed requeriments.
  24. * @param wanted_format The ideal format to be returned. It may not be the returned format.
  25. * @param wanted_usage The usage that must be fulfilled even if the format is not supported.
  26. * @param format_type Format type usage.
  27. * @returns A format supported by the device.
  28. */
  29. VkFormat GetSupportedFormat(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  30. FormatType format_type) const;
  31. /// Returns true if a format is supported.
  32. bool IsFormatSupported(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  33. FormatType format_type) const;
  34. /// Reports a device loss.
  35. void ReportLoss() const;
  36. /// Reports a shader to Nsight Aftermath.
  37. void SaveShader(std::span<const u32> spirv) const;
  38. /// Returns the name of the VkDriverId reported from Vulkan.
  39. std::string GetDriverName() const;
  40. /// Returns the dispatch loader with direct function pointers of the device.
  41. const vk::DeviceDispatch& GetDispatchLoader() const {
  42. return dld;
  43. }
  44. /// Returns the logical device.
  45. const vk::Device& GetLogical() const {
  46. return logical;
  47. }
  48. /// Returns the physical device.
  49. vk::PhysicalDevice GetPhysical() const {
  50. return physical;
  51. }
  52. /// Returns the main graphics queue.
  53. vk::Queue GetGraphicsQueue() const {
  54. return graphics_queue;
  55. }
  56. /// Returns the main present queue.
  57. vk::Queue GetPresentQueue() const {
  58. return present_queue;
  59. }
  60. /// Returns main graphics queue family index.
  61. u32 GetGraphicsFamily() const {
  62. return graphics_family;
  63. }
  64. /// Returns main present queue family index.
  65. u32 GetPresentFamily() const {
  66. return present_family;
  67. }
  68. /// Returns the current Vulkan API version provided in Vulkan-formatted version numbers.
  69. u32 ApiVersion() const {
  70. return properties.apiVersion;
  71. }
  72. /// Returns the current driver version provided in Vulkan-formatted version numbers.
  73. u32 GetDriverVersion() const {
  74. return properties.driverVersion;
  75. }
  76. /// Returns the device name.
  77. std::string_view GetModelName() const {
  78. return properties.deviceName;
  79. }
  80. /// Returns the driver ID.
  81. VkDriverIdKHR GetDriverID() const {
  82. return driver_id;
  83. }
  84. bool ShouldBoostClocks() const;
  85. /// Returns uniform buffer alignment requeriment.
  86. VkDeviceSize GetUniformBufferAlignment() const {
  87. return properties.limits.minUniformBufferOffsetAlignment;
  88. }
  89. /// Returns storage alignment requeriment.
  90. VkDeviceSize GetStorageBufferAlignment() const {
  91. return properties.limits.minStorageBufferOffsetAlignment;
  92. }
  93. /// Returns the maximum range for storage buffers.
  94. VkDeviceSize GetMaxStorageBufferRange() const {
  95. return properties.limits.maxStorageBufferRange;
  96. }
  97. /// Returns the maximum size for push constants.
  98. VkDeviceSize GetMaxPushConstantsSize() const {
  99. return properties.limits.maxPushConstantsSize;
  100. }
  101. /// Returns the maximum size for shared memory.
  102. u32 GetMaxComputeSharedMemorySize() const {
  103. return properties.limits.maxComputeSharedMemorySize;
  104. }
  105. /// Returns float control properties of the device.
  106. const VkPhysicalDeviceFloatControlsPropertiesKHR& FloatControlProperties() const {
  107. return float_controls;
  108. }
  109. /// Returns true if ASTC is natively supported.
  110. bool IsOptimalAstcSupported() const {
  111. return is_optimal_astc_supported;
  112. }
  113. /// Returns true if the device supports float16 natively.
  114. bool IsFloat16Supported() const {
  115. return is_float16_supported;
  116. }
  117. /// Returns true if the device supports int8 natively.
  118. bool IsInt8Supported() const {
  119. return is_int8_supported;
  120. }
  121. /// Returns true if the device warp size can potentially be bigger than guest's warp size.
  122. bool IsWarpSizePotentiallyBiggerThanGuest() const {
  123. return is_warp_potentially_bigger;
  124. }
  125. /// Returns true if the device can be forced to use the guest warp size.
  126. bool IsGuestWarpSizeSupported(VkShaderStageFlagBits stage) const {
  127. return guest_warp_stages & stage;
  128. }
  129. /// Returns the maximum number of push descriptors.
  130. u32 MaxPushDescriptors() const {
  131. return max_push_descriptors;
  132. }
  133. /// Returns true if formatless image load is supported.
  134. bool IsFormatlessImageLoadSupported() const {
  135. return is_formatless_image_load_supported;
  136. }
  137. /// Returns true if shader int64 is supported.
  138. bool IsShaderInt64Supported() const {
  139. return is_shader_int64_supported;
  140. }
  141. /// Returns true if shader int16 is supported.
  142. bool IsShaderInt16Supported() const {
  143. return is_shader_int16_supported;
  144. }
  145. // Returns true if depth bounds is supported.
  146. bool IsDepthBoundsSupported() const {
  147. return is_depth_bounds_supported;
  148. }
  149. /// Returns true when blitting from and to depth stencil images is supported.
  150. bool IsBlitDepthStencilSupported() const {
  151. return is_blit_depth_stencil_supported;
  152. }
  153. /// Returns true if the device supports VK_NV_viewport_swizzle.
  154. bool IsNvViewportSwizzleSupported() const {
  155. return nv_viewport_swizzle;
  156. }
  157. /// Returns true if the device supports VK_NV_viewport_array2.
  158. bool IsNvViewportArray2Supported() const {
  159. return nv_viewport_array2;
  160. }
  161. /// Returns true if the device supports VK_NV_geometry_shader_passthrough.
  162. bool IsNvGeometryShaderPassthroughSupported() const {
  163. return nv_geometry_shader_passthrough;
  164. }
  165. /// Returns true if the device supports VK_KHR_uniform_buffer_standard_layout.
  166. bool IsKhrUniformBufferStandardLayoutSupported() const {
  167. return khr_uniform_buffer_standard_layout;
  168. }
  169. /// Returns true if the device supports VK_KHR_push_descriptor.
  170. bool IsKhrPushDescriptorSupported() const {
  171. return khr_push_descriptor;
  172. }
  173. /// Returns true if VK_KHR_pipeline_executable_properties is enabled.
  174. bool IsKhrPipelineExecutablePropertiesEnabled() const {
  175. return khr_pipeline_executable_properties;
  176. }
  177. /// Returns true if VK_KHR_swapchain_mutable_format is enabled.
  178. bool IsKhrSwapchainMutableFormatEnabled() const {
  179. return khr_swapchain_mutable_format;
  180. }
  181. /// Returns true if the device supports VK_KHR_workgroup_memory_explicit_layout.
  182. bool IsKhrWorkgroupMemoryExplicitLayoutSupported() const {
  183. return khr_workgroup_memory_explicit_layout;
  184. }
  185. /// Returns true if the device supports VK_EXT_primitive_topology_list_restart.
  186. bool IsTopologyListPrimitiveRestartSupported() const {
  187. return is_topology_list_restart_supported;
  188. }
  189. /// Returns true if the device supports VK_EXT_primitive_topology_list_restart.
  190. bool IsPatchListPrimitiveRestartSupported() const {
  191. return is_patch_list_restart_supported;
  192. }
  193. /// Returns true if the device supports VK_EXT_index_type_uint8.
  194. bool IsExtIndexTypeUint8Supported() const {
  195. return ext_index_type_uint8;
  196. }
  197. /// Returns true if the device supports VK_EXT_sampler_filter_minmax.
  198. bool IsExtSamplerFilterMinmaxSupported() const {
  199. return ext_sampler_filter_minmax;
  200. }
  201. /// Returns true if the device supports VK_EXT_depth_range_unrestricted.
  202. bool IsExtDepthRangeUnrestrictedSupported() const {
  203. return ext_depth_range_unrestricted;
  204. }
  205. /// Returns true if the device supports VK_EXT_depth_clip_control.
  206. bool IsExtDepthClipControlSupported() const {
  207. return ext_depth_clip_control;
  208. }
  209. /// Returns true if the device supports VK_EXT_shader_viewport_index_layer.
  210. bool IsExtShaderViewportIndexLayerSupported() const {
  211. return ext_shader_viewport_index_layer;
  212. }
  213. /// Returns true if the device supports VK_EXT_subgroup_size_control.
  214. bool IsExtSubgroupSizeControlSupported() const {
  215. return ext_subgroup_size_control;
  216. }
  217. /// Returns true if the device supports VK_EXT_transform_feedback.
  218. bool IsExtTransformFeedbackSupported() const {
  219. return ext_transform_feedback;
  220. }
  221. /// Returns true if the device supports VK_EXT_custom_border_color.
  222. bool IsExtCustomBorderColorSupported() const {
  223. return ext_custom_border_color;
  224. }
  225. /// Returns true if the device supports VK_EXT_extended_dynamic_state.
  226. bool IsExtExtendedDynamicStateSupported() const {
  227. return ext_extended_dynamic_state;
  228. }
  229. /// Returns true if the device supports VK_EXT_extended_dynamic_state2.
  230. bool IsExtExtendedDynamicState2Supported() const {
  231. return ext_extended_dynamic_state_2;
  232. }
  233. bool IsExtExtendedDynamicState2ExtrasSupported() const {
  234. return ext_extended_dynamic_state_2_extra;
  235. }
  236. /// Returns true if the device supports VK_EXT_extended_dynamic_state3.
  237. bool IsExtExtendedDynamicState3Supported() const {
  238. return ext_extended_dynamic_state_3;
  239. }
  240. /// Returns true if the device supports VK_EXT_extended_dynamic_state3.
  241. bool IsExtExtendedDynamicState3BlendingSupported() const {
  242. return ext_extended_dynamic_state_3_blend;
  243. }
  244. /// Returns true if the device supports VK_EXT_extended_dynamic_state3.
  245. bool IsExtExtendedDynamicState3EnablesSupported() const {
  246. return ext_extended_dynamic_state_3_enables;
  247. }
  248. /// Returns true if the device supports VK_EXT_line_rasterization.
  249. bool IsExtLineRasterizationSupported() const {
  250. return ext_line_rasterization;
  251. }
  252. /// Returns true if the device supports VK_EXT_vertex_input_dynamic_state.
  253. bool IsExtVertexInputDynamicStateSupported() const {
  254. return ext_vertex_input_dynamic_state;
  255. }
  256. /// Returns true if the device supports VK_EXT_shader_stencil_export.
  257. bool IsExtShaderStencilExportSupported() const {
  258. return ext_shader_stencil_export;
  259. }
  260. /// Returns true if the device supports VK_EXT_conservative_rasterization.
  261. bool IsExtConservativeRasterizationSupported() const {
  262. return ext_conservative_rasterization;
  263. }
  264. /// Returns true if the device supports VK_EXT_provoking_vertex.
  265. bool IsExtProvokingVertexSupported() const {
  266. return ext_provoking_vertex;
  267. }
  268. /// Returns true if the device supports VK_KHR_shader_atomic_int64.
  269. bool IsExtShaderAtomicInt64Supported() const {
  270. return ext_shader_atomic_int64;
  271. }
  272. /// Returns the minimum supported version of SPIR-V.
  273. u32 SupportedSpirvVersion() const {
  274. if (instance_version >= VK_API_VERSION_1_3) {
  275. return 0x00010600U;
  276. }
  277. if (khr_spirv_1_4) {
  278. return 0x00010400U;
  279. }
  280. return 0x00010000U;
  281. }
  282. /// Returns true when a known debugging tool is attached.
  283. bool HasDebuggingToolAttached() const {
  284. return has_renderdoc || has_nsight_graphics;
  285. }
  286. /// Returns true when the device does not properly support cube compatibility.
  287. bool HasBrokenCubeImageCompability() const {
  288. return has_broken_cube_compatibility;
  289. }
  290. /// Returns the vendor name reported from Vulkan.
  291. std::string_view GetVendorName() const {
  292. return vendor_name;
  293. }
  294. /// Returns the list of available extensions.
  295. const std::vector<std::string>& GetAvailableExtensions() const {
  296. return supported_extensions;
  297. }
  298. u64 GetDeviceLocalMemory() const {
  299. return device_access_memory;
  300. }
  301. bool CanReportMemoryUsage() const {
  302. return ext_memory_budget;
  303. }
  304. u64 GetDeviceMemoryUsage() const;
  305. u32 GetSetsPerPool() const {
  306. return sets_per_pool;
  307. }
  308. bool SupportsD24DepthBuffer() const {
  309. return supports_d24_depth;
  310. }
  311. bool CantBlitMSAA() const {
  312. return cant_blit_msaa;
  313. }
  314. bool MustEmulateBGR565() const {
  315. return must_emulate_bgr565;
  316. }
  317. bool HasNullDescriptor() const {
  318. return has_null_descriptor;
  319. }
  320. u32 GetMaxVertexInputAttributes() const {
  321. return max_vertex_input_attributes;
  322. }
  323. u32 GetMaxVertexInputBindings() const {
  324. return max_vertex_input_bindings;
  325. }
  326. private:
  327. /// Checks if the physical device is suitable.
  328. void CheckSuitability(bool requires_swapchain) const;
  329. /// Loads extensions into a vector and stores available ones in this object.
  330. std::vector<const char*> LoadExtensions(bool requires_surface);
  331. /// Sets up queue families.
  332. void SetupFamilies(VkSurfaceKHR surface);
  333. /// Sets up device features.
  334. void SetupFeatures();
  335. /// Sets up device properties.
  336. void SetupProperties();
  337. /// Collects telemetry information from the device.
  338. void CollectTelemetryParameters();
  339. /// Collects information about attached tools.
  340. void CollectToolingInfo();
  341. /// Collects information about the device's local memory.
  342. void CollectPhysicalMemoryInfo();
  343. /// Returns a list of queue initialization descriptors.
  344. std::vector<VkDeviceQueueCreateInfo> GetDeviceQueueCreateInfos() const;
  345. /// Returns true if ASTC textures are natively supported.
  346. bool IsOptimalAstcSupported(const VkPhysicalDeviceFeatures& features) const;
  347. /// Returns true if the device natively supports blitting depth stencil images.
  348. bool TestDepthStencilBlits() const;
  349. VkInstance instance; ///< Vulkan instance.
  350. vk::DeviceDispatch dld; ///< Device function pointers.
  351. vk::PhysicalDevice physical; ///< Physical device.
  352. VkPhysicalDeviceProperties properties; ///< Device properties.
  353. VkPhysicalDeviceFloatControlsPropertiesKHR float_controls{}; ///< Float control properties.
  354. vk::Device logical; ///< Logical device.
  355. vk::Queue graphics_queue; ///< Main graphics queue.
  356. vk::Queue present_queue; ///< Main present queue.
  357. u32 instance_version{}; ///< Vulkan onstance version.
  358. u32 graphics_family{}; ///< Main graphics queue family index.
  359. u32 present_family{}; ///< Main present queue family index.
  360. VkDriverIdKHR driver_id{}; ///< Driver ID.
  361. VkShaderStageFlags guest_warp_stages{}; ///< Stages where the guest warp size can be forced.
  362. u64 device_access_memory{}; ///< Total size of device local memory in bytes.
  363. u32 max_push_descriptors{}; ///< Maximum number of push descriptors
  364. u32 sets_per_pool{}; ///< Sets per Description Pool
  365. bool is_optimal_astc_supported{}; ///< Support for native ASTC.
  366. bool is_float16_supported{}; ///< Support for float16 arithmetic.
  367. bool is_int8_supported{}; ///< Support for int8 arithmetic.
  368. bool is_warp_potentially_bigger{}; ///< Host warp size can be bigger than guest.
  369. bool is_formatless_image_load_supported{}; ///< Support for shader image read without format.
  370. bool is_depth_bounds_supported{}; ///< Support for depth bounds.
  371. bool is_shader_float64_supported{}; ///< Support for float64.
  372. bool is_shader_int64_supported{}; ///< Support for int64.
  373. bool is_shader_int16_supported{}; ///< Support for int16.
  374. bool is_shader_storage_image_multisample{}; ///< Support for image operations on MSAA images.
  375. bool is_blit_depth_stencil_supported{}; ///< Support for blitting from and to depth stencil.
  376. bool is_topology_list_restart_supported{}; ///< Support for primitive restart with list
  377. ///< topologies.
  378. bool is_patch_list_restart_supported{}; ///< Support for primitive restart with list patch.
  379. bool is_integrated{}; ///< Is GPU an iGPU.
  380. bool is_virtual{}; ///< Is GPU a virtual GPU.
  381. bool is_non_gpu{}; ///< Is SoftwareRasterizer, FPGA, non-GPU device.
  382. bool nv_viewport_swizzle{}; ///< Support for VK_NV_viewport_swizzle.
  383. bool nv_viewport_array2{}; ///< Support for VK_NV_viewport_array2.
  384. bool nv_geometry_shader_passthrough{}; ///< Support for VK_NV_geometry_shader_passthrough.
  385. bool khr_draw_indirect_count{}; ///< Support for VK_KHR_draw_indirect_count.
  386. bool khr_uniform_buffer_standard_layout{}; ///< Support for scalar uniform buffer layouts.
  387. bool khr_spirv_1_4{}; ///< Support for VK_KHR_spirv_1_4.
  388. bool khr_workgroup_memory_explicit_layout{}; ///< Support for explicit workgroup layouts.
  389. bool khr_push_descriptor{}; ///< Support for VK_KHR_push_descritor.
  390. bool khr_pipeline_executable_properties{}; ///< Support for executable properties.
  391. bool khr_swapchain_mutable_format{}; ///< Support for VK_KHR_swapchain_mutable_format.
  392. bool ext_index_type_uint8{}; ///< Support for VK_EXT_index_type_uint8.
  393. bool ext_sampler_filter_minmax{}; ///< Support for VK_EXT_sampler_filter_minmax.
  394. bool ext_depth_clip_control{}; ///< Support for VK_EXT_depth_clip_control
  395. bool ext_depth_range_unrestricted{}; ///< Support for VK_EXT_depth_range_unrestricted.
  396. bool ext_shader_viewport_index_layer{}; ///< Support for VK_EXT_shader_viewport_index_layer.
  397. bool ext_tooling_info{}; ///< Support for VK_EXT_tooling_info.
  398. bool ext_subgroup_size_control{}; ///< Support for VK_EXT_subgroup_size_control.
  399. bool ext_transform_feedback{}; ///< Support for VK_EXT_transform_feedback.
  400. bool ext_custom_border_color{}; ///< Support for VK_EXT_custom_border_color.
  401. bool ext_extended_dynamic_state{}; ///< Support for VK_EXT_extended_dynamic_state.
  402. bool ext_extended_dynamic_state_2{}; ///< Support for VK_EXT_extended_dynamic_state2.
  403. bool ext_extended_dynamic_state_2_extra{}; ///< Support for VK_EXT_extended_dynamic_state2.
  404. bool ext_extended_dynamic_state_3{}; ///< Support for VK_EXT_extended_dynamic_state3.
  405. bool ext_extended_dynamic_state_3_blend{}; ///< Support for VK_EXT_extended_dynamic_state3.
  406. bool ext_extended_dynamic_state_3_enables{}; ///< Support for VK_EXT_extended_dynamic_state3.
  407. bool ext_line_rasterization{}; ///< Support for VK_EXT_line_rasterization.
  408. bool ext_vertex_input_dynamic_state{}; ///< Support for VK_EXT_vertex_input_dynamic_state.
  409. bool ext_shader_stencil_export{}; ///< Support for VK_EXT_shader_stencil_export.
  410. bool ext_shader_atomic_int64{}; ///< Support for VK_KHR_shader_atomic_int64.
  411. bool ext_conservative_rasterization{}; ///< Support for VK_EXT_conservative_rasterization.
  412. bool ext_provoking_vertex{}; ///< Support for VK_EXT_provoking_vertex.
  413. bool ext_memory_budget{}; ///< Support for VK_EXT_memory_budget.
  414. bool nv_device_diagnostics_config{}; ///< Support for VK_NV_device_diagnostics_config.
  415. bool has_broken_cube_compatibility{}; ///< Has broken cube compatiblity bit
  416. bool has_renderdoc{}; ///< Has RenderDoc attached
  417. bool has_nsight_graphics{}; ///< Has Nsight Graphics attached
  418. bool supports_d24_depth{}; ///< Supports D24 depth buffers.
  419. bool cant_blit_msaa{}; ///< Does not support MSAA<->MSAA blitting.
  420. bool must_emulate_bgr565{}; ///< Emulates BGR565 by swizzling RGB565 format.
  421. bool has_null_descriptor{}; ///< Has support for null descriptors.
  422. u32 max_vertex_input_attributes{}; ///< Max vertex input attributes in pipeline
  423. u32 max_vertex_input_bindings{}; ///< Max vertex input buffers in pipeline
  424. // Telemetry parameters
  425. std::string vendor_name; ///< Device's driver name.
  426. std::vector<std::string> supported_extensions; ///< Reported Vulkan extensions.
  427. std::vector<size_t> valid_heap_memory; ///< Heaps used.
  428. /// Format properties dictionary.
  429. std::unordered_map<VkFormat, VkFormatProperties> format_properties;
  430. /// Nsight Aftermath GPU crash tracker
  431. std::unique_ptr<NsightAftermathTracker> nsight_aftermath_tracker;
  432. };
  433. } // namespace Vulkan