vk_swapchain.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <vector>
  6. #include "common/common_types.h"
  7. #include "video_core/vulkan_common/vulkan_wrapper.h"
  8. namespace Layout {
  9. struct FramebufferLayout;
  10. }
  11. namespace Vulkan {
  12. class Device;
  13. class VKScheduler;
  14. class VKSwapchain {
  15. public:
  16. explicit VKSwapchain(VkSurfaceKHR surface, const Device& device, VKScheduler& scheduler);
  17. ~VKSwapchain();
  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. Returns true when the swapchains had to be
  23. /// recreated. Takes responsability for the ownership of fence.
  24. bool Present(VkSemaphore render_semaphore);
  25. /// Returns true when the framebuffer layout has changed.
  26. bool HasFramebufferChanged(const Layout::FramebufferLayout& framebuffer) const;
  27. VkExtent2D GetSize() const {
  28. return extent;
  29. }
  30. std::size_t GetImageCount() const {
  31. return image_count;
  32. }
  33. std::size_t GetImageIndex() const {
  34. return image_index;
  35. }
  36. VkImage GetImageIndex(std::size_t index) const {
  37. return images[index];
  38. }
  39. VkImageView GetImageViewIndex(std::size_t index) const {
  40. return *image_views[index];
  41. }
  42. VkFormat GetImageFormat() const {
  43. return image_format;
  44. }
  45. bool GetSrgbState() const {
  46. return current_srgb;
  47. }
  48. private:
  49. void CreateSwapchain(const VkSurfaceCapabilitiesKHR& capabilities, u32 width, u32 height,
  50. bool srgb);
  51. void CreateSemaphores();
  52. void CreateImageViews();
  53. void Destroy();
  54. const VkSurfaceKHR surface;
  55. const Device& device;
  56. VKScheduler& scheduler;
  57. vk::SwapchainKHR swapchain;
  58. std::size_t image_count{};
  59. std::vector<VkImage> images;
  60. std::vector<vk::ImageView> image_views;
  61. std::vector<vk::Framebuffer> framebuffers;
  62. std::vector<u64> resource_ticks;
  63. std::vector<vk::Semaphore> present_semaphores;
  64. u32 image_index{};
  65. u32 frame_index{};
  66. VkFormat image_format{};
  67. VkExtent2D extent{};
  68. u32 current_width{};
  69. u32 current_height{};
  70. bool current_srgb{};
  71. };
  72. } // namespace Vulkan