Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
PostprocManager.h
Go to the documentation of this file.
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_POSTPROCMANAGER
19#define INCLUDED_POSTPROCMANAGER
20
22#include "ps/CStr.h"
25
26#include <array>
27#include <cstdint>
28#include <memory>
29#include <vector>
30
32namespace Renderer::Backend { class IDevice; }
33namespace Renderer::Backend { class IDeviceCommandContext; }
34namespace Renderer::Backend { class IVertexInputLayout; }
35
37{
38public:
40
41 // Returns true if the the manager can be used.
42 bool IsEnabled() const;
43
44 // Create all buffers/textures in GPU memory and set default effect.
45 // @note Must be called before using in the renderer. May be called multiple times.
46 void Initialize(const uint32_t width, const uint32_t height);
47
48 // Recreate needed resources. Useful to call on settings changes.
49 void Recreate();
50
51 void CreateViewSizeDependentObjects(const uint32_t width, const uint32_t height);
53
54 // Returns a list of xml files found in shaders/effects/postproc.
55 static std::vector<CStrW> GetPostEffects();
56
57 // Returns the name of the current effect.
58 const CStrW& GetPostEffect() const
59 {
60 return m_PostProcEffect;
61 }
62
63 // Sets the current effect.
64 void SetPostEffect(const CStrW& name);
65
66 // Triggers update of shaders and framebuffers if needed.
70 void SetUpscaleTechnique(const CStr& upscaleName);
71
72 void SetDepthBufferClipPlanes(float nearPlane, float farPlane);
73
74 // @note CPostprocManager must be initialized first
76
77 // First renders blur textures, then calls ApplyEffect for each effect pass,
78 // ping-ponging the buffers at each step.
79 // @note CPostprocManager must be initialized first
80 void ApplyPostproc(
81 Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
82
83 // Blits the final postprocessed texture to the system framebuffer. The system
84 // framebuffer is selected as the output buffer. Should be called before
85 // silhouette rendering.
86 // @note CPostprocManager must be initialized first
88 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
90
91 // Returns true if we render main scene in the MSAA framebuffer.
92 bool IsMultisampleEnabled() const;
93
94 // Resolves the MSAA framebuffer into the regular one.
96 Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
97
98 float GetScale() const { return m_Scale; }
99
100 bool IsPBREnabled() const { return m_PBREnabled; }
101
103
104private:
108
109 void InitializePBR();
110
111 void RecalculateSize(const uint32_t width, const uint32_t height);
112
113 bool ShouldUpscale() const;
114 bool ShouldDownscale() const;
115
117 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
118 CShaderTechnique* shaderTechnique,
120 Renderer::Backend::ITexture* destination);
122 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
123 CShaderTechnique* shaderTechnique,
126
128 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
129 CShaderTechnique* shaderTechnique,
131 Renderer::Backend::ITexture* destination);
132
134 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
135 CShaderTechnique* shaderTechnique,
137 Renderer::Backend::ITexture* destination);
138
139 /**
140 * Resolve PBR HDR output to SDR one.
141 */
142 void ResolvePBR(
143 Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
144
146
149
150 bool m_PBREnabled{false};
151 std::unique_ptr<Renderer::Backend::ITexture> m_PBROutputTexture;
152
153 std::unique_ptr<Renderer::Backend::IFramebuffer> m_CaptureFramebuffer;
154
155 // Two framebuffers, that we flip between at each shader pass.
156 std::unique_ptr<Renderer::Backend::IFramebuffer>
158
159 // Unique color textures for the framebuffers.
160 std::unique_ptr<Renderer::Backend::ITexture> m_ColorTex1, m_ColorTex2;
161
162 std::unique_ptr<Renderer::Backend::ITexture>
164 std::unique_ptr<Renderer::Backend::IFramebuffer>
166 float m_Scale = 1.0f;
167
168 // The framebuffers share a depth/stencil texture.
169 std::unique_ptr<Renderer::Backend::ITexture> m_DepthTex;
171
172 // A framebuffer and textures x2 for each blur level we render.
174 {
175 struct Step
176 {
177 std::unique_ptr<Renderer::Backend::IFramebuffer> framebuffer;
178 std::unique_ptr<Renderer::Backend::ITexture> texture;
179 };
180 std::array<Step, 2> steps;
181 };
182 std::array<BlurScale, 3> m_BlurScales;
183
184 // Indicates which of the ping-pong buffers is used for reading and which for drawing.
186
188
189 // The name and shader technique we are using. "default" name means no technique is used
190 // (i.e. while we do allocate the buffers, no effects are rendered).
193
196
200
204 // Sharp technique only for FSR upscale.
206
210 std::unique_ptr<Renderer::Backend::IFramebuffer> m_MultisampleFramebuffer;
211 std::unique_ptr<Renderer::Backend::ITexture>
214 std::vector<uint32_t> m_AllowedSampleCounts;
215
216 // The current screen dimensions in pixels.
217 uint32_t m_Width, m_Height;
219
220 // Is the postproc manager initialized? Buffers created? Default effect loaded?
222
223 // Creates blur textures at various scales, for bloom, DOF, etc.
224 void ApplyBlur(
225 Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
226
227 // High quality GPU image scaling to half size. outTex must have exactly half the size
228 // of inTex. inWidth and inHeight are the dimensions of inTex in texels.
230 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
233 int inWidth, int inHeight);
234
235 // GPU-based Gaussian blur in two passes. inOutTex contains the input image and will be filled
236 // with the blurred image. tempTex must have the same size as inOutTex.
237 // inWidth and inHeight are the dimensions of the images in texels.
238 void ApplyBlurGauss(
239 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
242 Renderer::Backend::IFramebuffer* tempFramebuffer,
243 Renderer::Backend::IFramebuffer* outFramebuffer,
244 int inWidth, int inHeight);
245
246 // Applies a pass of a given effect to the entire current framebuffer. The shader is
247 // provided with a number of general-purpose variables, including the rendered screen so far,
248 // the depth buffer, a number of blur textures, the screen size, the zNear/zFar planes and
249 // some other parameters used by the optional bloom/HDR pass.
250 void ApplyEffect(
251 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
252 const CShaderTechniquePtr& shaderTech, int pass);
253
254 // Delete all allocated buffers/textures from GPU memory.
255 void Cleanup();
256
257 // Delete existing buffers/textures and create them again, using a new screen size if needed.
258 // (the textures are also attached to the framebuffers)
259 void RecreateBuffers();
260};
261
262#endif // INCLUDED_POSTPROCMANAGER
std::shared_ptr< CShaderTechnique > CShaderTechniquePtr
Definition: ShaderTechniquePtr.h:30
Definition: PostprocManager.h:37
bool m_PBREnabled
Definition: PostprocManager.h:150
std::array< BlurScale, 3 > m_BlurScales
Definition: PostprocManager.h:182
void SetDepthBufferClipPlanes(float nearPlane, float farPlane)
Definition: PostprocManager.cpp:941
void ResolvePBR(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Resolve PBR HDR output to SDR one.
Definition: PostprocManager.cpp:1019
uint32_t m_MultisampleCount
Definition: PostprocManager.h:213
void SetUpscaleTechnique(const CStr &upscaleName)
Definition: PostprocManager.cpp:921
void UpdateSharpeningTechnique()
Definition: PostprocManager.cpp:899
float m_FarPlane
Definition: PostprocManager.h:170
CPostprocManager(Renderer::Backend::IDevice *device)
Definition: PostprocManager.cpp:99
float m_NearPlane
Definition: PostprocManager.h:170
void DestroyViewSizeDependentObjects()
Definition: PostprocManager.cpp:243
bool m_UsingMultisampleBuffer
Definition: PostprocManager.h:209
std::unique_ptr< Renderer::Backend::ITexture > m_UnscaledTexture2
Definition: PostprocManager.h:163
CStr m_AAName
Definition: PostprocManager.h:207
void RecreateBuffers()
Definition: PostprocManager.cpp:249
float GetScale() const
Definition: PostprocManager.h:98
void UpdateAntiAliasingTechnique()
Definition: PostprocManager.cpp:848
std::unique_ptr< Renderer::Backend::IFramebuffer > m_PongFramebuffer
Definition: PostprocManager.h:157
void Recreate()
Definition: PostprocManager.cpp:228
void UpscaleTextureByCompute(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, CShaderTechnique *shaderTechnique, Renderer::Backend::ITexture *source, Renderer::Backend::ITexture *destination)
Definition: PostprocManager.cpp:526
void DownscaleTextureByCompute(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, CShaderTechnique *shaderTechnique, Renderer::Backend::ITexture *source, Renderer::Backend::ITexture *destination)
Definition: PostprocManager.cpp:616
static std::vector< CStrW > GetPostEffects()
Definition: PostprocManager.cpp:812
CShaderTechniquePtr m_DownscaleComputeTech
Definition: PostprocManager.h:203
void UpscaleTextureByFullscreenQuad(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, CShaderTechnique *shaderTechnique, Renderer::Backend::ITexture *source, Renderer::Backend::IFramebuffer *destination)
Definition: PostprocManager.cpp:552
void ApplySharpnessAfterScale(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, CShaderTechnique *shaderTechnique, Renderer::Backend::ITexture *source, Renderer::Backend::ITexture *destination)
Definition: PostprocManager.cpp:585
std::unique_ptr< Renderer::Backend::IFramebuffer > m_UnscaledFramebuffer2
Definition: PostprocManager.h:165
std::unique_ptr< Renderer::Backend::ITexture > m_UnscaledTexture1
Definition: PostprocManager.h:163
bool IsEnabled() const
Definition: PostprocManager.cpp:106
CShaderTechniquePtr m_RCASComputeTech
Definition: PostprocManager.h:205
CShaderTechniquePtr m_PostProcTech
Definition: PostprocManager.h:192
void ApplyBlurGauss(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, Renderer::Backend::ITexture *inTex, Renderer::Backend::ITexture *tempTex, Renderer::Backend::IFramebuffer *tempFramebuffer, Renderer::Backend::IFramebuffer *outFramebuffer, int inWidth, int inHeight)
Definition: PostprocManager.cpp:437
CShaderTechniquePtr m_SharpTech
Definition: PostprocManager.h:198
void Cleanup()
Definition: PostprocManager.cpp:116
std::unique_ptr< Renderer::Backend::ITexture > m_ColorTex2
Definition: PostprocManager.h:160
void DestroyMultisampleBuffer()
Definition: PostprocManager.cpp:995
void ApplyEffect(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, const CShaderTechniquePtr &shaderTech, int pass)
Definition: PostprocManager.cpp:720
std::vector< uint32_t > m_AllowedSampleCounts
Definition: PostprocManager.h:214
uint32_t m_Width
Definition: PostprocManager.h:217
Renderer::Backend::IVertexInputLayout * m_VertexInputLayout
Definition: PostprocManager.h:187
void Initialize(const uint32_t width, const uint32_t height)
Definition: PostprocManager.cpp:141
std::unique_ptr< Renderer::Backend::ITexture > m_ColorTex1
Definition: PostprocManager.h:160
CShaderTechniquePtr m_AATech
Definition: PostprocManager.h:208
std::unique_ptr< Renderer::Backend::IFramebuffer > m_CaptureFramebuffer
Definition: PostprocManager.h:153
bool IsMultisampleEnabled() const
Definition: PostprocManager.cpp:1002
std::unique_ptr< Renderer::Backend::IFramebuffer > m_PingFramebuffer
Definition: PostprocManager.h:157
Renderer::Backend::IDevice * m_Device
Definition: PostprocManager.h:145
Renderer::Backend::Format GetFramebufferColorFormat() const
Definition: PostprocManager.h:102
float m_Sharpness
Definition: PostprocManager.h:199
const CStrW & GetPostEffect() const
Definition: PostprocManager.h:58
bool IsPBREnabled() const
Definition: PostprocManager.h:100
uint32_t m_UnscaledHeight
Definition: PostprocManager.h:218
Renderer::Backend::Format m_FramebufferColorFormat
Definition: PostprocManager.h:147
void ResolveMultisampleFramebuffer(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: PostprocManager.cpp:1007
uint32_t m_Height
Definition: PostprocManager.h:217
void ApplyBlur(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: PostprocManager.cpp:497
std::unique_ptr< Renderer::Backend::IFramebuffer > m_MultisampleFramebuffer
Definition: PostprocManager.h:210
void UpdateSharpnessFactor()
Definition: PostprocManager.cpp:916
CShaderTechniquePtr m_UpscaleTech
Definition: PostprocManager.h:201
void RecalculateSize(const uint32_t width, const uint32_t height)
Definition: PostprocManager.cpp:1083
void ApplyBlurDownscale2x(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, Renderer::Backend::IFramebuffer *framebuffer, Renderer::Backend::ITexture *inTex, int inWidth, int inHeight)
Definition: PostprocManager.cpp:406
bool m_WhichBuffer
Definition: PostprocManager.h:185
Renderer::Backend::IFramebuffer * PrepareAndGetOutputFramebuffer()
Definition: PostprocManager.cpp:515
bool ShouldUpscale() const
Definition: PostprocManager.cpp:1097
bool ShouldDownscale() const
Definition: PostprocManager.cpp:1102
std::unique_ptr< Renderer::Backend::ITexture > m_DepthTex
Definition: PostprocManager.h:169
CShaderTechniquePtr m_UpscaleComputeTech
Definition: PostprocManager.h:202
void InitializePBR()
Definition: PostprocManager.cpp:186
void CreateViewSizeDependentObjects(const uint32_t width, const uint32_t height)
Definition: PostprocManager.cpp:234
void BlitOutputFramebuffer(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, Renderer::Backend::IFramebuffer *destination)
Definition: PostprocManager.cpp:642
std::unique_ptr< Renderer::Backend::ITexture > m_MultisampleColorTex
Definition: PostprocManager.h:212
CStr m_SharpName
Definition: PostprocManager.h:197
void CreateMultisampleBuffer()
Definition: PostprocManager.cpp:947
std::unique_ptr< Renderer::Backend::IFramebuffer > m_UnscaledFramebuffer1
Definition: PostprocManager.h:165
float m_Scale
Definition: PostprocManager.h:166
void ApplyPostproc(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: PostprocManager.cpp:775
CShaderTechniquePtr m_ResolvePBRTech
Definition: PostprocManager.h:194
std::unique_ptr< Renderer::Backend::ITexture > m_MultisampleDepthTex
Definition: PostprocManager.h:212
void RecreateCaptureFramebuffer()
Definition: PostprocManager.cpp:383
bool m_IsInitialized
Definition: PostprocManager.h:221
void SetPostEffect(const CStrW &name)
Definition: PostprocManager.cpp:834
CShaderTechniquePtr m_ResolvePBRComputeTech
Definition: PostprocManager.h:195
uint32_t m_UnscaledWidth
Definition: PostprocManager.h:218
CStrW m_PostProcEffect
Definition: PostprocManager.h:191
std::unique_ptr< Renderer::Backend::ITexture > m_PBROutputTexture
Definition: PostprocManager.h:151
Implements a render technique consisting of a sequence of passes.
Definition: ShaderTechnique.h:61
Definition: IDeviceCommandContext.h:46
Definition: IDevice.h:64
IFramebuffer stores attachments which should be used by backend as rendering destinations.
Definition: IFramebuffer.h:87
Definition: ITexture.h:33
IVertexInputLayout stores precompiled list of vertex attributes.
Definition: IShaderProgram.h:78
Definition: Canvas2D.h:30
Format
Definition: Format.h:28
Definition: PostprocManager.h:176
std::unique_ptr< Renderer::Backend::ITexture > texture
Definition: PostprocManager.h:178
std::unique_ptr< Renderer::Backend::IFramebuffer > framebuffer
Definition: PostprocManager.h:177
Definition: PostprocManager.h:174
std::array< Step, 2 > steps
Definition: PostprocManager.h:180