AntiAliasing.cc 9.99 KB
Newer Older
1
2
3
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
Jan Möbius's avatar
Jan Möbius committed
4
5
6
7
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
8
9
 *                                                                           *
 *---------------------------------------------------------------------------*
Jan Möbius's avatar
Jan Möbius committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision: 18130 $                                                       *
 *   $Author: moebius $                                                      *
 *   $Date: 2014-02-05 10:41:16 +0100 (Wed, 05 Feb 2014) $                   *
 *                                                                           *
\*===========================================================================*/



//=============================================================================
//
54
//  OpenGL AntiAliasing methods
55
56
57
58
59
60
//
//=============================================================================


//== INCLUDES =================================================================

61
#include <ACG/GL/acg_glew.hh>
62
#include <ACG/GL/AntiAliasing.hh>
Christopher Tenter's avatar
Christopher Tenter committed
63
64
65
#include <ACG/ShaderUtils/GLSLShader.hh>
#include <ACG/GL/ScreenQuad.hh>

66
#include <ACG/GL/ShaderCache.hh>
Christopher Tenter's avatar
Christopher Tenter committed
67
68
69
70
71
72

//== DEFINES ==================================================================

// shader files
#define MSAA_SCREENQUAD_SHADER "ScreenQuad/screenquad.glsl"
#define MSAA_NEAREST_SHADER "MSAA/sample_nearest.glsl"
73
#define MSAA_NEAREST_DEPTH_SHADER "MSAA/sample_nearest_and_depth.glsl"
Christopher Tenter's avatar
Christopher Tenter committed
74
75
76
#define MSAA_LINEAR_SHADER "MSAA/sample_linear.glsl"


77
78
79
80
81
82

//== NAMESPACES ===============================================================

namespace ACG {


Christopher Tenter's avatar
Christopher Tenter committed
83
//== CLASS IMPLEMENTATION =====================================================
84

85
86
#ifdef GL_ARB_texture_multisample

87
88
89
90
91
92
93
94
95
96

MSFilterWeights::MSFilterWeights(int _numSamples) : numSamples_(_numSamples) {

  weights_.resize(_numSamples);
  float sumWeights = 0.0f;

  // texel center is at (0.5, 0.5) as specified in 
  //  http://www.opengl.org/sdk/docs/man3/xhtml/glGetMultisample.xml
  Vec2f texelCenter(0.5f, 0.5f);

97
98
99
100
101
  // query sample count of currently bound fbo to avoid error on calling glGetMultisample
  int fboSamples = 0;
  glGetIntegerv(GL_SAMPLES, &fboSamples);


102
103
  for (int i = 0; i < _numSamples; ++i) {
    GLfloat val[2];
104
105
106
107
108

    if (i < fboSamples)
      glGetMultisamplefv(GL_SAMPLE_POSITION, i, val);
    else
      val[0] = val[1] = 0.5f; // maybe output warning here
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
    
    Vec2f samplePosition(val[0], val[1]);

    // weighting based on distance to texel center
    float sampleDist = (samplePosition - texelCenter).norm();

    // samples close to the center are weighted higher than samples farther away
    weights_[i] = 1.0f - sampleDist;

    sumWeights += weights_[i];
  }

  // normalize weights

  for (int i = 0; i < _numSamples; ++i)
    weights_[i] /= sumWeights;
}

//=============================================================================

129
130
#if defined(GL_ARB_texture_buffer_object)
    
131
132
133
134
void MSFilterWeights::asTextureBuffer( TextureBuffer& out ) {
  if (numSamples_)
    out.setBufferData(numSamples_ * 4, &weights_[0], GL_R32F, GL_STATIC_DRAW);
}
135
#endif
136
137
138

//=============================================================================

Christopher Tenter's avatar
Christopher Tenter committed
139
140
141
142
143
144



//=============================================================================


145
MSTextureSampler::MSTextureSampler() : shaderNearest_(0), shaderNearestDepth_(0), shaderLinear_(0) {
Christopher Tenter's avatar
Christopher Tenter committed
146
147
148
149
150
151
}


MSTextureSampler::~MSTextureSampler() {
  delete shaderNearest_;
  delete shaderLinear_;
152
  delete shaderNearestDepth_;
Christopher Tenter's avatar
Christopher Tenter committed
153
154
155
156
157
158
159
160
161
162
163
164
165
}

MSTextureSampler& MSTextureSampler::instance() {
  static MSTextureSampler singleton;
  return singleton;
}

//=============================================================================

void MSTextureSampler::init() {
  if (!shaderNearest_)
    shaderNearest_ = GLSL::loadProgram(MSAA_SCREENQUAD_SHADER, MSAA_NEAREST_SHADER);

166
167
168
  if (!shaderNearestDepth_)
    shaderNearestDepth_ = GLSL::loadProgram(MSAA_SCREENQUAD_SHADER, MSAA_NEAREST_DEPTH_SHADER);

Christopher Tenter's avatar
Christopher Tenter committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
  if (!shaderLinear_)
    shaderLinear_ = GLSL::loadProgram(MSAA_SCREENQUAD_SHADER, MSAA_LINEAR_SHADER);
}

//=============================================================================

void MSTextureSampler::filterMSAATexture_Nearest( GLuint _texture, int _samples, const float* _weights /*= 0*/ ) {

  MSTextureSampler& sampler = instance();

  // load shader
  if (!sampler.shaderNearest_)
    sampler.init();

  GLSL::Program* shader = sampler.shaderNearest_;

  if (!shader)
    return;
  

  shader->use();

  // offset and scale of screenquad
  shader->setUniform("offset", Vec2f(0.0f, 0.0f));
  shader->setUniform("size", Vec2f(1.0f, 1.0f));
  
  // sample count and filter weights
  shader->setUniform("numSamples", _samples);

  // bind multisampled texture to slot 0
  shader->setUniform("inputTex", 0);
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, _texture);

  // run texture filter
  ScreenQuad::draw(shader);

  shader->disable();
}

//=============================================================================
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251

void MSTextureSampler::filterMSAATexture_Nearest( GLuint _texture, GLuint _depthTexture, int _samples, const float* _weights /*= 0*/ ) {

  MSTextureSampler& sampler = instance();

  // load shader
  if (!sampler.shaderNearestDepth_)
    sampler.init();

//  GLSL::Program* shader = sampler.shaderNearestDepth_;
  GLSL::Program* shader = ACG::ShaderCache::getInstance()->getProgram(MSAA_SCREENQUAD_SHADER, MSAA_NEAREST_DEPTH_SHADER);

  if (!shader)
    return;


  shader->use();

  // offset and scale of screenquad
  shader->setUniform("offset", Vec2f(0.0f, 0.0f));
  shader->setUniform("size", Vec2f(1.0f, 1.0f));

  // sample count and filter weights
  shader->setUniform("numSamples", _samples);

  // bind multisampled depth texture to slot 1
  shader->setUniform("inputDepthTex", 1);
  glActiveTexture(GL_TEXTURE1);
  glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, _depthTexture);

  // bind multisampled texture to slot 0
  shader->setUniform("inputTex", 0);
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, _texture);

  // run texture filter
  ScreenQuad::draw(shader);

  shader->disable();
}

//=============================================================================
Christopher Tenter's avatar
Christopher Tenter committed
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291

void MSTextureSampler::filterMSAATexture_Linear( GLuint _texture, int _samples, const float* _weights /*= 0*/ ) {

  MSTextureSampler& sampler = instance();

  // load shader
  if (!sampler.shaderLinear_)
    sampler.init();

  GLSL::Program* shader = sampler.shaderLinear_;

  if (!shader)
    return;


  shader->use();

  // offset and scale of screenquad
  shader->setUniform("offset", Vec2f(0.0f, 0.0f));
  shader->setUniform("size", Vec2f(1.0f, 1.0f));

  // sample count and filter weights
  shader->setUniform("numSamples", _samples);

  // bind multisampled texture to slot 0
  shader->setUniform("inputTex", 0);
  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, _texture);

  // run texture filter
  ScreenQuad::draw(shader);

  shader->disable();
}


//=============================================================================



292
#endif // GL_ARB_texture_multisample
293
294
295
296


} // namespace ACG
//=============================================================================