Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PostProcessing.cc
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 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 
51 #include <ACG/GL/acg_glew.hh>
52 #include <ACG/GL/GLError.hh>
53 #include <ACG/GL/AntiAliasing.hh>
54 
55 #include <OpenFlipper/common/RendererInfo.hh>
56 
57 
58 #include "PostProcessing.hh"
59 
60 
61 
62 
63 PostProcessing::PostProcessing()
64  : debugLevel_(10), backbufferFBO_(0), backbufferTarget_(GL_BACK), stereoMode_(false)
65 {
66  backbufferViewport_[0] = backbufferViewport_[1] = backbufferViewport_[2] = backbufferViewport_[3] = 0;
67 }
68 
69 
70 PostProcessing::~PostProcessing()
71 {
72 
73 }
74 
75 
76 int PostProcessing::setupScene( int _viewerID, int _width, int _height, int _samples /*= 0*/, int _stereoEye /*= -1*/ )
77 {
78  /*
79  try to keep memory usage minimal
80  -> prefer unorm over fp format
81  */
82 
83  const int numProcs = postProcessorManager().numActive(_viewerID);
84 
85  if (!numProcs && _stereoEye < 0)
86  return -1; // nothing to do
87 
88  /* create enough textures for the postprocessing chain:
89 
90  - Scene Rendering pass: 1 FBO, maybe multisampled
91  - Resolve MSAA: 1 FBO, non msaa target, post processor input, can be skipped if multisampling is disabled
92  - 1st PostProc:
93  if no more following PostProcs, write to hw fbo
94  otherwise, write to temp fbo
95 
96  - 2nd PostProc:
97  write to
98 
99 
100  At least 2 FBO's required, 3 for multiple PostProcs.
101 
102  */
103 
104 
105  // All render targets have the same format for now.
106  // If one PostProc requires floating point, then all PostProcs work on fp data.
107 
108  // Search for a post proc that requires floating pt.
109  bool requiresFP = false;
110 
111  for (int i = 0; i < numProcs && !requiresFP; ++i)
112  {
113  PostProcessorInfo* pi = postProcessorManager().active(_viewerID, i);
114 
115  if (pi && pi->plugin)
116  {
118  fmt.outputFormat_ = PostProcessorFormatDesc::PostProcessorFormat_DONTCARE;
119  pi->plugin->getFormatDesc(&fmt);
120 
121  if (!fmt.inputFormats_.empty())
122  {
123  size_t numInputs = fmt.inputFormats_.size();
124 
125  for (size_t k = 0; k < numInputs; ++k)
126  {
127  if (fmt.inputFormats_[k] == PostProcessorFormatDesc::PostProcessorFormat_FLOAT)
128  {
129  requiresFP = true;
130  break;
131  }
132  }
133  }
134 
135  if (fmt.outputFormat_ == PostProcessorFormatDesc::PostProcessorFormat_FLOAT)
136  requiresFP = true;
137  }
138  }
139 
140  // avoid dealing with multisampling texture type for only one sample
141  if (_samples < 2)
142  _samples = 0;
143 
144  // number of eyes from which to scene is rendered
145  const int numEyes = _stereoEye < 0 ? 1 : 2;
146 
147  GLuint texInternalFmt = requiresFP ? GL_RGBA32F : GL_RGBA;
148 
149  // allocate enough FBOs for the pipeline
150  if (_stereoEye < 1)
151  {
152  // save output target FBO
153  glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, (GLint*)&backbufferFBO_);
154  glGetIntegerv(GL_DRAW_BUFFER, (GLint*)&backbufferTarget_);
155  glGetIntegerv(GL_VIEWPORT, backbufferViewport_);
156 
157  // in stereo mode, allocation for both eyes is done while setting up the left eye
158 
159  // alloc/resize scene targets
160  for (int i = 0; i < numEyes; ++i)
161  setupFBO(sceneFBO_ + i, _width, _height, texInternalFmt, _samples);
162 
163  // alloc/resize processing double buffer
164 
165  int numTempBuffers = std::min(numProcs - 1, 2);
166 
167  if (_samples > 0) // resolve msaa into temp buffer, requires additional step
168  numTempBuffers = std::min(numProcs, 2);
169 
170  for (int i = 0; i < numTempBuffers; ++i)
171  setupFBO(procFBO_ + i, _width, _height, texInternalFmt, 0);
172 
173  // alloc/resize stereo final targets
174  for (int i = 0; i < (_stereoEye >= 0 ? 2 : 0); ++i)
175  setupFBO(stereoFBO_ + i, _width, _height, texInternalFmt, 0);
176  }
177 
178 
179  // bind scene render target FBO
180  int targetFboId = std::max(std::min(_stereoEye, 1), 0); // clamp(eye, 0, 1)
181  sceneFBO_[targetFboId].bind();
182  glDrawBuffer(GL_COLOR_ATTACHMENT0);
183  glViewport(0, 0, _width, _height);
184 
185  // clear fbo
186  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
187 
188  // save stereo setting
189  stereoMode_ = _stereoEye >= 0;
190 
191  // enable multisample FBO
192  if (_samples > 0)
193  glEnable(GL_MULTISAMPLE);
194 
195  return 1;
196 }
197 
198 
199 void PostProcessing::setupFBO( ACG::FBO* _dst, int _width, int _height, GLuint _intfmt , int _samples )
200 {
201  // render to multisampled texture -> no texfilter!
202  GLuint filterMode = _samples ? GL_NEAREST : GL_LINEAR;
203 
204  if (!_dst->getFboID())
205  {
206  // alloc
207  _dst->setMultisampling(_samples);
208  _dst->attachTexture2D(GL_COLOR_ATTACHMENT0, _width, _height, _intfmt, GL_RGBA, GL_CLAMP, filterMode, filterMode);
209 
210  // reading from GL_DEPTH24_STENCIL8 is buggy on ati
211  _dst->attachTexture2DDepth(_width, _height, GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL);
212 
213  // GL_DEPTH_COMPONENT32: has weird effect on color buffer - colors are darker!
214  // reading from depth works, but no stencil support
215 // _dst->attachTexture2DDepth(_width, _height, GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT);
216 
217  // GL_DEPTH_COMPONENT: same as GL_DEPTH_COMPONENT32
218 // _dst->attachTexture2DDepth(_width, _height, GL_DEPTH_COMPONENT, GL_DEPTH_COMPONENT);
219 
220  // colors are darker only in classical renderer!!
221  }
222  else
223  {
224  // resize
225  _dst->setMultisampling(_samples);
226  _dst->resize(_width, _height);
227 
228  // reformat
229  if (_dst->getInternalFormat(GL_COLOR_ATTACHMENT0) != _intfmt)
230  _dst->attachTexture2D(GL_COLOR_ATTACHMENT0, _width, _height, _intfmt, GL_RGBA, GL_CLAMP, filterMode, filterMode);
231  }
232 }
233 
234 
235 void PostProcessing::postProcess( int _viewerID, ACG::GLState* _glstate, const ACG::GLMatrixd& _modelview, const ACG::GLMatrixd& _proj1, const ACG::GLMatrixd& _proj2, bool _hwOpenGLStereo )
236 {
237  // disable multisample FBO
238  if (sceneFBO_[0].getMultisamplingCount())
239  glDisable(GL_MULTISAMPLE);
240 
241  // 1st post processing source: active fbo
242  int postProcSrc = 1;
243  PostProcessorInput postProcInput;
244 
245  std::vector<const PostProcessorInput*> postProcInputVec;
246  postProcInputVec.push_back(&postProcInput);
247 
248  // # post processors in chain
249  const int numProcs = postProcessorManager().numActive(_viewerID);
250 
251  // # executions of postproc chain, one for each eye
252  int numChainExecs = stereoMode_ ? 2 : 1;
253 
254  // use GL_LEQUAL for depth testing, restore current depth func afterwards
255  GLint saveDepthFunc = GL_LESS;
256  glGetIntegerv(GL_DEPTH_FUNC, &saveDepthFunc);
257 
258  glDepthMask(1);
259  glColorMask(1,1,1,1);
260 
261 
262  // execute
263 
264  for (int chainId = 0; chainId < numChainExecs; ++chainId) {
265 
266  // 1. input: scene render target
267 
268  postProcInput.width = sceneFBO_[chainId].width();
269  postProcInput.height = sceneFBO_[chainId].height();
270 
271  postProcInput.depthRange_[0] = 0.0f;
272  postProcInput.depthRange_[1] = 1.0f;
273 
274  postProcInput.view_ = _modelview;
275  postProcInput.proj_ = chainId ? _proj2 : _proj1;
276 
277  // resolve multisampling before post processing
278  if (sceneFBO_[chainId].getMultisamplingCount())
279  {
280  // resolve into first temp target or into stereo output (no postprocs, but stereo)
281  ACG::FBO* resolveTarget = numProcs ? &procFBO_[0] : &stereoFBO_[chainId];
282 
283  if (numProcs || (stereoMode_ && !_hwOpenGLStereo))
284  resolveMultisampling(resolveTarget, &sceneFBO_[chainId]);
285 
286  // use resolved texture as input for processing
287  postProcInput.colorTex_ = resolveTarget->getAttachment(GL_COLOR_ATTACHMENT0);
288  postProcInput.depthTex_ = resolveTarget->getAttachment(GL_DEPTH_ATTACHMENT);
289 
290  postProcInput.texfmt_ = resolveTarget->getInternalFormat(GL_COLOR_ATTACHMENT0);
291 
292  // read from procFBO_[0], write to procFBO_[1]
293  postProcSrc = 0;
294  }
295  else
296  {
297  // read directly from scene fbo
298  postProcInput.colorTex_ = sceneFBO_[chainId].getAttachment(GL_COLOR_ATTACHMENT0);
299  postProcInput.depthTex_ = sceneFBO_[chainId].getAttachment(GL_DEPTH_ATTACHMENT);
300 
301  postProcInput.texfmt_ = sceneFBO_[chainId].getInternalFormat(GL_COLOR_ATTACHMENT0);
302 
303  // write to procFBO_[0]
304  postProcSrc = 1;
305  }
306 
307 
308  // execute post processing chain with 2 FBOs
309  for (int i = 0; i < numProcs; ++i) {
310 
311  int postProcTarget = 1 - postProcSrc;
312 
313  // specify output target of the next post processor
314  GLuint outputFBO = procFBO_[postProcTarget].getFboID();
315  GLuint outputDrawbuffer = GL_COLOR_ATTACHMENT0;
316  GLint outputViewport[4] = {0, 0, procFBO_[postProcTarget].width(), procFBO_[postProcTarget].height()};
317 
318  const GLint* outputViewportPtr = outputViewport;
319 
320  // write to stereo or back buffer in last step
321  if (i + 1 == numProcs)
322  {
323  if (stereoMode_)
324  {
325  if (_hwOpenGLStereo)
326  {
327  outputFBO = backbufferFBO_;
328  outputDrawbuffer = chainId ? GL_BACK_RIGHT : GL_BACK_LEFT;
329 
330  outputViewportPtr = backbufferViewport_;
331  }
332  else
333  {
334  outputFBO = stereoFBO_[chainId].getFboID();
335  outputDrawbuffer = GL_COLOR_ATTACHMENT0;
336 
337  outputViewport[0] = outputViewport[1] = 0;
338  outputViewport[2] = stereoFBO_[chainId].width();
339  outputViewport[3] = stereoFBO_[chainId].height();
340  }
341  }
342  else
343  {
344  outputFBO = backbufferFBO_;
345  outputDrawbuffer = backbufferTarget_;
346  outputViewportPtr = backbufferViewport_;
347  }
348  }
349 
350  PostProcessorOutput postProcOutput(outputFBO, outputDrawbuffer,
351  procFBO_[postProcTarget].width(), procFBO_[postProcTarget].height(), outputViewportPtr);
352 
353  // apply post processor
354  PostProcessorInfo* proc = postProcessorManager().active( _viewerID, i );
355  if (proc && proc->plugin)
356  {
357  // set default opengl states
358  glDepthMask(1);
359  glColorMask(1,1,1,1);
360 
361  glEnable(GL_CULL_FACE);
362  glCullFace(GL_BACK);
363  glFrontFace(GL_CCW);
364  glDisable(GL_BLEND);
365  glDepthFunc(GL_LEQUAL);
366 
367  proc->plugin->postProcess(_glstate, postProcInputVec, postProcOutput);
368 
369  // make sure that post processor writes to specified output
370  if (debugLevel_ > 0)
371  {
372  GLint testFBO = 0, testDrawbuffer = 0;
373  GLint testVp[4];
374 
375  glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &testFBO);
376  glGetIntegerv(GL_DRAW_BUFFER, &testDrawbuffer);
377  glGetIntegerv(GL_VIEWPORT, testVp);
378 
379  if (GLuint(testFBO) != postProcOutput.fbo_)
380  std::cerr << "Error: PostProcessor does not write to specified FBO: " << proc->plugin->postProcessorName().toStdString() << std::endl;
381 
382  if (GLuint(testDrawbuffer) != postProcOutput.drawBuffer_)
383  std::cerr << "Error: PostProcessor does not write to specified draw-buffer: " << proc->plugin->postProcessorName().toStdString() << std::endl;
384 
385  if (testVp[0] != postProcOutput.viewport_[0] || testVp[1] != postProcOutput.viewport_[1] ||
386  testVp[2] != postProcOutput.viewport_[2] || testVp[3] != postProcOutput.viewport_[3])
387  std::cerr << "Error: PostProcessor does not write to specified viewport: " << proc->plugin->postProcessorName().toStdString()
388  << " actual: (" << testVp[0] << " " << testVp[1] << " " << testVp[2] << " " << testVp[3] << ") expected: ("
389  << postProcOutput.viewport_[0] << " " << postProcOutput.viewport_[1] << " " << postProcOutput.viewport_[2] << " " << postProcOutput.viewport_[3] << ")" << std::endl;
390  }
391  }
392 
393  // swap target/source fbo
394  postProcSrc = postProcTarget;
395 
396  postProcInput.colorTex_ = procFBO_[postProcSrc].getAttachment(GL_COLOR_ATTACHMENT0);
397  }
398 
399  }
400 
401 
402  // restore backbuffer
403  glBindFramebuffer(GL_FRAMEBUFFER, backbufferFBO_);
404  glDrawBuffer(backbufferTarget_);
405  glViewport(backbufferViewport_[0], backbufferViewport_[1], backbufferViewport_[2], backbufferViewport_[3]);
406 
407  // restore depth func
408  glDepthFunc(saveDepthFunc);
409 }
410 
411 void PostProcessing::resolveMultisampling( ACG::FBO* _dst, ACG::FBO* _src )
412 {
413  const int resolveMethod = 0;
414 
415  GLint curFbo = 0;
416  glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &curFbo);
417 
418  if (resolveMethod == 0)
419  {
420  // resolve by blitting
421 
422  glBindFramebuffer(GL_READ_FRAMEBUFFER, _src->getFboID());
423  glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _dst->getFboID());
424 
425  if (debugLevel_ > 0)
426  {
427  if (_src->size() != _dst->size())
428  std::cerr << "PostProcessing::resolveMultisampling - FBO size mismatch : src ("
429  << _src->width() << " " << _src->height() << ") dst ("
430  << _dst->width() << " " << _dst->height() << ")" << std::endl;
431  }
432 
433  glBlitFramebuffer(0, 0, _src->width(), _src->height(),
434  0, 0, _dst->width(), _dst->height(),
435  GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, GL_NEAREST);
436 
437  }
438  else
439  {
440  // resolve manually
441  _dst->bind();
442  glDrawBuffer(GL_COLOR_ATTACHMENT0);
443  GLuint multisampledColorTex = _src->getAttachment(GL_COLOR_ATTACHMENT0);
444  GLuint multisampledDepthTex = _src->getAttachment(GL_DEPTH_ATTACHMENT);
445  glDepthFunc(GL_ALWAYS);
446  ACG::MSTextureSampler::filterMSAATexture_Nearest(multisampledColorTex, multisampledDepthTex, _src->getMultisamplingCount());
447  glDepthFunc(GL_LEQUAL);
448  }
449 
450  checkGLError();
451 
452  glBindFramebuffer(GL_FRAMEBUFFER, curFbo);
453 
454 }
455 
456 
458 {
459  PostProcessorInfo* procAnaglyph = postProcessorManager().getPostProcessor("Anaglyph Stereo Postprocessor Plugin");
460 
461  if (procAnaglyph)
462  {
463  const int numProcs = postProcessorManager().numActive(_viewerID);
464 
465  PostProcessorInput inputs[2];
466 
467  for (int i = 0; i < 2; ++i)
468  {
469  // if the scene is multisampled or at least one post processor is active, read from stereoFBO_
470  // otherwise, read from the non-multisampled and unprocessed sceneFBO_
471  ACG::FBO* eyeSrcFBO = &stereoFBO_[i];
472 
473  if (!numProcs && !sceneFBO_[i].getMultisamplingCount())
474  eyeSrcFBO = &sceneFBO_[i];
475 
476  inputs[i].colorTex_ = eyeSrcFBO->getAttachment(GL_COLOR_ATTACHMENT0);
477  inputs[i].depthTex_ = eyeSrcFBO->getAttachment(GL_DEPTH_ATTACHMENT);
478  inputs[i].width = eyeSrcFBO->width();
479  inputs[i].height = eyeSrcFBO->height();
480 
481  inputs[i].texfmt_ = eyeSrcFBO->getInternalFormat(GL_COLOR_ATTACHMENT0);
482  }
483 
484 
485  std::vector<const PostProcessorInput*> anaglyphInputVec(2);
486  anaglyphInputVec[0] = inputs;
487  anaglyphInputVec[1] = inputs + 1;
488 
489  PostProcessorOutput anaglyphOutput(backbufferFBO_,
490  backbufferTarget_,
491  inputs[0].width, inputs[0].height, backbufferViewport_);
492 
493  procAnaglyph->plugin->postProcess(0, anaglyphInputVec, anaglyphOutput);
494  }
495  else
496  std::cerr << "error: stereo anaglyph plugin missing!" << std::endl;
497 }
498 
ACG::FBO stereoFBO_[2]
store post processing results for each stereo eyes, input for anaglyph processor, no msaa ...
PostProcessorInfo * getPostProcessor(QString _name)
get post processor with the given name
void attachTexture2D(GLenum _attachment, GLsizei _width, GLsizei _height, GLuint _internalFmt, GLenum _format, GLint _wrapMode=GL_CLAMP, GLint _minFilter=GL_NEAREST, GLint _magFilter=GL_NEAREST)
function to attach a texture to fbo
Definition: FBO.cc:200
PostProcessorInterface * plugin
Pointer to the loaded plugin (Already casted when loading it)
ACG::FBO procFBO_[2]
post-processing read/write FBOs, no msaa
void resolveStereoAnyglyph(int _viewerID)
Resolve stereo buffers as anaglyph.
GLsizei width() const
get width of fbo texture
Definition: FBO.hh:161
virtual void postProcess(ACG::GLState *_glState, const std::vector< const PostProcessorInput * > &_input, const PostProcessorOutput &_output)=0
post processor function
Definition: FBO.hh:83
GLsizei getMultisamplingCount() const
get number of samples
Definition: FBO.hh:105
virtual QString postProcessorName()=0
announce name for the postProcessor function
int numActive(int _id)
Get the number of active post processors for viewer.
void resize(GLsizei _width, GLsizei _height, bool _forceResize=false)
resize function (if textures created by this class)
Definition: FBO.cc:543
GLuint getFboID()
return opengl id
Definition: FBO.cc:597
void attachTexture2DDepth(GLsizei _width, GLsizei _height, GLuint _internalFmt=GL_DEPTH_COMPONENT32, GLenum _format=GL_DEPTH_COMPONENT)
function to attach a depth-buffer texture to fbo (using GL_DEPTH_ATTACHMENT)
Definition: FBO.cc:327
GLuint getAttachment(GLenum _attachment)
return attached texture id
Definition: FBO.cc:529
PostProcessorInfo * active(int _id, int _chainIdx=0)
Get the current active post processor for viewer at chain index.
ACG::FBO sceneFBO_[2]
FBO for scene rendering, one for each eye in stereo mode, possibly multisampled.
int debugLevel_
perform extra checks for debugging: 0 -> no checks, 1 -> fast checks, 2 -> more checks etc...
GLuint getInternalFormat(GLenum _attachment)
return internal texture format of attachment
Definition: FBO.cc:536
bool stereoMode_
postprocessing in stereo mode
GLsizei height() const
get height of fbo texture
Definition: FBO.hh:164
int setupScene(int _viewerID, int _width, int _height, int _samples=0, int _stereoEye=-1)
Bind fbo for scene rendering.
GLuint backbufferFBO_
backbuffer output, not owned by this class
void postProcess(int _viewerID, ACG::GLState *_glstate, const ACG::GLMatrixd &_modelview, const ACG::GLMatrixd &_proj1, const ACG::GLMatrixd &_proj2, bool _hwOpenGLStereo=false)
Perform all post processing.
ACG::Vec2i size() const
get width and height of fbo texture
Definition: FBO.hh:167
GLsizei setMultisampling(GLsizei _samples, GLboolean _fixedsamplelocations=GL_TRUE)
Definition: FBO.cc:602
bool bind()
bind the fbo and sets it as rendertarget
Definition: FBO.cc:451