VTK  9.0.20200804
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 /* Dec 2018 this class has been cleaned up such that
133  * AddColorAttachment and AddDepthAttachment no longer
134  * take a mode argument. The mode is determined by how
135  * the framebuffer is bound. If you are using these methods
136  * and need to support both the old and new signatures you
137  * can check for the following define in your code.
138  */
139 #define VTK_UPDATED_FRAMEBUFFER
140 
147 #ifdef NDEBUG
148 #define vtkCheckFrameBufferStatusMacro(mode)
149 #define vtkStaticCheckFrameBufferStatusMacro(mode)
150 #else
151 #define vtkCheckFrameBufferStatusMacroImpl(macro, mode) \
152  { \
153  const char* eStr; \
154  bool ok = vtkOpenGLFramebufferObject::GetFrameBufferStatus(mode, eStr); \
155  if (!ok) \
156  { \
157  macro(<< "OpenGL ERROR. The FBO is incomplete : " << eStr); \
158  } \
159  }
160 #define vtkCheckFrameBufferStatusMacro(mode) vtkCheckFrameBufferStatusMacroImpl(vtkErrorMacro, mode)
161 #define vtkStaticCheckFrameBufferStatusMacro(mode) \
162  vtkCheckFrameBufferStatusMacroImpl(vtkGenericWarningMacro, mode)
163 #endif
164 
166 #include "vtkRenderingOpenGL2Module.h" // For export macro
167 #include <map> // for the maps
168 #include <vector> // for the lists of logical buffers.
169 
170 class vtkFOInfo;
175 class vtkRenderWindow;
176 class vtkRenderbuffer;
177 class vtkShaderProgram;
178 class vtkTextureObject;
179 class vtkWindow;
180 
181 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLFramebufferObject : public vtkFrameBufferObjectBase
182 {
183 public:
186  void PrintSelf(ostream& os, vtkIndent indent) override;
187 
189 
196  void SetContext(vtkRenderWindow* context);
197  vtkOpenGLRenderWindow* GetContext();
199 
203  void Bind();
204  void Bind(unsigned int mode);
205 
209  void UnBind();
210  void UnBind(unsigned int mode);
211 
213 
216  void SaveCurrentBindingsAndBuffers();
217  void SaveCurrentBindingsAndBuffers(unsigned int mode);
218  void RestorePreviousBindingsAndBuffers();
219  void RestorePreviousBindingsAndBuffers(unsigned int mode);
221 
223 
232  bool Start(int width, int height);
233  bool StartNonOrtho(int width, int height);
235 
240  void InitializeViewport(int width, int height);
241 
243  // activate deactivate draw/read buffers (color buffers)
244  void ActivateDrawBuffers(unsigned int n);
245  void ActivateDrawBuffers(unsigned int* ids, int n);
246  void ActivateDrawBuffer(unsigned int id);
247  void ActivateReadBuffer(unsigned int id);
248  void ActivateBuffer(unsigned int id)
249  {
250  this->ActivateDrawBuffer(id);
251  this->ActivateReadBuffer(id);
252  }
253  void DeactivateDrawBuffers();
254  void DeactivateReadBuffer();
256 
257  vtkGetMacro(ActiveReadBuffer, unsigned int);
258  unsigned int GetActiveDrawBuffer(unsigned int id);
259 
270  void RenderQuad(int minX, int maxX, int minY, int maxY, vtkShaderProgram* program,
272 
274 
277  void AddColorAttachment(unsigned int attId, vtkTextureObject* tex, unsigned int zslice = 0,
278  unsigned int format = 0, unsigned int mipmapLevel = 0);
279  void AddColorAttachment(unsigned int attId, vtkRenderbuffer* tex);
280  void RemoveColorAttachment(unsigned int index);
281  void RemoveColorAttachments(unsigned int num);
283 
288  vtkTextureObject* GetColorAttachmentAsTextureObject(unsigned int num);
289 
293  int GetNumberOfColorAttachments();
294 
296 
299  void AddDepthAttachment();
300  void AddDepthAttachment(vtkTextureObject* tex);
301  void AddDepthAttachment(vtkRenderbuffer* tex);
302  void RemoveDepthAttachment();
304 
306 
311  bool PopulateFramebuffer(int width, int height);
312  bool PopulateFramebuffer(int width, int height, bool useTextures, int numberOfColorAttachments,
313  int colorDataType, bool wantDepthAttachment, int depthBitplanes, int multisamples,
314  bool wantStencilAttachment = false);
316 
322  unsigned int GetMaximumNumberOfActiveTargets();
323 
329  unsigned int GetMaximumNumberOfRenderTargets();
330 
332 
335  int* GetLastSize() override
336  {
337  vtkDebugMacro(<< this->GetClassName() << " (" << this << "): returning LastSize pointer "
338  << this->LastSize);
339  return this->LastSize;
340  }
341  void GetLastSize(int& _arg1, int& _arg2) override
342  {
343  _arg1 = this->LastSize[0];
344  _arg2 = this->LastSize[1];
345  vtkDebugMacro(<< this->GetClassName() << " (" << this << "): returning LastSize (" << _arg1
346  << "," << _arg2 << ")");
347  }
348  void GetLastSize(int _arg[2]) override { this->GetLastSize(_arg[0], _arg[1]); }
350 
355  static bool IsSupported(vtkOpenGLRenderWindow*) { return true; }
356 
361  int CheckFrameBufferStatus(unsigned int mode);
362 
366  virtual void ReleaseGraphicsResources(vtkWindow* win);
367 
374  static bool GetFrameBufferStatus(unsigned int mode, const char*& desc);
375 
376  vtkGetMacro(FBOIndex, unsigned int);
377 
384  static int Blit(
385  const int srcExt[4], const int destExt[4], unsigned int bits, unsigned int mapping);
386 
395  vtkPixelBufferObject* DownloadColor1(int extent[4], int vtkType, int channel);
396 
397  vtkPixelBufferObject* DownloadColor3(int extent[4], int vtkType);
398 
399  vtkPixelBufferObject* DownloadColor4(int extent[4], int vtkType);
400 
406  vtkPixelBufferObject* DownloadDepth(int extent[4], int vtkType);
407 
415  vtkPixelBufferObject* Download(
416  int extent[4], int vtkType, int nComps, int oglType, int oglFormat);
417 
418  static void Download(
419  int extent[4], int vtkType, int nComps, int oglType, int oglFormat, vtkPixelBufferObject* pbo);
420 
421  // returns the mode values for draw/read/both
422  // Can be used in cases where you do not
423  // want to have OpenGL code mixed in.
424  static unsigned int GetDrawMode();
425  static unsigned int GetReadMode();
426  static unsigned int GetBothMode();
427 
431  void Resize(int width, int height);
432 
433  int GetMultiSamples();
434 
435 protected:
439  void AttachColorBuffer(unsigned int index);
440  void AttachDepthBuffer();
441 
445  static bool LoadRequiredExtensions(vtkOpenGLRenderWindow*) { return true; }
446 
448 
449  // gen buffer (occurs when context is set)
450  void CreateFBO();
451 
452  // delete buffer (occurs during destruction or context switch)
453  void DestroyFBO();
454 
455  // detach and delete our reference(s)
456  void DestroyDepthBuffer(vtkWindow* win);
457  void DestroyColorBuffers(vtkWindow* win);
458 
459  // glDrawBuffers
460  void ActivateBuffers();
461 
462  // examine attachments to see if they have the same size
463  void UpdateSize();
464 
468  void DisplayFrameBufferAttachments();
469 
473  void DisplayFrameBufferAttachment(unsigned int uattachment);
474 
478  void DisplayDrawBuffers();
479 
483  void DisplayReadBuffer();
484 
488  void DisplayBuffer(int value);
489 
493  int GetOpenGLType(int vtkType);
494 
496  ~vtkOpenGLFramebufferObject() override;
497 
499 
500  unsigned int FBOIndex;
501 
506 
507  int LastSize[2];
508  std::vector<unsigned int> ActiveBuffers;
509  unsigned int ActiveReadBuffer;
510 
511  vtkFOInfo* DepthBuffer;
512  std::map<unsigned int, vtkFOInfo*> ColorBuffers;
513 
514 private:
516  void operator=(const vtkOpenGLFramebufferObject&) = delete;
517 };
518 
519 #endif
vtkOpenGLFramebufferObject::GetLastSize
int * GetLastSize() override
Dimensions in pixels of the framebuffer.
Definition: vtkOpenGLFramebufferObject.h:335
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:504
vtkX3D::value
Definition: vtkX3D.h:226
vtkOpenGLFramebufferObject::DrawBindingSaved
bool DrawBindingSaved
Definition: vtkOpenGLFramebufferObject.h:502
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:181
vtkOpenGLFramebufferObject::GetLastSize
void GetLastSize(int _arg[2]) override
Definition: vtkOpenGLFramebufferObject.h:348
vtkOpenGLFramebufferObject::LoadRequiredExtensions
static bool LoadRequiredExtensions(vtkOpenGLRenderWindow *)
Load all necessary extensions.
Definition: vtkOpenGLFramebufferObject.h:445
vtkFrameBufferObjectBase
abstract interface to OpenGL FBOs
Definition: vtkFrameBufferObjectBase.h:32
vtkWindow
window superclass for vtkRenderWindow
Definition: vtkWindow.h:38
vtkObjectBase::GetClassName
const char * GetClassName() const
Return the class name as a string.
vtkX3D::height
Definition: vtkX3D.h:260
vtkOpenGLFramebufferObject::ReadBufferSaved
bool ReadBufferSaved
Definition: vtkOpenGLFramebufferObject.h:505
vtkShaderProgram
The ShaderProgram uses one or more Shader objects.
Definition: vtkShaderProgram.h:44
vtkGenericOpenGLResourceFreeCallback
Definition: vtkOpenGLResourceFreeCallback.h:25
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:33
vtkTextureObject
abstracts an OpenGL texture object.
Definition: vtkTextureObject.h:40
vtkOpenGLFramebufferObject::IsSupported
static bool IsSupported(vtkOpenGLRenderWindow *)
Returns if the context supports the required extensions.
Definition: vtkOpenGLFramebufferObject.h:355
vtkOpenGLFramebufferObject::DepthBuffer
vtkFOInfo * DepthBuffer
Definition: vtkOpenGLFramebufferObject.h:511
vtkOpenGLFramebufferObject::ResourceCallback
vtkGenericOpenGLResourceFreeCallback * ResourceCallback
Definition: vtkOpenGLFramebufferObject.h:447
vtkOpenGLFramebufferObject::ActiveReadBuffer
unsigned int ActiveReadBuffer
Definition: vtkOpenGLFramebufferObject.h:509
vtkOpenGLFramebufferObject::ReadBindingSaved
bool ReadBindingSaved
Definition: vtkOpenGLFramebufferObject.h:503
vtkOpenGLFramebufferObject::FBOIndex
unsigned int FBOIndex
Definition: vtkOpenGLFramebufferObject.h:500
vtkFrameBufferObjectBase.h
vtkOpenGLFramebufferObject::ColorBuffers
std::map< unsigned int, vtkFOInfo * > ColorBuffers
Definition: vtkOpenGLFramebufferObject.h:512
vtkX3D::mode
Definition: vtkX3D.h:253
vtkOpenGLRenderWindow
OpenGL rendering window.
Definition: vtkOpenGLRenderWindow.h:53
vtkX3D::extent
Definition: vtkX3D.h:351
vtkRenderWindow
create a window for renderers to draw into
Definition: vtkRenderWindow.h:94
vtkOpenGLFramebufferObject::GetLastSize
void GetLastSize(int &_arg1, int &_arg2) override
Definition: vtkOpenGLFramebufferObject.h:341
vtkOpenGLFramebufferObject::ActiveBuffers
std::vector< unsigned int > ActiveBuffers
Definition: vtkOpenGLFramebufferObject.h:508
vtkX3D::index
Definition: vtkX3D.h:252
vtkOpenGLVertexArrayObject
The VertexArrayObject class uses, or emulates, vertex array objects.
Definition: vtkOpenGLVertexArrayObject.h:36
vtkPixelBufferObject
abstracts an OpenGL pixel buffer object.
Definition: vtkPixelBufferObject.h:43
vtkRenderbuffer
Storage for FBO's.
Definition: vtkRenderbuffer.h:32
vtkOpenGLFramebufferObject::ActivateBuffer
void ActivateBuffer(unsigned int id)
Definition: vtkOpenGLFramebufferObject.h:248
vtkOpenGLFramebufferObject::Context
vtkOpenGLRenderWindow * Context
Definition: vtkOpenGLFramebufferObject.h:498