vk_device.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/declarations.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(const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDevice physical,
  20. vk::SurfaceKHR surface);
  21. ~VKDevice();
  22. /// Initializes the device. Returns true on success.
  23. bool Create(const vk::DispatchLoaderDynamic& dldi, vk::Instance instance);
  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. vk::Format GetSupportedFormat(vk::Format wanted_format, vk::FormatFeatureFlags 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::DispatchLoaderDynamic& GetDispatchLoader() const {
  37. return dld;
  38. }
  39. /// Returns the logical device.
  40. vk::Device GetLogical() const {
  41. return logical.get();
  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::PhysicalDeviceType::eIntegratedGpu;
  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. vk::DriverIdKHR GetDriverID() const {
  81. return driver_id;
  82. }
  83. /// Returns uniform buffer alignment requeriment.
  84. vk::DeviceSize GetUniformBufferAlignment() const {
  85. return properties.limits.minUniformBufferOffsetAlignment;
  86. }
  87. /// Returns storage alignment requeriment.
  88. vk::DeviceSize GetStorageBufferAlignment() const {
  89. return properties.limits.minStorageBufferOffsetAlignment;
  90. }
  91. /// Returns the maximum range for storage buffers.
  92. vk::DeviceSize GetMaxStorageBufferRange() const {
  93. return properties.limits.maxStorageBufferRange;
  94. }
  95. /// Returns the maximum size for push constants.
  96. vk::DeviceSize 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(vk::ShaderStageFlagBits stage) const {
  113. return (guest_warp_stages & stage) != vk::ShaderStageFlags{};
  114. }
  115. /// Returns true if the device supports VK_EXT_scalar_block_layout.
  116. bool IsKhrUniformBufferStandardLayoutSupported() const {
  117. return khr_uniform_buffer_standard_layout;
  118. }
  119. /// Returns true if the device supports VK_EXT_index_type_uint8.
  120. bool IsExtIndexTypeUint8Supported() const {
  121. return ext_index_type_uint8;
  122. }
  123. /// Returns true if the device supports VK_EXT_depth_range_unrestricted.
  124. bool IsExtDepthRangeUnrestrictedSupported() const {
  125. return ext_depth_range_unrestricted;
  126. }
  127. /// Returns true if the device supports VK_EXT_shader_viewport_index_layer.
  128. bool IsExtShaderViewportIndexLayerSupported() const {
  129. return ext_shader_viewport_index_layer;
  130. }
  131. /// Returns true if the device supports VK_NV_device_diagnostic_checkpoints.
  132. bool IsNvDeviceDiagnosticCheckpoints() const {
  133. return nv_device_diagnostic_checkpoints;
  134. }
  135. /// Returns the vendor name reported from Vulkan.
  136. std::string_view GetVendorName() const {
  137. return vendor_name;
  138. }
  139. /// Returns the list of available extensions.
  140. const std::vector<std::string>& GetAvailableExtensions() const {
  141. return reported_extensions;
  142. }
  143. /// Checks if the physical device is suitable.
  144. static bool IsSuitable(const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDevice physical,
  145. vk::SurfaceKHR surface);
  146. private:
  147. /// Loads extensions into a vector and stores available ones in this object.
  148. std::vector<const char*> LoadExtensions(const vk::DispatchLoaderDynamic& dldi);
  149. /// Sets up queue families.
  150. void SetupFamilies(const vk::DispatchLoaderDynamic& dldi, vk::SurfaceKHR surface);
  151. /// Sets up device features.
  152. void SetupFeatures(const vk::DispatchLoaderDynamic& dldi);
  153. /// Collects telemetry information from the device.
  154. void CollectTelemetryParameters();
  155. /// Returns a list of queue initialization descriptors.
  156. std::vector<vk::DeviceQueueCreateInfo> GetDeviceQueueCreateInfos() const;
  157. /// Returns true if ASTC textures are natively supported.
  158. bool IsOptimalAstcSupported(const vk::PhysicalDeviceFeatures& features,
  159. const vk::DispatchLoaderDynamic& dldi) const;
  160. /// Returns true if a format is supported.
  161. bool IsFormatSupported(vk::Format wanted_format, vk::FormatFeatureFlags wanted_usage,
  162. FormatType format_type) const;
  163. /// Returns the device properties for Vulkan formats.
  164. static std::unordered_map<vk::Format, vk::FormatProperties> GetFormatProperties(
  165. const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDevice physical);
  166. const vk::PhysicalDevice physical; ///< Physical device.
  167. vk::DispatchLoaderDynamic dld; ///< Device function pointers.
  168. vk::PhysicalDeviceProperties properties; ///< Device properties.
  169. UniqueDevice logical; ///< Logical device.
  170. vk::Queue graphics_queue; ///< Main graphics queue.
  171. vk::Queue present_queue; ///< Main present queue.
  172. u32 graphics_family{}; ///< Main graphics queue family index.
  173. u32 present_family{}; ///< Main present queue family index.
  174. vk::DriverIdKHR driver_id{}; ///< Driver ID.
  175. vk::ShaderStageFlags guest_warp_stages{}; ///< Stages where the guest warp size can be forced.
  176. bool is_optimal_astc_supported{}; ///< Support for native ASTC.
  177. bool is_float16_supported{}; ///< Support for float16 arithmetics.
  178. bool is_warp_potentially_bigger{}; ///< Host warp size can be bigger than guest.
  179. bool khr_uniform_buffer_standard_layout{}; ///< Support for std430 on UBOs.
  180. bool ext_index_type_uint8{}; ///< Support for VK_EXT_index_type_uint8.
  181. bool ext_depth_range_unrestricted{}; ///< Support for VK_EXT_depth_range_unrestricted.
  182. bool ext_shader_viewport_index_layer{}; ///< Support for VK_EXT_shader_viewport_index_layer.
  183. bool nv_device_diagnostic_checkpoints{}; ///< Support for VK_NV_device_diagnostic_checkpoints.
  184. // Telemetry parameters
  185. std::string vendor_name; ///< Device's driver name.
  186. std::vector<std::string> reported_extensions; ///< Reported Vulkan extensions.
  187. /// Format properties dictionary.
  188. std::unordered_map<vk::Format, vk::FormatProperties> format_properties;
  189. };
  190. } // namespace Vulkan