@UnstableApi
public final class GlProgram


Represents a GLSL shader program.

After constructing a program, keep a reference for its lifetime and call delete (or release the current GL context) when it's no longer needed.

Summary

Public constructors

GlProgram(String vertexShaderGlsl, String fragmentShaderGlsl)

Creates a GL shader program from vertex and fragment shader GLSL GLES20 code.

GlProgram(
    Context context,
    String vertexShaderFilePath,
    String fragmentShaderFilePath
)

Compiles a GL shader program from vertex and fragment shader GLSL GLES20 code.

Public methods

void

Binds all attributes and uniforms in the program.

void

Deletes the program.

int

Returns the location of an Attribute, which has been enabled as a vertex attribute array.

int

Returns the location of a Uniform.

void
setBufferAttribute(String name, float[] values, int size)

Sets a float buffer type attribute.

void
setExternalTexturesRequireNearestSampling(
    boolean externalTexturesRequireNearestSampling
)

Sets whether to sample external textures with GL_NEAREST.

void
setFloatUniform(String name, float value)

Sets a float type uniform.

void
setFloatsUniform(String name, float[] value)

Sets a float[] type uniform.

void
setFloatsUniformIfPresent(String name, float[] value)

Sets a float[] type uniform if name is present, no-op otherwise.

void
setIntUniform(String name, int value)

Sets an int type uniform.

void
setIntsUniform(String name, int[] value)

Sets a int[] type uniform.

void
setSamplerTexIdUniform(String name, int texId, int texUnitIndex)

Sets a texture sampler type uniform.

void
use()

Uses the program.

Public constructors

GlProgram

public GlProgram(String vertexShaderGlsl, String fragmentShaderGlsl)

Creates a GL shader program from vertex and fragment shader GLSL GLES20 code.

This involves slow steps, like compiling, linking, and switching the GL program, so do not call this in fast rendering loops.

Parameters
String vertexShaderGlsl

The vertex shader program.

String fragmentShaderGlsl

The fragment shader program.

GlProgram

public GlProgram(
    Context context,
    String vertexShaderFilePath,
    String fragmentShaderFilePath
)

Compiles a GL shader program from vertex and fragment shader GLSL GLES20 code.

Parameters
Context context

The Context.

String vertexShaderFilePath

The path to a vertex shader program.

String fragmentShaderFilePath

The path to a fragment shader program.

Throws
java.io.IOException

When failing to read shader files.

Public methods

bindAttributesAndUniforms

public void bindAttributesAndUniforms()

Binds all attributes and uniforms in the program.

delete

public void delete()

Deletes the program. Deleted programs cannot be used again.

getAttributeArrayLocationAndEnable

public int getAttributeArrayLocationAndEnable(String attributeName)

Returns the location of an Attribute, which has been enabled as a vertex attribute array.

getUniformLocation

public int getUniformLocation(String uniformName)

Returns the location of a Uniform.

setBufferAttribute

public void setBufferAttribute(String name, float[] values, int size)

Sets a float buffer type attribute.

setExternalTexturesRequireNearestSampling

public void setExternalTexturesRequireNearestSampling(
    boolean externalTexturesRequireNearestSampling
)

Sets whether to sample external textures with GL_NEAREST.

The default value is false.

setFloatUniform

public void setFloatUniform(String name, float value)

Sets a float type uniform.

setFloatsUniform

public void setFloatsUniform(String name, float[] value)

Sets a float[] type uniform.

setFloatsUniformIfPresent

public void setFloatsUniformIfPresent(String name, float[] value)

Sets a float[] type uniform if name is present, no-op otherwise.

setIntUniform

public void setIntUniform(String name, int value)

Sets an int type uniform.

setIntsUniform

public void setIntsUniform(String name, int[] value)

Sets a int[] type uniform.

setSamplerTexIdUniform

public void setSamplerTexIdUniform(String name, int texId, int texUnitIndex)

Sets a texture sampler type uniform.

Parameters
String name

The uniform's name.

int texId

The texture identifier.

int texUnitIndex

The texture unit index. Use a different index (0, 1, 2, ...) for each texture sampler in the program.

use

public void use()

Uses the program.

Call this in the rendering loop to switch between different programs.