Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
Renderer.h
Go to the documentation of this file.
1/* Copyright (C) 2024 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 "graphics/Camera.h"
24#include "ps/containers/Span.h"
25#include "ps/Singleton.h"
29#include "renderer/Scene.h"
30
31#include <memory>
32
33class CDebugRenderer;
34class CFontManager;
36class CSceneRenderer;
37class CShaderManager;
38class CTextureManager;
39class CTimeManager;
41
42#define g_Renderer CRenderer::GetSingleton()
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(int width, int height);
86
87 // resize renderer view
88 void Resize(int width, int 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 */
149
150protected:
151 friend class CDecalRData;
152 friend class CPatchRData;
154 friend class CRenderingOptions;
156
157 bool ShouldRender() const;
158
159 void RenderFrameImpl(const bool renderGUI, const bool renderLogger);
160 void RenderFrame2D(const bool renderGUI, const bool renderLogger);
161 void RenderScreenShot(const bool needsPresent);
162 void RenderBigScreenShot(const bool needsPresent);
163
164 // SetRenderPath: Select the preferred render path.
165 // This may only be called before Open(), because the layout of vertex arrays and other
166 // data may depend on the chosen render path.
167 void SetRenderPath(RenderPath rp);
168
169 void ReloadShaders();
170
171 // Private data that is not needed by inline functions.
172 class Internals;
173 std::unique_ptr<Internals> m;
174 // view width
175 int m_Width = 0;
176 // view height
177 int m_Height = 0;
178
179 // per-frame renderer stats
181
183
185};
186
187#endif // INCLUDED_RENDERER
RenderPath
Definition: RenderingOptions.h:36
Definition: DebugRenderer.h:39
Definition: DecalRData.h:36
Font manager: loads and caches bitmap fonts.
Definition: FontManager.h:31
Render animated models using a ShaderRenderModifier.
Definition: CPUSkinnedModelRenderer.h:32
Definition: PatchRData.h:42
Definition: PostprocManager.h:32
Struct CRendererInternals: Truly hide data that is supposed to be hidden in this structure so it won'...
Definition: Renderer.cpp:243
Higher level interface on top of the whole frame rendering.
Definition: Renderer.h:50
CFontManager & GetFontManager()
Definition: Renderer.cpp:886
bool m_ShouldPreloadResourcesBeforeNextFrame
Definition: Renderer.h:182
Stats m_Stats
Definition: Renderer.h:180
bool Open(int width, int height)
Definition: Renderer.cpp:361
CPostprocManager & GetPostprocManager()
Definition: Renderer.cpp:871
CRenderer(Renderer::Backend::IDevice *device)
Definition: Renderer.cpp:315
Renderer::Backend::IDeviceCommandContext * GetDeviceCommandContext()
Definition: Renderer.cpp:901
std::unique_ptr< Internals > m
Definition: Renderer.h:173
CVertexBufferManager & GetVertexBufferManager()
Definition: Renderer.cpp:856
void RenderFrameImpl(const bool renderGUI, const bool renderLogger)
Definition: Renderer.cpp:471
Stats & GetStats()
Definition: Renderer.h:106
ScreenShotType
Definition: Renderer.h:74
void RenderFrame2D(const bool renderGUI, const bool renderLogger)
Definition: Renderer.cpp:611
void MakeShadersDirty()
Definition: Renderer.cpp:845
void BeginFrame()
Definition: Renderer.cpp:825
void RenderFrame(bool needsPresent)
Definition: Renderer.cpp:432
CShaderManager & GetShaderManager()
Definition: Renderer.cpp:861
void PreloadResourcesBeforeNextFrame()
Performs a complete frame without presenting to force loading all needed resources.
Definition: Renderer.cpp:891
void SetRenderPath(RenderPath rp)
Definition: Renderer.cpp:392
CDebugRenderer & GetDebugRenderer()
Definition: Renderer.cpp:881
CSceneRenderer & GetSceneRenderer()
Definition: Renderer.cpp:876
void EndFrame()
Definition: Renderer.cpp:838
int m_Height
Definition: Renderer.h:177
void RenderScreenShot(const bool needsPresent)
Definition: Renderer.cpp:649
void Resize(int width, int height)
Definition: Renderer.cpp:382
void MakeScreenShotOnNextFrame(ScreenShotType screenShotType)
Makes a screenshot on the next RenderFrame according of the given screenshot type.
Definition: Renderer.cpp:896
ScreenShotType m_ScreenShotType
Definition: Renderer.h:184
bool ShouldRender() const
Definition: Renderer.cpp:427
Renderer::Backend::IVertexInputLayout * GetVertexInputLayout(const PS::span< const Renderer::Backend::SVertexAttributeFormat > attributes)
Returns a cached vertex input layout.
Definition: Renderer.cpp:906
void RenderBigScreenShot(const bool needsPresent)
Definition: Renderer.cpp:697
int m_Width
Definition: Renderer.h:175
void ReloadShaders()
Definition: Renderer.cpp:353
~CRenderer()
Definition: Renderer.cpp:343
int GetWidth() const
Definition: Renderer.h:91
int GetHeight() const
Definition: Renderer.h:93
CTextureManager & GetTextureManager()
Definition: Renderer.cpp:851
CTimeManager & GetTimeManager()
Definition: Renderer.cpp:866
Definition: RenderingOptions.h:69
Definition: SceneRenderer.h:52
Shader manager: loads and caches shader programs.
Definition: ShaderManager.h:39
Texture manager with asynchronous loading and automatic DDS conversion/compression.
Definition: TextureManager.h:73
Definition: TimeManager.h:22
Definition: VertexBufferManager.h:35
Render non-animated (but potentially moving) models using a ShaderRenderModifier.
Definition: InstancingModelRenderer.h:36
Simplifed version of std::span (C++20) as we don't support the original one yet.
Definition: Span.h:37
Definition: IDeviceCommandContext.h:42
Definition: IDevice.h:48
IVertexInputLayout stores precompiled list of vertex attributes.
Definition: IShaderProgram.h:74
Template base class for singletons.
Definition: Singleton.h:34
#define DEFAULT(name)
Definition: mocks_test.cpp:29
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