Skip to content

Commit

Permalink
Enable recycling in the GL Object pool
Browse files Browse the repository at this point in the history
  • Loading branch information
gwaldron committed Feb 23, 2024
1 parent ae287e9 commit 2dbba5a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/osgEarth/GLUtils
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,9 @@ namespace osgEarth
//! construct an object pool under the given graphics context ID
GLObjectPool(unsigned contextID);

//! Whether to support recycling of GL objects with identical properties
static void setEnableRecycling(bool value) { _enableRecycling = value; }

//! Fetch the object pool for the graphics context represented
//! by the state object
static GLObjectPool* get(osg::State& state);
Expand Down Expand Up @@ -600,7 +603,8 @@ namespace osgEarth

template<typename T>
typename T::Ptr recycle(const GLObject::Compatible& compatible) {
std::unique_lock<std::mutex> lock(_mutex);
if (!_enableRecycling) return {};
std::lock_guard<std::mutex> lock(_mutex);
typename T::Ptr result;
for (auto& object : _objects) {
if (object.use_count() == 1 && compatible(object.get())) {
Expand Down Expand Up @@ -628,6 +632,7 @@ namespace osgEarth
mutable std::mutex _mutex;
Collection _objects; // objects being monitored
GLsizeiptr _totalBytes;
static bool _enableRecycling;
unsigned _hits;
unsigned _misses;
unsigned _avarice;
Expand Down
23 changes: 17 additions & 6 deletions src/osgEarth/GLUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ using namespace osgEarth;

#define LC "[GLUtils] "

//#define USE_RECYCLING
#define USE_RECYCLING

#define OE_DEVEL OE_DEBUG

Expand Down Expand Up @@ -525,6 +525,7 @@ namespace
std::mutex s_pools_mutex;
std::vector<GLObjectPool*> s_pools;
}
bool GLObjectPool::_enableRecycling = true;


GLObjectPool*
Expand All @@ -544,7 +545,7 @@ std::unordered_map<int, GLObjectPool*>
GLObjectPool::getAll()
{
std::unordered_map<int, GLObjectPool*> result;
std::unique_lock<std::mutex> lock(s_pools_mutex);
std::lock_guard<std::mutex> lock(s_pools_mutex);
for (auto& pool : s_pools)
result[pool->getContextID()] = pool;
return result;
Expand All @@ -559,7 +560,7 @@ GLObjectPool::GLObjectPool(unsigned cxid) :
{
_gcs.resize(256);

std::unique_lock<std::mutex> lock(s_pools_mutex);
std::lock_guard<std::mutex> lock(s_pools_mutex);
s_pools.emplace_back(this);

char* value = ::getenv("OSGEARTH_GL_OBJECT_POOL_DELAY");
Expand Down Expand Up @@ -605,7 +606,7 @@ GLObjectPool::track(osg::GraphicsContext* gc)
void
GLObjectPool::watch(GLObject::Ptr object)
{
std::unique_lock<std::mutex> lock(_mutex);
std::lock_guard<std::mutex> lock(_mutex);
_objects.emplace_back(object);
}

Expand Down Expand Up @@ -663,7 +664,7 @@ GLObjectPool::discardAllGLObjects()
void
GLObjectPool::releaseAll(const osg::GraphicsContext* gc)
{
std::unique_lock<std::mutex> lock(_mutex);
std::lock_guard<std::mutex> lock(_mutex);

GLsizeiptr bytes = 0;
GLObjectPool::Collection keepers;
Expand All @@ -688,7 +689,7 @@ GLObjectPool::releaseAll(const osg::GraphicsContext* gc)
void
GLObjectPool::releaseOrphans(const osg::GraphicsContext* gc)
{
std::unique_lock<std::mutex> lock(_mutex);
std::lock_guard<std::mutex> lock(_mutex);

unsigned maxNumToRelease = std::max(1u, (unsigned)pow(4.0f, _avarice));
unsigned numReleased = 0u;
Expand Down Expand Up @@ -1755,6 +1756,16 @@ namespace

std::atomic_int GLObjectsCompiler::_jobsActive;

namespace
{
struct StateToCompileEx : public osgUtil::StateToCompile
{
void apply(osg::StateSet& stateSet) override
{
}
};
}

osg::ref_ptr<osgUtil::StateToCompile>
GLObjectsCompiler::collectState(osg::Node* node) const
{
Expand Down
7 changes: 7 additions & 0 deletions src/osgEarth/ImGui/RenderingGUI
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,13 @@ namespace osgEarth
}
if (pools.size() > 1)
ImGui::Separator();

auto recycle_attempts = glpool->recycleHits() + glpool->recycleMisses();
if (recycle_attempts > 0)
{
float re = (float)glpool->recycleHits() / (float)recycle_attempts;
ImGui::Text("Recycle Efficiency: %.0f%%", 100.0f * re);
}
}
}
}
Expand Down

0 comments on commit 2dbba5a

Please sign in to comment.