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_GL_SWAPCHAIN
19#define INCLUDED_RENDERER_BACKEND_GL_SWAPCHAIN
20
21#include "lib/config2.h"
24
25#if CONFIG2_GLES
27#else
28#include <glad/gl.h>
29#endif
30
31#include <cstdint>
32#include <memory>
33#include <unordered_map>
34
35typedef struct SDL_Window SDL_Window;
36
37namespace Renderer
38{
39
40namespace Backend
41{
42
43namespace GL
44{
45
46class CDevice;
47
48class CSwapChain final : public ISwapChain
49{
50public:
51 ~CSwapChain() override;
52
53 IDevice* GetDevice() override;
54
55 bool IsValid() const override { return true; }
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
67private:
68 friend class CDevice;
69
70 static std::unique_ptr<CSwapChain> Create(
71 CDevice* device, SDL_Window* window,
72 int surfaceDrawableWidth, int surfaceDrawableHeight,
73 const bool vsync);
74
76
77 CDevice* m_Device{nullptr};
81
82 using BackbufferKey = std::tuple<
86 {
87 size_t operator()(const BackbufferKey& key) const;
88 };
89 // We use std::unordered_map to avoid storing sizes of Attachment*Op
90 // enumerations. If it becomes a performance issue we'll replace it
91 // by an array.
92 std::unordered_map<
93 BackbufferKey, std::unique_ptr<CFramebuffer>, BackbufferKeyHash> m_Backbuffers;
95};
96
97} // namespace GL
98
99} // namespace Backend
100
101} // namespace Renderer
102
103#endif // INCLUDED_RENDERER_BACKEND_GL_SWAPCHAIN
struct SDL_Window SDL_Window
Definition: VideoMode.h:31
Definition: Device.h:62
Definition: SwapChain.h:49
SDL_Window * m_Window
Definition: SwapChain.h:78
bool AcquireNextBackbuffer() override
Acquires a backbuffer for rendering a frame.
Definition: SwapChain.cpp:72
IDevice * GetDevice() override
Definition: SwapChain.cpp:67
~CSwapChain() override
Definition: SwapChain.cpp:63
void Present() override
Presents the backbuffer to the swapchain queue to be flipped on a screen.
Definition: SwapChain.cpp:109
CDevice * m_Device
Definition: SwapChain.h:77
std::unordered_map< BackbufferKey, std::unique_ptr< CFramebuffer >, BackbufferKeyHash > m_Backbuffers
Definition: SwapChain.h:93
bool m_BackbufferAcquired
Definition: SwapChain.h:94
static std::unique_ptr< CSwapChain > Create(CDevice *device, SDL_Window *window, int surfaceDrawableWidth, int surfaceDrawableHeight, const bool vsync)
Definition: SwapChain.cpp:47
int m_SurfaceDrawableHeight
Definition: SwapChain.h:80
int m_SurfaceDrawableWidth
Definition: SwapChain.h:79
std::tuple< AttachmentLoadOp, AttachmentStoreOp, AttachmentLoadOp, AttachmentStoreOp > BackbufferKey
Definition: SwapChain.h:84
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:89
bool IsValid() const override
Definition: SwapChain.h:55
Definition: IDevice.h:64
IFramebuffer stores attachments which should be used by backend as rendering destinations.
Definition: IFramebuffer.h:87
Definition: ISwapChain.h:33
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
size_t operator()(const BackbufferKey &key) const
Definition: SwapChain.cpp:79
pthread_key_t key
Definition: wpthread.cpp:141