OpenWalnut  1.5.0dev
Classes | Public Types | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
WGEShader Class Reference

Class encapsulating the OSG Program class for a more convenient way of adding and modifying shader. More...

#include <WGEShader.h>

+ Inheritance diagram for WGEShader:
+ Collaboration diagram for WGEShader:

Classes

class  SafeUpdaterCallback
 Update callback which handles the shader reloading. More...
 

Public Types

typedef osg::ref_ptr< WGEShaderRefPtr
 Convenience typedef for an osg::ref_ptr. More...
 
typedef osg::ref_ptr< const WGEShaderConstRefPtr
 Convenience typedef for an osg::ref_ptr; const. More...
 

Public Member Functions

 WGEShader (std::string name, boost::filesystem::path search=WPathHelper::getShaderPath())
 Default constructor. More...
 
virtual ~WGEShader ()
 Destructor. More...
 
virtual void apply (osg::ref_ptr< osg::Node > node)
 Apply this shader to the specified node. More...
 
virtual void applyDirect (osg::State &state)
 If enabled, activate our program in the GL pipeline, performing any rebuild operations that might be pending. More...
 
virtual void deactivate (osg::ref_ptr< osg::Node > node)
 Removes the shader from the specified node. More...
 
virtual void reload ()
 Initiate a reload of the shader during the next update cycle. More...
 
template<typename T >
WGEShaderDefine< T >::SPtr setDefine (std::string key, T value)
 Sets a define which is include into the shader source code. More...
 
WGEShaderDefineSwitch::SPtr setDefine (std::string key)
 Sets a define which is include into the shader source code. More...
 
void addPreprocessor (WGEShaderPreprocessor::SPtr preproc)
 Adds the specified preprocessor to this shader. More...
 
void removePreprocessor (WGEShaderPreprocessor::SPtr preproc)
 Removes the specified preprocessor. More...
 
void clearPreprocessors ()
 Removes all preprocessors. More...
 

Protected Types

typedef WSharedAssociativeContainer< std::map< WGEShaderPreprocessor::SPtr, boost::signals2::connection > > PreprocessorsList
 The list of preprocessors - Type. More...
 

Protected Member Functions

std::string processShaderRecursive (const std::string filename, bool optional=false, int level=0)
 This method searches and processes all includes in the shader source. More...
 
std::string processShader (const std::string filename, bool optional=false)
 This method searches and processes all includes in the shader source. More...
 
void reloadShader ()
 This completely reloads the shader file and processes it. More...
 
void updatePrograms ()
 Handles all state changes in m_reload and m_deactivated. More...
 

Protected Attributes

boost::filesystem::path m_shaderPath
 String that stores the location of all shader files. More...
 
std::string m_name
 The name of the shader. More...
 
bool m_reload
 Flag denoting whether a shader should be reloaded. More...
 
bool m_shaderLoaded
 True if the shaders have been loaded successfully previously. More...
 
bool m_deactivated
 Flag denoting whether a shader should be deactivated. More...
 
boost::signals2::connection m_reloadSignalConnection
 Connection object to the reload signal from WGraphbicsEngine. More...
 
PreprocessorsList m_preprocessors
 List of all pre-processing that need to be applied to this shader instance. More...
 
WGEShaderPreprocessor::SPtr m_versionPreprocessor
 This preprocessor needs to be run LAST. More...
 
osg::ref_ptr< osg::Shader > m_vertexShader
 the vertex shader object More...
 
osg::ref_ptr< osg::Shader > m_fragmentShader
 the fragment shader object More...
 
osg::ref_ptr< osg::Shader > m_geometryShader
 the geometry shader object More...
 

Detailed Description

Class encapsulating the OSG Program class for a more convenient way of adding and modifying shader.

Definition at line 47 of file WGEShader.h.

Member Typedef Documentation

◆ ConstRefPtr

typedef osg::ref_ptr< const WGEShader > WGEShader::ConstRefPtr

Convenience typedef for an osg::ref_ptr; const.

Definition at line 58 of file WGEShader.h.

◆ PreprocessorsList

typedef WSharedAssociativeContainer< std::map< WGEShaderPreprocessor::SPtr, boost::signals2::connection > > WGEShader::PreprocessorsList
protected

The list of preprocessors - Type.

Definition at line 215 of file WGEShader.h.

◆ RefPtr

typedef osg::ref_ptr< WGEShader > WGEShader::RefPtr

Convenience typedef for an osg::ref_ptr.

Definition at line 53 of file WGEShader.h.

Constructor & Destructor Documentation

◆ WGEShader()

WGEShader::WGEShader ( std::string  name,
boost::filesystem::path  search = WPathHelper::getShaderPath() 
)

Default constructor.

Loads the specified shader programs. The path that can be specified is optional but allows modules to load their own local shaders. The search order for shader files is as follows: 1. search, 2. search/shaders, 3. WPathHelper::getShaderPath()

Parameters
namethe name of the shader. It gets searched in the shader path.
searchthe local search path. If not specified, the global shader path is used.

Definition at line 49 of file WGEShader.cpp.

References WGraphicsEngine::getGraphicsEngine(), m_fragmentShader, m_geometryShader, m_reloadSignalConnection, m_versionPreprocessor, m_vertexShader, and reload().

+ Here is the call graph for this function:

◆ ~WGEShader()

WGEShader::~WGEShader ( )
virtual

Destructor.

Definition at line 73 of file WGEShader.cpp.

References m_reloadSignalConnection.

Member Function Documentation

◆ addPreprocessor()

void WGEShader::addPreprocessor ( WGEShaderPreprocessor::SPtr  preproc)

Adds the specified preprocessor to this shader.

The preprocessor is able to force shader reloads.

Parameters
preprocthe preprocessor to add.

Definition at line 359 of file WGEShader.cpp.

References WSharedObject< T >::getWriteTicket(), m_preprocessors, and reload().

Referenced by setDefine().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ apply()

void WGEShader::apply ( osg::ref_ptr< osg::Node >  node)
virtual

Apply this shader to the specified node.

Use this method to ensure, that reload events can be handled properly during the update cycle.

Parameters
nodethe node where the program should be registered to.

Definition at line 79 of file WGEShader.cpp.

References m_deactivated, m_reload, and m_shaderLoaded.

◆ applyDirect()

void WGEShader::applyDirect ( osg::State &  state)
virtual

If enabled, activate our program in the GL pipeline, performing any rebuild operations that might be pending.

In addition to the standard OSG functionality, it also loads/reloads the shader source from file.

Parameters
statethe state to apply the shader program to.

Definition at line 92 of file WGEShader.cpp.

References updatePrograms().

+ Here is the call graph for this function:

◆ clearPreprocessors()

void WGEShader::clearPreprocessors ( )

Removes all preprocessors.

Be careful with this one since it removes the WGESHaderVersionPreprocessor too, which is mandatory.

Definition at line 384 of file WGEShader.cpp.

References WSharedObject< T >::getWriteTicket(), m_preprocessors, and reload().

+ Here is the call graph for this function:

◆ deactivate()

void WGEShader::deactivate ( osg::ref_ptr< osg::Node >  node)
virtual

Removes the shader from the specified node.

Parameters
nodethe node where the program is registered to.

Definition at line 98 of file WGEShader.cpp.

References m_deactivated, and m_shaderLoaded.

◆ processShader()

std::string WGEShader::processShader ( const std::string  filename,
bool  optional = false 
)
protected

This method searches and processes all includes in the shader source.

The filenames in the include statement are assumed to be relative to this shader's path. It additionally applies preprocessors.

See also
processShaderRecursive
Parameters
filenamethe filename of the shader to process.
optionaldenotes whether a "file not found" is critical or not
Returns
the processed source.

Definition at line 334 of file WGEShader.cpp.

References WSharedObject< T >::getReadTicket(), m_preprocessors, m_versionPreprocessor, and processShaderRecursive().

Referenced by reloadShader().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ processShaderRecursive()

std::string WGEShader::processShaderRecursive ( const std::string  filename,
bool  optional = false,
int  level = 0 
)
protected

This method searches and processes all includes in the shader source.

The filenames in the include statement are assumed to be relative to this shader's path. It simply unrolls the code.

Parameters
filenamethe filename of the shader to process.
optionaldenotes whether a "file not found" is critical or not
levelthe inclusion level. This is used to avoid cycles.
Returns
the processed source.

Definition at line 206 of file WGEShader.cpp.

References WLogger::addLogMessage(), WLogger::getLogger(), WPathHelper::getShaderPath(), and m_shaderPath.

Referenced by processShader().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ reload()

void WGEShader::reload ( )
virtual

Initiate a reload of the shader during the next update cycle.

Definition at line 117 of file WGEShader.cpp.

References m_reload.

Referenced by addPreprocessor(), clearPreprocessors(), removePreprocessor(), and WGEShader().

+ Here is the caller graph for this function:

◆ reloadShader()

void WGEShader::reloadShader ( )
protected

This completely reloads the shader file and processes it.

It also resets m_reload to false.

Definition at line 122 of file WGEShader.cpp.

References WLogger::addLogMessage(), WLogger::getLogger(), m_fragmentShader, m_geometryShader, m_name, m_reload, m_shaderLoaded, m_vertexShader, and processShader().

Referenced by updatePrograms().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ removePreprocessor()

void WGEShader::removePreprocessor ( WGEShaderPreprocessor::SPtr  preproc)

Removes the specified preprocessor.

Changes inside the preprocessor won't cause any updates anymore.

Parameters
preprocthe preprocessor to remove. If not exists: nothing is done.

Definition at line 372 of file WGEShader.cpp.

References WSharedObject< T >::getWriteTicket(), m_preprocessors, and reload().

+ Here is the call graph for this function:

◆ setDefine() [1/2]

WGEShaderDefineSwitch::SPtr WGEShader::setDefine ( std::string  key)

Sets a define which is include into the shader source code.

This allows the preprocessor to turn on/off several parts of your code. In GLSL defines are a better choice when compared with a lot of branches (if-statements).

Parameters
keyThe name of the define
Returns
the switch allowing to control the define

Definition at line 398 of file WGEShader.cpp.

◆ setDefine() [2/2]

template<typename T >
WGEShaderDefine< T >::SPtr WGEShader::setDefine ( std::string  key,
value 
)

Sets a define which is include into the shader source code.

This allows the preprocessor to turn on/off several parts of your code. In GLSL defines are a better choice when compared with a lot of branches (if-statements).

Parameters
keyThe name of the define
valueThe value of the define. If this is not specified, the define can be used as simple ifdef switch.
Returns
the define object allowing later control

Definition at line 277 of file WGEShader.h.

References addPreprocessor(), WGEShaderDefine< ValueType >::getName(), WSharedObject< T >::getReadTicket(), m_preprocessors, and WGEShaderDefine< ValueType >::setValue().

+ Here is the call graph for this function:

◆ updatePrograms()

void WGEShader::updatePrograms ( )
protected

Handles all state changes in m_reload and m_deactivated.

It ensure that the shader programs are bound properly or deactivated.

Definition at line 177 of file WGEShader.cpp.

References m_deactivated, m_fragmentShader, m_geometryShader, m_reload, m_vertexShader, and reloadShader().

Referenced by applyDirect().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ m_deactivated

bool WGEShader::m_deactivated
protected

Flag denoting whether a shader should be deactivated.

Definition at line 205 of file WGEShader.h.

Referenced by apply(), deactivate(), and updatePrograms().

◆ m_fragmentShader

osg::ref_ptr< osg::Shader > WGEShader::m_fragmentShader
protected

the fragment shader object

Definition at line 235 of file WGEShader.h.

Referenced by reloadShader(), updatePrograms(), and WGEShader().

◆ m_geometryShader

osg::ref_ptr< osg::Shader > WGEShader::m_geometryShader
protected

the geometry shader object

Definition at line 240 of file WGEShader.h.

Referenced by reloadShader(), updatePrograms(), and WGEShader().

◆ m_name

std::string WGEShader::m_name
protected

The name of the shader.

It is used to construct the actual filename to load.

Definition at line 190 of file WGEShader.h.

Referenced by reloadShader().

◆ m_preprocessors

PreprocessorsList WGEShader::m_preprocessors
protected

List of all pre-processing that need to be applied to this shader instance.

Definition at line 220 of file WGEShader.h.

Referenced by addPreprocessor(), clearPreprocessors(), processShader(), removePreprocessor(), and setDefine().

◆ m_reload

bool WGEShader::m_reload
protected

Flag denoting whether a shader should be reloaded.

Definition at line 195 of file WGEShader.h.

Referenced by apply(), reload(), reloadShader(), and updatePrograms().

◆ m_reloadSignalConnection

boost::signals2::connection WGEShader::m_reloadSignalConnection
protected

Connection object to the reload signal from WGraphbicsEngine.

Definition at line 210 of file WGEShader.h.

Referenced by WGEShader(), and ~WGEShader().

◆ m_shaderLoaded

bool WGEShader::m_shaderLoaded
protected

True if the shaders have been loaded successfully previously.

Definition at line 200 of file WGEShader.h.

Referenced by apply(), deactivate(), and reloadShader().

◆ m_shaderPath

boost::filesystem::path WGEShader::m_shaderPath
protected

String that stores the location of all shader files.

Definition at line 185 of file WGEShader.h.

Referenced by processShaderRecursive().

◆ m_versionPreprocessor

WGEShaderPreprocessor::SPtr WGEShader::m_versionPreprocessor
protected

This preprocessor needs to be run LAST.

It handles version-statements in GLSL.

Definition at line 225 of file WGEShader.h.

Referenced by processShader(), and WGEShader().

◆ m_vertexShader

osg::ref_ptr< osg::Shader > WGEShader::m_vertexShader
protected

the vertex shader object

Definition at line 230 of file WGEShader.h.

Referenced by reloadShader(), updatePrograms(), and WGEShader().


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