VTK  9.3.1
vtkOpenGLState.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
51 #ifndef vtkOpenGLState_h
52 #define vtkOpenGLState_h
53 
54 #include "vtkObject.h"
55 #include "vtkRenderingOpenGL2Module.h" // For export macro
56 #include <array> // for ivar
57 #include <list> // for ivar
58 #include <map> // for ivar
59 #include <stack> // for ivar
60 #include <string> // for ivar
61 
62 VTK_ABI_NAMESPACE_BEGIN
67 class vtkTextureObject;
69 
70 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLState : public vtkObject
71 {
72 public:
73  static vtkOpenGLState* New();
74  vtkTypeMacro(vtkOpenGLState, vtkObject);
75  void PrintSelf(ostream& os, vtkIndent indent) override;
76 
78  // cached OpenGL methods. By calling these the context will check
79  // the current value prior to making the OpenGL call. This can reduce
80  // the burden on the driver.
81  //
82  void vtkglClearColor(float red, float green, float blue, float alpha);
83  void vtkglClearDepth(double depth);
84  void vtkglDepthFunc(unsigned int val);
85  void vtkglDepthMask(unsigned char flag);
86  void vtkglColorMask(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
87  void vtkglViewport(int x, int y, int width, int height);
88  void vtkglScissor(int x, int y, int width, int height);
89  void vtkglEnable(unsigned int cap);
90  void vtkglDisable(unsigned int cap);
91  void vtkglBlendFunc(unsigned int sfactor, unsigned int dfactor)
92  {
93  this->vtkglBlendFuncSeparate(sfactor, dfactor, sfactor, dfactor);
94  }
95  void vtkglBlendFuncSeparate(unsigned int sfactorRGB, unsigned int dfactorRGB,
96  unsigned int sfactorAlpha, unsigned int dfactorAlpha);
97  void vtkglBlendEquation(unsigned int val);
98  void vtkglBlendEquationSeparate(unsigned int col, unsigned int alpha);
99  void vtkglCullFace(unsigned int val);
100  void vtkglActiveTexture(unsigned int);
101 
102  void vtkglBindFramebuffer(unsigned int target, unsigned int fb);
103  void vtkglDrawBuffer(unsigned int);
104  void vtkglDrawBuffers(unsigned int n, unsigned int*);
105  void vtkglReadBuffer(unsigned int);
106 
107  void vtkglPointSize(float);
108  void vtkglLineWidth(float);
109  void vtkglStencilMaskSeparate(unsigned int face, unsigned int mask);
110  void vtkglStencilMask(unsigned int mask);
111  void vtkglStencilOpSeparate(
112  unsigned int face, unsigned int sfail, unsigned int dpfail, unsigned int dppass);
113  void vtkglStencilOp(unsigned int sfail, unsigned int dpfail, unsigned int dppass);
114  void vtkglStencilFuncSeparate(unsigned int face, unsigned int func, int ref, unsigned int mask);
115  void vtkglStencilFunc(unsigned int func, int ref, unsigned int mask);
116 
117  void vtkBindFramebuffer(unsigned int target, vtkOpenGLFramebufferObject* fo);
118  void vtkDrawBuffers(unsigned int n, unsigned int*, vtkOpenGLFramebufferObject*);
119  void vtkReadBuffer(unsigned int, vtkOpenGLFramebufferObject*);
120 
121  void vtkglPixelStorei(unsigned int, int);
123 
125  // Methods to reset the state to the current OpenGL context value.
126  // These methods are useful when interfacing with third party code
127  // that may have changed the opengl state.
128  //
129  void ResetGLClearColorState();
130  void ResetGLClearDepthState();
131  void ResetGLDepthFuncState();
132  void ResetGLDepthMaskState();
133  void ResetGLColorMaskState();
134  void ResetGLViewportState();
135  void ResetGLScissorState();
136  void ResetGLBlendFuncState();
137  void ResetGLBlendEquationState();
138  void ResetGLCullFaceState();
139  void ResetGLActiveTexture();
141 
143  // OpenGL functions that we provide an API for even though they may
144  // not hold any state.
145  void vtkglClear(unsigned int mask);
147 
149  // Get methods that can be used to query state if the state is not cached
150  // they fall through and call the underlying opengl functions
151  void vtkglGetBooleanv(unsigned int pname, unsigned char* params);
152  void vtkglGetIntegerv(unsigned int pname, int* params);
153  void vtkglGetDoublev(unsigned int pname, double* params);
154  void vtkglGetFloatv(unsigned int pname, float* params);
156 
157  // convenience to get all 4 values at once
158  void GetBlendFuncState(int*);
159 
160  // convenience to return a bool
161  // as opposed to a unsigned char
162  bool GetEnumState(unsigned int name);
163 
164  // convenience method to set a enum (glEnable/glDisable)
165  void SetEnumState(unsigned int name, bool value);
166 
170  void ResetEnumState(unsigned int name);
171 
172  // superclass for Scoped subclasses
173  template <typename T>
174  class VTKRENDERINGOPENGL2_EXPORT ScopedValue
175  {
176  public:
177  ~ScopedValue() // restore value
178  {
179  ((*this->State).*(this->Method))(this->Value);
180  }
181 
182  protected:
184  T Value;
185  void (vtkOpenGLState::*Method)(T);
186  };
187 
191  void ActivateTexture(vtkTextureObject*);
192 
196  void DeactivateTexture(vtkTextureObject*);
197 
201  int GetTextureUnitForTexture(vtkTextureObject*);
202 
206  void VerifyNoActiveTextures();
207 
209 
213  {
214  this->PushDrawFramebufferBinding();
215  this->PushReadFramebufferBinding();
216  }
217  void PushDrawFramebufferBinding();
218  void PushReadFramebufferBinding();
219 
221  {
222  this->PopReadFramebufferBinding();
223  this->PopDrawFramebufferBinding();
224  }
225  void PopDrawFramebufferBinding();
226  void PopReadFramebufferBinding();
227 
228  void ResetFramebufferBindings();
230 
231  // Scoped classes you can use to save state
232  class VTKRENDERINGOPENGL2_EXPORT ScopedglDepthMask : public ScopedValue<unsigned char>
233  {
234  public:
236  };
237  class VTKRENDERINGOPENGL2_EXPORT ScopedglClearColor : public ScopedValue<std::array<float, 4>>
238  {
239  public:
241  };
242  class VTKRENDERINGOPENGL2_EXPORT ScopedglColorMask
243  : public ScopedValue<std::array<unsigned char, 4>>
244  {
245  public:
247  };
248  class VTKRENDERINGOPENGL2_EXPORT ScopedglScissor : public ScopedValue<std::array<int, 4>>
249  {
250  public:
252  };
253  class VTKRENDERINGOPENGL2_EXPORT ScopedglViewport : public ScopedValue<std::array<int, 4>>
254  {
255  public:
257  };
258  class VTKRENDERINGOPENGL2_EXPORT ScopedglBlendFuncSeparate
259  : public ScopedValue<std::array<unsigned int, 4>>
260  {
261  public:
263  };
264  class VTKRENDERINGOPENGL2_EXPORT ScopedglDepthFunc : public ScopedValue<unsigned int>
265  {
266  public:
268  };
269  class VTKRENDERINGOPENGL2_EXPORT ScopedglActiveTexture : public ScopedValue<unsigned int>
270  {
271  public:
273  };
274 
276  {
277  public:
279  {
280  this->State = state;
281  this->Name = name;
282  unsigned char val;
283  this->State->vtkglGetBooleanv(name, &val);
284  this->Value = val == 1;
285  }
286  ~ScopedglEnableDisable() // restore value
287  {
288  this->State->SetEnumState(this->Name, this->Value);
289  }
290 
291  protected:
293  unsigned int Name;
294  bool Value;
295  };
296 
301 
305  void SetTextureUnitManager(vtkTextureUnitManager* textureUnitManager);
306 
312 
313  // get the shader program cache for this context
314  vtkGetObjectMacro(ShaderCache, vtkOpenGLShaderCache);
315 
316  // get the vbo buffer cache for this context
317  vtkGetObjectMacro(VBOCache, vtkOpenGLVertexBufferObjectCache);
318 
319  // set the VBO Cache to use for this state
320  // this allows two contexts to share VBOs
321  // basically this is OPenGL's support for shared
322  // lists
324 
331  int vtktype, int numComponents, bool needInteger, bool needFloat, bool needSRGB);
332 
336  void GetCurrentDrawFramebufferState(unsigned int& drawBinding, unsigned int& drawBuffer);
337 
342  void vtkglBlitFramebuffer(int, int, int, int, int, int, int, int, unsigned int, unsigned int);
343 
358  void Reset();
359 
365  void Push();
366 
371  void Pop();
372 
376  std::string const& GetVersion() { return this->Version; }
377 
381  std::string const& GetVendor() { return this->Vendor; }
382 
387  std::string const& GetRenderer() { return this->Renderer; }
388 
389 protected:
390  vtkOpenGLState(); // set initial values
391  ~vtkOpenGLState() override;
392 
393  void BlendFuncSeparate(std::array<unsigned int, 4> val);
394  void ClearColor(std::array<float, 4> val);
395  void ColorMask(std::array<unsigned char, 4> val);
396  void Scissor(std::array<int, 4> val);
397  void Viewport(std::array<int, 4> val);
398 
399  int TextureInternalFormats[VTK_OBJECT + 1][3][5];
401 
403  std::map<const vtkTextureObject*, int> TextureResourceIds;
404 
409  void CheckState();
410 
411  // framebuffers hold state themselves
412  // specifically they hold their draw and read buffers
413  // and when bound they reinstate those buffers
414  class VTKRENDERINGOPENGL2_EXPORT BufferBindingState
415  {
416  public:
418  unsigned int Binding;
419  unsigned int ReadBuffer;
420  unsigned int DrawBuffers[10];
421  unsigned int GetBinding();
422  unsigned int GetDrawBuffer(unsigned int);
423  unsigned int GetReadBuffer();
424  };
425  std::list<BufferBindingState> DrawBindings;
426  std::list<BufferBindingState> ReadBindings;
427 
428  // static opengl properties
435 
436  class VTKRENDERINGOPENGL2_EXPORT GLState
437  {
438  public:
439  double ClearDepth;
440  unsigned char DepthMask;
441  unsigned int DepthFunc;
442  unsigned int BlendEquationValue1;
443  unsigned int BlendEquationValue2;
444  unsigned int CullFaceMode;
445  unsigned int ActiveTexture;
446 
447  float PointSize;
448  float LineWidth;
449  unsigned int StencilMaskFront;
450  unsigned int StencilMaskBack;
451  std::array<unsigned int, 3> StencilFuncFront;
452  std::array<unsigned int, 3> StencilFuncBack;
453  std::array<unsigned int, 3> StencilOpFront;
454  std::array<unsigned int, 3> StencilOpBack;
455 
460 
461  std::array<float, 4> ClearColor;
462  std::array<unsigned char, 4> ColorMask;
463  std::array<int, 4> Viewport;
464  std::array<int, 4> Scissor;
465  std::array<unsigned int, 4> BlendFunc;
466  bool DepthTest;
467  bool CullFace;
470  bool Blend;
474  int BoundVAO;
480  GLState() = default;
481  };
482 
483  std::stack<GLState> Stack;
484 
487 
488 private:
489  vtkOpenGLState(const vtkOpenGLState&) = delete;
490  void operator=(const vtkOpenGLState&) = delete;
491 };
492 
493 VTK_ABI_NAMESPACE_END
494 #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:51
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:56
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:28
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.