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"
35 class CPostprocManager
;
38 class CTextureManager
;
40 class CVertexBufferManager
;
42 #define g_Renderer CRenderer::GetSingleton()
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
>
52 // stats class - per frame counts of number of draw calls, poly counts etc
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
73 enum class ScreenShotType
81 CRenderer(Renderer::Backend::IDevice
* device
);
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
);
91 int GetWidth() const { return m_Width
; }
93 int GetHeight() const { return m_Height
; }
95 void RenderFrame(bool needsPresent
);
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
130 void PreloadResourcesBeforeNextFrame();
133 * Makes a screenshot on the next RenderFrame according of the given
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
);
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.
174 std::unique_ptr
<Internals
> m
;
180 // per-frame renderer stats
183 bool m_ShouldPreloadResourcesBeforeNextFrame
= false;
185 ScreenShotType m_ScreenShotType
= ScreenShotType::NONE
;
188 #endif // INCLUDED_RENDERER