OpenSceneGraph/examples/osgvirtualprogram/VirtualProgram.cpp

185 lines
6.2 KiB
C++
Raw Normal View History

2009-05-29 16:24:52 +08:00
////////////////////////////////////////////////////////////////////////////////
#include<osg/Shader>
#include<osg/Program>
#include<osg/State>
#include<osg/Notify>
#include<cassert>
////////////////////////////////////////////////////////////////////////////////
#include "VirtualProgram.h"
using namespace osg;
// If graphics board has program linking problems set MERGE_SHADERS to 1
2016-05-26 18:13:56 +08:00
// Merge shaders can be used to merge shaders strings into one shader.
2009-05-29 16:24:52 +08:00
#define MERGE_SHADERS 0
#define NOTIFICATION_MESSAGES 0
namespace osgCandidate {
////////////////////////////////////////////////////////////////////////////////
2016-05-26 18:13:56 +08:00
VirtualProgram::VirtualProgram( unsigned int mask ) : _mask( mask )
2009-05-29 16:24:52 +08:00
{
}
////////////////////////////////////////////////////////////////////////////////
VirtualProgram::VirtualProgram
2016-05-26 18:13:56 +08:00
( const VirtualProgram& vp, const osg::CopyOp& copyop ):
osg::Program( vp, copyop ),
_shaderMap( vp._shaderMap ),
_mask( vp._mask )
2009-05-29 16:24:52 +08:00
{
}
////////////////////////////////////////////////////////////////////////////////
VirtualProgram::~VirtualProgram( void )
{
}
////////////////////////////////////////////////////////////////////////////////
osg::Shader * VirtualProgram::getShader
( const std::string & shaderSemantic, osg::Shader::Type type )
{
ShaderMap::key_type key( shaderSemantic, type );
return _shaderMap[ key ].get();
}
////////////////////////////////////////////////////////////////////////////////
osg::Shader * VirtualProgram::setShader
( const std::string & shaderSemantic, osg::Shader * shader )
{
2016-05-26 18:13:56 +08:00
if( shader->getType() == osg::Shader::UNDEFINED )
2009-05-29 16:24:52 +08:00
return NULL;
ShaderMap::key_type key( shaderSemantic, shader->getType() );
ref_ptr< osg::Shader > shaderNew = shader;
ref_ptr< osg::Shader > & shaderCurrent = _shaderMap[ key ];
2016-05-26 18:13:56 +08:00
#if 0 // Good for debugging of shader linking problems.
// Don't do it - User could use the name for its own purposes
2009-05-29 16:24:52 +08:00
shaderNew->setName( shaderSemantic );
#endif
if( shaderCurrent != shaderNew ) {
#if 0
if( shaderCurrent.valid() )
Program::removeShader( shaderCurrent.get() );
if( shaderNew.valid() )
Program::addShader( shaderNew.get() );
#endif
shaderCurrent = shaderNew;
}
return shaderCurrent.get();
2009-05-29 16:24:52 +08:00
}
////////////////////////////////////////////////////////////////////////////////
void VirtualProgram::apply( osg::State & state ) const
{
if( _shaderMap.empty() ) // Virtual Program works as normal Program
return Program::apply( state );
State::AttributeVec *av = &state.getAttributeVec(this);
#if NOTIFICATION_MESSAGES
std::ostream &os = osg::notify( osg::NOTICE );
os << "VirtualProgram cumulate Begin" << std::endl;
#endif
ShaderMap shaderMap;
2016-05-26 18:13:56 +08:00
for( State::AttributeVec::iterator ai = av->begin(); ai != av->end(); ++ai )
2009-05-29 16:24:52 +08:00
{
2016-05-26 18:13:56 +08:00
const osg::StateAttribute * sa = ai->first;
2009-05-29 16:24:52 +08:00
const VirtualProgram * vp = dynamic_cast< const VirtualProgram *>( sa );
if( vp && ( vp->_mask & _mask ) ) {
#if NOTIFICATION_MESSAGES
if( vp->getName().empty() )
os << "VirtualProgram cumulate [ Unnamed VP ] apply" << std::endl;
2016-05-26 18:13:56 +08:00
else
2009-05-29 16:24:52 +08:00
os << "VirtualProgram cumulate ["<< vp->getName() << "] apply" << std::endl;
#endif
for( ShaderMap::const_iterator i = vp->_shaderMap.begin();
i != vp->_shaderMap.end(); ++i )
{
shaderMap[ i->first ] = i->second;
}
} else {
#if NOTIFICATION_MESSAGES
os << "VirtualProgram cumulate ( not VP or mask not match ) ignored" << std::endl;
#endif
continue; // ignore osg::Programs
}
}
for( ShaderMap::const_iterator i = this->_shaderMap.begin();
i != this->_shaderMap.end(); ++i )
shaderMap[ i->first ] = i->second;
#if NOTIFICATION_MESSAGES
os << "VirtualProgram cumulate End" << std::endl;
#endif
if( shaderMap.size() ) {
ShaderList sl;
for( ShaderMap::iterator i = shaderMap.begin(); i != shaderMap.end(); ++i )
sl.push_back( i->second );
osg::ref_ptr< osg::Program > & program = _programMap[ sl ];
if( !program.valid() ) {
program = new osg::Program;
#if !MERGE_SHADERS
for( ShaderList::iterator i = sl.begin(); i != sl.end(); ++i )
program->addShader( i->get() );
#else
std::string strFragment;
std::string strVertex;
std::string strGeometry;
2016-05-26 18:13:56 +08:00
2009-05-29 16:24:52 +08:00
for( ShaderList::iterator i = sl.begin(); i != sl.end(); ++i )
{
if( i->get()->getType() == osg::Shader::FRAGMENT )
strFragment += i->get()->getShaderSource();
else if ( i->get()->getType() == osg::Shader::VERTEX )
strVertex += i->get()->getShaderSource();
else if ( i->get()->getType() == osg::Shader::GEOMETRY )
strGeometry += i->get()->getShaderSource();
}
if( strFragment.length() > 0 ) {
program->addShader( new osg::Shader( osg::Shader::FRAGMENT, strFragment ) );
#if NOTIFICATION_MESSAGES
os << "====VirtualProgram merged Fragment Shader:" << std::endl << strFragment << "====" << std::endl;
#endif
}
if( strVertex.length() > 0 ) {
program->addShader( new osg::Shader( osg::Shader::VERTEX, strVertex ) );
#if NOTIFICATION_MESSAGES
os << "VirtualProgram merged Vertex Shader:" << std::endl << strVertex << "====" << std::endl;
#endif
}
if( strGeometry.length() > 0 ) {
program->addShader( new osg::Shader( osg::Shader::GEOMETRY, strGeometry ) );
#if NOTIFICATION_MESSAGES
os << "VirtualProgram merged Geometry Shader:" << std::endl << strGeometry << "====" << std::endl;
#endif
}
#endif
}
state.applyAttribute( program.get() );
2009-05-29 16:24:52 +08:00
} else {
Program::apply( state );
}
#if NOTIFICATION_MESSAGES
os << "VirtualProgram Apply" << std::endl;
#endif
}
////////////////////////////////////////////////////////////////////////////////
} // namespace osgExt