Pyrogenesis  trunk
PostprocManager.h
Go to the documentation of this file.
1 /* Copyright (C) 2023 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 {
33 public:
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 FBO if needed.
62  void UpdateSharpnessFactor();
63 
64  void SetDepthBufferClipPlanes(float nearPlane, float farPlane);
65 
66  // @note CPostprocManager must be initialized first
68 
69  // First renders blur textures, then calls ApplyEffect for each effect pass,
70  // ping-ponging the buffers at each step.
71  // @note CPostprocManager must be initialized first
72  void ApplyPostproc(
73  Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
74 
75  // Blits the final postprocessed texture to the system framebuffer. The system
76  // framebuffer is selected as the output buffer. Should be called before
77  // silhouette rendering.
78  // @note CPostprocManager must be initialized first
80  Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
81  Renderer::Backend::IFramebuffer* destination);
82 
83  // Returns true if we render main scene in the MSAA framebuffer.
84  bool IsMultisampleEnabled() const;
85 
86  // Resolves the MSAA framebuffer into the regular one.
88  Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
89 
90 private:
93 
94  std::unique_ptr<Renderer::Backend::IFramebuffer> m_CaptureFramebuffer;
95 
96  // Two framebuffers, that we flip between at each shader pass.
97  std::unique_ptr<Renderer::Backend::IFramebuffer>
99 
100  // Unique color textures for the framebuffers.
101  std::unique_ptr<Renderer::Backend::ITexture> m_ColorTex1, m_ColorTex2;
102 
103  // The framebuffers share a depth/stencil texture.
104  std::unique_ptr<Renderer::Backend::ITexture> m_DepthTex;
106 
107  // A framebuffer and textures x2 for each blur level we render.
108  struct BlurScale
109  {
110  struct Step
111  {
112  std::unique_ptr<Renderer::Backend::IFramebuffer> framebuffer;
113  std::unique_ptr<Renderer::Backend::ITexture> texture;
114  };
115  std::array<Step, 2> steps;
116  };
117  std::array<BlurScale, 3> m_BlurScales;
118 
119  // Indicates which of the ping-pong buffers is used for reading and which for drawing.
121 
123 
124  // The name and shader technique we are using. "default" name means no technique is used
125  // (i.e. while we do allocate the buffers, no effects are rendered).
128 
131  float m_Sharpness;
132 
133  CStr m_AAName;
136  std::unique_ptr<Renderer::Backend::IFramebuffer> m_MultisampleFramebuffer;
137  std::unique_ptr<Renderer::Backend::ITexture>
140  std::vector<uint32_t> m_AllowedSampleCounts;
141 
142  // The current screen dimensions in pixels.
144 
145  // Is the postproc manager initialized? Buffers created? Default effect loaded?
147 
148  // Creates blur textures at various scales, for bloom, DOF, etc.
149  void ApplyBlur(
150  Renderer::Backend::IDeviceCommandContext* deviceCommandContext);
151 
152  // High quality GPU image scaling to half size. outTex must have exactly half the size
153  // of inTex. inWidth and inHeight are the dimensions of inTex in texels.
155  Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
158  int inWidth, int inHeight);
159 
160  // GPU-based Gaussian blur in two passes. inOutTex contains the input image and will be filled
161  // with the blurred image. tempTex must have the same size as inOutTex.
162  // inWidth and inHeight are the dimensions of the images in texels.
163  void ApplyBlurGauss(
164  Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
167  Renderer::Backend::IFramebuffer* tempFramebuffer,
168  Renderer::Backend::IFramebuffer* outFramebuffer,
169  int inWidth, int inHeight);
170 
171  // Applies a pass of a given effect to the entire current framebuffer. The shader is
172  // provided with a number of general-purpose variables, including the rendered screen so far,
173  // the depth buffer, a number of blur textures, the screen size, the zNear/zFar planes and
174  // some other parameters used by the optional bloom/HDR pass.
175  void ApplyEffect(
176  Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
177  const CShaderTechniquePtr& shaderTech, int pass);
178 
179  // Delete all allocated buffers/textures from GPU memory.
180  void Cleanup();
181 
182  // Delete existing buffers/textures and create them again, using a new screen size if needed.
183  // (the textures are also attached to the framebuffers)
184  void RecreateBuffers();
185 };
186 
187 #endif // INCLUDED_POSTPROCMANAGER
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:310
void UpdateAntiAliasingTechnique()
Definition: PostprocManager.cpp:542
void BlitOutputFramebuffer(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, Renderer::Backend::IFramebuffer *destination)
Definition: PostprocManager.cpp:399
std::unique_ptr< Renderer::Backend::IFramebuffer > m_PongFramebuffer
Definition: PostprocManager.h:98
std::unique_ptr< Renderer::Backend::ITexture > m_ColorTex1
Definition: PostprocManager.h:101
bool IsEnabled() const
Definition: PostprocManager.cpp:96
CShaderTechniquePtr m_PostProcTech
Definition: PostprocManager.h:127
Definition: ITexture.h:33
std::unique_ptr< Renderer::Backend::IFramebuffer > m_CaptureFramebuffer
Definition: PostprocManager.h:94
void ResolveMultisampleFramebuffer(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: PostprocManager.cpp:686
std::unique_ptr< Renderer::Backend::ITexture > m_DepthTex
Definition: PostprocManager.h:104
std::vector< uint32_t > m_AllowedSampleCounts
Definition: PostprocManager.h:140
IVertexInputLayout stores precompiled list of vertex attributes.
Definition: IShaderProgram.h:73
Definition: PostprocManager.h:108
const CStrW & GetPostEffect() const
Definition: PostprocManager.h:51
void Initialize()
Definition: PostprocManager.cpp:133
std::unique_ptr< Renderer::Backend::ITexture > texture
Definition: PostprocManager.h:113
CStr m_SharpName
Definition: PostprocManager.h:129
bool m_IsInitialized
Definition: PostprocManager.h:146
static std::vector< CStrW > GetPostEffects()
Definition: PostprocManager.cpp:506
void ApplyPostproc(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: PostprocManager.cpp:467
std::unique_ptr< Renderer::Backend::IFramebuffer > m_MultisampleFramebuffer
Definition: PostprocManager.h:136
float m_NearPlane
Definition: PostprocManager.h:105
bool IsMultisampleEnabled() const
Definition: PostprocManager.cpp:681
uint32_t m_MultisampleCount
Definition: PostprocManager.h:139
CPostprocManager()
Definition: PostprocManager.cpp:85
void ApplyEffect(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, const CShaderTechniquePtr &shaderTech, int pass)
Definition: PostprocManager.cpp:412
std::unique_ptr< Renderer::Backend::IFramebuffer > framebuffer
Definition: PostprocManager.h:112
std::unique_ptr< Renderer::Backend::ITexture > m_MultisampleDepthTex
Definition: PostprocManager.h:138
Renderer::Backend::IFramebuffer * PrepareAndGetOutputFramebuffer()
Definition: PostprocManager.cpp:388
int m_Width
Definition: PostprocManager.h:143
int m_Height
Definition: PostprocManager.h:143
void UpdateSharpeningTechnique()
Definition: PostprocManager.cpp:593
void Resize()
Definition: PostprocManager.cpp:171
std::unique_ptr< Renderer::Backend::IFramebuffer > m_PingFramebuffer
Definition: PostprocManager.h:98
float m_Sharpness
Definition: PostprocManager.h:131
std::array< BlurScale, 3 > m_BlurScales
Definition: PostprocManager.h:117
bool m_WhichBuffer
Definition: PostprocManager.h:120
void CreateMultisampleBuffer()
Definition: PostprocManager.cpp:622
void UpdateSharpnessFactor()
Definition: PostprocManager.cpp:611
float m_FarPlane
Definition: PostprocManager.h:105
void ApplyBlur(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: PostprocManager.cpp:370
unsigned int uint32_t
Definition: wposix_types.h:53
bool m_UsingMultisampleBuffer
Definition: PostprocManager.h:135
void SetDepthBufferClipPlanes(float nearPlane, float farPlane)
Definition: PostprocManager.cpp:616
~CPostprocManager()
Definition: PostprocManager.cpp:91
Definition: PostprocManager.h:31
std::unique_ptr< Renderer::Backend::ITexture > m_MultisampleColorTex
Definition: PostprocManager.h:138
std::shared_ptr< CShaderTechnique > CShaderTechniquePtr
Definition: ShaderTechniquePtr.h:27
CShaderTechniquePtr m_AATech
Definition: PostprocManager.h:134
Renderer::Backend::IVertexInputLayout * m_VertexInputLayout
Definition: PostprocManager.h:122
void RecreateBuffers()
Definition: PostprocManager.cpp:181
void SetPostEffect(const CStrW &name)
Definition: PostprocManager.cpp:528
void Cleanup()
Definition: PostprocManager.cpp:109
Definition: IDeviceCommandContext.h:40
IFramebuffer stores attachments which should be used by backend as rendering destinations.
Definition: IFramebuffer.h:84
void ApplyBlurDownscale2x(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, Renderer::Backend::IFramebuffer *framebuffer, Renderer::Backend::ITexture *inTex, int inWidth, int inHeight)
Definition: PostprocManager.cpp:279
std::array< Step, 2 > steps
Definition: PostprocManager.h:115
std::unique_ptr< Renderer::Backend::ITexture > m_ColorTex2
Definition: PostprocManager.h:101
CStr m_AAName
Definition: PostprocManager.h:133
void DestroyMultisampleBuffer()
Definition: PostprocManager.cpp:672
Definition: PostprocManager.h:110
CStrW m_PostProcEffect
Definition: PostprocManager.h:126
CShaderTechniquePtr m_SharpTech
Definition: PostprocManager.h:130