Changed the Optimizer::StateVisitor so that it can individually decide whether

to optimize away duplicate state with dynamic, static and unspecified DataVarience.  By default
the code now optimizes away duplicate state with either static and unspecied state, previously 
it was just handling static state.
This commit is contained in:
Robert Osfield 2008-06-29 12:22:50 +00:00
parent 8820d0bb1d
commit 5e0169f664
3 changed files with 39 additions and 10 deletions

View File

@ -432,8 +432,16 @@ class OSGUTIL_EXPORT Optimizer
public: public:
/// default to traversing all children. /// default to traversing all children.
StateVisitor(Optimizer* optimizer=0): StateVisitor(bool combineDynamicState,
BaseOptimizerVisitor(optimizer, SHARE_DUPLICATE_STATE) {} bool combineStaticState,
bool combineUnspecifiedState,
Optimizer* optimizer=0):
BaseOptimizerVisitor(optimizer, SHARE_DUPLICATE_STATE)
{
_optimize[osg::Object::DYNAMIC] = combineDynamicState;
_optimize[osg::Object::STATIC] = combineStaticState;
_optimize[osg::Object::UNSPECIFIED] = combineUnspecifiedState;
}
/** empty visitor, make it ready for next traversal.*/ /** empty visitor, make it ready for next traversal.*/
virtual void reset(); virtual void reset();
@ -447,10 +455,18 @@ class OSGUTIL_EXPORT Optimizer
protected: protected:
void addStateSet(osg::StateSet* stateset,osg::Object* obj); void addStateSet(osg::StateSet* stateset,osg::Object* obj);
inline bool optimize(osg::Object::DataVariance variance)
{
return _optimize[variance];
}
typedef std::set<osg::Object*> ObjectSet; typedef std::set<osg::Object*> ObjectSet;
typedef std::map<osg::StateSet*,ObjectSet> StateSetMap; typedef std::map<osg::StateSet*,ObjectSet> StateSetMap;
// note, one element for DYNAMIC, STATIC and UNSPECIFIED
bool _optimize[3];
StateSetMap _statesets; StateSetMap _statesets;
}; };

View File

@ -188,7 +188,11 @@ void Optimizer::optimize(osg::Node* node, unsigned int options)
{ {
osg::notify(osg::INFO)<<"Optimizer::optimize() doing SHARE_DUPLICATE_STATE"<<std::endl; osg::notify(osg::INFO)<<"Optimizer::optimize() doing SHARE_DUPLICATE_STATE"<<std::endl;
StateVisitor osv(this); bool combineDynamicState = false;
bool combineStaticState = true;
bool combineUnspecifiedState = true;
StateVisitor osv(combineDynamicState, combineStaticState, combineUnspecifiedState, this);
node->accept(osv); node->accept(osv);
osv.optimize(); osv.optimize();
} }
@ -203,7 +207,11 @@ void Optimizer::optimize(osg::Node* node, unsigned int options)
tav.optimize(); tav.optimize();
// now merge duplicate state, that may have been introduced by merge textures into texture atlas' // now merge duplicate state, that may have been introduced by merge textures into texture atlas'
StateVisitor osv(this); bool combineDynamicState = false;
bool combineStaticState = true;
bool combineUnspecifiedState = true;
StateVisitor osv(combineDynamicState, combineStaticState, combineUnspecifiedState, this);
node->accept(osv); node->accept(osv);
osv.optimize(); osv.optimize();
} }
@ -464,7 +472,7 @@ void Optimizer::StateVisitor::optimize()
aitr!=attributes.end(); aitr!=attributes.end();
++aitr) ++aitr)
{ {
if (aitr->second.first->getDataVariance()==osg::Object::STATIC) if (optimize(aitr->second.first->getDataVariance()))
{ {
attributeToStateSetMap[aitr->second.first.get()].insert(StateSetUnitPair(sitr->first,NON_TEXTURE_ATTRIBUTE)); attributeToStateSetMap[aitr->second.first.get()].insert(StateSetUnitPair(sitr->first,NON_TEXTURE_ATTRIBUTE));
} }
@ -479,7 +487,7 @@ void Optimizer::StateVisitor::optimize()
aitr!=attributes.end(); aitr!=attributes.end();
++aitr) ++aitr)
{ {
if (aitr->second.first->getDataVariance()==osg::Object::STATIC) if (optimize(aitr->second.first->getDataVariance()))
{ {
attributeToStateSetMap[aitr->second.first.get()].insert(StateSetUnitPair(sitr->first,unit)); attributeToStateSetMap[aitr->second.first.get()].insert(StateSetUnitPair(sitr->first,unit));
} }
@ -492,7 +500,7 @@ void Optimizer::StateVisitor::optimize()
uitr!=uniforms.end(); uitr!=uniforms.end();
++uitr) ++uitr)
{ {
if (uitr->second.first->getDataVariance()==osg::Object::STATIC) if (optimize(uitr->second.first->getDataVariance()))
{ {
uniformToStateSetMap[uitr->second.first.get()].insert(sitr->first); uniformToStateSetMap[uitr->second.first.get()].insert(sitr->first);
} }

View File

@ -646,9 +646,8 @@ END_REFLECTOR
BEGIN_OBJECT_REFLECTOR(osgUtil::Optimizer::StateVisitor) BEGIN_OBJECT_REFLECTOR(osgUtil::Optimizer::StateVisitor)
I_DeclaringFile("osgUtil/Optimizer"); I_DeclaringFile("osgUtil/Optimizer");
I_BaseType(osgUtil::BaseOptimizerVisitor); I_BaseType(osgUtil::BaseOptimizerVisitor);
I_ConstructorWithDefaults1(IN, osgUtil::Optimizer *, optimizer, 0, I_ConstructorWithDefaults4(IN, bool, combineDynamicState, , IN, bool, combineStaticState, , IN, bool, combineUnspecifiedState, , IN, osgUtil::Optimizer *, optimizer, 0,
Properties::NON_EXPLICIT, ____StateVisitor__bool__bool__bool__Optimizer_P1,
____StateVisitor__Optimizer_P1,
"default to traversing all children. ", "default to traversing all children. ",
""); "");
I_Method0(void, reset, I_Method0(void, reset,
@ -677,6 +676,12 @@ BEGIN_OBJECT_REFLECTOR(osgUtil::Optimizer::StateVisitor)
__void__addStateSet__osg_StateSet_P1__osg_Object_P1, __void__addStateSet__osg_StateSet_P1__osg_Object_P1,
"", "",
""); "");
I_ProtectedMethod1(bool, optimize, IN, osg::Object::DataVariance, variance,
Properties::NON_VIRTUAL,
Properties::NON_CONST,
__bool__optimize__osg_Object_DataVariance,
"",
"");
END_REFLECTOR END_REFLECTOR
BEGIN_OBJECT_REFLECTOR(osgUtil::Optimizer::StaticObjectDetectionVisitor) BEGIN_OBJECT_REFLECTOR(osgUtil::Optimizer::StaticObjectDetectionVisitor)