Class StateGuard
java.lang.Object
lol.trq.helios.state.StateGuard
- All Implemented Interfaces:
AutoCloseable
Records the GL state a draw changes and puts it back.
The first change to a slot reads its current value from GL and keeps it; later changes to the same slot are not
saved again. close() restores the touched slots in reverse order of first touch. States the host caches
are changed and restored through its GlStateCache, so its cache stays true; the rest (program, framebuffer,
viewport) go to the backend. Use one guard per draw, in a try-with-resources block, on the GL thread.
try (StateGuard g = StateGuard.begin(gl, cache)) {
g.blend(true);
g.blendFunc(SRC_ALPHA, ONE_MINUS_SRC_ALPHA, ONE, ONE_MINUS_SRC_ALPHA);
g.program(program.id());
// draw
}
Saved values come from GL, which agrees with the host cache as long as everyone changes cached state through the cache.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumA piece of GL state the guard can save and restore. -
Method Summary
Modifier and TypeMethodDescriptionarrayBuffer(int id) Binds an array buffer.static StateGuardbegin(GlBackend gl, GlStateCache cache) Opens a guard.blend(boolean on) Sets blending on or off.blendFunc(int srcRgb, int dstRgb, int srcAlpha, int dstAlpha) Sets the separate blend factors.voidclose()Restores every touched slot, newest first.colorMask(boolean r, boolean g, boolean b, boolean a) Sets which colour channels are written.cull(boolean on) Sets face culling on or off.depthMask(boolean write) Sets the depth write mask.depthTest(boolean on) Sets the depth test on or off.framebuffer(int id) Binds a framebuffer.program(int id) Uses a program.scissor(boolean on, int x, int y, int width, int height) Sets the scissor test and box.stencil(boolean on, int func, int ref, int valueMask, int sfail, int dpfail, int dppass, int writeMask) Sets the stencil test, function and operations.texture(int unit, int texture) Binds a 2D texture on a texture unit, leaving that unit active.touched()Returns the slots this guard will restore, in first-touch order.vertexArray(int id) Binds a vertex array object.viewport(int x, int y, int width, int height) Sets the viewport.
-
Method Details
-
begin
Opens a guard.- Parameters:
gl- the backendcache- the host's state cache- Returns:
- the guard
-
blend
Sets blending on or off.- Parameters:
on- whether blending is on- Returns:
- this guard
-
blendFunc
Sets the separate blend factors.- Parameters:
srcRgb- source RGB factordstRgb- destination RGB factorsrcAlpha- source alpha factordstAlpha- destination alpha factor- Returns:
- this guard
-
depthTest
Sets the depth test on or off.- Parameters:
on- whether the depth test is on- Returns:
- this guard
-
depthMask
Sets the depth write mask.- Parameters:
write- whether depth writes are on- Returns:
- this guard
-
cull
Sets face culling on or off.- Parameters:
on- whether culling is on- Returns:
- this guard
-
program
Uses a program.- Parameters:
id- the program, or 0- Returns:
- this guard
-
framebuffer
Binds a framebuffer.- Parameters:
id- the framebuffer, or 0 for the window- Returns:
- this guard
-
viewport
Sets the viewport.- Parameters:
x- lefty- bottomwidth- width in pixelsheight- height in pixels- Returns:
- this guard
-
arrayBuffer
Binds an array buffer.- Parameters:
id- the buffer, or 0- Returns:
- this guard
-
vertexArray
Binds a vertex array object. Core profile draws need one bound; the element buffer and attribute layout are part of it, so they need no slots of their own.- Parameters:
id- the vertex array, or 0- Returns:
- this guard
-
texture
Binds a 2D texture on a texture unit, leaving that unit active.- Parameters:
unit- the unit number, below 8texture- the texture- Returns:
- this guard
-
scissor
Sets the scissor test and box.- Parameters:
on- whether the scissor test is onx- left in pixelsy- bottom in pixelswidth- width in pixelsheight- height in pixels- Returns:
- this guard
-
stencil
public StateGuard stencil(boolean on, int func, int ref, int valueMask, int sfail, int dpfail, int dppass, int writeMask) Sets the stencil test, function and operations.- Parameters:
on- whether the stencil test is onfunc- the comparisonref- the reference valuevalueMask- the comparison masksfail- action when the stencil test failsdpfail- action when the depth test failsdppass- action when both passwriteMask- which stencil bits are written- Returns:
- this guard
-
colorMask
Sets which colour channels are written.- Parameters:
r- redg- greenb- bluea- alpha- Returns:
- this guard
-
touched
Returns the slots this guard will restore, in first-touch order.- Returns:
- an unmodifiable view
-
close
public void close()Restores every touched slot, newest first. Calling it again does nothing.- Specified by:
closein interfaceAutoCloseable
-