VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
Public Member Functions | Protected Member Functions | List of all members

Shader which has instanced values for uniforms and other GL state. More...

Inheritance diagram for InstancedShader:
Shader InstancedShaderBase< TShaderImpl > InstancedShaderBase< SpriteShader > TerrainShader SingletonInstancedShaderBase< TShaderImpl > SingletonInstancedShaderBase< SpriteShader > SpriteShader

Public Member Functions

 InstancedShader (const ShaderPtr &sharedShader)
 Create an InstancedShader which references a shader Shader. More...
 
const ShaderPtrgetSharedShader () const
 Returns the Shader that is shared among instances. More...
 
virtual bool isCurrent () const override
 Returns whether or not this instance is bound. More...
 
virtual bool isValid () const override
 Returns whether or not this shader is valid (compiled and linked) and can be used for rendering. More...
 
virtual bool isCompiled () const override
 Returns whether or not this shader has been processed for compilation. More...
 
virtual GLuint getProgramId () const override
 Returns the GL program id for this shader. More...
 
virtual void begin () override
 Binds this shader, and sets uniforms and other GL state associated with this instance. More...
 
virtual void end () override
 Unbinds this shader if bound by a corresponding call to begin(). More...
 
virtual void compile () override
 Compiles this shader, resulting in the compilation of the shared Shader if necessary. More...
 
virtual std::string getErrorLog () const override
 Retrieves the string describing compilation errors, if the call to compile() failed and isValid() is false. More...
 
virtual int getAttributeLocation (int index) const override
 Returns the location of an indexed Attribute. More...
 
virtual int getUniformLocation (int index) const override
 Returns the location of an indexed uniform. More...
 
void setIndexedTexture (int index, int stage, const TexturePtr &value, TexturePtr *stateValue)
 Set the indexed texture for this instance at the given index. More...
 
- Public Member Functions inherited from Shader
 Shader (const AttributeSetPtr &attributes, const std::vector< std::string > &uniformNames)
 Construct a Shader with the given list of attributes and uniforms. More...
 
const AttributeSetPtrgetAttributes () const
 Returns the list of indexed Attributes for this shader, as an AttributeSet. More...
 
const std::vector< std::string > & getUniformNames () const
 Returns the list of indexed uniforms. More...
 
void setIndexedUniform1f (int index, bool global, float value, float *stateValue)
 Set the value of a float scalar uniform at the given index. More...
 
void setIndexedUniform2f (int index, bool global, const Vector2 &value, Vector2 *stateValue)
 Set the value of a 2D float vector uniform at the given index. More...
 
void setIndexedUniform3f (int index, bool global, float value1, float value2, float value3, float *stateValue)
 Set the value of a 3D float vector uniform at the given index. More...
 
void setIndexedUniform4f (int index, bool global, float value1, float value2, float value3, float value4, float *stateValue)
 Set the value of a 4D float vector uniform at the given index. More...
 
void setIndexedUniform1i (int index, bool global, int value, int *stateValue)
 Set the value of an integer scalar uniform at the given index. More...
 
void setIndexedUniformMatrix4f (int index, bool global, bool transpose, const Matrix4 &value, Matrix4 *stateValue)
 Set the value of a 4x4 float matrix uniform at the given index. More...
 

Protected Member Functions

virtual void updateState ()
 Update state in response to this instance getting bound. More...
 

Detailed Description

Shader which has instanced values for uniforms and other GL state.

All instances of the same shader share the same underlying shader program, but will upload new values for uniforms specific to that instance when selected for rendering. Because each instance stores a copy of its own state, instanced shaders can effectively be used as simple "materials".

Note
The setIndexed family of methods (many inherited from Shader) are helpers to implement subclasses of InstancedShaders. The value to set is copied into stateValue. This should normally be a member variable of the InstancedShader subclass. If the instance is currently bound, the state change then happens immediately. If the instance is not bound, the saved off value will get set during the next call to begin(). This is implemented by the subclass by overriding updateStates() which is called by begin().
Global uniforms are those that are uninstanced and shared between all instances. These will always be set immediately, regardless of whether or not they are currently bound. They also do not require that their value be saved off to a member variable. An example of a global uniform might be a projection matrix. There might only ever need to be one value for all instances if there is only one camera. An example of an instanced uniform might be color. All instances of the shader would be able to specify their own unique colors.

Constructor & Destructor Documentation

InstancedShader::InstancedShader ( const ShaderPtr sharedShader)
explicit

Create an InstancedShader which references a shader Shader.

Parameters
sharedShaderShader that is shared among instances, and that will receive specific uniform values when this instance is bound

Member Function Documentation

virtual void InstancedShader::begin ( )
overridevirtual

Binds this shader, and sets uniforms and other GL state associated with this instance.

Shader uniforms and GL state are updated through a call to updateState(). Multiple calls to begin have no effect.

Implements Shader.

virtual void InstancedShader::compile ( )
overridevirtual

Compiles this shader, resulting in the compilation of the shared Shader if necessary.

Implements Shader.

virtual void InstancedShader::end ( )
overridevirtual

Unbinds this shader if bound by a corresponding call to begin().

If the shader is not currently selected, this call has no effect.

Implements Shader.

virtual int InstancedShader::getAttributeLocation ( int  index) const
overridevirtual

Returns the location of an indexed Attribute.

Indexed attributes are those that are named in the AttributeSet provided to the shader's constructor.

Parameters
indexIndex into the list of attributes

Implements Shader.

virtual std::string InstancedShader::getErrorLog ( ) const
overridevirtual

Retrieves the string describing compilation errors, if the call to compile() failed and isValid() is false.

Returns
String describing compilation errors

Implements Shader.

virtual GLuint InstancedShader::getProgramId ( ) const
overridevirtual

Returns the GL program id for this shader.

Implements Shader.

const ShaderPtr& InstancedShader::getSharedShader ( ) const
inline

Returns the Shader that is shared among instances.

virtual int InstancedShader::getUniformLocation ( int  index) const
overridevirtual

Returns the location of an indexed uniform.

Indexed uniforms are those that are named in the list of uniforms provided to the shader's constructor.

Parameters
indexIndex into the list of uniforms

Implements Shader.

virtual bool InstancedShader::isCompiled ( ) const
overridevirtual

Returns whether or not this shader has been processed for compilation.

Note
This does not indicate whether compilation succeeded. To test for success, use isValid().

Implements Shader.

virtual bool InstancedShader::isCurrent ( ) const
overridevirtual

Returns whether or not this instance is bound.

Implements Shader.

virtual bool InstancedShader::isValid ( ) const
overridevirtual

Returns whether or not this shader is valid (compiled and linked) and can be used for rendering.

Implements Shader.

void InstancedShader::setIndexedTexture ( int  index,
int  stage,
const TexturePtr value,
TexturePtr stateValue 
)

Set the indexed texture for this instance at the given index.

See InstancedShader for details on the setIndexed family of methods.

Parameters
indexIndex of the indexed texture
stageTexture stage to bind the texture to
valueTexture to bind
stateValueMemory to save off state to, or nullptr if state should not be saved
virtual void InstancedShader::updateState ( )
protectedvirtual

Update state in response to this instance getting bound.

Implemented by subclasses. Implementers should update state such as uniforms that have values specific to this instance. The setIndexed family of methods can be used, with the stateValue parameters set to nullptr, and the value parameter set to the previously saved off value.

Reimplemented in TerrainShader, and SpriteShader.