Class ComputeShaderProgram
java.lang.Object
gaiasky.util.gdx.shader.ComputeShaderProgram
- All Implemented Interfaces:
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidbegin()Bind the shader program for dispatchvoidcleanup()Deletes the programvoidcompile()Compile shader and detect uniformsvoiddispose()voidend(int numElements) Dispatch the compute shader for numElements, inserting a memory barrier.protected intfetchUniformLocation(String name) getLog()getName()intReturns the OpenGL program ID (for advanced usage, e.g., SSBO binding)intgetUniformLocation(String name) Get uniform location for advanced usageintgetUniformType(String name) Get uniform type for debuggingbooleanvoidreload()Reloads and recompiles the program.voidsetUniform(String name, float value) voidsetUniform(String name, float[] values) voidsetUniform(String name, float x, float y) voidsetUniform(String name, float x, float y, float z) voidsetUniform(String name, float x, float y, float z, float w) voidsetUniform(String name, int value) Set uniform by name with type checkingvoidsetUniform(String name, int[] values) voidsetUniform(String name, com.badlogic.gdx.math.Vector3 vec) voidsetUniform2fv(String name, float[] values) voidsetUniform3fv(String name, float[] values) voidsetUniformMatrix(int location, com.badlogic.gdx.math.Matrix3 matrix) voidsetUniformMatrix(int location, com.badlogic.gdx.math.Matrix3 matrix, boolean transpose) voidsetUniformMatrix(int location, com.badlogic.gdx.math.Matrix4 matrix) voidsetUniformMatrix(int location, com.badlogic.gdx.math.Matrix4 matrix, boolean transpose) voidsetUniformMatrix(String name, com.badlogic.gdx.math.Matrix3 matrix) Sets the uniform matrix with the given name.voidsetUniformMatrix(String name, com.badlogic.gdx.math.Matrix3 matrix, boolean transpose) Sets the uniform matrix with the given name.voidsetUniformMatrix(String name, com.badlogic.gdx.math.Matrix4 matrix) Sets the uniform matrix with the given name.voidsetUniformMatrix(String name, com.badlogic.gdx.math.Matrix4 matrix, boolean transpose) Sets the uniform matrix with the given name.voidsetUniformMatrix3fv(String name, FloatBuffer buffer, int count, boolean transpose) Sets an array of uniform matrices with the given name.voidsetUniformMatrix4fv(int location, float[] values, int offset, int length) voidsetUniformMatrix4fv(String name, float[] values, int offset, int length) voidsetUniformMatrix4fv(String name, FloatBuffer buffer, int count, boolean transpose) Sets an array of uniform matrices with the given name.voidsetUniformUint(String name, int value) Additional method for unsigned int (common in compute shaders)
-
Constructor Details
-
ComputeShaderProgram
- Throws:
IOException
-
-
Method Details
-
reload
public void reload()Reloads and recompiles the program. -
getName
-
getShaderFileName
-
getShaderSource
-
getLog
-
isCompiled
public boolean isCompiled() -
compile
public void compile()Compile shader and detect uniforms -
fetchUniformLocation
-
setUniform
Set uniform by name with type checking -
setUniform
-
setUniform
-
setUniform
-
setUniform
-
setUniform
-
setUniform2fv
-
setUniform3fv
-
setUniform
-
setUniform
-
setUniformMatrix
Sets the uniform matrix with the given name. TheExtShaderProgrammust be bound for this to work.- Parameters:
name- the name of the uniformmatrix- the matrix
-
setUniformMatrix
Sets the uniform matrix with the given name. TheExtShaderProgrammust be bound for this to work.- Parameters:
name- the name of the uniformmatrix- the matrixtranspose- 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
Sets the uniform matrix with the given name. TheExtShaderProgrammust be bound for this to work.- Parameters:
name- the name of the uniformmatrix- the matrix
-
setUniformMatrix
Sets the uniform matrix with the given name. TheExtShaderProgrammust be bound for this to work.- Parameters:
name- the name of the uniformmatrix- the matrixtranspose- 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
Sets an array of uniform matrices with the given name. TheExtShaderProgrammust be bound for this to work.- Parameters:
name- the name of the uniformbuffer- buffer containing the matrix datatranspose- whether the uniform matrix should be transposed
-
setUniformMatrix4fv
Sets an array of uniform matrices with the given name. TheExtShaderProgrammust be bound for this to work.- Parameters:
name- the name of the uniformbuffer- buffer containing the matrix datatranspose- whether the uniform matrix should be transposed
-
setUniformMatrix4fv
public void setUniformMatrix4fv(int location, float[] values, int offset, int length) -
setUniformMatrix4fv
-
setUniformUint
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
Get uniform type for debugging -
getUniformLocation
Get uniform location for advanced usage -
cleanup
public void cleanup()Deletes the program -
dispose
public void dispose()- Specified by:
disposein interfacecom.badlogic.gdx.utils.Disposable
-