Shader
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
* Copyright (C) 2003-2005 3Dlabs Inc. Ltd.
* Copyright (C) 2004-2005 Nathan Cournia
* Copyright (C) 2008 Zebra Imaging
* Copyright (C) 2010 VIRES Simulationstechnologie GmbH
*
* This application is open source and may be redistributed and/or modified
* freely and without restriction, both in commercial and non commercial
* applications, as long as this copyright notice is maintained.
*
* This application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/* file: include/osg/Shader
* author: Mike Weiblen 2008-01-02
* Holger Helmich 2010-10-21
*/
#ifndef OSG_SHADER
#define OSG_SHADER 1
#include <osg/GLExtensions>
#include <osg/Object>
#include <osg/buffered_value>
#include <set>
#include <map>
namespace osg {
class Program;
// set of shader define strings that the shader is dependent upon.
typedef std::set<std::string> ShaderDefines;
class OSG_EXPORT ShaderBinary : public osg::Object
{
public:
ShaderBinary();
ShaderBinary(const ShaderBinary& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osg, ShaderBinary);
void allocate(unsigned int size);
void assign(unsigned int size, const unsigned char* data);
unsigned int getSize() const { return static_cast<unsigned int>(_data.size()); }
unsigned char* getData() { return _data.empty() ? 0 : &(_data.front()); }
const unsigned char* getData() const { return _data.empty() ? 0 : &(_data.front()); }
static ShaderBinary* readShaderBinaryFile(const std::string& fileName);
protected:
typedef std::vector<unsigned char> Data;
Data _data;
};
class OSG_EXPORT Shader : public osg::Object
{
public:
enum Type {
VERTEX = GL_VERTEX_SHADER,
TESSCONTROL = GL_TESS_CONTROL_SHADER,
TESSEVALUATION = GL_TESS_EVALUATION_SHADER,
GEOMETRY = GL_GEOMETRY_SHADER,
FRAGMENT = GL_FRAGMENT_SHADER,
COMPUTE = GL_COMPUTE_SHADER,
UNDEFINED = -1
};
Shader(Type type = UNDEFINED);
Shader(Type type, const std::string& source );
Shader(Type type, ShaderBinary* shaderBinary );
Shader(const Shader& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);
META_Object(osg, Shader);
int compare(const Shader& rhs) const;
bool setType(Type t);
inline Type getType() const { return _type; }
const char* getTypename() const;
inline void setFileName(const std::string& fileName) { _shaderFileName = fileName; }
inline const std::string& getFileName() const { return _shaderFileName; }
void setShaderSource(const std::string& sourceText);
inline const std::string& getShaderSource() const { return _shaderSource; }
enum ShaderDefinesMode
{
USE_SHADER_PRAGMA,
USE_MANUAL_SETTINGS
};
void setShaderDefinesMode(ShaderDefinesMode sdm) { _shaderDefinesMode = sdm; }
ShaderDefinesMode getShaderDefinesMode() const { return _shaderDefinesMode; }
void setShaderDefines(const ShaderDefines& shaderDefs) { _shaderDefines = shaderDefs; }
ShaderDefines& getShaderDefines() { return _shaderDefines; }
const ShaderDefines& getShaderDefines() const { return _shaderDefines; }
void setShaderRequirements(const ShaderDefines& shaderDefs) { _shaderRequirements = shaderDefs; }
ShaderDefines& getShaderRequirements() { return _shaderRequirements; }
const ShaderDefines& getShaderRequirements() const { return _shaderRequirements; }
void setShaderBinary(ShaderBinary* shaderBinary) { _shaderBinary = shaderBinary; }
ShaderBinary* getShaderBinary() { return _shaderBinary.get(); }
const ShaderBinary* getShaderBinary() const { return _shaderBinary.get(); }
#ifdef OSG_USE_DEPRECATED_API
static Shader* readShaderFile( Type type, const std::string& fileName );
bool loadShaderSourceFromFile( const std::string& fileName );
#endif
typedef std::multimap<float, std::string> CodeInjectionMap;
void addCodeInjection(float position, const std::string& code) { _codeInjectionMap.insert(CodeInjectionMap::value_type(position, code)); }
CodeInjectionMap& getCodeInjectionMap() { return _codeInjectionMap; }
const CodeInjectionMap& getCodeInjectionMap() const { return _codeInjectionMap; }
virtual void resizeGLObjectBuffers(unsigned int maxSize);
void releaseGLObjects(osg::State* state=0) const;
void dirtyShader();
void compileShader(osg::State& state) const;
static Shader::Type getTypeId( const std::string& tname );
public:
class OSG_EXPORT PerContextShader : public osg::Referenced
{
public:
PerContextShader(const Shader* shader, unsigned int contextID);
void setDefineString(const std::string& defStr) { _defineStr = defStr; }
const std::string& getDefineString() const { return _defineStr; }
GLuint getHandle() const {return _glShaderHandle;}
void requestCompile();
void compileShader(osg::State& state);
bool needsCompile() const {return _needsCompile;}
bool isCompiled() const {return _isCompiled;}
bool getInfoLog( std::string& infoLog ) const;
void attachShader(GLuint program) const;
void detachShader(GLuint program) const;
protected: /*methods*/
~PerContextShader();
protected: /*data*/
const Shader* _shader;
osg::ref_ptr<osg::GLExtensions> _extensions;
GLuint _glShaderHandle;
std::string _defineStr;
bool _needsCompile;
bool _isCompiled;
const unsigned int _contextID;
private:
PerContextShader(); // disallowed
PerContextShader(const PerContextShader&); // disallowed
PerContextShader& operator=(const PerContextShader&); // disallowed
};
struct OSG_EXPORT ShaderObjects : public osg::Referenced
{
typedef std::vector< osg::ref_ptr<PerContextShader> > PerContextShaders;
ShaderObjects(const Shader* shader, unsigned int contextID);
unsigned int _contextID;
const Shader* _shader;
mutable PerContextShaders _perContextShaders;
PerContextShader* getPCS(const std::string& defineStr) const;
PerContextShader* createPerContextShader(const std::string& defineStr);
void requestCompile();
};
PerContextShader* getPCS(osg::State& state) const;
protected: /*methods*/
virtual ~Shader();
friend class osg::Program;
bool addProgramRef( osg::Program* program );
bool removeProgramRef( osg::Program* program );
void _computeShaderDefines();
void _parseShaderDefines(const std::string& str, ShaderDefines& defines);
protected: /*data*/
Type _type;
std::string _shaderFileName;
std::string _shaderSource;
osg::ref_ptr<ShaderBinary> _shaderBinary;
CodeInjectionMap _codeInjectionMap;
// ShaderDefines variables
ShaderDefinesMode _shaderDefinesMode;
ShaderDefines _shaderDefines;
ShaderDefines _shaderRequirements;
typedef std::set< osg::Program* > ProgramSet;
ProgramSet _programSet;
OpenThreads::Mutex _programSetMutex;
mutable osg::buffered_value< osg::ref_ptr<ShaderObjects> > _pcsList;
private:
Shader& operator=(const Shader&); // disallowed
};
class OSG_EXPORT ShaderComponent : public osg::Object
{
public:
ShaderComponent();
ShaderComponent(const ShaderComponent& sc,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
META_Object(osg, ShaderComponent);
unsigned int addShader(osg::Shader* shader);
void removeShader(unsigned int i);
osg::Shader* getShader(unsigned int i) { return _shaders[i].get(); }
const osg::Shader* getShader(unsigned int i) const { return _shaders[i].get(); }
unsigned int getNumShaders() const { return static_cast<unsigned int>(_shaders.size()); }
virtual void compileGLObjects(State& state) const;
virtual void resizeGLObjectBuffers(unsigned int maxSize);
virtual void releaseGLObjects(State* state=0) const;
protected:
typedef std::vector< osg::ref_ptr<osg::Shader> > Shaders;
Shaders _shaders;
};
}
#endif
GL_FRAGMENT_SHADER
#define GL_FRAGMENT_SHADER
Definition GLDefines:92
GL_GEOMETRY_SHADER
#define GL_GEOMETRY_SHADER
Definition GLDefines:198
GL_TESS_CONTROL_SHADER
#define GL_TESS_CONTROL_SHADER
Definition GLDefines:242
GL_COMPUTE_SHADER
#define GL_COMPUTE_SHADER
Definition GLDefines:477
GL_TESS_EVALUATION_SHADER
#define GL_TESS_EVALUATION_SHADER
Definition GLDefines:241
GL_VERTEX_SHADER
#define GL_VERTEX_SHADER
Definition GLDefines:93
GLExtensions
Object
buffered_value
OpenThreads::Mutex
This class provides an object-oriented thread mutex interface.
Definition Mutex:31
osg::CopyOp
Definition CopyOp:41
osg::CopyOp::SHALLOW_COPY
@ SHALLOW_COPY
Definition CopyOp:47
osg::Object
Definition Object:61
osg::Program
Definition osg/Program:52
osg::Referenced
Definition Referenced:44
osg::ShaderBinary
Definition Shader:42
osg::ShaderBinary::_data
Data _data
Definition Shader:74
osg::ShaderBinary::getData
unsigned char * getData()
Definition Shader:62
osg::ShaderBinary::ShaderBinary
ShaderBinary(const ShaderBinary &rhs, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
osg::ShaderBinary::META_Object
META_Object(osg, ShaderBinary)
osg::ShaderBinary::ShaderBinary
ShaderBinary()
osg::ShaderBinary::allocate
void allocate(unsigned int size)
osg::ShaderBinary::getData
const unsigned char * getData() const
Definition Shader:65
osg::ShaderBinary::readShaderBinaryFile
static ShaderBinary * readShaderBinaryFile(const std::string &fileName)
osg::ShaderBinary::getSize
unsigned int getSize() const
Definition Shader:59
osg::ShaderBinary::Data
std::vector< unsigned char > Data
Definition Shader:73
osg::ShaderBinary::assign
void assign(unsigned int size, const unsigned char *data)
osg::ShaderComponent::getNumShaders
unsigned int getNumShaders() const
Definition Shader:332
osg::ShaderComponent::Shaders
std::vector< osg::ref_ptr< osg::Shader > > Shaders
Definition Shader:340
osg::ShaderComponent::releaseGLObjects
virtual void releaseGLObjects(State *state=0) const
osg::ShaderComponent::compileGLObjects
virtual void compileGLObjects(State &state) const
osg::ShaderComponent::ShaderComponent
ShaderComponent()
osg::ShaderComponent::getShader
const osg::Shader * getShader(unsigned int i) const
Definition Shader:330
osg::ShaderComponent::ShaderComponent
ShaderComponent(const ShaderComponent &sc, const CopyOp &copyop=CopyOp::SHALLOW_COPY)
osg::ShaderComponent::META_Object
META_Object(osg, ShaderComponent)
osg::ShaderComponent::_shaders
Shaders _shaders
Definition Shader:341
osg::ShaderComponent::getShader
osg::Shader * getShader(unsigned int i)
Definition Shader:329
osg::ShaderComponent::removeShader
void removeShader(unsigned int i)
osg::ShaderComponent::resizeGLObjectBuffers
virtual void resizeGLObjectBuffers(unsigned int maxSize)
osg::ShaderComponent::addShader
unsigned int addShader(osg::Shader *shader)
osg::Shader::PerContextShader
Definition Shader:212
osg::Shader::PerContextShader::requestCompile
void requestCompile()
osg::Shader::PerContextShader::~PerContextShader
~PerContextShader()
osg::Shader::PerContextShader::attachShader
void attachShader(GLuint program) const
osg::Shader::PerContextShader::needsCompile
bool needsCompile() const
Definition Shader:223
osg::Shader::PerContextShader::_defineStr
std::string _defineStr
Definition Shader:247
osg::Shader::PerContextShader::_needsCompile
bool _needsCompile
Definition Shader:250
osg::Shader::PerContextShader::_shader
const Shader * _shader
Definition Shader:238
osg::Shader::PerContextShader::getInfoLog
bool getInfoLog(std::string &infoLog) const
osg::Shader::PerContextShader::setDefineString
void setDefineString(const std::string &defStr)
Definition Shader:216
osg::Shader::PerContextShader::_glShaderHandle
GLuint _glShaderHandle
Definition Shader:244
osg::Shader::PerContextShader::isCompiled
bool isCompiled() const
Definition Shader:224
osg::Shader::PerContextShader::compileShader
void compileShader(osg::State &state)
osg::Shader::PerContextShader::getDefineString
const std::string & getDefineString() const
Definition Shader:217
osg::Shader::PerContextShader::_contextID
const unsigned int _contextID
Definition Shader:255
osg::Shader::PerContextShader::PerContextShader
PerContextShader(const Shader *shader, unsigned int contextID)
osg::Shader::PerContextShader::getHandle
GLuint getHandle() const
Definition Shader:219
osg::Shader::PerContextShader::_isCompiled
bool _isCompiled
Definition Shader:253
osg::Shader::PerContextShader::_extensions
osg::ref_ptr< osg::GLExtensions > _extensions
Definition Shader:241
osg::Shader::PerContextShader::detachShader
void detachShader(GLuint program) const
osg::Shader
Definition Shader:89
osg::Shader::_shaderRequirements
ShaderDefines _shaderRequirements
Definition Shader:304
osg::Shader::getFileName
const std::string & getFileName() const
Definition Shader:127
osg::Shader::_type
Type _type
Definition Shader:294
osg::Shader::Shader
Shader(const Shader &rhs, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
osg::Shader::_shaderFileName
std::string _shaderFileName
Definition Shader:295
osg::Shader::getShaderBinary
ShaderBinary * getShaderBinary()
Definition Shader:160
osg::Shader::Shader
Shader(Type type, ShaderBinary *shaderBinary)
osg::Shader::getShaderRequirements
const ShaderDefines & getShaderRequirements() const
Definition Shader:153
osg::Shader::Shader
Shader(Type type, const std::string &source)
osg::Shader::_parseShaderDefines
void _parseShaderDefines(const std::string &str, ShaderDefines &defines)
osg::Shader::_pcsList
osg::buffered_value< osg::ref_ptr< ShaderObjects > > _pcsList
Definition Shader:310
osg::Shader::setType
bool setType(Type t)
osg::Shader::ShaderDefinesMode
ShaderDefinesMode
Definition Shader:138
osg::Shader::USE_SHADER_PRAGMA
@ USE_SHADER_PRAGMA
Definition Shader:139
osg::Shader::USE_MANUAL_SETTINGS
@ USE_MANUAL_SETTINGS
Definition Shader:140
osg::Shader::META_Object
META_Object(osg, Shader)
osg::Shader::_shaderDefines
ShaderDefines _shaderDefines
Definition Shader:303
osg::Shader::getShaderBinary
const ShaderBinary * getShaderBinary() const
Definition Shader:163
osg::Shader::releaseGLObjects
void releaseGLObjects(osg::State *state=0) const
osg::Shader::getShaderDefines
ShaderDefines & getShaderDefines()
Definition Shader:148
osg::Shader::resizeGLObjectBuffers
virtual void resizeGLObjectBuffers(unsigned int maxSize)
osg::Shader::_shaderBinary
osg::ref_ptr< ShaderBinary > _shaderBinary
Definition Shader:297
osg::Shader::_shaderSource
std::string _shaderSource
Definition Shader:296
osg::Shader::_computeShaderDefines
void _computeShaderDefines()
osg::Shader::~Shader
virtual ~Shader()
osg::Shader::ProgramSet
std::set< osg::Program * > ProgramSet
Definition Shader:307
osg::Shader::getType
Type getType() const
Definition Shader:117
osg::Shader::getShaderDefinesMode
ShaderDefinesMode getShaderDefinesMode() const
Definition Shader:144
osg::Shader::setShaderBinary
void setShaderBinary(ShaderBinary *shaderBinary)
Definition Shader:157
osg::Shader::setFileName
void setFileName(const std::string &fileName)
Definition Shader:124
osg::Shader::getShaderDefines
const ShaderDefines & getShaderDefines() const
Definition Shader:149
osg::Shader::_programSet
ProgramSet _programSet
Definition Shader:308
osg::Shader::setShaderSource
void setShaderSource(const std::string &sourceText)
osg::Shader::addProgramRef
bool addProgramRef(osg::Program *program)
osg::Shader::setShaderDefines
void setShaderDefines(const ShaderDefines &shaderDefs)
Definition Shader:147
osg::Shader::getShaderRequirements
ShaderDefines & getShaderRequirements()
Definition Shader:152
osg::Shader::getPCS
PerContextShader * getPCS(osg::State &state) const
osg::Shader::getTypename
const char * getTypename() const
osg::Shader::Type
Type
Definition Shader:92
osg::Shader::FRAGMENT
@ FRAGMENT
Definition Shader:97
osg::Shader::GEOMETRY
@ GEOMETRY
Definition Shader:96
osg::Shader::UNDEFINED
@ UNDEFINED
Definition Shader:99
osg::Shader::TESSCONTROL
@ TESSCONTROL
Definition Shader:94
osg::Shader::COMPUTE
@ COMPUTE
Definition Shader:98
osg::Shader::TESSEVALUATION
@ TESSEVALUATION
Definition Shader:95
osg::Shader::VERTEX
@ VERTEX
Definition Shader:93
osg::Shader::setShaderDefinesMode
void setShaderDefinesMode(ShaderDefinesMode sdm)
Definition Shader:143
osg::Shader::_programSetMutex
OpenThreads::Mutex _programSetMutex
Definition Shader:309
osg::Shader::removeProgramRef
bool removeProgramRef(osg::Program *program)
osg::Shader::compare
int compare(const Shader &rhs) const
osg::Shader::Shader
Shader(Type type=UNDEFINED)
osg::Shader::getShaderSource
const std::string & getShaderSource() const
Definition Shader:134
osg::Shader::getCodeInjectionMap
CodeInjectionMap & getCodeInjectionMap()
Definition Shader:185
osg::Shader::setShaderRequirements
void setShaderRequirements(const ShaderDefines &shaderDefs)
Definition Shader:151
osg::Shader::addCodeInjection
void addCodeInjection(float position, const std::string &code)
Definition Shader:182
osg::Shader::compileShader
void compileShader(osg::State &state) const
osg::Shader::dirtyShader
void dirtyShader()
osg::Shader::getCodeInjectionMap
const CodeInjectionMap & getCodeInjectionMap() const
Definition Shader:188
osg::Shader::_codeInjectionMap
CodeInjectionMap _codeInjectionMap
Definition Shader:299
osg::Shader::getTypeId
static Shader::Type getTypeId(const std::string &tname)
osg::Shader::CodeInjectionMap
std::multimap< float, std::string > CodeInjectionMap
Definition Shader:174
osg::Shader::_shaderDefinesMode
ShaderDefinesMode _shaderDefinesMode
Definition Shader:302
osg::State
Definition State:80
osg::buffered_value
Definition buffered_value:27
osg::ref_ptr
Definition ref_ptr:32
osg
Definition AlphaFunc:19
osg::ShaderDefines
std::set< std::string > ShaderDefines
Definition Shader:37
OSG_EXPORT
#define OSG_EXPORT
Definition osg/Export:39
osg::Shader::ShaderObjects::ShaderObjects
ShaderObjects(const Shader *shader, unsigned int contextID)
osg::Shader::ShaderObjects::_contextID
unsigned int _contextID
Definition Shader:270
osg::Shader::ShaderObjects::createPerContextShader
PerContextShader * createPerContextShader(const std::string &defineStr)
osg::Shader::ShaderObjects::getPCS
PerContextShader * getPCS(const std::string &defineStr) const
osg::Shader::ShaderObjects::_perContextShaders
PerContextShaders _perContextShaders
Definition Shader:272
osg::Shader::ShaderObjects::PerContextShaders
std::vector< osg::ref_ptr< PerContextShader > > PerContextShaders
Definition Shader:266
osg::Shader::ShaderObjects::requestCompile
void requestCompile()
osg::Shader::ShaderObjects::_shader
const Shader * _shader
Definition Shader:271