FBO.cc 19.5 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
4
5
6
7
8
9
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
39
40
41
/*===========================================================================*\
 *                                                                           *
 *                              OpenFlipper                                  *
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * 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.              *
 *                                                                           *
\*===========================================================================*/

Jan Möbius's avatar
Jan Möbius committed
42
43
44
45
46
47
48
49
//=============================================================================
//
//  CLASS FBO - IMPLEMENTATION
//
//=============================================================================

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

50
#include <ACG/GL/acg_glew.hh>
Jan Möbius's avatar
Jan Möbius committed
51
#include "FBO.hh"
52
#include "GLState.hh"
53
#include "GLError.hh"
54
#include "GLFormatInfo.hh"
Jan Möbius's avatar
Jan Möbius committed
55
56
57
58
59
60
61
62

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

namespace ACG
{

//== IMPLEMENTATION ==========================================================

63

64
65
66
67
68
69
FBO::RenderTexture::RenderTexture()
  : id(0), target(0), internalFormat(0), format(0), gltype(0),
  dim(0,0,0), wrapMode(0), minFilter(0), magFilter(0), owner(false)
{
}

70
71

FBO::FBO()
72
: fbo_(0), depthbuffer_(0), stencilbuffer_(0), width_(0), height_(0), samples_(0), fixedsamplelocation_(GL_TRUE), prevFbo_(0), prevDrawBuffer_(GL_NONE)
73
74
75
{
}

Jan Möbius's avatar
Jan Möbius committed
76
77
78
FBO::
~FBO()
{
79
  del();
Jan Möbius's avatar
Jan Möbius committed
80
81
82
83
84
85
86
87
}

//-----------------------------------------------------------------------------

void
FBO::
init()
{
88
89
90
91
92
  // Create framebuffer object
  if (!checkExtensionSupported("GL_EXT_framebuffer_object")) {
    std::cerr << "Framebuffer object not supported! " << std::endl;
    exit( 1 );
  }
Jan Möbius's avatar
Jan Möbius committed
93

94
95
96
  // test whether fbo hasn't been created before
  if(!fbo_)
    glGenFramebuffersEXT( 1, &fbo_ );
Jan Möbius's avatar
Jan Möbius committed
97

98
99
  // check status
  checkFramebufferStatus();
Jan Möbius's avatar
Jan Möbius committed
100
101
}

102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//-----------------------------------------------------------------------------

void FBO::del()
{
  // delete framebuffer object
  if(fbo_)
    glDeleteFramebuffersEXT( 1, &fbo_ );

  // delete render buffer
  if(depthbuffer_)
    glDeleteRenderbuffersEXT(1, &depthbuffer_);

  // delete stencil buffer
  if(stencilbuffer_)
    glDeleteRenderbuffersEXT(1, &stencilbuffer_);

  for (AttachmentList::iterator it = attachments_.begin(); it != attachments_.end(); ++it)
    if (it->second.id && it->second.owner)
      glDeleteTextures(1, &it->second.id);
}


Jan Möbius's avatar
Jan Möbius committed
124
125
//-----------------------------------------------------------------------------

Christopher Tenter's avatar
Christopher Tenter committed
126
127
void FBO::attachTexture( GLenum _attachment, GLuint _texture, GLuint _level )
{
128
#ifdef GL_VERSION_3_2
Christopher Tenter's avatar
Christopher Tenter committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
  // bind fbo
  bind();

  // add texture to frame buffer object
  glFramebufferTexture( GL_FRAMEBUFFER_EXT, _attachment, _texture, _level );

//   GLint layered = 0;
//   glGetFramebufferAttachmentParameteriv( GL_FRAMEBUFFER_EXT, _attachment, GL_FRAMEBUFFER_ATTACHMENT_LAYERED, &layered);

  checkGLError();

  // check status
  checkFramebufferStatus();

  // unbind fbo
  unbind();

146
147
148
149
150

  // store texture id in internal array
  RenderTexture intID;
  intID.id = _texture;

Christopher Tenter's avatar
Christopher Tenter committed
151
152
153
154
155
  // free previously bound texture
  const RenderTexture& prevTex = attachments_[_attachment];
  if (prevTex.owner && prevTex.id)
    glDeleteTextures(1, &prevTex.id);

Christopher Tenter's avatar
Christopher Tenter committed
156
  // track texture id
157
  attachments_[_attachment] = intID;
158
159
160
#else
  std::cerr << "error: FBO::attachTexture unsupported - update glew headers and rebuild" << std::endl;
#endif
Christopher Tenter's avatar
Christopher Tenter committed
161
162
163
164
}

//-----------------------------------------------------------------------------

Jan Möbius's avatar
Jan Möbius committed
165
166
void
FBO::
167
attachTexture2D( GLenum _attachment, GLuint _texture, GLenum _target )
Jan Möbius's avatar
Jan Möbius committed
168
{
169
170
  // bind fbo
  bind();
Jan Möbius's avatar
Jan Möbius committed
171

172
  // add texture to frame buffer object
173
174
175
  glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, _attachment, _target, _texture, 0 );

  checkGLError();
Jan Möbius's avatar
Jan Möbius committed
176

177
178
  // check status
  checkFramebufferStatus();
Jan Möbius's avatar
Jan Möbius committed
179

180
181
182
  // unbind fbo
  unbind();

183
184
185
186
187
188

  // store texture id in internal array
  RenderTexture intID;
  intID.id = _texture;
  intID.target = _target;

Christopher Tenter's avatar
Christopher Tenter committed
189
190
191
192
193
  // free previously bound texture
  const RenderTexture& prevTex = attachments_[_attachment];
  if (prevTex.owner && prevTex.id)
    glDeleteTextures(1, &prevTex.id);

194
  // track texture id
195
  attachments_[_attachment] = intID;
196
197
198
199
200
201
202
203
204
205
}

//-----------------------------------------------------------------------------

void FBO::attachTexture2D( GLenum _attachment, GLsizei _width, GLsizei _height, GLuint _internalFmt, GLenum _format, GLint _wrapMode /*= GL_CLAMP*/, GLint _minFilter /*= GL_LINEAR*/, GLint _magFilter /*= GL_LINEAR*/ )
{
  // gen texture id
  GLuint texID;
  glGenTextures(1, &texID);

206
#ifdef GL_ARB_texture_multisample
207
  GLenum target = samples_ ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
208
209
210
#else
  GLenum target = GL_TEXTURE_2D;
#endif // GL_ARB_texture_multisample
211

212

Christopher Tenter's avatar
Christopher Tenter committed
213
214
215
216
217
218
219
220
221
222
223
224
  // if multisampled, texfilter must be GL_NEAREST
  // texelFetch returns darker color otherwise!
  if (samples_)
  {
    if (_minFilter != GL_NEAREST || _magFilter != GL_NEAREST)
    {
      std::cerr << "ACG::FBO - Multisampled texture must be filtered with GL_NEAREST!" << std::endl;

      _minFilter = _magFilter = GL_NEAREST;
    }
  }

225
226
227
228
229
  // store texture id in internal array
  RenderTexture intID;
  intID.id = texID;
  intID.internalFormat = _internalFmt;
  intID.format = _format;
230
  intID.gltype = GLFormatInfo(_internalFmt).type();
231
  intID.target = target;
232
233
234
235
236
  intID.dim = ACG::Vec3i(_width, _height, 1);
  intID.wrapMode = _wrapMode;
  intID.minFilter = _minFilter;
  intID.magFilter = _magFilter;
  intID.owner = true;
237
238
239


  // specify texture
240
241
  glBindTexture(target, texID);

242

243
#ifdef GL_ARB_texture_multisample
244
245
  if (!samples_)
  {
246
247
    glTexParameteri(target, GL_TEXTURE_WRAP_S, _wrapMode);
    glTexParameteri(target, GL_TEXTURE_WRAP_T, _wrapMode);
248
249
    glTexParameteri(target, GL_TEXTURE_MIN_FILTER, _minFilter);
    glTexParameteri(target, GL_TEXTURE_MAG_FILTER, _magFilter);
250
    glTexImage2D(target, 0, _internalFmt, _width, _height, 0, _format, intID.gltype, 0);
251
252
253
  }
  else
    glTexImage2DMultisample(target, samples_, _internalFmt, _width, _height, fixedsamplelocation_);
254
#else
255
256
  glTexParameteri(target, GL_TEXTURE_WRAP_S, _wrapMode);
  glTexParameteri(target, GL_TEXTURE_WRAP_T, _wrapMode);
257
258
  glTexParameteri(target, GL_TEXTURE_MIN_FILTER, _minFilter);
  glTexParameteri(target, GL_TEXTURE_MAG_FILTER, _magFilter);
259
  glTexImage2D(target, 0, _internalFmt, _width, _height, 0, _format, intID.gltype, 0);
260
261
#endif // GL_ARB_texture_multisample

262

263
  checkGLError();
264
265
266
267

  width_ = _width;
  height_ = _height;

268
  glBindTexture(target, 0);
269
270

  // attach
271
  attachTexture2D(_attachment, texID, target);
272
273
274

  // track texture id
  attachments_[_attachment] = intID;
275
276
277
278
}

//-----------------------------------------------------------------------------

Christopher Tenter's avatar
Christopher Tenter committed
279
280
281
282
283
284
285
286
287
288
289
290
291
void FBO::attachTexture3D( GLenum _attachment, GLsizei _width, GLsizei _height, GLsizei _depth, GLuint _internalFmt, GLenum _format, GLint _wrapMode , GLint _minFilter , GLint _magFilter  )
{
  // gen texture id
  GLuint texID;
  glGenTextures(1, &texID);

  GLenum target = GL_TEXTURE_3D;

  // store texture id in internal array
  RenderTexture intID;
  intID.id = texID;
  intID.internalFormat = _internalFmt;
  intID.format = _format;
292
  intID.gltype = GLFormatInfo(_internalFmt).type();
Christopher Tenter's avatar
Christopher Tenter committed
293
  intID.target = target;
294
295
296
297
298
  intID.dim = ACG::Vec3i(_width, _height, _depth);
  intID.wrapMode = _wrapMode;
  intID.minFilter = _minFilter;
  intID.magFilter = _magFilter;
  intID.owner = true;
Christopher Tenter's avatar
Christopher Tenter committed
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319


  // specify texture
  glBindTexture(target, texID);

  glTexParameteri(target, GL_TEXTURE_WRAP_S, _wrapMode);
  glTexParameteri(target, GL_TEXTURE_WRAP_T, _wrapMode);
  glTexParameteri(target, GL_TEXTURE_WRAP_R, _wrapMode);
  glTexParameteri(target, GL_TEXTURE_MIN_FILTER, _minFilter);
  glTexParameteri(target, GL_TEXTURE_MAG_FILTER, _magFilter);
  glTexImage3D(target, 0, _internalFmt, _width, _height, _depth, 0, _format, GL_FLOAT, 0);

  checkGLError();

  width_ = _width;
  height_ = _height;

  glBindTexture(target, 0);

  // attach
  attachTexture(_attachment, texID, 0);
320
321
322

  // track texture id
  attachments_[_attachment] = intID;
Christopher Tenter's avatar
Christopher Tenter committed
323
324
325
326
}

//-----------------------------------------------------------------------------

327
328
void FBO::attachTexture2DDepth( GLsizei _width, GLsizei _height, GLuint _internalFmt /*= GL_DEPTH_COMPONENT32*/, GLenum _format /*= GL_DEPTH_COMPONENT */ )
{
329
  attachTexture2D(GL_DEPTH_ATTACHMENT, _width, _height, _internalFmt, _format, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
Jan Möbius's avatar
Jan Möbius committed
330
331
332
333
}

//-----------------------------------------------------------------------------

334
335
void FBO::attachTexture2DStencil( GLsizei _width, GLsizei _height )
{
336
  attachTexture2D(GL_STENCIL_ATTACHMENT_EXT, _width, _height, GL_STENCIL_INDEX8, GL_STENCIL_INDEX, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
337
338
339
340
}

//-----------------------------------------------------------------------------

Jan Möbius's avatar
Jan Möbius committed
341
342
343
344
void
FBO::
addDepthBuffer( GLuint _width, GLuint _height )
{
345
346
347
  if (depthbuffer_)
    glDeleteRenderbuffersEXT(1, &depthbuffer_);

Jan Möbius's avatar
Jan Möbius committed
348
349
350
351
352
353
354
  // create renderbuffer
  glGenRenderbuffersEXT(1, &depthbuffer_);

  // bind renderbuffer
  glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthbuffer_);

  // malloc
355
356
357
#ifdef GL_ARB_texture_multisample
  glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, samples_, GL_DEPTH_COMPONENT, _width, _height);
#else
Jan Möbius's avatar
Jan Möbius committed
358
  glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, _width, _height);
359
#endif
Jan Möbius's avatar
Jan Möbius committed
360
361
362

  // attach to framebuffer object
  if ( bind() )
363
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthbuffer_);
Jan Möbius's avatar
Jan Möbius committed
364
365
366
367
368
369
370
371
372
373
374
375
376
377

  // check status
  checkFramebufferStatus();

  // normal render mode
  unbind();
}

//-----------------------------------------------------------------------------

void
FBO::
addStencilBuffer( GLuint _width, GLuint _height )
{
378
379
380
  if (stencilbuffer_)
    glDeleteRenderbuffersEXT(1, &stencilbuffer_);

Jan Möbius's avatar
Jan Möbius committed
381
382
383
384
385
386
387
  // create renderbuffer
  glGenRenderbuffersEXT(1, &stencilbuffer_);

  // bind renderbuffer
  glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, stencilbuffer_);

  // malloc
388
389
390
#ifdef GL_ARB_texture_multisample
  glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, samples_, GL_STENCIL_INDEX, _width, _height);
#else
Jan Möbius's avatar
Jan Möbius committed
391
  glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX, _width, _height);
392
#endif
Jan Möbius's avatar
Jan Möbius committed
393
394
395

  // attach to framebuffer object
  if ( bind() )
396
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, stencilbuffer_);
Jan Möbius's avatar
Jan Möbius committed
397
398
399
400
401
402
403
404

  // check status
  checkFramebufferStatus();

  // normal render mode
  unbind();
}

405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//-----------------------------------------------------------------------------

void 
FBO::
addDepthStencilBuffer( GLuint _width, GLuint _height )
{
  if (depthbuffer_)
    glDeleteRenderbuffersEXT(1, &depthbuffer_);

  if (stencilbuffer_)
    glDeleteRenderbuffersEXT(1, &stencilbuffer_);

  depthbuffer_ = stencilbuffer_ = 0;

  // create renderbuffer
  glGenRenderbuffersEXT(1, &depthbuffer_);

  // bind renderbuffer
  glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthbuffer_);

  // malloc
#ifdef GL_ARB_texture_multisample
  glRenderbufferStorageMultisampleEXT(GL_RENDERBUFFER_EXT, samples_, GL_DEPTH_STENCIL, _width, _height);
#else
  glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_STENCIL, _width, _height);
#endif

  // attach to framebuffer object
  if (bind())
  {
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthbuffer_);
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthbuffer_);
  }

  // check status
  checkFramebufferStatus();

  // normal render mode
  unbind();
}
Jan Möbius's avatar
Jan Möbius committed
445
446
447
448
449
450
451
452


//-----------------------------------------------------------------------------

bool
FBO::
bind()
{
453
454
  // save previous fbo id
  glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, (GLint*)&prevFbo_);
455
  glGetIntegerv(GL_DRAW_BUFFER, (GLint*)&prevDrawBuffer_);
456

457
458
459
460
461
462
  if ( !fbo_ )
    init();

  if ( !fbo_)
    return false;

463
464
  // bind framebuffer object
  ACG::GLState::bindFramebuffer( GL_FRAMEBUFFER_EXT, fbo_ );
Jan Möbius's avatar
Jan Möbius committed
465

466

467
  return true;
Jan Möbius's avatar
Jan Möbius committed
468
469
470
471
472
473
474
475
}

//-----------------------------------------------------------------------------

void
FBO::
unbind()
{
476
  //set to normal rendering
477
  ACG::GLState::bindFramebuffer( GL_FRAMEBUFFER_EXT, prevFbo_ );
478
  ACG::GLState::drawBuffer( prevDrawBuffer_ );
Jan Möbius's avatar
Jan Möbius committed
479
480
481
482
483
484
485
486
}

//-----------------------------------------------------------------------------

bool
FBO::
checkFramebufferStatus()
{
487
488
  GLenum status;
  status = ( GLenum ) glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
Jan Möbius's avatar
Jan Möbius committed
489
  //std::cout << "Framebuffer status: " << status << std::endl;
490
491
492
  switch ( status )
  {
    case GL_FRAMEBUFFER_COMPLETE_EXT:
Jan Möbius's avatar
Jan Möbius committed
493
      //std::cout << "Framebuffer ok\n";
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
      break;
    case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
      std::cout << " Framebuffer incomplete attachment\n";
      break;
    case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
      std::cout << "Unsupported framebuffer format\n";
      break;
    case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
      std::cout << "Framebuffer incomplete, missing attachment\n";
      break;
    case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
      std::cout <<  "Framebuffer incomplete, attached images must have same dimensions\n";
      break;
    case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
      std::cout << "Framebuffer incomplete, attached images must have same format\n";
      break;
    case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
      std::cout << "Framebuffer incomplete, missing draw buffer\n";
      break;
    case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
      std::cout << "Framebuffer incomplete, missing read buffer\n";
      break;
516
517
518
    case 0x8D56: // GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
      std::cout << "Framebuffer incomplete, attached images must have same multisample count\n";
      break;
519
520
521
522
523
524
525
526
527
528
529
530
    default:
      std::cout << "Unhandled case\n";
      break;
  }

  return ( status == GL_FRAMEBUFFER_COMPLETE_EXT );
}

//-----------------------------------------------------------------------------

GLuint FBO::getAttachment( GLenum _attachment )
{
531
  return attachments_[_attachment].id;
532
533
534
535
}

//-----------------------------------------------------------------------------

Christopher Tenter's avatar
Christopher Tenter committed
536
537
538
539
540
541
542
GLuint FBO::getInternalFormat( GLenum _attachment )
{
  return attachments_[_attachment].internalFormat;
}

//-----------------------------------------------------------------------------

543
void FBO::resize( GLsizei _width, GLsizei _height, bool _forceResize )
544
{
545
  if (_width != width_ ||_height != height_ || _forceResize)
546
  {
547
548
549
    // resizing already existing textures is highly driver dependent and does not always
    // work for all combinations of texture type (2d, 2dms, 3d) and format
    // safest way to resize is to first delete the FBO and all its internal textures, and then rebuild
550

551
552
553
    if (fbo_)
      glDeleteFramebuffersEXT(1, &fbo_);
    glGenFramebuffersEXT(1, &fbo_);
Christopher Tenter's avatar
Christopher Tenter committed
554

555
556
557
    // "detach" all textures
    AttachmentList temp;
    temp.swap(attachments_);
Christopher Tenter's avatar
Christopher Tenter committed
558

559
560
561
562
    // reattach all targets
    for (AttachmentList::iterator it = temp.begin(); it != temp.end(); ++it)
    {
      RenderTexture* rt = &it->second;
Christopher Tenter's avatar
Christopher Tenter committed
563

564
565
      // only resize textures that are owned by the FBO
      if (rt->owner)
566
      {
567
568
        glDeleteTextures(1, &rt->id);

569
        switch (rt->target)
Christopher Tenter's avatar
Christopher Tenter committed
570
        {
571
        case GL_TEXTURE_2D: 
572
#ifdef GL_ARB_texture_multisample
573
        case GL_TEXTURE_2D_MULTISAMPLE:
574
#endif
Christopher Tenter's avatar
Christopher Tenter committed
575
          attachTexture2D(it->first, _width, _height, rt->internalFormat, rt->format, rt->wrapMode, rt->minFilter, rt->magFilter);
576
          break;
577

578
        case GL_TEXTURE_3D:
Christopher Tenter's avatar
Christopher Tenter committed
579
          attachTexture3D(it->first, _width, _height, rt->dim[2], rt->internalFormat, rt->format, rt->wrapMode, rt->minFilter, rt->magFilter);
580
          break;
581

582
583
584
        default:
          std::cout << "FBO::resize - unknown resize target " << rt->target << std::endl;
        }
Christopher Tenter's avatar
Christopher Tenter committed
585
      }
586
587
    }

588
589
590
    // reattach render buffers
    if(depthbuffer_)
      addDepthBuffer(_width, _height);
591

592
593
    if(stencilbuffer_)
      addStencilBuffer(_width, _height);
594
595
596
597
598
599
  }
}

GLuint FBO::getFboID()
{
  return fbo_;
Jan Möbius's avatar
Jan Möbius committed
600
601
}

602
GLsizei FBO::setMultisampling( GLsizei _samples, GLboolean _fixedsamplelocations /*= GL_TRUE*/ )
603
{
Christopher Tenter's avatar
Christopher Tenter committed
604
605
  // recreate textures when params changed
  bool recreateTextures = fixedsamplelocation_ != _fixedsamplelocations;
606

Christopher Tenter's avatar
Christopher Tenter committed
607
  if (samples_ != _samples)
608
  {
Christopher Tenter's avatar
Christopher Tenter committed
609
610
611
612
    // clamp sample count to max supported
    static GLint maxSamples = -1;
    if (maxSamples < 0)
      glGetIntegerv(GL_MAX_SAMPLES, &maxSamples);
613

Christopher Tenter's avatar
Christopher Tenter committed
614
    if (_samples >= maxSamples) _samples = maxSamples - 1;
615

Christopher Tenter's avatar
Christopher Tenter committed
616
617
    // gpu driver might cause crash on calling glTexImage2DMultisample if _samples is not a power of 2
    // -> avoid by seeking to next native MSAA sample-count
618

Christopher Tenter's avatar
Christopher Tenter committed
619
620
621
    if (_samples)
    {
      int safeSampleCount = 1;
622

Christopher Tenter's avatar
Christopher Tenter committed
623
624
      while (safeSampleCount < _samples)
        safeSampleCount <<= 1;
625

Christopher Tenter's avatar
Christopher Tenter committed
626
627
      while (safeSampleCount >= maxSamples)
        safeSampleCount >>= 1;
628

Christopher Tenter's avatar
Christopher Tenter committed
629
630
631
632
      _samples = safeSampleCount;
    }

    recreateTextures = recreateTextures || (samples_ != _samples);
633

Christopher Tenter's avatar
Christopher Tenter committed
634
635
636
637
638
639
640
641
642
643
    samples_ = _samples;
  }

  fixedsamplelocation_ = _fixedsamplelocations;

  // force texture reloading to apply new multisampling
  if (recreateTextures)
    resize(width_, height_, true);
  
  return samples_;
644
645
}

Jan Möbius's avatar
Jan Möbius committed
646
647
648
649

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