19
19
SharedStateManager::SharedStateManager():
20
20
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN)
22
shareMode = SHARE_TEXTURES;
23
//shareMode = SHARE_STATESETS;
22
_shareMode = SHARE_TEXTURES;
23
//_shareMode = SHARE_STATESETS;
27
27
//----------------------------------------------------------------
29
29
//----------------------------------------------------------------
30
30
void SharedStateManager::prune()
32
StateSetSet::iterator sitr, sitr1;
33
for(sitr=_sharedStateSetList.begin(); sitr!=_sharedStateSetList.end(); sitr=sitr1)
32
StateSetSet::iterator sitr;
33
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_listMutex);
34
for(sitr=_sharedStateSetList.begin(); sitr!=_sharedStateSetList.end();)
36
if((*sitr)->referenceCount()<=1) _sharedStateSetList.erase(sitr);
36
if ((*sitr)->referenceCount()<=1)
37
_sharedStateSetList.erase(sitr++);
39
TextureSet::iterator titr, titr1;
40
for(titr=_sharedTextureList.begin(); titr!=_sharedTextureList.end(); titr=titr1)
42
TextureSet::iterator titr;
43
for(titr=_sharedTextureList.begin(); titr!=_sharedTextureList.end();)
43
if((*titr)->referenceCount()<=1) _sharedTextureList.erase(titr);
45
if ((*titr)->referenceCount()<=1)
46
_sharedTextureList.erase(titr++);
54
59
// const osg::Timer& timer = *osg::Timer::instance();
55
60
// osg::Timer_t start_tick = timer.tick();
59
64
tmpSharedTextureList.clear();
60
65
tmpSharedStateSetList.clear();
63
68
// osg::Timer_t end_tick = timer.tick();
64
69
// std::cout << "SHARING TIME = "<<timer.delta_m(start_tick,end_tick)<<"ms"<<std::endl;
99
bool SharedStateManager::isShared(osg::StateSet *ss)
101
if ((_shareMode & SHARE_STATESETS) != 0)
103
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_listMutex);
104
return find(ss) != 0;
95
110
//----------------------------------------------------------------
96
111
// SharedStateManager::find
97
112
//----------------------------------------------------------------
114
// The find methods don't need to lock _listMutex because the thread
115
// from which they are called is doing the writing to the lists.
98
116
osg::StateSet *SharedStateManager::find(osg::StateSet *ss)
100
for(StateSetSet::iterator itr=_sharedStateSetList.begin();
101
itr!=_sharedStateSetList.end();
104
if(ss->compare(*(itr->get()),true)==0)
105
return (osg::StateSet *)itr->get();
118
StateSetSet::iterator result
119
= _sharedStateSetList.find(osg::ref_ptr<osg::StateSet>(ss));
120
if (result == _sharedStateSetList.end())
123
return result->get();
109
125
osg::StateAttribute *SharedStateManager::find(osg::StateAttribute *sa)
111
for(TextureSet::iterator itr=_sharedTextureList.begin();
112
itr!=_sharedTextureList.end();
115
if(sa->compare(*(itr->get()))==0)
116
return (osg::StateAttribute *)itr->get();
127
TextureSet::iterator result
128
= _sharedTextureList.find(osg::ref_ptr<osg::StateAttribute>(sa));
129
if (result == _sharedTextureList.end())
132
return result->get();
164
178
// Texture is in sharedAttributeList:
165
179
// Share now. Required to be shared all next times
166
if(mutex) mutex->lock();
180
if(_mutex) _mutex->lock();
167
181
ss->setTextureAttributeAndModes(unit, textureFromSharedList, osg::StateAttribute::ON);
168
if(mutex) mutex->unlock();
182
if(_mutex) _mutex->unlock();
169
183
tmpSharedTextureList[texture] = TextureSharePair(textureFromSharedList, true);
173
187
// Texture is not in _sharedAttributeList:
174
// Add to _sharedAttributeList. Not needed to be shared all next times.
188
// Add to _sharedAttributeList. Not needed to be
189
// shared all next times.
190
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_listMutex);
175
191
_sharedTextureList.insert(texture);
176
192
tmpSharedTextureList[texture] = TextureSharePair(texture, false);
181
197
// Texture is in tmpSharedAttributeList and share flag is on:
182
198
// It should be shared
183
if(mutex) mutex->lock();
199
if(_mutex) _mutex->lock();
184
200
ss->setTextureAttributeAndModes(unit, titr->second.first, osg::StateAttribute::ON);
185
if(mutex) mutex->unlock();
201
if(_mutex) _mutex->unlock();
194
210
//----------------------------------------------------------------
195
211
void SharedStateManager::process(osg::StateSet* ss, osg::Object* parent)
197
if(shareMode & SHARE_STATESETS)
213
if(_shareMode & SHARE_STATESETS)
199
215
// Valid StateSet to be shared
200
216
if(ss->getDataVariance()==osg::Object::STATIC)
210
226
// StateSet is in sharedStateSetList:
211
227
// Share now. Required to be shared all next times
212
if(mutex) mutex->lock();
228
if(_mutex) _mutex->lock();
213
229
setStateSet(ssFromSharedList, parent);
214
if(mutex) mutex->unlock();
230
if(_mutex) _mutex->unlock();
215
231
tmpSharedStateSetList[ss] = StateSetSharePair(ssFromSharedList, true);
219
235
// StateSet is not in sharedStateSetList:
220
236
// Add to sharedStateSetList. Not needed to be shared all next times.
221
_sharedStateSetList.insert(ss);
222
tmpSharedStateSetList[ss] = StateSetSharePair(ss, false);
238
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_listMutex);
239
_sharedStateSetList.insert(ss);
240
tmpSharedStateSetList[ss]
241
= StateSetSharePair(ss, false);
224
243
// Only in this case sharing textures is also required
225
if(shareMode & SHARE_TEXTURES)
244
if(_shareMode & SHARE_TEXTURES)
227
246
shareTextures(ss);
233
252
// StateSet is in tmpSharedStateSetList and share flag is on:
234
253
// It should be shared
235
if(mutex) mutex->lock();
254
if(_mutex) _mutex->lock();
236
255
setStateSet(sitr->second.first, parent);
237
if(mutex) mutex->unlock();
256
if(_mutex) _mutex->unlock();
242
else if(shareMode & SHARE_TEXTURES)
261
else if(_shareMode & SHARE_TEXTURES)
244
263
shareTextures(ss);