VTK  9.6.20260515
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 <array> // for ivar
57#include <list> // for ivar
58#include <map> // for ivar
59#include <stack> // for ivar
60#include <string> // for ivar
61
62VTK_ABI_NAMESPACE_BEGIN
70
71class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLState : public vtkObject
72{
73public:
75 vtkTypeMacro(vtkOpenGLState, vtkObject);
76 void PrintSelf(ostream& os, vtkIndent indent) override;
77
79 // cached OpenGL methods. By calling these the context will check
80 // the current value prior to making the OpenGL call. This can reduce
81 // the burden on the driver.
82 //
83 void vtkglClearColor(float red, float green, float blue, float alpha);
84 void vtkglClearDepth(double depth);
85 void vtkglDepthFunc(unsigned int val);
86 void vtkglDepthMask(unsigned char flag);
87 void vtkglColorMask(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
88 void vtkglViewport(int x, int y, int width, int height);
89 void vtkglScissor(int x, int y, int width, int height);
90 void vtkglEnable(unsigned int cap);
91 void vtkglDisable(unsigned int cap);
92 void vtkglBlendFunc(unsigned int sfactor, unsigned int dfactor)
93 {
94 this->vtkglBlendFuncSeparate(sfactor, dfactor, sfactor, dfactor);
95 }
96 void vtkglBlendFuncSeparate(unsigned int sfactorRGB, unsigned int dfactorRGB,
97 unsigned int sfactorAlpha, unsigned int dfactorAlpha);
98 void vtkglBlendEquation(unsigned int val);
99 void vtkglBlendEquationSeparate(unsigned int col, unsigned int alpha);
100 void vtkglCullFace(unsigned int val);
101 void vtkglActiveTexture(unsigned int);
102
103 void vtkglBindFramebuffer(unsigned int target, unsigned int fb);
104 void vtkglDrawBuffer(unsigned int);
105 void vtkglDrawBuffers(unsigned int n, unsigned int*);
106 void vtkglReadBuffer(unsigned int);
107
108 void vtkglPointSize(float);
109 void vtkglLineWidth(float);
110 void vtkglStencilMaskSeparate(unsigned int face, unsigned int mask);
111 void vtkglStencilMask(unsigned int mask);
113 unsigned int face, unsigned int sfail, unsigned int dpfail, unsigned int dppass);
114 void vtkglStencilOp(unsigned int sfail, unsigned int dpfail, unsigned int dppass);
115 void vtkglStencilFuncSeparate(unsigned int face, unsigned int func, int ref, unsigned int mask);
116 void vtkglStencilFunc(unsigned int func, int ref, unsigned int mask);
117
119 void vtkDrawBuffers(unsigned int n, unsigned int*, vtkOpenGLFramebufferObject*);
121
122 void vtkglPixelStorei(unsigned int, int);
124
126 // Methods to reset the state to the current OpenGL context value.
127 // These methods are useful when interfacing with third party code
128 // that may have changed the opengl state.
129 //
142
144 // OpenGL functions that we provide an API for even though they may
145 // not hold any state.
146 void vtkglClear(unsigned int mask);
148
150 // Get methods that can be used to query state if the state is not cached
151 // they fall through and call the underlying opengl functions
152 void vtkglGetBooleanv(unsigned int pname, unsigned char* params);
153 void vtkglGetIntegerv(unsigned int pname, int* params);
154 void vtkglGetDoublev(unsigned int pname, double* params);
155 void vtkglGetFloatv(unsigned int pname, float* params);
157
158 // convenience to get all 4 values at once
160
161 // convenience to return a bool
162 // as opposed to a unsigned char
163 bool GetEnumState(unsigned int name);
164
165 // convenience method to set a enum (glEnable/glDisable)
166 void SetEnumState(unsigned int name, bool value);
167
171 void ResetEnumState(unsigned int name);
172
173 // superclass for Scoped subclasses
174 template <typename T>
175 class VTKRENDERINGOPENGL2_EXPORT ScopedValue
176 {
177 public:
178 ~ScopedValue() // restore value
179 {
180 ((*this->State).*(this->Method))(this->Value);
181 }
182
183 protected:
187 };
188
193
198
203
208
210
220
228
231
232 // Scoped classes you can use to save state
233 class VTKRENDERINGOPENGL2_EXPORT ScopedglDepthMask : public ScopedValue<unsigned char>
234 {
235 public:
237 };
238 class VTKRENDERINGOPENGL2_EXPORT ScopedglClearColor : public ScopedValue<std::array<float, 4>>
239 {
240 public:
242 };
243 class VTKRENDERINGOPENGL2_EXPORT ScopedglColorMask
244 : public ScopedValue<std::array<unsigned char, 4>>
245 {
246 public:
248 };
249 class VTKRENDERINGOPENGL2_EXPORT ScopedglScissor : public ScopedValue<std::array<int, 4>>
250 {
251 public:
253 };
254 class VTKRENDERINGOPENGL2_EXPORT ScopedglViewport : public ScopedValue<std::array<int, 4>>
255 {
256 public:
258 };
259 class VTKRENDERINGOPENGL2_EXPORT ScopedglBlendFuncSeparate
260 : public ScopedValue<std::array<unsigned int, 4>>
261 {
262 public:
264 };
265 class VTKRENDERINGOPENGL2_EXPORT ScopedglDepthFunc : public ScopedValue<unsigned int>
266 {
267 public:
269 };
270 class VTKRENDERINGOPENGL2_EXPORT ScopedglActiveTexture : public ScopedValue<unsigned int>
271 {
272 public:
274 };
275
277 {
278 public:
279 ScopedglEnableDisable(vtkOpenGLState* state, unsigned int name)
280 {
281 this->State = state;
282 this->Name = name;
283 unsigned char val;
284 this->State->vtkglGetBooleanv(name, &val);
285 this->Value = val == 1;
286 }
287 ~ScopedglEnableDisable() // restore value
288 {
289 this->State->SetEnumState(this->Name, this->Value);
290 }
291
292 protected:
294 unsigned int Name;
295 bool Value;
296 };
297
302
307
313
314 // get the shader program cache for this context
316
317 // get the vbo buffer cache for this context
319
320 // set the VBO Cache to use for this state
321 // this allows two contexts to share VBOs
322 // basically this is OPenGL's support for shared
323 // lists
325
332 int vtktype, int numComponents, bool needInteger, bool needFloat, bool needSRGB);
333
337 void GetCurrentDrawFramebufferState(unsigned int& drawBinding, unsigned int& drawBuffer);
338
343 void vtkglBlitFramebuffer(int, int, int, int, int, int, int, int, unsigned int, unsigned int);
344
359 void Reset();
360
366 void Push();
367
372 void Pop();
373
377 std::string const& GetVersion() { return this->Version; }
378
382 std::string const& GetVendor() { return this->Vendor; }
383
388 std::string const& GetRenderer() { return this->Renderer; }
389
396
405
406protected:
407 vtkOpenGLState(); // set initial values
408 ~vtkOpenGLState() override;
409
410 void BlendFuncSeparate(std::array<unsigned int, 4> val);
411 void ClearColor(std::array<float, 4> val);
412 void ColorMask(std::array<unsigned char, 4> val);
413 void Scissor(std::array<int, 4> val);
414 void Viewport(std::array<int, 4> val);
415
419
422 std::map<const vtkTextureObject*, int> TextureResourceIds;
423
429
430 // framebuffers hold state themselves
431 // specifically they hold their draw and read buffers
432 // and when bound they reinstate those buffers
433 class VTKRENDERINGOPENGL2_EXPORT BufferBindingState
434 {
435 public:
437 unsigned int Binding;
438 unsigned int ReadBuffer;
439 unsigned int DrawBuffers[10];
440 unsigned int GetBinding();
441 unsigned int GetDrawBuffer(unsigned int);
442 unsigned int GetReadBuffer();
443 };
444 std::list<BufferBindingState> DrawBindings;
445 std::list<BufferBindingState> ReadBindings;
446
447 // static opengl properties
452 std::string Vendor;
453 std::string Renderer;
454 std::string Version;
455
456 class VTKRENDERINGOPENGL2_EXPORT GLState
457 {
458 public:
460 unsigned char DepthMask;
461 unsigned int DepthFunc;
464 unsigned int CullFaceMode;
465 unsigned int ActiveTexture;
466
469 unsigned int StencilMaskFront;
470 unsigned int StencilMaskBack;
471 std::array<unsigned int, 3> StencilFuncFront;
472 std::array<unsigned int, 3> StencilFuncBack;
473 std::array<unsigned int, 3> StencilOpFront;
474 std::array<unsigned int, 3> StencilOpBack;
475
480
481 std::array<float, 4> ClearColor;
482 std::array<unsigned char, 4> ColorMask;
483 std::array<int, 4> Viewport;
484 std::array<int, 4> Scissor;
485 std::array<unsigned int, 4> BlendFunc;
490 bool Blend;
500 GLState() = default;
501 };
502
503 std::stack<GLState> Stack;
504
507
508private:
509 vtkOpenGLState(const vtkOpenGLState&) = delete;
510 void operator=(const vtkOpenGLState&) = delete;
511};
512
513VTK_ABI_NAMESPACE_END
514#endif
a simple class to control print indentation
Definition vtkIndent.h:108
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 *)
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)
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)
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
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