From 6fd4677868cd1da59cec9e809242495d90fb7964 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 12 Jul 2004 13:20:18 +0000 Subject: [PATCH] Added OPTIMIZER_TEXTURE_SETTINGS pass to Optimizer, which enables unref image data after apply, client storage hint. --- include/osgUtil/Optimizer | 44 +++++++++++++++- src/osgDB/DatabasePager.cpp | 17 ++---- src/osgUtil/Optimizer.cpp | 101 +++++++++++++++++++++++++++++++++++- 3 files changed, 146 insertions(+), 16 deletions(-) diff --git a/include/osgUtil/Optimizer b/include/osgUtil/Optimizer index 6cf24fb27..d358819a8 100644 --- a/include/osgUtil/Optimizer +++ b/include/osgUtil/Optimizer @@ -49,12 +49,14 @@ class OSGUTIL_EXPORT Optimizer COPY_SHARED_NODES = 0x080, TRISTRIP_GEOMETRY = 0x100, TESSELATE_GEOMETRY = 0x200, + OPTIMIZE_TEXTURE_SETTINGS = 0x400, DEFAULT_OPTIMIZATIONS = FLATTEN_STATIC_TRANSFORMS | REMOVE_REDUNDANT_NODES | COMBINE_ADJACENT_LODS | SHARE_DUPLICATE_STATE | MERGE_GEOMETRY | - CHECK_GEOMETRY, + CHECK_GEOMETRY | + OPTIMIZE_TEXTURE_SETTINGS, ALL_OPTIMIZATIONS = FLATTEN_STATIC_TRANSFORMS | REMOVE_REDUNDANT_NODES | COMBINE_ADJACENT_LODS | @@ -63,7 +65,8 @@ class OSGUTIL_EXPORT Optimizer CHECK_GEOMETRY | SPATIALIZE_GROUPS | COPY_SHARED_NODES | - TRISTRIP_GEOMETRY + TRISTRIP_GEOMETRY | + OPTIMIZE_TEXTURE_SETTINGS }; /** reset internal data to initial state - the getPrimissableOptionsMap is cleared.*/ @@ -408,6 +411,43 @@ class OSGUTIL_EXPORT Optimizer Optimizer* _optimizer; }; + + + /** For all textures apply settings.*/ + class OSGUTIL_EXPORT TextureVisitor : public osg::NodeVisitor + { + public: + + TextureVisitor(bool changeAutoUnRef, bool valueAutoUnRef, + bool changeClientImageStorage, bool valueClientImageStorage, + bool changeAnisotropy, float valueAnisotropy, + Optimizer* optimizer=0): + osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), + _optimizer(optimizer), + _changeAutoUnRef(changeAutoUnRef), _valueAutoUnRef(valueAutoUnRef), + _changeClientImageStorage(changeClientImageStorage), _valueClientImageStorage(valueClientImageStorage), + _changeAnisotropy(changeAnisotropy), _valueAnisotropy(valueAnisotropy) {} + + virtual void apply(osg::Geode& node); + virtual void apply(osg::Node& node); + + void apply(osg::StateSet& stateset); + void apply(osg::Texture& texture); + + inline bool isOperationPermissableForObject(const osg::Object* object) const + { + return _optimizer ? _optimizer->isOperationPermissableForObject(object,OPTIMIZE_TEXTURE_SETTINGS) : true; + } + + Optimizer* _optimizer; + bool _changeAutoUnRef, _valueAutoUnRef; + bool _changeClientImageStorage, _valueClientImageStorage; + bool _changeAnisotropy, _valueAnisotropy; + + + + }; + }; } diff --git a/src/osgDB/DatabasePager.cpp b/src/osgDB/DatabasePager.cpp index 5fbdc6d03..bb0b41384 100644 --- a/src/osgDB/DatabasePager.cpp +++ b/src/osgDB/DatabasePager.cpp @@ -242,21 +242,14 @@ public: { // search for the existance of any texture object attributes bool foundTextureState = false; - osg::StateSet::TextureAttributeList& tal = stateset->getTextureAttributeList(); - for(osg::StateSet::TextureAttributeList::iterator itr=tal.begin(); - itr!=tal.end() && !foundTextureState; - ++itr) + for(unsigned int i=0;igetTextureAttributeList().size();++i) { - osg::StateSet::AttributeList& al = *itr; - if (al.count(osg::StateAttribute::TEXTURE)==1) + osg::Texture* texture = dynamic_cast(stateset->getTextureAttribute(i,osg::StateAttribute::TEXTURE)); + if (texture) { + texture->setUnRefImageDataAfterApply(_unrefImageOnApply); + texture->setClientStorageHint(_clientStorageHint); foundTextureState = true; - osg::Texture* texture = dynamic_cast(al[osg::StateAttribute::TEXTURE].first.get()); - if (texture) - { - texture->setUnRefImageDataAfterApply(_unrefImageOnApply); - texture->setClientStorageHint(_clientStorageHint); - } } } diff --git a/src/osgUtil/Optimizer.cpp b/src/osgUtil/Optimizer.cpp index bbec01692..11048269b 100644 --- a/src/osgUtil/Optimizer.cpp +++ b/src/osgUtil/Optimizer.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -56,7 +57,7 @@ void Optimizer::reset() _permissableOptimizationsMap.clear(); } -static osg::ApplicationUsageProxy Optimizer_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_OPTIMIZER \" []\"","DEFAULT | FLATTEN_STATIC_TRANSFORMS | REMOVE_REDUNDANT_NODES | COMBINE_ADJACENT_LODS | SHARE_DUPLICATE_STATE | MERGE_GEOMETRY | SPATIALIZE_GROUPS | COPY_SHARED_NODES | TRISTRIP_GEOMETRY"); +static osg::ApplicationUsageProxy Optimizer_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_OPTIMIZER \" []\"","DEFAULT | FLATTEN_STATIC_TRANSFORMS | REMOVE_REDUNDANT_NODES | COMBINE_ADJACENT_LODS | SHARE_DUPLICATE_STATE | MERGE_GEOMETRY | SPATIALIZE_GROUPS | COPY_SHARED_NODES | TRISTRIP_GEOMETRY | OPTIMIZE_TEXTURE_SETTINGS"); void Optimizer::optimize(osg::Node* node) { @@ -98,6 +99,11 @@ void Optimizer::optimize(osg::Node* node) if(str.find("~TRISTRIP_GEOMETRY")!=std::string::npos) options ^= TRISTRIP_GEOMETRY; else if(str.find("TRISTRIP_GEOMETRY")!=std::string::npos) options |= TRISTRIP_GEOMETRY; + if(str.find("~OPTIMIZE_TEXTURE_SETTINGS")!=std::string::npos) options ^= OPTIMIZE_TEXTURE_SETTINGS; + else if(str.find("OPTIMIZE_TEXTURE_SETTINGS")!=std::string::npos) options |= OPTIMIZE_TEXTURE_SETTINGS; + + + } else { @@ -127,6 +133,17 @@ void Optimizer::optimize(osg::Node* node, unsigned int options) clv.combineLODs(); } + if (options & OPTIMIZE_TEXTURE_SETTINGS) + { + osg::notify(osg::INFO)<<"Optimizer::optimize() doing OPTIMIZE_TEXTURE_SETTINGS"<accept(tv); + } + if (options & SHARE_DUPLICATE_STATE) { osg::notify(osg::INFO)<<"Optimizer::optimize() doing SHARE_DUPLICATE_STATE"<accept(tsv); tsv.stripify(); } - + } @@ -2151,3 +2168,83 @@ void Optimizer::CopySharedSubgraphsVisitor::copySharedNodes() } } } + + +//////////////////////////////////////////////////////////////////////////////////////////// +// +// Set the attributes of textures up. +// + + +void Optimizer::TextureVisitor::apply(osg::Node& node) +{ + + osg::StateSet* ss = node.getStateSet(); + if (ss && + isOperationPermissableForObject(&node) && + isOperationPermissableForObject(ss)) + { + apply(*ss); + } + + traverse(node); +} + +void Optimizer::TextureVisitor::apply(osg::Geode& geode) +{ + if (!isOperationPermissableForObject(&geode)) return; + + osg::StateSet* ss = geode.getStateSet(); + + if (ss && isOperationPermissableForObject(ss)) + { + apply(*ss); + } + + for(unsigned int i=0;igetStateSet(); + if (ss && + isOperationPermissableForObject(drawable) && + isOperationPermissableForObject(ss)) + { + apply(*ss); + } + } + } +} + +void Optimizer::TextureVisitor::apply(osg::StateSet& stateset) +{ + for(unsigned int i=0;i(sa); + if (texture && isOperationPermissableForObject(texture)) + { + apply(*texture); + } + } +} + +void Optimizer::TextureVisitor::apply(osg::Texture& texture) +{ + if (_changeAutoUnRef) + { + texture.setUnRefImageDataAfterApply(_valueAutoUnRef); + } + + if (_changeClientImageStorage) + { + texture.setClientStorageHint(_valueClientImageStorage); + } + + if (_changeAnisotropy) + { + texture.setMaxAnisotropy(_valueAnisotropy); + } + +}