Class StateGuard

java.lang.Object
lol.trq.helios.state.StateGuard
All Implemented Interfaces:
AutoCloseable

public final class StateGuard extends Object implements 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.

  • Method Details

    • begin

      public static StateGuard begin(GlBackend gl, GlStateCache cache)
      Opens a guard.
      Parameters:
      gl - the backend
      cache - the host's state cache
      Returns:
      the guard
    • blend

      public StateGuard blend(boolean on)
      Sets blending on or off.
      Parameters:
      on - whether blending is on
      Returns:
      this guard
    • blendFunc

      public StateGuard blendFunc(int srcRgb, int dstRgb, int srcAlpha, int dstAlpha)
      Sets the separate blend factors.
      Parameters:
      srcRgb - source RGB factor
      dstRgb - destination RGB factor
      srcAlpha - source alpha factor
      dstAlpha - destination alpha factor
      Returns:
      this guard
    • depthTest

      public StateGuard depthTest(boolean on)
      Sets the depth test on or off.
      Parameters:
      on - whether the depth test is on
      Returns:
      this guard
    • depthMask

      public StateGuard depthMask(boolean write)
      Sets the depth write mask.
      Parameters:
      write - whether depth writes are on
      Returns:
      this guard
    • cull

      public StateGuard cull(boolean on)
      Sets face culling on or off.
      Parameters:
      on - whether culling is on
      Returns:
      this guard
    • program

      public StateGuard program(int id)
      Uses a program.
      Parameters:
      id - the program, or 0
      Returns:
      this guard
    • framebuffer

      public StateGuard framebuffer(int id)
      Binds a framebuffer.
      Parameters:
      id - the framebuffer, or 0 for the window
      Returns:
      this guard
    • viewport

      public StateGuard viewport(int x, int y, int width, int height)
      Sets the viewport.
      Parameters:
      x - left
      y - bottom
      width - width in pixels
      height - height in pixels
      Returns:
      this guard
    • arrayBuffer

      public StateGuard arrayBuffer(int id)
      Binds an array buffer.
      Parameters:
      id - the buffer, or 0
      Returns:
      this guard
    • vertexArray

      public StateGuard vertexArray(int id)
      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

      public StateGuard texture(int unit, int texture)
      Binds a 2D texture on a texture unit, leaving that unit active.
      Parameters:
      unit - the unit number, below 8
      texture - the texture
      Returns:
      this guard
    • scissor

      public StateGuard scissor(boolean on, int x, int y, int width, int height)
      Sets the scissor test and box.
      Parameters:
      on - whether the scissor test is on
      x - left in pixels
      y - bottom in pixels
      width - width in pixels
      height - 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 on
      func - the comparison
      ref - the reference value
      valueMask - the comparison mask
      sfail - action when the stencil test fails
      dpfail - action when the depth test fails
      dppass - action when both pass
      writeMask - which stencil bits are written
      Returns:
      this guard
    • colorMask

      public StateGuard colorMask(boolean r, boolean g, boolean b, boolean a)
      Sets which colour channels are written.
      Parameters:
      r - red
      g - green
      b - blue
      a - alpha
      Returns:
      this guard
    • touched

      public List<StateGuard.Slot> 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:
      close in interface AutoCloseable