vk_swapchain.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <vector>
  5. #include "common/common_types.h"
  6. #include "video_core/vulkan_common/vulkan_wrapper.h"
  7. namespace Layout {
  8. struct FramebufferLayout;
  9. }
  10. namespace Vulkan {
  11. class Device;
  12. class Scheduler;
  13. class Swapchain {
  14. public:
  15. explicit Swapchain(VkSurfaceKHR surface, const Device& device, Scheduler& scheduler, u32 width,
  16. u32 height, bool srgb);
  17. ~Swapchain();
  18. /// Creates (or recreates) the swapchain with a given size.
  19. void Create(u32 width, u32 height, bool srgb);
  20. /// Acquires the next image in the swapchain, waits as needed.
  21. bool AcquireNextImage();
  22. /// Presents the rendered image to the swapchain.
  23. void Present(VkSemaphore render_semaphore);
  24. /// Returns true when the swapchain needs to be recreated.
  25. bool NeedsRecreation(bool is_srgb) const {
  26. return HasColorSpaceChanged(is_srgb) || IsSubOptimal() || NeedsPresentModeUpdate();
  27. }
  28. /// Returns true when the color space has changed.
  29. bool HasColorSpaceChanged(bool is_srgb) const {
  30. return current_srgb != is_srgb;
  31. }
  32. /// Returns true when the swapchain is outdated.
  33. bool IsOutDated() const {
  34. return is_outdated;
  35. }
  36. /// Returns true when the swapchain is suboptimal.
  37. bool IsSubOptimal() const {
  38. return is_suboptimal;
  39. }
  40. /// Returns true when the swapchain format is in the srgb color space
  41. bool IsSrgb() const {
  42. return current_srgb;
  43. }
  44. VkExtent2D GetSize() const {
  45. return extent;
  46. }
  47. std::size_t GetImageCount() const {
  48. return image_count;
  49. }
  50. std::size_t GetImageIndex() const {
  51. return image_index;
  52. }
  53. std::size_t GetFrameIndex() const {
  54. return frame_index;
  55. }
  56. VkImage GetImageIndex(std::size_t index) const {
  57. return images[index];
  58. }
  59. VkImage CurrentImage() const {
  60. return images[image_index];
  61. }
  62. VkFormat GetImageViewFormat() const {
  63. return image_view_format;
  64. }
  65. VkFormat GetImageFormat() const {
  66. return surface_format.format;
  67. }
  68. VkSemaphore CurrentPresentSemaphore() const {
  69. return *present_semaphores[frame_index];
  70. }
  71. VkSemaphore CurrentRenderSemaphore() const {
  72. return *render_semaphores[frame_index];
  73. }
  74. u32 GetWidth() const {
  75. return width;
  76. }
  77. u32 GetHeight() const {
  78. return height;
  79. }
  80. VkExtent2D GetExtent() const {
  81. return extent;
  82. }
  83. private:
  84. void CreateSwapchain(const VkSurfaceCapabilitiesKHR& capabilities, bool srgb);
  85. void CreateSemaphores();
  86. void CreateImageViews();
  87. void Destroy();
  88. bool HasFpsUnlockChanged() const;
  89. bool NeedsPresentModeUpdate() const;
  90. const VkSurfaceKHR surface;
  91. const Device& device;
  92. Scheduler& scheduler;
  93. vk::SwapchainKHR swapchain;
  94. std::size_t image_count{};
  95. std::vector<VkImage> images;
  96. std::vector<u64> resource_ticks;
  97. std::vector<vk::Semaphore> present_semaphores;
  98. std::vector<vk::Semaphore> render_semaphores;
  99. u32 width;
  100. u32 height;
  101. u32 image_index{};
  102. u32 frame_index{};
  103. VkFormat image_view_format{};
  104. VkExtent2D extent{};
  105. VkPresentModeKHR present_mode{};
  106. VkSurfaceFormatKHR surface_format{};
  107. bool current_srgb{};
  108. bool current_fps_unlocked{};
  109. bool is_outdated{};
  110. bool is_suboptimal{};
  111. };
  112. } // namespace Vulkan