2006-07-18 23:21:48 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2004-01-11 01:13:20 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
{
|
2008-07-02 17:25:08 +08:00
|
|
|
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,
|
2004-01-11 01:13:20 +08:00
|
|
|
SHARE_ALL = SHARE_TEXTURES |
|
|
|
|
SHARE_STATESETS
|
|
|
|
};
|
|
|
|
|
2008-07-02 17:25:08 +08:00
|
|
|
SharedStateManager(unsigned int mode = SHARE_ALL);
|
2004-01-11 01:13:20 +08:00
|
|
|
|
2008-12-17 20:13:15 +08:00
|
|
|
META_NodeVisitor("osgDB","SharedStateManager")
|
|
|
|
|
2008-07-02 17:25:08 +08:00
|
|
|
void setShareMode(unsigned int mode);
|
|
|
|
|
2007-12-11 20:32:31 +08:00
|
|
|
unsigned int getShareMode() { return _shareMode; }
|
2004-01-11 01:13:20 +08:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
void apply(osg::Geode& geode);
|
2010-10-15 02:16:03 +08:00
|
|
|
|
2007-12-11 20:32:31 +08:00
|
|
|
// 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);
|
2010-10-15 02:16:03 +08:00
|
|
|
|
|
|
|
bool isShared(osg::Texture* texture);
|
|
|
|
|
2004-10-01 17:40:06 +08:00
|
|
|
protected:
|
|
|
|
|
2008-07-02 17:25:08 +08:00
|
|
|
inline bool shareTexture(osg::Object::DataVariance variance)
|
|
|
|
{
|
|
|
|
return _shareTexture[variance];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool shareStateSet(osg::Object::DataVariance variance)
|
|
|
|
{
|
|
|
|
return _shareStateSet[variance];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-01-11 01:13:20 +08:00
|
|
|
void process(osg::StateSet* ss, osg::Object* parent);
|
2005-01-29 00:09:11 +08:00
|
|
|
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);
|
2004-01-11 01:13:20 +08:00
|
|
|
|
2007-12-11 20:32:31 +08:00
|
|
|
struct CompareStateAttributes
|
|
|
|
{
|
|
|
|
bool operator()(const osg::ref_ptr<osg::StateAttribute>& lhs,
|
2007-12-12 18:47:58 +08:00
|
|
|
const osg::ref_ptr<osg::StateAttribute>& rhs) const
|
|
|
|
{
|
|
|
|
return *lhs < *rhs;
|
|
|
|
}
|
2007-12-11 20:32:31 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CompareStateSets
|
|
|
|
{
|
|
|
|
bool operator()(const osg::ref_ptr<osg::StateSet>& lhs,
|
2007-12-12 18:47:58 +08:00
|
|
|
const osg::ref_ptr<osg::StateSet>& rhs) const
|
|
|
|
{
|
|
|
|
return lhs->compare(*rhs, true) < 0;
|
|
|
|
}
|
2007-12-11 20:32:31 +08:00
|
|
|
};
|
|
|
|
|
2004-01-11 01:13:20 +08:00
|
|
|
// Lists of shared objects
|
2007-12-11 20:32:31 +08:00
|
|
|
typedef std::set< osg::ref_ptr<osg::StateAttribute>, CompareStateAttributes > TextureSet;
|
2004-01-11 01:13:20 +08:00
|
|
|
TextureSet _sharedTextureList;
|
2007-12-11 20:32:31 +08:00
|
|
|
|
|
|
|
typedef std::set< osg::ref_ptr<osg::StateSet>, CompareStateSets > StateSetSet;
|
2004-01-11 01:13:20 +08:00
|
|
|
StateSetSet _sharedStateSetList;
|
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// Temporary lists just to avoid unnecessary find calls
|
2004-01-11 01:13:20 +08:00
|
|
|
typedef std::pair<osg::StateAttribute*, bool> TextureSharePair;
|
|
|
|
typedef std::map<osg::StateAttribute*, TextureSharePair> TextureTextureSharePairMap;
|
|
|
|
TextureTextureSharePairMap tmpSharedTextureList;
|
2007-12-11 20:32:31 +08:00
|
|
|
|
2004-01-11 01:13:20 +08:00
|
|
|
typedef std::pair<osg::StateSet*, bool> StateSetSharePair;
|
|
|
|
typedef std::map<osg::StateSet*, StateSetSharePair> StateSetStateSetSharePairMap;
|
|
|
|
StateSetStateSetSharePairMap tmpSharedStateSetList;
|
|
|
|
|
2008-07-02 17:25:08 +08:00
|
|
|
unsigned int _shareMode;
|
|
|
|
bool _shareTexture[3];
|
|
|
|
bool _shareStateSet[3];
|
|
|
|
|
2004-01-11 01:13:20 +08:00
|
|
|
// Share connection mutex
|
|
|
|
|
2007-12-11 20:32:31 +08:00
|
|
|
OpenThreads::Mutex *_mutex;
|
|
|
|
// Mutex for doing isShared queries from other threads
|
|
|
|
mutable OpenThreads::Mutex _listMutex;
|
2004-01-11 01:13:20 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|