vulkan_device.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 <string>
  6. #include <string_view>
  7. #include <unordered_map>
  8. #include <vector>
  9. #include "common/common_types.h"
  10. #include "video_core/vulkan_common/vulkan_wrapper.h"
  11. namespace Vulkan {
  12. class NsightAftermathTracker;
  13. /// Format usage descriptor.
  14. enum class FormatType { Linear, Optimal, Buffer };
  15. /// Subgroup size of the guest emulated hardware (Nvidia has 32 threads per subgroup).
  16. const u32 GuestWarpSize = 32;
  17. /// Handles data specific to a physical device.
  18. class Device {
  19. public:
  20. explicit Device(VkInstance instance, vk::PhysicalDevice physical, VkSurfaceKHR surface,
  21. const vk::InstanceDispatch& dld);
  22. ~Device();
  23. /**
  24. * Returns a format supported by the device for the passed requeriments.
  25. * @param wanted_format The ideal format to be returned. It may not be the returned format.
  26. * @param wanted_usage The usage that must be fulfilled even if the format is not supported.
  27. * @param format_type Format type usage.
  28. * @returns A format supported by the device.
  29. */
  30. VkFormat GetSupportedFormat(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  31. FormatType format_type) const;
  32. /// Reports a device loss.
  33. void ReportLoss() const;
  34. /// Reports a shader to Nsight Aftermath.
  35. void SaveShader(const std::vector<u32>& spirv) const;
  36. /// Returns the name of the VkDriverId reported from Vulkan.
  37. std::string GetDriverName() const;
  38. /// Returns the dispatch loader with direct function pointers of the device.
  39. const vk::DeviceDispatch& GetDispatchLoader() const {
  40. return dld;
  41. }
  42. /// Returns the logical device.
  43. const vk::Device& GetLogical() const {
  44. return logical;
  45. }
  46. /// Returns the physical device.
  47. vk::PhysicalDevice GetPhysical() const {
  48. return physical;
  49. }
  50. /// Returns the main graphics queue.
  51. vk::Queue GetGraphicsQueue() const {
  52. return graphics_queue;
  53. }
  54. /// Returns the main present queue.
  55. vk::Queue GetPresentQueue() const {
  56. return present_queue;
  57. }
  58. /// Returns main graphics queue family index.
  59. u32 GetGraphicsFamily() const {
  60. return graphics_family;
  61. }
  62. /// Returns main present queue family index.
  63. u32 GetPresentFamily() const {
  64. return present_family;
  65. }
  66. /// Returns the current Vulkan API version provided in Vulkan-formatted version numbers.
  67. u32 ApiVersion() const {
  68. return properties.apiVersion;
  69. }
  70. /// Returns the current driver version provided in Vulkan-formatted version numbers.
  71. u32 GetDriverVersion() const {
  72. return properties.driverVersion;
  73. }
  74. /// Returns the device name.
  75. std::string_view GetModelName() const {
  76. return properties.deviceName;
  77. }
  78. /// Returns the driver ID.
  79. VkDriverIdKHR GetDriverID() const {
  80. return driver_id;
  81. }
  82. /// Returns uniform buffer alignment requeriment.
  83. VkDeviceSize GetUniformBufferAlignment() const {
  84. return properties.limits.minUniformBufferOffsetAlignment;
  85. }
  86. /// Returns storage alignment requeriment.
  87. VkDeviceSize GetStorageBufferAlignment() const {
  88. return properties.limits.minStorageBufferOffsetAlignment;
  89. }
  90. /// Returns the maximum range for storage buffers.
  91. VkDeviceSize GetMaxStorageBufferRange() const {
  92. return properties.limits.maxStorageBufferRange;
  93. }
  94. /// Returns the maximum size for push constants.
  95. VkDeviceSize GetMaxPushConstantsSize() const {
  96. return properties.limits.maxPushConstantsSize;
  97. }
  98. /// Returns the maximum size for shared memory.
  99. u32 GetMaxComputeSharedMemorySize() const {
  100. return properties.limits.maxComputeSharedMemorySize;
  101. }
  102. /// Returns float control properties of the device.
  103. const VkPhysicalDeviceFloatControlsPropertiesKHR& FloatControlProperties() const {
  104. return float_controls;
  105. }
  106. /// Returns true if ASTC is natively supported.
  107. bool IsOptimalAstcSupported() const {
  108. return is_optimal_astc_supported;
  109. }
  110. /// Returns true if the device supports float16 natively
  111. bool IsFloat16Supported() const {
  112. return is_float16_supported;
  113. }
  114. /// Returns true if the device warp size can potentially be bigger than guest's warp size.
  115. bool IsWarpSizePotentiallyBiggerThanGuest() const {
  116. return is_warp_potentially_bigger;
  117. }
  118. /// Returns true if the device can be forced to use the guest warp size.
  119. bool IsGuestWarpSizeSupported(VkShaderStageFlagBits stage) const {
  120. return guest_warp_stages & stage;
  121. }
  122. /// Returns true if formatless image load is supported.
  123. bool IsFormatlessImageLoadSupported() const {
  124. return is_formatless_image_load_supported;
  125. }
  126. /// Returns true when blitting from and to depth stencil images is supported.
  127. bool IsBlitDepthStencilSupported() const {
  128. return is_blit_depth_stencil_supported;
  129. }
  130. /// Returns true if the device supports VK_NV_viewport_swizzle.
  131. bool IsNvViewportSwizzleSupported() const {
  132. return nv_viewport_swizzle;
  133. }
  134. /// Returns true if the device supports VK_EXT_scalar_block_layout.
  135. bool IsKhrUniformBufferStandardLayoutSupported() const {
  136. return khr_uniform_buffer_standard_layout;
  137. }
  138. /// Returns true if the device supports VK_EXT_index_type_uint8.
  139. bool IsExtIndexTypeUint8Supported() const {
  140. return ext_index_type_uint8;
  141. }
  142. /// Returns true if the device supports VK_EXT_sampler_filter_minmax.
  143. bool IsExtSamplerFilterMinmaxSupported() const {
  144. return ext_sampler_filter_minmax;
  145. }
  146. /// Returns true if the device supports VK_EXT_depth_range_unrestricted.
  147. bool IsExtDepthRangeUnrestrictedSupported() const {
  148. return ext_depth_range_unrestricted;
  149. }
  150. /// Returns true if the device supports VK_EXT_shader_viewport_index_layer.
  151. bool IsExtShaderViewportIndexLayerSupported() const {
  152. return ext_shader_viewport_index_layer;
  153. }
  154. /// Returns true if the device supports VK_EXT_subgroup_size_control.
  155. bool IsExtSubgroupSizeControlSupported() const {
  156. return ext_subgroup_size_control;
  157. }
  158. /// Returns true if the device supports VK_EXT_transform_feedback.
  159. bool IsExtTransformFeedbackSupported() const {
  160. return ext_transform_feedback;
  161. }
  162. /// Returns true if the device supports VK_EXT_custom_border_color.
  163. bool IsExtCustomBorderColorSupported() const {
  164. return ext_custom_border_color;
  165. }
  166. /// Returns true if the device supports VK_EXT_extended_dynamic_state.
  167. bool IsExtExtendedDynamicStateSupported() const {
  168. return ext_extended_dynamic_state;
  169. }
  170. /// Returns true if the device supports VK_EXT_shader_stencil_export.
  171. bool IsExtShaderStencilExportSupported() const {
  172. return ext_shader_stencil_export;
  173. }
  174. /// Returns true when a known debugging tool is attached.
  175. bool HasDebuggingToolAttached() const {
  176. return has_renderdoc || has_nsight_graphics;
  177. }
  178. /// Returns the vendor name reported from Vulkan.
  179. std::string_view GetVendorName() const {
  180. return vendor_name;
  181. }
  182. /// Returns the list of available extensions.
  183. const std::vector<std::string>& GetAvailableExtensions() const {
  184. return reported_extensions;
  185. }
  186. u64 GetDeviceLocalMemory() const {
  187. return device_access_memory;
  188. }
  189. private:
  190. /// Checks if the physical device is suitable.
  191. void CheckSuitability(bool requires_swapchain) const;
  192. /// Loads extensions into a vector and stores available ones in this object.
  193. std::vector<const char*> LoadExtensions(bool requires_surface);
  194. /// Sets up queue families.
  195. void SetupFamilies(VkSurfaceKHR surface);
  196. /// Sets up device features.
  197. void SetupFeatures();
  198. /// Sets up device properties.
  199. void SetupProperties();
  200. /// Collects telemetry information from the device.
  201. void CollectTelemetryParameters();
  202. /// Collects information about attached tools.
  203. void CollectToolingInfo();
  204. /// Collects information about the device's local memory.
  205. void CollectPhysicalMemoryInfo();
  206. /// Returns a list of queue initialization descriptors.
  207. std::vector<VkDeviceQueueCreateInfo> GetDeviceQueueCreateInfos() const;
  208. /// Returns true if ASTC textures are natively supported.
  209. bool IsOptimalAstcSupported(const VkPhysicalDeviceFeatures& features) const;
  210. /// Returns true if the device natively supports blitting depth stencil images.
  211. bool TestDepthStencilBlits() const;
  212. /// Returns true if a format is supported.
  213. bool IsFormatSupported(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  214. FormatType format_type) const;
  215. VkInstance instance; ///< Vulkan instance.
  216. vk::DeviceDispatch dld; ///< Device function pointers.
  217. vk::PhysicalDevice physical; ///< Physical device.
  218. VkPhysicalDeviceProperties properties; ///< Device properties.
  219. VkPhysicalDeviceFloatControlsPropertiesKHR float_controls{}; ///< Float control properties.
  220. vk::Device logical; ///< Logical device.
  221. vk::Queue graphics_queue; ///< Main graphics queue.
  222. vk::Queue present_queue; ///< Main present queue.
  223. u32 instance_version{}; ///< Vulkan onstance version.
  224. u32 graphics_family{}; ///< Main graphics queue family index.
  225. u32 present_family{}; ///< Main present queue family index.
  226. VkDriverIdKHR driver_id{}; ///< Driver ID.
  227. VkShaderStageFlags guest_warp_stages{}; ///< Stages where the guest warp size can be forced.
  228. u64 device_access_memory{}; ///< Total size of device local memory in bytes.
  229. bool is_optimal_astc_supported{}; ///< Support for native ASTC.
  230. bool is_float16_supported{}; ///< Support for float16 arithmetics.
  231. bool is_warp_potentially_bigger{}; ///< Host warp size can be bigger than guest.
  232. bool is_formatless_image_load_supported{}; ///< Support for shader image read without format.
  233. bool is_shader_storage_image_multisample{}; ///< Support for image operations on MSAA images.
  234. bool is_blit_depth_stencil_supported{}; ///< Support for blitting from and to depth stencil.
  235. bool nv_viewport_swizzle{}; ///< Support for VK_NV_viewport_swizzle.
  236. bool khr_uniform_buffer_standard_layout{}; ///< Support for std430 on UBOs.
  237. bool ext_index_type_uint8{}; ///< Support for VK_EXT_index_type_uint8.
  238. bool ext_sampler_filter_minmax{}; ///< Support for VK_EXT_sampler_filter_minmax.
  239. bool ext_depth_range_unrestricted{}; ///< Support for VK_EXT_depth_range_unrestricted.
  240. bool ext_shader_viewport_index_layer{}; ///< Support for VK_EXT_shader_viewport_index_layer.
  241. bool ext_tooling_info{}; ///< Support for VK_EXT_tooling_info.
  242. bool ext_subgroup_size_control{}; ///< Support for VK_EXT_subgroup_size_control.
  243. bool ext_transform_feedback{}; ///< Support for VK_EXT_transform_feedback.
  244. bool ext_custom_border_color{}; ///< Support for VK_EXT_custom_border_color.
  245. bool ext_extended_dynamic_state{}; ///< Support for VK_EXT_extended_dynamic_state.
  246. bool ext_shader_stencil_export{}; ///< Support for VK_EXT_shader_stencil_export.
  247. bool nv_device_diagnostics_config{}; ///< Support for VK_NV_device_diagnostics_config.
  248. bool has_renderdoc{}; ///< Has RenderDoc attached
  249. bool has_nsight_graphics{}; ///< Has Nsight Graphics attached
  250. // Telemetry parameters
  251. std::string vendor_name; ///< Device's driver name.
  252. std::vector<std::string> reported_extensions; ///< Reported Vulkan extensions.
  253. /// Format properties dictionary.
  254. std::unordered_map<VkFormat, VkFormatProperties> format_properties;
  255. /// Nsight Aftermath GPU crash tracker
  256. std::unique_ptr<NsightAftermathTracker> nsight_aftermath_tracker;
  257. };
  258. } // namespace Vulkan