Class ShaderState
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
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal ObjectattachObject(String name, Object obj) Attach user object for the given name to this ShaderState.booleanattachShaderProgram(GL2ES2 gl, ShaderProgram prog, boolean enable) Attach or switch a shader programvoidbindAttribLocation(GL2ES2 gl, int location, GLArrayData data) Binds a shaderGLArrayDataattribute to a location.voidbindAttribLocation(GL2ES2 gl, int location, String name) Binds a shader attribute to a location.voidfinal ObjectdetachObject(String name) voiddisableAllVertexAttributeArrays(GL2ES2 gl, boolean removeFromState) Disables all vertex attribute arrays.booleandisableVertexAttribArray(GL2ES2 gl, GLArrayData data) Disables a vertex attribute array This method uses theGLArrayData's location if set and is the preferred alternative todisableVertexAttribArray(GL2ES2, String).booleandisableVertexAttribArray(GL2ES2 gl, String name) Disables a vertex attribute array This method retrieves the the location viagetAttribLocation(GL2ES2, GLArrayData)hencedisableVertexAttribArray(GL2ES2, GLArrayData)shall be preferred.booleanenableVertexAttribArray(GL2ES2 gl, GLArrayData data) Enables a vertex attribute array, usually invoked byGLArrayDataEditable.enableBuffer(GL, boolean).booleanenableVertexAttribArray(GL2ES2 gl, String name) Enables a vertex attribute array.final ObjectgetAttachedObject(String name) Returns the attached user object for the given name to this ShaderState.intgetAttribLocation(GL2ES2 gl, GLArrayData data) Validates and returns the location of a shader attribute.
Uses either the cached valuegetCachedAttribLocation(String)if valid, or the GLSL queried viaGL2ES2.glGetAttribLocation(int, String).
The location will be cached and set in theGLArrayDataobject.intgetAttribLocation(GL2ES2 gl, String name) Gets the location of a shader attribute with givenname.
Uses either the cached valuegetCachedAttribLocation(String)if valid, or the GLSL queried viaGL2ES2.glGetAttribLocation(int, String).
The location will be cached.getAttribute(String name) Get the previous cached vertex attribute data.intGets the cached location of a shader attribute.final intGets the cached location of the shader uniform.getUniform(String name) Get the uniform data, previously set.intgetUniformLocation(GL2ES2 gl, GLUniformData data) Validates and returns the location of a shader uniform.
Uses either the cached valuegetCachedUniformLocation(String)if valid, or the GLSL queried viaGL2ES2.glGetUniformLocation(int, String).
The location will be cached and set in theGLUniformDataobject.final intgetUniformLocation(GL2ES2 gl, String name) Gets the location of a shader uniform with givenname.
Uses either the cached valuegetCachedUniformLocation(String)if valid, or the GLSL queried viaGL2ES2.glGetUniformLocation(int, String).
The location will be cached.booleaninUse()booleanisActiveAttribute(GLArrayData attribute) final booleanfinal booleanbooleanlinked()voidownAttribute(GLArrayData attribute, boolean own) Binds or unbinds theGLArrayDatalifecycle to this ShaderState.booleanownsAttribute(GLArrayData attribute) booleanownsUniform(GLUniformData uniform) voidownUniform(GLUniformData uniform) Bind theGLUniformlifecycle to this ShaderState.voidrelease(GL2ES2 gl, boolean destroyBoundAttributes, boolean destroyShaderProgram, boolean destroyShaderCode) voidReleases all mapped vertex attribute data, disables all enabled attributes and loses all indicesvoidreleaseAllData(GL2ES2 gl) voidReleases all mapped uniform data and loses all indicesvoidsetVerbose(boolean v) toString()toString(StringBuilder sb, boolean alsoUnlocated) booleanuniform(GL2ES2 gl, GLUniformData data) Set the uniform data, if it's location is valid, i.e.voiduseProgram(GL2ES2 gl, boolean on) Turns the shader program on or off.booleanverbose()booleanvertexAttribPointer(GL2ES2 gl, GLArrayData data) Set theGLArrayDatavertex attribute data, if it's location is valid, i.e.
-
Field Details
-
DEBUG
public static final boolean DEBUG
-
-
Constructor Details
-
ShaderState
public ShaderState()
-
-
Method Details
-
verbose
public boolean verbose() -
setVerbose
public void setVerbose(boolean v) -
getAttachedObject
Returns the attached user object for the given name to this ShaderState. -
attachObject
Attach user object for the given name to this ShaderState. Returns the previously set object or null.- Returns:
- the previous mapped object or null if none
-
detachObject
- Parameters:
name- name of the mapped object to detach- Returns:
- the previous mapped object or null if none
-
useProgram
Turns the shader program on or off.- Throws:
GLException- if no program is attached- See Also:
-
linked
public boolean linked() -
inUse
public boolean inUse() -
attachShaderProgram
public boolean attachShaderProgram(GL2ES2 gl, ShaderProgram prog, boolean enable) throws GLException Attach or switch a shader programAttaching 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), ifenableistrue.- Returns:
- true if shader program was attached, otherwise false (already attached)
- Throws:
GLException- if program was not linked and linking fails
-
shaderProgram
-
destroy
- See Also:
-
#glReleaseAllVertexAttributes#glReleaseAllUniformsrelease(GL2ES2, boolean, boolean, boolean)
-
releaseAllData
- See Also:
-
#glReleaseAllVertexAttributes#glReleaseAllUniformsrelease(GL2ES2, boolean, boolean, boolean)
-
release
public void release(GL2ES2 gl, boolean destroyBoundAttributes, boolean destroyShaderProgram, boolean destroyShaderCode) - See Also:
-
#glReleaseAllVertexAttributes#glReleaseAllUniformsShaderProgram.release(GL2ES2, boolean)
-
getCachedAttribLocation
Gets the cached location of a shader attribute.- Returns:
- -1 if there is no such attribute available, otherwise >= 0
- See Also:
-
getAttribute
Get the previous cached vertex attribute data.- Returns:
- the GLArrayData object, null if not previously set.
- See Also:
-
ownAttribute(GLArrayData, boolean)#glEnableVertexAttribArray#glDisableVertexAttribArray#glVertexAttribPointer#getVertexAttribPointer#glReleaseAllVertexAttributes#glResetAllVertexAttributesShaderProgram#glReplaceShader
-
isActiveAttribute
-
ownAttribute
Binds or unbinds theGLArrayDatalifecycle to this ShaderState.If an attribute location is cached (ie
bindAttribLocation(GL2ES2, int, String)) it is promoted to theGLArrayDatainstance.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
associatedwith this ShaderState.- Parameters:
attribute- theGLArrayDatawhich lifecycle shall be managedown- true if owning shall be performs, false if disowning.- See Also:
-
ownsAttribute
-
bindAttribLocation
Binds a shader attribute to a location. Multiple names can be bound to one location. The value will be cached and can be retrieved viagetCachedAttribLocation(String)before or after linking.- Throws:
GLException- if no program is attachedGLException- if the program is already linked- See Also:
-
bindAttribLocation
Binds a shaderGLArrayDataattribute to a location. Multiple names can be bound to one location. The value will be cached and can be retrieved viagetCachedAttribLocation(String)andgetAttribute(String)before or after linking. TheGLArrayData's location will be set as well.- Throws:
GLException- if no program is attachedGLException- if the program is already linked- See Also:
-
getAttribLocation
Gets the location of a shader attribute with givenname.
Uses either the cached valuegetCachedAttribLocation(String)if valid, or the GLSL queried viaGL2ES2.glGetAttribLocation(int, String).
The location will be cached.- Returns:
- -1 if there is no such attribute available, otherwise >= 0
- Throws:
GLException- if no program is attachedGLException- if the program is not linked and no location was cached.- See Also:
-
getAttribLocation
Validates and returns the location of a shader attribute.
Uses either the cached valuegetCachedAttribLocation(String)if valid, or the GLSL queried viaGL2ES2.glGetAttribLocation(int, String).
The location will be cached and set in theGLArrayDataobject.- Returns:
- -1 if there is no such attribute available, otherwise >= 0
- Throws:
GLException- if no program is attachedGLException- if the program is not linked and no location was cached.- See Also:
-
isVertexAttribArrayEnabled
- Returns:
- true if the named attribute is enable
-
isVertexAttribArrayEnabled
- Returns:
- true if the
GLArrayDataattribute is enable
-
enableVertexAttribArray
Enables a vertex attribute array. This method retrieves the the location viagetAttribLocation(GL2ES2, GLArrayData)henceenableVertexAttribArray(GL2ES2, GLArrayData)shall be preferred. Even if the attribute is not found in the current shader, it is marked enabled in this state.- Returns:
- false, if the name is not found, otherwise true
- Throws:
GLException- if the program is not linked and no location was cached.- See Also:
-
#glEnableVertexAttribArray#glDisableVertexAttribArray#glVertexAttribPointer#getVertexAttribPointer
-
enableVertexAttribArray
Enables a vertex attribute array, usually invoked byGLArrayDataEditable.enableBuffer(GL, boolean). This method uses theGLArrayData's location if set and is the preferred alternative toenableVertexAttribArray(GL2ES2, String). If data location is unset it will be retrieved viagetAttribLocation(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:
- false, if the name is not found, otherwise true
- Throws:
GLException- if the program is not linked and no location was cached.- See Also:
-
#glEnableVertexAttribArray#glDisableVertexAttribArray#glVertexAttribPointer#getVertexAttribPointerGLArrayDataEditable.enableBuffer(GL, boolean)
-
disableVertexAttribArray
Disables a vertex attribute array This method retrieves the the location viagetAttribLocation(GL2ES2, GLArrayData)hencedisableVertexAttribArray(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:
- false, if the name is not found, otherwise true
- Throws:
GLException- if no program is attachedGLException- if the program is not linked and no location was cached.- See Also:
-
#glEnableVertexAttribArray#glDisableVertexAttribArray#glVertexAttribPointer#getVertexAttribPointer
-
disableVertexAttribArray
Disables a vertex attribute array This method uses theGLArrayData's location if set and is the preferred alternative todisableVertexAttribArray(GL2ES2, String). If data location is unset it will be retrieved viagetAttribLocation(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:
- false, if the name is not found, otherwise true
- Throws:
GLException- if no program is attachedGLException- if the program is not linked and no location was cached.- See Also:
-
#glEnableVertexAttribArray#glDisableVertexAttribArray#glVertexAttribPointer#getVertexAttribPointer
-
vertexAttribPointer
Set theGLArrayDatavertex 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 viagetAttribLocation(GL2ES2, GLArrayData), set and cached in this state.- Returns:
- false, if the location could not be determined, otherwise true
- Throws:
GLException- if no program is attachedGLException- if the program is not linked and no location was cached.- See Also:
-
#glEnableVertexAttribArray#glDisableVertexAttribArray#glVertexAttribPointer#getVertexAttribPointer
-
releaseAllAttributes
Releases all mapped vertex attribute data, disables all enabled attributes and loses all indices- See Also:
-
#glEnableVertexAttribArray#glDisableVertexAttribArray#glVertexAttribPointer#getVertexAttribPointer#glReleaseAllVertexAttributes#glResetAllVertexAttributes#glResetAllVertexAttributesShaderProgram#glReplaceShader
-
disableAllVertexAttributeArrays
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#glResetAllVertexAttributesShaderProgram#glReplaceShader
-
getCachedUniformLocation
Gets the cached location of the shader uniform.- Returns:
- -1 if there is no such uniform available, otherwise >= 0
-
ownUniform
Bind theGLUniformlifecycle to this ShaderState.If a uniform location is cached it is promoted to the
GLUniformDatainstance.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- theGLUniformDatawhich lifecycle shall be managed- See Also:
-
ownsUniform
-
getUniformLocation
Gets the location of a shader uniform with givenname.
Uses either the cached valuegetCachedUniformLocation(String)if valid, or the GLSL queried viaGL2ES2.glGetUniformLocation(int, String).
The location will be cached.The current shader program (
#attachShaderProgram(GL2ES2, ShaderProgram)) must be in use (useProgram(GL2ES2, boolean)) !- Returns:
- -1 if there is no such attribute available, otherwise >= 0
- Throws:
GLException- is the program is not linked- See Also:
-
#glGetUniformLocationGL2ES2.glGetUniformLocation(int, java.lang.String)getUniformLocation(com.jogamp.opengl.GL2ES2, java.lang.String)ShaderProgram#glReplaceShader
-
getUniformLocation
Validates and returns the location of a shader uniform.
Uses either the cached valuegetCachedUniformLocation(String)if valid, or the GLSL queried viaGL2ES2.glGetUniformLocation(int, String).
The location will be cached and set in theGLUniformDataobject.The current shader program (
#attachShaderProgram(GL2ES2, ShaderProgram)) must be in use (useProgram(GL2ES2, boolean)) !- Returns:
- -1 if there is no such attribute available, otherwise >= 0
- Throws:
GLException- is the program is not linked- See Also:
-
#glGetUniformLocationGL2ES2.glGetUniformLocation(int, java.lang.String)getUniformLocation(com.jogamp.opengl.GL2ES2, java.lang.String)ShaderProgram#glReplaceShader
-
uniform
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 viagetUniformLocation(GL2ES2, GLUniformData), set and cached in this state.- Returns:
- false, if the location could not be determined, otherwise true
- See Also:
-
#glGetUniformLocationGL2ES2.glGetUniformLocation(int, java.lang.String)GL2ES2.glUniform(com.jogamp.opengl.GLUniformData)getUniformLocation(com.jogamp.opengl.GL2ES2, java.lang.String)ShaderProgram#glReplaceShader
-
getUniform
Get the uniform data, previously set.- Returns:
- the GLUniformData object, null if not previously set.
-
releaseAllUniforms
Releases all mapped uniform data and loses all indices -
toString
-
toString
-