Merge 'remotes/trunk'
[0ad.git] / source / renderer / PostprocManager.h
blobf554eabd0e84f1f80fc10eb288a300ba9c91728e
1 /* Copyright (C) 2015 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #ifndef INCLUDED_POSTPROCMANAGER
19 #define INCLUDED_POSTPROCMANAGER
22 #include "graphics/ShaderTechnique.h"
24 class CPostprocManager
26 private:
28 // Two framebuffers, that we flip between at each shader pass.
29 GLuint m_PingFbo, m_PongFbo;
31 // Unique color textures for the framebuffers.
32 GLuint m_ColorTex1, m_ColorTex2;
34 // The framebuffers share a depth/stencil texture.
35 GLuint m_DepthTex;
37 // A framebuffer and textures x2 for each blur level we render.
38 GLuint m_BloomFbo, m_BlurTex2a, m_BlurTex2b, m_BlurTex4a, m_BlurTex4b, m_BlurTex8a, m_BlurTex8b;
40 // Indicates which of the ping-pong buffers is used for reading and which for drawing.
41 bool m_WhichBuffer;
43 // The name and shader technique we are using. "default" name means no technique is used
44 // (i.e. while we do allocate the buffers, no effects are rendered).
45 CStrW m_PostProcEffect;
46 CShaderTechniquePtr m_PostProcTech;
48 // The current screen dimensions in pixels.
49 int m_Width, m_Height;
51 // Is the postproc manager initialized? Buffers created? Default effect loaded?
52 bool m_IsInitialized;
54 // Creates blur textures at various scales, for bloom, DOF, etc.
55 void ApplyBlur();
57 // High quality GPU image scaling to half size. outTex must have exactly half the size
58 // of inTex. inWidth and inHeight are the dimensions of inTex in texels.
59 void ApplyBlurDownscale2x(GLuint inTex, GLuint outTex, int inWidth, int inHeight);
61 // GPU-based Gaussian blur in two passes. inOutTex contains the input image and will be filled
62 // with the blurred image. tempTex must have the same size as inOutTex.
63 // inWidth and inHeight are the dimensions of the images in texels.
64 void ApplyBlurGauss(GLuint inOutTex, GLuint tempTex, int inWidth, int inHeight);
66 // Applies a pass of a given effect to the entire current framebuffer. The shader is
67 // provided with a number of general-purpose variables, including the rendered screen so far,
68 // the depth buffer, a number of blur textures, the screen size, the zNear/zFar planes and
69 // some other parameters used by the optional bloom/HDR pass.
70 void ApplyEffect(CShaderTechniquePtr &shaderTech1, int pass);
72 // Delete all allocated buffers/textures from GPU memory.
73 void Cleanup();
75 // Delete existing buffers/textures and create them again, using a new screen size if needed.
76 // (the textures are also attached to the framebuffers)
77 void RecreateBuffers();
79 public:
80 CPostprocManager();
81 ~CPostprocManager();
83 // Create all buffers/textures in GPU memory and set default effect.
84 // @note Must be called before using in the renderer. May be called multiple times.
85 void Initialize();
87 // Update the size of the screen
88 void Resize();
90 // Returns a list of xml files found in shaders/effects/postproc.
91 static std::vector<CStrW> GetPostEffects();
93 // Returns the name of the current effect.
94 inline const CStrW& GetPostEffect() const
96 return m_PostProcEffect;
99 // Sets the current effect.
100 void SetPostEffect(const CStrW& name);
102 // Clears the two color buffers and depth buffer, and redirects all rendering
103 // to our textures instead of directly to the system framebuffer.
104 // @note CPostprocManager must be initialized first
105 void CaptureRenderOutput();
107 // First renders blur textures, then calls ApplyEffect for each effect pass,
108 // ping-ponging the buffers at each step.
109 // @note CPostprocManager must be initialized first
110 void ApplyPostproc();
112 // Blits the final postprocessed texture to the system framebuffer. The system framebuffer
113 // is selected as the output buffer. Should be called before silhouette rendering.
114 // @note CPostprocManager must be initialized first
115 void ReleaseRenderOutput();
119 #endif //INCLUDED_POSTPROCMANAGER