VTK  9.0.20210926
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);
199 
203  void Bind();
204  void Bind(unsigned int mode);
205 
209  void UnBind();
210  void UnBind(unsigned int mode);
211 
213 
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  }
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 
289 
294 
296 
305 
307 
312  bool PopulateFramebuffer(int width, int height);
313  bool PopulateFramebuffer(int width, int height, bool useTextures, int numberOfColorAttachments,
314  int colorDataType, bool wantDepthAttachment, int depthBitplanes, int multisamples,
315  bool wantStencilAttachment = false);
317 
324 
331 
333 
336  int* GetLastSize() override
337  {
338  vtkDebugMacro(<< this->GetClassName() << " (" << this << "): returning LastSize pointer "
339  << this->LastSize);
340  return this->LastSize;
341  }
342  void GetLastSize(int& _arg1, int& _arg2) override
343  {
344  _arg1 = this->LastSize[0];
345  _arg2 = this->LastSize[1];
346  vtkDebugMacro(<< this->GetClassName() << " (" << this << "): returning LastSize (" << _arg1
347  << "," << _arg2 << ")");
348  }
349  void GetLastSize(int _arg[2]) override { this->GetLastSize(_arg[0], _arg[1]); }
351 
356  static bool IsSupported(vtkOpenGLRenderWindow*) { return true; }
357 
362  int CheckFrameBufferStatus(unsigned int mode);
363 
368 
375  static bool GetFrameBufferStatus(unsigned int mode, const char*& desc);
376 
377  vtkGetMacro(FBOIndex, unsigned int);
378 
385  static int Blit(
386  const int srcExt[4], const int destExt[4], unsigned int bits, unsigned int mapping);
387 
396  vtkPixelBufferObject* DownloadColor1(int extent[4], int vtkType, int channel);
397 
399 
401 
408 
417  int extent[4], int vtkType, int nComps, int oglType, int oglFormat);
418 
419  static void Download(
420  int extent[4], int vtkType, int nComps, int oglType, int oglFormat, vtkPixelBufferObject* pbo);
421 
422  // returns the mode values for draw/read/both
423  // Can be used in cases where you do not
424  // want to have OpenGL code mixed in.
425  static unsigned int GetDrawMode();
426  static unsigned int GetReadMode();
427  static unsigned int GetBothMode();
428 
432  void Resize(int width, int height);
433 
435 
436 protected:
440  void AttachColorBuffer(unsigned int index);
442 
446  static bool LoadRequiredExtensions(vtkOpenGLRenderWindow*) { return true; }
447 
449 
450  // gen buffer (occurs when context is set)
451  void CreateFBO();
452 
453  // delete buffer (occurs during destruction or context switch)
454  void DestroyFBO();
455 
456  // detach and delete our reference(s)
459 
460  // glDrawBuffers
462 
463  // examine attachments to see if they have the same size
464  void UpdateSize();
465 
470 
474  void DisplayFrameBufferAttachment(unsigned int uattachment);
475 
480 
485 
489  void DisplayBuffer(int value);
490 
494  int GetOpenGLType(int vtkType);
495 
498 
500 
501  unsigned int FBOIndex;
502 
507 
508  int LastSize[2];
509  std::vector<unsigned int> ActiveBuffers;
510  unsigned int ActiveReadBuffer;
511 
512  vtkFOInfo* DepthBuffer;
513  std::map<unsigned int, vtkFOInfo*> ColorBuffers;
514 
515 private:
517  void operator=(const vtkOpenGLFramebufferObject&) = delete;
518 };
519 
520 #endif
vtkOpenGLFramebufferObject::GetLastSize
int * GetLastSize() override
Dimensions in pixels of the framebuffer.
Definition: vtkOpenGLFramebufferObject.h:336
vtkOpenGLFramebufferObject::GetDrawMode
static unsigned int GetDrawMode()
vtkOpenGLFramebufferObject::DisplayReadBuffer
void DisplayReadBuffer()
Display the read buffer.
vtkOpenGLFramebufferObject::UnBind
void UnBind(unsigned int mode)
vtkOpenGLFramebufferObject::SaveCurrentBindingsAndBuffers
void SaveCurrentBindingsAndBuffers(unsigned int mode)
Store/Restore the current framebuffer bindings and buffers.
vtkOpenGLFramebufferObject::DrawBufferSaved
bool DrawBufferSaved
Definition: vtkOpenGLFramebufferObject.h:505
vtkOpenGLFramebufferObject::UpdateSize
void UpdateSize()
vtkX3D::value
@ value
Definition: vtkX3D.h:226
vtkOpenGLFramebufferObject::DeactivateDrawBuffers
void DeactivateDrawBuffers()
vtkOpenGLFramebufferObject::RestorePreviousBindingsAndBuffers
void RestorePreviousBindingsAndBuffers()
Store/Restore the current framebuffer bindings and buffers.
vtkOpenGLFramebufferObject::DrawBindingSaved
bool DrawBindingSaved
Definition: vtkOpenGLFramebufferObject.h:503
vtkOpenGLFramebufferObject::StartNonOrtho
bool StartNonOrtho(int width, int height)
User must take care that width/height match the dimensions of the user defined texture attachments.
vtkOpenGLFramebufferObject::vtkOpenGLFramebufferObject
vtkOpenGLFramebufferObject()
vtkOpenGLFramebufferObject::PopulateFramebuffer
bool PopulateFramebuffer(int width, int height)
Convenience method to populate a framebuffer with attachments created as well.
vtkOpenGLFramebufferObject::AddDepthAttachment
void AddDepthAttachment(vtkTextureObject *tex)
Directly assign/remove a texture/renderbuffer to depth attachments.
vtkOpenGLFramebufferObject
Internal class which encapsulates OpenGL FramebufferObject.
Definition: vtkOpenGLFramebufferObject.h:182
vtkOpenGLFramebufferObject::RemoveDepthAttachment
void RemoveDepthAttachment()
Directly assign/remove a texture/renderbuffer to depth attachments.
vtkOpenGLFramebufferObject::GetMaximumNumberOfRenderTargets
unsigned int GetMaximumNumberOfRenderTargets()
Returns the maximum number of render targets available.
vtkOpenGLFramebufferObject::GetLastSize
void GetLastSize(int _arg[2]) override
Dimensions in pixels of the framebuffer.
Definition: vtkOpenGLFramebufferObject.h:349
vtkOpenGLFramebufferObject::ActivateReadBuffer
void ActivateReadBuffer(unsigned int id)
vtkOpenGLFramebufferObject::GetActiveDrawBuffer
unsigned int GetActiveDrawBuffer(unsigned int id)
vtkOpenGLFramebufferObject::DownloadColor3
vtkPixelBufferObject * DownloadColor3(int extent[4], int vtkType)
vtkOpenGLFramebufferObject::DisplayDrawBuffers
void DisplayDrawBuffers()
Display the draw buffers.
vtkOpenGLFramebufferObject::DownloadDepth
vtkPixelBufferObject * DownloadDepth(int extent[4], int vtkType)
Download data from the depth attachment of the currently bound FBO.
vtkOpenGLFramebufferObject::GetNumberOfColorAttachments
int GetNumberOfColorAttachments()
Return the number of color attachments for the given mode.
vtkOpenGLFramebufferObject::LoadRequiredExtensions
static bool LoadRequiredExtensions(vtkOpenGLRenderWindow *)
Load all necessary extensions.
Definition: vtkOpenGLFramebufferObject.h:446
vtkOpenGLFramebufferObject::DownloadColor4
vtkPixelBufferObject * DownloadColor4(int extent[4], int vtkType)
vtkFrameBufferObjectBase
abstract interface to OpenGL FBOs
Definition: vtkFrameBufferObjectBase.h:33
vtkWindow
window superclass for vtkRenderWindow
Definition: vtkWindow.h:39
vtkOpenGLFramebufferObject::ActivateDrawBuffers
void ActivateDrawBuffers(unsigned int n)
vtkOpenGLFramebufferObject::Start
bool Start(int width, int height)
User must take care that width/height match the dimensions of the user defined texture attachments.
vtkOpenGLFramebufferObject::GetContext
vtkOpenGLRenderWindow * GetContext()
Get/Set the context.
vtkOpenGLFramebufferObject::DisplayBuffer
void DisplayBuffer(int value)
Display any buffer (convert value into string).
vtkOpenGLFramebufferObject::ActivateDrawBuffers
void ActivateDrawBuffers(unsigned int *ids, int n)
vtkOpenGLFramebufferObject::CreateFBO
void CreateFBO()
vtkObjectBase::GetClassName
const char * GetClassName() const
Return the class name as a string.
vtkOpenGLFramebufferObject::Download
vtkPixelBufferObject * Download(int extent[4], int vtkType, int nComps, int oglType, int oglFormat)
Download data from the read buffer of the current FBO.
vtkOpenGLFramebufferObject::New
static vtkOpenGLFramebufferObject * New()
vtkOpenGLFramebufferObject::RemoveColorAttachment
void RemoveColorAttachment(unsigned int index)
Directly assign/remove a texture to color attachments.
vtkOpenGLFramebufferObject::CheckFrameBufferStatus
int CheckFrameBufferStatus(unsigned int mode)
Validate the current FBO configuration (attachments, formats, etc) prints detected errors to vtkError...
vtkX3D::height
@ height
Definition: vtkX3D.h:260
vtkOpenGLFramebufferObject::InitializeViewport
void InitializeViewport(int width, int height)
Set up ortho viewport with scissor, lighting, blend, and depth disabled.
vtkOpenGLFramebufferObject::ReadBufferSaved
bool ReadBufferSaved
Definition: vtkOpenGLFramebufferObject.h:506
vtkOpenGLFramebufferObject::GetMultiSamples
int GetMultiSamples()
vtkShaderProgram
The ShaderProgram uses one or more Shader objects.
Definition: vtkShaderProgram.h:124
vtkGenericOpenGLResourceFreeCallback
Definition: vtkOpenGLResourceFreeCallback.h:26
vtkOpenGLFramebufferObject::Resize
void Resize(int width, int height)
Resize all FO attachments.
vtkOpenGLFramebufferObject::DestroyDepthBuffer
void DestroyDepthBuffer(vtkWindow *win)
vtkIndent
a simple class to control print indentation
Definition: vtkIndent.h:113
vtkTextureObject
abstracts an OpenGL texture object.
Definition: vtkTextureObject.h:60
vtkOpenGLFramebufferObject::PrintSelf
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
vtkOpenGLFramebufferObject::IsSupported
static bool IsSupported(vtkOpenGLRenderWindow *)
Returns if the context supports the required extensions.
Definition: vtkOpenGLFramebufferObject.h:356
vtkOpenGLFramebufferObject::Bind
void Bind()
Make the draw frame buffer active.
vtkOpenGLFramebufferObject::DepthBuffer
vtkFOInfo * DepthBuffer
Definition: vtkOpenGLFramebufferObject.h:512
vtkOpenGLFramebufferObject::DeactivateReadBuffer
void DeactivateReadBuffer()
vtkOpenGLFramebufferObject::ResourceCallback
vtkGenericOpenGLResourceFreeCallback * ResourceCallback
Definition: vtkOpenGLFramebufferObject.h:448
vtkOpenGLFramebufferObject::ActiveReadBuffer
unsigned int ActiveReadBuffer
Definition: vtkOpenGLFramebufferObject.h:510
vtkOpenGLFramebufferObject::GetDepthAttachmentAsTextureObject
vtkTextureObject * GetDepthAttachmentAsTextureObject()
Directly assign/remove a texture/renderbuffer to depth attachments.
vtkOpenGLFramebufferObject::Blit
static int Blit(const int srcExt[4], const int destExt[4], unsigned int bits, unsigned int mapping)
Copy from the currently bound READ FBO to the currently bound DRAW FBO.
vtkOpenGLFramebufferObject::ReadBindingSaved
bool ReadBindingSaved
Definition: vtkOpenGLFramebufferObject.h:504
vtkOpenGLFramebufferObject::AttachColorBuffer
void AttachColorBuffer(unsigned int index)
Attach a specific buffer.
vtkOpenGLFramebufferObject::GetColorAttachmentAsTextureObject
vtkTextureObject * GetColorAttachmentAsTextureObject(unsigned int num)
Return the texture object bound to the passed attachment number.
vtkOpenGLFramebufferObject::ActivateBuffers
void ActivateBuffers()
vtkOpenGLFramebufferObject::GetOpenGLType
int GetOpenGLType(int vtkType)
Given a vtk type get a compatible open gl type.
vtkOpenGLFramebufferObject::PopulateFramebuffer
bool PopulateFramebuffer(int width, int height, bool useTextures, int numberOfColorAttachments, int colorDataType, bool wantDepthAttachment, int depthBitplanes, int multisamples, bool wantStencilAttachment=false)
Convenience method to populate a framebuffer with attachments created as well.
vtkOpenGLFramebufferObject::AddDepthAttachment
void AddDepthAttachment()
Directly assign/remove a texture/renderbuffer to depth attachments.
vtkOpenGLFramebufferObject::DisplayFrameBufferAttachment
void DisplayFrameBufferAttachment(unsigned int uattachment)
Display a given attachment for the current framebuffer object.
vtkOpenGLFramebufferObject::FBOIndex
unsigned int FBOIndex
Definition: vtkOpenGLFramebufferObject.h:501
vtkOpenGLFramebufferObject::AddDepthAttachment
void AddDepthAttachment(vtkRenderbuffer *tex)
Directly assign/remove a texture/renderbuffer to depth attachments.
vtkOpenGLFramebufferObject::RenderQuad
void RenderQuad(int minX, int maxX, int minY, int maxY, vtkShaderProgram *program, vtkOpenGLVertexArrayObject *vao)
Renders a quad at the given location with pixel coordinates.
vtkOpenGLFramebufferObject::DestroyColorBuffers
void DestroyColorBuffers(vtkWindow *win)
vtkOpenGLFramebufferObject::SetContext
void SetContext(vtkRenderWindow *context)
Get/Set the context.
vtkOpenGLFramebufferObject::~vtkOpenGLFramebufferObject
~vtkOpenGLFramebufferObject() override
vtkOpenGLFramebufferObject::RemoveColorAttachments
void RemoveColorAttachments(unsigned int num)
Directly assign/remove a texture to color attachments.
vtkOpenGLFramebufferObject::Download
static void Download(int extent[4], int vtkType, int nComps, int oglType, int oglFormat, vtkPixelBufferObject *pbo)
vtkOpenGLFramebufferObject::Bind
void Bind(unsigned int mode)
vtkOpenGLFramebufferObject::RestorePreviousBindingsAndBuffers
void RestorePreviousBindingsAndBuffers(unsigned int mode)
Store/Restore the current framebuffer bindings and buffers.
vtkFrameBufferObjectBase.h
vtkOpenGLFramebufferObject::ColorBuffers
std::map< unsigned int, vtkFOInfo * > ColorBuffers
Definition: vtkOpenGLFramebufferObject.h:513
vtkOpenGLFramebufferObject::GetBothMode
static unsigned int GetBothMode()
vtkOpenGLFramebufferObject::DownloadColor1
vtkPixelBufferObject * DownloadColor1(int extent[4], int vtkType, int channel)
Download data from the read color attachment of the currently bound FBO into the returned PBO.
vtkOpenGLFramebufferObject::ReleaseGraphicsResources
virtual void ReleaseGraphicsResources(vtkWindow *win)
Deactivate and UnBind.
vtkOpenGLFramebufferObject::AddColorAttachment
void AddColorAttachment(unsigned int attId, vtkRenderbuffer *tex)
Directly assign/remove a texture to color attachments.
vtkX3D::mode
@ mode
Definition: vtkX3D.h:253
vtkOpenGLFramebufferObject::AddColorAttachment
void AddColorAttachment(unsigned int attId, vtkTextureObject *tex, unsigned int zslice=0, unsigned int format=0, unsigned int mipmapLevel=0)
Directly assign/remove a texture to color attachments.
vtkOpenGLRenderWindow
OpenGL rendering window.
Definition: vtkOpenGLRenderWindow.h:83
vtkX3D::extent
@ extent
Definition: vtkX3D.h:351
vtkRenderWindow
create a window for renderers to draw into
Definition: vtkRenderWindow.h:205
vtkOpenGLFramebufferObject::GetLastSize
void GetLastSize(int &_arg1, int &_arg2) override
Dimensions in pixels of the framebuffer.
Definition: vtkOpenGLFramebufferObject.h:342
vtkOpenGLFramebufferObject::ActiveBuffers
std::vector< unsigned int > ActiveBuffers
Definition: vtkOpenGLFramebufferObject.h:509
vtkOpenGLFramebufferObject::GetFrameBufferStatus
static bool GetFrameBufferStatus(unsigned int mode, const char *&desc)
Validate the current FBO configuration (attachments, formats, etc) return false if the FBO is incompl...
vtkOpenGLFramebufferObject::UnBind
void UnBind()
Unbind this buffer.
vtkOpenGLFramebufferObject::GetMaximumNumberOfActiveTargets
unsigned int GetMaximumNumberOfActiveTargets()
Returns the maximum number of targets that can be rendered to at one time.
vtkOpenGLFramebufferObject::AttachDepthBuffer
void AttachDepthBuffer()
vtkX3D::index
@ index
Definition: vtkX3D.h:252
vtkOpenGLFramebufferObject::SaveCurrentBindingsAndBuffers
void SaveCurrentBindingsAndBuffers()
Store/Restore the current framebuffer bindings and buffers.
vtkOpenGLFramebufferObject::DisplayFrameBufferAttachments
void DisplayFrameBufferAttachments()
Display all the attachments of the current framebuffer object.
vtkOpenGLVertexArrayObject
The VertexArrayObject class uses, or emulates, vertex array objects.
Definition: vtkOpenGLVertexArrayObject.h:37
vtkOpenGLFramebufferObject::ActivateDrawBuffer
void ActivateDrawBuffer(unsigned int id)
vtkOpenGLFramebufferObject::GetReadMode
static unsigned int GetReadMode()
vtkPixelBufferObject
abstracts an OpenGL pixel buffer object.
Definition: vtkPixelBufferObject.h:44
vtkOpenGLFramebufferObject::DestroyFBO
void DestroyFBO()
vtkRenderbuffer
Storage for FBO's.
Definition: vtkRenderbuffer.h:33
vtkOpenGLFramebufferObject::ActivateBuffer
void ActivateBuffer(unsigned int id)
Definition: vtkOpenGLFramebufferObject.h:248
vtkOpenGLFramebufferObject::Context
vtkOpenGLRenderWindow * Context
Definition: vtkOpenGLFramebufferObject.h:499