vk_swapchain.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/renderer_vulkan/wrapper.h"
  8. namespace Layout {
  9. struct FramebufferLayout;
  10. }
  11. namespace Vulkan {
  12. class VKDevice;
  13. class VKFence;
  14. class VKSwapchain {
  15. public:
  16. explicit VKSwapchain(VkSurfaceKHR surface, const VKDevice& device);
  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, VKFence& fence);
  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 VKDevice& device;
  56. vk::SwapchainKHR swapchain;
  57. std::size_t image_count{};
  58. std::vector<VkImage> images;
  59. std::vector<vk::ImageView> image_views;
  60. std::vector<vk::Framebuffer> framebuffers;
  61. std::vector<VKFence*> fences;
  62. std::vector<vk::Semaphore> present_semaphores;
  63. u32 image_index{};
  64. u32 frame_index{};
  65. VkFormat image_format{};
  66. VkExtent2D extent{};
  67. u32 current_width{};
  68. u32 current_height{};
  69. bool current_srgb{};
  70. };
  71. } // namespace Vulkan