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