vk_device.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 <map>
  6. #include <vector>
  7. #include "common/common_types.h"
  8. #include "video_core/renderer_vulkan/declarations.h"
  9. namespace Vulkan {
  10. /// Format usage descriptor.
  11. enum class FormatType { Linear, Optimal, Buffer };
  12. /// Handles data specific to a physical device.
  13. class VKDevice final {
  14. public:
  15. explicit VKDevice(const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDevice physical,
  16. vk::SurfaceKHR surface);
  17. ~VKDevice();
  18. /// Initializes the device. Returns true on success.
  19. bool Create(const vk::DispatchLoaderDynamic& dldi, vk::Instance instance);
  20. /**
  21. * Returns a format supported by the device for the passed requeriments.
  22. * @param wanted_format The ideal format to be returned. It may not be the returned format.
  23. * @param wanted_usage The usage that must be fulfilled even if the format is not supported.
  24. * @param format_type Format type usage.
  25. * @returns A format supported by the device.
  26. */
  27. vk::Format GetSupportedFormat(vk::Format wanted_format, vk::FormatFeatureFlags wanted_usage,
  28. FormatType format_type) const;
  29. /// Returns the dispatch loader with direct function pointers of the device.
  30. const vk::DispatchLoaderDynamic& GetDispatchLoader() const {
  31. return dld;
  32. }
  33. /// Returns the logical device.
  34. vk::Device GetLogical() const {
  35. return logical.get();
  36. }
  37. /// Returns the physical device.
  38. vk::PhysicalDevice GetPhysical() const {
  39. return physical;
  40. }
  41. /// Returns the main graphics queue.
  42. vk::Queue GetGraphicsQueue() const {
  43. return graphics_queue;
  44. }
  45. /// Returns the main present queue.
  46. vk::Queue GetPresentQueue() const {
  47. return present_queue;
  48. }
  49. /// Returns main graphics queue family index.
  50. u32 GetGraphicsFamily() const {
  51. return graphics_family;
  52. }
  53. /// Returns main present queue family index.
  54. u32 GetPresentFamily() const {
  55. return present_family;
  56. }
  57. /// Returns if the device is integrated with the host CPU.
  58. bool IsIntegrated() const {
  59. return device_type == vk::PhysicalDeviceType::eIntegratedGpu;
  60. }
  61. /// Returns uniform buffer alignment requeriment.
  62. u64 GetUniformBufferAlignment() const {
  63. return uniform_buffer_alignment;
  64. }
  65. /// Returns the maximum range for storage buffers.
  66. u64 GetMaxStorageBufferRange() const {
  67. return max_storage_buffer_range;
  68. }
  69. /// Returns true if ASTC is natively supported.
  70. bool IsOptimalAstcSupported() const {
  71. return is_optimal_astc_supported;
  72. }
  73. /// Returns true if the device supports VK_EXT_scalar_block_layout.
  74. bool IsExtScalarBlockLayoutSupported() const {
  75. return ext_scalar_block_layout;
  76. }
  77. /// Checks if the physical device is suitable.
  78. static bool IsSuitable(const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDevice physical,
  79. vk::SurfaceKHR surface);
  80. private:
  81. /// Loads extensions into a vector and stores available ones in this object.
  82. std::vector<const char*> LoadExtensions(const vk::DispatchLoaderDynamic& dldi);
  83. /// Sets up queue families.
  84. void SetupFamilies(const vk::DispatchLoaderDynamic& dldi, vk::SurfaceKHR surface);
  85. /// Sets up device properties.
  86. void SetupProperties(const vk::DispatchLoaderDynamic& dldi);
  87. /// Sets up device features.
  88. void SetupFeatures(const vk::DispatchLoaderDynamic& dldi);
  89. /// Returns a list of queue initialization descriptors.
  90. std::vector<vk::DeviceQueueCreateInfo> GetDeviceQueueCreateInfos() const;
  91. /// Returns true if ASTC textures are natively supported.
  92. bool IsOptimalAstcSupported(const vk::PhysicalDeviceFeatures& features,
  93. const vk::DispatchLoaderDynamic& dldi) const;
  94. /// Returns true if a format is supported.
  95. bool IsFormatSupported(vk::Format wanted_format, vk::FormatFeatureFlags wanted_usage,
  96. FormatType format_type) const;
  97. /// Returns the device properties for Vulkan formats.
  98. static std::map<vk::Format, vk::FormatProperties> GetFormatProperties(
  99. const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDevice physical);
  100. const vk::PhysicalDevice physical; ///< Physical device.
  101. vk::DispatchLoaderDynamic dld; ///< Device function pointers.
  102. UniqueDevice logical; ///< Logical device.
  103. vk::Queue graphics_queue; ///< Main graphics queue.
  104. vk::Queue present_queue; ///< Main present queue.
  105. u32 graphics_family{}; ///< Main graphics queue family index.
  106. u32 present_family{}; ///< Main present queue family index.
  107. vk::PhysicalDeviceType device_type; ///< Physical device type.
  108. u64 uniform_buffer_alignment{}; ///< Uniform buffer alignment requeriment.
  109. u64 max_storage_buffer_range{}; ///< Max storage buffer size.
  110. bool is_optimal_astc_supported{}; ///< Support for native ASTC.
  111. bool ext_scalar_block_layout{}; ///< Support for VK_EXT_scalar_block_layout.
  112. std::map<vk::Format, vk::FormatProperties> format_properties; ///< Format properties dictionary.
  113. };
  114. } // namespace Vulkan