2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2004-01-11 01:13:20 +08:00
|
|
|
*
|
2012-03-22 01:36: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
|
2004-01-11 01:13:20 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2004-01-11 01:13:20 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2004-01-11 01:13:20 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSGDB_SHAREDSTATEMANAGER
|
|
|
|
#define OSGDB_SHAREDSTATEMANAGER 1
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-01-11 01:13:20 +08:00
|
|
|
|
|
|
|
#include <osg/NodeVisitor>
|
|
|
|
#include <osg/Geode>
|
|
|
|
|
|
|
|
#include <osgDB/Export>
|
|
|
|
|
|
|
|
#include <OpenThreads/Mutex>
|
|
|
|
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
|
|
|
|
namespace osgDB {
|
|
|
|
|
|
|
|
class OSGDB_EXPORT SharedStateManager : public osg::NodeVisitor
|
|
|
|
{
|
2012-03-22 01:36:20 +08:00
|
|
|
public:
|
2004-01-11 01:13:20 +08:00
|
|
|
|
|
|
|
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")
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2008-07-02 17:25:08 +08:00
|
|
|
void setShareMode(unsigned int mode);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
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);
|
|
|
|
|
2010-12-13 20:16:57 +08:00
|
|
|
void releaseGLObjects(osg::State* state ) const;
|
|
|
|
|
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
|
|
|
};
|
2012-03-22 01:36:20 +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];
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
// Share connection mutex
|
2004-01-11 01:13:20 +08:00
|
|
|
|
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
|