vulkan_device.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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_line_rasterization.
  229. bool IsExtLineRasterizationSupported() const {
  230. return ext_line_rasterization;
  231. }
  232. /// Returns true if the device supports VK_EXT_vertex_input_dynamic_state.
  233. bool IsExtVertexInputDynamicStateSupported() const {
  234. return ext_vertex_input_dynamic_state;
  235. }
  236. /// Returns true if the device supports VK_EXT_shader_stencil_export.
  237. bool IsExtShaderStencilExportSupported() const {
  238. return ext_shader_stencil_export;
  239. }
  240. /// Returns true if the device supports VK_EXT_conservative_rasterization.
  241. bool IsExtConservativeRasterizationSupported() const {
  242. return ext_conservative_rasterization;
  243. }
  244. /// Returns true if the device supports VK_EXT_provoking_vertex.
  245. bool IsExtProvokingVertexSupported() const {
  246. return ext_provoking_vertex;
  247. }
  248. /// Returns true if the device supports VK_KHR_shader_atomic_int64.
  249. bool IsExtShaderAtomicInt64Supported() const {
  250. return ext_shader_atomic_int64;
  251. }
  252. /// Returns the minimum supported version of SPIR-V.
  253. u32 SupportedSpirvVersion() const {
  254. if (instance_version >= VK_API_VERSION_1_3) {
  255. return 0x00010600U;
  256. }
  257. if (khr_spirv_1_4) {
  258. return 0x00010400U;
  259. }
  260. return 0x00010000U;
  261. }
  262. /// Returns true when a known debugging tool is attached.
  263. bool HasDebuggingToolAttached() const {
  264. return has_renderdoc || has_nsight_graphics;
  265. }
  266. /// Returns true when the device does not properly support cube compatibility.
  267. bool HasBrokenCubeImageCompability() const {
  268. return has_broken_cube_compatibility;
  269. }
  270. /// Returns the vendor name reported from Vulkan.
  271. std::string_view GetVendorName() const {
  272. return vendor_name;
  273. }
  274. /// Returns the list of available extensions.
  275. const std::vector<std::string>& GetAvailableExtensions() const {
  276. return supported_extensions;
  277. }
  278. u64 GetDeviceLocalMemory() const {
  279. return device_access_memory;
  280. }
  281. bool CanReportMemoryUsage() const {
  282. return ext_memory_budget;
  283. }
  284. u64 GetDeviceMemoryUsage() const;
  285. u32 GetSetsPerPool() const {
  286. return sets_per_pool;
  287. }
  288. bool SupportsD24DepthBuffer() const {
  289. return supports_d24_depth;
  290. }
  291. bool CantBlitMSAA() const {
  292. return cant_blit_msaa;
  293. }
  294. bool MustEmulateBGR565() const {
  295. return must_emulate_bgr565;
  296. }
  297. private:
  298. /// Checks if the physical device is suitable.
  299. void CheckSuitability(bool requires_swapchain) const;
  300. /// Loads extensions into a vector and stores available ones in this object.
  301. std::vector<const char*> LoadExtensions(bool requires_surface);
  302. /// Sets up queue families.
  303. void SetupFamilies(VkSurfaceKHR surface);
  304. /// Sets up device features.
  305. void SetupFeatures();
  306. /// Sets up device properties.
  307. void SetupProperties();
  308. /// Collects telemetry information from the device.
  309. void CollectTelemetryParameters();
  310. /// Collects information about attached tools.
  311. void CollectToolingInfo();
  312. /// Collects information about the device's local memory.
  313. void CollectPhysicalMemoryInfo();
  314. /// Returns a list of queue initialization descriptors.
  315. std::vector<VkDeviceQueueCreateInfo> GetDeviceQueueCreateInfos() const;
  316. /// Returns true if ASTC textures are natively supported.
  317. bool IsOptimalAstcSupported(const VkPhysicalDeviceFeatures& features) const;
  318. /// Returns true if the device natively supports blitting depth stencil images.
  319. bool TestDepthStencilBlits() const;
  320. VkInstance instance; ///< Vulkan instance.
  321. vk::DeviceDispatch dld; ///< Device function pointers.
  322. vk::PhysicalDevice physical; ///< Physical device.
  323. VkPhysicalDeviceProperties properties; ///< Device properties.
  324. VkPhysicalDeviceFloatControlsPropertiesKHR float_controls{}; ///< Float control properties.
  325. vk::Device logical; ///< Logical device.
  326. vk::Queue graphics_queue; ///< Main graphics queue.
  327. vk::Queue present_queue; ///< Main present queue.
  328. u32 instance_version{}; ///< Vulkan onstance version.
  329. u32 graphics_family{}; ///< Main graphics queue family index.
  330. u32 present_family{}; ///< Main present queue family index.
  331. VkDriverIdKHR driver_id{}; ///< Driver ID.
  332. VkShaderStageFlags guest_warp_stages{}; ///< Stages where the guest warp size can be forced.
  333. u64 device_access_memory{}; ///< Total size of device local memory in bytes.
  334. u32 max_push_descriptors{}; ///< Maximum number of push descriptors
  335. u32 sets_per_pool{}; ///< Sets per Description Pool
  336. bool is_optimal_astc_supported{}; ///< Support for native ASTC.
  337. bool is_float16_supported{}; ///< Support for float16 arithmetic.
  338. bool is_int8_supported{}; ///< Support for int8 arithmetic.
  339. bool is_warp_potentially_bigger{}; ///< Host warp size can be bigger than guest.
  340. bool is_formatless_image_load_supported{}; ///< Support for shader image read without format.
  341. bool is_depth_bounds_supported{}; ///< Support for depth bounds.
  342. bool is_shader_float64_supported{}; ///< Support for float64.
  343. bool is_shader_int64_supported{}; ///< Support for int64.
  344. bool is_shader_int16_supported{}; ///< Support for int16.
  345. bool is_shader_storage_image_multisample{}; ///< Support for image operations on MSAA images.
  346. bool is_blit_depth_stencil_supported{}; ///< Support for blitting from and to depth stencil.
  347. bool is_topology_list_restart_supported{}; ///< Support for primitive restart with list
  348. ///< topologies.
  349. bool is_patch_list_restart_supported{}; ///< Support for primitive restart with list patch.
  350. bool is_integrated{}; ///< Is GPU an iGPU.
  351. bool is_virtual{}; ///< Is GPU a virtual GPU.
  352. bool is_non_gpu{}; ///< Is SoftwareRasterizer, FPGA, non-GPU device.
  353. bool nv_viewport_swizzle{}; ///< Support for VK_NV_viewport_swizzle.
  354. bool nv_viewport_array2{}; ///< Support for VK_NV_viewport_array2.
  355. bool nv_geometry_shader_passthrough{}; ///< Support for VK_NV_geometry_shader_passthrough.
  356. bool khr_uniform_buffer_standard_layout{}; ///< Support for scalar uniform buffer layouts.
  357. bool khr_spirv_1_4{}; ///< Support for VK_KHR_spirv_1_4.
  358. bool khr_workgroup_memory_explicit_layout{}; ///< Support for explicit workgroup layouts.
  359. bool khr_push_descriptor{}; ///< Support for VK_KHR_push_descritor.
  360. bool khr_pipeline_executable_properties{}; ///< Support for executable properties.
  361. bool khr_swapchain_mutable_format{}; ///< Support for VK_KHR_swapchain_mutable_format.
  362. bool ext_index_type_uint8{}; ///< Support for VK_EXT_index_type_uint8.
  363. bool ext_sampler_filter_minmax{}; ///< Support for VK_EXT_sampler_filter_minmax.
  364. bool ext_depth_clip_control{}; ///< Support for VK_EXT_depth_clip_control
  365. bool ext_depth_range_unrestricted{}; ///< Support for VK_EXT_depth_range_unrestricted.
  366. bool ext_shader_viewport_index_layer{}; ///< Support for VK_EXT_shader_viewport_index_layer.
  367. bool ext_tooling_info{}; ///< Support for VK_EXT_tooling_info.
  368. bool ext_subgroup_size_control{}; ///< Support for VK_EXT_subgroup_size_control.
  369. bool ext_transform_feedback{}; ///< Support for VK_EXT_transform_feedback.
  370. bool ext_custom_border_color{}; ///< Support for VK_EXT_custom_border_color.
  371. bool ext_extended_dynamic_state{}; ///< Support for VK_EXT_extended_dynamic_state.
  372. bool ext_line_rasterization{}; ///< Support for VK_EXT_line_rasterization.
  373. bool ext_vertex_input_dynamic_state{}; ///< Support for VK_EXT_vertex_input_dynamic_state.
  374. bool ext_shader_stencil_export{}; ///< Support for VK_EXT_shader_stencil_export.
  375. bool ext_shader_atomic_int64{}; ///< Support for VK_KHR_shader_atomic_int64.
  376. bool ext_conservative_rasterization{}; ///< Support for VK_EXT_conservative_rasterization.
  377. bool ext_provoking_vertex{}; ///< Support for VK_EXT_provoking_vertex.
  378. bool ext_memory_budget{}; ///< Support for VK_EXT_memory_budget.
  379. bool nv_device_diagnostics_config{}; ///< Support for VK_NV_device_diagnostics_config.
  380. bool has_broken_cube_compatibility{}; ///< Has broken cube compatiblity bit
  381. bool has_renderdoc{}; ///< Has RenderDoc attached
  382. bool has_nsight_graphics{}; ///< Has Nsight Graphics attached
  383. bool supports_d24_depth{}; ///< Supports D24 depth buffers.
  384. bool cant_blit_msaa{}; ///< Does not support MSAA<->MSAA blitting.
  385. bool must_emulate_bgr565{}; ///< Emulates BGR565 by swizzling RGB565 format.
  386. // Telemetry parameters
  387. std::string vendor_name; ///< Device's driver name.
  388. std::vector<std::string> supported_extensions; ///< Reported Vulkan extensions.
  389. std::vector<size_t> valid_heap_memory; ///< Heaps used.
  390. /// Format properties dictionary.
  391. std::unordered_map<VkFormat, VkFormatProperties> format_properties;
  392. /// Nsight Aftermath GPU crash tracker
  393. std::unique_ptr<NsightAftermathTracker> nsight_aftermath_tracker;
  394. };
  395. } // namespace Vulkan