VTK  9.3.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 <map> // for ivar
24 #include <set> // for ivar
25 #include <string> // for ivar
26 
27 VTK_ABI_NAMESPACE_BEGIN
28 class vtkIdList;
31 class vtkOpenGLHardwareSupport;
36 class vtkShaderProgram;
37 class vtkTexture;
38 class vtkTextureObject;
41 class vtkOpenGLState;
42 
43 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLRenderWindow : public vtkRenderWindow
44 {
45 public:
47  void PrintSelf(ostream& os, vtkIndent indent) override;
48 
52  void Start() override;
53 
58  void Frame() override;
59 
63  const char* GetRenderingBackend() override;
64 
66 
69  static void SetGlobalMaximumNumberOfMultiSamples(int val);
70  static int GetGlobalMaximumNumberOfMultiSamples();
72 
74 
79  unsigned char* GetPixelData(int x, int y, int x2, int y2, int front, int right) override;
80  int GetPixelData(
81  int x, int y, int x2, int y2, int front, vtkUnsignedCharArray* data, int right) override;
82  int SetPixelData(
83  int x, int y, int x2, int y2, unsigned char* data, int front, int right) override;
84  int SetPixelData(
85  int x, int y, int x2, int y2, vtkUnsignedCharArray* data, int front, int right) override;
87 
89 
92  float* GetRGBAPixelData(int x, int y, int x2, int y2, int front, int right = 0) override;
93  int GetRGBAPixelData(
94  int x, int y, int x2, int y2, int front, vtkFloatArray* data, int right = 0) override;
95  int SetRGBAPixelData(
96  int x, int y, int x2, int y2, float* data, int front, int blend = 0, int right = 0) override;
97  int SetRGBAPixelData(int x, int y, int x2, int y2, vtkFloatArray* data, int front, int blend = 0,
98  int right = 0) override;
99  void ReleaseRGBAPixelData(float* data) override;
100  unsigned char* GetRGBACharPixelData(
101  int x, int y, int x2, int y2, int front, int right = 0) override;
103  int x, int y, int x2, int y2, int front, vtkUnsignedCharArray* data, int right = 0) override;
104  int SetRGBACharPixelData(int x, int y, int x2, int y2, unsigned char* data, int front,
105  int blend = 0, int right = 0) override;
106  int SetRGBACharPixelData(int x, int y, int x2, int y2, vtkUnsignedCharArray* data, int front,
107  int blend = 0, int right = 0) override;
109 
111 
114  float* GetZbufferData(int x1, int y1, int x2, int y2) override;
115  int GetZbufferData(int x1, int y1, int x2, int y2, float* z) override;
116  int GetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray* buffer) override;
117  int SetZbufferData(int x1, int y1, int x2, int y2, float* buffer) override;
118  int SetZbufferData(int x1, int y1, int x2, int y2, vtkFloatArray* buffer) override;
120 
124  void ActivateTexture(vtkTextureObject*);
125 
129  void DeactivateTexture(vtkTextureObject*);
130 
134  int GetTextureUnitForTexture(vtkTextureObject*);
135 
139  int GetDepthBufferSize() override;
140 
144  bool GetUsingSRGBColorSpace();
145 
150  int GetColorBufferSizes(int* rgba) override;
151 
157  int GetColorBufferInternalFormat(int attachmentPoint);
158 
162  virtual void OpenGLInit();
163 
164  // Initialize the state of OpenGL that VTK wants for this window
165  virtual void OpenGLInitState();
166 
167  // Initialize VTK for rendering in a new OpenGL context
168  virtual void OpenGLInitContext();
169 
175  void GetOpenGLVersion(int& major, int& minor);
176 
180  virtual vtkMTimeType GetContextCreationTime();
181 
185  vtkOpenGLShaderCache* GetShaderCache();
186 
190  vtkOpenGLVertexBufferObjectCache* GetVBOCache();
191 
193 
196  vtkGetObjectMacro(RenderFramebuffer, vtkOpenGLFramebufferObject);
198 
202  vtkGetObjectMacro(DisplayFramebuffer, vtkOpenGLFramebufferObject);
203 
208  vtkTextureUnitManager* GetTextureUnitManager();
209 
214  void WaitForCompletion() override;
215 
219  virtual void DrawPixels(
220  int x1, int y1, int x2, int y2, int numComponents, int dataType, void* data);
221 
226  virtual void DrawPixels(int dstXmin, int dstYmin, int dstXmax, int dstYmax, int srcXmin,
227  int srcYmin, int srcXmax, int srcYmax, int srcWidth, int srcHeight, int numComponents,
228  int dataType, void* data);
229 
234  virtual void DrawPixels(int srcWidth, int srcHeight, int numComponents, int dataType, void* data);
235 
239  virtual float GetMaximumHardwareLineWidth() { return this->MaximumHardwareLineWidth; }
240 
247  virtual bool IsPointSpriteBugPresent() { return false; }
248 
254  int GetDefaultTextureInternalFormat(
255  int vtktype, int numComponents, bool needInteger, bool needFloat, bool needSRGB);
256 
262  std::string GetOpenGLSupportMessage() { return this->OpenGLSupportMessage; }
263 
267  int SupportsOpenGL() override;
268 
272  const char* ReportCapabilities() override;
273 
280  virtual void Initialize() {}
281 
282  std::set<vtkGenericOpenGLResourceFreeCallback*> Resources;
283 
285  {
286  std::set<vtkGenericOpenGLResourceFreeCallback*>::iterator it = this->Resources.find(cb);
287  if (it == this->Resources.end())
288  {
289  this->Resources.insert(cb);
290  }
291  }
292 
294  {
295  std::set<vtkGenericOpenGLResourceFreeCallback*>::iterator it = this->Resources.find(cb);
296  if (it != this->Resources.end())
297  {
298  this->Resources.erase(it);
299  }
300  }
301 
311  virtual void PushContext() { this->MakeCurrent(); }
312  virtual void PopContext() {}
313 
318  bool InitializeFromCurrentContext() override;
319 
329  virtual bool SetSwapControl(int) { return false; }
330 
331  // Get the state object used to keep track of
332  // OpenGL state
333  virtual vtkOpenGLState* GetState() { return this->State; }
334 
335  // Get a VBO that can be shared by many
336  // It consists of normalized display
337  // coordinates for a quad and tcoords
338  vtkOpenGLBufferObject* GetTQuad2DVBO();
339 
340  // Activate and return thje texture unit for a generic 2d 64x64
341  // float greyscale noise texture ranging from 0 to 1. The texture is
342  // a hard-coded blue noise texture. This texture unit will automatically
343  // be deactivated at the end of the render process.
344  int GetNoiseTextureUnit();
345 
349  void End() override;
350 
354  void Render() override;
355 
360  void StereoMidpoint() override;
361 
362  // does VTKs framebuffer require resolving for reading pixels
363  bool GetBufferNeedsResolving();
364 
369  void ReleaseGraphicsResources(vtkWindow*) override;
370 
374  void BlitDisplayFramebuffer();
375 
379  void BlitDisplayFramebuffer(int right, int srcX, int srcY, int srcWidth, int srcHeight, int destX,
380  int destY, int destWidth, int destHeight, int bufferMode, int interpolation);
381 
383 
387  void BlitToRenderFramebuffer(bool includeDepth);
388  void BlitToRenderFramebuffer(int srcX, int srcY, int srcWidth, int srcHeight, int destX,
389  int destY, int destWidth, int destHeight, int bufferMode, int interpolation);
391 
397  {
398  BlitToHardware, // hardware buffers
399  BlitToCurrent, // currently bound draw framebuffer
400  NoBlit // no blit, GUI or external code will handle the blit
401  };
402 
404 
427  vtkSetClampMacro(FrameBlitMode, FrameBlitModes, BlitToHardware, NoBlit);
428  vtkGetMacro(FrameBlitMode, FrameBlitModes);
429  void SetFrameBlitModeToBlitToHardware() { this->SetFrameBlitMode(BlitToHardware); }
430  void SetFrameBlitModeToBlitToCurrent() { this->SetFrameBlitMode(BlitToCurrent); }
431  void SetFrameBlitModeToNoBlit() { this->SetFrameBlitMode(NoBlit); }
433 
435 
438  vtkSetMacro(FramebufferFlipY, bool);
439  vtkGetMacro(FramebufferFlipY, bool);
440  vtkBooleanMacro(FramebufferFlipY, bool);
442 
444  // copy depth values from a source framebuffer to a destination framebuffer
445  // using texture maps to do the copy. The source framebufferobject must be texture
446  // backed. This method is designed to work around issues with trying to blit depth
447  // values between framebuffers that have different depth formats.
448 
449  // blit entire source texture to active viewport
450  virtual void TextureDepthBlit(vtkTextureObject* source);
451 
452  // blit specified source texels to active viewport
453  virtual void TextureDepthBlit(vtkTextureObject* source, int srcX, int srcY, int srcX2, int srcY2);
454 
455  // blit specified source texels to specified viewport
456  virtual void TextureDepthBlit(vtkTextureObject* source, int srcX, int srcY, int srcX2, int srcY2,
457  int destX, int destY, int destX2, int destY2);
459 
460 protected:
462  ~vtkOpenGLRenderWindow() override;
463 
464  // blits the display buffers to the appropriate hardware buffers
465  virtual void BlitDisplayFramebuffersToHardware();
466 
467  // when frame is called, at the end blit to the hardware buffers
469 
470  // a FSQ we use to resolve MSAA that handles gamma
472 
473  // a FSQ we use to blit depth values
475 
476  // a FSQ we use to flip framebuffer texture
478 
479  // a FSQ we use to read depth component on platforms with OpenGL ES implementations
480  // because `glReadPixels` cannot be used to read GL_DEPTH_COMPONENT
482 
483  // flip quad helpers Y tcoord
485 
486  // resolve and flip renderframebuffer as needed
487  // when copying to displayframebuffer. Returns
488  // true if the color buffer was copied.
489  virtual bool ResolveFlipRenderFramebuffer();
490 
491  // On GLES, the depth attachment buffer cannot be downloaded from
492  // the GPU with `glReadPixels`.
493  // This method reads the depth buffer bits.
494  // The depth attachment size can be 8,16,24 or 32. The values are split into 4 8-bit numbers.
495  // These are stored in the form of an RGBA color attachment in DepthFrameBuffer.
496  // `glReadPixels` can read that RGBA format and reconstruct full 8,16,24 or 32-bit integer
497  // followed by scaling down to 0-1.
498  bool ReadDepthComponent(int depthSize);
499 
500  // used in testing for opengl support
501  // in the SupportsOpenGL() method
505 
506  virtual int ReadPixels(
507  const vtkRecti& rect, int front, int glFormat, int glType, void* data, int right = 0);
508 
517  int CreateFramebuffers(int width, int height);
520 
521  // used when we need to resolve a multisampled
522  // framebuffer
524 
525  // used when we need to read depth component
526  // with OpenGL ES 3
528 
532  virtual void CreateAWindow() = 0;
533 
537  virtual void DestroyWindow() = 0;
538 
542  void SaveGLState();
543 
547  void RestoreGLState();
548 
549  std::map<std::string, int> GLStateIntegers;
550 
555 
557 
559 
560  bool Initialized; // ensure glewinit has been called
561  bool GlewInitValid; // Did glewInit initialize with a valid state?
562 
564 
566 
567  // used for fast quad rendering
569 
570  // noise texture
572 
574 
575  // keep track of in case we need to recreate the framebuffer
577 
578  int ScreenSize[2];
579 
580 private:
582  void operator=(const vtkOpenGLRenderWindow&) = delete;
583 
584  // Keeping `State` private so the only way to access it is through
585  // `this->GetState()`.
586  vtkOpenGLState* State;
587 };
588 
589 VTK_ABI_NAMESPACE_END
590 #endif
std::set< vtkGenericOpenGLResourceFreeCallback * > Resources
OpenGL rendering window.
vtkOpenGLFramebufferObject * ResolveFramebuffer
virtual void Start()
Start the rendering process for a frame.
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:184
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:270
manage Shader Programs within a context
void UnregisterGraphicsResources(vtkGenericOpenGLResourceFreeCallback *cb)
virtual int GetColorBufferSizes(int *)
Get the size of the color buffer.
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:30
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...
vtkOpenGLFramebufferObject * DepthFramebuffer
vtkOpenGLQuadHelper * DepthReadQuad
int vtkTypeBool
Definition: vtkABI.h:64
window superclass for vtkRenderWindow
Definition: vtkWindow.h:27
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.
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:22
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 RegisterGraphicsResources(vtkGenericOpenGLResourceFreeCallback *cb)
handles properties associated with a texture map
Definition: vtkTexture.h:57
virtual void Initialize()
Initialize the rendering window.
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:235
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
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
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:169
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.