vulkan_device.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. /// Reports a device loss.
  34. void ReportLoss() const;
  35. /// Reports a shader to Nsight Aftermath.
  36. void SaveShader(std::span<const u32> spirv) const;
  37. /// Returns the name of the VkDriverId reported from Vulkan.
  38. std::string GetDriverName() const;
  39. /// Returns the dispatch loader with direct function pointers of the device.
  40. const vk::DeviceDispatch& GetDispatchLoader() const {
  41. return dld;
  42. }
  43. /// Returns the logical device.
  44. const vk::Device& GetLogical() const {
  45. return logical;
  46. }
  47. /// Returns the physical device.
  48. vk::PhysicalDevice GetPhysical() const {
  49. return physical;
  50. }
  51. /// Returns the main graphics queue.
  52. vk::Queue GetGraphicsQueue() const {
  53. return graphics_queue;
  54. }
  55. /// Returns the main present queue.
  56. vk::Queue GetPresentQueue() const {
  57. return present_queue;
  58. }
  59. /// Returns main graphics queue family index.
  60. u32 GetGraphicsFamily() const {
  61. return graphics_family;
  62. }
  63. /// Returns main present queue family index.
  64. u32 GetPresentFamily() const {
  65. return present_family;
  66. }
  67. /// Returns the current Vulkan API version provided in Vulkan-formatted version numbers.
  68. u32 ApiVersion() const {
  69. return properties.apiVersion;
  70. }
  71. /// Returns the current driver version provided in Vulkan-formatted version numbers.
  72. u32 GetDriverVersion() const {
  73. return properties.driverVersion;
  74. }
  75. /// Returns the device name.
  76. std::string_view GetModelName() const {
  77. return properties.deviceName;
  78. }
  79. /// Returns the driver ID.
  80. VkDriverIdKHR GetDriverID() const {
  81. return driver_id;
  82. }
  83. /// Returns uniform buffer alignment requeriment.
  84. VkDeviceSize GetUniformBufferAlignment() const {
  85. return properties.limits.minUniformBufferOffsetAlignment;
  86. }
  87. /// Returns storage alignment requeriment.
  88. VkDeviceSize GetStorageBufferAlignment() const {
  89. return properties.limits.minStorageBufferOffsetAlignment;
  90. }
  91. /// Returns the maximum range for storage buffers.
  92. VkDeviceSize GetMaxStorageBufferRange() const {
  93. return properties.limits.maxStorageBufferRange;
  94. }
  95. /// Returns the maximum size for push constants.
  96. VkDeviceSize GetMaxPushConstantsSize() const {
  97. return properties.limits.maxPushConstantsSize;
  98. }
  99. /// Returns the maximum size for shared memory.
  100. u32 GetMaxComputeSharedMemorySize() const {
  101. return properties.limits.maxComputeSharedMemorySize;
  102. }
  103. /// Returns float control properties of the device.
  104. const VkPhysicalDeviceFloatControlsPropertiesKHR& FloatControlProperties() const {
  105. return float_controls;
  106. }
  107. /// Returns true if ASTC is natively supported.
  108. bool IsOptimalAstcSupported() const {
  109. return is_optimal_astc_supported;
  110. }
  111. /// Returns true if the device supports float16 natively
  112. bool IsFloat16Supported() const {
  113. return is_float16_supported;
  114. }
  115. /// Returns true if the device warp size can potentially be bigger than guest's warp size.
  116. bool IsWarpSizePotentiallyBiggerThanGuest() const {
  117. return is_warp_potentially_bigger;
  118. }
  119. /// Returns true if the device can be forced to use the guest warp size.
  120. bool IsGuestWarpSizeSupported(VkShaderStageFlagBits stage) const {
  121. return guest_warp_stages & stage;
  122. }
  123. /// Returns the maximum number of push descriptors.
  124. u32 MaxPushDescriptors() const {
  125. return max_push_descriptors;
  126. }
  127. /// Returns true if formatless image load is supported.
  128. bool IsFormatlessImageLoadSupported() const {
  129. return is_formatless_image_load_supported;
  130. }
  131. /// Returns true if shader int64 is supported.
  132. bool IsShaderInt64Supported() const {
  133. return is_shader_int64_supported;
  134. }
  135. /// Returns true if shader int16 is supported.
  136. bool IsShaderInt16Supported() const {
  137. return is_shader_int16_supported;
  138. }
  139. // Returns true if depth bounds is supported.
  140. bool IsDepthBoundsSupported() const {
  141. return is_depth_bounds_supported;
  142. }
  143. /// Returns true when blitting from and to depth stencil images is supported.
  144. bool IsBlitDepthStencilSupported() const {
  145. return is_blit_depth_stencil_supported;
  146. }
  147. /// Returns true if the device supports VK_NV_viewport_swizzle.
  148. bool IsNvViewportSwizzleSupported() const {
  149. return nv_viewport_swizzle;
  150. }
  151. /// Returns true if the device supports VK_NV_viewport_array2.
  152. bool IsNvViewportArray2Supported() const {
  153. return nv_viewport_array2;
  154. }
  155. /// Returns true if the device supports VK_NV_geometry_shader_passthrough.
  156. bool IsNvGeometryShaderPassthroughSupported() const {
  157. return nv_geometry_shader_passthrough;
  158. }
  159. /// Returns true if the device supports VK_KHR_uniform_buffer_standard_layout.
  160. bool IsKhrUniformBufferStandardLayoutSupported() const {
  161. return khr_uniform_buffer_standard_layout;
  162. }
  163. /// Returns true if the device supports VK_KHR_spirv_1_4.
  164. bool IsKhrSpirv1_4Supported() const {
  165. return khr_spirv_1_4;
  166. }
  167. /// Returns true if the device supports VK_KHR_push_descriptor.
  168. bool IsKhrPushDescriptorSupported() const {
  169. return khr_push_descriptor;
  170. }
  171. /// Returns true if VK_KHR_pipeline_executable_properties is enabled.
  172. bool IsKhrPipelineEexecutablePropertiesEnabled() const {
  173. return khr_pipeline_executable_properties;
  174. }
  175. /// Returns true if the device supports VK_KHR_workgroup_memory_explicit_layout.
  176. bool IsKhrWorkgroupMemoryExplicitLayoutSupported() const {
  177. return khr_workgroup_memory_explicit_layout;
  178. }
  179. /// Returns true if the device supports VK_EXT_index_type_uint8.
  180. bool IsExtIndexTypeUint8Supported() const {
  181. return ext_index_type_uint8;
  182. }
  183. /// Returns true if the device supports VK_EXT_sampler_filter_minmax.
  184. bool IsExtSamplerFilterMinmaxSupported() const {
  185. return ext_sampler_filter_minmax;
  186. }
  187. /// Returns true if the device supports VK_EXT_depth_range_unrestricted.
  188. bool IsExtDepthRangeUnrestrictedSupported() const {
  189. return ext_depth_range_unrestricted;
  190. }
  191. /// Returns true if the device supports VK_EXT_shader_viewport_index_layer.
  192. bool IsExtShaderViewportIndexLayerSupported() const {
  193. return ext_shader_viewport_index_layer;
  194. }
  195. /// Returns true if the device supports VK_EXT_subgroup_size_control.
  196. bool IsExtSubgroupSizeControlSupported() const {
  197. return ext_subgroup_size_control;
  198. }
  199. /// Returns true if the device supports VK_EXT_transform_feedback.
  200. bool IsExtTransformFeedbackSupported() const {
  201. return ext_transform_feedback;
  202. }
  203. /// Returns true if the device supports VK_EXT_custom_border_color.
  204. bool IsExtCustomBorderColorSupported() const {
  205. return ext_custom_border_color;
  206. }
  207. /// Returns true if the device supports VK_EXT_extended_dynamic_state.
  208. bool IsExtExtendedDynamicStateSupported() const {
  209. return ext_extended_dynamic_state;
  210. }
  211. /// Returns true if the device supports VK_EXT_line_rasterization.
  212. bool IsExtLineRasterizationSupported() const {
  213. return ext_line_rasterization;
  214. }
  215. /// Returns true if the device supports VK_EXT_vertex_input_dynamic_state.
  216. bool IsExtVertexInputDynamicStateSupported() const {
  217. return ext_vertex_input_dynamic_state;
  218. }
  219. /// Returns true if the device supports VK_EXT_shader_stencil_export.
  220. bool IsExtShaderStencilExportSupported() const {
  221. return ext_shader_stencil_export;
  222. }
  223. /// Returns true if the device supports VK_EXT_conservative_rasterization.
  224. bool IsExtConservativeRasterizationSupported() const {
  225. return ext_conservative_rasterization;
  226. }
  227. /// Returns true if the device supports VK_EXT_provoking_vertex.
  228. bool IsExtProvokingVertexSupported() const {
  229. return ext_provoking_vertex;
  230. }
  231. /// Returns true if the device supports VK_KHR_shader_atomic_int64.
  232. bool IsExtShaderAtomicInt64Supported() const {
  233. return ext_shader_atomic_int64;
  234. }
  235. /// Returns true when a known debugging tool is attached.
  236. bool HasDebuggingToolAttached() const {
  237. return has_renderdoc || has_nsight_graphics;
  238. }
  239. /// Returns the vendor name reported from Vulkan.
  240. std::string_view GetVendorName() const {
  241. return vendor_name;
  242. }
  243. /// Returns the list of available extensions.
  244. const std::vector<std::string>& GetAvailableExtensions() const {
  245. return supported_extensions;
  246. }
  247. u64 GetDeviceLocalMemory() const {
  248. return device_access_memory;
  249. }
  250. private:
  251. /// Checks if the physical device is suitable.
  252. void CheckSuitability(bool requires_swapchain) const;
  253. /// Loads extensions into a vector and stores available ones in this object.
  254. std::vector<const char*> LoadExtensions(bool requires_surface);
  255. /// Sets up queue families.
  256. void SetupFamilies(VkSurfaceKHR surface);
  257. /// Sets up device features.
  258. void SetupFeatures();
  259. /// Sets up device properties.
  260. void SetupProperties();
  261. /// Collects telemetry information from the device.
  262. void CollectTelemetryParameters();
  263. /// Collects information about attached tools.
  264. void CollectToolingInfo();
  265. /// Collects information about the device's local memory.
  266. void CollectPhysicalMemoryInfo();
  267. /// Returns a list of queue initialization descriptors.
  268. std::vector<VkDeviceQueueCreateInfo> GetDeviceQueueCreateInfos() const;
  269. /// Returns true if ASTC textures are natively supported.
  270. bool IsOptimalAstcSupported(const VkPhysicalDeviceFeatures& features) const;
  271. /// Returns true if the device natively supports blitting depth stencil images.
  272. bool TestDepthStencilBlits() const;
  273. /// Returns true if a format is supported.
  274. bool IsFormatSupported(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  275. FormatType format_type) const;
  276. VkInstance instance; ///< Vulkan instance.
  277. vk::DeviceDispatch dld; ///< Device function pointers.
  278. vk::PhysicalDevice physical; ///< Physical device.
  279. VkPhysicalDeviceProperties properties; ///< Device properties.
  280. VkPhysicalDeviceFloatControlsPropertiesKHR float_controls{}; ///< Float control properties.
  281. vk::Device logical; ///< Logical device.
  282. vk::Queue graphics_queue; ///< Main graphics queue.
  283. vk::Queue present_queue; ///< Main present queue.
  284. u32 instance_version{}; ///< Vulkan onstance version.
  285. u32 graphics_family{}; ///< Main graphics queue family index.
  286. u32 present_family{}; ///< Main present queue family index.
  287. VkDriverIdKHR driver_id{}; ///< Driver ID.
  288. VkShaderStageFlags guest_warp_stages{}; ///< Stages where the guest warp size can be forced.
  289. u64 device_access_memory{}; ///< Total size of device local memory in bytes.
  290. u32 max_push_descriptors{}; ///< Maximum number of push descriptors
  291. bool is_optimal_astc_supported{}; ///< Support for native ASTC.
  292. bool is_float16_supported{}; ///< Support for float16 arithmetics.
  293. bool is_warp_potentially_bigger{}; ///< Host warp size can be bigger than guest.
  294. bool is_formatless_image_load_supported{}; ///< Support for shader image read without format.
  295. bool is_depth_bounds_supported{}; ///< Support for depth bounds.
  296. bool is_shader_float64_supported{}; ///< Support for float64.
  297. bool is_shader_int64_supported{}; ///< Support for int64.
  298. bool is_shader_int16_supported{}; ///< Support for int16.
  299. bool is_shader_storage_image_multisample{}; ///< Support for image operations on MSAA images.
  300. bool is_blit_depth_stencil_supported{}; ///< Support for blitting from and to depth stencil.
  301. bool nv_viewport_swizzle{}; ///< Support for VK_NV_viewport_swizzle.
  302. bool nv_viewport_array2{}; ///< Support for VK_NV_viewport_array2.
  303. bool nv_geometry_shader_passthrough{}; ///< Support for VK_NV_geometry_shader_passthrough.
  304. bool khr_uniform_buffer_standard_layout{}; ///< Support for scalar uniform buffer layouts.
  305. bool khr_spirv_1_4{}; ///< Support for VK_KHR_spirv_1_4.
  306. bool khr_workgroup_memory_explicit_layout{}; ///< Support for explicit workgroup layouts.
  307. bool khr_push_descriptor{}; ///< Support for VK_KHR_push_descritor.
  308. bool khr_pipeline_executable_properties{}; ///< Support for executable properties.
  309. bool ext_index_type_uint8{}; ///< Support for VK_EXT_index_type_uint8.
  310. bool ext_sampler_filter_minmax{}; ///< Support for VK_EXT_sampler_filter_minmax.
  311. bool ext_depth_range_unrestricted{}; ///< Support for VK_EXT_depth_range_unrestricted.
  312. bool ext_shader_viewport_index_layer{}; ///< Support for VK_EXT_shader_viewport_index_layer.
  313. bool ext_tooling_info{}; ///< Support for VK_EXT_tooling_info.
  314. bool ext_subgroup_size_control{}; ///< Support for VK_EXT_subgroup_size_control.
  315. bool ext_transform_feedback{}; ///< Support for VK_EXT_transform_feedback.
  316. bool ext_custom_border_color{}; ///< Support for VK_EXT_custom_border_color.
  317. bool ext_extended_dynamic_state{}; ///< Support for VK_EXT_extended_dynamic_state.
  318. bool ext_line_rasterization{}; ///< Support for VK_EXT_line_rasterization.
  319. bool ext_vertex_input_dynamic_state{}; ///< Support for VK_EXT_vertex_input_dynamic_state.
  320. bool ext_shader_stencil_export{}; ///< Support for VK_EXT_shader_stencil_export.
  321. bool ext_shader_atomic_int64{}; ///< Support for VK_KHR_shader_atomic_int64.
  322. bool ext_conservative_rasterization{}; ///< Support for VK_EXT_conservative_rasterization.
  323. bool ext_provoking_vertex{}; ///< Support for VK_EXT_provoking_vertex.
  324. bool nv_device_diagnostics_config{}; ///< Support for VK_NV_device_diagnostics_config.
  325. bool has_renderdoc{}; ///< Has RenderDoc attached
  326. bool has_nsight_graphics{}; ///< Has Nsight Graphics attached
  327. // Telemetry parameters
  328. std::string vendor_name; ///< Device's driver name.
  329. std::vector<std::string> supported_extensions; ///< Reported Vulkan extensions.
  330. /// Format properties dictionary.
  331. std::unordered_map<VkFormat, VkFormatProperties> format_properties;
  332. /// Nsight Aftermath GPU crash tracker
  333. std::unique_ptr<NsightAftermathTracker> nsight_aftermath_tracker;
  334. };
  335. } // namespace Vulkan