vulkan_device.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. /// Returns uniform buffer alignment requeriment.
  85. VkDeviceSize GetUniformBufferAlignment() const {
  86. return properties.limits.minUniformBufferOffsetAlignment;
  87. }
  88. /// Returns storage alignment requeriment.
  89. VkDeviceSize GetStorageBufferAlignment() const {
  90. return properties.limits.minStorageBufferOffsetAlignment;
  91. }
  92. /// Returns the maximum range for storage buffers.
  93. VkDeviceSize GetMaxStorageBufferRange() const {
  94. return properties.limits.maxStorageBufferRange;
  95. }
  96. /// Returns the maximum size for push constants.
  97. VkDeviceSize GetMaxPushConstantsSize() const {
  98. return properties.limits.maxPushConstantsSize;
  99. }
  100. /// Returns the maximum size for shared memory.
  101. u32 GetMaxComputeSharedMemorySize() const {
  102. return properties.limits.maxComputeSharedMemorySize;
  103. }
  104. /// Returns float control properties of the device.
  105. const VkPhysicalDeviceFloatControlsPropertiesKHR& FloatControlProperties() const {
  106. return float_controls;
  107. }
  108. /// Returns true if ASTC is natively supported.
  109. bool IsOptimalAstcSupported() const {
  110. return is_optimal_astc_supported;
  111. }
  112. /// Returns true if the device supports float16 natively.
  113. bool IsFloat16Supported() const {
  114. return is_float16_supported;
  115. }
  116. /// Returns true if the device supports int8 natively.
  117. bool IsInt8Supported() const {
  118. return is_int8_supported;
  119. }
  120. /// Returns true if the device warp size can potentially be bigger than guest's warp size.
  121. bool IsWarpSizePotentiallyBiggerThanGuest() const {
  122. return is_warp_potentially_bigger;
  123. }
  124. /// Returns true if the device can be forced to use the guest warp size.
  125. bool IsGuestWarpSizeSupported(VkShaderStageFlagBits stage) const {
  126. return guest_warp_stages & stage;
  127. }
  128. /// Returns the maximum number of push descriptors.
  129. u32 MaxPushDescriptors() const {
  130. return max_push_descriptors;
  131. }
  132. /// Returns true if formatless image load is supported.
  133. bool IsFormatlessImageLoadSupported() const {
  134. return is_formatless_image_load_supported;
  135. }
  136. /// Returns true if shader int64 is supported.
  137. bool IsShaderInt64Supported() const {
  138. return is_shader_int64_supported;
  139. }
  140. /// Returns true if shader int16 is supported.
  141. bool IsShaderInt16Supported() const {
  142. return is_shader_int16_supported;
  143. }
  144. // Returns true if depth bounds is supported.
  145. bool IsDepthBoundsSupported() const {
  146. return is_depth_bounds_supported;
  147. }
  148. /// Returns true when blitting from and to depth stencil images is supported.
  149. bool IsBlitDepthStencilSupported() const {
  150. return is_blit_depth_stencil_supported;
  151. }
  152. /// Returns true if the device supports VK_NV_viewport_swizzle.
  153. bool IsNvViewportSwizzleSupported() const {
  154. return nv_viewport_swizzle;
  155. }
  156. /// Returns true if the device supports VK_NV_viewport_array2.
  157. bool IsNvViewportArray2Supported() const {
  158. return nv_viewport_array2;
  159. }
  160. /// Returns true if the device supports VK_NV_geometry_shader_passthrough.
  161. bool IsNvGeometryShaderPassthroughSupported() const {
  162. return nv_geometry_shader_passthrough;
  163. }
  164. /// Returns true if the device supports VK_KHR_uniform_buffer_standard_layout.
  165. bool IsKhrUniformBufferStandardLayoutSupported() const {
  166. return khr_uniform_buffer_standard_layout;
  167. }
  168. /// Returns true if the device supports VK_KHR_push_descriptor.
  169. bool IsKhrPushDescriptorSupported() const {
  170. return khr_push_descriptor;
  171. }
  172. /// Returns true if VK_KHR_pipeline_executable_properties is enabled.
  173. bool IsKhrPipelineExecutablePropertiesEnabled() const {
  174. return khr_pipeline_executable_properties;
  175. }
  176. /// Returns true if VK_KHR_swapchain_mutable_format is enabled.
  177. bool IsKhrSwapchainMutableFormatEnabled() const {
  178. return khr_swapchain_mutable_format;
  179. }
  180. /// Returns true if the device supports VK_KHR_workgroup_memory_explicit_layout.
  181. bool IsKhrWorkgroupMemoryExplicitLayoutSupported() const {
  182. return khr_workgroup_memory_explicit_layout;
  183. }
  184. /// Returns true if the device supports VK_EXT_primitive_topology_list_restart.
  185. bool IsTopologyListPrimitiveRestartSupported() const {
  186. return is_topology_list_restart_supported;
  187. }
  188. /// Returns true if the device supports VK_EXT_primitive_topology_list_restart.
  189. bool IsPatchListPrimitiveRestartSupported() const {
  190. return is_patch_list_restart_supported;
  191. }
  192. /// Returns true if the device supports VK_EXT_index_type_uint8.
  193. bool IsExtIndexTypeUint8Supported() const {
  194. return ext_index_type_uint8;
  195. }
  196. /// Returns true if the device supports VK_EXT_sampler_filter_minmax.
  197. bool IsExtSamplerFilterMinmaxSupported() const {
  198. return ext_sampler_filter_minmax;
  199. }
  200. /// Returns true if the device supports VK_EXT_depth_range_unrestricted.
  201. bool IsExtDepthRangeUnrestrictedSupported() const {
  202. return ext_depth_range_unrestricted;
  203. }
  204. /// Returns true if the device supports VK_EXT_depth_clip_control.
  205. bool IsExtDepthClipControlSupported() const {
  206. return ext_depth_clip_control;
  207. }
  208. /// Returns true if the device supports VK_EXT_shader_viewport_index_layer.
  209. bool IsExtShaderViewportIndexLayerSupported() const {
  210. return ext_shader_viewport_index_layer;
  211. }
  212. /// Returns true if the device supports VK_EXT_subgroup_size_control.
  213. bool IsExtSubgroupSizeControlSupported() const {
  214. return ext_subgroup_size_control;
  215. }
  216. /// Returns true if the device supports VK_EXT_transform_feedback.
  217. bool IsExtTransformFeedbackSupported() const {
  218. return ext_transform_feedback;
  219. }
  220. /// Returns true if the device supports VK_EXT_custom_border_color.
  221. bool IsExtCustomBorderColorSupported() const {
  222. return ext_custom_border_color;
  223. }
  224. /// Returns true if the device supports VK_EXT_extended_dynamic_state.
  225. bool IsExtExtendedDynamicStateSupported() const {
  226. return ext_extended_dynamic_state;
  227. }
  228. /// Returns true if the device supports VK_EXT_extended_dynamic_state2.
  229. bool IsExtExtendedDynamicState2Supported() const {
  230. return ext_extended_dynamic_state_2;
  231. }
  232. bool IsExtExtendedDynamicState2ExtrasSupported() const {
  233. return ext_extended_dynamic_state_2_extra;
  234. }
  235. /// Returns true if the device supports VK_EXT_extended_dynamic_state3.
  236. bool IsExtExtendedDynamicState3Supported() const {
  237. return ext_extended_dynamic_state_3;
  238. }
  239. /// Returns true if the device supports VK_EXT_extended_dynamic_state3.
  240. bool IsExtExtendedDynamicState3BlendingSupported() const {
  241. return ext_extended_dynamic_state_3_blend;
  242. }
  243. /// Returns true if the device supports VK_EXT_extended_dynamic_state3.
  244. bool IsExtExtendedDynamicState3EnablesSupported() const {
  245. return ext_extended_dynamic_state_3_enables;
  246. }
  247. /// Returns true if the device supports VK_EXT_line_rasterization.
  248. bool IsExtLineRasterizationSupported() const {
  249. return ext_line_rasterization;
  250. }
  251. /// Returns true if the device supports VK_EXT_vertex_input_dynamic_state.
  252. bool IsExtVertexInputDynamicStateSupported() const {
  253. return ext_vertex_input_dynamic_state;
  254. }
  255. /// Returns true if the device supports VK_EXT_shader_stencil_export.
  256. bool IsExtShaderStencilExportSupported() const {
  257. return ext_shader_stencil_export;
  258. }
  259. /// Returns true if the device supports VK_EXT_conservative_rasterization.
  260. bool IsExtConservativeRasterizationSupported() const {
  261. return ext_conservative_rasterization;
  262. }
  263. /// Returns true if the device supports VK_EXT_provoking_vertex.
  264. bool IsExtProvokingVertexSupported() const {
  265. return ext_provoking_vertex;
  266. }
  267. /// Returns true if the device supports VK_KHR_shader_atomic_int64.
  268. bool IsExtShaderAtomicInt64Supported() const {
  269. return ext_shader_atomic_int64;
  270. }
  271. /// Returns the minimum supported version of SPIR-V.
  272. u32 SupportedSpirvVersion() const {
  273. if (instance_version >= VK_API_VERSION_1_3) {
  274. return 0x00010600U;
  275. }
  276. if (khr_spirv_1_4) {
  277. return 0x00010400U;
  278. }
  279. return 0x00010000U;
  280. }
  281. /// Returns true when a known debugging tool is attached.
  282. bool HasDebuggingToolAttached() const {
  283. return has_renderdoc || has_nsight_graphics;
  284. }
  285. /// Returns true when the device does not properly support cube compatibility.
  286. bool HasBrokenCubeImageCompability() const {
  287. return has_broken_cube_compatibility;
  288. }
  289. /// Returns the vendor name reported from Vulkan.
  290. std::string_view GetVendorName() const {
  291. return vendor_name;
  292. }
  293. /// Returns the list of available extensions.
  294. const std::vector<std::string>& GetAvailableExtensions() const {
  295. return supported_extensions;
  296. }
  297. u64 GetDeviceLocalMemory() const {
  298. return device_access_memory;
  299. }
  300. bool CanReportMemoryUsage() const {
  301. return ext_memory_budget;
  302. }
  303. u64 GetDeviceMemoryUsage() const;
  304. u32 GetSetsPerPool() const {
  305. return sets_per_pool;
  306. }
  307. bool SupportsD24DepthBuffer() const {
  308. return supports_d24_depth;
  309. }
  310. bool CantBlitMSAA() const {
  311. return cant_blit_msaa;
  312. }
  313. bool MustEmulateBGR565() const {
  314. return must_emulate_bgr565;
  315. }
  316. u32 GetMaxVertexInputAttributes() const {
  317. return max_vertex_input_attributes;
  318. }
  319. u32 GetMaxVertexInputBindings() const {
  320. return max_vertex_input_bindings;
  321. }
  322. private:
  323. /// Checks if the physical device is suitable.
  324. void CheckSuitability(bool requires_swapchain) const;
  325. /// Loads extensions into a vector and stores available ones in this object.
  326. std::vector<const char*> LoadExtensions(bool requires_surface);
  327. /// Sets up queue families.
  328. void SetupFamilies(VkSurfaceKHR surface);
  329. /// Sets up device features.
  330. void SetupFeatures();
  331. /// Sets up device properties.
  332. void SetupProperties();
  333. /// Collects telemetry information from the device.
  334. void CollectTelemetryParameters();
  335. /// Collects information about attached tools.
  336. void CollectToolingInfo();
  337. /// Collects information about the device's local memory.
  338. void CollectPhysicalMemoryInfo();
  339. /// Returns a list of queue initialization descriptors.
  340. std::vector<VkDeviceQueueCreateInfo> GetDeviceQueueCreateInfos() const;
  341. /// Returns true if ASTC textures are natively supported.
  342. bool IsOptimalAstcSupported(const VkPhysicalDeviceFeatures& features) const;
  343. /// Returns true if the device natively supports blitting depth stencil images.
  344. bool TestDepthStencilBlits() const;
  345. VkInstance instance; ///< Vulkan instance.
  346. vk::DeviceDispatch dld; ///< Device function pointers.
  347. vk::PhysicalDevice physical; ///< Physical device.
  348. VkPhysicalDeviceProperties properties; ///< Device properties.
  349. VkPhysicalDeviceFloatControlsPropertiesKHR float_controls{}; ///< Float control properties.
  350. vk::Device logical; ///< Logical device.
  351. vk::Queue graphics_queue; ///< Main graphics queue.
  352. vk::Queue present_queue; ///< Main present queue.
  353. u32 instance_version{}; ///< Vulkan onstance version.
  354. u32 graphics_family{}; ///< Main graphics queue family index.
  355. u32 present_family{}; ///< Main present queue family index.
  356. VkDriverIdKHR driver_id{}; ///< Driver ID.
  357. VkShaderStageFlags guest_warp_stages{}; ///< Stages where the guest warp size can be forced.
  358. u64 device_access_memory{}; ///< Total size of device local memory in bytes.
  359. u32 max_push_descriptors{}; ///< Maximum number of push descriptors
  360. u32 sets_per_pool{}; ///< Sets per Description Pool
  361. bool is_optimal_astc_supported{}; ///< Support for native ASTC.
  362. bool is_float16_supported{}; ///< Support for float16 arithmetic.
  363. bool is_int8_supported{}; ///< Support for int8 arithmetic.
  364. bool is_warp_potentially_bigger{}; ///< Host warp size can be bigger than guest.
  365. bool is_formatless_image_load_supported{}; ///< Support for shader image read without format.
  366. bool is_depth_bounds_supported{}; ///< Support for depth bounds.
  367. bool is_shader_float64_supported{}; ///< Support for float64.
  368. bool is_shader_int64_supported{}; ///< Support for int64.
  369. bool is_shader_int16_supported{}; ///< Support for int16.
  370. bool is_shader_storage_image_multisample{}; ///< Support for image operations on MSAA images.
  371. bool is_blit_depth_stencil_supported{}; ///< Support for blitting from and to depth stencil.
  372. bool is_topology_list_restart_supported{}; ///< Support for primitive restart with list
  373. ///< topologies.
  374. bool is_patch_list_restart_supported{}; ///< Support for primitive restart with list patch.
  375. bool is_integrated{}; ///< Is GPU an iGPU.
  376. bool is_virtual{}; ///< Is GPU a virtual GPU.
  377. bool is_non_gpu{}; ///< Is SoftwareRasterizer, FPGA, non-GPU device.
  378. bool nv_viewport_swizzle{}; ///< Support for VK_NV_viewport_swizzle.
  379. bool nv_viewport_array2{}; ///< Support for VK_NV_viewport_array2.
  380. bool nv_geometry_shader_passthrough{}; ///< Support for VK_NV_geometry_shader_passthrough.
  381. bool khr_draw_indirect_count{}; ///< Support for VK_KHR_draw_indirect_count.
  382. bool khr_uniform_buffer_standard_layout{}; ///< Support for scalar uniform buffer layouts.
  383. bool khr_spirv_1_4{}; ///< Support for VK_KHR_spirv_1_4.
  384. bool khr_workgroup_memory_explicit_layout{}; ///< Support for explicit workgroup layouts.
  385. bool khr_push_descriptor{}; ///< Support for VK_KHR_push_descritor.
  386. bool khr_pipeline_executable_properties{}; ///< Support for executable properties.
  387. bool khr_swapchain_mutable_format{}; ///< Support for VK_KHR_swapchain_mutable_format.
  388. bool ext_index_type_uint8{}; ///< Support for VK_EXT_index_type_uint8.
  389. bool ext_sampler_filter_minmax{}; ///< Support for VK_EXT_sampler_filter_minmax.
  390. bool ext_depth_clip_control{}; ///< Support for VK_EXT_depth_clip_control
  391. bool ext_depth_range_unrestricted{}; ///< Support for VK_EXT_depth_range_unrestricted.
  392. bool ext_shader_viewport_index_layer{}; ///< Support for VK_EXT_shader_viewport_index_layer.
  393. bool ext_tooling_info{}; ///< Support for VK_EXT_tooling_info.
  394. bool ext_subgroup_size_control{}; ///< Support for VK_EXT_subgroup_size_control.
  395. bool ext_transform_feedback{}; ///< Support for VK_EXT_transform_feedback.
  396. bool ext_custom_border_color{}; ///< Support for VK_EXT_custom_border_color.
  397. bool ext_extended_dynamic_state{}; ///< Support for VK_EXT_extended_dynamic_state.
  398. bool ext_extended_dynamic_state_2{}; ///< Support for VK_EXT_extended_dynamic_state2.
  399. bool ext_extended_dynamic_state_2_extra{}; ///< Support for VK_EXT_extended_dynamic_state2.
  400. bool ext_extended_dynamic_state_3{}; ///< Support for VK_EXT_extended_dynamic_state3.
  401. bool ext_extended_dynamic_state_3_blend{}; ///< Support for VK_EXT_extended_dynamic_state3.
  402. bool ext_extended_dynamic_state_3_enables{}; ///< Support for VK_EXT_extended_dynamic_state3.
  403. bool ext_line_rasterization{}; ///< Support for VK_EXT_line_rasterization.
  404. bool ext_vertex_input_dynamic_state{}; ///< Support for VK_EXT_vertex_input_dynamic_state.
  405. bool ext_shader_stencil_export{}; ///< Support for VK_EXT_shader_stencil_export.
  406. bool ext_shader_atomic_int64{}; ///< Support for VK_KHR_shader_atomic_int64.
  407. bool ext_conservative_rasterization{}; ///< Support for VK_EXT_conservative_rasterization.
  408. bool ext_provoking_vertex{}; ///< Support for VK_EXT_provoking_vertex.
  409. bool ext_memory_budget{}; ///< Support for VK_EXT_memory_budget.
  410. bool nv_device_diagnostics_config{}; ///< Support for VK_NV_device_diagnostics_config.
  411. bool has_broken_cube_compatibility{}; ///< Has broken cube compatiblity bit
  412. bool has_renderdoc{}; ///< Has RenderDoc attached
  413. bool has_nsight_graphics{}; ///< Has Nsight Graphics attached
  414. bool supports_d24_depth{}; ///< Supports D24 depth buffers.
  415. bool cant_blit_msaa{}; ///< Does not support MSAA<->MSAA blitting.
  416. bool must_emulate_bgr565{}; ///< Emulates BGR565 by swizzling RGB565 format.
  417. u32 max_vertex_input_attributes{}; ///< Max vertex input attributes in pipeline
  418. u32 max_vertex_input_bindings{}; ///< Max vertex input buffers in pipeline
  419. // Telemetry parameters
  420. std::string vendor_name; ///< Device's driver name.
  421. std::vector<std::string> supported_extensions; ///< Reported Vulkan extensions.
  422. std::vector<size_t> valid_heap_memory; ///< Heaps used.
  423. /// Format properties dictionary.
  424. std::unordered_map<VkFormat, VkFormatProperties> format_properties;
  425. /// Nsight Aftermath GPU crash tracker
  426. std::unique_ptr<NsightAftermathTracker> nsight_aftermath_tracker;
  427. };
  428. } // namespace Vulkan