vk_device.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /// Checks if the physical device is suitable.
  66. static bool IsSuitable(const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDevice physical,
  67. vk::SurfaceKHR surface);
  68. private:
  69. /// Sets up queue families.
  70. void SetupFamilies(const vk::DispatchLoaderDynamic& dldi, vk::SurfaceKHR surface);
  71. /// Sets up device properties.
  72. void SetupProperties(const vk::DispatchLoaderDynamic& dldi);
  73. /// Returns a list of queue initialization descriptors.
  74. std::vector<vk::DeviceQueueCreateInfo> GetDeviceQueueCreateInfos() const;
  75. /// Returns true if a format is supported.
  76. bool IsFormatSupported(vk::Format wanted_format, vk::FormatFeatureFlags wanted_usage,
  77. FormatType format_type) const;
  78. /// Returns the device properties for Vulkan formats.
  79. static std::map<vk::Format, vk::FormatProperties> GetFormatProperties(
  80. const vk::DispatchLoaderDynamic& dldi, vk::PhysicalDevice physical);
  81. const vk::PhysicalDevice physical; ///< Physical device
  82. vk::DispatchLoaderDynamic dld; ///< Device function pointers
  83. UniqueDevice logical; ///< Logical device
  84. vk::Queue graphics_queue; ///< Main graphics queue
  85. vk::Queue present_queue; ///< Main present queue
  86. u32 graphics_family{}; ///< Main graphics queue family index
  87. u32 present_family{}; ///< Main present queue family index
  88. vk::PhysicalDeviceType device_type; ///< Physical device type
  89. u64 uniform_buffer_alignment{}; ///< Uniform buffer alignment requeriment
  90. std::map<vk::Format, vk::FormatProperties> format_properties; ///< Format properties dictionary
  91. };
  92. } // namespace Vulkan