Introduced preliminary osg::deleteAllGLObjects() and osg::discardAllGLObjects() functions and associated support into Texture and BufferObjects

This commit is contained in:
Robert Osfield 2009-11-26 12:33:07 +00:00
parent 7146f8a62f
commit ab8d93a181
7 changed files with 241 additions and 20 deletions

View File

@ -238,6 +238,8 @@ class OSG_EXPORT GLBufferObject : public Referenced
* by contextID.*/
static void deleteBufferObject(unsigned int contextID,GLuint globj);
static void deleteAllBufferObjects(unsigned int contextID);
static void discardAllBufferObjects(unsigned int contextID);
static void flushAllDeletedBufferObjects(unsigned int contextID);
static void discardAllDeletedBufferObjects(unsigned int contextID);
static void flushDeletedBufferObjects(unsigned int contextID,double currentTime, double& availbleTime);
@ -351,6 +353,9 @@ class OSG_EXPORT GLBufferObjectSet : public Referenced
GLBufferObjectSet(GLBufferObjectManager* parent, const BufferObjectProfile& profile);
void handlePendingOrphandedGLBufferObjects();
void deleteAllGLBufferObjects();
void discardAllGLBufferObjects();
void flushAllDeletedGLBufferObjects();
void discardAllDeletedGLBufferObjects();
void flushDeletedGLBufferObjects(double currentTime, double& availableTime);
@ -419,6 +424,9 @@ class OSG_EXPORT GLBufferObjectManager : public osg::Referenced
GLBufferObject* generateGLBufferObject(const osg::BufferObject* bufferObject);
void handlePendingOrphandedGLBufferObjects();
void deleteAllGLBufferObjects();
void discardAllGLBufferObjects();
void flushAllDeletedGLBufferObjects();
void discardAllDeletedGLBufferObjects();
void flushDeletedGLBufferObjects(double currentTime, double& availableTime);

View File

@ -26,6 +26,10 @@ extern OSG_EXPORT void flushDeletedGLObjects(unsigned int contextID, double curr
* Note, must be called from a thread which has current the graphics context associated with contextID. */
extern OSG_EXPORT void flushAllDeletedGLObjects(unsigned int contextID);
/** Do a GL delete all OpenGL objects.
* Note, must be called from a thread which has current the graphics context associated with contextID. */
extern OSG_EXPORT void deleteAllGLObjects(unsigned int contextID);
/** Discard all deleted OpenGL objects.
* Note, unlike flushAllDeletedObjectObjects discard does not
* do any OpenGL calls so can be called from any thread, but as a consequence it
@ -33,6 +37,13 @@ extern OSG_EXPORT void flushAllDeletedGLObjects(unsigned int contextID);
* called when the associated graphics context is being/has been closed. */
extern OSG_EXPORT void discardAllDeletedGLObjects(unsigned int contextID);
/** Discard all OpenGL objects.
* Note, unlike deletedAllObjectObjects discard does not
* do any OpenGL calls so can be called from any thread, but as a consequence it
* also doesn't remove the associated OpenGL resource so discard should only be
* called when the associated graphics context is being/has been closed. */
extern OSG_EXPORT void discardAllGLObjects(unsigned int contextID);
}
#endif

View File

@ -983,6 +983,7 @@ class OSG_EXPORT Texture : public osg::StateAttribute
_profile.match(target,numMipmapLevels,internalFormat,width,height,depth,border);
}
void bind();
inline GLenum id() const { return _id; }
@ -1019,6 +1020,10 @@ class OSG_EXPORT Texture : public osg::StateAttribute
bool _allocated;
unsigned int _frameLastUsed;
double _timeStamp;
protected:
virtual ~TextureObject();
};
typedef std::list< ref_ptr<TextureObject> > TextureObjectList;
@ -1029,6 +1034,9 @@ class OSG_EXPORT Texture : public osg::StateAttribute
TextureObjectSet(TextureObjectManager* parent, const TextureProfile& profile);
void handlePendingOrphandedTextureObjects();
void deleteAllTextureObjects();
void discardAllTextureObjects();
void flushAllDeletedTextureObjects();
void discardAllDeletedTextureObjects();
void flushDeletedTextureObjects(double currentTime, double& availableTime);
@ -1104,6 +1112,8 @@ class OSG_EXPORT Texture : public osg::StateAttribute
GLsizei depth,
GLint border);
void handlePendingOrphandedTextureObjects();
void deleteAllTextureObjects();
void discardAllTextureObjects();
void flushAllDeletedTextureObjects();
void discardAllDeletedTextureObjects();
void flushDeletedTextureObjects(double currentTime, double& availableTime);
@ -1173,12 +1183,11 @@ class OSG_EXPORT Texture : public osg::StateAttribute
/** Get the minimum number of display lists to retain in the deleted display list cache. */
static unsigned int getMinimumNumberOfTextureObjectsToRetainInCache();
static void deleteAllTextureObjects(unsigned int contextID);
static void discardAllTextureObjects(unsigned int contextID);
static void flushAllDeletedTextureObjects(unsigned int contextID);
static void discardAllDeletedTextureObjects(unsigned int contextID);
static void flushDeletedTextureObjects(unsigned int contextID,double currentTime, double& availableTime);
static void releaseTextureObject(unsigned int contextID, TextureObject* to);
protected:

View File

@ -60,10 +60,13 @@ GLBufferObject::GLBufferObject(unsigned int contextID, BufferObject* bufferObjec
assign(bufferObject);
_extensions = GLBufferObject::getExtensions(contextID, true);
_extensions->glGenBuffers(1, &_glObjectID);
osg::notify(osg::NOTICE)<<"Constucting BufferObject "<<this<<std::endl;
}
GLBufferObject::~GLBufferObject()
{
osg::notify(osg::NOTICE)<<"Destucting BufferObject "<<this<<std::endl;
}
void GLBufferObject::bindBuffer()
@ -541,6 +544,37 @@ void GLBufferObjectSet::handlePendingOrphandedGLBufferObjects()
checkConsistency();
}
void GLBufferObjectSet::deleteAllGLBufferObjects()
{
osg::notify(osg::NOTICE)<<"GLBufferObjectSet::deleteAllGLBufferObjects() not implemented yet."<<std::endl;
}
void GLBufferObjectSet::discardAllGLBufferObjects()
{
osg::notify(osg::NOTICE)<<"GLBufferObjectSet::discardAllGLBufferObjects()"<<std::endl;
GLBufferObject* to = _head;
while(to!=0)
{
ref_ptr<GLBufferObject> glbo = to;
to = to->_next;
ref_ptr<BufferObject> original_BufferObject = glbo->getBufferObject();
if (original_BufferObject.valid())
{
// detect the GLBufferObject from the BufferObject
original_BufferObject->setGLBufferObject(_contextID,0);
}
}
// the linked list should now be empty
_head = 0;
_pendingOrphanedGLBufferObjects.clear();
_orphanedGLBufferObjects.clear();
}
void GLBufferObjectSet::flushAllDeletedGLBufferObjects()
{
for(GLBufferObjectList::iterator itr = _orphanedGLBufferObjects.begin();
@ -566,6 +600,15 @@ void GLBufferObjectSet::flushAllDeletedGLBufferObjects()
void GLBufferObjectSet::discardAllDeletedGLBufferObjects()
{
osg::notify(osg::NOTICE)<<"GLBufferObjectSet::discardAllDeletedGLBufferObjects()"<<std::endl;
// clean up the pending orphans.
handlePendingOrphandedGLBufferObjects();
osg::notify(osg::NOTICE)<<" _orphanedGLBufferObjects.size() = "<<_orphanedGLBufferObjects.size()<<std::endl;
osg::notify(osg::NOTICE)<<" _pendingOrphanedGLBufferObjects.size() = "<<_pendingOrphanedGLBufferObjects.size()<<std::endl;
unsigned int numDiscarded = _orphanedGLBufferObjects.size();
_numOfGLBufferObjects -= numDiscarded;
@ -832,7 +875,7 @@ void GLBufferObjectSet::addToBack(GLBufferObject* to)
void GLBufferObjectSet::orphan(GLBufferObject* to)
{
// osg::notify(osg::NOTICE)<<"GLBufferObjectSet::orphan("<<to<<")"<<std::endl;
osg::notify(osg::NOTICE)<<"GLBufferObjectSet::orphan("<<to<<")"<<std::endl;
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
@ -846,7 +889,7 @@ void GLBufferObjectSet::orphan(GLBufferObject* to)
// to avoid having to mutex the process of appling active TO's.
_pendingOrphanedGLBufferObjects.push_back(to);
#if 0
#if 1
osg::notify(osg::NOTICE)<<"GLBufferObjectSet::orphan("<<to<<") _pendingOrphanedGLBufferObjects.size()="<<_pendingOrphanedGLBufferObjects.size()<<std::endl;
osg::notify(osg::NOTICE)<<" _orphanedGLBufferObjects.size()="<<_orphanedGLBufferObjects.size()<<std::endl;
#endif
@ -969,6 +1012,28 @@ void GLBufferObjectManager::handlePendingOrphandedGLBufferObjects()
}
}
void GLBufferObjectManager::deleteAllGLBufferObjects()
{
ElapsedTime elapsedTime(&(getDeleteTime()));
for(GLBufferObjectSetMap::iterator itr = _glBufferObjectSetMap.begin();
itr != _glBufferObjectSetMap.end();
++itr)
{
(*itr).second->deleteAllGLBufferObjects();
}
}
void GLBufferObjectManager::discardAllGLBufferObjects()
{
for(GLBufferObjectSetMap::iterator itr = _glBufferObjectSetMap.begin();
itr != _glBufferObjectSetMap.end();
++itr)
{
(*itr).second->discardAllGLBufferObjects();
}
}
void GLBufferObjectManager::flushAllDeletedGLBufferObjects()
{
ElapsedTime elapsedTime(&(getDeleteTime()));
@ -1056,6 +1121,16 @@ GLBufferObject* GLBufferObject::createGLBufferObject(unsigned int contextID, con
return GLBufferObjectManager::getGLBufferObjectManager(contextID)->generateGLBufferObject(bufferObject);
}
void GLBufferObject::deleteAllBufferObjects(unsigned int contextID)
{
GLBufferObjectManager::getGLBufferObjectManager(contextID)->deleteAllGLBufferObjects();
}
void GLBufferObject::discardAllBufferObjects(unsigned int contextID)
{
GLBufferObjectManager::getGLBufferObjectManager(contextID)->discardAllGLBufferObjects();
}
void GLBufferObject::flushAllDeletedBufferObjects(unsigned int contextID)
{
GLBufferObjectManager::getGLBufferObjectManager(contextID)->flushAllDeletedGLBufferObjects();
@ -1118,6 +1193,7 @@ void BufferObject::resizeGLObjectBuffers(unsigned int maxSize)
void BufferObject::releaseGLObjects(State* state) const
{
osg::notify(osg::NOTICE)<<"BufferObject::releaseGLObjects("<<state<<")"<<std::endl;
if (state)
{
unsigned int contextID = state->getContextID();
@ -1133,6 +1209,7 @@ void BufferObject::releaseGLObjects(State* state) const
{
if (_glBufferObjects[i].valid())
{
osg::notify(osg::NOTICE)<<" GLBufferObject::releaseGLBufferObject("<<i<<", _glBufferObjects["<<i<<"]="<<_glBufferObjects[i].get()<<")"<<std::endl;
GLBufferObject::releaseGLBufferObject(i, _glBufferObjects[i].get());
_glBufferObjects[i] = 0;
}

View File

@ -55,16 +55,43 @@ void osg::flushAllDeletedGLObjects(unsigned int contextID)
osg::VertexProgram::flushDeletedVertexProgramObjects(contextID,currentTime,availableTime);
#endif
osg::GLBufferObject::flushDeletedBufferObjects(contextID,currentTime,availableTime);
osg::GLBufferObject::flushAllDeletedBufferObjects(contextID);
osg::Texture::flushAllDeletedTextureObjects(contextID);
osg::FrameBufferObject::flushDeletedFrameBufferObjects(contextID,currentTime,availableTime);
osg::Program::flushDeletedGlPrograms(contextID,currentTime,availableTime);
osg::RenderBuffer::flushDeletedRenderBuffers(contextID,currentTime,availableTime);
osg::Shader::flushDeletedGlShaders(contextID,currentTime,availableTime);
osg::Texture::flushAllDeletedTextureObjects(contextID);
osg::OcclusionQueryNode::flushDeletedQueryObjects(contextID,currentTime,availableTime);
}
void osg::discardAllDeletedGLObjects(unsigned int contextID)
void osg::deleteAllGLObjects(unsigned int contextID)
{
double currentTime = DBL_MAX;
double availableTime = DBL_MAX;
#ifdef OSG_GL_DISPLAYLISTS_AVAILABLE
osg::Drawable::flushAllDeletedDisplayLists(contextID);
#endif
#ifdef OSG_GL_FIXED_FUNCTION_AVAILABLE
osg::FragmentProgram::flushDeletedFragmentProgramObjects(contextID,currentTime,availableTime);
osg::VertexProgram::flushDeletedVertexProgramObjects(contextID,currentTime,availableTime);
#endif
osg::GLBufferObject::deleteAllBufferObjects(contextID);
osg::Texture::deleteAllTextureObjects(contextID);
osg::FrameBufferObject::flushDeletedFrameBufferObjects(contextID,currentTime,availableTime);
osg::Program::flushDeletedGlPrograms(contextID,currentTime,availableTime);
osg::RenderBuffer::flushDeletedRenderBuffers(contextID,currentTime,availableTime);
osg::Shader::flushDeletedGlShaders(contextID,currentTime,availableTime);
osg::OcclusionQueryNode::flushDeletedQueryObjects(contextID,currentTime,availableTime);
}
void osg::discardAllGLObjects(unsigned int contextID)
{
#ifdef OSG_GL_DISPLAYLISTS_AVAILABLE
osg::Drawable::discardAllDeletedDisplayLists(contextID);
@ -75,11 +102,12 @@ void osg::discardAllDeletedGLObjects(unsigned int contextID)
osg::VertexProgram::discardDeletedVertexProgramObjects(contextID);
#endif
osg::GLBufferObject::discardAllDeletedBufferObjects(contextID);
osg::GLBufferObject::discardAllBufferObjects(contextID);
osg::Texture::discardAllTextureObjects(contextID);
osg::FrameBufferObject::discardDeletedFrameBufferObjects(contextID);
osg::Program::discardDeletedGlPrograms(contextID);
osg::RenderBuffer::discardDeletedRenderBuffers(contextID);
osg::Shader::discardDeletedGlShaders(contextID);
osg::Texture::discardAllDeletedTextureObjects(contextID);
osg::OcclusionQueryNode::discardDeletedQueryObjects(contextID);
}

View File

@ -531,11 +531,11 @@ void GraphicsContext::close(bool callCloseImplementation)
if (makeCurrent())
{
osg::notify(osg::INFO)<<"Doing Flush"<<std::endl;
osg::notify(osg::INFO)<<"Doing delete of GL objects"<<std::endl;
osg::flushAllDeletedGLObjects(_state->getContextID());
osg::deleteAllGLObjects(_state->getContextID());
osg::notify(osg::INFO)<<"Done Flush "<<std::endl;
osg::notify(osg::INFO)<<"Done delete of GL objects"<<std::endl;
_state->reset();
@ -557,7 +557,7 @@ void GraphicsContext::close(bool callCloseImplementation)
{
osg::notify(osg::INFO)<<"Doing discard of deleted OpenGL objects."<<std::endl;
osg::discardAllDeletedGLObjects(_state->getContextID());
osg::discardAllGLObjects(_state->getContextID());
}
if (_state.valid())

View File

@ -72,6 +72,11 @@ unsigned int Texture::getMinimumNumberOfTextureObjectsToRetainInCache()
#define USE_NEW_TEXTURE_POOL 1
Texture::TextureObject::~TextureObject()
{
osg::notify(osg::NOTICE)<<"Texture::TextureObject::~TextureObject() "<<this<<std::endl;
}
void Texture::TextureObject::bind()
{
glBindTexture( _profile._target, _id);
@ -218,7 +223,7 @@ bool Texture::TextureObjectSet::checkConsistency() const
void Texture::TextureObjectSet::handlePendingOrphandedTextureObjects()
{
// osg::notify(osg::NOTICE)<<"handlePendingOrphandedTextureObjects()"<<_pendingOrphanedTextureObjects.size()<<std::endl;
osg::notify(osg::NOTICE)<<"handlePendingOrphandedTextureObjects()"<<_pendingOrphanedTextureObjects.size()<<std::endl;
if (_pendingOrphanedTextureObjects.empty()) return;
@ -253,8 +258,42 @@ void Texture::TextureObjectSet::handlePendingOrphandedTextureObjects()
checkConsistency();
}
void Texture::TextureObjectSet::deleteAllTextureObjects()
{
osg::notify(osg::NOTICE)<<"Texture::TextureObjectSet::deleteAllTextureObjects() not implemented yet."<<std::endl;
}
void Texture::TextureObjectSet::discardAllTextureObjects()
{
osg::notify(osg::NOTICE)<<"Texture::TextureObjectSet::discardAllTextureObjects()."<<std::endl;
TextureObject* to = _head;
while(to!=0)
{
ref_ptr<TextureObject> glto = to;
to = to->_next;
ref_ptr<Texture> original_texture = glto->getTexture();
if (original_texture.valid())
{
original_texture->setTextureObject(_contextID,0);
}
}
// the linked list should now be empty
_head = 0;
_pendingOrphanedTextureObjects.clear();
_orphanedTextureObjects.clear();
}
void Texture::TextureObjectSet::flushAllDeletedTextureObjects()
{
// osg::notify(osg::NOTICE)<<"Texture::TextureObjectSet::flushAllDeletedTextureObjects()"<<std::endl;
for(TextureObjectList::iterator itr = _orphanedTextureObjects.begin();
itr != _orphanedTextureObjects.end();
++itr)
@ -280,6 +319,15 @@ void Texture::TextureObjectSet::flushAllDeletedTextureObjects()
void Texture::TextureObjectSet::discardAllDeletedTextureObjects()
{
osg::notify(osg::NOTICE)<<"Texture::TextureObjectSet::discardAllDeletedTextureObjects()"<<std::endl;
// clean up the pending orphans.
handlePendingOrphandedTextureObjects();
osg::notify(osg::NOTICE)<<" _orphanedTextureObjects.size() = "<<_orphanedTextureObjects.size()<<std::endl;
osg::notify(osg::NOTICE)<<" _pendingOrphanedTextureObjects.size() = "<<_pendingOrphanedTextureObjects.size()<<std::endl;
unsigned int numDiscarded = _orphanedTextureObjects.size();
_numOfTextureObjects -= numDiscarded;
@ -299,7 +347,7 @@ void Texture::TextureObjectSet::discardAllDeletedTextureObjects()
void Texture::TextureObjectSet::flushDeletedTextureObjects(double currentTime, double& availableTime)
{
// osg::notify(osg::NOTICE)<<"Texture::TextureObjectSet::flushDeletedTextureObjects(..) Not properly implemented yet"<<std::endl;
// osg::notify(osg::NOTICE)<<"Texture::TextureObjectSet::flushDeletedTextureObjects(..)"<<std::endl;
// if nothing to delete return
if (_orphanedTextureObjects.empty()) return;
@ -696,8 +744,36 @@ void Texture::TextureObjectManager::handlePendingOrphandedTextureObjects()
}
}
void Texture::TextureObjectManager::deleteAllTextureObjects()
{
osg::notify(osg::NOTICE)<<"Texture::TextureObjectManager::deleteAllTextureObjects() _contextID="<<_contextID<<std::endl;
ElapsedTime elapsedTime(&(getDeleteTime()));
for(TextureSetMap::iterator itr = _textureSetMap.begin();
itr != _textureSetMap.end();
++itr)
{
(*itr).second->deleteAllTextureObjects();
}
}
void Texture::TextureObjectManager::discardAllTextureObjects()
{
osg::notify(osg::NOTICE)<<"Texture::TextureObjectManager::discardAllTextureObjects() _contextID="<<_contextID<<" _numActiveTextureObjects="<<_numActiveTextureObjects<<std::endl;
for(TextureSetMap::iterator itr = _textureSetMap.begin();
itr != _textureSetMap.end();
++itr)
{
(*itr).second->discardAllTextureObjects();
}
}
void Texture::TextureObjectManager::flushAllDeletedTextureObjects()
{
osg::notify(osg::NOTICE)<<"Texture::TextureObjectManager::flushAllDeletedTextureObjects() _contextID="<<_contextID<<std::endl;
ElapsedTime elapsedTime(&(getDeleteTime()));
for(TextureSetMap::iterator itr = _textureSetMap.begin();
@ -710,6 +786,8 @@ void Texture::TextureObjectManager::flushAllDeletedTextureObjects()
void Texture::TextureObjectManager::discardAllDeletedTextureObjects()
{
osg::notify(osg::NOTICE)<<"Texture::TextureObjectManager::discardAllDeletedTextureObjects() _contextID="<<_contextID<<" _numActiveTextureObjects="<<_numActiveTextureObjects<<std::endl;
for(TextureSetMap::iterator itr = _textureSetMap.begin();
itr != _textureSetMap.end();
++itr)
@ -798,6 +876,16 @@ Texture::TextureObject* Texture::generateTextureObject(const Texture* texture, u
return getTextureObjectManager(contextID)->generateTextureObject(texture,target,numMipmapLevels,internalFormat,width,height,depth,border);
}
void Texture::deleteAllTextureObjects(unsigned int contextID)
{
getTextureObjectManager(contextID)->deleteAllTextureObjects();
}
void Texture::discardAllTextureObjects(unsigned int contextID)
{
getTextureObjectManager(contextID)->discardAllTextureObjects();
}
void Texture::flushAllDeletedTextureObjects(unsigned int contextID)
{
getTextureObjectManager(contextID)->flushAllDeletedTextureObjects();