FBO.cc 19.4 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
void FBO::attachTexture( GLenum _attachment, GLuint _texture, GLuint _level )
{
  // 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();

145
146
147
148
149

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

Christopher Tenter's avatar
Christopher Tenter committed
150
151
152
153
154
  // 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
155
  // track texture id
156
  attachments_[_attachment] = intID;
Christopher Tenter's avatar
Christopher Tenter committed
157
158
159
160
}

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

Jan Möbius's avatar
Jan Möbius committed
161
162
void
FBO::
163
attachTexture2D( GLenum _attachment, GLuint _texture, GLenum _target )
Jan Möbius's avatar
Jan Möbius committed
164
{
165
166
  // bind fbo
  bind();
Jan Möbius's avatar
Jan Möbius committed
167

168
  // add texture to frame buffer object
169
170
171
  glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, _attachment, _target, _texture, 0 );

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

173
174
  // check status
  checkFramebufferStatus();
Jan Möbius's avatar
Jan Möbius committed
175

176
177
178
  // unbind fbo
  unbind();

179
180
181
182
183
184

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

Christopher Tenter's avatar
Christopher Tenter committed
185
186
187
188
189
  // free previously bound texture
  const RenderTexture& prevTex = attachments_[_attachment];
  if (prevTex.owner && prevTex.id)
    glDeleteTextures(1, &prevTex.id);

190
  // track texture id
191
  attachments_[_attachment] = intID;
192
193
194
195
196
197
198
199
200
201
}

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

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);

202
#ifdef GL_ARB_texture_multisample
203
  GLenum target = samples_ ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D;
204
205
206
#else
  GLenum target = GL_TEXTURE_2D;
#endif // GL_ARB_texture_multisample
207

208

Christopher Tenter's avatar
Christopher Tenter committed
209
210
211
212
213
214
215
216
217
218
219
220
  // 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;
    }
  }

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


  // specify texture
236
237
  glBindTexture(target, texID);

238

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

258

259
  checkGLError();
260
261
262
263

  width_ = _width;
  height_ = _height;

264
  glBindTexture(target, 0);
265
266

  // attach
267
  attachTexture2D(_attachment, texID, target);
268
269
270

  // track texture id
  attachments_[_attachment] = intID;
271
272
273
274
}

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

Christopher Tenter's avatar
Christopher Tenter committed
275
276
277
278
279
280
281
282
283
284
285
286
287
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;
288
  intID.gltype = GLFormatInfo(_internalFmt).type();
Christopher Tenter's avatar
Christopher Tenter committed
289
  intID.target = target;
290
291
292
293
294
  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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315


  // 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);
316
317
318

  // track texture id
  attachments_[_attachment] = intID;
Christopher Tenter's avatar
Christopher Tenter committed
319
320
321
322
}

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

323
324
void FBO::attachTexture2DDepth( GLsizei _width, GLsizei _height, GLuint _internalFmt /*= GL_DEPTH_COMPONENT32*/, GLenum _format /*= GL_DEPTH_COMPONENT */ )
{
325
  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
326
327
328
329
}

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

330
331
void FBO::attachTexture2DStencil( GLsizei _width, GLsizei _height )
{
332
  attachTexture2D(GL_STENCIL_ATTACHMENT_EXT, _width, _height, GL_STENCIL_INDEX8, GL_STENCIL_INDEX, GL_CLAMP_TO_EDGE, GL_NEAREST, GL_NEAREST);
333
334
335
336
}

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

Jan Möbius's avatar
Jan Möbius committed
337
338
339
340
void
FBO::
addDepthBuffer( GLuint _width, GLuint _height )
{
341
342
343
  if (depthbuffer_)
    glDeleteRenderbuffersEXT(1, &depthbuffer_);

Jan Möbius's avatar
Jan Möbius committed
344
345
346
347
348
349
350
  // create renderbuffer
  glGenRenderbuffersEXT(1, &depthbuffer_);

  // bind renderbuffer
  glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthbuffer_);

  // malloc
351
352
353
#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
354
  glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, _width, _height);
355
#endif
Jan Möbius's avatar
Jan Möbius committed
356
357
358

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

  // check status
  checkFramebufferStatus();

  // normal render mode
  unbind();
}

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

void
FBO::
addStencilBuffer( GLuint _width, GLuint _height )
{
374
375
376
  if (stencilbuffer_)
    glDeleteRenderbuffersEXT(1, &stencilbuffer_);

Jan Möbius's avatar
Jan Möbius committed
377
378
379
380
381
382
383
  // create renderbuffer
  glGenRenderbuffersEXT(1, &stencilbuffer_);

  // bind renderbuffer
  glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, stencilbuffer_);

  // malloc
384
385
386
#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
387
  glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_STENCIL_INDEX, _width, _height);
388
#endif
Jan Möbius's avatar
Jan Möbius committed
389
390
391

  // attach to framebuffer object
  if ( bind() )
392
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_STENCIL_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, stencilbuffer_);
Jan Möbius's avatar
Jan Möbius committed
393
394
395
396
397
398
399
400

  // check status
  checkFramebufferStatus();

  // normal render mode
  unbind();
}

401
402
403
404
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
//-----------------------------------------------------------------------------

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
441
442
443
444
445
446
447
448


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

bool
FBO::
bind()
{
449
450
  // save previous fbo id
  glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, (GLint*)&prevFbo_);
451
  glGetIntegerv(GL_DRAW_BUFFER, (GLint*)&prevDrawBuffer_);
452

453
454
455
456
457
458
  if ( !fbo_ )
    init();

  if ( !fbo_)
    return false;

459
460
  // bind framebuffer object
  ACG::GLState::bindFramebuffer( GL_FRAMEBUFFER_EXT, fbo_ );
Jan Möbius's avatar
Jan Möbius committed
461

462

463
  return true;
Jan Möbius's avatar
Jan Möbius committed
464
465
466
467
468
469
470
471
}

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

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

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

bool
FBO::
checkFramebufferStatus()
{
483
484
  GLenum status;
  status = ( GLenum ) glCheckFramebufferStatusEXT( GL_FRAMEBUFFER_EXT );
Jan Möbius's avatar
Jan Möbius committed
485
  //std::cout << "Framebuffer status: " << status << std::endl;
486
487
488
  switch ( status )
  {
    case GL_FRAMEBUFFER_COMPLETE_EXT:
Jan Möbius's avatar
Jan Möbius committed
489
      //std::cout << "Framebuffer ok\n";
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
      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;
512
513
514
    case 0x8D56: // GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE
      std::cout << "Framebuffer incomplete, attached images must have same multisample count\n";
      break;
515
516
517
518
519
520
521
522
523
524
525
526
    default:
      std::cout << "Unhandled case\n";
      break;
  }

  return ( status == GL_FRAMEBUFFER_COMPLETE_EXT );
}

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

GLuint FBO::getAttachment( GLenum _attachment )
{
527
  return attachments_[_attachment].id;
528
529
530
531
}

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

Christopher Tenter's avatar
Christopher Tenter committed
532
533
534
535
536
537
538
GLuint FBO::getInternalFormat( GLenum _attachment )
{
  return attachments_[_attachment].internalFormat;
}

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

539
void FBO::resize( GLsizei _width, GLsizei _height, bool _forceResize )
540
{
541
  if (_width != width_ ||_height != height_ || _forceResize)
542
  {
543
544
545
    // 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
546

547
548
549
    if (fbo_)
      glDeleteFramebuffersEXT(1, &fbo_);
    glGenFramebuffersEXT(1, &fbo_);
Christopher Tenter's avatar
Christopher Tenter committed
550

551
552
553
    // "detach" all textures
    AttachmentList temp;
    temp.swap(attachments_);
Christopher Tenter's avatar
Christopher Tenter committed
554

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

560
561
      // only resize textures that are owned by the FBO
      if (rt->owner)
562
      {
563
564
        glDeleteTextures(1, &rt->id);

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

574
        case GL_TEXTURE_3D:
Christopher Tenter's avatar
Christopher Tenter committed
575
          attachTexture3D(it->first, _width, _height, rt->dim[2], rt->internalFormat, rt->format, rt->wrapMode, rt->minFilter, rt->magFilter);
576
          break;
577

578
579
580
        default:
          std::cout << "FBO::resize - unknown resize target " << rt->target << std::endl;
        }
Christopher Tenter's avatar
Christopher Tenter committed
581
      }
582
583
    }

584
585
586
    // reattach render buffers
    if(depthbuffer_)
      addDepthBuffer(_width, _height);
587

588
589
    if(stencilbuffer_)
      addStencilBuffer(_width, _height);
590
591
592
593
594
595
  }
}

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

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

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

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

Christopher Tenter's avatar
Christopher Tenter committed
612
613
    // 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
614

Christopher Tenter's avatar
Christopher Tenter committed
615
616
617
    if (_samples)
    {
      int safeSampleCount = 1;
618

Christopher Tenter's avatar
Christopher Tenter committed
619
620
      while (safeSampleCount < _samples)
        safeSampleCount <<= 1;
621

Christopher Tenter's avatar
Christopher Tenter committed
622
623
      while (safeSampleCount >= maxSamples)
        safeSampleCount >>= 1;
624

Christopher Tenter's avatar
Christopher Tenter committed
625
626
627
628
      _samples = safeSampleCount;
    }

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

Christopher Tenter's avatar
Christopher Tenter committed
630
631
632
633
634
635
636
637
638
639
    samples_ = _samples;
  }

  fixedsamplelocation_ = _fixedsamplelocations;

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

Jan Möbius's avatar
Jan Möbius committed
642
643
644
645

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