From 6ac688c0e2e16e4eec74ed5ef2d6a1a05a367d66 Mon Sep 17 00:00:00 2001 From: scrawl Date: Fri, 5 Feb 2016 19:28:52 +0100 Subject: [PATCH] Change the way that texture filtering setting changes are applied at runtime to not require keeping a reference to textures The references would be difficult to clean up because there may or may not be another reference to the texture in the osgDB::SharedStateManager. --- components/resource/scenemanager.cpp | 68 ++++++++-------------------- components/resource/scenemanager.hpp | 6 +-- 2 files changed, 21 insertions(+), 53 deletions(-) diff --git a/components/resource/scenemanager.cpp b/components/resource/scenemanager.cpp index 80e6f737d..df103f50c 100644 --- a/components/resource/scenemanager.cpp +++ b/components/resource/scenemanager.cpp @@ -129,21 +129,14 @@ namespace Resource tex->setFilter(osg::Texture::MIN_FILTER, mMinFilter); tex->setFilter(osg::Texture::MAG_FILTER, mMagFilter); tex->setMaxAnisotropy(mMaxAnisotropy); - mTexturesProcessed.insert(tex); } } } - const std::set >& getTexturesProcessed() - { - return mTexturesProcessed; - } - private: osg::Texture::FilterMode mMinFilter; osg::Texture::FilterMode mMagFilter; int mMaxAnisotropy; - std::set > mTexturesProcessed; }; /// Set texture filtering settings on textures contained in StateSets. @@ -199,20 +192,12 @@ namespace Resource tex->setFilter(osg::Texture::MIN_FILTER, mMinFilter); tex->setFilter(osg::Texture::MAG_FILTER, mMagFilter); tex->setMaxAnisotropy(mMaxAnisotropy); - mTexturesProcessed.insert(tex); } } - - const std::set >& getTexturesProcessed() - { - return mTexturesProcessed; - } - private: osg::Texture::FilterMode mMinFilter; osg::Texture::FilterMode mMagFilter; int mMaxAnisotropy; - std::set > mTexturesProcessed; }; @@ -341,10 +326,6 @@ namespace Resource SetFilterSettingsControllerVisitor setFilterSettingsControllerVisitor(mMinFilter, mMagFilter, mMaxAnisotropy); loaded->accept(setFilterSettingsControllerVisitor); - // remember which textures we set a filtering setting on so we can re-apply it when the setting changes - mTexturesWithFilterSetting.insert(setFilterSettingsVisitor.getTexturesProcessed().begin(), setFilterSettingsVisitor.getTexturesProcessed().end()); - mTexturesWithFilterSetting.insert(setFilterSettingsControllerVisitor.getTexturesProcessed().begin(), setFilterSettingsControllerVisitor.getTexturesProcessed().end()); - // share state osgDB::Registry::instance()->getOrCreateSharedStateManager()->share(loaded.get()); @@ -447,7 +428,26 @@ namespace Resource } if(viewer) viewer->stopThreading(); - setFilterSettings(min, mag, maxAnisotropy); + + mMinFilter = min; + mMagFilter = mag; + mMaxAnisotropy = std::max(1, maxAnisotropy); + + SetFilterSettingsControllerVisitor setFilterSettingsControllerVisitor (mMinFilter, mMagFilter, mMaxAnisotropy); + SetFilterSettingsVisitor setFilterSettingsVisitor (mMinFilter, mMagFilter, mMaxAnisotropy); + if (viewer && viewer->getSceneData()) + { + viewer->getSceneData()->accept(setFilterSettingsControllerVisitor); + viewer->getSceneData()->accept(setFilterSettingsVisitor); + } + + for (Index::iterator it = mIndex.begin(); it != mIndex.end(); ++it) + { + osg::Node* node = it->second; + node->accept(setFilterSettingsControllerVisitor); + node->accept(setFilterSettingsVisitor); + } + if(viewer) viewer->startThreading(); } @@ -456,7 +456,6 @@ namespace Resource tex->setFilter(osg::Texture::MIN_FILTER, mMinFilter); tex->setFilter(osg::Texture::MAG_FILTER, mMagFilter); tex->setMaxAnisotropy(mMaxAnisotropy); - mTexturesWithFilterSetting.insert(tex); } void SceneManager::setUnRefImageDataAfterApply(bool unref) @@ -464,32 +463,5 @@ namespace Resource mUnRefImageDataAfterApply = unref; } - void SceneManager::setFilterSettings(osg::Texture::FilterMode minFilter, osg::Texture::FilterMode magFilter, int maxAnisotropy) - { - mMinFilter = minFilter; - mMagFilter = magFilter; - mMaxAnisotropy = std::max(1, maxAnisotropy); - - for (std::set >::const_iterator it = mTexturesWithFilterSetting.begin(); it != mTexturesWithFilterSetting.end(); ++it) - { - (*it)->setFilter(osg::Texture::MIN_FILTER, mMinFilter); - (*it)->setFilter(osg::Texture::MAG_FILTER, mMagFilter); - (*it)->setMaxAnisotropy(mMaxAnisotropy); - } - } - - void SceneManager::clearCache() - { - // clear textures that are no longer referenced - for (std::set >::const_iterator it = mTexturesWithFilterSetting.begin(); it != mTexturesWithFilterSetting.end();) - { - osg::Texture* tex = *it; - if (tex->referenceCount() <= 1) - mTexturesWithFilterSetting.erase(it++); - else - ++it; - } - - } } diff --git a/components/resource/scenemanager.hpp b/components/resource/scenemanager.hpp index df330af5b..ae5fa2db6 100644 --- a/components/resource/scenemanager.hpp +++ b/components/resource/scenemanager.hpp @@ -87,8 +87,6 @@ namespace Resource /// otherwise should be disabled to reduce memory usage. void setUnRefImageDataAfterApply(bool unref); - void clearCache(); - private: const VFS::Manager* mVFS; Resource::TextureManager* mTextureManager; @@ -104,11 +102,9 @@ namespace Resource unsigned int mParticleSystemMask; // observer_ptr? - typedef std::map > Index; + typedef std::map > Index; Index mIndex; - std::set > mTexturesWithFilterSetting; - SceneManager(const SceneManager&); void operator = (const SceneManager&);