Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
PostprocManager.h
Go to the documentation of this file.
1/* Copyright (C) 2024 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"
27
28#include <array>
29#include <vector>
30
32{
33public:
36
37 // Returns true if the the manager can be used.
38 bool IsEnabled() const;
39
40 // Create all buffers/textures in GPU memory and set default effect.
41 // @note Must be called before using in the renderer. May be called multiple times.
42 void Initialize();
43
44 // Update the size of the screen
45 void Resize();
46
47 // Returns a list of xml files found in shaders/effects/postproc.
48 static std::vector<CStrW> GetPostEffects();
49
50 // Returns the name of the current effect.
51 const CStrW& GetPostEffect() const
52 {
53 return m_PostProcEffect;
54 }
55
56 // Sets the current effect.
57 void SetPostEffect(const CStrW& name);
58
59 // Triggers update of shaders and framebuffers if needed.
63 void SetUpscaleTechnique(const CStr& upscaleName);
64
65 void SetDepthBufferClipPlanes(float nearPlane, float farPlane);
66
67 // @note CPostprocManager must be initialized first
69
70 // First renders blur textures, then calls ApplyEffect for each effect pass,
71 // ping-ponging the buffers at each step.
72 // @note CPostprocManager must be initialized first
73 void ApplyPostproc(
74 Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
75
76 // Blits the final postprocessed texture to the system framebuffer. The system
77 // framebuffer is selected as the output buffer. Should be called before
78 // silhouette rendering.
79 // @note CPostprocManager must be initialized first
81 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
83
84 // Returns true if we render main scene in the MSAA framebuffer.
85 bool IsMultisampleEnabled() const;
86
87 // Resolves the MSAA framebuffer into the regular one.
89 Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
90
91 float GetScale() const { return m_Scale; }
92
93private:
96
97 void RecalculateSize(const uint32_t width, const uint32_t height);
98
99 bool ShouldUpscale() const;
100 bool ShouldDownscale() const;
101
103 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
104 CShaderTechnique* shaderTechnique,
106 Renderer::Backend::ITexture* destination);
108 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
109 CShaderTechnique* shaderTechnique,
112
114 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
115 CShaderTechnique* shaderTechnique,
117 Renderer::Backend::ITexture* destination);
118
120 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
121 CShaderTechnique* shaderTechnique,
123 Renderer::Backend::ITexture* destination);
124
126
127 std::unique_ptr<Renderer::Backend::IFramebuffer> m_CaptureFramebuffer;
128
129 // Two framebuffers, that we flip between at each shader pass.
130 std::unique_ptr<Renderer::Backend::IFramebuffer>
132
133 // Unique color textures for the framebuffers.
134 std::unique_ptr<Renderer::Backend::ITexture> m_ColorTex1, m_ColorTex2;
135
136 std::unique_ptr<Renderer::Backend::ITexture>
138 std::unique_ptr<Renderer::Backend::IFramebuffer>
140 float m_Scale = 1.0f;
141
142 // The framebuffers share a depth/stencil texture.
143 std::unique_ptr<Renderer::Backend::ITexture> m_DepthTex;
145
146 // A framebuffer and textures x2 for each blur level we render.
148 {
149 struct Step
150 {
151 std::unique_ptr<Renderer::Backend::IFramebuffer> framebuffer;
152 std::unique_ptr<Renderer::Backend::ITexture> texture;
153 };
154 std::array<Step, 2> steps;
155 };
156 std::array<BlurScale, 3> m_BlurScales;
157
158 // Indicates which of the ping-pong buffers is used for reading and which for drawing.
160
162
163 // The name and shader technique we are using. "default" name means no technique is used
164 // (i.e. while we do allocate the buffers, no effects are rendered).
167
171
175 // Sharp technique only for FSR upscale.
177
181 std::unique_ptr<Renderer::Backend::IFramebuffer> m_MultisampleFramebuffer;
182 std::unique_ptr<Renderer::Backend::ITexture>
185 std::vector<uint32_t> m_AllowedSampleCounts;
186
187 // The current screen dimensions in pixels.
190
191 // Is the postproc manager initialized? Buffers created? Default effect loaded?
193
194 // Creates blur textures at various scales, for bloom, DOF, etc.
195 void ApplyBlur(
196 Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
197
198 // High quality GPU image scaling to half size. outTex must have exactly half the size
199 // of inTex. inWidth and inHeight are the dimensions of inTex in texels.
201 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
204 int inWidth, int inHeight);
205
206 // GPU-based Gaussian blur in two passes. inOutTex contains the input image and will be filled
207 // with the blurred image. tempTex must have the same size as inOutTex.
208 // inWidth and inHeight are the dimensions of the images in texels.
209 void ApplyBlurGauss(
210 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
213 Renderer::Backend::IFramebuffer* tempFramebuffer,
214 Renderer::Backend::IFramebuffer* outFramebuffer,
215 int inWidth, int inHeight);
216
217 // Applies a pass of a given effect to the entire current framebuffer. The shader is
218 // provided with a number of general-purpose variables, including the rendered screen so far,
219 // the depth buffer, a number of blur textures, the screen size, the zNear/zFar planes and
220 // some other parameters used by the optional bloom/HDR pass.
221 void ApplyEffect(
222 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
223 const CShaderTechniquePtr& shaderTech, int pass);
224
225 // Delete all allocated buffers/textures from GPU memory.
226 void Cleanup();
227
228 // Delete existing buffers/textures and create them again, using a new screen size if needed.
229 // (the textures are also attached to the framebuffers)
230 void RecreateBuffers();
231};
232
233#endif // INCLUDED_POSTPROCMANAGER
std::shared_ptr< CShaderTechnique > CShaderTechniquePtr
Definition: ShaderTechniquePtr.h:28
Definition: PostprocManager.h:32
std::array< BlurScale, 3 > m_BlurScales
Definition: PostprocManager.h:156
void SetDepthBufferClipPlanes(float nearPlane, float farPlane)
Definition: PostprocManager.cpp:853
uint32_t m_MultisampleCount
Definition: PostprocManager.h:184
void SetUpscaleTechnique(const CStr &upscaleName)
Definition: PostprocManager.cpp:833
void UpdateSharpeningTechnique()
Definition: PostprocManager.cpp:810
float m_FarPlane
Definition: PostprocManager.h:144
CPostprocManager(Renderer::Backend::IDevice *device)
Definition: PostprocManager.cpp:84
float m_NearPlane
Definition: PostprocManager.h:144
~CPostprocManager()
Definition: PostprocManager.cpp:91
bool m_UsingMultisampleBuffer
Definition: PostprocManager.h:180
std::unique_ptr< Renderer::Backend::ITexture > m_UnscaledTexture2
Definition: PostprocManager.h:137
CStr m_AAName
Definition: PostprocManager.h:178
void RecreateBuffers()
Definition: PostprocManager.cpp:184
float GetScale() const
Definition: PostprocManager.h:91
void UpdateAntiAliasingTechnique()
Definition: PostprocManager.cpp:760
std::unique_ptr< Renderer::Backend::IFramebuffer > m_PongFramebuffer
Definition: PostprocManager.h:131
void UpscaleTextureByCompute(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, CShaderTechnique *shaderTechnique, Renderer::Backend::ITexture *source, Renderer::Backend::ITexture *destination)
Definition: PostprocManager.cpp:436
void DownscaleTextureByCompute(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, CShaderTechnique *shaderTechnique, Renderer::Backend::ITexture *source, Renderer::Backend::ITexture *destination)
Definition: PostprocManager.cpp:526
static std::vector< CStrW > GetPostEffects()
Definition: PostprocManager.cpp:724
CShaderTechniquePtr m_DownscaleComputeTech
Definition: PostprocManager.h:174
void UpscaleTextureByFullscreenQuad(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, CShaderTechnique *shaderTechnique, Renderer::Backend::ITexture *source, Renderer::Backend::IFramebuffer *destination)
Definition: PostprocManager.cpp:462
void ApplySharpnessAfterScale(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, CShaderTechnique *shaderTechnique, Renderer::Backend::ITexture *source, Renderer::Backend::ITexture *destination)
Definition: PostprocManager.cpp:495
std::unique_ptr< Renderer::Backend::IFramebuffer > m_UnscaledFramebuffer2
Definition: PostprocManager.h:139
std::unique_ptr< Renderer::Backend::ITexture > m_UnscaledTexture1
Definition: PostprocManager.h:137
void Resize()
Definition: PostprocManager.cpp:175
bool IsEnabled() const
Definition: PostprocManager.cpp:96
CShaderTechniquePtr m_RCASComputeTech
Definition: PostprocManager.h:176
CShaderTechniquePtr m_PostProcTech
Definition: PostprocManager.h:166
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:347
CShaderTechniquePtr m_SharpTech
Definition: PostprocManager.h:169
void Cleanup()
Definition: PostprocManager.cpp:108
std::unique_ptr< Renderer::Backend::ITexture > m_ColorTex2
Definition: PostprocManager.h:134
void DestroyMultisampleBuffer()
Definition: PostprocManager.cpp:907
void ApplyEffect(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, const CShaderTechniquePtr &shaderTech, int pass)
Definition: PostprocManager.cpp:630
std::vector< uint32_t > m_AllowedSampleCounts
Definition: PostprocManager.h:185
uint32_t m_Width
Definition: PostprocManager.h:188
Renderer::Backend::IVertexInputLayout * m_VertexInputLayout
Definition: PostprocManager.h:161
std::unique_ptr< Renderer::Backend::ITexture > m_ColorTex1
Definition: PostprocManager.h:134
CShaderTechniquePtr m_AATech
Definition: PostprocManager.h:179
std::unique_ptr< Renderer::Backend::IFramebuffer > m_CaptureFramebuffer
Definition: PostprocManager.h:127
bool IsMultisampleEnabled() const
Definition: PostprocManager.cpp:916
std::unique_ptr< Renderer::Backend::IFramebuffer > m_PingFramebuffer
Definition: PostprocManager.h:131
Renderer::Backend::IDevice * m_Device
Definition: PostprocManager.h:125
float m_Sharpness
Definition: PostprocManager.h:170
const CStrW & GetPostEffect() const
Definition: PostprocManager.h:51
uint32_t m_UnscaledHeight
Definition: PostprocManager.h:189
void ResolveMultisampleFramebuffer(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: PostprocManager.cpp:921
uint32_t m_Height
Definition: PostprocManager.h:188
void ApplyBlur(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: PostprocManager.cpp:407
std::unique_ptr< Renderer::Backend::IFramebuffer > m_MultisampleFramebuffer
Definition: PostprocManager.h:181
void UpdateSharpnessFactor()
Definition: PostprocManager.cpp:828
CShaderTechniquePtr m_UpscaleTech
Definition: PostprocManager.h:172
void RecalculateSize(const uint32_t width, const uint32_t height)
Definition: PostprocManager.cpp:932
void ApplyBlurDownscale2x(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, Renderer::Backend::IFramebuffer *framebuffer, Renderer::Backend::ITexture *inTex, int inWidth, int inHeight)
Definition: PostprocManager.cpp:316
bool m_WhichBuffer
Definition: PostprocManager.h:159
Renderer::Backend::IFramebuffer * PrepareAndGetOutputFramebuffer()
Definition: PostprocManager.cpp:425
bool ShouldUpscale() const
Definition: PostprocManager.cpp:951
bool ShouldDownscale() const
Definition: PostprocManager.cpp:956
std::unique_ptr< Renderer::Backend::ITexture > m_DepthTex
Definition: PostprocManager.h:143
CShaderTechniquePtr m_UpscaleComputeTech
Definition: PostprocManager.h:173
void BlitOutputFramebuffer(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, Renderer::Backend::IFramebuffer *destination)
Definition: PostprocManager.cpp:552
std::unique_ptr< Renderer::Backend::ITexture > m_MultisampleColorTex
Definition: PostprocManager.h:183
CStr m_SharpName
Definition: PostprocManager.h:168
void CreateMultisampleBuffer()
Definition: PostprocManager.cpp:859
std::unique_ptr< Renderer::Backend::IFramebuffer > m_UnscaledFramebuffer1
Definition: PostprocManager.h:139
void Initialize()
Definition: PostprocManager.cpp:132
float m_Scale
Definition: PostprocManager.h:140
void ApplyPostproc(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: PostprocManager.cpp:685
std::unique_ptr< Renderer::Backend::ITexture > m_MultisampleDepthTex
Definition: PostprocManager.h:183
bool m_IsInitialized
Definition: PostprocManager.h:192
void SetPostEffect(const CStrW &name)
Definition: PostprocManager.cpp:746
uint32_t m_UnscaledWidth
Definition: PostprocManager.h:189
CStrW m_PostProcEffect
Definition: PostprocManager.h:165
Implements a render technique consisting of a sequence of passes.
Definition: ShaderTechnique.h:60
Definition: IDeviceCommandContext.h:42
Definition: IDevice.h:48
IFramebuffer stores attachments which should be used by backend as rendering destinations.
Definition: IFramebuffer.h:85
Definition: ITexture.h:34
IVertexInputLayout stores precompiled list of vertex attributes.
Definition: IShaderProgram.h:74
Definition: PostprocManager.h:150
std::unique_ptr< Renderer::Backend::ITexture > texture
Definition: PostprocManager.h:152
std::unique_ptr< Renderer::Backend::IFramebuffer > framebuffer
Definition: PostprocManager.h:151
Definition: PostprocManager.h:148
std::array< Step, 2 > steps
Definition: PostprocManager.h:154
unsigned int uint32_t
Definition: wposix_types.h:53