Mandrill 2025.6.0
Loading...
Searching...
No Matches
Swapchain.h
1#pragma once
2
3#include "Common.h"
4
5#include "Buffer.h"
6#include "Device.h"
7#include "Error.h"
8#include "Image.h"
9
10namespace Mandrill
11{
16 {
17 public:
18 MANDRILL_NON_COPYABLE(Swapchain)
19
20
25 MANDRILL_API Swapchain(ptr<Device> pDevice, uint32_t framesInFlight = 2);
26
30 MANDRILL_API ~Swapchain();
31
35 MANDRILL_API void recreate();
36
41 MANDRILL_API VkCommandBuffer acquireNextImage();
42
48 MANDRILL_API void present(VkCommandBuffer cmd, ptr<Image> pImage);
49
54 MANDRILL_API void requestScreenshot();
55
61 MANDRILL_API std::vector<uint8_t> waitForScreenshot();
62
67 MANDRILL_API VkSwapchainKHR getSwapchain() const
68 {
69 return mSwapchain;
70 }
71
76 MANDRILL_API VkImage getImage() const
77 {
78 return mImages[mImageIndex];
79 }
80
85 MANDRILL_API const std::vector<VkImage>& getImages() const
86 {
87 return mImages;
88 }
89
94 MANDRILL_API VkImageView getImageView() const
95 {
96 return mImageViews[mImageIndex];
97 }
98
103 MANDRILL_API const std::vector<VkImageView>& getImageViews() const
104 {
105 return mImageViews;
106 }
107
113 MANDRILL_API VkDescriptorSet getImageDescriptorSet() const
114 {
115 return mDescriptorSets[mImageIndex];
116 }
117
122 MANDRILL_API VkFormat getImageFormat() const
123 {
124 return mImageFormat;
125 }
126
131 MANDRILL_API VkExtent2D getExtent() const
132 {
133 return mExtent;
134 }
135
140 MANDRILL_API uint32_t getImageIndex() const
141 {
142 return mImageIndex;
143 }
144
149 MANDRILL_API uint32_t getInFlightIndex() const
150 {
151 return mInFlightIndex;
152 }
153
159 MANDRILL_API uint32_t getPreviousInFlightIndex() const
160 {
161 return mPreviousInFlightIndex;
162 }
163
168 MANDRILL_API uint32_t getFramesInFlightCount() const
169 {
170 return static_cast<uint32_t>(mInFlightFences.size());
171 }
172
177 MANDRILL_API bool recreated() const
178 {
179 return mRecreated;
180 }
181
182 private:
183 void querySupport();
184 void createSwapchain();
185 void destroySwapchain();
186 void createSyncObjects(uint32_t framesInFlight);
187 void destroySyncObjects();
188 void createDescriptor();
189 void destroyDescriptor();
190
191 ptr<Device> mpDevice;
192
193 VkSwapchainKHR mSwapchain;
194 VkFormat mImageFormat;
195 VkExtent2D mExtent;
196
197 std::vector<VkImage> mImages;
198 std::vector<VkImageView> mImageViews;
199
200 struct {
201 VkSurfaceCapabilitiesKHR capabilities{};
202 std::vector<VkSurfaceFormatKHR> formats;
203 std::vector<VkPresentModeKHR> presentModes;
204 } mSupportDetails;
205
206 std::vector<VkCommandBuffer> mCommandBuffers;
207
208 std::vector<VkSemaphore> mRenderFinishedSemaphores;
209 std::vector<VkSemaphore> mPresentFinishedSemaphores;
210 std::vector<VkFence> mInFlightFences;
211
212 uint32_t mInFlightIndex = 0;
213 uint32_t mPreviousInFlightIndex = 0;
214 uint32_t mImageIndex = 0;
215
216 VkDescriptorSetLayout mDescriptorSetLayout;
217 VkDescriptorPool mDescriptorPool;
218 std::vector<VkDescriptorSet> mDescriptorSets;
219
220 bool mRecreated = false;
221
222 enum class ScreenshotState {
223 Idle,
224 Requested,
225 QueuedForBlitting,
226 BlittedToStage,
227 CopiedToHost,
228 };
229
230 ScreenshotState mScreenshotState = ScreenshotState::Idle;
231 ptr<Image> mScreenshotStageImage;
232 std::mutex mScreenshotMutex;
233 std::condition_variable mScreenshotAvailableCV;
234 uint32_t mScreenshotInFlightIndex;
235 };
236} // namespace Mandrill
Swapchain class for managing the swapchain and its images.
Definition Swapchain.h:16
MANDRILL_API VkImageView getImageView() const
Get the current swapchain image view.
Definition Swapchain.h:94
MANDRILL_API VkDescriptorSet getImageDescriptorSet() const
Get the descriptor set of the current swapchain image. Useful if using the swapchain image as a stora...
Definition Swapchain.h:113
MANDRILL_API void present(VkCommandBuffer cmd, ptr< Image > pImage)
Present a rendered image to the swapchain image.
Definition Swapchain.cpp:163
MANDRILL_API void requestScreenshot()
Request a screenshot from the next rendered frame. This call must be paired with a call to waitForScr...
Definition Swapchain.cpp:263
MANDRILL_API VkExtent2D getExtent() const
Get the extent (resolution) of the swapchain.
Definition Swapchain.h:131
MANDRILL_API void recreate()
Recreate the swapchain, for instance if window size changed.
Definition Swapchain.cpp:92
MANDRILL_API VkSwapchainKHR getSwapchain() const
Get the swapchain handle.
Definition Swapchain.h:67
MANDRILL_API std::vector< uint8_t > waitForScreenshot()
Wait for screenshot to be available and acquire it. Only call this after a call to requestScreenshot(...
Definition Swapchain.cpp:270
MANDRILL_API bool recreated() const
Check if the swapchain was recreated during the last frame. This is reset in present().
Definition Swapchain.h:177
MANDRILL_API ~Swapchain()
Destructor for swapchain.
Definition Swapchain.cpp:85
MANDRILL_API const std::vector< VkImageView > & getImageViews() const
Get all the swapchain image views.
Definition Swapchain.h:103
MANDRILL_API const std::vector< VkImage > & getImages() const
Get all the swapchain images.
Definition Swapchain.h:85
MANDRILL_API uint32_t getPreviousInFlightIndex() const
Get the index of the previous frame that was in flight. This is useful when indexing after calling pr...
Definition Swapchain.h:159
MANDRILL_API VkImage getImage() const
Get the current swapchain image.
Definition Swapchain.h:76
MANDRILL_API uint32_t getInFlightIndex() const
Get the current frame in flight index.
Definition Swapchain.h:149
MANDRILL_API VkFormat getImageFormat() const
Get the image format of the swapchain.
Definition Swapchain.h:122
MANDRILL_API VkCommandBuffer acquireNextImage()
Acquire the next image in the swapchain to render to.
Definition Swapchain.cpp:116
MANDRILL_API uint32_t getImageIndex() const
Get the current image index.
Definition Swapchain.h:140
MANDRILL_API uint32_t getFramesInFlightCount() const
Get the count of the total number of frames in flight.
Definition Swapchain.h:168