From 46b221a8321d4d7e0229cdc7a9a3cd1ca09f1c49 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 6 Jul 2010 12:19:26 +0000 Subject: [PATCH] Added compile/release and resize of GL objects to ShaderAttribute. Removed the StateAttribute::compose() method. Fixed the default type value in ShaderAttribute --- .../osgshadercomposition.cpp | 24 ++++++++++++++ include/osg/Shader | 4 +++ include/osg/ShaderAttribute | 2 -- include/osg/StateAttribute | 3 -- src/osg/Shader.cpp | 32 +++++++++++++++++++ src/osg/ShaderAttribute.cpp | 16 ++++------ src/osg/State.cpp | 11 ++++++- 7 files changed, 76 insertions(+), 16 deletions(-) diff --git a/examples/osgshadercomposition/osgshadercomposition.cpp b/examples/osgshadercomposition/osgshadercomposition.cpp index 47026662a..3e517fb69 100644 --- a/examples/osgshadercomposition/osgshadercomposition.cpp +++ b/examples/osgshadercomposition/osgshadercomposition.cpp @@ -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; } diff --git a/include/osg/Shader b/include/osg/Shader index 1d5eb8a0a..b84bc00b4 100644 --- a/include/osg/Shader +++ b/include/osg/Shader @@ -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 > Shaders; diff --git a/include/osg/ShaderAttribute b/include/osg/ShaderAttribute index a331acefc..f29677ddb 100644 --- a/include/osg/ShaderAttribute +++ b/include/osg/ShaderAttribute @@ -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); diff --git a/include/osg/StateAttribute b/include/osg/StateAttribute index f992e2267..97136c3e4 100644 --- a/include/osg/StateAttribute +++ b/include/osg/StateAttribute @@ -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 {} diff --git a/src/osg/Shader.cpp b/src/osg/Shader.cpp index 136401aee..de828d497 100644 --- a/src/osg/Shader.cpp +++ b/src/osg/Shader.cpp @@ -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 diff --git a/src/osg/ShaderAttribute.cpp b/src/osg/ShaderAttribute.cpp index da7847da8..0a60299d2 100644 --- a/src/osg/ShaderAttribute.cpp +++ b/src/osg/ShaderAttribute.cpp @@ -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(..)"<compileGLObjects(state); } void ShaderAttribute::resizeGLObjectBuffers(unsigned int maxSize) { - OSG_NOTICE<<"ShaderAttribute::resizeGLObjectBuffers(..)"<resizeGLObjectBuffers(maxSize); } void ShaderAttribute::releaseGLObjects(State* state) const { - OSG_NOTICE<<"ShaderAttribute::releaseGLObjects(..)"<releaseGLObjects(state); } diff --git a/src/osg/State.cpp b/src/osg/State.cpp index 5157cf016..9475e2f8b 100644 --- a/src/osg/State.cpp +++ b/src/osg/State.cpp @@ -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; } } @@ -261,7 +263,10 @@ void State::reset() _currentActiveTextureUnit = 0; _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()<first="<first.first<<", "<first.second<second; if (as.last_applied_shadercomponent) {