vk_device.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/wrapper.h"
  11. namespace Vulkan {
  12. /// Format usage descriptor.
  13. enum class FormatType { Linear, Optimal, Buffer };
  14. /// Subgroup size of the guest emulated hardware (Nvidia has 32 threads per subgroup).
  15. const u32 GuestWarpSize = 32;
  16. /// Handles data specific to a physical device.
  17. class VKDevice final {
  18. public:
  19. explicit VKDevice(VkInstance instance, vk::PhysicalDevice physical, VkSurfaceKHR surface,
  20. const vk::InstanceDispatch& dld);
  21. ~VKDevice();
  22. /// Initializes the device. Returns true on success.
  23. bool Create();
  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. /// Returns the dispatch loader with direct function pointers of the device.
  36. const vk::DeviceDispatch& GetDispatchLoader() const {
  37. return dld;
  38. }
  39. /// Returns the logical device.
  40. const vk::Device& GetLogical() const {
  41. return logical;
  42. }
  43. /// Returns the physical device.
  44. vk::PhysicalDevice GetPhysical() const {
  45. return physical;
  46. }
  47. /// Returns the main graphics queue.
  48. vk::Queue GetGraphicsQueue() const {
  49. return graphics_queue;
  50. }
  51. /// Returns the main present queue.
  52. vk::Queue GetPresentQueue() const {
  53. return present_queue;
  54. }
  55. /// Returns main graphics queue family index.
  56. u32 GetGraphicsFamily() const {
  57. return graphics_family;
  58. }
  59. /// Returns main present queue family index.
  60. u32 GetPresentFamily() const {
  61. return present_family;
  62. }
  63. /// Returns true if the device is integrated with the host CPU.
  64. bool IsIntegrated() const {
  65. return properties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU;
  66. }
  67. /// Returns the current Vulkan API version provided in Vulkan-formatted version numbers.
  68. u32 GetApiVersion() 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 true if ASTC is natively supported.
  100. bool IsOptimalAstcSupported() const {
  101. return is_optimal_astc_supported;
  102. }
  103. /// Returns true if the device supports float16 natively
  104. bool IsFloat16Supported() const {
  105. return is_float16_supported;
  106. }
  107. /// Returns true if the device warp size can potentially be bigger than guest's warp size.
  108. bool IsWarpSizePotentiallyBiggerThanGuest() const {
  109. return is_warp_potentially_bigger;
  110. }
  111. /// Returns true if the device can be forced to use the guest warp size.
  112. bool IsGuestWarpSizeSupported(VkShaderStageFlagBits stage) const {
  113. return guest_warp_stages & stage;
  114. }
  115. /// Returns true if formatless image load is supported.
  116. bool IsFormatlessImageLoadSupported() const {
  117. return is_formatless_image_load_supported;
  118. }
  119. /// Returns true if the device supports VK_EXT_scalar_block_layout.
  120. bool IsKhrUniformBufferStandardLayoutSupported() const {
  121. return khr_uniform_buffer_standard_layout;
  122. }
  123. /// Returns true if the device supports VK_EXT_index_type_uint8.
  124. bool IsExtIndexTypeUint8Supported() const {
  125. return ext_index_type_uint8;
  126. }
  127. /// Returns true if the device supports VK_EXT_depth_range_unrestricted.
  128. bool IsExtDepthRangeUnrestrictedSupported() const {
  129. return ext_depth_range_unrestricted;
  130. }
  131. /// Returns true if the device supports VK_EXT_shader_viewport_index_layer.
  132. bool IsExtShaderViewportIndexLayerSupported() const {
  133. return ext_shader_viewport_index_layer;
  134. }
  135. /// Returns true if the device supports VK_EXT_transform_feedback.
  136. bool IsExtTransformFeedbackSupported() const {
  137. return ext_transform_feedback;
  138. }
  139. /// Returns true if the device supports VK_NV_device_diagnostic_checkpoints.
  140. bool IsNvDeviceDiagnosticCheckpoints() const {
  141. return nv_device_diagnostic_checkpoints;
  142. }
  143. /// Returns the vendor name reported from Vulkan.
  144. std::string_view GetVendorName() const {
  145. return vendor_name;
  146. }
  147. /// Returns the list of available extensions.
  148. const std::vector<std::string>& GetAvailableExtensions() const {
  149. return reported_extensions;
  150. }
  151. /// Checks if the physical device is suitable.
  152. static bool IsSuitable(vk::PhysicalDevice physical, VkSurfaceKHR surface);
  153. private:
  154. /// Loads extensions into a vector and stores available ones in this object.
  155. std::vector<const char*> LoadExtensions();
  156. /// Sets up queue families.
  157. void SetupFamilies(VkSurfaceKHR surface);
  158. /// Sets up device features.
  159. void SetupFeatures();
  160. /// Collects telemetry information from the device.
  161. void CollectTelemetryParameters();
  162. /// Returns a list of queue initialization descriptors.
  163. std::vector<VkDeviceQueueCreateInfo> GetDeviceQueueCreateInfos() const;
  164. /// Returns true if ASTC textures are natively supported.
  165. bool IsOptimalAstcSupported(const VkPhysicalDeviceFeatures& features) const;
  166. /// Returns true if a format is supported.
  167. bool IsFormatSupported(VkFormat wanted_format, VkFormatFeatureFlags wanted_usage,
  168. FormatType format_type) const;
  169. vk::DeviceDispatch dld; ///< Device function pointers.
  170. vk::PhysicalDevice physical; ///< Physical device.
  171. VkPhysicalDeviceProperties properties; ///< Device properties.
  172. vk::Device logical; ///< Logical device.
  173. vk::Queue graphics_queue; ///< Main graphics queue.
  174. vk::Queue present_queue; ///< Main present queue.
  175. u32 graphics_family{}; ///< Main graphics queue family index.
  176. u32 present_family{}; ///< Main present queue family index.
  177. VkDriverIdKHR driver_id{}; ///< Driver ID.
  178. VkShaderStageFlags guest_warp_stages{}; ///< Stages where the guest warp size can be forced.ed
  179. bool is_optimal_astc_supported{}; ///< Support for native ASTC.
  180. bool is_float16_supported{}; ///< Support for float16 arithmetics.
  181. bool is_warp_potentially_bigger{}; ///< Host warp size can be bigger than guest.
  182. bool is_formatless_image_load_supported{}; ///< Support for shader image read without format.
  183. bool khr_uniform_buffer_standard_layout{}; ///< Support for std430 on UBOs.
  184. bool ext_index_type_uint8{}; ///< Support for VK_EXT_index_type_uint8.
  185. bool ext_depth_range_unrestricted{}; ///< Support for VK_EXT_depth_range_unrestricted.
  186. bool ext_shader_viewport_index_layer{}; ///< Support for VK_EXT_shader_viewport_index_layer.
  187. bool ext_transform_feedback{}; ///< Support for VK_EXT_transform_feedback.
  188. bool nv_device_diagnostic_checkpoints{}; ///< Support for VK_NV_device_diagnostic_checkpoints.
  189. // Telemetry parameters
  190. std::string vendor_name; ///< Device's driver name.
  191. std::vector<std::string> reported_extensions; ///< Reported Vulkan extensions.
  192. /// Format properties dictionary.
  193. std::unordered_map<VkFormat, VkFormatProperties> format_properties;
  194. };
  195. } // namespace Vulkan