VTK  9.2.6
vtkOpenGLState.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkOpenGLState.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 =========================================================================*/
63 #ifndef vtkOpenGLState_h
64 #define vtkOpenGLState_h
65 
66 #include "vtkObject.h"
67 #include "vtkRenderingOpenGL2Module.h" // For export macro
68 #include <array> // for ivar
69 #include <list> // for ivar
70 #include <map> // for ivar
71 #include <stack> // for ivar
72 #include <string> // for ivar
73 
78 class vtkTextureObject;
80 
81 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLState : public vtkObject
82 {
83 public:
84  static vtkOpenGLState* New();
85  vtkTypeMacro(vtkOpenGLState, vtkObject);
86  void PrintSelf(ostream& os, vtkIndent indent) override;
87 
89  // cached OpenGL methods. By calling these the context will check
90  // the current value prior to making the OpenGL call. This can reduce
91  // the burden on the driver.
92  //
93  void vtkglClearColor(float red, float green, float blue, float alpha);
94  void vtkglClearDepth(double depth);
95  void vtkglDepthFunc(unsigned int val);
96  void vtkglDepthMask(unsigned char flag);
97  void vtkglColorMask(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
98  void vtkglViewport(int x, int y, int width, int height);
99  void vtkglScissor(int x, int y, int width, int height);
100  void vtkglEnable(unsigned int cap);
101  void vtkglDisable(unsigned int cap);
102  void vtkglBlendFunc(unsigned int sfactor, unsigned int dfactor)
103  {
104  this->vtkglBlendFuncSeparate(sfactor, dfactor, sfactor, dfactor);
105  }
106  void vtkglBlendFuncSeparate(unsigned int sfactorRGB, unsigned int dfactorRGB,
107  unsigned int sfactorAlpha, unsigned int dfactorAlpha);
108  void vtkglBlendEquation(unsigned int val);
109  void vtkglBlendEquationSeparate(unsigned int col, unsigned int alpha);
110  void vtkglCullFace(unsigned int val);
111  void vtkglActiveTexture(unsigned int);
112 
113  void vtkglBindFramebuffer(unsigned int target, unsigned int fb);
114  void vtkglDrawBuffer(unsigned int);
115  void vtkglDrawBuffers(unsigned int n, unsigned int*);
116  void vtkglReadBuffer(unsigned int);
117 
118  void vtkglPointSize(float);
119  void vtkglLineWidth(float);
120  void vtkglStencilMaskSeparate(unsigned int face, unsigned int mask);
121  void vtkglStencilMask(unsigned int mask);
122  void vtkglStencilOpSeparate(
123  unsigned int face, unsigned int sfail, unsigned int dpfail, unsigned int dppass);
124  void vtkglStencilOp(unsigned int sfail, unsigned int dpfail, unsigned int dppass);
125  void vtkglStencilFuncSeparate(unsigned int face, unsigned int func, int ref, unsigned int mask);
126  void vtkglStencilFunc(unsigned int func, int ref, unsigned int mask);
127 
128  void vtkBindFramebuffer(unsigned int target, vtkOpenGLFramebufferObject* fo);
129  void vtkDrawBuffers(unsigned int n, unsigned int*, vtkOpenGLFramebufferObject*);
130  void vtkReadBuffer(unsigned int, vtkOpenGLFramebufferObject*);
131 
132  void vtkglPixelStorei(unsigned int, int);
134 
136  // Methods to reset the state to the current OpenGL context value.
137  // These methods are useful when interfacing with third party code
138  // that may have changed the opengl state.
139  //
140  void ResetGLClearColorState();
141  void ResetGLClearDepthState();
142  void ResetGLDepthFuncState();
143  void ResetGLDepthMaskState();
144  void ResetGLColorMaskState();
145  void ResetGLViewportState();
146  void ResetGLScissorState();
147  void ResetGLBlendFuncState();
148  void ResetGLBlendEquationState();
149  void ResetGLCullFaceState();
150  void ResetGLActiveTexture();
152 
154  // OpenGL functions that we provide an API for even though they may
155  // not hold any state.
156  void vtkglClear(unsigned int mask);
158 
160  // Get methods that can be used to query state if the state is not cached
161  // they fall through and call the underlying opengl functions
162  void vtkglGetBooleanv(unsigned int pname, unsigned char* params);
163  void vtkglGetIntegerv(unsigned int pname, int* params);
164  void vtkglGetDoublev(unsigned int pname, double* params);
165  void vtkglGetFloatv(unsigned int pname, float* params);
167 
168  // convenience to get all 4 values at once
169  void GetBlendFuncState(int*);
170 
171  // convenience to return a bool
172  // as opposed to a unsigned char
173  bool GetEnumState(unsigned int name);
174 
175  // convenience method to set a enum (glEnable/glDisable)
176  void SetEnumState(unsigned int name, bool value);
177 
181  void ResetEnumState(unsigned int name);
182 
183  // superclass for Scoped subclasses
184  template <typename T>
185  class VTKRENDERINGOPENGL2_EXPORT ScopedValue
186  {
187  public:
188  ~ScopedValue() // restore value
189  {
190  ((*this->State).*(this->Method))(this->Value);
191  }
192 
193  protected:
195  T Value;
196  void (vtkOpenGLState::*Method)(T);
197  };
198 
202  void ActivateTexture(vtkTextureObject*);
203 
207  void DeactivateTexture(vtkTextureObject*);
208 
212  int GetTextureUnitForTexture(vtkTextureObject*);
213 
217  void VerifyNoActiveTextures();
218 
220 
224  {
225  this->PushDrawFramebufferBinding();
226  this->PushReadFramebufferBinding();
227  }
228  void PushDrawFramebufferBinding();
229  void PushReadFramebufferBinding();
230 
232  {
233  this->PopReadFramebufferBinding();
234  this->PopDrawFramebufferBinding();
235  }
236  void PopDrawFramebufferBinding();
237  void PopReadFramebufferBinding();
238 
239  void ResetFramebufferBindings();
241 
242  // Scoped classes you can use to save state
243  class VTKRENDERINGOPENGL2_EXPORT ScopedglDepthMask : public ScopedValue<unsigned char>
244  {
245  public:
247  };
248  class VTKRENDERINGOPENGL2_EXPORT ScopedglClearColor : public ScopedValue<std::array<float, 4>>
249  {
250  public:
252  };
253  class VTKRENDERINGOPENGL2_EXPORT ScopedglColorMask
254  : public ScopedValue<std::array<unsigned char, 4>>
255  {
256  public:
258  };
259  class VTKRENDERINGOPENGL2_EXPORT ScopedglScissor : public ScopedValue<std::array<int, 4>>
260  {
261  public:
263  };
264  class VTKRENDERINGOPENGL2_EXPORT ScopedglViewport : public ScopedValue<std::array<int, 4>>
265  {
266  public:
268  };
269  class VTKRENDERINGOPENGL2_EXPORT ScopedglBlendFuncSeparate
270  : public ScopedValue<std::array<unsigned int, 4>>
271  {
272  public:
274  };
275  class VTKRENDERINGOPENGL2_EXPORT ScopedglDepthFunc : public ScopedValue<unsigned int>
276  {
277  public:
279  };
280  class VTKRENDERINGOPENGL2_EXPORT ScopedglActiveTexture : public ScopedValue<unsigned int>
281  {
282  public:
284  };
285 
287  {
288  public:
290  {
291  this->State = state;
292  this->Name = name;
293  unsigned char val;
294  this->State->vtkglGetBooleanv(name, &val);
295  this->Value = val == 1;
296  }
297  ~ScopedglEnableDisable() // restore value
298  {
299  this->State->SetEnumState(this->Name, this->Value);
300  }
301 
302  protected:
304  unsigned int Name;
305  bool Value;
306  };
307 
312 
316  void SetTextureUnitManager(vtkTextureUnitManager* textureUnitManager);
317 
323 
324  // get the shader program cache for this context
325  vtkGetObjectMacro(ShaderCache, vtkOpenGLShaderCache);
326 
327  // get the vbo buffer cache for this context
328  vtkGetObjectMacro(VBOCache, vtkOpenGLVertexBufferObjectCache);
329 
330  // set the VBO Cache to use for this state
331  // this allows two contexts to share VBOs
332  // basically this is OPenGL's support for shared
333  // lists
335 
342  int vtktype, int numComponents, bool needInteger, bool needFloat, bool needSRGB);
343 
347  void GetCurrentDrawFramebufferState(unsigned int& drawBinding, unsigned int& drawBuffer);
348 
353  void vtkglBlitFramebuffer(int, int, int, int, int, int, int, int, unsigned int, unsigned int);
354 
369  void Reset();
370 
376  void Push();
377 
382  void Pop();
383 
387  std::string const& GetVersion() { return this->Version; }
388 
392  std::string const& GetVendor() { return this->Vendor; }
393 
398  std::string const& GetRenderer() { return this->Renderer; }
399 
400 protected:
401  vtkOpenGLState(); // set initial values
402  ~vtkOpenGLState() override;
403 
404  void BlendFuncSeparate(std::array<unsigned int, 4> val);
405  void ClearColor(std::array<float, 4> val);
406  void ColorMask(std::array<unsigned char, 4> val);
407  void Scissor(std::array<int, 4> val);
408  void Viewport(std::array<int, 4> val);
409 
410  int TextureInternalFormats[VTK_OBJECT + 1][3][5];
412 
414  std::map<const vtkTextureObject*, int> TextureResourceIds;
415 
420  void CheckState();
421 
422  // framebuffers hold state themselves
423  // specifically they hold their draw and read buffers
424  // and when bound they reinstate those buffers
425  class VTKRENDERINGOPENGL2_EXPORT BufferBindingState
426  {
427  public:
429  unsigned int Binding;
430  unsigned int ReadBuffer;
431  unsigned int DrawBuffers[10];
432  unsigned int GetBinding();
433  unsigned int GetDrawBuffer(unsigned int);
434  unsigned int GetReadBuffer();
435  };
436  std::list<BufferBindingState> DrawBindings;
437  std::list<BufferBindingState> ReadBindings;
438 
439  // static opengl properties
446 
447  class VTKRENDERINGOPENGL2_EXPORT GLState
448  {
449  public:
450  double ClearDepth;
451  unsigned char DepthMask;
452  unsigned int DepthFunc;
453  unsigned int BlendEquationValue1;
454  unsigned int BlendEquationValue2;
455  unsigned int CullFaceMode;
456  unsigned int ActiveTexture;
457 
458  float PointSize;
459  float LineWidth;
460  unsigned int StencilMaskFront;
461  unsigned int StencilMaskBack;
462  std::array<unsigned int, 3> StencilFuncFront;
463  std::array<unsigned int, 3> StencilFuncBack;
464  std::array<unsigned int, 3> StencilOpFront;
465  std::array<unsigned int, 3> StencilOpBack;
466 
471 
472  std::array<float, 4> ClearColor;
473  std::array<unsigned char, 4> ColorMask;
474  std::array<int, 4> Viewport;
475  std::array<int, 4> Scissor;
476  std::array<unsigned int, 4> BlendFunc;
477  bool DepthTest;
478  bool CullFace;
481  bool Blend;
485  int BoundVAO;
491  GLState() = default;
492  };
493 
494  std::stack<GLState> Stack;
495 
498 
499 private:
500  vtkOpenGLState(const vtkOpenGLState&) = delete;
501  void operator=(const vtkOpenGLState&) = delete;
502 };
503 
504 #endif
OpenGL rendering window.
void vtkglBlitFramebuffer(int, int, int, int, int, int, int, int, unsigned int, unsigned int)
Perform a blit but handle some driver bugs safely.
std::array< int, 4 > Viewport
abstract base class for most VTK objects
Definition: vtkObject.h:62
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
BufferBindingState DrawBinding
void ColorMask(std::array< unsigned char, 4 > val)
#define VTK_OBJECT
Definition: vtkType.h:68
void Push()
Push all the recorded state onto the stack.
std::array< float, 4 > ClearColor
manage Shader Programs within a context
std::map< const vtkTextureObject *, int > TextureResourceIds
std::array< unsigned int, 3 > StencilOpFront
std::array< unsigned int, 3 > StencilFuncBack
void PushFramebufferBindings()
Store/Restore the current framebuffer bindings and buffers.
manage vertex buffer objects shared within a context
vtkOpenGLVertexBufferObjectCache * VBOCache
std::array< unsigned int, 3 > StencilOpBack
std::array< unsigned char, 4 > ColorMask
std::array< int, 4 > Scissor
void vtkglBlendFunc(unsigned int sfactor, unsigned int dfactor)
void Viewport(std::array< int, 4 > val)
~vtkOpenGLState() override
void InitializeTextureInternalFormats()
std::string Vendor
unsigned int BlendEquationValue2
unsigned int BlendEquationValue1
void BlendFuncSeparate(std::array< unsigned int, 4 > val)
std::list< BufferBindingState > ReadBindings
BufferBindingState ReadBinding
OpenGL state storage.
void PopFramebufferBindings()
Store/Restore the current framebuffer bindings and buffers.
a simple class to control print indentation
Definition: vtkIndent.h:39
unsigned int StencilMaskBack
boost::graph_traits< vtkGraph * >::vertex_descriptor target(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
void CheckState()
Check that this OpenGL state has consistent values with the current OpenGL context.
std::stack< GLState > Stack
ScopedglEnableDisable(vtkOpenGLState *state, unsigned int name)
int GetDefaultTextureInternalFormat(int vtktype, int numComponents, bool needInteger, bool needFloat, bool needSRGB)
Get a mapping of vtk data types to native texture formats for this window we put this on the RenderWi...
vtkTextureUnitManager * GetTextureUnitManager()
Returns its texture unit manager object.
void Pop()
Pop the state stack to restore a previous state.
std::string const & GetVersion()
Return the opengl version for this context.
Internal class which encapsulates OpenGL FramebufferObject.
vtkOpenGLShaderCache * ShaderCache
std::list< BufferBindingState > DrawBindings
allocate/free texture units.
abstracts an OpenGL texture object.
unsigned int StencilMaskFront
std::array< unsigned int, 4 > BlendFunc
std::array< unsigned int, 3 > StencilFuncFront
void SetVBOCache(vtkOpenGLVertexBufferObjectCache *val)
std::string Renderer
void GetCurrentDrawFramebufferState(unsigned int &drawBinding, unsigned int &drawBuffer)
Get the current stored state of the draw buffer and binding.
void Reset()
Record the OpenGL state into this class.
std::string const & GetRenderer()
Return the opengl renderer for this context.
vtkTextureUnitManager * TextureUnitManager
void Scissor(std::array< int, 4 > val)
static vtkObject * New()
Create an object with Debug turned off, modified time initialized to zero, and reference counting on...
void SetTextureUnitManager(vtkTextureUnitManager *textureUnitManager)
Set the texture unit manager.
std::string Version
void ClearColor(std::array< float, 4 > val)
std::string const & GetVendor()
Return the opengl vendor for this context.
void Initialize(vtkOpenGLRenderWindow *)
Initialize OpenGL context using current state.