vulkan_device.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 dispatch loader with direct function pointers of the device.
  37. const vk::DeviceDispatch& GetDispatchLoader() const {
  38. return dld;
  39. }
  40. /// Returns the logical device.
  41. const vk::Device& GetLogical() const {
  42. return logical;
  43. }
  44. /// Returns the physical device.
  45. vk::PhysicalDevice GetPhysical() const {
  46. return physical;
  47. }
  48. /// Returns the main graphics queue.
  49. vk::Queue GetGraphicsQueue() const {
  50. return graphics_queue;
  51. }
  52. /// Returns the main present queue.
  53. vk::Queue GetPresentQueue() const {
  54. return present_queue;
  55. }
  56. /// Returns main graphics queue family index.
  57. u32 GetGraphicsFamily() const {
  58. return graphics_family;
  59. }
  60. /// Returns main present queue family index.
  61. u32 GetPresentFamily() const {
  62. return present_family;
  63. }
  64. /// Returns the current Vulkan API version provided in Vulkan-formatted version numbers.
  65. u32 ApiVersion() const {
  66. return properties.apiVersion;
  67. }
  68. /// Returns the current driver version provided in Vulkan-formatted version numbers.
  69. u32 GetDriverVersion() const {
  70. return properties.driverVersion;
  71. }
  72. /// Returns the device name.
  73. std::string_view GetModelName() const {
  74. return properties.deviceName;
  75. }
  76. /// Returns the driver ID.
  77. VkDriverIdKHR GetDriverID() const {
  78. return driver_id;
  79. }
  80. /// Returns uniform buffer alignment requeriment.
  81. VkDeviceSize GetUniformBufferAlignment() const {
  82. return properties.limits.minUniformBufferOffsetAlignment;
  83. }
  84. /// Returns storage alignment requeriment.
  85. VkDeviceSize GetStorageBufferAlignment() const {
  86. return properties.limits.minStorageBufferOffsetAlignment;
  87. }
  88. /// Returns the maximum range for storage buffers.
  89. VkDeviceSize GetMaxStorageBufferRange() const {
  90. return properties.limits.maxStorageBufferRange;
  91. }
  92. /// Returns the maximum size for push constants.
  93. VkDeviceSize GetMaxPushConstantsSize() const {
  94. return properties.limits.maxPushConstantsSize;
  95. }
  96. /// Returns the maximum size for shared memory.
  97. u32 GetMaxComputeSharedMemorySize() const {
  98. return properties.limits.maxComputeSharedMemorySize;
  99. }
  100. /// Returns true if ASTC is natively supported.
  101. bool IsOptimalAstcSupported() const {
  102. return is_optimal_astc_supported;
  103. }
  104. /// Returns true if the device supports float16 natively
  105. bool IsFloat16Supported() const {
  106. return is_float16_supported;
  107. }
  108. /// Returns true if the device warp size can potentially be bigger than guest's warp size.
  109. bool IsWarpSizePotentiallyBiggerThanGuest() const {
  110. return is_warp_potentially_bigger;
  111. }
  112. /// Returns true if the device can be forced to use the guest warp size.
  113. bool IsGuestWarpSizeSupported(VkShaderStageFlagBits stage) const {
  114. return guest_warp_stages & stage;
  115. }
  116. /// Returns true if formatless image load is supported.
  117. bool IsFormatlessImageLoadSupported() const {
  118. return is_formatless_image_load_supported;
  119. }
  120. /// Returns true when blitting from and to depth stencil images is supported.
  121. bool IsBlitDepthStencilSupported() const {
  122. return is_blit_depth_stencil_supported;
  123. }
  124. /// Returns true if the device supports VK_NV_viewport_swizzle.
  125. bool IsNvViewportSwizzleSupported() const {
  126. return nv_viewport_swizzle;
  127. }
  128. /// Returns true if the device supports VK_EXT_scalar_block_layout.
  129. bool IsKhrUniformBufferStandardLayoutSupported() const {
  130. return khr_uniform_buffer_standard_layout;
  131. }
  132. /// Returns true if the device supports VK_EXT_index_type_uint8.
  133. bool IsExtIndexTypeUint8Supported() const {
  134. return ext_index_type_uint8;
  135. }
  136. /// Returns true if the device supports VK_EXT_sampler_filter_minmax.
  137. bool IsExtSamplerFilterMinmaxSupported() const {
  138. return ext_sampler_filter_minmax;
  139. }
  140. /// Returns true if the device supports VK_EXT_depth_range_unrestricted.
  141. bool IsExtDepthRangeUnrestrictedSupported() const {
  142. return ext_depth_range_unrestricted;
  143. }
  144. /// Returns true if the device supports VK_EXT_shader_viewport_index_layer.
  145. bool IsExtShaderViewportIndexLayerSupported() const {
  146. return ext_shader_viewport_index_layer;
  147. }
  148. /// Returns true if the device supports VK_EXT_transform_feedback.
  149. bool IsExtTransformFeedbackSupported() const {
  150. return ext_transform_feedback;
  151. }
  152. /// Returns true if the device supports VK_EXT_custom_border_color.
  153. bool IsExtCustomBorderColorSupported() const {
  154. return ext_custom_border_color;
  155. }
  156. /// Returns true if the device supports VK_EXT_extended_dynamic_state.
  157. bool IsExtExtendedDynamicStateSupported() const {
  158. return ext_extended_dynamic_state;
  159. }
  160. /// Returns true if the device supports VK_EXT_shader_stencil_export.
  161. bool IsExtShaderStencilExportSupported() const {
  162. return ext_shader_stencil_export;
  163. }
  164. /// Returns true when a known debugging tool is attached.
  165. bool HasDebuggingToolAttached() const {
  166. return has_renderdoc || has_nsight_graphics;
  167. }
  168. /// Returns the vendor name reported from Vulkan.
  169. std::string_view GetVendorName() const {
  170. return vendor_name;
  171. }
  172. /// Returns the list of available extensions.
  173. const std::vector<std::string>& GetAvailableExtensions() const {
  174. return reported_extensions;
  175. }
  176. /// Returns true if the setting for async shader compilation is enabled.
  177. bool UseAsynchronousShaders() const {
  178. return use_asynchronous_shaders;
  179. }
  180. private:
  181. /// Checks if the physical device is suitable.
  182. void CheckSuitability(bool requires_swapchain) const;
  183. /// Loads extensions into a vector and stores available ones in this object.
  184. std::vector<const char*> LoadExtensions(bool requires_surface);
  185. /// Sets up queue families.
  186. void SetupFamilies(VkSurfaceKHR surface);
  187. /// Sets up device features.
  188. void SetupFeatures();
  189. /// Collects telemetry information from the device.
  190. void CollectTelemetryParameters();
  191. /// Collects information about attached tools.
  192. void CollectToolingInfo();
  193. /// Returns a list of queue initialization descriptors.
  194. std::vector<VkDeviceQueueCreateInfo> GetDeviceQueueCreateInfos() const;
  195. /// Returns true if ASTC textures are natively supported.
  196. bool IsOptimalAstcSupported(const VkPhysicalDeviceFeatures& features) const;
  197. /// Returns true if the device natively supports blitting depth stencil images.
  198. bool TestDepthStencilBlits() const;
  199. /// Returns true if a format is supported.
  200. bool IsFormatSupported(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  201. FormatType format_type) const;
  202. VkInstance instance; ///< Vulkan instance.
  203. vk::DeviceDispatch dld; ///< Device function pointers.
  204. vk::PhysicalDevice physical; ///< Physical device.
  205. VkPhysicalDeviceProperties properties; ///< Device properties.
  206. vk::Device logical; ///< Logical device.
  207. vk::Queue graphics_queue; ///< Main graphics queue.
  208. vk::Queue present_queue; ///< Main present queue.
  209. u32 instance_version{}; ///< Vulkan onstance version.
  210. u32 graphics_family{}; ///< Main graphics queue family index.
  211. u32 present_family{}; ///< Main present queue family index.
  212. VkDriverIdKHR driver_id{}; ///< Driver ID.
  213. VkShaderStageFlags guest_warp_stages{}; ///< Stages where the guest warp size can be forced.ed
  214. bool is_optimal_astc_supported{}; ///< Support for native ASTC.
  215. bool is_float16_supported{}; ///< Support for float16 arithmetics.
  216. bool is_warp_potentially_bigger{}; ///< Host warp size can be bigger than guest.
  217. bool is_formatless_image_load_supported{}; ///< Support for shader image read without format.
  218. bool is_shader_storage_image_multisample{}; ///< Support for image operations on MSAA images.
  219. bool is_blit_depth_stencil_supported{}; ///< Support for blitting from and to depth stencil.
  220. bool nv_viewport_swizzle{}; ///< Support for VK_NV_viewport_swizzle.
  221. bool khr_uniform_buffer_standard_layout{}; ///< Support for std430 on UBOs.
  222. bool ext_index_type_uint8{}; ///< Support for VK_EXT_index_type_uint8.
  223. bool ext_sampler_filter_minmax{}; ///< Support for VK_EXT_sampler_filter_minmax.
  224. bool ext_depth_range_unrestricted{}; ///< Support for VK_EXT_depth_range_unrestricted.
  225. bool ext_shader_viewport_index_layer{}; ///< Support for VK_EXT_shader_viewport_index_layer.
  226. bool ext_tooling_info{}; ///< Support for VK_EXT_tooling_info.
  227. bool ext_transform_feedback{}; ///< Support for VK_EXT_transform_feedback.
  228. bool ext_custom_border_color{}; ///< Support for VK_EXT_custom_border_color.
  229. bool ext_extended_dynamic_state{}; ///< Support for VK_EXT_extended_dynamic_state.
  230. bool ext_shader_stencil_export{}; ///< Support for VK_EXT_shader_stencil_export.
  231. bool nv_device_diagnostics_config{}; ///< Support for VK_NV_device_diagnostics_config.
  232. bool has_renderdoc{}; ///< Has RenderDoc attached
  233. bool has_nsight_graphics{}; ///< Has Nsight Graphics attached
  234. // Asynchronous Graphics Pipeline setting
  235. bool use_asynchronous_shaders{}; ///< Setting to use asynchronous shaders/graphics pipeline
  236. // Telemetry parameters
  237. std::string vendor_name; ///< Device's driver name.
  238. std::vector<std::string> reported_extensions; ///< Reported Vulkan extensions.
  239. /// Format properties dictionary.
  240. std::unordered_map<VkFormat, VkFormatProperties> format_properties;
  241. /// Nsight Aftermath GPU crash tracker
  242. std::unique_ptr<NsightAftermathTracker> nsight_aftermath_tracker;
  243. };
  244. } // namespace Vulkan