FPLBase
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
fplbase::Shader Class Reference

Represents a shader consisting of a vertex and pixel shader. More...

#include <shader.h>

Inheritance diagram for fplbase::Shader:
fplbase::AsyncAsset fplbase::Asset

Detailed Description

Represents a shader consisting of a vertex and pixel shader.

Represents a shader consisting of a vertex and pixel shader. Also stores ids of standard uniforms. Use the Renderer class below to create these.

Public Member Functions

 Shader (const char *filename, const std::vector< std::string > &local_defines, Renderer *renderer)
 
 Shader (ShaderHandle program, ShaderHandle vs, ShaderHandle ps)
 
void UpdateGlobalDefines (const std::vector< std::string > &global_defines_to_add, const std::vector< std::string > &global_defines_to_omit)
 Recalculates the defines of this shader, and marks dirty if they differ from the last Reload(). More...
 
bool ReloadIfDirty ()
 If the shader has been marked dirty, reload it and clear the dirty flag. More...
 
virtual void Load ()
 Loads and unpacks the Shader from filename_ into data_.
 
virtual bool Finalize ()
 Creates a Shader from data_.
 
bool IsValid ()
 Whether this object loaded and finalized correctly. Call after Finalize has been called (by AssetManager::TryFinalize).
 
void Set (const Renderer &renderer) const
 Activate this shader for subsequent draw calls. More...
 
UniformHandle FindUniform (const char *uniform_name)
 Find a non-standard uniform by name. More...
 
void SetUniform (UniformHandle uniform_loc, const float *value, size_t num_components)
 Raw call to set any uniform (with 1/2/3/4/16 components). More...
 
template<int N>
void SetUniform (UniformHandle uniform_loc, const mathfu::Vector< float, N > &value)
 Set an non-standard uniform to a vec2/3/4 value. More...
 
template<int N>
bool SetUniform (const char *uniform_name, const mathfu::Vector< float, N > &value)
 Convenience call that does a Lookup and a Set if found. More...
 
bool SetUniform (const char *uniform_name, float value)
 Set a non-standard uniform to a float value. More...
 
bool SetUniform (const char *uniform_name, const mathfu::mat4 &value)
 Set a non-standard uniform to a mat4 value. More...
 
void InitializeUniforms ()
 
ShaderHandle program () const
 
bool HasDefine (const char *define) const
 
bool IsDirty () const
 
void MarkDirty ()
 Call to mark the shader as needing to be reloaded. More...
 
ShaderImpl * impl ()
 
- Public Member Functions inherited from fplbase::AsyncAsset
 AsyncAsset ()
 Default constructor for an empty AsyncAsset.
 
 AsyncAsset (const char *filename)
 Construct an AsyncAsset with a given file name. More...
 
virtual ~AsyncAsset ()
 AsyncAsset destructor.
 
bool LoadNow ()
 Performs a synchronous load by calling Load & Finalize. More...
 
void set_filename (const std::string &filename)
 Sets the filename that should be loaded. More...
 
const std::string & filename () const
 The name of the file associated with the resource. More...
 
void AddFinalizeCallback (AssetFinalizedCallback callback)
 Adds a callback to be called when the asset is finalized. More...
 
- Public Member Functions inherited from fplbase::Asset
void IncreaseRefCount ()
 indicate there is an additional owner of this asset. By default, when you call any of the UnLoad*() functions in the AssetManager, that will directly delete the asset since they all start out with a single reference count. Call this function to indicate multiple owners will call Unload*() independently, and only have the asset deleted by the last one.
 

Static Public Member Functions

static ShaderLoadFromShaderDef (const char *filename)
 Loads a .fplshader file from disk. Used by the more convenient AssetManager interface, but can also be used without it.
 

Friends

class RendererBase
 

Additional Inherited Members

- Public Types inherited from fplbase::AsyncAsset
typedef std::function< void()> AssetFinalizedCallback
 A function pointer to an asset loaded callback function.
 
- Protected Member Functions inherited from fplbase::AsyncAsset
void CallFinalizeCallback ()
 Calls app callbacks when an asset is ready to be used. More...
 
- Protected Attributes inherited from fplbase::AsyncAsset
std::string filename_
 The resource file name.
 
const uint8_t * data_
 The resource data.
 
std::vector
< AssetFinalizedCallback
finalize_callbacks_
 

Member Function Documentation

UniformHandle fplbase::Shader::FindUniform ( const char *  uniform_name)

Find a non-standard uniform by name.

Parameters
uniform_nameThe name of the uniform to find.
Returns
Returns a handle to the requested uniform, -1 if not found.
void fplbase::Shader::MarkDirty ( )
inline

Call to mark the shader as needing to be reloaded.

Useful when you've changed the shader source and want to dynamically re-compile the shader.

Note
Be sure to call ReloadIfDirty() on your render thread before the shader is used. Otherwise an assert will be hit in Shader::Set().
bool fplbase::Shader::ReloadIfDirty ( )

If the shader has been marked dirty, reload it and clear the dirty flag.

Lazy loading ensures shaders are only loaded and compiled when they are used. Also allows the caller to ensure that the reload is happening on the correct thread.

void fplbase::Shader::Set ( const Renderer renderer) const

Activate this shader for subsequent draw calls.

Will make this shader active for any subsequent draw calls, and sets all standard uniforms (e.g. mvp matrix) based on current values in Renderer, if this shader refers to them.

Parameters
rendererThe renderer that has the standard uniforms set.
void fplbase::Shader::SetUniform ( UniformHandle  uniform_loc,
const float *  value,
size_t  num_components 
)

Raw call to set any uniform (with 1/2/3/4/16 components).

More convenient variants below.

Parameters
uniform_locHandle to the uniform that will be set.
valueThe value to set the uniform to.
num_componentsThe number of components used by the uniform.
template<int N>
void fplbase::Shader::SetUniform ( UniformHandle  uniform_loc,
const mathfu::Vector< float, N > &  value 
)
inline

Set an non-standard uniform to a vec2/3/4 value.

Call this after Set() or FindUniform().

Parameters
uniform_locHandle to the uniform that will be set.
valueThe vector to set the uniform to.
template<int N>
bool fplbase::Shader::SetUniform ( const char *  uniform_name,
const mathfu::Vector< float, N > &  value 
)
inline

Convenience call that does a Lookup and a Set if found.

Call this after Set().

Parameters
uniform_nameThe name of the uniform that will be set.
valueThe vector to set the uniform to.
Returns
Returns true if the uniform was found and set, false otherwise.
bool fplbase::Shader::SetUniform ( const char *  uniform_name,
float  value 
)
inline

Set a non-standard uniform to a float value.

Call this after Set().

Parameters
uniform_nameThe name of the uniform that will be set.
valueThe float to set the uniform to.
Returns
Returns true if the uniform was found and set, false otherwise.
bool fplbase::Shader::SetUniform ( const char *  uniform_name,
const mathfu::mat4 &  value 
)
inline

Set a non-standard uniform to a mat4 value.

Call this after Set().

Parameters
uniform_nameThe name of the uniform that will be set.
valueThe mat4 to set the uniform to.
Returns
Returns true if the uniform was found and set, false otherwise.
void fplbase::Shader::UpdateGlobalDefines ( const std::vector< std::string > &  global_defines_to_add,
const std::vector< std::string > &  global_defines_to_omit 
)

Recalculates the defines of this shader, and marks dirty if they differ from the last Reload().

Defines change the shader source code when it is preprocessed. Typically, you have one uber-shader that contains all possible shader features, and enable the features using preprocessor defines. This function allows you to specify which features are enabled.

Note
This function does not reload the shader. It will simply mark the shader as dirty, if its defines have changed.
Parameters
global_defines_to_addthe defines to be added into 'local_defines' above. These are generally global rendering settings, for example, enabling shadows or specular.
global_defines_to_omitthe defines to forcefully omit from 'local_defines'. Generally used to globally disable expensive features such as shadows, on low-powered hardware.

The documentation for this class was generated from the following file: