vulkan_device.h 18 KB

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