VTK
vtkOpenGLFramebufferObject.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkOpenGLFramebufferObject.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
129 #ifndef vtkOpenGLFramebufferObject_h
130 #define vtkOpenGLFramebufferObject_h
131 
132 
133 /* Dec 2018 this class has been cleaned up such that
134  * AddColorAttachment and AddDepthAttachment no longer
135  * take a mode argument. The mode is determined by how
136  * the framebuffer is bound. If you are using these methods
137  * and need to support both the old and new signatures you
138  * can check for the following define in your code.
139  */
140 #define VTK_UPDATED_FRAMEBUFFER
141 
148 #ifdef NDEBUG
149 # define vtkCheckFrameBufferStatusMacro(mode)
150 # define vtkStaticCheckFrameBufferStatusMacro(mode)
151 #else
152 # define vtkCheckFrameBufferStatusMacroImpl(macro, mode) \
153 { \
154 const char *eStr; \
155 bool ok = vtkOpenGLFramebufferObject::GetFrameBufferStatus(mode, eStr); \
156 if (!ok) \
157 { \
158  macro( \
159  << "OpenGL ERROR. The FBO is incomplete : " << eStr); \
160 } \
161  }
162 # define vtkCheckFrameBufferStatusMacro(mode) \
163  vtkCheckFrameBufferStatusMacroImpl(vtkErrorMacro, mode)
164 # define vtkStaticCheckFrameBufferStatusMacro(mode) \
165  vtkCheckFrameBufferStatusMacroImpl(vtkGenericWarningMacro, mode)
166 #endif
167 
168 
170 #include "vtkRenderingOpenGL2Module.h" // For export macro
171 #include "vtkWeakPointer.h" // needed for vtkWeakPointer.
172 #include <map> // for the maps
173 #include <vector> // for the lists of logical buffers.
174 
175 class vtkFOInfo;
180 class vtkRenderWindow;
181 class vtkRenderbuffer;
182 class vtkShaderProgram;
183 class vtkTextureObject;
184 class vtkWindow;
185 
186 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLFramebufferObject : public vtkFrameBufferObjectBase
187 {
188 public:
191  void PrintSelf(ostream& os, vtkIndent indent) override;
192 
194 
201  void SetContext(vtkRenderWindow *context);
202  vtkOpenGLRenderWindow *GetContext();
204 
208  void Bind();
209  void Bind(unsigned int mode);
210 
214  void UnBind();
215  void UnBind(unsigned int mode);
216 
218 
221  void SaveCurrentBindingsAndBuffers();
222  void SaveCurrentBindingsAndBuffers(unsigned int mode);
223  void RestorePreviousBindingsAndBuffers();
224  void RestorePreviousBindingsAndBuffers(unsigned int mode);
226 
228 
237  bool Start(int width, int height);
238  bool StartNonOrtho(int width, int height);
240 
245  void InitializeViewport(int width, int height);
246 
248  // activate deactivate draw/read buffers (color buffers)
249  void ActivateDrawBuffers(unsigned int n);
250  void ActivateDrawBuffers(unsigned int *ids, int n);
251  void ActivateDrawBuffer(unsigned int id);
252  void ActivateReadBuffer(unsigned int id);
253  void ActivateBuffer(unsigned int id) {
254  this->ActivateDrawBuffer(id);
255  this->ActivateReadBuffer(id); }
256  void DeactivateDrawBuffers();
257  void DeactivateReadBuffer();
259 
260  vtkGetMacro(ActiveReadBuffer, unsigned int);
261  unsigned int GetActiveDrawBuffer(unsigned int id);
262 
273  void RenderQuad(int minX, int maxX, int minY, int maxY,
275 
277 
280  void AddColorAttachment(
281  unsigned int attId,
282  vtkTextureObject* tex,
283  unsigned int zslice = 0,
284  unsigned int format = 0,
285  unsigned int mipmapLevel = 0);
286  void AddColorAttachment(
287  unsigned int attId,
288  vtkRenderbuffer* tex);
289  void RemoveColorAttachment(unsigned int index);
290  void RemoveColorAttachments(unsigned int num);
292 
296  int GetNumberOfColorAttachments();
297 
299 
302  void AddDepthAttachment();
303  void AddDepthAttachment(vtkTextureObject* tex);
304  void AddDepthAttachment(vtkRenderbuffer* tex);
305  void RemoveDepthAttachment();
307 
309 
314  bool PopulateFramebuffer(int width, int height);
315  bool PopulateFramebuffer(
316  int width,
317  int height,
318  bool useTextures,
319  int numberOfColorAttachments,
320  int colorDataType,
321  bool wantDepthAttachment,
322  int depthBitplanes,
323  int multisamples,
324  bool wantStencilAttachment = false
325  );
327 
333  unsigned int GetMaximumNumberOfActiveTargets();
334 
340  unsigned int GetMaximumNumberOfRenderTargets();
341 
343 
346  int *GetLastSize() override
347  {
348  vtkDebugMacro(<< this->GetClassName() << " (" << this << "): returning LastSize pointer " << this->LastSize);
349  return this->LastSize;
350  }
351  void GetLastSize(int &_arg1, int &_arg2) override
352  {
353  _arg1 = this->LastSize[0];
354  _arg2 = this->LastSize[1];
355  vtkDebugMacro(<< this->GetClassName() << " (" << this << "): returning LastSize (" << _arg1 << "," << _arg2 << ")");
356  }
357  void GetLastSize (int _arg[2]) override
358  {
359  this->GetLastSize (_arg[0], _arg[1]);
360  }
362 
368  return true; }
369 
374  int CheckFrameBufferStatus(unsigned int mode);
375 
379  virtual void ReleaseGraphicsResources(vtkWindow *win);
380 
387  static
388  bool GetFrameBufferStatus(
389  unsigned int mode,
390  const char *&desc);
391 
392  vtkGetMacro(FBOIndex, unsigned int);
393 
400  static int Blit(
401  const int srcExt[4], const int destExt[4], unsigned int bits, unsigned int mapping);
402 
411  vtkPixelBufferObject *DownloadColor1(
412  int extent[4],
413  int vtkType,
414  int channel);
415 
416  vtkPixelBufferObject *DownloadColor3(
417  int extent[4],
418  int vtkType);
419 
420  vtkPixelBufferObject *DownloadColor4(
421  int extent[4],
422  int vtkType);
423 
429  vtkPixelBufferObject *DownloadDepth(
430  int extent[4],
431  int vtkType);
432 
440  vtkPixelBufferObject *Download(
441  int extent[4],
442  int vtkType,
443  int nComps,
444  int oglType,
445  int oglFormat);
446 
447  static
448  void Download(
449  int extent[4],
450  int vtkType,
451  int nComps,
452  int oglType,
453  int oglFormat,
454  vtkPixelBufferObject *pbo);
455 
456  // returns the mode values for draw/read/both
457  // Can be used in cases where you do not
458  // want to have OpenGL code mixed in.
459  static unsigned int GetDrawMode();
460  static unsigned int GetReadMode();
461  static unsigned int GetBothMode();
462 
466  void Resize(int width, int height);
467 
468 protected:
472  void AttachColorBuffer(unsigned int index);
473  void AttachDepthBuffer();
474 
478  static
480  return true; };
481 
482  vtkGenericOpenGLResourceFreeCallback *ResourceCallback;
483 
484  // gen buffer (occurs when context is set)
485  void CreateFBO();
486 
487  // delete buffer (occurs during destruction or context switch)
488  void DestroyFBO();
489 
490  // detach and delete our reference(s)
491  void DestroyDepthBuffer(vtkWindow *win);
492  void DestroyColorBuffers(vtkWindow *win);
493 
494  // glDrawBuffers
495  void ActivateBuffers();
496 
497  // examine attachments to see if they have the same size
498  void UpdateSize();
499 
503  void DisplayFrameBufferAttachments();
504 
508  void DisplayFrameBufferAttachment(unsigned int uattachment);
509 
513  void DisplayDrawBuffers();
514 
518  void DisplayReadBuffer();
519 
523  void DisplayBuffer(int value);
524 
528  int GetOpenGLType(int vtkType);
529 
531  ~vtkOpenGLFramebufferObject() override;
532 
534 
535  unsigned int FBOIndex;
536 
541 
542  int LastSize[2];
543  std::vector<unsigned int> ActiveBuffers;
544  unsigned int ActiveReadBuffer;
545 
546  vtkFOInfo *DepthBuffer;
547  std::map<unsigned int, vtkFOInfo *> ColorBuffers;
548 
549 private:
551  void operator=(const vtkOpenGLFramebufferObject&) = delete;
552 };
553 
554 #endif
vtkOpenGLFramebufferObject::GetLastSize
int * GetLastSize() override
Dimensions in pixels of the framebuffer.
Definition: vtkOpenGLFramebufferObject.h:346
vtkFrameBufferObjectBase::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkOpenGLFramebufferObject::DrawBufferSaved
bool DrawBufferSaved
Definition: vtkOpenGLFramebufferObject.h:539
vtkX3D::value
Definition: vtkX3D.h:220
vtkOpenGLFramebufferObject::DrawBindingSaved
bool DrawBindingSaved
Definition: vtkOpenGLFramebufferObject.h:537
vtkObject::New
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on.
vtkOpenGLFramebufferObject
Internal class which encapsulates OpenGL FramebufferObject.
Definition: vtkOpenGLFramebufferObject.h:186
vtkOpenGLFramebufferObject::GetLastSize
void GetLastSize(int _arg[2]) override
Definition: vtkOpenGLFramebufferObject.h:357
vtkOpenGLFramebufferObject::Context
vtkWeakPointer< vtkOpenGLRenderWindow > Context
Definition: vtkOpenGLFramebufferObject.h:533
vtkOpenGLFramebufferObject::LoadRequiredExtensions
static bool LoadRequiredExtensions(vtkOpenGLRenderWindow *)
Load all necessary extensions.
Definition: vtkOpenGLFramebufferObject.h:479
vtkFrameBufferObjectBase
abstract interface to OpenGL FBOs
Definition: vtkFrameBufferObjectBase.h:32
vtkWindow
window superclass for vtkRenderWindow
Definition: vtkWindow.h:34
vtkObjectBase::GetClassName
const char * GetClassName() const
Return the class name as a string.
vtkX3D::height
Definition: vtkX3D.h:254
vtkOpenGLFramebufferObject::ReadBufferSaved
bool ReadBufferSaved
Definition: vtkOpenGLFramebufferObject.h:540
vtkShaderProgram
The ShaderProgram uses one or more Shader objects.
Definition: vtkShaderProgram.h:47
vtkGenericOpenGLResourceFreeCallback
Definition: vtkOpenGLResourceFreeCallback.h:25
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:39
vtkTextureObject
abstracts an OpenGL texture object.
Definition: vtkTextureObject.h:44
vtkOpenGLFramebufferObject::IsSupported
static bool IsSupported(vtkOpenGLRenderWindow *)
Returns if the context supports the required extensions.
Definition: vtkOpenGLFramebufferObject.h:367
vtkOpenGLFramebufferObject::DepthBuffer
vtkFOInfo * DepthBuffer
Definition: vtkOpenGLFramebufferObject.h:546
vtkOpenGLFramebufferObject::ActiveReadBuffer
unsigned int ActiveReadBuffer
Definition: vtkOpenGLFramebufferObject.h:544
vtkWeakPointer.h
vtkOpenGLFramebufferObject::ReadBindingSaved
bool ReadBindingSaved
Definition: vtkOpenGLFramebufferObject.h:538
vtkOpenGLFramebufferObject::FBOIndex
unsigned int FBOIndex
Definition: vtkOpenGLFramebufferObject.h:535
vtkFrameBufferObjectBase.h
vtkX3D::mode
Definition: vtkX3D.h:247
vtkOpenGLRenderWindow
OpenGL rendering window.
Definition: vtkOpenGLRenderWindow.h:54
vtkX3D::extent
Definition: vtkX3D.h:345
vtkRenderWindow
create a window for renderers to draw into
Definition: vtkRenderWindow.h:90
vtkOpenGLFramebufferObject::GetLastSize
void GetLastSize(int &_arg1, int &_arg2) override
Definition: vtkOpenGLFramebufferObject.h:351
vtkOpenGLFramebufferObject::ActiveBuffers
std::vector< unsigned int > ActiveBuffers
Definition: vtkOpenGLFramebufferObject.h:543
vtkX3D::index
Definition: vtkX3D.h:246
vtkOpenGLVertexArrayObject
The VertexArrayObject class uses, or emulates, vertex array objects.
Definition: vtkOpenGLVertexArrayObject.h:36
vtkFrameBufferObjectBase::GetLastSize
virtual int * GetLastSize()=0
Dimensions in pixels of the framebuffer.
vtkOpenGLFramebufferObject::ColorBuffers
std::map< unsigned int, vtkFOInfo * > ColorBuffers
Definition: vtkOpenGLFramebufferObject.h:547
vtkPixelBufferObject
abstracts an OpenGL pixel buffer object.
Definition: vtkPixelBufferObject.h:43
vtkWeakPointer< vtkOpenGLRenderWindow >
vtkRenderbuffer
Storage for FBO's.
Definition: vtkRenderbuffer.h:32
vtkOpenGLFramebufferObject::ActivateBuffer
void ActivateBuffer(unsigned int id)
Definition: vtkOpenGLFramebufferObject.h:253