Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Renderer.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
19#define INCLUDED_RENDERER
20
21#include "ps/Singleton.h"
23
24#include <cstring>
25#include <memory>
26#include <span>
27
28class CDebugRenderer;
29class CFontManager;
30class CGameView;
32class CSceneRenderer;
33class CShaderManager;
34class CTextureManager;
35class CTimeManager;
37namespace PS::Memory { class LinearAllocator; }
38namespace Renderer::Backend { class IDevice; }
39namespace Renderer::Backend { class IDeviceCommandContext; }
40namespace Renderer::Backend { class ISwapChain; }
41namespace Renderer::Backend { class IVertexInputLayout; }
42namespace Renderer::Backend { struct SVertexAttributeFormat; }
43
44/**
45 * Higher level interface on top of the whole frame rendering. It does know
46 * what should be rendered and via which renderer but shouldn't know how to
47 * render a particular area, like UI or scene.
48 */
49class CRenderer : public Singleton<CRenderer>
50{
51public:
52 // stats class - per frame counts of number of draw calls, poly counts etc
53 struct Stats
54 {
55 // set all stats to zero
56 void Reset() { memset(this, 0, sizeof(*this)); }
57 // number of draw calls per frame - total DrawElements + Begin/End immediate mode loops
59 // number of terrain triangles drawn
61 // number of water triangles drawn
63 // number of (non-transparent) model triangles drawn
65 // number of overlay triangles drawn
67 // number of splat passes for alphamapping
69 // number of particles
71 };
72
73 enum class ScreenShotType
74 {
75 NONE,
76 DEFAULT,
77 BIG
78 };
79
80public:
82 ~CRenderer();
83
84 // open up the renderer: performs any necessary initialisation
85 bool Open(const uint32_t width, const uint32_t height);
86
87 void CreateViewSizeDependentObjects(const uint32_t width, const uint32_t height);
89
90 // return view width
91 int GetWidth() const { return m_Width; }
92 // return view height
93 int GetHeight() const { return m_Height; }
94
95 void RenderFrame(bool needsPresent);
96
97 // signal frame start
98 void BeginFrame();
99 // signal frame end
100 void EndFrame();
101
102 // trigger a reload of shaders (when parameters they depend on have changed)
103 void MakeShadersDirty();
104
105 // return stats accumulated for current frame
106 Stats& GetStats() { return m_Stats; }
107
109
111
113
115
117
119
121
123
124 /**
125 * Performs a complete frame without presenting to force loading all needed
126 * resources. It's used for the first frame on a game start.
127 * TODO: It might be better to preload resources without a complete frame
128 * rendering.
129 */
131
132 /**
133 * Makes a screenshot on the next RenderFrame according of the given
134 * screenshot type.
135 */
136 void MakeScreenShotOnNextFrame(ScreenShotType screenShotType);
137
139
140 /**
141 * Returns a cached vertex input layout. The renderer owns the layout to be
142 * able to share it between different clients. As backend should have
143 * as few different layouts as possible.
144 * The function isn't cheap so it should be called as rarely as possible.
145 * TODO: we need to make VertexArray less error prone by passing layout.
146 */
148 const std::span<const Renderer::Backend::SVertexAttributeFormat> attributes);
149
150 /**
151 * Currently using the linear allocated is allowed in small scopes to avoid
152 * high memory overhead. To validate that use PS::Memory::ScopedLinearAllocator.
153 */
155
156protected:
157 friend class CDecalRData;
158 friend class CPatchRData;
160 friend class CRenderingOptions;
163
164 bool ShouldRender() const;
165
166 void RenderFrameImpl(
168 const bool renderGUI, const bool renderLogger);
169 void RenderScreenShot(const bool needsPresent);
170 void RenderBigScreenShot(const bool needsPresent);
171
172 void RenderGameAndGUI(
173 Renderer::Backend::ISwapChain& swapChain, CGameView& gameView,
174 const bool renderGUI, const bool renderLogger);
176 Renderer::Backend::ISwapChain& swapChain, CGameView& gameView,
177 CPostprocManager& postprocManager,
178 const bool renderGUI, const bool renderLogger);
179 void RenderGUIOnly(
181 const bool renderGUI, const bool renderLogger);
182
183 void RenderGameOverlays(CGameView& gameView);
184 void Render2D(const bool renderGUI, const bool renderLogger);
185
186 void ReloadShaders();
187
188 // Private data that is not needed by inline functions.
189 class Internals;
190 std::unique_ptr<Internals> m;
191 // view width
192 int m_Width = 0;
193 // view height
194 int m_Height = 0;
195
196 // per-frame renderer stats
198
200};
201
202#define g_Renderer CRenderer::GetSingleton()
203
204#endif // INCLUDED_RENDERER
Definition: DebugRenderer.h:40
Definition: DecalRData.h:37
Font manager: loads and caches bitmap fonts.
Definition: FontManager.h:41
Definition: GameView.h:37
Render animated models using a ShaderRenderModifier.
Definition: CPUSkinnedModelRenderer.h:37
Definition: PatchRData.h:46
Definition: PostprocManager.h:37
Struct CRendererInternals: Truly hide data that is supposed to be hidden in this structure so it won'...
Definition: Renderer.cpp:321
Higher level interface on top of the whole frame rendering.
Definition: Renderer.h:50
CFontManager & GetFontManager()
Definition: Renderer.cpp:1052
bool m_ShouldPreloadResourcesBeforeNextFrame
Definition: Renderer.h:199
void DestroyViewSizeDependentObjects()
Definition: Renderer.cpp:506
Stats m_Stats
Definition: Renderer.h:197
CPostprocManager & GetPostprocManager()
Definition: Renderer.cpp:1037
CRenderer(Renderer::Backend::IDevice *device)
Definition: Renderer.cpp:431
Renderer::Backend::IDeviceCommandContext * GetDeviceCommandContext()
Definition: Renderer.cpp:1067
std::unique_ptr< Internals > m
Definition: Renderer.h:190
void Render2D(const bool renderGUI, const bool renderLogger)
Definition: Renderer.cpp:754
void RenderGameWithPostProcessingAndGUI(Renderer::Backend::ISwapChain &swapChain, CGameView &gameView, CPostprocManager &postprocManager, const bool renderGUI, const bool renderLogger)
Definition: Renderer.cpp:653
CVertexBufferManager & GetVertexBufferManager()
Definition: Renderer.cpp:1022
Stats & GetStats()
Definition: Renderer.h:106
PS::Memory::LinearAllocator & GetLinearAllocator()
Currently using the linear allocated is allowed in small scopes to avoid high memory overhead.
Definition: Renderer.cpp:1082
ScreenShotType
Definition: Renderer.h:74
void RenderGUIOnly(Renderer::Backend::ISwapChain &swapChain, const bool renderGUI, const bool renderLogger)
Definition: Renderer.cpp:710
void MakeShadersDirty()
Definition: Renderer.cpp:1011
void BeginFrame()
Definition: Renderer.cpp:987
void RenderFrame(bool needsPresent)
Definition: Renderer.cpp:519
CShaderManager & GetShaderManager()
Definition: Renderer.cpp:1027
void PreloadResourcesBeforeNextFrame()
Performs a complete frame without presenting to force loading all needed resources.
Definition: Renderer.cpp:1057
CDebugRenderer & GetDebugRenderer()
Definition: Renderer.cpp:1047
CSceneRenderer & GetSceneRenderer()
Definition: Renderer.cpp:1042
void EndFrame()
Definition: Renderer.cpp:1000
int m_Height
Definition: Renderer.h:194
void RenderScreenShot(const bool needsPresent)
Definition: Renderer.cpp:794
void RenderFrameImpl(Renderer::Backend::ISwapChain &swapChain, const bool renderGUI, const bool renderLogger)
Definition: Renderer.cpp:559
void CreateViewSizeDependentObjects(const uint32_t width, const uint32_t height)
Definition: Renderer.cpp:496
void MakeScreenShotOnNextFrame(ScreenShotType screenShotType)
Makes a screenshot on the next RenderFrame according of the given screenshot type.
Definition: Renderer.cpp:1062
bool ShouldRender() const
Definition: Renderer.cpp:512
bool Open(const uint32_t width, const uint32_t height)
Definition: Renderer.cpp:478
Renderer::Backend::IVertexInputLayout * GetVertexInputLayout(const std::span< const Renderer::Backend::SVertexAttributeFormat > attributes)
Returns a cached vertex input layout.
Definition: Renderer.cpp:1072
void RenderBigScreenShot(const bool needsPresent)
Definition: Renderer.cpp:846
int m_Width
Definition: Renderer.h:192
void RenderGameOverlays(CGameView &gameView)
Definition: Renderer.cpp:741
void ReloadShaders()
Definition: Renderer.cpp:470
~CRenderer()
Definition: Renderer.cpp:459
int GetWidth() const
Definition: Renderer.h:91
int GetHeight() const
Definition: Renderer.h:93
CTextureManager & GetTextureManager()
Definition: Renderer.cpp:1017
CTimeManager & GetTimeManager()
Definition: Renderer.cpp:1032
void RenderGameAndGUI(Renderer::Backend::ISwapChain &swapChain, CGameView &gameView, const bool renderGUI, const bool renderLogger)
Definition: Renderer.cpp:626
Definition: RenderingOptions.h:51
Definition: SceneRenderer.h:48
Shader manager: loads and caches shader programs.
Definition: ShaderManager.h:48
Texture manager with asynchronous loading and automatic DDS conversion/compression.
Definition: TextureManager.h:79
Definition: TimeManager.h:22
Definition: VertexBufferManager.h:39
Render animated models using a ShaderRenderModifier.
Definition: GPUSkinnedModelRenderer.h:38
Render non-animated (but potentially moving) models using a ShaderRenderModifier.
Definition: InstancingModelRenderer.h:38
Linear allocator (also known as an arena allocator or bump allocator) is a simple and fast system tha...
Definition: LinearAllocator.h:50
Definition: IDeviceCommandContext.h:46
Definition: IDevice.h:64
Definition: ISwapChain.h:33
IVertexInputLayout stores precompiled list of vertex attributes.
Definition: IShaderProgram.h:78
Template base class for singletons.
Definition: Singleton.h:35
Definition: AllocatorAdapter.h:26
Definition: Canvas2D.h:30
Definition: Renderer.h:54
size_t m_OverlayTris
Definition: Renderer.h:66
size_t m_BlendSplats
Definition: Renderer.h:68
size_t m_ModelTris
Definition: Renderer.h:64
size_t m_TerrainTris
Definition: Renderer.h:60
void Reset()
Definition: Renderer.h:56
size_t m_Particles
Definition: Renderer.h:70
size_t m_WaterTris
Definition: Renderer.h:62
size_t m_DrawCalls
Definition: Renderer.h:58