VTK  9.3.20240422
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
62VTK_ABI_NAMESPACE_BEGIN
69
70class VTKRENDERINGOPENGL2_EXPORT vtkOpenGLState : public vtkObject
71{
72public:
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);
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
118 void vtkDrawBuffers(unsigned int n, 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 //
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
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:
185 void (vtkOpenGLState::*Method)(T);
186 };
187
192
197
202
207
209
213 {
214 this->PushDrawFramebufferBinding();
215 this->PushReadFramebufferBinding();
216 }
219
221 {
222 this->PopReadFramebufferBinding();
223 this->PopDrawFramebufferBinding();
224 }
227
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:
278 ScopedglEnableDisable(vtkOpenGLState* state, unsigned int name)
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
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
389protected:
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
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
433 std::string Vendor;
434 std::string Renderer;
435 std::string Version;
436
437 class VTKRENDERINGOPENGL2_EXPORT GLState
438 {
439 public:
441 unsigned char DepthMask;
442 unsigned int DepthFunc;
445 unsigned int CullFaceMode;
446 unsigned int ActiveTexture;
447
450 unsigned int StencilMaskFront;
451 unsigned int StencilMaskBack;
452 std::array<unsigned int, 3> StencilFuncFront;
453 std::array<unsigned int, 3> StencilFuncBack;
454 std::array<unsigned int, 3> StencilOpFront;
455 std::array<unsigned int, 3> StencilOpBack;
456
461
462 std::array<float, 4> ClearColor;
463 std::array<unsigned char, 4> ColorMask;
464 std::array<int, 4> Viewport;
465 std::array<int, 4> Scissor;
466 std::array<unsigned int, 4> BlendFunc;
471 bool Blend;
481 GLState() = default;
482 };
483
484 std::stack<GLState> Stack;
485
488
489private:
490 vtkOpenGLState(const vtkOpenGLState&) = delete;
491 void operator=(const vtkOpenGLState&) = delete;
492};
493
494VTK_ABI_NAMESPACE_END
495#endif
a simple class to control print indentation
Definition vtkIndent.h:108
abstract base class for most VTK objects
Definition vtkObject.h:162
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
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)
OpenGL state storage.
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 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.
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.
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
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)
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:56