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_POSTPROCMANAGER
19 : #define INCLUDED_POSTPROCMANAGER
20 :
21 : #include "graphics/ShaderTechniquePtr.h"
22 : #include "ps/CStr.h"
23 : #include "renderer/backend/IFramebuffer.h"
24 : #include "renderer/backend/ITexture.h"
25 :
26 : #include <array>
27 : #include <cstdint>
28 : #include <memory>
29 : #include <vector>
30 :
31 : class CShaderTechnique;
32 : namespace Renderer::Backend { class IDevice; }
33 : namespace Renderer::Backend { class IDeviceCommandContext; }
34 : namespace Renderer::Backend { class IVertexInputLayout; }
35 :
36 : class CPostprocManager
37 : {
38 : public:
39 : CPostprocManager(Renderer::Backend::IDevice* device);
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);
52 : void DestroyViewSizeDependentObjects();
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 0 : 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.
67 : void UpdateAntiAliasingTechnique();
68 : void UpdateSharpeningTechnique();
69 : void UpdateSharpnessFactor();
70 : void SetUpscaleTechnique(const CStr& upscaleName);
71 :
72 : void SetDepthBufferClipPlanes(float nearPlane, float farPlane);
73 :
74 : // @note CPostprocManager must be initialized first
75 : Renderer::Backend::IFramebuffer* PrepareAndGetOutputFramebuffer();
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
87 : void BlitOutputFramebuffer(
88 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
89 : Renderer::Backend::IFramebuffer* destination);
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.
95 : void ResolveMultisampleFramebuffer(
96 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
97 :
98 0 : float GetScale() const { return m_Scale; }
99 :
100 : bool IsPBREnabled() const { return m_PBREnabled; }
101 :
102 0 : Renderer::Backend::Format GetFramebufferColorFormat() const { return m_FramebufferColorFormat; }
103 :
104 : private:
105 : void CreateMultisampleBuffer();
106 : void DestroyMultisampleBuffer();
107 : void RecreateCaptureFramebuffer();
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 :
116 : void UpscaleTextureByCompute(
117 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
118 : CShaderTechnique* shaderTechnique,
119 : Renderer::Backend::ITexture* source,
120 : Renderer::Backend::ITexture* destination);
121 : void UpscaleTextureByFullscreenQuad(
122 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
123 : CShaderTechnique* shaderTechnique,
124 : Renderer::Backend::ITexture* source,
125 : Renderer::Backend::IFramebuffer* destination);
126 :
127 : void ApplySharpnessAfterScale(
128 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
129 : CShaderTechnique* shaderTechnique,
130 : Renderer::Backend::ITexture* source,
131 : Renderer::Backend::ITexture* destination);
132 :
133 : void DownscaleTextureByCompute(
134 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
135 : CShaderTechnique* shaderTechnique,
136 : Renderer::Backend::ITexture* source,
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 :
145 : Renderer::Backend::IDevice* m_Device = nullptr;
146 :
147 : Renderer::Backend::Format m_FramebufferColorFormat{
148 : Renderer::Backend::Format::R8G8B8A8_UNORM};
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>
157 : m_PingFramebuffer, m_PongFramebuffer;
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>
163 : m_UnscaledTexture1, m_UnscaledTexture2;
164 : std::unique_ptr<Renderer::Backend::IFramebuffer>
165 : m_UnscaledFramebuffer1, m_UnscaledFramebuffer2;
166 : float m_Scale = 1.0f;
167 :
168 : // The framebuffers share a depth/stencil texture.
169 : std::unique_ptr<Renderer::Backend::ITexture> m_DepthTex;
170 : float m_NearPlane, m_FarPlane;
171 :
172 : // A framebuffer and textures x2 for each blur level we render.
173 0 : struct BlurScale
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.
185 : bool m_WhichBuffer;
186 :
187 : Renderer::Backend::IVertexInputLayout* m_VertexInputLayout = nullptr;
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).
191 : CStrW m_PostProcEffect;
192 : CShaderTechniquePtr m_PostProcTech;
193 :
194 : CShaderTechniquePtr m_ResolvePBRTech;
195 : CShaderTechniquePtr m_ResolvePBRComputeTech;
196 :
197 : CStr m_SharpName;
198 : CShaderTechniquePtr m_SharpTech;
199 : float m_Sharpness;
200 :
201 : CShaderTechniquePtr m_UpscaleTech;
202 : CShaderTechniquePtr m_UpscaleComputeTech;
203 : CShaderTechniquePtr m_DownscaleComputeTech;
204 : // Sharp technique only for FSR upscale.
205 : CShaderTechniquePtr m_RCASComputeTech;
206 :
207 : CStr m_AAName;
208 : CShaderTechniquePtr m_AATech;
209 : bool m_UsingMultisampleBuffer;
210 : std::unique_ptr<Renderer::Backend::IFramebuffer> m_MultisampleFramebuffer;
211 : std::unique_ptr<Renderer::Backend::ITexture>
212 : m_MultisampleColorTex, m_MultisampleDepthTex;
213 : uint32_t m_MultisampleCount;
214 : std::vector<uint32_t> m_AllowedSampleCounts;
215 :
216 : // The current screen dimensions in pixels.
217 : uint32_t m_Width, m_Height;
218 : uint32_t m_UnscaledWidth, m_UnscaledHeight;
219 :
220 : // Is the postproc manager initialized? Buffers created? Default effect loaded?
221 : bool m_IsInitialized;
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.
229 : void ApplyBlurDownscale2x(
230 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
231 : Renderer::Backend::IFramebuffer* framebuffer,
232 : Renderer::Backend::ITexture* inTex,
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,
240 : Renderer::Backend::ITexture* inTex,
241 : Renderer::Backend::ITexture* tempTex,
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
|