SharedStateManager
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGDB_SHAREDSTATEMANAGER
#define OSGDB_SHAREDSTATEMANAGER 1
#include <osg/NodeVisitor>
#include <osg/Geode>
#include <osgDB/Export>
#include <OpenThreads/Mutex>
#include <set>
namespace osgDB {
class OSGDB_EXPORT SharedStateManager : public osg::NodeVisitor
{
public:
enum ShareMode
{
SHARE_NONE = 0,
SHARE_STATIC_TEXTURES = 1<<0,
SHARE_UNSPECIFIED_TEXTURES = 1<<1,
SHARE_DYNAMIC_TEXTURES = 1<<2,
SHARE_STATIC_STATESETS = 1<<3,
SHARE_UNSPECIFIED_STATESETS = 1<<4,
SHARE_DYNAMIC_STATESETS = 1<<5,
SHARE_TEXTURES = SHARE_STATIC_TEXTURES | SHARE_UNSPECIFIED_TEXTURES,
SHARE_STATESETS = SHARE_STATIC_STATESETS | SHARE_UNSPECIFIED_STATESETS,
SHARE_ALL = SHARE_TEXTURES |
SHARE_STATESETS
};
SharedStateManager(unsigned int mode = SHARE_ALL);
META_NodeVisitor(osgDB, SharedStateManager)
void setShareMode(unsigned int mode);
unsigned int getShareMode() { return _shareMode; }
// Call right after each unload and before Registry cache prune.
void prune();
// Call right after each load
void share(osg::Node *node, OpenThreads::Mutex *mt=0);
void apply(osg::Node& node);
// Answers the question "Will this state set be eliminated by
// the SharedStateManager because an equivalent one has been
// seen already?" Safe to call from the pager thread.
bool isShared(osg::StateSet* stateSet);
bool isShared(osg::Texture* texture);
void releaseGLObjects(osg::State* state ) const;
protected:
inline bool shareTexture(osg::Object::DataVariance variance)
{
return _shareTexture[variance];
}
inline bool shareStateSet(osg::Object::DataVariance variance)
{
return _shareStateSet[variance];
}
void process(osg::StateSet* ss, osg::Object* parent);
osg::StateAttribute *find(osg::StateAttribute *sa);
osg::StateSet *find(osg::StateSet *ss);
void setStateSet(osg::StateSet* ss, osg::Object* object);
void shareTextures(osg::StateSet* ss);
struct CompareStateAttributes
{
bool operator()(const osg::ref_ptr<osg::StateAttribute>& lhs,
const osg::ref_ptr<osg::StateAttribute>& rhs) const
{
return *lhs < *rhs;
}
};
struct CompareStateSets
{
bool operator()(const osg::ref_ptr<osg::StateSet>& lhs,
const osg::ref_ptr<osg::StateSet>& rhs) const
{
return lhs->compare(*rhs, true) < 0;
}
};
// Lists of shared objects
typedef std::set< osg::ref_ptr<osg::StateAttribute>, CompareStateAttributes > TextureSet;
TextureSet _sharedTextureList;
typedef std::set< osg::ref_ptr<osg::StateSet>, CompareStateSets > StateSetSet;
StateSetSet _sharedStateSetList;
// Temporary lists just to avoid unnecessary find calls
typedef std::pair<osg::StateAttribute*, bool> TextureSharePair;
typedef std::map<osg::StateAttribute*, TextureSharePair> TextureTextureSharePairMap;
TextureTextureSharePairMap tmpSharedTextureList;
typedef std::pair<osg::StateSet*, bool> StateSetSharePair;
typedef std::map<osg::StateSet*, StateSetSharePair> StateSetStateSetSharePairMap;
StateSetStateSetSharePairMap tmpSharedStateSetList;
unsigned int _shareMode;
bool _shareTexture[3];
bool _shareStateSet[3];
// Share connection mutex
OpenThreads::Mutex *_mutex;
// Mutex for doing isShared queries from other threads
mutable OpenThreads::Mutex _listMutex;
};
}
#endif
Geode
Mutex
NodeVisitor
OpenThreads::Mutex
This class provides an object-oriented thread mutex interface.
Definition Mutex:31
osgDB::SharedStateManager::tmpSharedStateSetList
StateSetStateSetSharePairMap tmpSharedStateSetList
Definition SharedStateManager:125
osgDB::SharedStateManager::_shareStateSet
bool _shareStateSet[3]
Definition SharedStateManager:129
osgDB::SharedStateManager::ShareMode
ShareMode
Definition SharedStateManager:35
osgDB::SharedStateManager::SHARE_UNSPECIFIED_STATESETS
@ SHARE_UNSPECIFIED_STATESETS
Definition SharedStateManager:41
osgDB::SharedStateManager::SHARE_STATIC_TEXTURES
@ SHARE_STATIC_TEXTURES
Definition SharedStateManager:37
osgDB::SharedStateManager::SHARE_STATESETS
@ SHARE_STATESETS
Definition SharedStateManager:44
osgDB::SharedStateManager::SHARE_STATIC_STATESETS
@ SHARE_STATIC_STATESETS
Definition SharedStateManager:40
osgDB::SharedStateManager::SHARE_TEXTURES
@ SHARE_TEXTURES
Definition SharedStateManager:43
osgDB::SharedStateManager::SHARE_ALL
@ SHARE_ALL
Definition SharedStateManager:45
osgDB::SharedStateManager::SHARE_NONE
@ SHARE_NONE
Definition SharedStateManager:36
osgDB::SharedStateManager::SHARE_DYNAMIC_STATESETS
@ SHARE_DYNAMIC_STATESETS
Definition SharedStateManager:42
osgDB::SharedStateManager::SHARE_UNSPECIFIED_TEXTURES
@ SHARE_UNSPECIFIED_TEXTURES
Definition SharedStateManager:38
osgDB::SharedStateManager::SHARE_DYNAMIC_TEXTURES
@ SHARE_DYNAMIC_TEXTURES
Definition SharedStateManager:39
osgDB::SharedStateManager::META_NodeVisitor
META_NodeVisitor(osgDB, SharedStateManager) void setShareMode(unsigned int mode)
osgDB::SharedStateManager::TextureSharePair
std::pair< osg::StateAttribute *, bool > TextureSharePair
Definition SharedStateManager:119
osgDB::SharedStateManager::process
void process(osg::StateSet *ss, osg::Object *parent)
osgDB::SharedStateManager::shareStateSet
bool shareStateSet(osg::Object::DataVariance variance)
Definition SharedStateManager:81
osgDB::SharedStateManager::tmpSharedTextureList
TextureTextureSharePairMap tmpSharedTextureList
Definition SharedStateManager:121
osgDB::SharedStateManager::_sharedTextureList
TextureSet _sharedTextureList
Definition SharedStateManager:113
osgDB::SharedStateManager::find
osg::StateAttribute * find(osg::StateAttribute *sa)
osgDB::SharedStateManager::shareTexture
bool shareTexture(osg::Object::DataVariance variance)
Definition SharedStateManager:76
osgDB::SharedStateManager::find
osg::StateSet * find(osg::StateSet *ss)
osgDB::SharedStateManager::_sharedStateSetList
StateSetSet _sharedStateSetList
Definition SharedStateManager:116
osgDB::SharedStateManager::isShared
bool isShared(osg::StateSet *stateSet)
osgDB::SharedStateManager::_shareTexture
bool _shareTexture[3]
Definition SharedStateManager:128
osgDB::SharedStateManager::getShareMode
unsigned int getShareMode()
Definition SharedStateManager:55
osgDB::SharedStateManager::_listMutex
OpenThreads::Mutex _listMutex
Definition SharedStateManager:135
osgDB::SharedStateManager::_mutex
OpenThreads::Mutex * _mutex
Definition SharedStateManager:133
osgDB::SharedStateManager::isShared
bool isShared(osg::Texture *texture)
osgDB::SharedStateManager::setStateSet
void setStateSet(osg::StateSet *ss, osg::Object *object)
osgDB::SharedStateManager::StateSetSharePair
std::pair< osg::StateSet *, bool > StateSetSharePair
Definition SharedStateManager:123
osgDB::SharedStateManager::releaseGLObjects
void releaseGLObjects(osg::State *state) const
osgDB::SharedStateManager::_shareMode
unsigned int _shareMode
Definition SharedStateManager:127
osgDB::SharedStateManager::share
void share(osg::Node *node, OpenThreads::Mutex *mt=0)
osgDB::SharedStateManager::StateSetSet
std::set< osg::ref_ptr< osg::StateSet >, CompareStateSets > StateSetSet
Definition SharedStateManager:115
osgDB::SharedStateManager::TextureSet
std::set< osg::ref_ptr< osg::StateAttribute >, CompareStateAttributes > TextureSet
Definition SharedStateManager:112
osgDB::SharedStateManager::TextureTextureSharePairMap
std::map< osg::StateAttribute *, TextureSharePair > TextureTextureSharePairMap
Definition SharedStateManager:120
osgDB::SharedStateManager::shareTextures
void shareTextures(osg::StateSet *ss)
osgDB::SharedStateManager::prune
void prune()
osgDB::SharedStateManager::StateSetStateSetSharePairMap
std::map< osg::StateSet *, StateSetSharePair > StateSetStateSetSharePairMap
Definition SharedStateManager:124
osgDB::SharedStateManager::SharedStateManager
SharedStateManager(unsigned int mode=SHARE_ALL)
osgDB::SharedStateManager::apply
void apply(osg::Node &node)
osg::NodeVisitor
Definition NodeVisitor:82
osg::Node
Definition Node:72
osg::Object
Definition Object:61
osg::Object::DataVariance
DataVariance
Definition Object:217
osg::StateAttribute
Definition StateAttribute:77
osg::StateSet
Definition StateSet:46
osg::State
Definition State:80
osg::Texture
Definition Texture:422
osg::ref_ptr
Definition ref_ptr:32
osgDB
Definition Archive:24
Export
OSGDB_EXPORT
#define OSGDB_EXPORT
Definition osgDB/Export:39
osgDB::SharedStateManager::CompareStateAttributes
Definition SharedStateManager:94
osgDB::SharedStateManager::CompareStateAttributes::operator()
bool operator()(const osg::ref_ptr< osg::StateAttribute > &lhs, const osg::ref_ptr< osg::StateAttribute > &rhs) const
Definition SharedStateManager:95
osgDB::SharedStateManager::CompareStateSets
Definition SharedStateManager:103
osgDB::SharedStateManager::CompareStateSets::operator()
bool operator()(const osg::ref_ptr< osg::StateSet > &lhs, const osg::ref_ptr< osg::StateSet > &rhs) const
Definition SharedStateManager:104