VTK  9.7.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 "vtkSmartPointer.h" // for vtkSmartPointer
57 #include <array> // for ivar
58 #include <list> // for ivar
59 #include <map> // for ivar
60 #include <stack> // for ivar
61 #include <string> // for ivar
62 
63 VTK_ABI_NAMESPACE_BEGIN
70 class vtkTextureObject;
72 
73 class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLState : public vtkObject
74 {
75 public:
76  static vtkOpenGLState* New();
77  vtkTypeMacro(vtkOpenGLState, vtkObject);
78  void PrintSelf(ostream& os, vtkIndent indent) override;
79 
81  // cached OpenGL methods. By calling these the context will check
82  // the current value prior to making the OpenGL call. This can reduce
83  // the burden on the driver.
84  //
85  void vtkglClearColor(float red, float green, float blue, float alpha);
86  void vtkglClearDepth(double depth);
87  void vtkglDepthFunc(unsigned int val);
88  void vtkglDepthMask(unsigned char flag);
89  void vtkglColorMask(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
90  void vtkglViewport(int x, int y, int width, int height);
91  void vtkglScissor(int x, int y, int width, int height);
92  void vtkglEnable(unsigned int cap);
93  void vtkglDisable(unsigned int cap);
94  void vtkglBlendFunc(unsigned int sfactor, unsigned int dfactor)
95  {
96  this->vtkglBlendFuncSeparate(sfactor, dfactor, sfactor, dfactor);
97  }
98  void vtkglBlendFuncSeparate(unsigned int sfactorRGB, unsigned int dfactorRGB,
99  unsigned int sfactorAlpha, unsigned int dfactorAlpha);
100  void vtkglBlendEquation(unsigned int val);
101  void vtkglBlendEquationSeparate(unsigned int col, unsigned int alpha);
102  void vtkglCullFace(unsigned int val);
103  void vtkglActiveTexture(unsigned int);
104 
105  void vtkglBindFramebuffer(unsigned int target, unsigned int fb);
106  void vtkglDrawBuffer(unsigned int);
107  void vtkglDrawBuffers(unsigned int n, unsigned int*);
108  void vtkglReadBuffer(unsigned int);
109 
110  void vtkglPointSize(float);
111  void vtkglLineWidth(float);
112  void vtkglStencilMaskSeparate(unsigned int face, unsigned int mask);
113  void vtkglStencilMask(unsigned int mask);
114  void vtkglStencilOpSeparate(
115  unsigned int face, unsigned int sfail, unsigned int dpfail, unsigned int dppass);
116  void vtkglStencilOp(unsigned int sfail, unsigned int dpfail, unsigned int dppass);
117  void vtkglStencilFuncSeparate(unsigned int face, unsigned int func, int ref, unsigned int mask);
118  void vtkglStencilFunc(unsigned int func, int ref, unsigned int mask);
119 
120  void vtkBindFramebuffer(unsigned int target, vtkOpenGLFramebufferObject* fo);
121  void vtkDrawBuffers(unsigned int n, unsigned int*, vtkOpenGLFramebufferObject*);
122  void vtkReadBuffer(unsigned int, vtkOpenGLFramebufferObject*);
123 
124  void vtkglPixelStorei(unsigned int, int);
126 
128  // Methods to reset the state to the current OpenGL context value.
129  // These methods are useful when interfacing with third party code
130  // that may have changed the opengl state.
131  //
132  void ResetGLClearColorState();
133  void ResetGLClearDepthState();
134  void ResetGLDepthFuncState();
135  void ResetGLDepthMaskState();
136  void ResetGLColorMaskState();
137  void ResetGLViewportState();
138  void ResetGLScissorState();
139  void ResetGLBlendFuncState();
140  void ResetGLBlendEquationState();
141  void ResetGLCullFaceState();
142  void ResetGLActiveTexture();
144 
146  // OpenGL functions that we provide an API for even though they may
147  // not hold any state.
148  void vtkglClear(unsigned int mask);
150 
152  // Get methods that can be used to query state if the state is not cached
153  // they fall through and call the underlying opengl functions
154  void vtkglGetBooleanv(unsigned int pname, unsigned char* params);
155  void vtkglGetIntegerv(unsigned int pname, int* params);
156  void vtkglGetDoublev(unsigned int pname, double* params);
157  void vtkglGetFloatv(unsigned int pname, float* params);
159 
160  // convenience to get all 4 values at once
161  void GetBlendFuncState(int*);
162 
163  // convenience to return a bool
164  // as opposed to a unsigned char
165  bool GetEnumState(unsigned int name);
166 
167  // convenience method to set a enum (glEnable/glDisable)
168  void SetEnumState(unsigned int name, bool value);
169 
173  void ResetEnumState(unsigned int name);
174 
175  // superclass for Scoped subclasses
176  template <typename T>
177  class VTKRENDERINGOPENGL2_EXPORT ScopedValue
178  {
179  public:
180  ~ScopedValue() // restore value
181  {
182  ((*this->State).*(this->Method))(this->Value);
183  }
184 
185  protected:
187  T Value;
188  void (vtkOpenGLState::*Method)(T);
189  };
190 
194  void ActivateTexture(vtkTextureObject*);
195 
199  void DeactivateTexture(vtkTextureObject*);
200 
204  int GetTextureUnitForTexture(vtkTextureObject*);
205 
209  void VerifyNoActiveTextures();
210 
212 
216  {
217  this->PushDrawFramebufferBinding();
218  this->PushReadFramebufferBinding();
219  }
220  void PushDrawFramebufferBinding();
221  void PushReadFramebufferBinding();
222 
224  {
225  this->PopReadFramebufferBinding();
226  this->PopDrawFramebufferBinding();
227  }
228  void PopDrawFramebufferBinding();
229  void PopReadFramebufferBinding();
230 
231  void ResetFramebufferBindings();
233 
234  // Scoped classes you can use to save state
235  class VTKRENDERINGOPENGL2_EXPORT ScopedglDepthMask : public ScopedValue<unsigned char>
236  {
237  public:
239  };
240  class VTKRENDERINGOPENGL2_EXPORT ScopedglClearColor : public ScopedValue<std::array<float, 4>>
241  {
242  public:
244  };
245  class VTKRENDERINGOPENGL2_EXPORT ScopedglColorMask
246  : public ScopedValue<std::array<unsigned char, 4>>
247  {
248  public:
250  };
251  class VTKRENDERINGOPENGL2_EXPORT ScopedglScissor : public ScopedValue<std::array<int, 4>>
252  {
253  public:
255  };
256  class VTKRENDERINGOPENGL2_EXPORT ScopedglViewport : public ScopedValue<std::array<int, 4>>
257  {
258  public:
260  };
261  class VTKRENDERINGOPENGL2_EXPORT ScopedglBlendFuncSeparate
262  : public ScopedValue<std::array<unsigned int, 4>>
263  {
264  public:
266  };
267  class VTKRENDERINGOPENGL2_EXPORT ScopedglDepthFunc : public ScopedValue<unsigned int>
268  {
269  public:
271  };
272  class VTKRENDERINGOPENGL2_EXPORT ScopedglActiveTexture : public ScopedValue<unsigned int>
273  {
274  public:
276  };
277 
279  {
280  public:
282  {
283  this->State = state;
284  this->Name = name;
285  unsigned char val;
286  this->State->vtkglGetBooleanv(name, &val);
287  this->Value = val == 1;
288  }
289  ~ScopedglEnableDisable() // restore value
290  {
291  this->State->SetEnumState(this->Name, this->Value);
292  }
293 
294  protected:
296  unsigned int Name;
297  bool Value;
298  };
299 
304 
308  void SetTextureUnitManager(vtkTextureUnitManager* textureUnitManager);
309 
315 
316  // get the shader program cache for this context
317  vtkGetObjectMacro(ShaderCache, vtkOpenGLShaderCache);
318 
319  // get the vbo buffer cache for this context
320  vtkGetObjectMacro(VBOCache, vtkOpenGLVertexBufferObjectCache);
321 
322  // set the VBO Cache to use for this state
323  // this allows two contexts to share VBOs
324  // basically this is OPenGL's support for shared
325  // lists
327 
329 
336 
343  int vtktype, int numComponents, bool needInteger, bool needFloat, bool needSRGB);
344 
348  void GetCurrentDrawFramebufferState(unsigned int& drawBinding, unsigned int& drawBuffer);
349 
354  void vtkglBlitFramebuffer(int, int, int, int, int, int, int, int, unsigned int, unsigned int);
355 
370  void Reset();
371 
377  void Push();
378 
383  void Pop();
384 
388  std::string const& GetVersion() { return this->Version; }
389 
393  std::string const& GetVendor() { return this->Vendor; }
394 
399  std::string const& GetRenderer() { return this->Renderer; }
400 
407 
413  {
414  return this->TextureNormalizationHelper;
415  }
416 
417 protected:
418  vtkOpenGLState(); // set initial values
419  ~vtkOpenGLState() override;
420 
421  void BlendFuncSeparate(std::array<unsigned int, 4> val);
422  void ClearColor(std::array<float, 4> val);
423  void ColorMask(std::array<unsigned char, 4> val);
424  void Scissor(std::array<int, 4> val);
425  void Viewport(std::array<int, 4> val);
426 
427  int TextureInternalFormats[VTK_OBJECT + 1][3][5];
428  bool SupportsTextureNorm16 = false;
430 
432  vtkOpenGLTextureNormalizationHelper* TextureNormalizationHelper = nullptr;
433  std::map<const vtkTextureObject*, int> TextureResourceIds;
434 
439  void CheckState();
440 
441  // framebuffers hold state themselves
442  // specifically they hold their draw and read buffers
443  // and when bound they reinstate those buffers
444  class VTKRENDERINGOPENGL2_EXPORT BufferBindingState
445  {
446  public:
448  unsigned int Binding;
449  unsigned int ReadBuffer;
450  unsigned int DrawBuffers[10];
451  unsigned int GetBinding();
452  unsigned int GetDrawBuffer(unsigned int);
453  unsigned int GetReadBuffer();
454  };
455  std::list<BufferBindingState> DrawBindings;
456  std::list<BufferBindingState> ReadBindings;
457 
458  // static opengl properties
466 
467  class VTKRENDERINGOPENGL2_EXPORT GLState
468  {
469  public:
470  double ClearDepth;
471  unsigned char DepthMask;
472  unsigned int DepthFunc;
473  unsigned int BlendEquationValue1;
474  unsigned int BlendEquationValue2;
475  unsigned int CullFaceMode;
476  unsigned int ActiveTexture;
477 
478  float PointSize;
479  float LineWidth;
480  unsigned int StencilMaskFront;
481  unsigned int StencilMaskBack;
482  std::array<unsigned int, 3> StencilFuncFront;
483  std::array<unsigned int, 3> StencilFuncBack;
484  std::array<unsigned int, 3> StencilOpFront;
485  std::array<unsigned int, 3> StencilOpBack;
486 
491 
492  std::array<float, 4> ClearColor;
493  std::array<unsigned char, 4> ColorMask;
494  std::array<int, 4> Viewport;
495  std::array<int, 4> Scissor;
496  std::array<unsigned int, 4> BlendFunc;
497  bool DepthTest;
498  bool CullFace;
501  bool Blend;
505  int BoundVAO;
511  GLState() = default;
512  };
513 
514  std::stack<GLState> Stack;
515 
519 
520 private:
521  vtkOpenGLState(const vtkOpenGLState&) = delete;
522  void operator=(const vtkOpenGLState&) = delete;
523 };
524 
525 VTK_ABI_NAMESPACE_END
526 #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:52
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:57
void Push()
Push all the recorded state onto the stack.
std::array< float, 4 > ClearColor
vtkSmartPointer< vtkOpenGLArrayTextureBufferCache > GetArrayTextureBufferCache()
Set/Get the array texture buffer cache to use for this state.
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
bool SupportsTextureNorm16
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)
GPU-based texture normalization for GLES 3.0 without GL_EXT_texture_norm16.
void Viewport(std::array< int, 4 > val)
~vtkOpenGLState() override
void InitializeTextureInternalFormats()
std::string Vendor
unsigned int BlendEquationValue2
unsigned int BlendEquationValue1
vtkSmartPointer< vtkOpenGLArrayTextureBufferCache > ArrayTextureBufferCache
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.
manage texture-buffer-backed data arrays shared within a context
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
vtkOpenGLTextureNormalizationHelper * GetTextureNormalizationHelper()
Get the texture normalization helper for GLES 3.0 without norm16 extension.
void SetArrayTextureBufferCache(vtkOpenGLArrayTextureBufferCache *cache)
Set/Get the array texture buffer cache to use for this state.
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
vtkOpenGLTextureNormalizationHelper * TextureNormalizationHelper
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...
bool GetSupportsTextureNorm16()
Return whether GL_R16 normalized formats are available for VTK_UNSIGNED_SHORT data.
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.