Merge 'remotes/trunk'
[0ad.git] / source / renderer / PostprocManager.h
blob3c3d090ad84312d0db41ae9de21ae3f067d036c2
1 /* Copyright (C) 2012 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 colour textures for the framebuffers.
32 GLuint m_ColourTex1, m_ColourTex2;
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 initialised? Buffers created? Default effect loaded?
52 bool m_IsInitialised;
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 public:
73 CPostprocManager();
74 ~CPostprocManager();
76 // Create all buffers/textures in GPU memory and set default effect.
77 void Initialize();
79 // Delete all allocated buffers/textures from GPU memory.
80 void Cleanup();
82 // Delete existing buffers/textures and create them again, using a new screen size if needed.
83 // (the textures are also attached to the framebuffers)
84 void RecreateBuffers();
86 // Loads a new postproc effect. "default" effect does NOT load anything.
87 void LoadEffect(CStrW &name);
89 // Returns a list of xml files found in shaders/effects/postproc.
90 std::vector<CStrW> GetPostEffects() const;
92 // Returns the name of the current effect.
93 inline const CStrW& GetPostEffect() const
95 return m_PostProcEffect;
98 // Matching setter that calls LoadEffect.
99 void SetPostEffect(CStrW name);
101 // Clears the two colour buffers and depth buffer, and redirects all rendering
102 // to our textures instead of directly to the system framebuffer.
103 void CaptureRenderOutput();
105 // First renders blur textures, then calls ApplyEffect for each effect pass,
106 // ping-ponging the buffers at each step.
107 void ApplyPostproc();
109 // Blits the final postprocessed texture to the system framebuffer. The system framebuffer
110 // is selected as the output buffer. Should be called before silhouette rendering.
111 void ReleaseRenderOutput();
115 #endif //INCLUDED_POSTPROCMANAGER