Developer Documentation
AntiAliasing.hh
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 //=============================================================================
43 //
44 // Helper classes for Anti-Aliasing of render targets
45 //
46 //=============================================================================
47 
48 
49 #ifndef ANTI_ALIASING_HH
50 #define ANTI_ALIASING_HH
51 
52 
53 //== INCLUDES =================================================================
54 
55 // GL
56 #include <ACG/GL/acg_glew.hh>
57 #include <ACG/GL/globjects.hh>
58 #include <ACG/GL/gl.hh>
59 
60 // C++
61 #include <vector>
62 
63 
64 //== FORWARD DECLARATIONS =====================================================
65 
66 namespace GLSL{
67  class Program;
68 }
69 
70 
71 //== NAMESPACES ===============================================================
72 
73 namespace ACG {
74 
75 
76 //== CLASS DEFINITION =========================================================
77 
78 
79 /*
80 OpenGL MSAA:
81 
82 A post-processing fragment shader has to compute colors per sample rather than per pixel for full MSAA.
83 Sample values are then weighted by filter weights to obtain a final pixel color.
84 
85 Filter weights can be computed with several methods, for example:
86  - distance based filter weights (computed with MSFilterWeights)
87  - constant filter weights: 1/numSamples
88 
89 However, a faster approach can be used where the MSAA texture is filtered onto a regular texture first,
90 followed by executing post processing shaders on that regular texture.
91 While this is not a correct MSAA implementation, it can give something close at better performance.
92 -> MSTextureSampler can be used to filter a MSAA texture onto a regular texture
93 
94 Recommended resources on implementing MSAA with render to texture:
95  http://diaryofagraphicsprogrammer.blogspot.com/2009/06/multisample-anti-aliasing.html
96  http://www.humus.name/index.php?page=3D&ID=81
97 */
98 
99 #ifdef GL_ARB_texture_multisample
100 #define MSFILTERWEIGHTS
101 
102 class ACGDLLEXPORT MSFilterWeights
103 {
104 public:
105 
106  MSFilterWeights(int _numSamples);
107 
108  virtual ~MSFilterWeights() { }
109 
110 
111  // Initializes a texture buffer with filter weights in format GL_R32F.
112  // This can be used for reading from multisampled textures in a shader.
113  void asTextureBuffer(TextureBuffer& out);
114 
115 
116  // get ptr to weights as array, can be used for setUniform() for example
117  const float* asDataPtr() const {return &weights_[0];}
118 
119 
120  float operator [] (int i) const {return weights_[i];}
121 
122  float getWeight(int i) const {return weights_[i];}
123 
124  int getNumSamples() const {return numSamples_;}
125 
126 private:
127 
128  int numSamples_;
129  std::vector<float> weights_;
130 };
131 
132 #endif // GL_ARB_texture_multisample
133 
134 
135 
136 
137 //== CLASS DEFINITION =========================================================
138 
139 
140 // This class performs multisampling on MSAA textures and writes the result to the currently bound FBO.
141 #ifdef GL_ARB_texture_multisample
142 #define MSTEXTURESAMPLER
143 
144 class ACGDLLEXPORT MSTextureSampler
145 {
146 public:
147 
148  ~MSTextureSampler();
149 
150 
151  // nearest point filtering of a MSAA texture, recommended when the target FBO has the same size as the input texture
152  static void filterMSAATexture_Nearest(GLuint _texture, int _samples, const float* _weights = 0);
153 
154  // nearest point filtering of a MSAA texture, recommended when the target FBO has the same size as the input texture
155  // also resolves a multisampled depth texture into the depth target
156  static void filterMSAATexture_Nearest(GLuint _texture, GLuint _depthTexture, int _samples, const float* _weights = 0);
157 
158  // bilinear filtering of a MSAA texture
159  static void filterMSAATexture_Linear(GLuint _texture, int _samples, const float* _weights = 0);
160 
161 private:
162 
163  MSTextureSampler();
164 
165 
166  void init();
167 
168  // singleton instance
169  static MSTextureSampler& instance();
170 
171 
172  TextureBuffer filterWeights_;
173 
174  // sampling shaders for nearest and bilinear texture filtering
175  GLSL::Program* shaderNearest_;
176  GLSL::Program* shaderNearestDepth_;
177  GLSL::Program* shaderLinear_;
178 };
179 
180 #endif // GL_ARB_texture_multisample
181 
182 
183 
184 // maybe add:
185 // - blitting of multisampled render buffers
186 // - automatic generation of MSAA variants of a post processing fragment shader
187 // - downsampling of supersampled FBOs
188 
189 //=============================================================================
190 } // namespace ACG
191 //=============================================================================
192 #endif // ANTI_ALIASING_HH defined
193 //=============================================================================
This namespace contains all the classes and functions for handling GLSL shader and program objects...
Definition: AntiAliasing.hh:66
Namespace providing different geometric functions concerning angles.
GLSL program class.
Definition: GLSLShader.hh:211