VTK  9.7.1
vtkOpenGLRenderWindow.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-License-Identifier: BSD-3-Clause
16 #ifndef vtkOpenGLRenderWindow_h
17 #define vtkOpenGLRenderWindow_h
18 
19 #include "vtkRect.h" // for vtkRecti
20 #include "vtkRenderWindow.h"
21 #include "vtkRenderingOpenGL2Module.h" // For export macro
22 #include "vtkType.h" // for ivar
23 #include "vtkWrappingHints.h" // For VTK_MARSHALAUTO
24 #include <map> // for ivar
25 #include <set> // for ivar
26 #include <string> // for ivar
27 
28 VTK_ABI_NAMESPACE_BEGIN
29 class vtkIdList;
32 class vtkOpenGLHardwareSupport;
38 class vtkShaderProgram;
39 class vtkTexture;
40 class vtkTextureObject;
43 class vtkOpenGLState;
45 
46 class VTKRENDERINGOPENGL2_EXPORT VTK_MARSHALAUTO vtkOpenGLRenderWindow : public vtkRenderWindow
47 {
48 public:
50 #if !(defined(__APPLE__) || defined(__ANDROID__) || defined(__EMSCRIPTEN__))
51  static vtkOpenGLRenderWindow* New();
53  static vtkOverrideAttribute* CreateOverrideAttributes();
54 #endif
55  void PrintSelf(ostream& os, vtkIndent indent) override;
56 
60  void Start() override;
61 
66  void Frame() override;
67 
71  const char* GetRenderingBackend() override;
72 
74 
77  static void SetGlobalMaximumNumberOfMultiSamples(int val);
78  static int GetGlobalMaximumNumberOfMultiSamples();
80 
82 
87  static void SetUseGenericOpenGLRenderWindow(bool val);
88  static bool GetUseGenericOpenGLRenderWindow();
90 
92 
97  unsigned char* GetPixelData(int x, int y, int x2, int y2, int front, int right) override;
98  int GetPixelData(
99  int x, int y, int x2, int y2, int front, vtkUnsignedCharArray* data, int right) override;
100  int SetPixelData(
101  int x, int y, int x2, int y2, unsigned char* data, int front, int right) override;
102  int SetPixelData(
103  int x, int y, int x2, int y2, vtkUnsignedCharArray* data, int front, int right) override;
105 
107 
110  float* GetRGBAPixelData(int x, int y, int x2, int y2, int front, int right = 0) override;
111  int GetRGBAPixelData(
112  int x, int y, int x2, int y2, int front, vtkFloatArray* data, int right = 0) override;
113  int SetRGBAPixelData(
114  int x, int y, int x2, int y2, float* data, int front, int blend = 0, int right = 0) override;
115  int SetRGBAPixelData(int x, int y, int x2, int y2, vtkFloatArray* data, int front, int blend = 0,
116  int right = 0) override;
117  void ReleaseRGBAPixelData(float* data) override;
118  unsigned char* GetRGBACharPixelData(
119  int x, int y, int x2, int y2, int front, int right = 0) override;
121  int x, int y, int x2, int y2, int front, vtkUnsignedCharArray* data, int right = 0) override;
122  int SetRGBACharPixelData(int x, int y, int x2, int y2, unsigned char* data, int front,
123  int blend = 0, int right = 0) override;
124  int SetRGBACharPixelData(int x, int y, int x2, int y2, vtkUnsignedCharArray* data, int front,
125  int blend = 0, int right = 0) override;
127 
129 
132  float* GetZbufferData(int x1, int y1, int x2, int y2) override;
133  int GetZbufferData(int x1, int y1, int x2, int y2, float* z) override;
134  int GetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray* buffer) override;
135  int SetZbufferData(int x1, int y1, int x2, int y2, float* buffer) override;
136  int SetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray* buffer) override;
138 
142  void ActivateTexture(vtkTextureObject*);
143 
147  void DeactivateTexture(vtkTextureObject*);
148 
152  int GetTextureUnitForTexture(vtkTextureObject*);
153 
157  int GetDepthBufferSize() override;
158 
162  bool GetUsingSRGBColorSpace();
163 
168  int GetColorBufferSizes(int* rgba) override;
169 
175  int GetColorBufferInternalFormat(int attachmentPoint);
176 
180  virtual void OpenGLInit();
181 
182  // Initialize the state of OpenGL that VTK wants for this window
183  virtual void OpenGLInitState();
184 
185  // Initialize VTK for rendering in a new OpenGL context
186  virtual void OpenGLInitContext();
187 
193  void GetOpenGLVersion(int& major, int& minor);
194 
198  virtual vtkMTimeType GetContextCreationTime();
199 
203  vtkOpenGLShaderCache* GetShaderCache();
204 
208  vtkOpenGLVertexBufferObjectCache* GetVBOCache();
209 
213  vtkOpenGLArrayTextureBufferCache* GetArrayTextureBufferCache();
214 
216 
219  vtkGetObjectMacro(RenderFramebuffer, vtkOpenGLFramebufferObject);
221 
225  vtkGetObjectMacro(DisplayFramebuffer, vtkOpenGLFramebufferObject);
226 
231  vtkTextureUnitManager* GetTextureUnitManager();
232 
237  void WaitForCompletion() override;
238 
242  virtual void DrawPixels(
243  int x1, int y1, int x2, int y2, int numComponents, int dataType, void* data);
244 
249  virtual void DrawPixels(int dstXmin, int dstYmin, int dstXmax, int dstYmax, int srcXmin,
250  int srcYmin, int srcXmax, int srcYmax, int srcWidth, int srcHeight, int numComponents,
251  int dataType, void* data);
252 
257  virtual void DrawPixels(int srcWidth, int srcHeight, int numComponents, int dataType, void* data);
258 
262  virtual float GetMaximumHardwareLineWidth() { return this->MaximumHardwareLineWidth; }
263 
270  virtual bool IsPointSpriteBugPresent() { return false; }
271 
281  bool IsPrimIDBugPresent();
282 
288  int GetDefaultTextureInternalFormat(
289  int vtktype, int numComponents, bool needInteger, bool needFloat, bool needSRGB);
290 
296  std::string GetOpenGLSupportMessage() { return this->OpenGLSupportMessage; }
297 
301  int SupportsOpenGL() override;
302 
306  const char* ReportCapabilities() override;
307 
314  void Initialize() override {}
315 
316  std::set<vtkGenericOpenGLResourceFreeCallback*> Resources;
317 
319  {
320  std::set<vtkGenericOpenGLResourceFreeCallback*>::iterator it = this->Resources.find(cb);
321  if (it == this->Resources.end())
322  {
323  this->Resources.insert(cb);
324  }
325  }
326 
328  {
329  std::set<vtkGenericOpenGLResourceFreeCallback*>::iterator it = this->Resources.find(cb);
330  if (it != this->Resources.end())
331  {
332  this->Resources.erase(it);
333  }
334  }
335 
345  virtual void PushContext() { this->MakeCurrent(); }
346  virtual void PopContext() {}
347 
352  bool InitializeFromCurrentContext() override;
353 
363  virtual bool SetSwapControl(int) { return false; }
364 
365  // Get the state object used to keep track of
366  // OpenGL state
367  virtual vtkOpenGLState* GetState() { return this->State; }
368 
369  // Get a VBO that can be shared by many
370  // It consists of normalized display
371  // coordinates for a quad and tcoords
372  vtkOpenGLBufferObject* GetTQuad2DVBO();
373 
374  // Activate and return thje texture unit for a generic 2d 64x64
375  // float greyscale noise texture ranging from 0 to 1. The texture is
376  // a hard-coded blue noise texture. This texture unit will automatically
377  // be deactivated at the end of the render process.
378  int GetNoiseTextureUnit();
379 
383  void End() override;
384 
388  void Render() override;
389 
394  void StereoMidpoint() override;
395 
396  // does VTKs framebuffer require resolving for reading pixels
397  bool GetBufferNeedsResolving();
398 
403  void ReleaseGraphicsResources(vtkWindow*) override;
404 
409  void BlitDisplayFramebuffer();
410 
415  void BlitDisplayFramebufferColorAndDepth();
416 
420  void BlitDisplayFramebuffer(int right, int srcX, int srcY, int srcWidth, int srcHeight, int destX,
421  int destY, int destWidth, int destHeight, int bufferMode, int interpolation);
422 
424 
428  void BlitToRenderFramebuffer(bool includeDepth);
429  void BlitToRenderFramebuffer(int srcX, int srcY, int srcWidth, int srcHeight, int destX,
430  int destY, int destWidth, int destHeight, int bufferMode, int interpolation);
432 
438  {
439  BlitToHardware, // hardware buffers
440  BlitToCurrent, // currently bound draw framebuffer, without depth buffer
441  BlitToCurrentWithDepth, // currently bound draw framebuffer, including depth buffer
442  NoBlit // no blit, GUI or external code will handle the blit
443  };
444 
446 
471  vtkSetClampMacro(FrameBlitMode, FrameBlitModes, BlitToHardware, NoBlit);
472  vtkGetMacro(FrameBlitMode, FrameBlitModes);
473  void SetFrameBlitModeToBlitToHardware() { this->SetFrameBlitMode(BlitToHardware); }
474  void SetFrameBlitModeToBlitToCurrent() { this->SetFrameBlitMode(BlitToCurrent); }
476  {
477  this->SetFrameBlitMode(BlitToCurrentWithDepth);
478  }
479  void SetFrameBlitModeToNoBlit() { this->SetFrameBlitMode(NoBlit); }
481 
483 
486  vtkSetMacro(FramebufferFlipY, bool);
487  vtkGetMacro(FramebufferFlipY, bool);
488  vtkBooleanMacro(FramebufferFlipY, bool);
490 
492 
495  vtkSetMacro(RenderBufferTargetDepthSize, int);
496  vtkGetMacro(RenderBufferTargetDepthSize, int);
498 
500  // copy depth values from a source framebuffer to a destination framebuffer
501  // using texture maps to do the copy. The source framebufferobject must be texture
502  // backed. This method is designed to work around issues with trying to blit depth
503  // values between framebuffers that have different depth formats.
504 
505  // blit entire source texture to active viewport
506  virtual void TextureDepthBlit(vtkTextureObject* source);
507 
508  // blit specified source texels to active viewport
509  virtual void TextureDepthBlit(vtkTextureObject* source, int srcX, int srcY, int srcX2, int srcY2);
510 
511  // blit specified source texels to specified viewport
512  virtual void TextureDepthBlit(vtkTextureObject* source, int srcX, int srcY, int srcX2, int srcY2,
513  int destX, int destY, int destX2, int destY2);
515 
516  typedef void (*VTKOpenGLAPIProc)();
517  typedef VTKOpenGLAPIProc (*VTKOpenGLLoaderFunction)(void* userptr, const char* name);
528  void SetOpenGLSymbolLoader(VTKOpenGLLoaderFunction loader, void* userData);
529 
545  void SetOpenGLSymbolLoader2(long long toolGetProcAddressFunc, long long glLibHandle);
546 
547 protected:
549  ~vtkOpenGLRenderWindow() override;
550 
551  // blits the display buffers to the appropriate hardware buffers
552  virtual void BlitDisplayFramebuffersToHardware();
553 
554  // when frame is called, at the end blit to the hardware buffers
556 
557  // a FSQ we use to resolve MSAA that handles gamma
559 
560  // a FSQ we use to blit depth values
562 
563  // a FSQ we use to flip framebuffer texture
565 
566  // a FSQ we use to read depth component on platforms with OpenGL ES implementations
567  // because `glReadPixels` cannot be used to read GL_DEPTH_COMPONENT
569 
570  // flip quad helpers Y tcoord
572 
573  // resolve and flip renderframebuffer as needed
574  // when copying to displayframebuffer. Returns
575  // true if the color buffer was copied.
576  virtual bool ResolveFlipRenderFramebuffer();
577 
578  // On GLES, the depth attachment buffer cannot be downloaded from
579  // the GPU with `glReadPixels`.
580  // This method reads the depth buffer bits.
581  // The depth attachment size can be 8,16,24 or 32. The values are split into 4 8-bit numbers.
582  // These are stored in the form of an RGBA color attachment in DepthFrameBuffer.
583  // `glReadPixels` can read that RGBA format and reconstruct full 8,16,24 or 32-bit integer
584  // followed by scaling down to 0-1.
585  bool ReadDepthComponent(int depthSize);
586 
587  // used in testing for opengl support
588  // in the SupportsOpenGL() method
592 
593  virtual int ReadPixels(
594  const vtkRecti& rect, int front, int glFormat, int glType, void* data, int right = 0);
595 
604  int CreateFramebuffers(int width, int height);
607 
608  // used when we need to resolve a multisampled
609  // framebuffer
611 
612  // used when we need to read depth component
613  // with OpenGL ES 3
615 
619  virtual void CreateAWindow() = 0;
620 
624  virtual void DestroyWindow() = 0;
625 
629  void SaveGLState();
630 
634  void RestoreGLState();
635 
636  std::map<std::string, int> GLStateIntegers;
637 
642 
644 
646 
648 
650 
651  // used for fast quad rendering
653 
654  // noise texture
656 
658 
659  // keep track of in case we need to recreate the framebuffer
661 
662  // how much bits to use for depth of created Framebuffers
664 
665  int ScreenSize[2];
666 
667  struct
668  {
669  VTKOpenGLLoaderFunction LoadFunction = nullptr;
670  void* UserData = nullptr;
671  } SymbolLoader;
672 
673  typedef VTKOpenGLAPIProc (*VTKOpenGLGetProcAddress)(const char* name);
675  {
676  VTKOpenGLGetProcAddress resolveFunc = nullptr;
677  void* libHandle = nullptr;
678  } FuncResolverState;
679 
680 private:
682  void operator=(const vtkOpenGLRenderWindow&) = delete;
683 
684  // Keeping `State` private so the only way to access it is through
685  // `this->GetState()`.
686  vtkOpenGLState* State;
687 };
688 #if !(defined(__APPLE__) || defined(__ANDROID__) || defined(__EMSCRIPTEN__))
689 #define vtkOpenGLRenderWindow_OVERRIDE_ATTRIBUTES vtkOpenGLRenderWindow::CreateOverrideAttributes()
690 #endif
691 VTK_ABI_NAMESPACE_END
692 #endif
std::set< vtkGenericOpenGLResourceFreeCallback * > Resources
OpenGL rendering window.
vtkOpenGLFramebufferObject * ResolveFramebuffer
virtual void Start()
Start the rendering process for a frame.
Attribute for vtkObjectFactory overrides.
vtkTypeBool OwnContext
Flag telling if the context has been created here or was inherited.
virtual int GetDepthBufferSize()
This method should be defined by the subclass.
virtual float * GetZbufferData(int, int, int, int)
Set/Get the zbuffer data from the frame buffer.
void SetFrameBlitModeToBlitToHardware()
SetGet how to handle blits at the end of a Frame() call.
virtual unsigned char * GetPixelData(int, int, int, int, int, int=0)
Get the pixel data of an image, transmitted as RGBRGBRGB.
Definition: vtkWindow.h:201
virtual int SetPixelData(int, int, int, int, unsigned char *, int, int=0)
Set/Get the pixel data of an image, transmitted as RGBRGBRGB.
vtkTypeUInt32 vtkMTimeType
Definition: vtkType.h:318
manage Shader Programs within a context
void UnregisterGraphicsResources(vtkGenericOpenGLResourceFreeCallback *cb)
virtual int GetColorBufferSizes(int *)
Get the size of the color buffer.
static vtkRenderWindow * New()
Construct an instance of vtkRenderWindow with its screen size set to 300x300, borders turned on...
record modification and/or execution time
Definition: vtkTimeStamp.h:24
void SetFrameBlitModeToBlitToCurrent()
SetGet how to handle blits at the end of a Frame() call.
dynamic, self-adjusting array of float
Definition: vtkFloatArray.h:31
vtkOpenGLQuadHelper * FlipQuad
std::string GetOpenGLSupportMessage()
Return a message profiding additional details about the results of calling SupportsOpenGL() This can ...
manage vertex buffer objects shared within a context
void Render() override
Ask each renderer owned by this RenderWindow to render its image and synchronize this process...
void SetFrameBlitModeToBlitToCurrentWithDepth()
SetGet how to handle blits at the end of a Frame() call.
vtkOpenGLFramebufferObject * DepthFramebuffer
vtkOpenGLQuadHelper * DepthReadQuad
int vtkTypeBool
Definition: vtkABI.h:64
window superclass for vtkRenderWindow
Definition: vtkWindow.h:41
virtual const char * ReportCapabilities()
Get report of capabilities for the render window.
virtual int SupportsOpenGL()
Does this render window support OpenGL? 0-false, 1-true.
OpenGL state storage.
virtual int SetZbufferData(int, int, int, int, float *)
Set/Get the zbuffer data from the frame buffer.
manage texture-buffer-backed data arrays shared within a context
a simple class to control print indentation
Definition: vtkIndent.h:28
The VertexArrayObject class uses, or emulates, vertex array objects.
vtkTextureObject * DrawPixelsTextureObject
list of point or cell ids
Definition: vtkIdList.h:25
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
virtual void WaitForCompletion()
Block the thread until the actual rendering is finished().
void Initialize() override
Initialize the rendering window.
void RegisterGraphicsResources(vtkGenericOpenGLResourceFreeCallback *cb)
handles properties associated with a texture map
Definition: vtkTexture.h:58
virtual void ReleaseRGBAPixelData(float *)
Same as Get/SetPixelData except that the image also contains an alpha component.
virtual void MakeCurrent()
Make the window current.
Definition: vtkWindow.h:255
virtual void PushContext()
Ability to push and pop this window's context as the current context.
boost::graph_traits< vtkGraph * >::vertex_descriptor source(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
Internal class which encapsulates OpenGL FramebufferObject.
vtkTextureObject * NoiseTextureObject
#define VTK_NEWINSTANCE
dynamic, self-adjusting array of unsigned char
virtual bool SetSwapControl(int)
Set the number of vertical syncs required between frames.
allocate/free texture units.
abstracts an OpenGL texture object.
virtual int SetRGBAPixelData(int, int, int, int, float *, int, int=0, int=0)
Same as Get/SetPixelData except that the image also contains an alpha component.
create a window for renderers to draw into
virtual bool InitializeFromCurrentContext()
Initialize the render window from the information associated with the currently activated OpenGL cont...
virtual const char * GetRenderingBackend()
What rendering backend has the user requested.
void SetFrameBlitModeToNoBlit()
SetGet how to handle blits at the end of a Frame() call.
virtual float GetMaximumHardwareLineWidth()
Return the largest line width supported by the hardware.
virtual unsigned char * GetRGBACharPixelData(int, int, int, int, int, int=0)
Same as Get/SetPixelData except that the image also contains an alpha component.
virtual void StereoMidpoint()
Intermediate method performs operations required between the rendering of the left and right eye...
vtkOpenGLQuadHelper * DepthBlitQuad
Class to make rendering a full screen quad easier.
FrameBlitModes
Define how the resulting image should be blitted when at the end of the Frame() call if SwapBuffers i...
vtkOpenGLFramebufferObject * RenderFramebuffer
virtual void End()
Update the system, if needed, at end of render process.
virtual float * GetRGBAPixelData(int, int, int, int, int, int=0)
Same as Get/SetPixelData except that the image also contains an alpha component.
vtkOpenGLBufferObject * TQuad2DVBO
#define VTK_MARSHALAUTO
OpenGL buffer object.
virtual vtkOpenGLState * GetState()
virtual bool IsPointSpriteBugPresent()
Returns true if driver has an EGL/OpenGL bug that makes vtkChartsCoreCxx-TestChartDoubleColors and ot...
std::map< std::string, int > GLStateIntegers
virtual void ReleaseGraphicsResources(vtkWindow *)
Release any graphics resources that are being consumed by this texture.
Definition: vtkWindow.h:186
vtkOpenGLQuadHelper * ResolveQuad
virtual void Frame()
A termination method performed at the end of the rendering process to do things like swapping buffers...
vtkOpenGLFramebufferObject * DisplayFramebuffer
The ShaderProgram uses one or more Shader objects.
virtual int SetRGBACharPixelData(int, int, int, int, unsigned char *, int, int=0, int=0)
Same as Get/SetPixelData except that the image also contains an alpha component.