Added compile/release and resize of GL objects to ShaderAttribute.

Removed the StateAttribute::compose() method.

Fixed the default type value in ShaderAttribute
This commit is contained in:
Robert Osfield 2010-07-06 12:19:26 +00:00
parent 74ae526bb5
commit 46b221a832
7 changed files with 76 additions and 16 deletions

View File

@ -31,6 +31,8 @@ osg::Node* createSceneGraph(osg::ArgumentParser& arguments)
osg::Vec3d position(0.0,0.0,0.0);
osg::ShaderAttribute* sa1 = NULL;
{
osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform;
pat->setPosition(position);
@ -40,6 +42,8 @@ osg::Node* createSceneGraph(osg::ArgumentParser& arguments)
osg::StateSet* stateset = pat->getOrCreateStateSet();
osg::ShaderAttribute* sa = new osg::ShaderAttribute;
//sa->setType(osg::StateAttribute::Type(10000));
sa1 = sa;
stateset->setAttribute(sa);
{
@ -72,7 +76,27 @@ osg::Node* createSceneGraph(osg::ArgumentParser& arguments)
group->addChild(pat);
}
#if 1
{
osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform;
pat->setPosition(position);
pat->addChild(node);
position.x() += spacing;
osg::StateSet* stateset = pat->getOrCreateStateSet();
osg::ShaderAttribute* sa = new osg::ShaderAttribute;
//sa->setType(osg::StateAttribute::Type(10000));
stateset->setAttribute(sa);
// reuse the same ShaderComponent as the first branch
sa->setShaderComponent(sa1->getShaderComponent());
sa->addUniform(new osg::Uniform("myColour",osg::Vec4(1.0f,1.0f,0.0f,1.0f)));
group->addChild(pat);
}
#endif
return group;
}

View File

@ -291,6 +291,10 @@ class OSG_EXPORT ShaderComponent : public osg::Object
unsigned int getNumShaders() const { return _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;

View File

@ -54,8 +54,6 @@ class OSG_EXPORT ShaderAttribute : public StateAttribute
virtual void apply(State& state) const;
virtual void compose(ShaderComposer& composer) const;
virtual void compileGLObjects(State& state) const;
virtual void resizeGLObjectBuffers(unsigned int maxSize);

View File

@ -322,9 +322,6 @@ class OSG_EXPORT StateAttribute : public Object
*/
virtual void apply(State&) const {}
/* compose associated shaders via the ShaderComposer. */
virtual void compose(ShaderComposer& composer) const {}
/** Default to nothing to compile - all state is applied immediately. */
virtual void compileGLObjects(State&) const {}

View File

@ -66,6 +66,38 @@ void ShaderComponent::removeShader(unsigned int i)
_shaders.erase(_shaders.begin()+i);
}
void ShaderComponent::compileGLObjects(State& state) const
{
for(Shaders::const_iterator itr = _shaders.begin();
itr != _shaders.end();
++itr)
{
(*itr)->compileShader(state);
}
}
void ShaderComponent::resizeGLObjectBuffers(unsigned int maxSize)
{
for(Shaders::const_iterator itr = _shaders.begin();
itr != _shaders.end();
++itr)
{
(*itr)->resizeGLObjectBuffers(maxSize);
}
}
void ShaderComponent::releaseGLObjects(State* state) const
{
for(Shaders::const_iterator itr = _shaders.begin();
itr != _shaders.end();
++itr)
{
(*itr)->releaseGLObjects(state);
}
}
///////////////////////////////////////////////////////////////////////////////////
//
// ShaderBinary

View File

@ -19,7 +19,8 @@
using namespace osg;
ShaderAttribute::ShaderAttribute()
ShaderAttribute::ShaderAttribute():
_type(osg::StateAttribute::Type(-1))
{
_shaderComponent = new osg::ShaderComponent;
}
@ -88,22 +89,17 @@ void ShaderAttribute::apply(State& state) const
}
}
void ShaderAttribute::compose(ShaderComposer& composer) const
void ShaderAttribute::compileGLObjects(State& state) const
{
OSG_NOTICE<<"ShaderAttribute::compose(..)"<<std::endl;
}
void ShaderAttribute::compileGLObjects(State&) const
{
OSG_NOTICE<<"ShaderAttribute::compileGLObjects(..)"<<std::endl;
if (_shaderComponent.valid()) _shaderComponent->compileGLObjects(state);
}
void ShaderAttribute::resizeGLObjectBuffers(unsigned int maxSize)
{
OSG_NOTICE<<"ShaderAttribute::resizeGLObjectBuffers(..)"<<std::endl;
if (_shaderComponent.valid()) _shaderComponent->resizeGLObjectBuffers(maxSize);
}
void ShaderAttribute::releaseGLObjects(State* state) const
{
OSG_NOTICE<<"ShaderAttribute::releaseGLObjects(..)"<<std::endl;
if (_shaderComponent.valid()) _shaderComponent->releaseGLObjects(state);
}

View File

@ -214,6 +214,7 @@ void State::reset()
AttributeStack& as = aitr->second;
as.attributeVec.clear();
as.last_applied_attribute = NULL;
as.last_applied_shadercomponent = NULL;
as.changed = true;
}
@ -239,6 +240,7 @@ void State::reset()
AttributeStack& as = aitr->second;
as.attributeVec.clear();
as.last_applied_attribute = NULL;
as.last_applied_shadercomponent = NULL;
as.changed = true;
}
}
@ -262,6 +264,9 @@ void State::reset()
_currentClientActiveTextureUnit = 0;
#endif
_shaderCompositionDirty = true;
_currentShaderCompositionUniformList.clear();
_lastAppliedProgramObject = 0;
for(AppliedProgramObjectSet::iterator apitr=_appliedProgramObjectSet.begin();
@ -596,10 +601,14 @@ void State::applyShaderComposition()
// build lits of current ShaderComponents
ShaderComponents shaderComponents;
// OSG_NOTICE<<"State::applyShaderComposition() : _attributeMap.size()=="<<_attributeMap.size()<<std::endl;
for(AttributeMap::iterator itr = _attributeMap.begin();
itr != _attributeMap.end();
++itr)
{
// OSG_NOTICE<<" itr->first="<<itr->first.first<<", "<<itr->first.second<<std::endl;
AttributeStack& as = itr->second;
if (as.last_applied_shadercomponent)
{