ImFusion SDK 4.3
StateGuard Class Reference

#include <ImFusion/Core/GL/StateGuard.h>

Guard to easily save and restore critical (modern) OpenGL state. More...

Detailed Description

Guard to easily save and restore critical (modern) OpenGL state.

On instantiation StateGuard will store selected parts of the current OpenGL state. On destruction StateGuard will restore those settings, no matter of what has been happened in the meantime.

Usage example:

// open a scope block
{
// store the current OpenGL state
StateGuard guard;
// perform a reduction on a custom FBO.
m_fbo->bind();
auto depthRange = m_reduction->reduce(inputDepthTex, m_fbo.get());
// Since the reduction has changed the viewport, we want to restore the original one
stateGuard.restoreState(StateGuard::Viewport);
// do some more rendering
// ...
}
// when the scope ends, guard will be destroyed and restore the previous OpenGL state
// (including the viewport, which has been restored before).
StateGuard(Flags< SaveFlags > saveFlags=Everything)
Constructor, initializes the guard and saves the current state.
@ Viewport
Save the viewport state (origin and extent)
Definition StateGuard.h:45

Public Types

enum  SaveFlags {
  None = 0 , Viewport = 1 << 0 , FBO = 1 << 1 , Program = 1 << 2 ,
  Blending = 1 << 3 , DepthTest = 1 << 4 , ScissorTest = 1 << 5 , Smoothing = 1 << 6 ,
  Stencil = 1 << 7 , Everything = Viewport | FBO | Program | Blending | DepthTest | ScissorTest | Smoothing | Stencil
}
 

Public Member Functions

 StateGuard (Flags< SaveFlags > saveFlags=Everything)
 Constructor, initializes the guard and saves the current state.
 
 StateGuard (StateGuard &&rhs)
 Move constructor needed to avoid double deletion when returning by value.
 
 ~StateGuard ()
 Destructor, restores the state of all save flags given originally, if not done before.
 
void restoreState (Flags< SaveFlags > saveFlagsToClear)
 Restores the state of the given save flags.
 
void release (Flags< SaveFlags > saveFlagsToRelease)
 Marks the given save flags as restored without actually restoring them.
 

Member Enumeration Documentation

◆ SaveFlags

enum SaveFlags
Enumerator
Viewport 

Save the viewport state (origin and extent)

FBO 

Save the currently bound FBO.

Program 

Save the currently bound shader/program.

Blending 

Save the GL_BLEND enable and glBlendFunc states.

DepthTest 

Save the GL_DEPTH_TEST, glDepthMask and glDepthFunc states.

ScissorTest 

Save the GL_SCISSOR_TEST state.

Smoothing 

Save the GL_LINE_SMOOTH and GL_POLYGON_SMOOTH states.

Stencil 

Save GL_STENCIL_TEST+glStencilMask states; glStencilFunc+glStencilOp are not saved.

Everything 

convenience value to include all save flags

Member Function Documentation

◆ restoreState()

void restoreState ( Flags< SaveFlags > saveFlagsToClear)

Restores the state of the given save flags.

Note
Calling this method is optional, the state will always be restored on destruction of the StateGuard object. Multiple subsequent calls to restoreState will always restore the original state during construction.

◆ release()

void release ( Flags< SaveFlags > saveFlagsToRelease)

Marks the given save flags as restored without actually restoring them.

In rare occasions you may want to delete a StateGuard without actually restoring the original state.

Note
This will completely dismiss the corresponding state, even an explicit restoreState() will no longer restore it after calling this function.

The documentation for this class was generated from the following file:
Search Tab / S to search, Esc to close