VTK  9.6.20260707
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
50
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
63VTK_ABI_NAMESPACE_BEGIN
72
73class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLState : public vtkObject
74{
75public:
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);
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
121 void vtkDrawBuffers(unsigned int n, 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 //
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
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:
189 };
190
195
200
205
210
212
222
230
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:
281 ScopedglEnableDisable(vtkOpenGLState* state, unsigned int name)
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
309
315
316 // get the shader program cache for this context
318
319 // get the vbo buffer cache for this context
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
416
417protected:
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
430
433 std::map<const vtkTextureObject*, int> TextureResourceIds;
434
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
463 std::string Vendor;
464 std::string Renderer;
465 std::string Version;
466
467 class VTKRENDERINGOPENGL2_EXPORT GLState
468 {
469 public:
471 unsigned char DepthMask;
472 unsigned int DepthFunc;
475 unsigned int CullFaceMode;
476 unsigned int ActiveTexture;
477
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;
501 bool Blend;
511 GLState() = default;
512 };
513
514 std::stack<GLState> Stack;
515
519
520private:
521 vtkOpenGLState(const vtkOpenGLState&) = delete;
522 void operator=(const vtkOpenGLState&) = delete;
523};
524
525VTK_ABI_NAMESPACE_END
526#endif
a simple class to control print indentation
Definition vtkIndent.h:108
manage texture-buffer-backed data arrays shared within a context
Internal class which encapsulates OpenGL FramebufferObject.
OpenGL rendering window.
manage Shader Programs within a context
unsigned int GetDrawBuffer(unsigned int)
BufferBindingState ReadBinding
BufferBindingState DrawBinding
unsigned int BlendEquationValue1
std::array< unsigned int, 4 > BlendFunc
std::array< unsigned int, 3 > StencilFuncFront
std::array< unsigned char, 4 > ColorMask
std::array< unsigned int, 3 > StencilFuncBack
unsigned int BlendEquationValue2
std::array< int, 4 > Viewport
std::array< unsigned int, 3 > StencilOpFront
std::array< int, 4 > Scissor
std::array< float, 4 > ClearColor
std::array< unsigned int, 3 > StencilOpBack
void(vtkOpenGLState::* Method)(T)
ScopedglActiveTexture(vtkOpenGLState *state)
ScopedglBlendFuncSeparate(vtkOpenGLState *state)
ScopedglClearColor(vtkOpenGLState *state)
ScopedglColorMask(vtkOpenGLState *state)
ScopedglDepthFunc(vtkOpenGLState *state)
ScopedglDepthMask(vtkOpenGLState *state)
ScopedglEnableDisable(vtkOpenGLState *state, unsigned int name)
ScopedglScissor(vtkOpenGLState *state)
ScopedglViewport(vtkOpenGLState *state)
void Scissor(std::array< int, 4 > val)
void vtkglViewport(int x, int y, int width, int height)
void vtkReadBuffer(unsigned int, vtkOpenGLFramebufferObject *)
void vtkglLineWidth(float)
void vtkglGetIntegerv(unsigned int pname, int *params)
bool GetEnumState(unsigned int name)
void vtkglStencilOpSeparate(unsigned int face, unsigned int sfail, unsigned int dpfail, unsigned int dppass)
void vtkDrawBuffers(unsigned int n, unsigned int *, vtkOpenGLFramebufferObject *)
void CheckState()
Check that this OpenGL state has consistent values with the current OpenGL context.
std::list< BufferBindingState > DrawBindings
void ResetGLBlendEquationState()
void ResetGLScissorState()
void SetVBOCache(vtkOpenGLVertexBufferObjectCache *val)
void vtkglActiveTexture(unsigned int)
std::string const & GetRenderer()
Return the opengl renderer for this context.
void vtkBindFramebuffer(unsigned int target, vtkOpenGLFramebufferObject *fo)
void Initialize(vtkOpenGLRenderWindow *)
Initialize OpenGL context using current state.
vtkOpenGLShaderCache * ShaderCache
void ResetGLActiveTexture()
void vtkglStencilFuncSeparate(unsigned int face, unsigned int func, int ref, unsigned int mask)
std::map< const vtkTextureObject *, int > TextureResourceIds
void PopFramebufferBindings()
Store/Restore the current framebuffer bindings and buffers.
void ResetEnumState(unsigned int name)
convenience method to reset an enum state from current openGL context
~vtkOpenGLState() override
void ColorMask(std::array< unsigned char, 4 > val)
void PopDrawFramebufferBinding()
Store/Restore the current framebuffer bindings and buffers.
void SetTextureUnitManager(vtkTextureUnitManager *textureUnitManager)
Set the texture unit manager.
void vtkglDrawBuffers(unsigned int n, unsigned int *)
void SetArrayTextureBufferCache(vtkOpenGLArrayTextureBufferCache *cache)
Set/Get the array texture buffer cache to use for this state.
int TextureInternalFormats[VTK_OBJECT+1][3][5]
void vtkglCullFace(unsigned int val)
void Viewport(std::array< int, 4 > val)
void vtkglReadBuffer(unsigned int)
void vtkglBlendFuncSeparate(unsigned int sfactorRGB, unsigned int dfactorRGB, unsigned int sfactorAlpha, unsigned int dfactorAlpha)
vtkSmartPointer< vtkOpenGLArrayTextureBufferCache > ArrayTextureBufferCache
void vtkglBindFramebuffer(unsigned int target, unsigned int fb)
void ResetGLDepthMaskState()
void ActivateTexture(vtkTextureObject *)
Activate a texture unit for this texture.
void Push()
Push all the recorded state onto the stack.
void vtkglDisable(unsigned int cap)
static vtkOpenGLState * New()
void vtkglGetDoublev(unsigned int pname, double *params)
std::string Version
void ResetGLDepthFuncState()
void vtkglStencilOp(unsigned int sfail, unsigned int dpfail, unsigned int dppass)
vtkSmartPointer< vtkOpenGLArrayTextureBufferCache > GetArrayTextureBufferCache()
Set/Get the array texture buffer cache to use for this state.
void Pop()
Pop the state stack to restore a previous state.
vtkOpenGLTextureNormalizationHelper * TextureNormalizationHelper
void BlendFuncSeparate(std::array< unsigned int, 4 > val)
void vtkglBlendFunc(unsigned int sfactor, unsigned int dfactor)
void vtkglEnable(unsigned int cap)
void PushFramebufferBindings()
Store/Restore the current framebuffer bindings and buffers.
bool GetSupportsTextureNorm16()
Return whether GL_R16 normalized formats are available for VTK_UNSIGNED_SHORT data.
void ResetGLClearDepthState()
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...
void Reset()
Record the OpenGL state into this class.
std::stack< GLState > Stack
void vtkglStencilMaskSeparate(unsigned int face, unsigned int mask)
void vtkglClearDepth(double depth)
void ResetGLColorMaskState()
void PopReadFramebufferBinding()
Store/Restore the current framebuffer bindings and buffers.
void vtkglDrawBuffer(unsigned int)
void InitializeTextureInternalFormats()
std::string const & GetVersion()
Return the opengl version for this context.
std::string const & GetVendor()
Return the opengl vendor for this context.
void vtkglGetFloatv(unsigned int pname, float *params)
void GetCurrentDrawFramebufferState(unsigned int &drawBinding, unsigned int &drawBuffer)
Get the current stored state of the draw buffer and binding.
void vtkglBlitFramebuffer(int, int, int, int, int, int, int, int, unsigned int, unsigned int)
Perform a blit but handle some driver bugs safely.
void PushReadFramebufferBinding()
Store/Restore the current framebuffer bindings and buffers.
void vtkglScissor(int x, int y, int width, int height)
void VerifyNoActiveTextures()
Check to make sure no textures have been left active.
void vtkglColorMask(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
void vtkglDepthMask(unsigned char flag)
vtkTextureUnitManager * GetTextureUnitManager()
Returns its texture unit manager object.
void vtkglPointSize(float)
void vtkglBlendEquationSeparate(unsigned int col, unsigned int alpha)
void vtkglClearColor(float red, float green, float blue, float alpha)
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
void DeactivateTexture(vtkTextureObject *)
Deactivate a previously activated texture.
std::list< BufferBindingState > ReadBindings
std::string Vendor
vtkOpenGLTextureNormalizationHelper * GetTextureNormalizationHelper()
Get the texture normalization helper for GLES 3.0 without norm16 extension.
std::string Renderer
void vtkglDepthFunc(unsigned int val)
void PushDrawFramebufferBinding()
Store/Restore the current framebuffer bindings and buffers.
void vtkglClear(unsigned int mask)
void SetEnumState(unsigned int name, bool value)
void vtkglPixelStorei(unsigned int, int)
void ResetGLViewportState()
void ResetGLClearColorState()
void ClearColor(std::array< float, 4 > val)
void vtkglBlendEquation(unsigned int val)
void ResetGLCullFaceState()
void vtkglGetBooleanv(unsigned int pname, unsigned char *params)
void ResetFramebufferBindings()
Store/Restore the current framebuffer bindings and buffers.
vtkOpenGLVertexBufferObjectCache * VBOCache
void ResetGLBlendFuncState()
void vtkglStencilFunc(unsigned int func, int ref, unsigned int mask)
void GetBlendFuncState(int *)
int GetTextureUnitForTexture(vtkTextureObject *)
Get the texture unit for a given texture object.
vtkTextureUnitManager * TextureUnitManager
void vtkglStencilMask(unsigned int mask)
GPU-based texture normalization for GLES 3.0 without GL_EXT_texture_norm16.
manage vertex buffer objects shared within a context
Hold a reference to a vtkObjectBase instance.
abstracts an OpenGL texture object.
allocate/free texture units.
boost::graph_traits< vtkGraph * >::vertex_descriptor target(boost::graph_traits< vtkGraph * >::edge_descriptor e, vtkGraph *)
#define VTK_OBJECT
Definition vtkType.h:57