Line data Source code
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" 22 : #include "renderer/backend/IDevice.h" 23 : #include "renderer/RenderingOptions.h" 24 : 25 : #include <cstring> 26 : #include <memory> 27 : #include <span> 28 : 29 : class CDebugRenderer; 30 : class CFontManager; 31 : class CGameView; 32 : class CPostprocManager; 33 : class CSceneRenderer; 34 : class CShaderManager; 35 : class CTextureManager; 36 : class CTimeManager; 37 : class CVertexBufferManager; 38 : namespace PS::Memory { class LinearAllocator; } 39 : namespace Renderer::Backend { class IDevice; } 40 : namespace Renderer::Backend { class IDeviceCommandContext; } 41 : namespace Renderer::Backend { class ISwapChain; } 42 : namespace Renderer::Backend { class IVertexInputLayout; } 43 : namespace Renderer::Backend { struct SVertexAttributeFormat; } 44 : 45 : /** 46 : * Higher level interface on top of the whole frame rendering. It does know 47 : * what should be rendered and via which renderer but shouldn't know how to 48 : * render a particular area, like UI or scene. 49 : */ 50 : class CRenderer : public Singleton<CRenderer> 51 : { 52 : public: 53 : // stats class - per frame counts of number of draw calls, poly counts etc 54 : struct Stats 55 : { 56 : // set all stats to zero 57 10 : void Reset() { memset(this, 0, sizeof(*this)); } 58 : // number of draw calls per frame - total DrawElements + Begin/End immediate mode loops 59 : size_t m_DrawCalls; 60 : // number of terrain triangles drawn 61 : size_t m_TerrainTris; 62 : // number of water triangles drawn 63 : size_t m_WaterTris; 64 : // number of (non-transparent) model triangles drawn 65 : size_t m_ModelTris; 66 : // number of overlay triangles drawn 67 : size_t m_OverlayTris; 68 : // number of splat passes for alphamapping 69 : size_t m_BlendSplats; 70 : // number of particles 71 : size_t m_Particles; 72 : }; 73 : 74 : enum class ScreenShotType 75 : { 76 : NONE, 77 : DEFAULT, 78 : BIG 79 : }; 80 : 81 : public: 82 : CRenderer(Renderer::Backend::IDevice* device); 83 : ~CRenderer(); 84 : 85 : // open up the renderer: performs any necessary initialisation 86 : bool Open(const uint32_t width, const uint32_t height); 87 : 88 : void CreateViewSizeDependentObjects(const uint32_t width, const uint32_t height); 89 : void DestroyViewSizeDependentObjects(); 90 : 91 : // return view width 92 10 : int GetWidth() const { return m_Width; } 93 : // return view height 94 20 : int GetHeight() const { return m_Height; } 95 : 96 : void RenderFrame(bool needsPresent); 97 : 98 : // signal frame start 99 : void BeginFrame(); 100 : // signal frame end 101 : void EndFrame(); 102 : 103 : // trigger a reload of shaders (when parameters they depend on have changed) 104 : void MakeShadersDirty(); 105 : 106 : // return stats accumulated for current frame 107 0 : Stats& GetStats() { return m_Stats; } 108 : 109 : CTextureManager& GetTextureManager(); 110 : 111 : CVertexBufferManager& GetVertexBufferManager(); 112 : 113 : CShaderManager& GetShaderManager(); 114 : 115 : CFontManager& GetFontManager(); 116 : 117 : CTimeManager& GetTimeManager(); 118 : 119 : CPostprocManager& GetPostprocManager(); 120 : 121 : CSceneRenderer& GetSceneRenderer(); 122 : 123 : CDebugRenderer& GetDebugRenderer(); 124 : 125 : /** 126 : * Performs a complete frame without presenting to force loading all needed 127 : * resources. It's used for the first frame on a game start. 128 : * TODO: It might be better to preload resources without a complete frame 129 : * rendering. 130 : */ 131 : void PreloadResourcesBeforeNextFrame(); 132 : 133 : /** 134 : * Makes a screenshot on the next RenderFrame according of the given 135 : * screenshot type. 136 : */ 137 : void MakeScreenShotOnNextFrame(ScreenShotType screenShotType); 138 : 139 : Renderer::Backend::IDeviceCommandContext* GetDeviceCommandContext(); 140 : 141 : /** 142 : * Returns a cached vertex input layout. The renderer owns the layout to be 143 : * able to share it between different clients. As backend should have 144 : * as few different layouts as possible. 145 : * The function isn't cheap so it should be called as rarely as possible. 146 : * TODO: we need to make VertexArray less error prone by passing layout. 147 : */ 148 : Renderer::Backend::IVertexInputLayout* GetVertexInputLayout( 149 : const std::span<const Renderer::Backend::SVertexAttributeFormat> attributes); 150 : 151 : /** 152 : * Currently using the linear allocated is allowed in small scopes to avoid 153 : * high memory overhead. To validate that use PS::Memory::ScopedLinearAllocator. 154 : */ 155 : PS::Memory::LinearAllocator& GetLinearAllocator(); 156 : 157 : const Renderer::Backend::IDevice::Capabilities& GetCapabilities() const; 158 : 159 : protected: 160 : friend class CDecalRData; 161 : friend class CPatchRData; 162 : friend class CPUSkinnedModelVertexRenderer; 163 : friend class CRenderingOptions; 164 : friend class GPUSkinnedModelModelRenderer; 165 : friend class InstancingModelRenderer; 166 : 167 : bool ShouldRender() const; 168 : 169 : void RenderFrameImpl( 170 : Renderer::Backend::ISwapChain& swapChain, 171 : const bool renderGUI, const bool renderLogger); 172 : void RenderScreenShot(const bool needsPresent); 173 : void RenderBigScreenShot(const bool needsPresent); 174 : 175 : void RenderGameAndGUI( 176 : Renderer::Backend::ISwapChain& swapChain, CGameView& gameView, 177 : const bool renderGUI, const bool renderLogger); 178 : void RenderGameWithPostProcessingAndGUI( 179 : Renderer::Backend::ISwapChain& swapChain, CGameView& gameView, 180 : CPostprocManager& postprocManager, 181 : const bool renderGUI, const bool renderLogger); 182 : void RenderGUIOnly( 183 : Renderer::Backend::ISwapChain& swapChain, 184 : const bool renderGUI, const bool renderLogger); 185 : 186 : void RenderGameOverlays(CGameView& gameView); 187 : void Render2D(const bool renderGUI, const bool renderLogger); 188 : 189 : void ReloadShaders(); 190 : 191 : // Private data that is not needed by inline functions. 192 : class Internals; 193 : std::unique_ptr<Internals> m; 194 : // view width 195 : int m_Width = 0; 196 : // view height 197 : int m_Height = 0; 198 : 199 : // per-frame renderer stats 200 : Stats m_Stats; 201 : 202 : bool m_ShouldPreloadResourcesBeforeNextFrame = false; 203 : }; 204 : 205 : #define g_Renderer CRenderer::GetSingleton() 206 : 207 : #endif // INCLUDED_RENDERER