VTK  9.2.20230603
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 =========================================================================*/
132 #ifndef vtkOpenGLFramebufferObject_h
133 #define vtkOpenGLFramebufferObject_h
134 
135 /* Dec 2018 this class has been cleaned up such that
136  * AddColorAttachment and AddDepthAttachment no longer
137  * take a mode argument. The mode is determined by how
138  * the framebuffer is bound. If you are using these methods
139  * and need to support both the old and new signatures you
140  * can check for the following define in your code.
141  */
142 #define VTK_UPDATED_FRAMEBUFFER
143 
150 #ifdef NDEBUG
151 #define vtkCheckFrameBufferStatusMacro(mode)
152 #define vtkStaticCheckFrameBufferStatusMacro(mode)
153 #else
154 #define vtkCheckFrameBufferStatusMacroImpl(macro, mode) \
155  { \
156  const char* eStr; \
157  bool ok = vtkOpenGLFramebufferObject::GetFrameBufferStatus(mode, eStr); \
158  if (!ok) \
159  { \
160  macro(<< "OpenGL ERROR. The FBO is incomplete : " << eStr); \
161  } \
162  }
163 #define vtkCheckFrameBufferStatusMacro(mode) vtkCheckFrameBufferStatusMacroImpl(vtkErrorMacro, mode)
164 #define vtkStaticCheckFrameBufferStatusMacro(mode) \
165  vtkCheckFrameBufferStatusMacroImpl(vtkGenericWarningMacro, mode)
166 #endif
167 
169 #include "vtkRenderingOpenGL2Module.h" // For export macro
170 #include <map> // for the maps
171 #include <vector> // for the lists of logical buffers.
172 
173 VTK_ABI_NAMESPACE_BEGIN
174 class vtkFOInfo;
179 class vtkRenderWindow;
180 class vtkRenderbuffer;
181 class vtkShaderProgram;
182 class vtkTextureObject;
183 class vtkWindow;
184 
185 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLFramebufferObject : public vtkFrameBufferObjectBase
186 {
187 public:
190  void PrintSelf(ostream& os, vtkIndent indent) override;
191 
193 
200  void SetContext(vtkRenderWindow* context);
203 
207  void Bind();
208  void Bind(unsigned int mode);
209 
213  void UnBind();
214  void UnBind(unsigned int mode);
215 
217 
225 
227 
236  bool Start(int width, int height);
237  bool StartNonOrtho(int width, int height);
239 
244  void InitializeViewport(int width, int height);
245 
247  // activate deactivate draw/read buffers (color buffers)
248  void ActivateDrawBuffers(unsigned int n);
249  void ActivateDrawBuffers(unsigned int* ids, int n);
250  void ActivateDrawBuffer(unsigned int id);
251  void ActivateReadBuffer(unsigned int id);
252  void ActivateBuffer(unsigned int id)
253  {
254  this->ActivateDrawBuffer(id);
255  this->ActivateReadBuffer(id);
256  }
260 
261  vtkGetMacro(ActiveReadBuffer, unsigned int);
262  unsigned int GetActiveDrawBuffer(unsigned int id);
263 
274  void RenderQuad(int minX, int maxX, int minY, int maxY, vtkShaderProgram* program,
276 
278 
281  void AddColorAttachment(unsigned int attId, vtkTextureObject* tex, unsigned int zslice = 0,
282  unsigned int format = 0, unsigned int mipmapLevel = 0);
283  void AddColorAttachment(unsigned int attId, vtkRenderbuffer* tex);
284  void RemoveColorAttachment(unsigned int index);
285  void RemoveColorAttachments(unsigned int num);
287 
293 
298 
300 
309 
311 
316  bool PopulateFramebuffer(int width, int height);
317  bool PopulateFramebuffer(int width, int height, bool useTextures, int numberOfColorAttachments,
318  int colorDataType, bool wantDepthAttachment, int depthBitplanes, int multisamples,
319  bool wantStencilAttachment = false);
321 
328 
335 
337 
340  int* GetLastSize() override
341  {
342  vtkDebugMacro(<< this->GetClassName() << " (" << this << "): returning LastSize pointer "
343  << this->LastSize);
344  return this->LastSize;
345  }
346  void GetLastSize(int& _arg1, int& _arg2) override
347  {
348  _arg1 = this->LastSize[0];
349  _arg2 = this->LastSize[1];
350  vtkDebugMacro(<< this->GetClassName() << " (" << this << "): returning LastSize (" << _arg1
351  << "," << _arg2 << ")");
352  }
353  void GetLastSize(int _arg[2]) override { this->GetLastSize(_arg[0], _arg[1]); }
355 
360  static bool IsSupported(vtkOpenGLRenderWindow*) { return true; }
361 
366  int CheckFrameBufferStatus(unsigned int mode);
367 
372 
379  static bool GetFrameBufferStatus(unsigned int mode, const char*& desc);
380 
381  vtkGetMacro(FBOIndex, unsigned int);
382 
389  static int Blit(
390  const int srcExt[4], const int destExt[4], unsigned int bits, unsigned int mapping);
391 
400  vtkPixelBufferObject* DownloadColor1(int extent[4], int vtkType, int channel);
401 
403 
405 
412 
421  int extent[4], int vtkType, int nComps, int oglType, int oglFormat);
422 
423  static void Download(
424  int extent[4], int vtkType, int nComps, int oglType, int oglFormat, vtkPixelBufferObject* pbo);
425 
426  // returns the mode values for draw/read/both
427  // Can be used in cases where you do not
428  // want to have OpenGL code mixed in.
429  static unsigned int GetDrawMode();
430  static unsigned int GetReadMode();
431  static unsigned int GetBothMode();
432 
436  void Resize(int width, int height);
437 
439 
440 protected:
444  void AttachColorBuffer(unsigned int index);
446 
450  static bool LoadRequiredExtensions(vtkOpenGLRenderWindow*) { return true; }
451 
453 
454  // gen buffer (occurs when context is set)
455  void CreateFBO();
456 
457  // delete buffer (occurs during destruction or context switch)
458  void DestroyFBO();
459 
460  // detach and delete our reference(s)
463 
464  // glDrawBuffers
466 
467  // examine attachments to see if they have the same size
468  void UpdateSize();
469 
474 
478  void DisplayFrameBufferAttachment(unsigned int uattachment);
479 
484 
489 
493  void DisplayBuffer(int value);
494 
498  int GetOpenGLType(int vtkType);
499 
502 
504 
505  unsigned int FBOIndex;
506 
511 
512  int LastSize[2];
513  std::vector<unsigned int> ActiveBuffers;
514  unsigned int ActiveReadBuffer;
515 
516  vtkFOInfo* DepthBuffer;
517  std::map<unsigned int, vtkFOInfo*> ColorBuffers;
518 
519 private:
521  void operator=(const vtkOpenGLFramebufferObject&) = delete;
522 };
523 
524 VTK_ABI_NAMESPACE_END
525 #endif
abstract interface to OpenGL FBOs
a simple class to control print indentation
Definition: vtkIndent.h:120
const char * GetClassName() const
Return the class name as a string.
Internal class which encapsulates OpenGL FramebufferObject.
void Bind()
Make the draw frame buffer active.
void ActivateDrawBuffer(unsigned int id)
~vtkOpenGLFramebufferObject() override
std::vector< unsigned int > ActiveBuffers
void SaveCurrentBindingsAndBuffers(unsigned int mode)
Store/Restore the current framebuffer bindings and buffers.
void AttachColorBuffer(unsigned int index)
Attach a specific buffer.
static bool IsSupported(vtkOpenGLRenderWindow *)
Returns if the context supports the required extensions.
void ActivateDrawBuffers(unsigned int *ids, int n)
void UnBind()
Unbind this buffer.
void InitializeViewport(int width, int height)
Set up ortho viewport with scissor, lighting, blend, and depth disabled.
void RestorePreviousBindingsAndBuffers(unsigned int mode)
Store/Restore the current framebuffer bindings and buffers.
void UnBind(unsigned int mode)
vtkPixelBufferObject * Download(int extent[4], int vtkType, int nComps, int oglType, int oglFormat)
Download data from the read buffer of the current FBO.
void GetLastSize(int &_arg1, int &_arg2) override
Dimensions in pixels of the framebuffer.
static void Download(int extent[4], int vtkType, int nComps, int oglType, int oglFormat, vtkPixelBufferObject *pbo)
void RemoveColorAttachment(unsigned int index)
Directly assign/remove a texture to color attachments.
void Bind(unsigned int mode)
void DisplayFrameBufferAttachment(unsigned int uattachment)
Display a given attachment for the current framebuffer object.
vtkTextureObject * GetColorAttachmentAsTextureObject(unsigned int num)
Return the texture object bound to the passed attachment number.
void AddDepthAttachment(vtkTextureObject *tex)
Directly assign/remove a texture/renderbuffer to depth attachments.
void RemoveColorAttachments(unsigned int num)
Directly assign/remove a texture to color attachments.
bool StartNonOrtho(int width, int height)
User must take care that width/height match the dimensions of the user defined texture attachments.
void DisplayDrawBuffers()
Display the draw buffers.
vtkTextureObject * GetDepthAttachmentAsTextureObject()
Directly assign/remove a texture/renderbuffer to depth attachments.
void RenderQuad(int minX, int maxX, int minY, int maxY, vtkShaderProgram *program, vtkOpenGLVertexArrayObject *vao)
Renders a quad at the given location with pixel coordinates.
void DestroyDepthBuffer(vtkWindow *win)
bool PopulateFramebuffer(int width, int height)
Convenience method to populate a framebuffer with attachments created as well.
void GetLastSize(int _arg[2]) override
Dimensions in pixels of the framebuffer.
static unsigned int GetBothMode()
int CheckFrameBufferStatus(unsigned int mode)
Validate the current FBO configuration (attachments, formats, etc) prints detected errors to vtkError...
void DisplayFrameBufferAttachments()
Display all the attachments of the current framebuffer object.
void DisplayReadBuffer()
Display the read buffer.
vtkGenericOpenGLResourceFreeCallback * ResourceCallback
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.
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.
static bool LoadRequiredExtensions(vtkOpenGLRenderWindow *)
Load all necessary extensions.
int GetOpenGLType(int vtkType)
Given a vtk type get a compatible open gl type.
void Resize(int width, int height)
Resize all FO attachments.
bool Start(int width, int height)
User must take care that width/height match the dimensions of the user defined texture attachments.
void ActivateDrawBuffers(unsigned int n)
static unsigned int GetDrawMode()
unsigned int GetActiveDrawBuffer(unsigned int id)
int * GetLastSize() override
Dimensions in pixels of the framebuffer.
static bool GetFrameBufferStatus(unsigned int mode, const char *&desc)
Validate the current FBO configuration (attachments, formats, etc) return false if the FBO is incompl...
vtkPixelBufferObject * DownloadDepth(int extent[4], int vtkType)
Download data from the depth attachment of the currently bound FBO.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
void AddDepthAttachment()
Directly assign/remove a texture/renderbuffer to depth attachments.
static vtkOpenGLFramebufferObject * New()
virtual void ReleaseGraphicsResources(vtkWindow *win)
Deactivate and UnBind.
std::map< unsigned int, vtkFOInfo * > ColorBuffers
void DestroyColorBuffers(vtkWindow *win)
vtkPixelBufferObject * DownloadColor4(int extent[4], int vtkType)
int GetNumberOfColorAttachments()
Return the number of color attachments for the given mode.
void DisplayBuffer(int value)
Display any buffer (convert value into string).
unsigned int GetMaximumNumberOfRenderTargets()
Returns the maximum number of render targets available.
void SaveCurrentBindingsAndBuffers()
Store/Restore the current framebuffer bindings and buffers.
void AddColorAttachment(unsigned int attId, vtkRenderbuffer *tex)
Directly assign/remove a texture to color attachments.
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.
void AddDepthAttachment(vtkRenderbuffer *tex)
Directly assign/remove a texture/renderbuffer to depth attachments.
vtkOpenGLRenderWindow * GetContext()
Get/Set the context.
vtkPixelBufferObject * DownloadColor3(int extent[4], int vtkType)
void RemoveDepthAttachment()
Directly assign/remove a texture/renderbuffer to depth attachments.
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.
unsigned int GetMaximumNumberOfActiveTargets()
Returns the maximum number of targets that can be rendered to at one time.
void SetContext(vtkRenderWindow *context)
Get/Set the context.
void ActivateReadBuffer(unsigned int id)
void RestorePreviousBindingsAndBuffers()
Store/Restore the current framebuffer bindings and buffers.
static unsigned int GetReadMode()
OpenGL rendering window.
The VertexArrayObject class uses, or emulates, vertex array objects.
abstracts an OpenGL pixel buffer object.
create a window for renderers to draw into
Storage for FBO's.
The ShaderProgram uses one or more Shader objects.
abstracts an OpenGL texture object.
window superclass for vtkRenderWindow
Definition: vtkWindow.h:40
@ mode
Definition: vtkX3D.h:259
@ value
Definition: vtkX3D.h:232
@ extent
Definition: vtkX3D.h:357
@ height
Definition: vtkX3D.h:266
@ index
Definition: vtkX3D.h:258