File: Renderer.h

package info (click to toggle)
0ad 0.27.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 171,928 kB
  • sloc: cpp: 194,011; javascript: 19,098; ansic: 15,066; python: 6,328; sh: 1,695; perl: 1,575; java: 533; xml: 415; php: 192; makefile: 99
file content (188 lines) | stat: -rw-r--r-- 5,436 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* Copyright (C) 2024 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef INCLUDED_RENDERER
#define INCLUDED_RENDERER

#include "graphics/Camera.h"
#include "graphics/ShaderDefines.h"
#include "graphics/ShaderProgramPtr.h"
#include "ps/containers/Span.h"
#include "ps/Singleton.h"
#include "renderer/backend/IDeviceCommandContext.h"
#include "renderer/backend/IShaderProgram.h"
#include "renderer/RenderingOptions.h"
#include "renderer/Scene.h"

#include <memory>

class CDebugRenderer;
class CFontManager;
class CPostprocManager;
class CSceneRenderer;
class CShaderManager;
class CTextureManager;
class CTimeManager;
class CVertexBufferManager;

#define g_Renderer CRenderer::GetSingleton()

/**
 * Higher level interface on top of the whole frame rendering. It does know
 * what should be rendered and via which renderer but shouldn't know how to
 * render a particular area, like UI or scene.
 */
class CRenderer : public Singleton<CRenderer>
{
public:
	// stats class - per frame counts of number of draw calls, poly counts etc
	struct Stats
	{
		// set all stats to zero
		void Reset() { memset(this, 0, sizeof(*this)); }
		// number of draw calls per frame - total DrawElements + Begin/End immediate mode loops
		size_t m_DrawCalls;
		// number of terrain triangles drawn
		size_t m_TerrainTris;
		// number of water triangles drawn
		size_t m_WaterTris;
		// number of (non-transparent) model triangles drawn
		size_t m_ModelTris;
		// number of overlay triangles drawn
		size_t m_OverlayTris;
		// number of splat passes for alphamapping
		size_t m_BlendSplats;
		// number of particles
		size_t m_Particles;
	};

	enum class ScreenShotType
	{
		NONE,
		DEFAULT,
		BIG
	};

public:
	CRenderer(Renderer::Backend::IDevice* device);
	~CRenderer();

	// open up the renderer: performs any necessary initialisation
	bool Open(int width, int height);

	// resize renderer view
	void Resize(int width, int height);

	// return view width
	int GetWidth() const { return m_Width; }
	// return view height
	int GetHeight() const { return m_Height; }

	void RenderFrame(bool needsPresent);

	// signal frame start
	void BeginFrame();
	// signal frame end
	void EndFrame();

	// trigger a reload of shaders (when parameters they depend on have changed)
	void MakeShadersDirty();

	// return stats accumulated for current frame
	Stats& GetStats() { return m_Stats; }

	CTextureManager& GetTextureManager();

	CVertexBufferManager& GetVertexBufferManager();

	CShaderManager& GetShaderManager();

	CFontManager& GetFontManager();

	CTimeManager& GetTimeManager();

	CPostprocManager& GetPostprocManager();

	CSceneRenderer& GetSceneRenderer();

	CDebugRenderer& GetDebugRenderer();

	/**
	 * Performs a complete frame without presenting to force loading all needed
	 * resources. It's used for the first frame on a game start.
	 * TODO: It might be better to preload resources without a complete frame
	 * rendering.
	 */
	void PreloadResourcesBeforeNextFrame();

	/**
	 * Makes a screenshot on the next RenderFrame according of the given
	 * screenshot type.
	 */
	void MakeScreenShotOnNextFrame(ScreenShotType screenShotType);

	Renderer::Backend::IDeviceCommandContext* GetDeviceCommandContext();

	/**
	 * Returns a cached vertex input layout. The renderer owns the layout to be
	 * able to share it between different clients. As backend should have
	 * as few different layouts as possible.
	 * The function isn't cheap so it should be called as rarely as possible.
	 * TODO: we need to make VertexArray less error prone by passing layout.
	 */
	Renderer::Backend::IVertexInputLayout* GetVertexInputLayout(
		const PS::span<const Renderer::Backend::SVertexAttributeFormat> attributes);

protected:
	friend class CDecalRData;
	friend class CPatchRData;
	friend class CPUSkinnedModelVertexRenderer;
	friend class CRenderingOptions;
	friend class GPUSkinnedModelModelRenderer;
	friend class InstancingModelRenderer;

	bool ShouldRender() const;

	void RenderFrameImpl(const bool renderGUI, const bool renderLogger);
	void RenderFrame2D(const bool renderGUI, const bool renderLogger);
	void RenderScreenShot(const bool needsPresent);
	void RenderBigScreenShot(const bool needsPresent);

	// SetRenderPath: Select the preferred render path.
	// This may only be called before Open(), because the layout of vertex arrays and other
	// data may depend on the chosen render path.
	void SetRenderPath(RenderPath rp);

	void ReloadShaders();

	// Private data that is not needed by inline functions.
	class Internals;
	std::unique_ptr<Internals> m;
	// view width
	int m_Width = 0;
	// view height
	int m_Height = 0;

	// per-frame renderer stats
	Stats m_Stats;

	bool m_ShouldPreloadResourcesBeforeNextFrame = false;

	ScreenShotType m_ScreenShotType = ScreenShotType::NONE;
};

#endif // INCLUDED_RENDERER