Top Description Fields Constructors Methods
com.jogamp.opengl.util.glsl

public Class ShaderState

extends Object
Class Inheritance
Imports
java.util.ArrayList, .HashMap, .Iterator, com.jogamp.opengl.GL, .GL2ES2, .GLArrayData, .GLException, .GLUniformData, jogamp.opengl.Debug, com.jogamp.common.ExceptionUtils, com.jogamp.common.os.Platform, com.jogamp.common.util.PropertyAccess, com.jogamp.opengl.util.GLArrayDataEditable

ShaderState allows to sharing data between shader programs, while updating the attribute and uniform locations when switching.

This allows seamless switching of programs using almost same data but performing different artifacts.

A used ShaderState is attached to the current GL context and can be retrieved via getShaderState(GL).

Field Summary

Modifier and TypeField and Description
private final HashMap<String, GLArrayData>
private final HashMap<String, Integer>
private final HashMap<String, Boolean>
private final HashMap<String, GLUniformData>
private final HashMap<String, Integer>
private final HashMap<String, Object>
public static final boolean
private final ArrayList<GLArrayData>
private final ArrayList<GLUniformData>
private boolean
private ShaderProgram
private boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public final Object

Returns:

the previous mapped object or null if none
attachObject
(final String name, final Object obj)

Attach user object for the given name to this ShaderState.

public synchronized boolean

Returns:

true if shader program was attached, otherwise false (already attached)
attachShaderProgram
(final GL2ES2 gl, final ShaderProgram prog, final boolean enable)

Attach or switch a shader program

Attaching a shader program the first time, as well as switching to another program on the fly, while managing all attribute and uniform data.

public void
bindAttribLocation(final GL2ES2 gl, final int location, final String name)

Binds a shader attribute to a location.

public void
bindAttribLocation(final GL2ES2 gl, final int location, final GLArrayData data)

Binds a shader GLArrayData attribute to a location.

public synchronized void
public final Object

Returns:

the previous mapped object or null if none
detachObject
(final String
name of the mapped object to detach
name
)

public void
disableAllVertexAttributeArrays(final GL2ES2 gl, final boolean removeFromState)

Disables all vertex attribute arrays.

private boolean
disableVertexAttribArray(final GL2ES2 gl, final String name, int location)

public boolean

Returns:

false, if the name is not found, otherwise true
disableVertexAttribArray
(final GL2ES2 gl, final String name)

Disables a vertex attribute array This method retrieves the the location via getAttribLocation(GL2ES2, GLArrayData) hence disableVertexAttribArray(GL2ES2, GLArrayData) shall be preferred.

public boolean

Returns:

false, if the name is not found, otherwise true
disableVertexAttribArray
(final GL2ES2 gl, final GLArrayData data)

Disables a vertex attribute array This method uses the GLArrayData's location if set and is the preferred alternative to disableVertexAttribArray(GL2ES2, String).

private boolean
enableVertexAttribArray(final GL2ES2 gl, final String name, int location)

public boolean

Returns:

false, if the name is not found, otherwise true
enableVertexAttribArray
(final GL2ES2 gl, final String name)

Enables a vertex attribute array.

public boolean

Returns:

false, if the name is not found, otherwise true
enableVertexAttribArray
(final GL2ES2 gl, final GLArrayData data)

Enables a vertex attribute array, usually invoked by GLArrayDataEditable#enableBuffer(GL, boolean).

public final Object
getAttachedObject(final String name)

Returns the attached user object for the given name to this ShaderState.

public int

Returns:

-1 if there is no such attribute available, otherwise >= 0
getAttribLocation
(final GL2ES2 gl, final String name)

Gets the location of a shader attribute with given name.

public int

Returns:

-1 if there is no such attribute available, otherwise >= 0
getAttribLocation
(final GL2ES2 gl, final GLArrayData data)

Validates and returns the location of a shader attribute.

public GLArrayData

Returns:

the GLArrayData object, null if not previously set.
getAttribute
(final String name)

Get the previous cached vertex attribute data.

public int

Returns:

-1 if there is no such attribute available, otherwise >= 0
getCachedAttribLocation
(final String name)

Gets the cached location of a shader attribute.

public final int

Returns:

-1 if there is no such uniform available, otherwise >= 0
getCachedUniformLocation
(final String name)

Gets the cached location of the shader uniform.

public GLUniformData

Returns:

the GLUniformData object, null if not previously set.
getUniform
(final String name)

Get the uniform data, previously set.

public final int

Returns:

-1 if there is no such attribute available, otherwise >= 0
getUniformLocation
(final GL2ES2 gl, final String name)

Gets the location of a shader uniform with given name.

public int

Returns:

-1 if there is no such attribute available, otherwise >= 0
getUniformLocation
(final GL2ES2 gl, final GLUniformData data)

Validates and returns the location of a shader uniform.

public boolean
public boolean
isActiveAttribute(final GLArrayData attribute)

public final boolean

Returns:

true if the named attribute is enable
isVertexAttribArrayEnabled
(final String name)

public final boolean

Returns:

true if the GLArrayData attribute is enable
isVertexAttribArrayEnabled
(final GLArrayData data)

public boolean
public void
ownAttribute(final GLArrayData
the GLArrayData which lifecycle shall be managed
attribute
,
final boolean
true if owning shall be performs, false if disowning.
own
)

Binds or unbinds the GLArrayData lifecycle to this ShaderState.

public boolean
ownsAttribute(final GLArrayData attribute)

public boolean
ownsUniform(final GLUniformData uniform)

public void
ownUniform(final GLUniformData
the GLUniformData which lifecycle shall be managed
uniform
)

Bind the GLUniform lifecycle to this ShaderState.

public synchronized void
release(final GL2ES2 gl, final boolean destroyBoundAttributes, final boolean destroyShaderProgram, final boolean destroyShaderCode)

public void
releaseAllAttributes(final GL2ES2 gl)

Releases all mapped vertex attribute data, disables all enabled attributes and loses all indices

public synchronized void
public void
releaseAllUniforms(final GL2ES2 gl)

Releases all mapped uniform data and loses all indices

private final void
relocateAttribute(final GL2ES2 gl, final GLArrayData attribute)

private final void
resetAllAttributes(final GL2ES2 gl)

Reset all previously enabled mapped vertex attribute data.

private final void
resetAllUniforms(final GL2ES2 gl)

Reset all previously mapped uniform data

Uniform data and location is bound to the program, hence both are updated.

private final void
setAllAttributes(final GL2ES2 gl)

preserves the attribute location ..

private final void
setAttribute(final GL2ES2 gl, final GLArrayData attribute)

public void
setVerbose(final boolean v)

public ShaderProgram
public StringBuilder
toString(StringBuilder sb, final boolean alsoUnlocated)

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public boolean

Returns:

false, if the location could not be determined, otherwise true
uniform
(final GL2ES2 gl, final GLUniformData data)

Set the uniform data, if it's location is valid, i.e. ≥ 0.

public synchronized void
useProgram(final GL2ES2 gl, final boolean on)

Turns the shader program on or off.

public boolean
public boolean

Returns:

false, if the location could not be determined, otherwise true
vertexAttribPointer
(final GL2ES2 gl, final GLArrayData data)

Set the GLArrayData vertex attribute data, if it's location is valid, i.e. ≥ 0.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

activeAttribDataMapback to summary
private final HashMap<String, GLArrayData> activeAttribDataMap
activeAttribLocationMapback to summary
private final HashMap<String, Integer> activeAttribLocationMap
activedAttribEnabledMapback to summary
private final HashMap<String, Boolean> activedAttribEnabledMap
activeUniformDataMapback to summary
private final HashMap<String, GLUniformData> activeUniformDataMap
activeUniformLocationMapback to summary
private final HashMap<String, Integer> activeUniformLocationMap
attachedObjectsByStringback to summary
private final HashMap<String, Object> attachedObjectsByString
DEBUGback to summary
public static final boolean DEBUG
managedAttributesback to summary
private final ArrayList<GLArrayData> managedAttributes
managedUniformsback to summary
private final ArrayList<GLUniformData> managedUniforms
resetAllShaderDataback to summary
private boolean resetAllShaderData
shaderProgramback to summary
private ShaderProgram shaderProgram
verboseback to summary
private boolean verbose

Constructor Detail

ShaderStateback to summary
public ShaderState()

Method Detail

attachObjectback to summary
public final Object attachObject(final String name, final Object obj)

Attach user object for the given name to this ShaderState. Returns the previously set object or null.

Returns:Object

the previous mapped object or null if none

attachShaderProgramback to summary
public synchronized boolean attachShaderProgram(final GL2ES2 gl, final ShaderProgram prog, final boolean enable) throws GLException

Attach or switch a shader program

Attaching a shader program the first time, as well as switching to another program on the fly, while managing all attribute and uniform data.

[Re]sets all data and use program in case of a program switch.

Use program, useProgram(GL2ES2, boolean), if enable is true.

Returns:boolean

true if shader program was attached, otherwise false (already attached)

Exceptions
GLException:
if program was not linked and linking fails
bindAttribLocationback to summary
public void bindAttribLocation(final GL2ES2 gl, final int location, final String name)

Binds a shader attribute to a location. Multiple names can be bound to one location. The value will be cached and can be retrieved via getCachedAttribLocation(String) before or after linking.

Exceptions
GLException:
  • if no program is attached
  • if the program is already linked
See Also
com.jogamp.opengl.GL2ES2#glBindAttribLocation(int, int, String), getAttribLocation(GL2ES2, String), getCachedAttribLocation(String)
bindAttribLocationback to summary
public void bindAttribLocation(final GL2ES2 gl, final int location, final GLArrayData data)

Binds a shader GLArrayData attribute to a location. Multiple names can be bound to one location. The value will be cached and can be retrieved via getCachedAttribLocation(String) and getAttribute(String)before or after linking. The GLArrayData's location will be set as well.

Exceptions
GLException:
  • if no program is attached
  • if the program is already linked
See Also
com.jogamp.opengl.GL2ES2#glBindAttribLocation(int, int, String), getAttribLocation(GL2ES2, String), getCachedAttribLocation(String), getAttribute(String)
destroyback to summary
public synchronized void destroy(final GL2ES2 gl)

Calls release(gl, true, true, true)

See Also
glReleaseAllVertexAttributes, glReleaseAllUniforms, release(GL2ES2, boolean, boolean, boolean)
detachObjectback to summary
public final Object detachObject(final String name)
Parameters
name:String

name of the mapped object to detach

Returns:Object

the previous mapped object or null if none

disableAllVertexAttributeArraysback to summary
public void disableAllVertexAttributeArrays(final GL2ES2 gl, final boolean removeFromState)

Disables all vertex attribute arrays. Their enabled stated will be removed from this state only if 'removeFromState' is true. This method purpose is more for debugging.

See Also
glEnableVertexAttribArray, glDisableVertexAttribArray, glVertexAttribPointer, getVertexAttribPointer, glReleaseAllVertexAttributes, glResetAllVertexAttributes, ShaderProgram#glReplaceShader
disableVertexAttribArrayback to summary
private boolean disableVertexAttribArray(final GL2ES2 gl, final String name, int location)
disableVertexAttribArrayback to summary
public boolean disableVertexAttribArray(final GL2ES2 gl, final String name)

Disables a vertex attribute array This method retrieves the the location via getAttribLocation(GL2ES2, GLArrayData) hence disableVertexAttribArray(GL2ES2, GLArrayData) shall be preferred. Even if the attribute is not found in the current shader, it is removed from this state enabled list.

Returns:boolean

false, if the name is not found, otherwise true

Exceptions
GLException:
  • if no program is attached
  • if the program is not linked and no location was cached.
See Also
glEnableVertexAttribArray, glDisableVertexAttribArray, glVertexAttribPointer, getVertexAttribPointer
disableVertexAttribArrayback to summary
public boolean disableVertexAttribArray(final GL2ES2 gl, final GLArrayData data)

Disables a vertex attribute array This method uses the GLArrayData's location if set and is the preferred alternative to disableVertexAttribArray(GL2ES2, String). If data location is unset it will be retrieved via getAttribLocation(GL2ES2, GLArrayData) set and cached in this state. Even if the attribute is not found in the current shader, it is removed from this state enabled list.

Returns:boolean

false, if the name is not found, otherwise true

Exceptions
GLException:
  • if no program is attached
  • if the program is not linked and no location was cached.
See Also
glEnableVertexAttribArray, glDisableVertexAttribArray, glVertexAttribPointer, getVertexAttribPointer
enableVertexAttribArrayback to summary
private boolean enableVertexAttribArray(final GL2ES2 gl, final String name, int location)
enableVertexAttribArrayback to summary
public boolean enableVertexAttribArray(final GL2ES2 gl, final String name)

Enables a vertex attribute array. This method retrieves the the location via getAttribLocation(GL2ES2, GLArrayData) hence enableVertexAttribArray(GL2ES2, GLArrayData) shall be preferred. Even if the attribute is not found in the current shader, it is marked enabled in this state.

Returns:boolean

false, if the name is not found, otherwise true

Exceptions
GLException:
if the program is not linked and no location was cached.
See Also
glEnableVertexAttribArray, glDisableVertexAttribArray, glVertexAttribPointer, getVertexAttribPointer
enableVertexAttribArrayback to summary
public boolean enableVertexAttribArray(final GL2ES2 gl, final GLArrayData data)

Enables a vertex attribute array, usually invoked by GLArrayDataEditable#enableBuffer(GL, boolean). This method uses the GLArrayData's location if set and is the preferred alternative to enableVertexAttribArray(GL2ES2, String). If data location is unset it will be retrieved via getAttribLocation(GL2ES2, GLArrayData) set and cached in this state. Even if the attribute is not found in the current shader, it is marked enabled in this state.

Returns:boolean

false, if the name is not found, otherwise true

Exceptions
GLException:
if the program is not linked and no location was cached.
See Also
glEnableVertexAttribArray, glDisableVertexAttribArray, glVertexAttribPointer, getVertexAttribPointer, GLArrayDataEditable#enableBuffer(GL, boolean)
getAttachedObjectback to summary
public final Object getAttachedObject(final String name)

Returns the attached user object for the given name to this ShaderState.

getAttribLocationback to summary
public int getAttribLocation(final GL2ES2 gl, final String name)

Gets the location of a shader attribute with given name.
Uses either the cached value getCachedAttribLocation(String) if valid, or the GLSL queried via GL2ES2#glGetAttribLocation(int, String).
The location will be cached.

Returns:int

-1 if there is no such attribute available, otherwise >= 0

Exceptions
GLException:
  • if no program is attached
  • if the program is not linked and no location was cached.
See Also
getCachedAttribLocation(String), bindAttribLocation(GL2ES2, int, GLArrayData), bindAttribLocation(GL2ES2, int, String), GL2ES2#glGetAttribLocation(int, String)
getAttribLocationback to summary
public int getAttribLocation(final GL2ES2 gl, final GLArrayData data)

Validates and returns the location of a shader attribute.
Uses either the cached value getCachedAttribLocation(String) if valid, or the GLSL queried via GL2ES2#glGetAttribLocation(int, String).
The location will be cached and set in the GLArrayData object.

Returns:int

-1 if there is no such attribute available, otherwise >= 0

Exceptions
GLException:
  • if no program is attached
  • if the program is not linked and no location was cached.
See Also
getCachedAttribLocation(String), bindAttribLocation(GL2ES2, int, GLArrayData), bindAttribLocation(GL2ES2, int, String), GL2ES2#glGetAttribLocation(int, String), getAttribute(String)
getAttributeback to summary
public GLArrayData getAttribute(final String name)

Get the previous cached vertex attribute data.

Returns:GLArrayData

the GLArrayData object, null if not previously set.

See Also
ownAttribute(GLArrayData, boolean), glEnableVertexAttribArray, glDisableVertexAttribArray, glVertexAttribPointer, getVertexAttribPointer, glReleaseAllVertexAttributes, glResetAllVertexAttributes, ShaderProgram#glReplaceShader
getCachedAttribLocationback to summary
public int getCachedAttribLocation(final String name)

Gets the cached location of a shader attribute.

Returns:int

-1 if there is no such attribute available, otherwise >= 0

See Also
bindAttribLocation(GL2ES2, int, String), bindAttribLocation(GL2ES2, int, GLArrayData), getAttribLocation(GL2ES2, String), GL2ES2#glGetAttribLocation(int, String)
getCachedUniformLocationback to summary
public final int getCachedUniformLocation(final String name)

Gets the cached location of the shader uniform.

Returns:int

-1 if there is no such uniform available, otherwise >= 0

getUniformback to summary
public GLUniformData getUniform(final String name)

Get the uniform data, previously set.

Returns:GLUniformData

the GLUniformData object, null if not previously set.

getUniformLocationback to summary
public final int getUniformLocation(final GL2ES2 gl, final String name)

Gets the location of a shader uniform with given name.
Uses either the cached value getCachedUniformLocation(String) if valid, or the GLSL queried via GL2ES2#glGetUniformLocation(int, String).
The location will be cached.

The current shader program (attachShaderProgram(GL2ES2, ShaderProgram)) must be in use (useProgram(GL2ES2, boolean)) !

Returns:int

-1 if there is no such attribute available, otherwise >= 0

Exceptions
GLException:
is the program is not linked
See Also
glGetUniformLocation, com.jogamp.opengl.GL2ES2#glGetUniformLocation, getUniformLocation, ShaderProgram#glReplaceShader
getUniformLocationback to summary
public int getUniformLocation(final GL2ES2 gl, final GLUniformData data)

Validates and returns the location of a shader uniform.
Uses either the cached value getCachedUniformLocation(String) if valid, or the GLSL queried via GL2ES2#glGetUniformLocation(int, String).
The location will be cached and set in the GLUniformData object.

The current shader program (attachShaderProgram(GL2ES2, ShaderProgram)) must be in use (useProgram(GL2ES2, boolean)) !

Returns:int

-1 if there is no such attribute available, otherwise >= 0

Exceptions
GLException:
is the program is not linked
See Also
glGetUniformLocation, com.jogamp.opengl.GL2ES2#glGetUniformLocation, getUniformLocation, ShaderProgram#glReplaceShader
inUseback to summary
public boolean inUse()
isActiveAttributeback to summary
public boolean isActiveAttribute(final GLArrayData attribute)
isVertexAttribArrayEnabledback to summary
public final boolean isVertexAttribArrayEnabled(final String name)
Returns:boolean

true if the named attribute is enable

isVertexAttribArrayEnabledback to summary
public final boolean isVertexAttribArrayEnabled(final GLArrayData data)
Returns:boolean

true if the GLArrayData attribute is enable

linkedback to summary
public boolean linked()
ownAttributeback to summary
public void ownAttribute(final GLArrayData attribute, final boolean own)

Binds or unbinds the GLArrayData lifecycle to this ShaderState.

If an attribute location is cached (ie bindAttribLocation(GL2ES2, int, String)) it is promoted to the GLArrayData instance.

The attribute will be destroyed with destroy(GL2ES2) and it's location will be reset when switching shader with attachShaderProgram(GL2ES2, ShaderProgram).

The data will not be transfered to the GPU, use vertexAttribPointer(GL2ES2, GLArrayData) additionally.

The data will also be associated with this ShaderState.

Parameters
attribute:GLArrayData

the GLArrayData which lifecycle shall be managed

own:boolean

true if owning shall be performs, false if disowning.

See Also
bindAttribLocation(GL2ES2, int, String), getAttribute(String), GLArrayData#associate(Object, boolean)
ownsAttributeback to summary
public boolean ownsAttribute(final GLArrayData attribute)
ownsUniformback to summary
public boolean ownsUniform(final GLUniformData uniform)
ownUniformback to summary
public void ownUniform(final GLUniformData uniform)

Bind the GLUniform lifecycle to this ShaderState.

If a uniform location is cached it is promoted to the GLUniformData instance.

The attribute will be destroyed with destroy(GL2ES2) and it's location will be reset when switching shader with attachShaderProgram(GL2ES2, ShaderProgram).

The data will not be transfered to the GPU, use uniform(GL2ES2, GLUniformData) additionally.

Parameters
uniform:GLUniformData

the GLUniformData which lifecycle shall be managed

See Also
getUniform(String)
releaseback to summary
public synchronized void release(final GL2ES2 gl, final boolean destroyBoundAttributes, final boolean destroyShaderProgram, final boolean destroyShaderCode)
See Also
glReleaseAllVertexAttributes, glReleaseAllUniforms, ShaderProgram#release(GL2ES2, boolean)
releaseAllAttributesback to summary
public void releaseAllAttributes(final GL2ES2 gl)

Releases all mapped vertex attribute data, disables all enabled attributes and loses all indices

See Also
glEnableVertexAttribArray, glDisableVertexAttribArray, glVertexAttribPointer, getVertexAttribPointer, glReleaseAllVertexAttributes, glResetAllVertexAttributes, ShaderProgram#glReplaceShader
releaseAllDataback to summary
public synchronized void releaseAllData(final GL2ES2 gl)

Calls release(gl, false, false, false)

See Also
glReleaseAllVertexAttributes, glReleaseAllUniforms, release(GL2ES2, boolean, boolean, boolean)
releaseAllUniformsback to summary
public void releaseAllUniforms(final GL2ES2 gl)

Releases all mapped uniform data and loses all indices

relocateAttributeback to summary
private final void relocateAttribute(final GL2ES2 gl, final GLArrayData attribute)
resetAllAttributesback to summary
private final void resetAllAttributes(final GL2ES2 gl)

Reset all previously enabled mapped vertex attribute data.

Attribute data is bound to the GL state, i.e. VBO data itself will not be updated.

Attribute location and it's data assignment is bound to the program, hence both are updated.

Note

Such update could only be prevented, if tracking am attribute/program dirty flag.

Exceptions
GLException:
is the program is not linked
See Also
attachShaderProgram(GL2ES2, ShaderProgram)
resetAllUniformsback to summary
private final void resetAllUniforms(final GL2ES2 gl)

Reset all previously mapped uniform data

Uniform data and location is bound to the program, hence both are updated.

Note

Such update could only be prevented, if tracking a uniform/program dirty flag.

Exceptions
GLException:
is the program is not in use
See Also
attachShaderProgram(GL2ES2, ShaderProgram)
setAllAttributesback to summary
private final void setAllAttributes(final GL2ES2 gl)

preserves the attribute location .. (program not linked)

setAttributeback to summary
private final void setAttribute(final GL2ES2 gl, final GLArrayData attribute)
setVerboseback to summary
public void setVerbose(final boolean v)
shaderProgramback to summary
public ShaderProgram shaderProgram()
toStringback to summary
public StringBuilder toString(StringBuilder sb, final boolean alsoUnlocated)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object

Annotations
@Override
uniformback to summary
public boolean uniform(final GL2ES2 gl, final GLUniformData data)

Set the uniform data, if it's location is valid, i.e. ≥ 0.

This method uses the GLUniformData's location if valid, i.e. ≥ 0.
If data's location is invalid, it will be retrieved via getUniformLocation(GL2ES2, GLUniformData), set and cached in this state.

Returns:boolean

false, if the location could not be determined, otherwise true

See Also
glGetUniformLocation, com.jogamp.opengl.GL2ES2#glGetUniformLocation, com.jogamp.opengl.GL2ES2#glUniform, getUniformLocation, ShaderProgram#glReplaceShader
useProgramback to summary
public synchronized void useProgram(final GL2ES2 gl, final boolean on) throws GLException

Turns the shader program on or off.

Exceptions
GLException:
if no program is attached
See Also
com.jogamp.opengl.util.glsl.ShaderState#useProgram(GL2ES2, boolean)
verboseback to summary
public boolean verbose()
vertexAttribPointerback to summary
public boolean vertexAttribPointer(final GL2ES2 gl, final GLArrayData data)

Set the GLArrayData vertex attribute data, if it's location is valid, i.e. ≥ 0.

This method uses the GLArrayData's location if valid, i.e. ≥ 0.
If data's location is invalid, it will be retrieved via getAttribLocation(GL2ES2, GLArrayData), set and cached in this state.

Returns:boolean

false, if the location could not be determined, otherwise true

Exceptions
GLException:
  • if no program is attached
  • if the program is not linked and no location was cached.
See Also
glEnableVertexAttribArray, glDisableVertexAttribArray, glVertexAttribPointer, getVertexAttribPointer