Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
SwapChain.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_RENDERER_BACKEND_VULKAN_SWAPCHAIN
19#define INCLUDED_RENDERER_BACKEND_VULKAN_SWAPCHAIN
20
24
25#include <cstddef>
26#include <cstdint>
27#include <glad/vulkan.h>
28#include <limits>
29#include <memory>
30#include <tuple>
31#include <unordered_map>
32#include <vector>
33
34namespace Renderer::Backend::Vulkan { class CDevice; }
35namespace Renderer::Backend::Vulkan { class CFramebuffer; }
36namespace Renderer::Backend::Vulkan { class CRingCommandContext; }
37namespace Renderer::Backend::Vulkan { class CTexture; }
38
39namespace Renderer
40{
41
42namespace Backend
43{
44
45namespace Vulkan
46{
47
48class CSwapChain final : public ISwapChain
49{
50public:
51 ~CSwapChain() override;
52
53 IDevice* GetDevice() override;
54
55 bool IsValid() const override { return m_IsValid; }
56
57 bool AcquireNextBackbuffer() override;
58
60 const AttachmentLoadOp colorAttachmentLoadOp,
61 const AttachmentStoreOp colorAttachmentStoreOp,
62 const AttachmentLoadOp depthStencilAttachmentLoadOp,
63 const AttachmentStoreOp depthStencilAttachmentStoreOp) override;
64
65 void Present() override;
66
67 VkSwapchainKHR GetVkSwapchain() { return m_SwapChain; }
68
69 bool AcquireNextImage();
71 CRingCommandContext& commandContext);
73 CRingCommandContext& commandContext);
74 void Present(VkSemaphore submitDone, VkQueue queue);
75
77
79
81
82private:
83 friend class CDevice;
84
85 static std::unique_ptr<CSwapChain> Create(
86 CDevice* device, CSubmitScheduler* submitScheduler,
87 const uint32_t queueFamilyIndex, VkQueue queue,
88 const char* name, VkSurfaceKHR surface,
89 int surfaceDrawableWidth, int surfaceDrawableHeight,
90 const bool vsync, std::unique_ptr<ISwapChain> oldSwapChain);
91
93
94 CDevice* m_Device = nullptr;
96 VkQueue m_Queue{VK_NULL_HANDLE};
97
98 bool m_IsValid = false;
99 VkSwapchainKHR m_SwapChain = VK_NULL_HANDLE;
100
101 uint32_t m_CurrentImageIndex = std::numeric_limits<uint32_t>::max();
102
103 std::vector<VkImage> m_Images;
104 std::vector<std::unique_ptr<CTexture>> m_Textures;
105 std::unique_ptr<CTexture> m_DepthTexture;
106 VkFormat m_ImageFormat = VK_FORMAT_UNDEFINED;
107
108 uint32_t m_FrameID{0};
109
110 // We can't reuse the same acquire semaphore immediately after present
111 // because it might still be processing on GPU as vkQueuePresentKHR doesn't
112 // have to be blocking.
113 // We need to wait for the image on GPU to draw to it.
115 {
116 VkSemaphore acquireImageSemaphore{VK_NULL_HANDLE};
117 // We need to know when we can reuse the semaphore.
119 };
120 std::vector<FrameObject> m_FrameObjects;
121
122 // The number of submit semaphores should be equal to the number of images
123 // in the swapchain. We could use NUMBER_OF_FRAMES_IN_FLIGHT of objects but
124 // it might be not safe. Since vkQueuePresentKHR doesn't provide a way to
125 // tell that a semaphore was signaled.
126 //
127 // A possible situation, list of acquired indices:
128 // 0, 1, 2, 0, 1, 0, 1
129 // ^
130 // In theory in the end we might still have a semaphore in use for the
131 // 2nd swapchain image.
132 //
133 // See also:
134 // https://docs.vulkan.org/guide/latest/swapchain_semaphore_reuse.html
135 // We need to present only after all submit work is done.
136 std::vector<VkSemaphore> m_SubmitSemaphores;
137
139 {
140 using BackbufferKey = std::tuple<
144 {
145 size_t operator()(const BackbufferKey& key) const;
146 };
147 std::unordered_map<
148 BackbufferKey, std::unique_ptr<CFramebuffer>, BackbufferKeyHash> backbuffers;
149
151
154
157 };
158 std::vector<SwapChainBackbuffer> m_Backbuffers;
159
160 std::unique_ptr<CTexture> m_BackbufferReadbackTexture;
161
162 std::unique_ptr<CRingCommandContext> m_AcquireCommandContext;
163 std::unique_ptr<CRingCommandContext> m_PresentCommandContext;
164
168};
169
170} // namespace Vulkan
171
172} // namespace Backend
173
174} // namespace Renderer
175
176#endif // INCLUDED_RENDERER_BACKEND_VULKAN_SWAPCHAIN
Represents a texture object.
Definition: TextureManager.h:271
Definition: IDevice.h:64
IFramebuffer stores attachments which should be used by backend as rendering destinations.
Definition: IFramebuffer.h:87
Definition: ISwapChain.h:33
Definition: Device.h:67
A simple helper class to decouple command buffers rotation from frames presenting.
Definition: RingCommandContext.h:53
A helper class to batch VkQueueSubmit calls and track VkCommandBuffer usages properly.
Definition: SubmitScheduler.h:46
static constexpr SubmitHandle INVALID_SUBMIT_HANDLE
Definition: SubmitScheduler.h:49
uint32_t SubmitHandle
Definition: SubmitScheduler.h:48
Definition: SwapChain.h:49
bool m_IsValid
Definition: SwapChain.h:98
std::vector< FrameObject > m_FrameObjects
Definition: SwapChain.h:120
IDevice * GetDevice() override
Definition: SwapChain.cpp:348
~CSwapChain() override
Definition: SwapChain.cpp:322
bool m_DebugWaitIdleAfterPresent
Definition: SwapChain.h:167
std::unique_ptr< CTexture > m_DepthTexture
Definition: SwapChain.h:105
uint32_t m_FrameID
Definition: SwapChain.h:108
VkSwapchainKHR GetVkSwapchain()
Definition: SwapChain.h:67
VkQueue m_Queue
Definition: SwapChain.h:96
IFramebuffer * GetCurrentBackbuffer(const AttachmentLoadOp colorAttachmentLoadOp, const AttachmentStoreOp colorAttachmentStoreOp, const AttachmentLoadOp depthStencilAttachmentLoadOp, const AttachmentStoreOp depthStencilAttachmentStoreOp) override
Returns a framebuffer for the current backbuffer with the required attachment operations.
Definition: SwapChain.cpp:512
CTexture * GetDepthTexture()
Definition: SwapChain.h:80
CDevice * m_Device
Definition: SwapChain.h:94
bool m_DebugWaitIdleBeforeAcquire
Definition: SwapChain.h:165
bool IsValid() const override
Definition: SwapChain.h:55
std::unique_ptr< CRingCommandContext > m_AcquireCommandContext
Definition: SwapChain.h:162
CTexture * GetOrCreateBackbufferReadbackTexture()
Definition: SwapChain.cpp:553
std::vector< VkImage > m_Images
Definition: SwapChain.h:103
CSubmitScheduler * m_SubmitScheduler
Definition: SwapChain.h:95
void SubmitCommandsAfterAcquireNextImage(CRingCommandContext &commandContext)
Definition: SwapChain.cpp:446
std::vector< SwapChainBackbuffer > m_Backbuffers
Definition: SwapChain.h:158
VkSwapchainKHR m_SwapChain
Definition: SwapChain.h:99
bool AcquireNextBackbuffer() override
Acquires a backbuffer for rendering a frame.
Definition: SwapChain.cpp:353
bool m_DebugWaitIdleBeforePresent
Definition: SwapChain.h:166
VkFormat m_ImageFormat
Definition: SwapChain.h:106
std::unique_ptr< CTexture > m_BackbufferReadbackTexture
Definition: SwapChain.h:160
bool AcquireNextImage()
Definition: SwapChain.cpp:422
std::vector< VkSemaphore > m_SubmitSemaphores
Definition: SwapChain.h:136
void SubmitCommandsBeforePresent(CRingCommandContext &commandContext)
Definition: SwapChain.cpp:470
std::vector< std::unique_ptr< CTexture > > m_Textures
Definition: SwapChain.h:104
CTexture * GetCurrentBackbufferTexture()
Definition: SwapChain.cpp:547
void Present() override
Presents the backbuffer to the swapchain queue to be flipped on a screen.
Definition: SwapChain.cpp:380
static std::unique_ptr< CSwapChain > Create(CDevice *device, CSubmitScheduler *submitScheduler, const uint32_t queueFamilyIndex, VkQueue queue, const char *name, VkSurfaceKHR surface, int surfaceDrawableWidth, int surfaceDrawableHeight, const bool vsync, std::unique_ptr< ISwapChain > oldSwapChain)
Definition: SwapChain.cpp:55
uint32_t m_CurrentImageIndex
Definition: SwapChain.h:101
std::unique_ptr< CRingCommandContext > m_PresentCommandContext
Definition: SwapChain.h:163
Definition: Texture.h:43
Definition: Buffer.cpp:37
AttachmentStoreOp
Store operation is set for each attachment, what should be done with its content on EndFramebufferPas...
Definition: IFramebuffer.h:54
Backend
Definition: Backend.h:28
AttachmentLoadOp
Load operation is set for each attachment, what should be done with its content on BeginFramebufferPa...
Definition: IFramebuffer.h:39
Definition: Canvas2D.h:30
CSubmitScheduler::SubmitHandle submitHandle
Definition: SwapChain.h:118
VkSemaphore acquireImageSemaphore
Definition: SwapChain.h:116
size_t operator()(const BackbufferKey &key) const
Definition: SwapChain.cpp:406
SwapChainBackbuffer & operator=(SwapChainBackbuffer &&other)
SwapChainBackbuffer(const SwapChainBackbuffer &)=delete
SwapChainBackbuffer & operator=(const SwapChainBackbuffer &)=delete
std::unordered_map< BackbufferKey, std::unique_ptr< CFramebuffer >, BackbufferKeyHash > backbuffers
Definition: SwapChain.h:148
std::tuple< AttachmentLoadOp, AttachmentStoreOp, AttachmentLoadOp, AttachmentStoreOp > BackbufferKey
Definition: SwapChain.h:142
pthread_key_t key
Definition: wpthread.cpp:141