Class ComputeShaderProgram

java.lang.Object
gaiasky.util.gdx.shader.ComputeShaderProgram
All Implemented Interfaces:
com.badlogic.gdx.utils.Disposable

public class ComputeShaderProgram extends Object implements com.badlogic.gdx.utils.Disposable
A compute shader program helper.

Usage pattern:

ParticleBuffer particleBuffer = new ParticleBuffer(100_000);
ComputeShaderProgram computeShader = new ComputeShaderProgram("galaxy", shaderCode);

computeShader.begin();
computeShader.setUniform("seed", 1234);
computeShader.setUniform("count", 100_000);
computeShader.setUniform("radius", 1.0f);

particleBuffer.bind(0); // matches layout(binding = 0) in GLSL
computeShader.end(100_000);

Notes: - Make sure your shader's `local_size_x` matches LOCAL_SIZE_X constant. - After dispatch, use the particle buffer in rendering. Memory barrier is applied automatically.

  • Constructor Details

  • Method Details

    • reload

      public void reload()
      Reloads and recompiles the program.
    • getName

      public String getName()
    • getShaderFileName

      public String getShaderFileName()
    • getShaderSource

      public String getShaderSource()
    • getLog

      public String getLog()
    • isCompiled

      public boolean isCompiled()
    • compile

      public void compile()
      Compile shader and detect uniforms
    • fetchUniformLocation

      protected int fetchUniformLocation(String name)
    • setUniform

      public void setUniform(String name, int value)
      Set uniform by name with type checking
    • setUniform

      public void setUniform(String name, float value)
    • setUniform

      public void setUniform(String name, com.badlogic.gdx.math.Vector3 vec)
    • setUniform

      public void setUniform(String name, float x, float y)
    • setUniform

      public void setUniform(String name, float x, float y, float z)
    • setUniform

      public void setUniform(String name, float x, float y, float z, float w)
    • setUniform2fv

      public void setUniform2fv(String name, float[] values)
    • setUniform3fv

      public void setUniform3fv(String name, float[] values)
    • setUniform

      public void setUniform(String name, float[] values)
    • setUniform

      public void setUniform(String name, int[] values)
    • setUniformMatrix

      public void setUniformMatrix(String name, com.badlogic.gdx.math.Matrix4 matrix)
      Sets the uniform matrix with the given name. The ExtShaderProgram must be bound for this to work.
      Parameters:
      name - the name of the uniform
      matrix - the matrix
    • setUniformMatrix

      public void setUniformMatrix(String name, com.badlogic.gdx.math.Matrix4 matrix, boolean transpose)
      Sets the uniform matrix with the given name. The ExtShaderProgram must be bound for this to work.
      Parameters:
      name - the name of the uniform
      matrix - the matrix
      transpose - whether the matrix should be transposed
    • setUniformMatrix

      public void setUniformMatrix(int location, com.badlogic.gdx.math.Matrix4 matrix)
    • setUniformMatrix

      public void setUniformMatrix(int location, com.badlogic.gdx.math.Matrix4 matrix, boolean transpose)
    • setUniformMatrix

      public void setUniformMatrix(String name, com.badlogic.gdx.math.Matrix3 matrix)
      Sets the uniform matrix with the given name. The ExtShaderProgram must be bound for this to work.
      Parameters:
      name - the name of the uniform
      matrix - the matrix
    • setUniformMatrix

      public void setUniformMatrix(String name, com.badlogic.gdx.math.Matrix3 matrix, boolean transpose)
      Sets the uniform matrix with the given name. The ExtShaderProgram must be bound for this to work.
      Parameters:
      name - the name of the uniform
      matrix - the matrix
      transpose - whether the uniform matrix should be transposed
    • setUniformMatrix

      public void setUniformMatrix(int location, com.badlogic.gdx.math.Matrix3 matrix)
    • setUniformMatrix

      public void setUniformMatrix(int location, com.badlogic.gdx.math.Matrix3 matrix, boolean transpose)
    • setUniformMatrix3fv

      public void setUniformMatrix3fv(String name, FloatBuffer buffer, int count, boolean transpose)
      Sets an array of uniform matrices with the given name. The ExtShaderProgram must be bound for this to work.
      Parameters:
      name - the name of the uniform
      buffer - buffer containing the matrix data
      transpose - whether the uniform matrix should be transposed
    • setUniformMatrix4fv

      public void setUniformMatrix4fv(String name, FloatBuffer buffer, int count, boolean transpose)
      Sets an array of uniform matrices with the given name. The ExtShaderProgram must be bound for this to work.
      Parameters:
      name - the name of the uniform
      buffer - buffer containing the matrix data
      transpose - whether the uniform matrix should be transposed
    • setUniformMatrix4fv

      public void setUniformMatrix4fv(int location, float[] values, int offset, int length)
    • setUniformMatrix4fv

      public void setUniformMatrix4fv(String name, float[] values, int offset, int length)
    • setUniformUint

      public void setUniformUint(String name, int value)
      Additional method for unsigned int (common in compute shaders)
    • begin

      public void begin()
      Bind the shader program for dispatch
    • end

      public void end(int numElements)
      Dispatch the compute shader for numElements, inserting a memory barrier.
      Parameters:
      numElements - number of elements (particles) to process
    • getProgram

      public int getProgram()
      Returns the OpenGL program ID (for advanced usage, e.g., SSBO binding)
    • getUniformType

      public int getUniformType(String name)
      Get uniform type for debugging
    • getUniformLocation

      public int getUniformLocation(String name)
      Get uniform location for advanced usage
    • cleanup

      public void cleanup()
      Deletes the program
    • dispose

      public void dispose()
      Specified by:
      dispose in interface com.badlogic.gdx.utils.Disposable