Merge 'remotes/trunk'
[0ad.git] / source / renderer / Renderer.h
blob773b16d7f00c1d84ab2a3b775e30fe3358f352f3
1 /* Copyright (C) 2023 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef INCLUDED_RENDERER
19 #define INCLUDED_RENDERER
21 #include "graphics/Camera.h"
22 #include "graphics/ShaderDefines.h"
23 #include "graphics/ShaderProgramPtr.h"
24 #include "ps/containers/Span.h"
25 #include "ps/Singleton.h"
26 #include "renderer/backend/IDeviceCommandContext.h"
27 #include "renderer/backend/IShaderProgram.h"
28 #include "renderer/RenderingOptions.h"
29 #include "renderer/Scene.h"
31 #include <memory>
33 class CDebugRenderer;
34 class CFontManager;
35 class CPostprocManager;
36 class CSceneRenderer;
37 class CShaderManager;
38 class CTextureManager;
39 class CTimeManager;
40 class CVertexBufferManager;
42 #define g_Renderer CRenderer::GetSingleton()
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.
49 class CRenderer : public Singleton<CRenderer>
51 public:
52 // stats class - per frame counts of number of draw calls, poly counts etc
53 struct Stats
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
58 size_t m_DrawCalls;
59 // number of terrain triangles drawn
60 size_t m_TerrainTris;
61 // number of water triangles drawn
62 size_t m_WaterTris;
63 // number of (non-transparent) model triangles drawn
64 size_t m_ModelTris;
65 // number of overlay triangles drawn
66 size_t m_OverlayTris;
67 // number of splat passes for alphamapping
68 size_t m_BlendSplats;
69 // number of particles
70 size_t m_Particles;
73 enum class ScreenShotType
75 NONE,
76 DEFAULT,
77 BIG
80 public:
81 CRenderer(Renderer::Backend::IDevice* device);
82 ~CRenderer();
84 // open up the renderer: performs any necessary initialisation
85 bool Open(int width, int height);
87 // resize renderer view
88 void Resize(int width, int height);
90 // return view width
91 int GetWidth() const { return m_Width; }
92 // return view height
93 int GetHeight() const { return m_Height; }
95 void RenderFrame(bool needsPresent);
97 // signal frame start
98 void BeginFrame();
99 // signal frame end
100 void EndFrame();
102 // trigger a reload of shaders (when parameters they depend on have changed)
103 void MakeShadersDirty();
105 // return stats accumulated for current frame
106 Stats& GetStats() { return m_Stats; }
108 CTextureManager& GetTextureManager();
110 CVertexBufferManager& GetVertexBufferManager();
112 CShaderManager& GetShaderManager();
114 CFontManager& GetFontManager();
116 CTimeManager& GetTimeManager();
118 CPostprocManager& GetPostprocManager();
120 CSceneRenderer& GetSceneRenderer();
122 CDebugRenderer& GetDebugRenderer();
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.
130 void PreloadResourcesBeforeNextFrame();
133 * Makes a screenshot on the next RenderFrame according of the given
134 * screenshot type.
136 void MakeScreenShotOnNextFrame(ScreenShotType screenShotType);
138 Renderer::Backend::IDeviceCommandContext* GetDeviceCommandContext();
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.
147 Renderer::Backend::IVertexInputLayout* GetVertexInputLayout(
148 const PS::span<const Renderer::Backend::SVertexAttributeFormat> attributes);
150 protected:
151 friend class CPatchRData;
152 friend class CDecalRData;
153 friend class HWLightingModelRenderer;
154 friend class ShaderModelVertexRenderer;
155 friend class InstancingModelRenderer;
156 friend class CRenderingOptions;
158 bool ShouldRender() const;
160 void RenderFrameImpl(const bool renderGUI, const bool renderLogger);
161 void RenderFrame2D(const bool renderGUI, const bool renderLogger);
162 void RenderScreenShot(const bool needsPresent);
163 void RenderBigScreenShot(const bool needsPresent);
165 // SetRenderPath: Select the preferred render path.
166 // This may only be called before Open(), because the layout of vertex arrays and other
167 // data may depend on the chosen render path.
168 void SetRenderPath(RenderPath rp);
170 void ReloadShaders();
172 // Private data that is not needed by inline functions.
173 class Internals;
174 std::unique_ptr<Internals> m;
175 // view width
176 int m_Width = 0;
177 // view height
178 int m_Height = 0;
180 // per-frame renderer stats
181 Stats m_Stats;
183 bool m_ShouldPreloadResourcesBeforeNextFrame = false;
185 ScreenShotType m_ScreenShotType = ScreenShotType::NONE;
188 #endif // INCLUDED_RENDERER