Skip to content

Commit ea1d627

Browse files
authored
Merge pull request #35 from elsid/erase_managers_sets
Erase empty object sets
2 parents ff19a67 + d645a50 commit ea1d627

File tree

10 files changed

+160
-74
lines changed

10 files changed

+160
-74
lines changed

include/osg/BufferObject

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ class OSG_EXPORT GLBufferObject : public GraphicsObject
272272
};
273273

274274
typedef std::list< ref_ptr<GLBufferObject> > GLBufferObjectList;
275+
typedef std::vector<ref_ptr<GLBufferObject>> GLBufferObjectVector;
275276

276277
class OSG_EXPORT GLBufferObjectSet : public Referenced
277278
{
@@ -322,7 +323,8 @@ class OSG_EXPORT GLBufferObjectSet : public Referenced
322323
BufferObjectProfile _profile;
323324
unsigned int _numOfGLBufferObjects;
324325
GLBufferObjectList _orphanedGLBufferObjects;
325-
GLBufferObjectList _pendingOrphanedGLBufferObjects;
326+
OpenThreads::Atomic _pendingOrphanedGLBufferObjectsSize;
327+
GLBufferObjectVector _pendingOrphanedGLBufferObjects;
326328

327329
GLBufferObject* _head;
328330
GLBufferObject* _tail;
@@ -368,6 +370,8 @@ class OSG_EXPORT GLBufferObjectManager : public GraphicsObjectManager
368370
void reportStats(std::ostream& out);
369371
void recomputeStats(std::ostream& out) const;
370372

373+
void reportStats(unsigned frameNumber, Stats& stats) const override;
374+
371375
unsigned int& getFrameNumber() { return _frameNumber; }
372376
unsigned int& getNumberFrames() { return _numFrames; }
373377

include/osg/ContextData

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414
#ifndef OSG_CONTEXTDATA
1515
#define OSG_CONTEXTDATA 1
1616

17+
#include <osg/GLObjects>
1718
#include <osg/GraphicsContext>
1819

1920
namespace osg {
2021

22+
class ContextData;
23+
24+
typedef std::map<unsigned int, osg::ref_ptr<ContextData> > ContextDataMap;
25+
2126
class OSG_EXPORT ContextData : public GraphicsObjectManager
2227
{
2328
public:
@@ -73,6 +78,8 @@ class OSG_EXPORT ContextData : public GraphicsObjectManager
7378
virtual void reportStats(std::ostream& out);
7479
virtual void recomputeStats(std::ostream& out) const;
7580

81+
void reportStats(unsigned frameNumber, Stats& stats) const override;
82+
7683
/** Flush all deleted OpenGL objects within the specified availableTime.
7784
* Note, must be called from a thread which has current the graphics context associated with contextID. */
7885
virtual void flushDeletedGLObjects(double currentTime, double& availableTime);
@@ -130,6 +137,8 @@ class OSG_EXPORT ContextData : public GraphicsObjectManager
130137
/** Unregister a GraphicsContext.*/
131138
static void unregisterGraphicsContext(GraphicsContext* gc);
132139

140+
static ContextDataMap getContextDataMap();
141+
133142
protected:
134143
virtual ~ContextData();
135144

include/osg/GLObjects

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace osg {
2323

2424
// forward declare
2525
class FrameStamp;
26+
class Stats;
2627

2728
/** Flush all deleted OpenGL objects within the specified availableTime.
2829
* Note, must be called from a thread which has current the graphics context associated with contextID. */
@@ -67,6 +68,8 @@ class OSG_EXPORT GraphicsObjectManager : public osg::Referenced
6768
virtual void reportStats(std::ostream& /*out*/) {}
6869
virtual void recomputeStats(std::ostream& /*out*/) const {}
6970

71+
virtual void reportStats(unsigned frameNumber, Stats& stats) const {}
72+
7073

7174
/** Flush all deleted OpenGL objects within the specified availableTime.
7275
* Note, must be called from a thread which has current the graphics context associated with contextID. */
@@ -117,11 +120,13 @@ public:
117120
/** implementation of the actual deletion of an GL object - subclasses from GLObjectManager must implement the appropriate GL calls.*/
118121
virtual void deleteGLObject(GLuint globj) = 0;
119122

123+
void reportStats(unsigned frameNumber, Stats& stats) const override;
124+
120125
protected:
121126
virtual ~GLObjectManager();
122127

123128
typedef std::list<GLuint> GLObjectHandleList;
124-
OpenThreads::Mutex _mutex;
129+
mutable OpenThreads::Mutex _mutex;
125130
GLObjectHandleList _deleteGLObjectHandles;
126131

127132
};

include/osg/Texture

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,7 @@ public:
11661166
unsigned int getNumPendingOrphans() const { return static_cast<unsigned int>(_pendingOrphanedTextureObjects.size()); }
11671167

11681168
protected:
1169+
typedef std::vector<ref_ptr<Texture::TextureObject>> TextureObjectVector;
11691170

11701171
virtual ~TextureObjectSet();
11711172

@@ -1176,7 +1177,8 @@ protected:
11761177
Texture::TextureProfile _profile;
11771178
unsigned int _numOfTextureObjects;
11781179
Texture::TextureObjectList _orphanedTextureObjects;
1179-
Texture::TextureObjectList _pendingOrphanedTextureObjects;
1180+
OpenThreads::Atomic _pendingOrphanedTextureObjectsSize;
1181+
TextureObjectVector _pendingOrphanedTextureObjects;
11801182

11811183
Texture::TextureObject* _head;
11821184
Texture::TextureObject* _tail;
@@ -1232,6 +1234,8 @@ public:
12321234
void recomputeStats(std::ostream& out) const;
12331235
bool checkConsistency() const;
12341236

1237+
void reportStats(unsigned frameNumber, Stats& stats) const override;
1238+
12351239
unsigned int& getFrameNumber() { return _frameNumber; }
12361240
unsigned int& getNumberFrames() { return _numFrames; }
12371241

src/osg/BufferObject.cpp

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <osg/PrimitiveSet>
2626
#include <osg/Array>
2727
#include <osg/ContextData>
28+
#include <osg/Stats>
2829

2930
#include <OpenThreads/ScopedLock>
3031
#include <OpenThreads/Mutex>
@@ -37,6 +38,19 @@
3738

3839
using namespace osg;
3940

41+
namespace
42+
{
43+
44+
template <class Iterator, class Map>
45+
Iterator eraseIfEmpty(Iterator it, Map& map)
46+
{
47+
if (it->second->getNumOfGLBufferObjects() == 0)
48+
return map.erase(it);
49+
return ++it;
50+
}
51+
52+
}
53+
4054
//////////////////////////////////////////////////////////////////////////////////////////////////////
4155
//
4256
// GLBufferObject::BufferEntry
@@ -351,7 +365,7 @@ void GLBufferObjectSet::handlePendingOrphandedGLBufferObjects()
351365

352366
unsigned int numOrphaned = _pendingOrphanedGLBufferObjects.size();
353367

354-
for(GLBufferObjectList::iterator itr = _pendingOrphanedGLBufferObjects.begin();
368+
for(GLBufferObjectVector::iterator itr = _pendingOrphanedGLBufferObjects.begin();
355369
itr != _pendingOrphanedGLBufferObjects.end();
356370
++itr)
357371
{
@@ -368,6 +382,7 @@ void GLBufferObjectSet::handlePendingOrphandedGLBufferObjects()
368382
_parent->getNumberActiveGLBufferObjects() -= numOrphaned;
369383

370384
_pendingOrphanedGLBufferObjects.clear();
385+
_pendingOrphanedGLBufferObjectsSize.exchange(0);
371386

372387
CHECK_CONSISTENCY
373388
}
@@ -376,13 +391,11 @@ void GLBufferObjectSet::deleteAllGLBufferObjects()
376391
{
377392
// OSG_NOTICE<<"GLBufferObjectSet::deleteAllGLBufferObjects()"<<std::endl;
378393

394+
if (_pendingOrphanedGLBufferObjectsSize != 0)
379395
{
380396
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
381-
if (!_pendingOrphanedGLBufferObjects.empty())
382-
{
383-
// OSG_NOTICE<<"GLBufferObjectSet::flushDeletedGLBufferObjects(..) handling orphans"<<std::endl;
384-
handlePendingOrphandedGLBufferObjects();
385-
}
397+
// OSG_NOTICE<<"GLBufferObjectSet::flushDeletedGLBufferObjects(..) handling orphans"<<std::endl;
398+
handlePendingOrphandedGLBufferObjects();
386399
}
387400

388401
CHECK_CONSISTENCY
@@ -455,13 +468,11 @@ void GLBufferObjectSet::discardAllGLBufferObjects()
455468

456469
void GLBufferObjectSet::flushAllDeletedGLBufferObjects()
457470
{
471+
if (_pendingOrphanedGLBufferObjectsSize != 0)
458472
{
459473
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
460-
if (!_pendingOrphanedGLBufferObjects.empty())
461-
{
462-
// OSG_NOTICE<<"GLBufferObjectSet::flushDeletedGLBufferObjects(..) handling orphans"<<std::endl;
463-
handlePendingOrphandedGLBufferObjects();
464-
}
474+
// OSG_NOTICE<<"GLBufferObjectSet::flushDeletedGLBufferObjects(..) handling orphans"<<std::endl;
475+
handlePendingOrphandedGLBufferObjects();
465476
}
466477

467478
for(GLBufferObjectList::iterator itr = _orphanedGLBufferObjects.begin();
@@ -487,13 +498,11 @@ void GLBufferObjectSet::discardAllDeletedGLBufferObjects()
487498
// OSG_NOTICE<<"GLBufferObjectSet::discardAllDeletedGLBufferObjects()"<<std::endl;
488499

489500
// clean up the pending orphans.
501+
if (_pendingOrphanedGLBufferObjectsSize != 0)
490502
{
491503
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
492-
if (!_pendingOrphanedGLBufferObjects.empty())
493-
{
494-
// OSG_NOTICE<<"GLBufferObjectSet::flushDeletedGLBufferObjects(..) handling orphans"<<std::endl;
495-
handlePendingOrphandedGLBufferObjects();
496-
}
504+
// OSG_NOTICE<<"GLBufferObjectSet::flushDeletedGLBufferObjects(..) handling orphans"<<std::endl;
505+
handlePendingOrphandedGLBufferObjects();
497506
}
498507

499508
unsigned int numDiscarded = _orphanedGLBufferObjects.size();
@@ -514,13 +523,11 @@ void GLBufferObjectSet::discardAllDeletedGLBufferObjects()
514523

515524
void GLBufferObjectSet::flushDeletedGLBufferObjects(double /*currentTime*/, double& availableTime)
516525
{
526+
if (_pendingOrphanedGLBufferObjectsSize != 0)
517527
{
518528
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
519-
if (!_pendingOrphanedGLBufferObjects.empty())
520-
{
521-
// OSG_NOTICE<<"GLBufferObjectSet::flushDeletedGLBufferObjects(..) handling orphans"<<std::endl;
522-
handlePendingOrphandedGLBufferObjects();
523-
}
529+
// OSG_NOTICE<<"GLBufferObjectSet::flushDeletedGLBufferObjects(..) handling orphans"<<std::endl;
530+
handlePendingOrphandedGLBufferObjects();
524531
}
525532

526533
if (_parent->getCurrGLBufferObjectPoolSize()<=_parent->getMaxGLBufferObjectPoolSize())
@@ -574,11 +581,8 @@ bool GLBufferObjectSet::makeSpace(unsigned int& size)
574581
{
575582
{
576583
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
577-
if (!_pendingOrphanedGLBufferObjects.empty())
578-
{
579-
// OSG_NOTICE<<"GLBufferSet::::makeSpace(..) handling orphans"<<std::endl;
580-
handlePendingOrphandedGLBufferObjects();
581-
}
584+
// OSG_NOTICE<<"GLBufferSet::::makeSpace(..) handling orphans"<<std::endl;
585+
handlePendingOrphandedGLBufferObjects();
582586
}
583587

584588
if (!_orphanedGLBufferObjects.empty())
@@ -795,6 +799,7 @@ void GLBufferObjectSet::orphan(GLBufferObject* to)
795799
// list. This double buffered approach to handling orphaned TO's is used
796800
// to avoid having to mutex the process of appling active TO's.
797801
_pendingOrphanedGLBufferObjects.push_back(to);
802+
++_pendingOrphanedGLBufferObjectsSize;
798803
}
799804

800805
void GLBufferObjectSet::remove(GLBufferObject* to)
@@ -936,10 +941,10 @@ void GLBufferObjectManager::deleteAllGLObjects()
936941
ElapsedTime elapsedTime(&(getDeleteTime()));
937942

938943
for(GLBufferObjectSetMap::iterator itr = _glBufferObjectSetMap.begin();
939-
itr != _glBufferObjectSetMap.end();
940-
++itr)
944+
itr != _glBufferObjectSetMap.end();)
941945
{
942946
(*itr).second->deleteAllGLBufferObjects();
947+
itr = eraseIfEmpty(itr, _glBufferObjectSetMap);
943948
}
944949
}
945950

@@ -951,27 +956,28 @@ void GLBufferObjectManager::discardAllGLObjects()
951956
{
952957
(*itr).second->discardAllGLBufferObjects();
953958
}
959+
_glBufferObjectSetMap.clear();
954960
}
955961

956962
void GLBufferObjectManager::flushAllDeletedGLObjects()
957963
{
958964
ElapsedTime elapsedTime(&(getDeleteTime()));
959965

960966
for(GLBufferObjectSetMap::iterator itr = _glBufferObjectSetMap.begin();
961-
itr != _glBufferObjectSetMap.end();
962-
++itr)
967+
itr != _glBufferObjectSetMap.end();)
963968
{
964969
(*itr).second->flushAllDeletedGLBufferObjects();
970+
itr = eraseIfEmpty(itr, _glBufferObjectSetMap);
965971
}
966972
}
967973

968974
void GLBufferObjectManager::discardAllDeletedGLObjects()
969975
{
970976
for(GLBufferObjectSetMap::iterator itr = _glBufferObjectSetMap.begin();
971-
itr != _glBufferObjectSetMap.end();
972-
++itr)
977+
itr != _glBufferObjectSetMap.end();)
973978
{
974979
(*itr).second->discardAllDeletedGLBufferObjects();
980+
itr = eraseIfEmpty(itr, _glBufferObjectSetMap);
975981
}
976982
}
977983

@@ -980,10 +986,10 @@ void GLBufferObjectManager::flushDeletedGLObjects(double currentTime, double& av
980986
ElapsedTime elapsedTime(&(getDeleteTime()));
981987

982988
for(GLBufferObjectSetMap::iterator itr = _glBufferObjectSetMap.begin();
983-
(itr != _glBufferObjectSetMap.end()) && (availableTime > 0.0);
984-
++itr)
989+
(itr != _glBufferObjectSetMap.end()) && (availableTime > 0.0);)
985990
{
986991
(*itr).second->flushDeletedGLBufferObjects(currentTime, availableTime);
992+
itr = eraseIfEmpty(itr, _glBufferObjectSetMap);
987993
}
988994
}
989995

@@ -1051,6 +1057,11 @@ void GLBufferObjectManager::recomputeStats(std::ostream& out) const
10511057
out<<" getMaxGLBufferObjectPoolSize()="<<getMaxGLBufferObjectPoolSize()<<" current/max size = "<<double(currentSize)/double(getMaxGLBufferObjectPoolSize())<<std::endl;
10521058
}
10531059

1060+
void GLBufferObjectManager::reportStats(unsigned frameNumber, Stats& stats) const
1061+
{
1062+
stats.setAttribute(frameNumber, "GLBufferObjectManager" + std::to_string(_contextID), static_cast<double>(_glBufferObjectSetMap.size()));
1063+
}
1064+
10541065
//////////////////////////////////////////////////////////////////////////////////////////////////////
10551066
//
10561067
// BufferObject

src/osg/ContextData.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
using namespace osg;
2020

2121

22-
typedef std::map<unsigned int, osg::ref_ptr<ContextData> > ContextIDMap;
22+
typedef ContextDataMap ContextIDMap;
2323
static ContextIDMap s_contextIDMap;
2424
static OpenThreads::ReentrantMutex s_contextIDMapMutex;
2525
static ContextData::GraphicsContexts s_registeredContexts;
@@ -79,6 +79,12 @@ void ContextData::recomputeStats(std::ostream& out) const
7979
}
8080
}
8181

82+
void ContextData::reportStats(unsigned frameNumber, Stats& stats) const
83+
{
84+
for (ManagerMap::const_iterator it = _managerMap.begin(); it != _managerMap.end(); ++it)
85+
if (osg::GraphicsObjectManager* const v = dynamic_cast<osg::GraphicsObjectManager*>(it->second.get()))
86+
v->reportStats(frameNumber, stats);
87+
}
8288

8389
void ContextData::flushDeletedGLObjects(double currentTime, double& availableTime)
8490
{
@@ -334,3 +340,9 @@ GraphicsContext* ContextData::getCompileContext(unsigned int contextID)
334340
if (itr != s_contextIDMap.end()) return itr->second->getCompileContext();
335341
else return 0;
336342
}
343+
344+
ContextDataMap ContextData::getContextDataMap()
345+
{
346+
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
347+
return s_contextIDMap;
348+
}

src/osg/Drawable.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,24 @@ class DisplayListManager : public GraphicsObjectManager
188188
#endif
189189
}
190190

191+
void reportStats(unsigned frameNumber, Stats& stats) const override
192+
{
193+
const std::size_t size = [&](){
194+
const OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex_deletedDisplayListCache);
195+
return _displayListMap.size();
196+
}();
197+
198+
stats.setAttribute(frameNumber, "DisplayListManager" + std::to_string(_contextID), static_cast<double>(size));
199+
}
200+
191201
protected:
192202

193203
int _numberDrawablesReusedLastInLastFrame;
194204
int _numberNewDrawablesInLastFrame;
195205
int _numberDeletedDrawablesInLastFrame;
196206

197207
typedef std::multimap<unsigned int,GLuint> DisplayListMap;
198-
OpenThreads::Mutex _mutex_deletedDisplayListCache;
208+
mutable OpenThreads::Mutex _mutex_deletedDisplayListCache;
199209
DisplayListMap _displayListMap;
200210

201211
};

src/osg/GLObjects.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,13 @@ GLuint GLObjectManager::createGLObject()
146146
OSG_INFO<<"void "<<_name<<"::createGLObject() : Not Implemented"<<std::endl;
147147
return 0;
148148
}
149+
150+
void GLObjectManager::reportStats(unsigned frameNumber, Stats& stats) const
151+
{
152+
const std::size_t size = [&](){
153+
const OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
154+
return _deleteGLObjectHandles.size();
155+
}();
156+
157+
stats.setAttribute(frameNumber, _name + std::to_string(_contextID), static_cast<double>(size));
158+
}

0 commit comments

Comments
 (0)