openscenegraph
SharedStateManager
Go to the documentation of this file.
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
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.
7 *
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.
12*/
13
14#ifndef OSGDB_SHAREDSTATEMANAGER
15#define OSGDB_SHAREDSTATEMANAGER 1
16
17
18#include <osg/NodeVisitor>
19#include <osg/Geode>
20
21#include <osgDB/Export>
22
23#include <OpenThreads/Mutex>
24
25#include <set>
26
27
28namespace osgDB {
29
31 {
32 public:
33
35 {
36 SHARE_NONE = 0,
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 |
46 SHARE_STATESETS
47 };
48
49 SharedStateManager(unsigned int mode = SHARE_ALL);
50
52
53 void setShareMode(unsigned int mode);
54
55 unsigned int getShareMode() { return _shareMode; }
56
57 // Call right after each unload and before Registry cache prune.
58 void prune();
59
60 // Call right after each load
62
63 void apply(osg::Node& node);
64
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);
69
70 bool isShared(osg::Texture* texture);
71
72 void releaseGLObjects(osg::State* state ) const;
73
74 protected:
75
77 {
78 return _shareTexture[variance];
79 }
80
82 {
83 return _shareStateSet[variance];
84 }
85
86
87 void process(osg::StateSet* ss, osg::Object* parent);
92
94 {
96 const osg::ref_ptr<osg::StateAttribute>& rhs) const
97 {
98 return *lhs < *rhs;
99 }
100 };
101
103 {
105 const osg::ref_ptr<osg::StateSet>& rhs) const
106 {
107 return lhs->compare(*rhs, true) < 0;
108 }
109 };
110
111 // Lists of shared objects
112 typedef std::set< osg::ref_ptr<osg::StateAttribute>, CompareStateAttributes > TextureSet;
114
115 typedef std::set< osg::ref_ptr<osg::StateSet>, CompareStateSets > StateSetSet;
117
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;
122
123 typedef std::pair<osg::StateSet*, bool> StateSetSharePair;
124 typedef std::map<osg::StateSet*, StateSetSharePair> StateSetStateSetSharePairMap;
126
127 unsigned int _shareMode;
128 bool _shareTexture[3];
129 bool _shareStateSet[3];
130
131 // Share connection mutex
132
134 // Mutex for doing isShared queries from other threads
136 };
137
138}
139
140#endif
This class provides an object-oriented thread mutex interface.
Definition Mutex:31
Definition SharedStateManager:31
StateSetStateSetSharePairMap tmpSharedStateSetList
Definition SharedStateManager:125
ShareMode
Definition SharedStateManager:35
META_NodeVisitor(osgDB, SharedStateManager) void setShareMode(unsigned int mode)
std::pair< osg::StateAttribute *, bool > TextureSharePair
Definition SharedStateManager:119
void process(osg::StateSet *ss, osg::Object *parent)
bool shareStateSet(osg::Object::DataVariance variance)
Definition SharedStateManager:81
TextureTextureSharePairMap tmpSharedTextureList
Definition SharedStateManager:121
TextureSet _sharedTextureList
Definition SharedStateManager:113
osg::StateAttribute * find(osg::StateAttribute *sa)
bool shareTexture(osg::Object::DataVariance variance)
Definition SharedStateManager:76
osg::StateSet * find(osg::StateSet *ss)
StateSetSet _sharedStateSetList
Definition SharedStateManager:116
bool isShared(osg::StateSet *stateSet)
unsigned int getShareMode()
Definition SharedStateManager:55
OpenThreads::Mutex _listMutex
Definition SharedStateManager:135
OpenThreads::Mutex * _mutex
Definition SharedStateManager:133
bool isShared(osg::Texture *texture)
void setStateSet(osg::StateSet *ss, osg::Object *object)
std::pair< osg::StateSet *, bool > StateSetSharePair
Definition SharedStateManager:123
void releaseGLObjects(osg::State *state) const
unsigned int _shareMode
Definition SharedStateManager:127
void share(osg::Node *node, OpenThreads::Mutex *mt=0)
std::set< osg::ref_ptr< osg::StateSet >, CompareStateSets > StateSetSet
Definition SharedStateManager:115
std::set< osg::ref_ptr< osg::StateAttribute >, CompareStateAttributes > TextureSet
Definition SharedStateManager:112
std::map< osg::StateAttribute *, TextureSharePair > TextureTextureSharePairMap
Definition SharedStateManager:120
void shareTextures(osg::StateSet *ss)
std::map< osg::StateSet *, StateSetSharePair > StateSetStateSetSharePairMap
Definition SharedStateManager:124
SharedStateManager(unsigned int mode=SHARE_ALL)
void apply(osg::Node &node)
Definition NodeVisitor:82
Definition Node:72
Definition Object:61
DataVariance
Definition Object:217
Definition StateAttribute:77
Definition StateSet:46
Definition State:80
Definition Texture:422
Definition ref_ptr:32
Definition Archive:24
#define OSGDB_EXPORT
Definition osgDB/Export:39
bool operator()(const osg::ref_ptr< osg::StateAttribute > &lhs, const osg::ref_ptr< osg::StateAttribute > &rhs) const
Definition SharedStateManager:95
Definition SharedStateManager:103
bool operator()(const osg::ref_ptr< osg::StateSet > &lhs, const osg::ref_ptr< osg::StateSet > &rhs) const
Definition SharedStateManager:104