vk_device.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/renderer_vulkan/nsight_aftermath_tracker.h"
  11. #include "video_core/renderer_vulkan/wrapper.h"
  12. namespace Vulkan {
  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 VKDevice final {
  19. public:
  20. explicit VKDevice(VkInstance instance, vk::PhysicalDevice physical, VkSurfaceKHR surface,
  21. const vk::InstanceDispatch& dld);
  22. ~VKDevice();
  23. /// Initializes the device. Returns true on success.
  24. bool Create();
  25. /**
  26. * Returns a format supported by the device for the passed requeriments.
  27. * @param wanted_format The ideal format to be returned. It may not be the returned format.
  28. * @param wanted_usage The usage that must be fulfilled even if the format is not supported.
  29. * @param format_type Format type usage.
  30. * @returns A format supported by the device.
  31. */
  32. VkFormat GetSupportedFormat(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(const std::vector<u32>& spirv) 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 true if the device is integrated with the host CPU.
  67. bool IsIntegrated() const {
  68. return properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;
  69. }
  70. /// Returns the current Vulkan API version provided in Vulkan-formatted version numbers.
  71. u32 GetApiVersion() 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 true if ASTC is natively supported.
  103. bool IsOptimalAstcSupported() const {
  104. return is_optimal_astc_supported;
  105. }
  106. /// Returns true if the device supports float16 natively
  107. bool IsFloat16Supported() const {
  108. return is_float16_supported;
  109. }
  110. /// Returns true if the device warp size can potentially be bigger than guest's warp size.
  111. bool IsWarpSizePotentiallyBiggerThanGuest() const {
  112. return is_warp_potentially_bigger;
  113. }
  114. /// Returns true if the device can be forced to use the guest warp size.
  115. bool IsGuestWarpSizeSupported(VkShaderStageFlagBits stage) const {
  116. return guest_warp_stages & stage;
  117. }
  118. /// Returns true if formatless image load is supported.
  119. bool IsFormatlessImageLoadSupported() const {
  120. return is_formatless_image_load_supported;
  121. }
  122. /// Returns true if the device supports VK_EXT_scalar_block_layout.
  123. bool IsKhrUniformBufferStandardLayoutSupported() const {
  124. return khr_uniform_buffer_standard_layout;
  125. }
  126. /// Returns true if the device supports VK_EXT_index_type_uint8.
  127. bool IsExtIndexTypeUint8Supported() const {
  128. return ext_index_type_uint8;
  129. }
  130. /// Returns true if the device supports VK_EXT_depth_range_unrestricted.
  131. bool IsExtDepthRangeUnrestrictedSupported() const {
  132. return ext_depth_range_unrestricted;
  133. }
  134. /// Returns true if the device supports VK_EXT_shader_viewport_index_layer.
  135. bool IsExtShaderViewportIndexLayerSupported() const {
  136. return ext_shader_viewport_index_layer;
  137. }
  138. /// Returns true if the device supports VK_EXT_transform_feedback.
  139. bool IsExtTransformFeedbackSupported() const {
  140. return ext_transform_feedback;
  141. }
  142. /// Returns the vendor name reported from Vulkan.
  143. std::string_view GetVendorName() const {
  144. return vendor_name;
  145. }
  146. /// Returns the list of available extensions.
  147. const std::vector<std::string>& GetAvailableExtensions() const {
  148. return reported_extensions;
  149. }
  150. /// Checks if the physical device is suitable.
  151. static bool IsSuitable(vk::PhysicalDevice physical, VkSurfaceKHR surface);
  152. private:
  153. /// Loads extensions into a vector and stores available ones in this object.
  154. std::vector<const char*> LoadExtensions();
  155. /// Sets up queue families.
  156. void SetupFamilies(VkSurfaceKHR surface);
  157. /// Sets up device features.
  158. void SetupFeatures();
  159. /// Collects telemetry information from the device.
  160. void CollectTelemetryParameters();
  161. /// Returns a list of queue initialization descriptors.
  162. std::vector<VkDeviceQueueCreateInfo> GetDeviceQueueCreateInfos() const;
  163. /// Returns true if ASTC textures are natively supported.
  164. bool IsOptimalAstcSupported(const VkPhysicalDeviceFeatures& features) const;
  165. /// Returns true if a format is supported.
  166. bool IsFormatSupported(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  167. FormatType format_type) const;
  168. vk::DeviceDispatch dld; ///< Device function pointers.
  169. vk::PhysicalDevice physical; ///< Physical device.
  170. VkPhysicalDeviceProperties properties; ///< Device properties.
  171. vk::Device logical; ///< Logical device.
  172. vk::Queue graphics_queue; ///< Main graphics queue.
  173. vk::Queue present_queue; ///< Main present queue.
  174. u32 graphics_family{}; ///< Main graphics queue family index.
  175. u32 present_family{}; ///< Main present queue family index.
  176. VkDriverIdKHR driver_id{}; ///< Driver ID.
  177. VkShaderStageFlags guest_warp_stages{}; ///< Stages where the guest warp size can be forced.ed
  178. bool is_optimal_astc_supported{}; ///< Support for native ASTC.
  179. bool is_float16_supported{}; ///< Support for float16 arithmetics.
  180. bool is_warp_potentially_bigger{}; ///< Host warp size can be bigger than guest.
  181. bool is_formatless_image_load_supported{}; ///< Support for shader image read without format.
  182. bool khr_uniform_buffer_standard_layout{}; ///< Support for std430 on UBOs.
  183. bool ext_index_type_uint8{}; ///< Support for VK_EXT_index_type_uint8.
  184. bool ext_depth_range_unrestricted{}; ///< Support for VK_EXT_depth_range_unrestricted.
  185. bool ext_shader_viewport_index_layer{}; ///< Support for VK_EXT_shader_viewport_index_layer.
  186. bool ext_transform_feedback{}; ///< Support for VK_EXT_transform_feedback.
  187. bool nv_device_diagnostics_config{}; ///< Support for VK_NV_device_diagnostics_config.
  188. // Telemetry parameters
  189. std::string vendor_name; ///< Device's driver name.
  190. std::vector<std::string> reported_extensions; ///< Reported Vulkan extensions.
  191. /// Format properties dictionary.
  192. std::unordered_map<VkFormat, VkFormatProperties> format_properties;
  193. /// Nsight Aftermath GPU crash tracker
  194. NsightAftermathTracker nsight_aftermath_tracker;
  195. };
  196. } // namespace Vulkan