APIdia Launch Partner
Top Description Fields Constructors Methods
com.badlogic.gdx.graphics.glutils

public Class ShaderProgram

extends Object
implements Disposable
Class Inheritance
All Implemented Interfaces
com.badlogic.gdx.utils.Disposable
Imports
java.nio.Buffer, .ByteBuffer, .ByteOrder, .FloatBuffer, .IntBuffer, com.badlogic.gdx.Application, .Gdx, com.badlogic.gdx.files.FileHandle, com.badlogic.gdx.graphics.Color, .GL20, .Mesh, com.badlogic.gdx.math.Matrix3, .Matrix4, .Vector2, .Vector3, .Vector4, com.badlogic.gdx.utils.Array, .BufferUtils, .Disposable, .ObjectIntMap, .ObjectMap

A shader program encapsulates a vertex and fragment shader pair linked to form a shader program.

After construction a ShaderProgram can be used to draw Mesh. To make the GPU use a specific ShaderProgram the programs ShaderProgram#bind() method must be used which effectively binds the program.

When a ShaderProgram is bound one can set uniforms, vertex attributes and attributes as needed via the respective methods.

A ShaderProgram must be disposed via a call to ShaderProgram#dispose() when it is no longer needed

ShaderPrograms are managed. In case the OpenGL context is lost all shaders get invalidated and have to be reloaded. This happens on Android when a user switches to another application or receives an incoming call. Managed ShaderPrograms are automatically reloaded when the OpenGL context is recreated so you don't have to do this manually.

Author
mzechner

Field Summary

Modifier and TypeField and Description
private String[]
attributeNames

attribute names

private final ObjectIntMap<String>
attributes

attribute lookup

private final ObjectIntMap<String>
attributeSizes

attribute sizes

private final ObjectIntMap<String>
attributeTypes

attribute types

public static final String
BINORMAL_ATTRIBUTE

default name for binormal attribute

public static final String
BONEWEIGHT_ATTRIBUTE

default name for boneweight attribute

public static final String
COLOR_ATTRIBUTE

default name for color attributes

private int
fragmentShaderHandle

fragment shader handle

private final String
fragmentShaderSource

fragment shader source

pack-priv static final IntBuffer
private boolean
invalidated

whether this shader was invalidated

private boolean
isCompiled

whether this program compiled successfully

private String
log

the log

private final FloatBuffer
matrix

matrix float buffer

public static final String
NORMAL_ATTRIBUTE

default name for normal attributes

pack-priv IntBuffer
public static boolean
pedantic

flag indicating whether attributes & uniforms must be present at all times

public static final String
POSITION_ATTRIBUTE

default name for position attributes

public static String
prependFragmentCode

code that is always added to every fragment shader code, typically used to inject a #version line.

public static String
prependVertexCode

code that is always added to the vertex shader code, typically used to inject a #version line.

private int
program

program handle

private int
refCount

reference count

private static final ObjectMap<Application, Array<ShaderProgram>>
shaders

the list of currently available shaders

public static final String
TANGENT_ATTRIBUTE

default name for tangent attribute

public static final String
TEXCOORD_ATTRIBUTE

default name for texcoords attributes, append texture unit number

pack-priv IntBuffer
private String[]
uniformNames

uniform names

private final ObjectIntMap<String>
uniforms

uniform lookup

private final ObjectIntMap<String>
uniformSizes

uniform sizes

private final ObjectIntMap<String>
uniformTypes

uniform types

private int
vertexShaderHandle

vertex shader handle

private final String
vertexShaderSource

vertex shader source

Constructor Summary

AccessConstructor and Description
public
ShaderProgram(String
the vertex shader
vertexShader
,
String
the fragment shader
fragmentShader
)

Constructs a new ShaderProgram and immediately compiles it.

public
ShaderProgram(FileHandle vertexShader, FileHandle fragmentShader)

Method Summary

Modifier and TypeMethod and Description
private void
public void
begin()
Deprecated use bind() instead, this method will be remove in future version

public void
bind()

private void
public static void
private void
compileShaders(String vertexShader, String fragmentShader)

Loads and compiles the shaders, creates a new program and links the shaders.

protected int
public void
disableVertexAttribute(String
the vertex attribute name
name
)

Disables the vertex attribute with the given name

public void
disableVertexAttribute(int location)

public void
dispose()

Implements com.badlogic.gdx.utils.Disposable.dispose.

Disposes all resources associated with this shader.

public void
enableVertexAttribute(String
the vertex attribute name
name
)

Enables the vertex attribute with the given name

public void
enableVertexAttribute(int location)

public void
end()
Deprecated no longer necessary, this method will be remove in future version

private int
private void
private int
public int
fetchUniformLocation(String name, boolean pedantic)

private void
public int
getAttributeLocation(String
the name of the attribute
name
)

Returns: the location of the attribute or -1.

public String[]
getAttributes()

Returns: the attributes

public int
getAttributeSize(String
the name of the attribute
name
)

Returns: the size of the attribute or 0.

public int
getAttributeType(String
the name of the attribute
name
)

Returns: the type of the attribute, one of GL20#GL_FLOAT, GL20#GL_FLOAT_VEC2 etc.

public String
getFragmentShaderSource()

Returns: the source of the fragment shader

public int
getHandle()

Returns: the handle of the shader program

public String
getLog()

Returns: the log info for the shader compilation and program linking stage.

public static String
public static int
getNumManagedShaderPrograms()

Returns: the number of managed shader programs currently loaded

public int
getUniformLocation(String
the name of the uniform
name
)

Returns: the location of the uniform or -1.

public String[]
getUniforms()

Returns: the uniforms

public int
getUniformSize(String
the name of the uniform
name
)

Returns: the size of the uniform or 0.

public int
getUniformType(String
the name of the uniform
name
)

Returns: the type of the uniform, one of GL20#GL_FLOAT, GL20#GL_FLOAT_VEC2 etc.

public String
getVertexShaderSource()

Returns: the source of the vertex shader

public boolean
hasAttribute(String
the name of the attribute
name
)

Returns: whether the attribute is available in the shader

public boolean
hasUniform(String
the name of the uniform
name
)

Returns: whether the uniform is available in the shader

public static void
invalidateAllShaderPrograms(Application app)

Invalidates all shaders so the next time they are used new handles are generated

public boolean
isCompiled()

Returns: whether this ShaderProgram compiled successfully.

private int
linkProgram(int program)

private int
loadShader(int type, String source)

public void
setAttributef(String
the name of the attribute
name
,
float
the first value
value1
,
float
the second value
value2
,
float
the third value
value3
,
float
the fourth value
value4
)

Sets the given attribute

public void
setUniform1fv(String name, float[] values, int offset, int length)

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

public void
setUniform1iv(String name, int[] values, int offset, int length)

public void
setUniform1iv(int location, int[] values, int offset, int length)

public void
setUniform2fv(String name, float[] values, int offset, int length)

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

public void
setUniform2iv(String name, int[] values, int offset, int length)

public void
setUniform2iv(int location, int[] values, int offset, int length)

public void
setUniform3fv(String name, float[] values, int offset, int length)

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

public void
setUniform3iv(String name, int[] values, int offset, int length)

public void
setUniform3iv(int location, int[] values, int offset, int length)

public void
setUniform4fv(String name, float[] values, int offset, int length)

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

public void
setUniform4iv(String name, int[] values, int offset, int length)

public void
setUniform4iv(int location, int[] values, int offset, int length)

public void
setUniformf(String
the name of the uniform
name
,
float
the value
value
)

Sets the uniform with the given name.

public void
setUniformf(int location, float value)

public void
setUniformf(String
the name of the uniform
name
,
float
the first value
value1
,
float
the second value
value2
)

Sets the uniform with the given name.

public void
setUniformf(int location, float value1, float value2)

public void
setUniformf(String
the name of the uniform
name
,
float
the first value
value1
,
float
the second value
value2
,
float
the third value
value3
)

Sets the uniform with the given name.

public void
setUniformf(int location, float value1, float value2, float value3)

public void
setUniformf(String
the name of the uniform
name
,
float
the first value
value1
,
float
the second value
value2
,
float
the third value
value3
,
float
the fourth value
value4
)

Sets the uniform with the given name.

public void
setUniformf(int location, float value1, float value2, float value3, float value4)

public void
setUniformf(String
the name of the uniform
name
,
Vector2
x and y as the first and second values respectively
values
)

Sets the uniform with the given name.

public void
setUniformf(int location, Vector2 values)

public void
setUniformf(String
the name of the uniform
name
,
Vector3
x, y and z as the first, second and third values respectively
values
)

Sets the uniform with the given name.

public void
setUniformf(int location, Vector3 values)

public void
setUniformf(String
the name of the uniform
name
,
Vector4
x, y, z, and w as the first, second, third, and fourth values respectively
values
)

Sets the uniform with the given name.

public void
setUniformf(int location, Vector4 values)

public void
setUniformf(String
the name of the uniform
name
,
Color
r, g, b and a as the first through fourth values respectively
values
)

Sets the uniform with the given name.

public void
setUniformf(int location, Color values)

public void
setUniformi(String
the name of the uniform
name
,
int
the value
value
)

Sets the uniform with the given name.

public void
setUniformi(int location, int value)

public void
setUniformi(String
the name of the uniform
name
,
int
the first value
value1
,
int
the second value
value2
)

Sets the uniform with the given name.

public void
setUniformi(int location, int value1, int value2)

public void
setUniformi(String
the name of the uniform
name
,
int
the first value
value1
,
int
the second value
value2
,
int
the third value
value3
)

Sets the uniform with the given name.

public void
setUniformi(int location, int value1, int value2, int value3)

public void
setUniformi(String
the name of the uniform
name
,
int
the first value
value1
,
int
the second value
value2
,
int
the third value
value3
,
int
the fourth value
value4
)

Sets the uniform with the given name.

public void
setUniformi(int location, int value1, int value2, int value3, int value4)

public void
setUniformMatrix(String
the name of the uniform
name
,
Matrix4
the matrix
matrix
)

Sets the uniform matrix with the given name.

public void
setUniformMatrix(String
the name of the uniform
name
,
Matrix4
the matrix
matrix
,
boolean
whether the matrix should be transposed
transpose
)

Sets the uniform matrix with the given name.

public void
setUniformMatrix(int location, Matrix4 matrix)

public void
setUniformMatrix(int location, Matrix4 matrix, boolean transpose)

public void
setUniformMatrix(String
the name of the uniform
name
,
Matrix3
the matrix
matrix
)

Sets the uniform matrix with the given name.

public void
setUniformMatrix(String
the name of the uniform
name
,
Matrix3
the matrix
matrix
,
boolean
whether the uniform matrix should be transposed
transpose
)

Sets the uniform matrix with the given name.

public void
setUniformMatrix(int location, Matrix3 matrix)

public void
setUniformMatrix(int location, Matrix3 matrix, boolean transpose)

public void
setUniformMatrix3fv(String
the name of the uniform
name
,
FloatBuffer
buffer containing the matrix data
buffer
,
int count, boolean
whether the uniform matrix should be transposed
transpose
)

Sets an array of uniform matrices with the given name.

public void
setUniformMatrix4fv(String
the name of the uniform
name
,
FloatBuffer
buffer containing the matrix data
buffer
,
int count, boolean
whether the uniform matrix should be transposed
transpose
)

Sets an array of uniform matrices with the given name.

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

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

public void
setVertexAttribute(String
the attribute name
name
,
int
the number of components, must be >= 1 and <= 4
size
,
int
the type, must be one of GL20.GL_BYTE, GL20.GL_UNSIGNED_BYTE, GL20.GL_SHORT, GL20.GL_UNSIGNED_SHORT,GL20.GL_FIXED, or GL20.GL_FLOAT. GL_FIXED will not work on the desktop
type
,
boolean
whether fixed point data should be normalized. Will not work on the desktop
normalize
,
int
the stride in bytes between successive attributes
stride
,
Buffer
the buffer containing the vertex attributes.
buffer
)

Sets the vertex attribute with the given name.

public void
setVertexAttribute(int location, int size, int type, boolean normalize, int stride, Buffer buffer)

public void
setVertexAttribute(String
the attribute name
name
,
int
the number of components, must be >= 1 and <= 4
size
,
int
the type, must be one of GL20.GL_BYTE, GL20.GL_UNSIGNED_BYTE, GL20.GL_SHORT, GL20.GL_UNSIGNED_SHORT,GL20.GL_FIXED, or GL20.GL_FLOAT. GL_FIXED will not work on the desktop
type
,
boolean
whether fixed point data should be normalized. Will not work on the desktop
normalize
,
int
the stride in bytes between successive attributes
stride
,
int
byte offset into the vertex buffer object bound to GL20.GL_ARRAY_BUFFER.
offset
)

Sets the vertex attribute with the given name.

public void
setVertexAttribute(int location, int size, int type, boolean normalize, int stride, int offset)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait