vk_swapchain.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. void 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. VkExtent2D GetSize() const {
  41. return extent;
  42. }
  43. std::size_t GetImageCount() const {
  44. return image_count;
  45. }
  46. std::size_t GetImageIndex() const {
  47. return image_index;
  48. }
  49. VkImage GetImageIndex(std::size_t index) const {
  50. return images[index];
  51. }
  52. VkImageView GetImageViewIndex(std::size_t index) const {
  53. return *image_views[index];
  54. }
  55. VkFormat GetImageViewFormat() const {
  56. return image_view_format;
  57. }
  58. VkSemaphore CurrentPresentSemaphore() const {
  59. return *present_semaphores[frame_index];
  60. }
  61. private:
  62. void CreateSwapchain(const VkSurfaceCapabilitiesKHR& capabilities, u32 width, u32 height,
  63. bool srgb);
  64. void CreateSemaphores();
  65. void CreateImageViews();
  66. void Destroy();
  67. bool HasFpsUnlockChanged() const;
  68. bool NeedsPresentModeUpdate() const;
  69. const VkSurfaceKHR surface;
  70. const Device& device;
  71. Scheduler& scheduler;
  72. vk::SwapchainKHR swapchain;
  73. std::size_t image_count{};
  74. std::vector<VkImage> images;
  75. std::vector<vk::ImageView> image_views;
  76. std::vector<vk::Framebuffer> framebuffers;
  77. std::vector<u64> resource_ticks;
  78. std::vector<vk::Semaphore> present_semaphores;
  79. u32 image_index{};
  80. u32 frame_index{};
  81. VkFormat image_view_format{};
  82. VkExtent2D extent{};
  83. VkPresentModeKHR present_mode{};
  84. bool current_srgb{};
  85. bool current_fps_unlocked{};
  86. bool is_outdated{};
  87. bool is_suboptimal{};
  88. };
  89. } // namespace Vulkan