From 161246d864ea0514543ed0493422e1bf0e99afb7 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 23 Sep 2015 09:47:34 +0000 Subject: [PATCH] Refactored the GL object deletion management to use new osg::GraphicsObjectManager/GLObjectManager base classes, and osg::ContextData container. This approach unifies much of the code handling the clean up of OpenGL graphics data, avoids lots of local mutexes and static variables that were previously required, and enables the clean up scheme to be easily extended by users providing their own GraphicsObjectManager subclasses. git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@15130 16af8721-9629-0410-8352-f15c8da7e697 --- examples/osganalysis/osganalysis.cpp | 24 +- examples/osgfpdepth/osgfpdepth.cpp | 10 +- include/osg/BufferObject | 43 ++-- include/osg/ContextData | 163 +++++++++++++ include/osg/Drawable | 26 +-- include/osg/FragmentProgram | 17 -- include/osg/FrameBufferObject | 76 +++--- include/osg/GLObjects | 92 +++++++- include/osg/Program | 17 +- include/osg/Shader | 15 -- include/osg/State | 12 +- include/osg/Texture | 299 ++++++++++++------------ include/osg/VertexProgram | 19 -- src/CMakeLists.txt | 6 +- src/osg/BufferObject.cpp | 86 ++----- src/osg/CMakeLists.txt | 2 + src/osg/ContextData.cpp | 335 +++++++++++++++++++++++++++ src/osg/Drawable.cpp | 321 +++++++++++++------------ src/osg/FragmentProgram.cpp | 72 +----- src/osg/FrameBufferObject.cpp | 152 +++--------- src/osg/GLObjects.cpp | 197 +++++++++------- src/osg/GraphicsContext.cpp | 166 ++----------- src/osg/OcclusionQueryNode.cpp | 65 ++---- src/osg/Program.cpp | 64 +---- src/osg/Shader.cpp | 75 ++---- src/osg/State.cpp | 5 +- src/osg/Texture.cpp | 216 +++++++---------- src/osg/Texture1D.cpp | 6 +- src/osg/Texture2D.cpp | 7 +- src/osg/Texture2DArray.cpp | 6 +- src/osg/Texture2DMultisample.cpp | 4 - src/osg/Texture3D.cpp | 6 +- src/osg/TextureCubeMap.cpp | 6 +- src/osg/TextureRectangle.cpp | 7 +- src/osg/VertexProgram.cpp | 72 +----- src/osgUtil/RenderStage.cpp | 15 +- src/osgUtil/SceneView.cpp | 9 +- 37 files changed, 1339 insertions(+), 1374 deletions(-) create mode 100644 include/osg/ContextData create mode 100644 src/osg/ContextData.cpp diff --git a/examples/osganalysis/osganalysis.cpp b/examples/osganalysis/osganalysis.cpp index a420c8297..1480eb851 100644 --- a/examples/osganalysis/osganalysis.cpp +++ b/examples/osganalysis/osganalysis.cpp @@ -20,6 +20,8 @@ #include #include +#include + #include #include #include @@ -504,7 +506,7 @@ protected: optimizeVertexOrder = false; reallocateMemory = false; - + modifyTextureSettings = false; buildImageMipmaps = false; compressImages = false; @@ -524,21 +526,21 @@ protected: bool optimizeVertexOrder; bool reallocateMemory; - + bool modifyTextureSettings; bool buildImageMipmaps; bool compressImages; bool disableMipmaps; }; -// +// class DatabasePagingOperation : public osg::Operation, public osgUtil::IncrementalCompileOperation::CompileCompletedCallback { public: DatabasePagingOperation(const std::string& filename, const std::string& outputFilename, - SceneGraphProcessor* sceneGraphProcessor, + SceneGraphProcessor* sceneGraphProcessor, osgUtil::IncrementalCompileOperation* ico): osg::Referenced(true), Operation("DatabasePaging Operation", false), @@ -570,7 +572,7 @@ public: if (!_outputFilename.empty()) { OSG_NOTICE<<"Writing out file "<<_outputFilename<reportStats(osg::notify(osg::NOTICE)); - osg::GLBufferObjectManager::getGLBufferObjectManager(0)->reportStats(osg::notify(osg::NOTICE)); + osg::getOrCreateContextData(0)->reportStats(osg::notify(osg::NOTICE)); } } return false; @@ -631,8 +632,7 @@ struct ReportStatsAnimationCompletedCallback : public osgGA::AnimationPathManipu virtual void completed(const osgGA::AnimationPathManipulator*) { OSG_NOTICE<<"Animation completed"<reportStats(osg::notify(osg::NOTICE)); - osg::GLBufferObjectManager::getGLBufferObjectManager(0)->reportStats(osg::notify(osg::NOTICE)); + osg::getOrCreateContextData(0)->reportStats(osg::notify(osg::NOTICE)); } }; @@ -664,7 +664,7 @@ int main(int argc, char** argv) { apm->setTimeScale(animationSpeed); apm->setAnimationCompletedCallback(new ReportStatsAnimationCompletedCallback()); - + unsigned int num = keyswitchManipulator->getNumMatrixManipulators(); keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm ); keyswitchManipulator->selectMatrixManipulator(num); @@ -675,7 +675,7 @@ int main(int argc, char** argv) viewer.setCameraManipulator( keyswitchManipulator.get() ); } - // set up event handlers + // set up event handlers { viewer.addEventHandler( new osgViewer::StatsHandler()); viewer.addEventHandler( new osgViewer::WindowSizeHandler() ); @@ -797,7 +797,7 @@ int main(int argc, char** argv) if (databasePagingOperation.get() && databasePagingOperation->_modelReadyToMerge) { OSG_NOTICE<<"Merging subgraph"<removeChildren(0,group->getNumChildren()); diff --git a/examples/osgfpdepth/osgfpdepth.cpp b/examples/osgfpdepth/osgfpdepth.cpp index 86541bc30..ecf1671eb 100644 --- a/examples/osgfpdepth/osgfpdepth.cpp +++ b/examples/osgfpdepth/osgfpdepth.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -313,12 +314,9 @@ void destroyFBO(GraphicsContext* gc, FboData &data) data.fb = 0; data.resolveFB = 0; State& state = *gc->getState(); - double availableTime = 100.0; - RenderBuffer::flushDeletedRenderBuffers(state.getContextID(), 0.0, - availableTime); - availableTime = 100.0; - FrameBufferObject::flushDeletedFrameBufferObjects(state.getContextID(), - 0.0, availableTime); + + osg::get(state.getContextID())->flushAllDeletedGLObjects(); + osg::get(state.getContextID())->flushAllDeletedGLObjects(); } void setAttachmentsFromConfig(Camera* camera, const FboConfig& config); diff --git a/include/osg/BufferObject b/include/osg/BufferObject index a729743ca..6192dd21f 100644 --- a/include/osg/BufferObject +++ b/include/osg/BufferObject @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -151,7 +152,7 @@ class BufferObjectProfile class GLBufferObjectSet; class GLBufferObjectManager; -class OSG_EXPORT GLBufferObject : public Referenced +class OSG_EXPORT GLBufferObject : public GraphicsObject { public: @@ -207,6 +208,9 @@ class OSG_EXPORT GLBufferObject : public Referenced _extensions->glBindBuffer(_profile._target,0); } + /** release GLBufferObject to the orphan list to be reused or deleted.*/ + void release(); + inline bool isDirty() const { return _dirty; } void dirty() { _dirty = true; } @@ -221,15 +225,6 @@ class OSG_EXPORT GLBufferObject : public Referenced bool isPBOSupported() const { return _extensions->isPBOSupported; } - static osg::ref_ptr createGLBufferObject(unsigned int contextID, const BufferObject* bufferObject); - - 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); - static void releaseGLBufferObject(unsigned int contextID, GLBufferObject* to); - bool hasAllBufferDataBeenRead() const; void setBufferDataHasBeenRead(const osg::BufferData* bd); @@ -327,14 +322,11 @@ class OSG_EXPORT GLBufferObjectSet : public Referenced GLBufferObject* _tail; }; -class OSG_EXPORT GLBufferObjectManager : public osg::Referenced +class OSG_EXPORT GLBufferObjectManager : public GraphicsObjectManager { public: GLBufferObjectManager(unsigned int contextID); - unsigned int getContextID() const { return _contextID; } - - void setNumberActiveGLBufferObjects(unsigned int size) { _numActiveGLBufferObjects = size; } unsigned int& getNumberActiveGLBufferObjects() { return _numActiveGLBufferObjects; } unsigned int getNumberActiveGLBufferObjects() const { return _numActiveGLBufferObjects; } @@ -357,12 +349,11 @@ class OSG_EXPORT GLBufferObjectManager : public osg::Referenced void handlePendingOrphandedGLBufferObjects(); - void deleteAllGLBufferObjects(); - void discardAllGLBufferObjects(); - void flushAllDeletedGLBufferObjects(); - void discardAllDeletedGLBufferObjects(); - void flushDeletedGLBufferObjects(double currentTime, double& availableTime); - void releaseGLBufferObject(GLBufferObject* to); + void deleteAllGLObjects(); + void discardAllGLObjects(); + void flushAllDeletedGLObjects(); + void discardAllDeletedGLObjects(); + void flushDeletedGLObjects(double currentTime, double& availableTime); GLBufferObjectSet* getGLBufferObjectSet(const BufferObjectProfile& profile); @@ -383,12 +374,12 @@ class OSG_EXPORT GLBufferObjectManager : public osg::Referenced unsigned int& getNumberApplied() { return _numApplied; } double& getApplyTime() { return _applyTime; } - static osg::ref_ptr& getGLBufferObjectManager(unsigned int contextID); - protected: + virtual ~GLBufferObjectManager(); + typedef std::map< BufferObjectProfile, osg::ref_ptr > GLBufferObjectSetMap; - unsigned int _contextID; + unsigned int _numActiveGLBufferObjects; unsigned int _numOrphanedGLBufferObjects; unsigned int _currGLBufferObjectPoolSize; @@ -471,11 +462,7 @@ class OSG_EXPORT BufferObject : public Object GLBufferObject* getGLBufferObject(unsigned int contextID) const { return _glBufferObjects[contextID].get(); } - GLBufferObject* getOrCreateGLBufferObject(unsigned int contextID) const - { - if (!_glBufferObjects[contextID]) _glBufferObjects[contextID] = GLBufferObject::createGLBufferObject(contextID, this); - return _glBufferObjects[contextID].get(); - } + GLBufferObject* getOrCreateGLBufferObject(unsigned int contextID) const; unsigned int computeRequiredBufferSize() const; diff --git a/include/osg/ContextData b/include/osg/ContextData new file mode 100644 index 000000000..c52d32e9d --- /dev/null +++ b/include/osg/ContextData @@ -0,0 +1,163 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#ifndef OSG_CONTEXTDATA +#define OSG_CONTEXTDATA 1 + +#include + +namespace osg { + +class OSG_EXPORT ContextData : public GraphicsObjectManager +{ + public: + ContextData(unsigned int contextID); + + void incrementUsageCount() { ++_numContexts; } + void decrementUsageCount() { --_numContexts; } + + void setNumContexts(unsigned int numContexts) { _numContexts = numContexts; } + unsigned int getNumContexts() const { return _numContexts; } + + void setCompileContext(osg::GraphicsContext* gc) { _compileContext = gc; } + osg::GraphicsContext* getCompileContext() { return _compileContext.get(); } + + /** Get a specific GL extensions object or GraphicsObjectManager, initialize if not already present. + * Note, must only be called from a the graphics context thread associated with this osg::State. */ + template + T* get() + { + const std::type_info* id(&typeid(T)); + osg::ref_ptr& ptr = _managerMap[id]; + if (!ptr) + { + ptr = new T(_contextID); + } + return static_cast(ptr.get()); + } + + /** Get a specific GL extensions object or GraphicsObjectManager if it already exists in the extension map. + * Note, safe to call outwith a the graphics context thread associated with this osg::State. + * Returns NULL if the desired extension object has not been created yet.*/ + template + const T* get() const + { + const std::type_info* id(&typeid(T)); + ManagerMap::const_iterator itr = _managerMap.find(id); + if (itr==_managerMap.end()) return 0; + else return itr->second.get(); + } + + /** Set a specific GL extensions object pr GraphicsObjectManager. */ + template + void set(T* ptr) + { + const std::type_info* id(&typeid(T)); + _managerMap[id] = ptr; + } + + /** Signal that a new frame has started.*/ + virtual void newFrame(osg::FrameStamp*); + + virtual void resetStats(); + virtual void reportStats(std::ostream& out); + virtual void recomputeStats(std::ostream& out) const; + + /** Flush all deleted OpenGL objects within the specified availableTime. + * Note, must be called from a thread which has current the graphics context associated with contextID. */ + virtual void flushDeletedGLObjects(double currentTime, double& availableTime); + + /** Flush all deleted OpenGL objects. + * Note, must be called from a thread which has current the graphics context associated with contextID. */ + virtual void flushAllDeletedGLObjects(); + + /** Do a GL delete all OpenGL objects. + * Note, must be called from a thread which has current the graphics context associated with contextID. */ + virtual void deleteAllGLObjects(); + + /** Discard all OpenGL objects. + * Note, unlike deleteAllGLjects 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. */ + virtual void discardAllGLObjects(); + + public: + + /** Create a contextID for a new graphics context, this contextID is used to set up the osg::State associate with context. + * Automatically increments the usage count of the contextID to 1.*/ + static unsigned int createNewContextID(); + + /** Get the current max ContextID.*/ + static unsigned int getMaxContextID(); + + /** Increment the usage count associate with a contextID. The usage count specifies how many graphics contexts a specific contextID is shared between.*/ + static void incrementContextIDUsageCount(unsigned int contextID); + + /** Decrement the usage count associate with a contextID. Once the contextID goes to 0 the contextID is then free to be reused.*/ + static void decrementContextIDUsageCount(unsigned int contextID); + + typedef GraphicsContext::GraphicsContexts GraphicsContexts; + + /** Get all the registered graphics contexts.*/ + static GraphicsContexts getAllRegisteredGraphicsContexts(); + + /** Get all the registered graphics contexts associated with a specific contextID.*/ + static GraphicsContexts getRegisteredGraphicsContexts(unsigned int contextID); + + /** Get the GraphicsContext for doing background compilation for GraphicsContexts associated with specified contextID.*/ + static void setCompileContext(unsigned int contextID, GraphicsContext* gc); + + /** Get existing or create a new GraphicsContext to do background compilation for GraphicsContexts associated with specified contextID.*/ + static GraphicsContext* getOrCreateCompileContext(unsigned int contextID); + + /** Get the GraphicsContext for doing background compilation for GraphicsContexts associated with specified contextID.*/ + static GraphicsContext* getCompileContext(unsigned int contextID); + + /** Register a GraphicsContext.*/ + static void registerGraphicsContext(GraphicsContext* gc); + + /** Unregister a GraphicsContext.*/ + static void unregisterGraphicsContext(GraphicsContext* gc); + + protected: + virtual ~ContextData(); + + unsigned int _numContexts; + osg::ref_ptr _compileContext; + + // ManagerMap contains GL Extentsions objects used by StateAttribue to call OpenGL extensions/advanced features + typedef std::map > ManagerMap; + ManagerMap _managerMap; +}; + + +/** Get the ContextData for a specific contextID.*/ +extern ContextData* getContextData(unsigned int contextID); + +/** Get or create the ContextData for a specific contextID.*/ +extern ContextData* getOrCreateContextData(unsigned int contextID); + +template +inline T* get(unsigned int contextID) +{ + ContextData* gc = getOrCreateContextData(contextID); + return gc->get(); +} + +// specialize for ContextData to avoid ContextData being nested within itself. +template<> inline ContextData* get(unsigned int contextID) { return getOrCreateContextData(contextID); } + +} + +#endif diff --git a/include/osg/Drawable b/include/osg/Drawable index c88492bc3..2c27d1283 100644 --- a/include/osg/Drawable +++ b/include/osg/Drawable @@ -95,10 +95,6 @@ class OSG_EXPORT Drawable : public Node { public: - static unsigned int s_numberDrawablesReusedLastInLastFrame; - static unsigned int s_numberNewDrawablesInLastFrame; - static unsigned int s_numberDeletedDrawablesInLastFrame; - Drawable(); /** Copy constructor using CopyOp to manage deep vs shallow copy.*/ @@ -355,31 +351,19 @@ class OSG_EXPORT Drawable : public Node /** Return a OpenGL display list handle a newly generated or reused from display list cache. */ static GLuint generateDisplayList(unsigned int contextID, unsigned int sizeHint = 0); - /** Set the minimum number of display lists to retain in the deleted display list cache. */ - static void setMinimumNumberOfDisplayListsToRetainInCache(unsigned int minimum); - - /** Get the minimum number of display lists to retain in the deleted display list cache. */ - static unsigned int getMinimumNumberOfDisplayListsToRetainInCache(); - /** Use deleteDisplayList instead of glDeleteList to allow * OpenGL display list to be cached until they can be deleted * by the OpenGL context in which they were created, specified * by contextID.*/ static void deleteDisplayList(unsigned int contextID,GLuint globj, unsigned int sizeHint = 0); - /** Flush all the cached display list which need to be deleted - * in the OpenGL context related to contextID.*/ - static void flushAllDeletedDisplayLists(unsigned int contextID); + /** Set the minimum number of display lists to retain in the deleted display list cache. */ + static void setMinimumNumberOfDisplayListsToRetainInCache(unsigned int minimum); + + /** Get the minimum number of display lists to retain in the deleted display list cache. */ + static unsigned int getMinimumNumberOfDisplayListsToRetainInCache(); - /** Flush all the cached display list which need to be deleted - * in the OpenGL context related to contextID. - * Note, unlike flush no OpenGL calls are made, instead the handles are all removed. - * this call is useful for when an OpenGL context has been destroyed. */ - static void discardAllDeletedDisplayLists(unsigned int contextID); - /** Flush the cached display list which need to be deleted - * in the OpenGL context related to contextID.*/ - static void flushDeletedDisplayLists(unsigned int contextID,double& availableTime); typedef unsigned int AttributeType; diff --git a/include/osg/FragmentProgram b/include/osg/FragmentProgram index c9c02c07a..f3c335921 100644 --- a/include/osg/FragmentProgram +++ b/include/osg/FragmentProgram @@ -202,26 +202,9 @@ class OSG_EXPORT FragmentProgram : public StateAttribute /** Get list of Matrices */ inline const MatrixList& getMatrices() const { return _matrixList; } - /** Force a recompile on next apply() of associated OpenGL vertex program objects.*/ void dirtyFragmentProgramObject(); - /** use deleteFragmentProgramObject instead of glDeletePrograms to allow - * OpenGL Fragment Program objects to be cached until they can be deleted - * by the OpenGL context in which they were created, specified - * by contextID.*/ - static void deleteFragmentProgramObject(unsigned int contextID,GLuint handle); - - /** flush all the cached fragment programs which need to be deleted - * in the OpenGL context related to contextID.*/ - static void flushDeletedFragmentProgramObjects(unsigned int contextID,double currentTime, double& availableTime); - - /** discard all the cached fragment programs which need to be deleted - * in the OpenGL context related to contextID. - * Note, unlike flush no OpenGL calls are made, instead the handles are all removed. - * this call is useful for when an OpenGL context has been destroyed. */ - static void discardDeletedFragmentProgramObjects(unsigned int contextID); - virtual void apply(State& state) const; virtual void compileGLObjects(State& state) const { apply(state); } diff --git a/include/osg/FrameBufferObject b/include/osg/FrameBufferObject index e3abdced1..8ca508a37 100644 --- a/include/osg/FrameBufferObject +++ b/include/osg/FrameBufferObject @@ -147,20 +147,6 @@ class OSG_EXPORT RenderBuffer: public Object GLuint getObjectID(unsigned int contextID, const GLExtensions *ext) const; inline int compare(const RenderBuffer &rb) const; - /** Mark internal RenderBuffer for deletion. - * Deletion requests are queued until they can be executed - * in the proper GL context. */ - static void deleteRenderBuffer(unsigned int contextID, GLuint rb); - - /** flush all the cached RenderBuffers which need to be deleted - * in the OpenGL context related to contextID.*/ - static void flushDeletedRenderBuffers(unsigned int contextID,double currentTime, double& availableTime); - - /** discard all the cached RenderBuffers which need to be deleted in the OpenGL context related to contextID. - * Note, unlike flush no OpenGL calls are made, instead the handles are all removed. - * this call is useful for when an OpenGL context has been destroyed. */ - static void discardDeletedRenderBuffers(unsigned int contextID); - static int getMaxSamples(unsigned int contextID, const GLExtensions* ext); /** Resize any per context GLObject buffers to specified size. */ @@ -372,19 +358,6 @@ class OSG_EXPORT FrameBufferObject: public StateAttribute /** Bind the FBO as either the read or draw target, or both. */ void apply(State &state, BindTarget target) const; - /** Mark internal FBO for deletion. - * Deletion requests are queued until they can be executed - * in the proper GL context. */ - static void deleteFrameBufferObject(unsigned int contextID, GLuint program); - - /** flush all the cached FBOs which need to be deleted - * in the OpenGL context related to contextID.*/ - static void flushDeletedFrameBufferObjects(unsigned int contextID,double currentTime, double& availableTime); - - /** discard all the cached FBOs which need to be deleted - * in the OpenGL context related to contextID.*/ - static void discardDeletedFrameBufferObjects(unsigned int contextID); - /** Resize any per context GLObject buffers to specified size. */ virtual void resizeGLObjectBuffers(unsigned int maxSize); @@ -413,30 +386,43 @@ class OSG_EXPORT FrameBufferObject: public StateAttribute mutable buffered_value _unsupported; mutable buffered_value _fboID; - }; +}; - // INLINE METHODS +// INLINE METHODS - inline const FrameBufferObject::AttachmentMap &FrameBufferObject::getAttachmentMap() const - { - return _attachments; - } +inline const FrameBufferObject::AttachmentMap &FrameBufferObject::getAttachmentMap() const +{ + return _attachments; +} - inline bool FrameBufferObject::hasAttachment(FrameBufferObject::BufferComponent attachment_point) const - { - return _attachments.find(attachment_point) != _attachments.end(); - } +inline bool FrameBufferObject::hasAttachment(FrameBufferObject::BufferComponent attachment_point) const +{ + return _attachments.find(attachment_point) != _attachments.end(); +} - inline const FrameBufferAttachment &FrameBufferObject::getAttachment(FrameBufferObject::BufferComponent attachment_point) const - { - return _attachments.find(attachment_point)->second; - } +inline const FrameBufferAttachment &FrameBufferObject::getAttachment(FrameBufferObject::BufferComponent attachment_point) const +{ + return _attachments.find(attachment_point)->second; +} - inline void FrameBufferObject::dirtyAll() - { - _dirtyAttachmentList.setAllElementsTo(1); - } +inline void FrameBufferObject::dirtyAll() +{ + _dirtyAttachmentList.setAllElementsTo(1); +} +class OSG_EXPORT GLRenderBufferManager : public GLObjectManager +{ +public: + GLRenderBufferManager(unsigned int contextID); + virtual void deleteGLObject(GLuint globj); +}; + +class OSG_EXPORT GLFrameBufferObjectManager : public GLObjectManager +{ +public: + GLFrameBufferObjectManager(unsigned int contextID); + virtual void deleteGLObject(GLuint globj); +}; } diff --git a/include/osg/GLObjects b/include/osg/GLObjects index 55be13199..67b4e2da7 100644 --- a/include/osg/GLObjects +++ b/include/osg/GLObjects @@ -14,10 +14,16 @@ #ifndef OSG_GLOBJECTS #define OSG_GLOBJECTS 1 -#include +#include +#include +#include +#include namespace osg { +// forward declare +class FrameStamp; + /** Flush all deleted OpenGL objects within the specified availableTime. * Note, must be called from a thread which has current the graphics context associated with contextID. */ extern OSG_EXPORT void flushDeletedGLObjects(unsigned int contextID, double currentTime, double& availableTime); @@ -37,6 +43,90 @@ extern OSG_EXPORT void deleteAllGLObjects(unsigned int contextID); * called when the associated graphics context is being/has been closed. */ extern OSG_EXPORT void discardAllGLObjects(unsigned int contextID); +class OSG_EXPORT GraphicsObject : public osg::Referenced +{ + public: + GraphicsObject(); + + protected: + virtual ~GraphicsObject(); +}; + + +class OSG_EXPORT GraphicsObjectManager : public osg::Referenced +{ + public: + GraphicsObjectManager(const std::string& name, unsigned int contextID); + + unsigned int getContextID() const { return _contextID; } + + /** Signal that a new frame has started.*/ + virtual void newFrame(osg::FrameStamp* fs) {} + + virtual void resetStats() {} + virtual void reportStats(std::ostream& out) {} + virtual void recomputeStats(std::ostream& out) const {} + + + /** Flush all deleted OpenGL objects within the specified availableTime. + * Note, must be called from a thread which has current the graphics context associated with contextID. */ + virtual void flushDeletedGLObjects(double currentTime, double& availableTime) = 0; + + /** Flush all deleted OpenGL objects. + * Note, must be called from a thread which has current the graphics context associated with contextID. */ + virtual void flushAllDeletedGLObjects() = 0; + + /** Do a GL delete all OpenGL objects. + * Note, must be called from a thread which has current the graphics context associated with contextID. */ + virtual void deleteAllGLObjects() = 0; + + /** Discard all OpenGL objects. + * Note, unlike deleteAllGLjects 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. */ + virtual void discardAllGLObjects() = 0; + + protected: + virtual ~GraphicsObjectManager(); + + std::string _name; + unsigned int _contextID; + +}; + +class OSG_EXPORT GLObjectManager : public GraphicsObjectManager +{ +public: + GLObjectManager(const std::string& name, unsigned int contextID); + + virtual void flushDeletedGLObjects(double currentTime, double& availableTime); + + virtual void flushAllDeletedGLObjects(); + + virtual void deleteAllGLObjects(); + + virtual void discardAllGLObjects(); + + /** schedule a GL object for deletion by the graphics thread.*/ + virtual void sheduleGLObjectForDeletion(GLuint globj); + + /** implementation of the actual creation of an GL object - subclasses from GLObjectManager must implement the appropriate GL calls.*/ + virtual GLuint createGLObject(); + + /** implementation of the actual deletion of an GL object - subclasses from GLObjectManager must implement the appropriate GL calls.*/ + virtual void deleteGLObject(GLuint globj) = 0; + +protected: + virtual ~GLObjectManager(); + + typedef std::list GLObjectHandleList; + OpenThreads::Mutex _mutex; + GLObjectHandleList _deleteGLObjectHandles; + +}; + + } #endif diff --git a/include/osg/Program b/include/osg/Program index 0cd786a08..16c7cef2e 100644 --- a/include/osg/Program +++ b/include/osg/Program @@ -228,21 +228,6 @@ class OSG_EXPORT Program : public osg::StateAttribute /** Query InfoLog from a glProgram */ bool getGlProgramInfoLog(unsigned int contextID, std::string& log) const; - /** Mark internal glProgram for deletion. - * Deletion requests are queued until they can be executed - * in the proper GL context. */ - static void deleteGlProgram(unsigned int contextID, GLuint program); - - /** flush all the cached glPrograms which need to be deleted - * in the OpenGL context related to contextID.*/ - static void flushDeletedGlPrograms(unsigned int contextID,double currentTime, double& availableTime); - - /** discard all the cached glPrograms which need to be deleted - * in the OpenGL context related to contextID. - * Note, unlike flush no OpenGL calls are made, instead the handles are all removed. - * this call is useful for when an OpenGL context has been destroyed. */ - static void discardDeletedGlPrograms(unsigned int contextID); - struct ActiveVarInfo { ActiveVarInfo() : _location(-1), _type(Uniform::UNDEFINED), _size(-1) {} ActiveVarInfo( GLint loc, GLenum type, GLint size ) : _location(loc), _type(type), _size(size) {} @@ -409,7 +394,7 @@ class OSG_EXPORT Program : public osg::StateAttribute PerContextProgram& operator=(const PerContextProgram&); // disallowed }; - struct OSG_EXPORT ProgramObjects : public osg::Referenced + struct OSG_EXPORT ProgramObjects : public osg::GraphicsObject { typedef std::vector< osg::ref_ptr > PerContextPrograms; diff --git a/include/osg/Shader b/include/osg/Shader index 3869c4edd..c24229134 100644 --- a/include/osg/Shader +++ b/include/osg/Shader @@ -205,21 +205,6 @@ class OSG_EXPORT Shader : public osg::Object /** If needed, compile the PCS's glShader */ void compileShader(osg::State& state) const; - - /** Mark internal glShader for deletion. - * Deletion requests are queued until they can be executed - * in the proper GL context. */ - static void deleteGlShader(unsigned int contextID, GLuint shader); - - /** flush all the cached glShaders which need to be deleted - * in the OpenGL context related to contextID.*/ - static void flushDeletedGlShaders(unsigned int contextID,double currentTime, double& availableTime); - - /** discard all the cached glShaders which need to be deleted in the OpenGL context related to contextID. - * Note, unlike flush no OpenGL calls are made, instead the handles are all removed. - * this call is useful for when an OpenGL context has been destroyed. */ - static void discardDeletedGlShaders(unsigned int contextID); - static Shader::Type getTypeId( const std::string& tname ); public: diff --git a/include/osg/State b/include/osg/State index 549098677..68ff52d5c 100644 --- a/include/osg/State +++ b/include/osg/State @@ -155,7 +155,7 @@ class OSG_EXPORT State : public Referenced typedef std::map > ExtensionMap; ExtensionMap _extensionMap; - /** Get a specific GL extensions object, initialize if not already present. + /** Get a specific GL extensions object or GraphicsObjectManager, initialize if not already present. * Note, must only be called from a the graphics context thread associated with this osg::State. */ template T* get() @@ -169,7 +169,7 @@ class OSG_EXPORT State : public Referenced return static_cast(ptr.get()); } - /** Get a specific GL extensions object if it already exists in the extension map. + /** Get a specific GL extensions object or GraphicsObjectManager if it already exists in the extension map. * Note, safe to call outwith a the graphics context thread associated with this osg::State. * Returns NULL if the desired extension object has not been created yet.*/ template @@ -181,6 +181,13 @@ class OSG_EXPORT State : public Referenced else return itr->second.get(); } + /** Set a specific GL extensions object pr GraphicsObjectManager. */ + template + void set(T* ptr) + { + const std::type_info* id(&typeid(T)); + _extensionMap[id] = ptr; + } /* Set whether shader composition is enabled.*/ void setShaderCompositionEnabled(bool flag) { _shaderCompositionEnabled = flag; } @@ -2945,6 +2952,7 @@ inline bool State::setActiveTextureUnit( unsigned int unit ) // forward declare speciailization of State::get() method template<> inline GLExtensions* State::get() { return _glExtensions.get(); } template<> inline const GLExtensions* State::get() const { return _glExtensions.get(); } +template<> inline void State::set(GLExtensions* ptr) { _glExtensions = ptr; } } diff --git a/include/osg/Texture b/include/osg/Texture index cd6cd753b..75522878e 100644 --- a/include/osg/Texture +++ b/include/osg/Texture @@ -405,9 +405,16 @@ #define GL_MAX_COMBINED_IMAGE_UNIFORMS 0x90CF #endif +//#define OSG_COLLECT_TEXTURE_APPLIED_STATS 1 + namespace osg { +// forward declare +class TextureObjectSet; +class TextureObjectManager; + + /** Texture pure virtual base class that encapsulates OpenGL texture * functionality common to the various types of OSG textures. */ @@ -983,11 +990,7 @@ class OSG_EXPORT Texture : public osg::StateAttribute unsigned int _size; }; - // forward declare - class TextureObjectSet; - class TextureObjectManager; - - class OSG_EXPORT TextureObject : public osg::Referenced + class OSG_EXPORT TextureObject : public GraphicsObject { public: @@ -1068,6 +1071,8 @@ class OSG_EXPORT Texture : public osg::StateAttribute inline bool isReusable() const { return _allocated && _profile._width!=0; } + /** release TextureObject to the orphan list to be reused or deleted.*/ + void release(); GLuint _id; TextureProfile _profile; @@ -1086,150 +1091,6 @@ class OSG_EXPORT Texture : public osg::StateAttribute typedef std::list< ref_ptr > TextureObjectList; - class OSG_EXPORT TextureObjectSet : public Referenced - { - public: - TextureObjectSet(TextureObjectManager* parent, const TextureProfile& profile); - - const TextureProfile& getProfile() const { return _profile; } - - void handlePendingOrphandedTextureObjects(); - - void deleteAllTextureObjects(); - void discardAllTextureObjects(); - void flushAllDeletedTextureObjects(); - void discardAllDeletedTextureObjects(); - void flushDeletedTextureObjects(double currentTime, double& availableTime); - - osg::ref_ptr takeFromOrphans(Texture* texture); - osg::ref_ptr takeOrGenerate(Texture* texture); - void moveToBack(TextureObject* to); - void addToBack(TextureObject* to); - void orphan(TextureObject* to); - void remove(TextureObject* to); - void moveToSet(TextureObject* to, TextureObjectSet* set); - - unsigned int size() const { return _profile._size * _numOfTextureObjects; } - - bool makeSpace(unsigned int& size); - - bool checkConsistency() const; - - TextureObjectManager* getParent() { return _parent; } - - unsigned int computeNumTextureObjectsInList() const; - unsigned int getNumOfTextureObjects() const { return _numOfTextureObjects; } - unsigned int getNumOrphans() const { return static_cast(_orphanedTextureObjects.size()); } - unsigned int getNumPendingOrphans() const { return static_cast(_pendingOrphanedTextureObjects.size()); } - - protected: - - virtual ~TextureObjectSet(); - - OpenThreads::Mutex _mutex; - - TextureObjectManager* _parent; - unsigned int _contextID; - TextureProfile _profile; - unsigned int _numOfTextureObjects; - TextureObjectList _orphanedTextureObjects; - TextureObjectList _pendingOrphanedTextureObjects; - - TextureObject* _head; - TextureObject* _tail; - - }; - - class OSG_EXPORT TextureObjectManager : public osg::Referenced - { - public: - TextureObjectManager(unsigned int contextID); - - unsigned int getContextID() const { return _contextID; } - - - - void setNumberActiveTextureObjects(unsigned int size) { _numActiveTextureObjects = size; } - unsigned int& getNumberActiveTextureObjects() { return _numActiveTextureObjects; } - unsigned int getNumberActiveTextureObjects() const { return _numActiveTextureObjects; } - - void setNumberOrphanedTextureObjects(unsigned int size) { _numOrphanedTextureObjects = size; } - unsigned int& getNumberOrphanedTextureObjects() { return _numOrphanedTextureObjects; } - unsigned int getNumberOrphanedTextureObjects() const { return _numOrphanedTextureObjects; } - - void setCurrTexturePoolSize(unsigned int size) { _currTexturePoolSize = size; } - unsigned int& getCurrTexturePoolSize() { return _currTexturePoolSize; } - unsigned int getCurrTexturePoolSize() const { return _currTexturePoolSize; } - - void setMaxTexturePoolSize(unsigned int size); - unsigned int getMaxTexturePoolSize() const { return _maxTexturePoolSize; } - - bool hasSpace(unsigned int size) const { return (_currTexturePoolSize+size)<=_maxTexturePoolSize; } - bool makeSpace(unsigned int size); - - osg::ref_ptr generateTextureObject(const Texture* texture, GLenum target); - osg::ref_ptr generateTextureObject(const Texture* texture, - GLenum target, - GLint numMipmapLevels, - GLenum internalFormat, - GLsizei width, - GLsizei height, - GLsizei depth, - GLint border); - void handlePendingOrphandedTextureObjects(); - void deleteAllTextureObjects(); - void discardAllTextureObjects(); - void flushAllDeletedTextureObjects(); - void discardAllDeletedTextureObjects(); - void flushDeletedTextureObjects(double currentTime, double& availableTime); - void releaseTextureObject(TextureObject* to); - - TextureObjectSet* getTextureObjectSet(const TextureProfile& profile); - - void newFrame(osg::FrameStamp* fs); - void resetStats(); - void reportStats(std::ostream& out); - void recomputeStats(std::ostream& out) const; - bool checkConsistency() const; - - unsigned int& getFrameNumber() { return _frameNumber; } - unsigned int& getNumberFrames() { return _numFrames; } - - unsigned int& getNumberDeleted() { return _numDeleted; } - double& getDeleteTime() { return _deleteTime; } - - unsigned int& getNumberGenerated() { return _numGenerated; } - double& getGenerateTime() { return _generateTime; } - - unsigned int& getNumberApplied() { return _numApplied; } - double& getApplyTime() { return _applyTime; } - - - protected: - - typedef std::map< TextureProfile, osg::ref_ptr > TextureSetMap; - unsigned int _contextID; - unsigned int _numActiveTextureObjects; - unsigned int _numOrphanedTextureObjects; - unsigned int _currTexturePoolSize; - unsigned int _maxTexturePoolSize; - TextureSetMap _textureSetMap; - - unsigned int _frameNumber; - - unsigned int _numFrames; - unsigned int _numDeleted; - double _deleteTime; - - unsigned int _numGenerated; - double _generateTime; - - unsigned int _numApplied; - double _applyTime; - - }; - - static osg::ref_ptr& getTextureObjectManager(unsigned int contextID); static osg::ref_ptr generateTextureObject(const Texture* texture, unsigned int contextID,GLenum target); @@ -1254,13 +1115,6 @@ class OSG_EXPORT Texture : public osg::StateAttribute GLsizei depth, GLint border) const; - 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: typedef buffered_object< ref_ptr > TextureObjectBuffer; @@ -1269,6 +1123,139 @@ class OSG_EXPORT Texture : public osg::StateAttribute }; +class OSG_EXPORT TextureObjectSet : public Referenced +{ +public: + TextureObjectSet(TextureObjectManager* parent, const Texture::TextureProfile& profile); + + const Texture::TextureProfile& getProfile() const { return _profile; } + + void handlePendingOrphandedTextureObjects(); + + void deleteAllTextureObjects(); + void discardAllTextureObjects(); + void flushAllDeletedTextureObjects(); + void discardAllDeletedTextureObjects(); + void flushDeletedTextureObjects(double currentTime, double& availableTime); + + osg::ref_ptr takeFromOrphans(Texture* texture); + osg::ref_ptr takeOrGenerate(Texture* texture); + void moveToBack(Texture::TextureObject* to); + void addToBack(Texture::TextureObject* to); + void orphan(Texture::TextureObject* to); + void remove(Texture::TextureObject* to); + void moveToSet(Texture::TextureObject* to, TextureObjectSet* set); + + unsigned int size() const { return _profile._size * _numOfTextureObjects; } + + bool makeSpace(unsigned int& size); + + bool checkConsistency() const; + + TextureObjectManager* getParent() { return _parent; } + + unsigned int computeNumTextureObjectsInList() const; + unsigned int getNumOfTextureObjects() const { return _numOfTextureObjects; } + unsigned int getNumOrphans() const { return static_cast(_orphanedTextureObjects.size()); } + unsigned int getNumPendingOrphans() const { return static_cast(_pendingOrphanedTextureObjects.size()); } + +protected: + + virtual ~TextureObjectSet(); + + OpenThreads::Mutex _mutex; + + TextureObjectManager* _parent; + unsigned int _contextID; + Texture::TextureProfile _profile; + unsigned int _numOfTextureObjects; + Texture::TextureObjectList _orphanedTextureObjects; + Texture::TextureObjectList _pendingOrphanedTextureObjects; + + Texture::TextureObject* _head; + Texture::TextureObject* _tail; + +}; + +class OSG_EXPORT TextureObjectManager : public GraphicsObjectManager +{ +public: + TextureObjectManager(unsigned int contextID); + + + void setNumberActiveTextureObjects(unsigned int size) { _numActiveTextureObjects = size; } + unsigned int& getNumberActiveTextureObjects() { return _numActiveTextureObjects; } + unsigned int getNumberActiveTextureObjects() const { return _numActiveTextureObjects; } + + void setNumberOrphanedTextureObjects(unsigned int size) { _numOrphanedTextureObjects = size; } + unsigned int& getNumberOrphanedTextureObjects() { return _numOrphanedTextureObjects; } + unsigned int getNumberOrphanedTextureObjects() const { return _numOrphanedTextureObjects; } + + void setCurrTexturePoolSize(unsigned int size) { _currTexturePoolSize = size; } + unsigned int& getCurrTexturePoolSize() { return _currTexturePoolSize; } + unsigned int getCurrTexturePoolSize() const { return _currTexturePoolSize; } + + void setMaxTexturePoolSize(unsigned int size); + unsigned int getMaxTexturePoolSize() const { return _maxTexturePoolSize; } + + bool hasSpace(unsigned int size) const { return (_currTexturePoolSize+size)<=_maxTexturePoolSize; } + bool makeSpace(unsigned int size); + + osg::ref_ptr generateTextureObject(const Texture* texture, GLenum target); + osg::ref_ptr generateTextureObject(const Texture* texture, + GLenum target, + GLint numMipmapLevels, + GLenum internalFormat, + GLsizei width, + GLsizei height, + GLsizei depth, + GLint border); + void handlePendingOrphandedTextureObjects(); + + void deleteAllGLObjects(); + void discardAllGLObjects(); + void flushAllDeletedGLObjects(); + void discardAllDeletedGLObjects(); + void flushDeletedGLObjects(double currentTime, double& availableTime); + + TextureObjectSet* getTextureObjectSet(const Texture::TextureProfile& profile); + + void newFrame(osg::FrameStamp* fs); + void resetStats(); + void reportStats(std::ostream& out); + void recomputeStats(std::ostream& out) const; + bool checkConsistency() const; + + unsigned int& getFrameNumber() { return _frameNumber; } + unsigned int& getNumberFrames() { return _numFrames; } + + unsigned int& getNumberDeleted() { return _numDeleted; } + double& getDeleteTime() { return _deleteTime; } + + unsigned int& getNumberGenerated() { return _numGenerated; } + double& getGenerateTime() { return _generateTime; } + +protected: + + ~TextureObjectManager(); + + typedef std::map< Texture::TextureProfile, osg::ref_ptr > TextureSetMap; + + unsigned int _numActiveTextureObjects; + unsigned int _numOrphanedTextureObjects; + unsigned int _currTexturePoolSize; + unsigned int _maxTexturePoolSize; + TextureSetMap _textureSetMap; + + unsigned int _frameNumber; + + unsigned int _numFrames; + unsigned int _numDeleted; + double _deleteTime; + + unsigned int _numGenerated; + double _generateTime; +}; } #endif diff --git a/include/osg/VertexProgram b/include/osg/VertexProgram index e30535b5d..67f328d64 100644 --- a/include/osg/VertexProgram +++ b/include/osg/VertexProgram @@ -205,25 +205,6 @@ class OSG_EXPORT VertexProgram : public StateAttribute /** Force a recompile on next apply() of associated OpenGL vertex program objects. */ void dirtyVertexProgramObject(); - /** Use deleteVertexProgramObject instead of glDeletePrograms to allow - * OpenGL Vertex Program objects to cached until they can be deleted - * by the OpenGL context in which they were created, specified - * by contextID. - */ - static void deleteVertexProgramObject(unsigned int contextID,GLuint handle); - - /** Flush all the cached vertex programs which need to be deleted - * in the OpenGL context related to contextID. - */ - static void flushDeletedVertexProgramObjects(unsigned int contextID,double currentTime, double& availableTime); - - /** discard all the cached vertex programs which need to be deleted - * in the OpenGL context related to contextID. - * Note, unlike flush no OpenGL calls are made, instead the handles are all removed. - * this call is useful for when an OpenGL context has been destroyed. - */ - static void discardDeletedVertexProgramObjects(unsigned int contextID); - virtual void apply(State& state) const; virtual void compileGLObjects(State& state) const { apply(state); } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 33edf5732..bea40f360 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -19,13 +19,13 @@ FOREACH( mylibfolder osgFX osgManipulator osgParticle - osgPresentation + osgUI + osgVolume osgShadow osgSim osgTerrain osgWidget - osgUI - osgVolume + osgPresentation osgWrappers/serializers osgWrappers/deprecated-dotosg osgPlugins diff --git a/src/osg/BufferObject.cpp b/src/osg/BufferObject.cpp index f23386970..bb3888e8e 100644 --- a/src/osg/BufferObject.cpp +++ b/src/osg/BufferObject.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -79,6 +80,11 @@ GLBufferObject::~GLBufferObject() //OSG_NOTICE<<"Destructing BufferObject "<orphan(this); +} + void GLBufferObject::setBufferObject(BufferObject* bufferObject) { assign(bufferObject); @@ -844,7 +850,7 @@ unsigned int GLBufferObjectSet::computeNumGLBufferObjectsInList() const GLBufferObjectManager::GLBufferObjectManager(unsigned int contextID): - _contextID(contextID), + GraphicsObjectManager("GLBufferObjectManager", contextID), _numActiveGLBufferObjects(0), _numOrphanedGLBufferObjects(0), _currGLBufferObjectPoolSize(0), @@ -860,6 +866,10 @@ GLBufferObjectManager::GLBufferObjectManager(unsigned int contextID): { } +GLBufferObjectManager::~GLBufferObjectManager() +{ +} + void GLBufferObjectManager::setMaxGLBufferObjectPoolSize(unsigned int size) { if (_maxGLBufferObjectPoolSize == size) return; @@ -915,7 +925,7 @@ void GLBufferObjectManager::handlePendingOrphandedGLBufferObjects() } } -void GLBufferObjectManager::deleteAllGLBufferObjects() +void GLBufferObjectManager::deleteAllGLObjects() { ElapsedTime elapsedTime(&(getDeleteTime())); @@ -927,7 +937,7 @@ void GLBufferObjectManager::deleteAllGLBufferObjects() } } -void GLBufferObjectManager::discardAllGLBufferObjects() +void GLBufferObjectManager::discardAllGLObjects() { for(GLBufferObjectSetMap::iterator itr = _glBufferObjectSetMap.begin(); itr != _glBufferObjectSetMap.end(); @@ -937,7 +947,7 @@ void GLBufferObjectManager::discardAllGLBufferObjects() } } -void GLBufferObjectManager::flushAllDeletedGLBufferObjects() +void GLBufferObjectManager::flushAllDeletedGLObjects() { ElapsedTime elapsedTime(&(getDeleteTime())); @@ -949,7 +959,7 @@ void GLBufferObjectManager::flushAllDeletedGLBufferObjects() } } -void GLBufferObjectManager::discardAllDeletedGLBufferObjects() +void GLBufferObjectManager::discardAllDeletedGLObjects() { for(GLBufferObjectSetMap::iterator itr = _glBufferObjectSetMap.begin(); itr != _glBufferObjectSetMap.end(); @@ -959,7 +969,7 @@ void GLBufferObjectManager::discardAllDeletedGLBufferObjects() } } -void GLBufferObjectManager::flushDeletedGLBufferObjects(double currentTime, double& availableTime) +void GLBufferObjectManager::flushDeletedGLObjects(double currentTime, double& availableTime) { ElapsedTime elapsedTime(&(getDeleteTime())); @@ -971,13 +981,6 @@ void GLBufferObjectManager::flushDeletedGLBufferObjects(double currentTime, doub } } -void GLBufferObjectManager::releaseGLBufferObject(GLBufferObject* to) -{ - if (to->_set) to->_set->orphan(to); - else OSG_NOTICE<<"GLBufferObjectManager::releaseGLBufferObject(GLBufferObject* to) Not implemented yet"<getFrameNumber(); @@ -1042,52 +1045,6 @@ void GLBufferObjectManager::recomputeStats(std::ostream& out) out<<" getMaxGLBufferObjectPoolSize()="<release(); _glBufferObjects[i] = 0; } } @@ -1235,7 +1197,7 @@ void BufferObject::deleteBufferObject(unsigned int contextID,GLuint globj) // implement deleteBufferObject for backwards compatibility by adding // a GLBufferObject for the globj id to BufferObjectManager/Set for the specified context. - osg::ref_ptr& bufferObjectManager = GLBufferObjectManager::getGLBufferObjectManager(contextID); + GLBufferObjectManager* bufferObjectManager = osg::get(contextID); if (!bufferObjectManager) { OSG_NOTICE<<"Warning::BufferObject::deleteBufferObject("< +#include +#include +#include + +using namespace osg; + + +typedef std::map > ContextIDMap; +static ContextIDMap s_contextIDMap; +static OpenThreads::ReentrantMutex s_contextIDMapMutex; +static ContextData::GraphicsContexts s_registeredContexts; + +ContextData::ContextData(unsigned int contextID): + GraphicsObjectManager("ContextData", contextID) +{ +} + +ContextData::~ContextData() +{ +} + +void ContextData::newFrame(osg::FrameStamp* frameStamp) +{ + // OSG_NOTICE<<"ContextData::newFrame("<getFrameNumber()<<")"<(itr->second.get()); + if (gom) gom->newFrame(frameStamp); + } +} + +void ContextData::resetStats() +{ + for(ManagerMap::iterator itr = _managerMap.begin(); + itr != _managerMap.end(); + ++itr) + { + osg::GraphicsObjectManager* gom = dynamic_cast(itr->second.get()); + if (gom) gom->resetStats(); + } +} + +void ContextData::reportStats(std::ostream& out) +{ + for(ManagerMap::iterator itr = _managerMap.begin(); + itr != _managerMap.end(); + ++itr) + { + osg::GraphicsObjectManager* gom = dynamic_cast(itr->second.get()); + if (gom) gom->reportStats(out); + } +} + +void ContextData::recomputeStats(std::ostream& out) const +{ + for(ManagerMap::const_iterator itr = _managerMap.begin(); + itr != _managerMap.end(); + ++itr) + { + osg::GraphicsObjectManager* gom = dynamic_cast(itr->second.get()); + if (gom) gom->recomputeStats(out); + } +} + + +void ContextData::flushDeletedGLObjects(double currentTime, double& availableTime) +{ + // OSG_NOTICE<<"ContextData::flushDeletedGLObjects("<(itr->second.get()); + if (gom) gom->flushDeletedGLObjects(currentTime, availableTime); + } +} + +void ContextData::flushAllDeletedGLObjects() +{ + // OSG_NOTICE<<"ContextData::flushAllDeletedGLObjects()"<(itr->second.get()); + if (gom) gom->flushAllDeletedGLObjects(); + } +} + +void ContextData::deleteAllGLObjects() +{ + // OSG_NOTICE<<"ContextData::deleteAllGLObjects()"<(itr->second.get()); + if (gom) gom->deleteAllGLObjects(); + } +} + +void ContextData::discardAllGLObjects() +{ + // OSG_NOTICE<<"ContextData::discardAllGLObjects()"<(itr->second.get()); + if (gom) gom->discardAllGLObjects(); + } +} + +ContextData* osg::getContextData(unsigned int contextID) +{ + OpenThreads::ScopedLock lock(s_contextIDMapMutex); + ContextIDMap::iterator itr = s_contextIDMap.find(contextID); + return (itr!=s_contextIDMap.end()) ? itr->second.get() : 0; +} + +/** Get or create the ContextData for a specific contextID.*/ +ContextData* osg::getOrCreateContextData(unsigned int contextID) +{ + //OpenThreads::ScopedLock lock(s_contextIDMapMutex); + osg::ref_ptr& cd = s_contextIDMap[contextID]; + if (!cd) + { + cd = new ContextData(contextID); + } + return cd.get(); +} + +unsigned int ContextData::createNewContextID() +{ + OpenThreads::ScopedLock lock(s_contextIDMapMutex); + + // first check to see if we can reuse contextID; + for(ContextIDMap::iterator itr = s_contextIDMap.begin(); + itr != s_contextIDMap.end(); + ++itr) + { + if (!itr->second || itr->second->getNumContexts() == 0) + { + itr->second = new ContextData(itr->first); + itr->second->setNumContexts(1); + + OSG_INFO<<"ContextData::createNewContextID() : reusing contextID="<first<first; + } + } + + unsigned int contextID = s_contextIDMap.size(); + s_contextIDMap[contextID] = new ContextData(contextID); + s_contextIDMap[contextID]->setNumContexts(1); + + OSG_INFO<<"ContextData::createNewContextID() creating contextID="<setMaxNumberOfGraphicsContexts( contextID + 1 ); + + return contextID; +} + +unsigned int ContextData::getMaxContextID() +{ + OpenThreads::ScopedLock lock(s_contextIDMapMutex); + unsigned int maxContextID = 0; + for(ContextIDMap::iterator itr = s_contextIDMap.begin(); + itr != s_contextIDMap.end(); + ++itr) + { + if (itr->first > maxContextID) maxContextID = itr->first; + } + return maxContextID; +} + + +void ContextData::incrementContextIDUsageCount(unsigned int contextID) +{ + OpenThreads::ScopedLock lock(s_contextIDMapMutex); + + if (!s_contextIDMap[contextID]) s_contextIDMap[contextID] = new ContextData(contextID); + + s_contextIDMap[contextID]->incrementUsageCount(); + + OSG_NOTICE<<"ContextData::incrementContextIDUsageCount("<getNumContexts()< lock(s_contextIDMapMutex); + + if (s_contextIDMap[contextID].valid()) + { + if (s_contextIDMap[contextID]->getNumContexts()!=0) + { + s_contextIDMap[contextID]->decrementUsageCount(); + } + + if (s_contextIDMap[contextID]->getNumContexts()==0) + { + s_contextIDMap[contextID] = 0; + } + } +} + + +void ContextData::registerGraphicsContext(GraphicsContext* gc) +{ + OSG_INFO<<"ContextData::registerGraphicsContext "< lock(s_contextIDMapMutex); + + GraphicsContexts::iterator itr = std::find(s_registeredContexts.begin(), s_registeredContexts.end(), gc); + if (itr != s_registeredContexts.end()) s_registeredContexts.erase(itr); + + s_registeredContexts.push_back(gc); +} + +void ContextData::unregisterGraphicsContext(GraphicsContext* gc) +{ + OSG_INFO<<"ContextData::unregisterGraphicsContext "< lock(s_contextIDMapMutex); + + GraphicsContexts::iterator itr = std::find(s_registeredContexts.begin(), s_registeredContexts.end(), gc); + if (itr != s_registeredContexts.end()) s_registeredContexts.erase(itr); +} + +ContextData::GraphicsContexts ContextData::getAllRegisteredGraphicsContexts() +{ + OSG_INFO<<"ContextData::getAllRegisteredGraphicsContexts s_registeredContexts.size()="< lock(s_contextIDMapMutex); + return s_registeredContexts; +} + +ContextData::GraphicsContexts ContextData::getRegisteredGraphicsContexts(unsigned int contextID) +{ + GraphicsContexts contexts; + + OpenThreads::ScopedLock lock(s_contextIDMapMutex); + for(GraphicsContexts::iterator itr = s_registeredContexts.begin(); + itr != s_registeredContexts.end(); + ++itr) + { + GraphicsContext* gc = *itr; + if (gc->getState() && gc->getState()->getContextID()==contextID) contexts.push_back(gc); + } + + OSG_INFO<<"ContextData::getRegisteredGraphicsContexts "< lock(s_contextIDMapMutex); + if (s_contextIDMap[contextID]->getCompileContext()) return s_contextIDMap[contextID]->getCompileContext(); + } + + GraphicsContexts contexts = ContextData::getRegisteredGraphicsContexts(contextID); + if (contexts.empty()) return 0; + + GraphicsContext* src_gc = contexts.front(); + const osg::GraphicsContext::Traits* src_traits = src_gc->getTraits(); + + osg::GraphicsContext::Traits* traits = new osg::GraphicsContext::Traits; + traits->screenNum = src_traits->screenNum; + traits->displayNum = src_traits->displayNum; + traits->hostName = src_traits->hostName; + traits->width = 100; + traits->height = 100; + traits->red = src_traits->red; + traits->green = src_traits->green; + traits->blue = src_traits->blue; + traits->alpha = src_traits->alpha; + traits->depth = src_traits->depth; + traits->sharedContext = src_gc; + traits->pbuffer = true; + + osg::ref_ptr gc = osg::GraphicsContext::createGraphicsContext(traits); + if (gc.valid() && gc->realize()) + { + OpenThreads::ScopedLock lock(s_contextIDMapMutex); + s_contextIDMap[contextID]->setCompileContext(gc.get()); + OSG_NOTICE<<" succeeded ContextData::createCompileContext."< lock(s_contextIDMapMutex); + s_contextIDMap[contextID]->setCompileContext(gc); +} + +GraphicsContext* ContextData::getCompileContext(unsigned int contextID) +{ + // OSG_NOTICE<<"ContextData::getCompileContext "< lock(s_contextIDMapMutex); + ContextIDMap::iterator itr = s_contextIDMap.find(contextID); + if (itr != s_contextIDMap.end()) return itr->second->getCompileContext(); + else return 0; +} diff --git a/src/osg/Drawable.cpp b/src/osg/Drawable.cpp index cd2073e42..f0eda6e4d 100644 --- a/src/osg/Drawable.cpp +++ b/src/osg/Drawable.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -32,58 +33,7 @@ using namespace osg; -unsigned int Drawable::s_numberDrawablesReusedLastInLastFrame = 0; -unsigned int Drawable::s_numberNewDrawablesInLastFrame = 0; -unsigned int Drawable::s_numberDeletedDrawablesInLastFrame = 0; - -// static cache of deleted display lists which can only -// by completely deleted once the appropriate OpenGL context -// is set. Used osg::Drawable::deleteDisplayList(..) and flushDeletedDisplayLists(..) below. -typedef std::multimap DisplayListMap; -typedef osg::buffered_object DeletedDisplayListCache; - -static OpenThreads::Mutex s_mutex_deletedDisplayListCache; -static DeletedDisplayListCache s_deletedDisplayListCache; - -GLuint Drawable::generateDisplayList(unsigned int contextID, unsigned int sizeHint) -{ -#ifdef OSG_GL_DISPLAYLISTS_AVAILABLE - OpenThreads::ScopedLock lock(s_mutex_deletedDisplayListCache); - - DisplayListMap& dll = s_deletedDisplayListCache[contextID]; - if (dll.empty()) - { - ++s_numberNewDrawablesInLastFrame; - return glGenLists( 1 ); - } - else - { - DisplayListMap::iterator itr = dll.lower_bound(sizeHint); - if (itr!=dll.end()) - { - // OSG_NOTICE<<"Reusing a display list of size = "<first<<" for requested size = "<second; - dll.erase(itr); - - return globj; - } - else - { - // OSG_NOTICE<<"Creating a new display list of size = "< lock(s_mutex_deletedDisplayListCache); - - // insert the globj into the cache for the appropriate context. - s_deletedDisplayListCache[contextID].insert(DisplayListMap::value_type(sizeHint,globj)); - } -#else - OSG_NOTICE<<"Warning: Drawable::deleteDisplayList(..) - not supported."< lock(s_mutex_deletedDisplayListCache); - - DisplayListMap& dll = s_deletedDisplayListCache[contextID]; - - for(DisplayListMap::iterator ditr=dll.begin(); - ditr!=dll.end(); - ++ditr) - { - glDeleteLists(ditr->second,1); } - dll.clear(); -#else - OSG_NOTICE<<"Warning: Drawable::deleteDisplayList(..) - not supported."< lock(s_mutex_deletedDisplayListCache); - - DisplayListMap& dll = s_deletedDisplayListCache[contextID]; - dll.clear(); -} - -void Drawable::flushDeletedDisplayLists(unsigned int contextID, double& availableTime) -{ -#ifdef OSG_GL_DISPLAYLISTS_AVAILABLE - // if no time available don't try to flush objects. - if (availableTime<=0.0) return; - - const osg::Timer& timer = *osg::Timer::instance(); - osg::Timer_t start_tick = timer.tick(); - double elapsedTime = 0.0; - - unsigned int noDeleted = 0; - + virtual void flushDeletedGLObjects(double, double& availableTime) { - OpenThreads::ScopedLock lock(s_mutex_deletedDisplayListCache); + #ifdef OSG_GL_DISPLAYLISTS_AVAILABLE + // OSG_NOTICE<<"void DisplayListManager::flushDeletedGLObjects(, "< lock(_mutex_deletedDisplayListCache); - DisplayListMap::iterator ditr=dll.begin(); - unsigned int maxNumToDelete = (dll.size() > s_minimumNumberOfDisplayListsToRetainInCache) ? dll.size()-s_minimumNumberOfDisplayListsToRetainInCache : 0; - for(; - ditr!=dll.end() && elapsedTimesecond,1); + unsigned int prev_size = _displayListMap.size(); - elapsedTime = timer.delta_s(start_tick,timer.tick()); - ++noDeleted; + DisplayListMap::iterator ditr=_displayListMap.begin(); + unsigned int maxNumToDelete = (_displayListMap.size() > s_minimumNumberOfDisplayListsToRetainInCache) ? _displayListMap.size()-s_minimumNumberOfDisplayListsToRetainInCache : 0; + for(; + ditr!=_displayListMap.end() && elapsedTimesecond,1); - ++Drawable::s_numberDeletedDrawablesInLastFrame; - } + elapsedTime = timer.delta_s(start_tick,timer.tick()); + ++noDeleted; - if (ditr!=dll.begin()) dll.erase(dll.begin(),ditr); + ++_numberDeletedDrawablesInLastFrame; + } - if (noDeleted+dll.size() != prev_size) - { - OSG_WARN<<"Error in delete"< s_minimumNumberOfDisplayListsToRetainInCache) ? _displayListMap.size()-s_minimumNumberOfDisplayListsToRetainInCache : 0; + for(; + ditr!=_displayListMap.rend() && elapsedTimesecond,1); + + elapsedTime = timer.delta_s(start_tick,timer.tick()); + ++noDeleted; + + ++_numberDeletedDrawablesInLastFrame; + } + + if (ditr!=_displayListMap.rbegin()) _displayListMap.erase(ditr.base(),_displayListMap.end()); + + if (noDeleted+_displayListMap.size() != prev_size) + { + OSG_WARN<<"Error in delete"< lock(_mutex_deletedDisplayListCache); + + for(DisplayListMap::iterator ditr=_displayListMap.begin(); + ditr!=_displayListMap.end(); + ++ditr) + { + glDeleteLists(ditr->second,1); + } + + _displayListMap.clear(); + #else + OSG_NOTICE<<"Warning: Drawable::deleteDisplayList(..) - not supported."< lock(_mutex_deletedDisplayListCache); + _displayListMap.clear(); + } + + void deleteDisplayList(GLuint globj, unsigned int sizeHint) + { + #ifdef OSG_GL_DISPLAYLISTS_AVAILABLE + if (globj!=0) + { + OpenThreads::ScopedLock lock(_mutex_deletedDisplayListCache); + + // insert the globj into the cache for the appropriate context. + _displayListMap.insert(DisplayListMap::value_type(sizeHint,globj)); + } + #else + OSG_NOTICE<<"Warning: Drawable::deleteDisplayList(..) - not supported."< lock(_mutex_deletedDisplayListCache); + + if (_displayListMap.empty()) + { + ++_numberNewDrawablesInLastFrame; + return glGenLists( 1 ); } else { - unsigned int prev_size = dll.size(); - - DisplayListMap::reverse_iterator ditr=dll.rbegin(); - unsigned int maxNumToDelete = (dll.size() > s_minimumNumberOfDisplayListsToRetainInCache) ? dll.size()-s_minimumNumberOfDisplayListsToRetainInCache : 0; - for(; - ditr!=dll.rend() && elapsedTimesecond,1); + // OSG_NOTICE<<"Reusing a display list of size = "<first<<" for requested size = "<second; + _displayListMap.erase(itr); - if (ditr!=dll.rbegin()) dll.erase(ditr.base(),dll.end()); - - if (noDeleted+dll.size() != prev_size) - { - OSG_WARN<<"Error in delete"< DisplayListMap; + OpenThreads::Mutex _mutex_deletedDisplayListCache; + DisplayListMap _displayListMap; + +}; + +GLuint Drawable::generateDisplayList(unsigned int contextID, unsigned int sizeHint) +{ + return osg::get(contextID)->generateDisplayList(sizeHint); } +void Drawable::deleteDisplayList(unsigned int contextID,GLuint globj, unsigned int sizeHint) +{ + osg::get(contextID)->deleteDisplayList(globj, sizeHint); +} + + bool Drawable::UpdateCallback::run(osg::Object* object, osg::Object* data) { osg::Drawable* drawable = dynamic_cast(object); diff --git a/src/osg/FragmentProgram.cpp b/src/osg/FragmentProgram.cpp index c1e22df43..ba0fe8f74 100644 --- a/src/osg/FragmentProgram.cpp +++ b/src/osg/FragmentProgram.cpp @@ -15,71 +15,21 @@ #include #include #include - -#include - -#include -#include +#include using namespace osg; -// static cache of deleted fragment programs which can only -// by completely deleted once the appropriate OpenGL context -// is set. -typedef std::list FragmentProgramObjectList; -typedef osg::buffered_object DeletedFragmentProgramObjectCache; - -static OpenThreads::Mutex s_mutex_deletedFragmentProgramObjectCache; -static DeletedFragmentProgramObjectCache s_deletedFragmentProgramObjectCache; - -void FragmentProgram::deleteFragmentProgramObject(unsigned int contextID,GLuint handle) +class GLFragmentProgramManager : public GLObjectManager { - if (handle!=0) +public: + GLFragmentProgramManager(unsigned int contextID) : GLObjectManager("GLFragmentProgramManager",contextID) {} + + virtual void deleteGLObject(GLuint globj) { - OpenThreads::ScopedLock lock(s_mutex_deletedFragmentProgramObjectCache); - - // insert the handle into the cache for the appropriate context. - s_deletedFragmentProgramObjectCache[contextID].push_back(handle); + const GLExtensions* extensions = GLExtensions::Get(_contextID,true); + if (extensions->isGlslSupported) extensions->glDeletePrograms(1, &globj ); } -} - - -void FragmentProgram::flushDeletedFragmentProgramObjects(unsigned int contextID,double /*currentTime*/, double& availableTime) -{ - // if no time available don't try to flush objects. - if (availableTime<=0.0) return; - - const osg::Timer& timer = *osg::Timer::instance(); - osg::Timer_t start_tick = timer.tick(); - double elapsedTime = 0.0; - - { - OpenThreads::ScopedLock lock(s_mutex_deletedFragmentProgramObjectCache); - - const GLExtensions* extensions = GLExtensions::Get(contextID,true); - - FragmentProgramObjectList& vpol = s_deletedFragmentProgramObjectCache[contextID]; - - for(FragmentProgramObjectList::iterator titr=vpol.begin(); - titr!=vpol.end() && elapsedTimeglDeletePrograms( 1L, &(*titr ) ); - titr = vpol.erase(titr); - elapsedTime = timer.delta_s(start_tick,timer.tick()); - } - } - - availableTime -= elapsedTime; -} - -void FragmentProgram::discardDeletedFragmentProgramObjects(unsigned int contextID) -{ - OpenThreads::ScopedLock lock(s_mutex_deletedFragmentProgramObjectCache); - FragmentProgramObjectList& vpol = s_deletedFragmentProgramObjectCache[contextID]; - vpol.clear(); -} - +}; FragmentProgram::FragmentProgram() { @@ -117,7 +67,7 @@ void FragmentProgram::dirtyFragmentProgramObject() { if (_fragmentProgramIDList[i] != 0) { - FragmentProgram::deleteFragmentProgramObject(i,_fragmentProgramIDList[i]); + osg::get(i)->deleteGLObject(_fragmentProgramIDList[i]); _fragmentProgramIDList[i] = 0; } } @@ -207,7 +157,7 @@ void FragmentProgram::releaseGLObjects(State* state) const unsigned int contextID = state->getContextID(); if (_fragmentProgramIDList[contextID] != 0) { - FragmentProgram::deleteFragmentProgramObject(contextID,_fragmentProgramIDList[contextID]); + osg::get(contextID)->deleteGLObject(_fragmentProgramIDList[contextID]); _fragmentProgramIDList[contextID] = 0; } } diff --git a/src/osg/FrameBufferObject.cpp b/src/osg/FrameBufferObject.cpp index a043bf3be..301130f87 100644 --- a/src/osg/FrameBufferObject.cpp +++ b/src/osg/FrameBufferObject.cpp @@ -24,73 +24,37 @@ #include #include #include +#include #include using namespace osg; +GLRenderBufferManager::GLRenderBufferManager(unsigned int contextID): + GLObjectManager("GLRenderBufferManager",contextID) +{} + +void GLRenderBufferManager::deleteGLObject(GLuint globj) +{ + const GLExtensions* extensions = GLExtensions::Get(_contextID,true); + if (extensions->isGlslSupported) extensions->glDeleteRenderbuffers(1, &globj ); +} + +GLFrameBufferObjectManager::GLFrameBufferObjectManager(unsigned int contextID): + GLObjectManager("GLFrameBufferObjectManager",contextID) +{} + +void GLFrameBufferObjectManager::deleteGLObject(GLuint globj) +{ + const GLExtensions* extensions = GLExtensions::Get(_contextID,true); + if (extensions->isGlslSupported) extensions->glDeleteFramebuffers(1, &globj ); +} + + /************************************************************************** * RenderBuffer **************************************************************************/ -/////////////////////////////////////////////////////////////////////////// -// static cache of glRenderbuffers flagged for deletion, which will actually -// be deleted in the correct GL context. - -typedef std::list RenderBufferHandleList; -typedef osg::buffered_object DeletedRenderBufferCache; - -static OpenThreads::Mutex s_mutex_deletedRenderBufferCache; -static DeletedRenderBufferCache s_deletedRenderBufferCache; - -void RenderBuffer::deleteRenderBuffer(unsigned int contextID, GLuint rb) -{ - if( rb ) - { - OpenThreads::ScopedLock lock(s_mutex_deletedRenderBufferCache); - - // add glProgram to the cache for the appropriate context. - s_deletedRenderBufferCache[contextID].push_back(rb); - } -} - -void RenderBuffer::flushDeletedRenderBuffers(unsigned int contextID,double /*currentTime*/, double& availableTime) -{ - // if no time available don't try to flush objects. - if (availableTime<=0.0) return; - - const GLExtensions* extensions = GLExtensions::Get(contextID,true); - if(!extensions || !extensions->isFrameBufferObjectSupported ) return; - - const osg::Timer& timer = *osg::Timer::instance(); - osg::Timer_t start_tick = timer.tick(); - double elapsedTime = 0.0; - - { - OpenThreads::ScopedLock lock(s_mutex_deletedRenderBufferCache); - - RenderBufferHandleList& pList = s_deletedRenderBufferCache[contextID]; - for(RenderBufferHandleList::iterator titr=pList.begin(); - titr!=pList.end() && elapsedTimeglDeleteRenderbuffers(1, &(*titr) ); - titr = pList.erase( titr ); - elapsedTime = timer.delta_s(start_tick,timer.tick()); - } - } - - availableTime -= elapsedTime; -} - -void RenderBuffer::discardDeletedRenderBuffers(unsigned int contextID) -{ - OpenThreads::ScopedLock lock(s_mutex_deletedRenderBufferCache); - RenderBufferHandleList& pList = s_deletedRenderBufferCache[contextID]; - pList.clear(); -} - - RenderBuffer::RenderBuffer() : Object(), _internalFormat(GL_DEPTH_COMPONENT24), @@ -125,7 +89,7 @@ RenderBuffer::~RenderBuffer() { for(unsigned i=0; i<_objectID.size(); ++i) { - if (_objectID[i]) deleteRenderBuffer(i, _objectID[i]); + if (_objectID[i]) osg::get(i)->deleteGLObject(_objectID[i]); } } @@ -209,7 +173,7 @@ void RenderBuffer::releaseGLObjects(osg::State* state) const unsigned int contextID = state->getContextID(); if (_objectID[contextID]) { - deleteRenderBuffer(contextID, _objectID[contextID]); + osg::get(contextID)->deleteGLObject(_objectID[contextID]); _objectID[contextID] = 0; } } @@ -219,7 +183,7 @@ void RenderBuffer::releaseGLObjects(osg::State* state) const { if (_objectID[i]) { - deleteRenderBuffer(i, _objectID[i]); + osg::get(i)->deleteGLObject(_objectID[i]); _objectID[i] = 0; } } @@ -615,66 +579,6 @@ unsigned int FrameBufferAttachment::getTextureArrayLayer() const * FrameBufferObject **************************************************************************/ -/////////////////////////////////////////////////////////////////////////// -// static cache of glRenderbuffers flagged for deletion, which will actually -// be deleted in the correct GL context. - -typedef std::list FrameBufferObjectHandleList; -typedef osg::buffered_object DeletedFrameBufferObjectCache; - -static OpenThreads::Mutex s_mutex_deletedFrameBufferObjectCache; -static DeletedFrameBufferObjectCache s_deletedFrameBufferObjectCache; - -void FrameBufferObject::deleteFrameBufferObject(unsigned int contextID, GLuint rb) -{ - if( rb ) - { - OpenThreads::ScopedLock lock(s_mutex_deletedFrameBufferObjectCache); - - // add glProgram to the cache for the appropriate context. - s_deletedFrameBufferObjectCache[contextID].push_back(rb); - } -} - -void FrameBufferObject::flushDeletedFrameBufferObjects(unsigned int contextID,double /*currentTime*/, double& availableTime) -{ - // if no time available don't try to flush objects. - if (availableTime<=0.0) return; - - const GLExtensions* extensions = GLExtensions::Get(contextID,true); - if(!extensions || !extensions->isFrameBufferObjectSupported ) return; - - const osg::Timer& timer = *osg::Timer::instance(); - osg::Timer_t start_tick = timer.tick(); - double elapsedTime = 0.0; - - { - OpenThreads::ScopedLock lock(s_mutex_deletedFrameBufferObjectCache); - - FrameBufferObjectHandleList& pList = s_deletedFrameBufferObjectCache[contextID]; - for(FrameBufferObjectHandleList::iterator titr=pList.begin(); - titr!=pList.end() && elapsedTimeglDeleteFramebuffers(1, &(*titr) ); - titr = pList.erase( titr ); - elapsedTime = timer.delta_s(start_tick,timer.tick()); - } - } - - availableTime -= elapsedTime; -} - -void FrameBufferObject::discardDeletedFrameBufferObjects(unsigned int contextID) -{ - OpenThreads::ScopedLock lock(s_mutex_deletedFrameBufferObjectCache); - FrameBufferObjectHandleList& pList = s_deletedFrameBufferObjectCache[contextID]; - - pList.clear(); -} - - - FrameBufferObject::FrameBufferObject() : StateAttribute() { @@ -691,7 +595,7 @@ FrameBufferObject::~FrameBufferObject() { for(unsigned i=0; i<_fboID.size(); ++i) { - if (_fboID[i]) deleteFrameBufferObject(i, _fboID[i]); + if (_fboID[i]) osg::get(i)->deleteGLObject(_fboID[i]); } } @@ -709,7 +613,7 @@ void FrameBufferObject::releaseGLObjects(osg::State* state) const unsigned int contextID = state->getContextID(); if (_fboID[contextID]) { - deleteFrameBufferObject(contextID, _fboID[contextID]); + osg::get(contextID)->deleteGLObject(_fboID[contextID]); _fboID[contextID] = 0; } } @@ -719,7 +623,7 @@ void FrameBufferObject::releaseGLObjects(osg::State* state) const { if (_fboID[i]) { - deleteFrameBufferObject(i, _fboID[i]); + osg::get(i)->deleteGLObject(_fboID[i]); _fboID[i] = 0; } } diff --git a/src/osg/GLObjects.cpp b/src/osg/GLObjects.cpp index 195e682c3..e5e470263 100644 --- a/src/osg/GLObjects.cpp +++ b/src/osg/GLObjects.cpp @@ -12,102 +12,139 @@ */ #include -#include #include #include -#include -#include #include -#include -#include +#include + +using namespace osg; void osg::flushDeletedGLObjects(unsigned int contextID, double currentTime, double& availableTime) { -#ifdef OSG_GL_DISPLAYLISTS_AVAILABLE - osg::Drawable::flushDeletedDisplayLists(contextID,availableTime); -#endif - -#ifdef OSG_GL_FIXED_FUNCTION_AVAILABLE - osg::FragmentProgram::flushDeletedFragmentProgramObjects(contextID,currentTime,availableTime); - osg::VertexProgram::flushDeletedVertexProgramObjects(contextID,currentTime,availableTime); -#endif - - osg::GLBufferObject::flushDeletedBufferObjects(contextID,currentTime,availableTime); - osg::FrameBufferObject::flushDeletedFrameBufferObjects(contextID,currentTime,availableTime); - osg::RenderBuffer::flushDeletedRenderBuffers(contextID,currentTime,availableTime); - osg::Program::flushDeletedGlPrograms(contextID,currentTime,availableTime); - osg::Shader::flushDeletedGlShaders(contextID,currentTime,availableTime); - osg::Texture::flushDeletedTextureObjects(contextID,currentTime,availableTime); - osg::OcclusionQueryNode::flushDeletedQueryObjects(contextID,currentTime,availableTime); + osg::getContextData(contextID)->flushDeletedGLObjects(currentTime, availableTime); } void osg::flushAllDeletedGLObjects(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::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::OcclusionQueryNode::flushDeletedQueryObjects(contextID,currentTime,availableTime); + osg::getContextData(contextID)->flushAllDeletedGLObjects(); } - 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); + osg::getContextData(contextID)->deleteAllGLObjects(); } - void osg::discardAllGLObjects(unsigned int contextID) { -#ifdef OSG_GL_DISPLAYLISTS_AVAILABLE - osg::Drawable::discardAllDeletedDisplayLists(contextID); -#endif - -#ifdef OSG_GL_FIXED_FUNCTION_AVAILABLE - osg::FragmentProgram::discardDeletedFragmentProgramObjects(contextID); - osg::VertexProgram::discardDeletedVertexProgramObjects(contextID); -#endif - - 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::OcclusionQueryNode::discardDeletedQueryObjects(contextID); + osg::getContextData(contextID)->discardAllGLObjects(); +} + +///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +// +// GraphicsObject +// +GraphicsObject::GraphicsObject() +{ +// OSG_NOTICE<<"GraphicsObject::GraphicsObject() "< lock(_mutex); + + for(GLObjectHandleList::iterator itr = _deleteGLObjectHandles.begin(); + itr != _deleteGLObjectHandles.end() && elapsedTime lock(_mutex); + + for(GLObjectHandleList::iterator itr = _deleteGLObjectHandles.begin(); + itr != _deleteGLObjectHandles.end(); + ++itr) + { + deleteGLObject( *itr ); + } + _deleteGLObjectHandles.clear(); +} + +void GLObjectManager::deleteAllGLObjects() +{ + OSG_INFO<<"void "<<_name<<"::deleteAllGLObjects() : Not Implementated"< lock(_mutex); + + _deleteGLObjectHandles.clear(); +} + +void GLObjectManager::sheduleGLObjectForDeletion(GLuint globj) +{ + OpenThreads::ScopedLock lock(_mutex); + + // add glProgram to the cache for the appropriate context. + _deleteGLObjectHandles.push_back(globj); +} + + +GLuint GLObjectManager::createGLObject() +{ + OSG_INFO<<"void "<<_name<<"::createGLObject() : Not Implementated"< #include #include +#include #include #include @@ -232,7 +233,7 @@ bool GraphicsContext::Traits::getContextVersion(unsigned int& major, unsigned in return true; } - +#if 0 class ContextData { public: @@ -261,197 +262,63 @@ public: osg::ref_ptr _compileContext; }; - - -typedef std::map ContextIDMap; -static ContextIDMap s_contextIDMap; -static OpenThreads::ReentrantMutex s_contextIDMapMutex; -static GraphicsContext::GraphicsContexts s_registeredContexts; +#endif unsigned int GraphicsContext::createNewContextID() { - OpenThreads::ScopedLock lock(s_contextIDMapMutex); - - // first check to see if we can reuse contextID; - for(ContextIDMap::iterator itr = s_contextIDMap.begin(); - itr != s_contextIDMap.end(); - ++itr) - { - if (itr->second._numContexts == 0) - { - - // reuse contextID; - itr->second._numContexts = 1; - - OSG_INFO<<"GraphicsContext::createNewContextID() reusing contextID="<first<first; - } - } - - unsigned int contextID = s_contextIDMap.size(); - s_contextIDMap[contextID]._numContexts = 1; - - OSG_INFO<<"GraphicsContext::createNewContextID() creating contextID="<setMaxNumberOfGraphicsContexts( contextID + 1 ); - - return contextID; + return ContextData::createNewContextID(); } unsigned int GraphicsContext::getMaxContextID() { - OpenThreads::ScopedLock lock(s_contextIDMapMutex); - unsigned int maxContextID = 0; - for(ContextIDMap::iterator itr = s_contextIDMap.begin(); - itr != s_contextIDMap.end(); - ++itr) - { - if (itr->first > maxContextID) maxContextID = itr->first; - } - return maxContextID; + return ContextData::getMaxContextID(); } void GraphicsContext::incrementContextIDUsageCount(unsigned int contextID) { - OpenThreads::ScopedLock lock(s_contextIDMapMutex); - - s_contextIDMap[contextID].incrementUsageCount(); - - OSG_INFO<<"GraphicsContext::incrementContextIDUsageCount("< lock(s_contextIDMapMutex); - - if (s_contextIDMap[contextID]._numContexts!=0) - { - s_contextIDMap[contextID].decrementUsageCount(); - } - else - { - OSG_NOTICE<<"Warning: decrementContextIDUsageCount("< lock(s_contextIDMapMutex); - - GraphicsContexts::iterator itr = std::find(s_registeredContexts.begin(), s_registeredContexts.end(), gc); - if (itr != s_registeredContexts.end()) s_registeredContexts.erase(itr); - - s_registeredContexts.push_back(gc); + ContextData::registerGraphicsContext(gc); } void GraphicsContext::unregisterGraphicsContext(GraphicsContext* gc) { - OSG_INFO<<"GraphicsContext::unregisterGraphicsContext "< lock(s_contextIDMapMutex); - - GraphicsContexts::iterator itr = std::find(s_registeredContexts.begin(), s_registeredContexts.end(), gc); - if (itr != s_registeredContexts.end()) s_registeredContexts.erase(itr); + ContextData::unregisterGraphicsContext(gc); } GraphicsContext::GraphicsContexts GraphicsContext::getAllRegisteredGraphicsContexts() { - OSG_INFO<<"GraphicsContext::getAllRegisteredGraphicsContexts s_registeredContexts.size()="< lock(s_contextIDMapMutex); - return s_registeredContexts; + return ContextData::getAllRegisteredGraphicsContexts(); } GraphicsContext::GraphicsContexts GraphicsContext::getRegisteredGraphicsContexts(unsigned int contextID) { - GraphicsContexts contexts; - - OpenThreads::ScopedLock lock(s_contextIDMapMutex); - for(GraphicsContexts::iterator itr = s_registeredContexts.begin(); - itr != s_registeredContexts.end(); - ++itr) - { - GraphicsContext* gc = *itr; - if (gc->getState() && gc->getState()->getContextID()==contextID) contexts.push_back(gc); - } - - OSG_INFO<<"GraphicsContext::getRegisteredGraphicsContexts "< lock(s_contextIDMapMutex); - if (s_contextIDMap[contextID]._compileContext.valid()) return s_contextIDMap[contextID]._compileContext.get(); - } - - GraphicsContext::GraphicsContexts contexts = GraphicsContext::getRegisteredGraphicsContexts(contextID); - if (contexts.empty()) return 0; - - GraphicsContext* src_gc = contexts.front(); - const osg::GraphicsContext::Traits* src_traits = src_gc->getTraits(); - - osg::GraphicsContext::Traits* traits = new osg::GraphicsContext::Traits; - traits->screenNum = src_traits->screenNum; - traits->displayNum = src_traits->displayNum; - traits->hostName = src_traits->hostName; - traits->width = 100; - traits->height = 100; - traits->red = src_traits->red; - traits->green = src_traits->green; - traits->blue = src_traits->blue; - traits->alpha = src_traits->alpha; - traits->depth = src_traits->depth; - traits->sharedContext = src_gc; - traits->pbuffer = true; - - osg::ref_ptr gc = osg::GraphicsContext::createGraphicsContext(traits); - if (gc.valid() && gc->realize()) - { - OpenThreads::ScopedLock lock(s_contextIDMapMutex); - s_contextIDMap[contextID]._compileContext = gc; - OSG_NOTICE<<" succeeded GraphicsContext::createCompileContext."< lock(s_contextIDMapMutex); - s_contextIDMap[contextID]._compileContext = gc; + return ContextData::setCompileContext(contextID, gc); } GraphicsContext* GraphicsContext::getCompileContext(unsigned int contextID) { - // OSG_NOTICE<<"GraphicsContext::getCompileContext "< lock(s_contextIDMapMutex); - ContextIDMap::iterator itr = s_contextIDMap.find(contextID); - if (itr != s_contextIDMap.end()) return itr->second._compileContext.get(); - else return 0; + return ContextData::getCompileContext(contextID); } @@ -530,8 +397,8 @@ void GraphicsContext::close(bool callCloseImplementation) if (_state.valid()) { - OpenThreads::ScopedLock lock(s_contextIDMapMutex); - if (s_contextIDMap[_state->getContextID()]._numContexts>1) sharedContextExists = true; + osg::ContextData* cd = osg::getContextData(_state->getContextID()); + if (cd && cd->getNumContexts()>1) sharedContextExists = true; } // release all the OpenGL objects in the scene graphs associated with this @@ -1011,3 +878,4 @@ void SyncSwapBuffersCallback::swapBuffersImplementation(osg::GraphicsContext* gc //OSG_NOTICE<<"After swap"< #include #include +#include #include #include @@ -230,18 +231,22 @@ struct ClearQueriesCallback : public osg::Camera::DrawCallback }; -// static cache of deleted query objects which can only -// be completely deleted once the appropriate OpenGL context -// is set. -typedef std::list< GLuint > QueryObjectList; -typedef osg::buffered_object< QueryObjectList > DeletedQueryObjectCache; - -static OpenThreads::Mutex s_mutex_deletedQueryObjectCache; -static DeletedQueryObjectCache s_deletedQueryObjectCache; namespace osg { +class QueryObjectManager : public GLObjectManager +{ +public: + QueryObjectManager(unsigned int contextID) : GLObjectManager("QueryObjectManager", contextID) {} + + virtual void deleteGLObject(GLuint globj) + { + const GLExtensions* extensions = GLExtensions::Get(_contextID,true); + if (extensions->isGlslSupported) extensions->glDeleteQueries( 1L, &globj ); + } +}; + QueryGeometry::QueryGeometry( const std::string& oqnName ) : _oqnName( oqnName ) @@ -378,7 +383,11 @@ QueryGeometry::releaseGLObjects( osg::State* state ) const TestResult& tr = it->second; if (tr._contextID == contextID) { +#if 1 + osg::get(contextID)->sheduleGLObjectForDeletion(tr._id ); +#else QueryGeometry::deleteQueryObject( contextID, tr._id ); +#endif tr._init = false; } it++; @@ -389,52 +398,20 @@ QueryGeometry::releaseGLObjects( osg::State* state ) const void QueryGeometry::deleteQueryObject( unsigned int contextID, GLuint handle ) { - if (handle!=0) - { - OpenThreads::ScopedLock< OpenThreads::Mutex > lock( s_mutex_deletedQueryObjectCache ); - - // insert the handle into the cache for the appropriate context. - s_deletedQueryObjectCache[contextID].push_back( handle ); - } + osg::get(contextID)->sheduleGLObjectForDeletion(handle); } void -QueryGeometry::flushDeletedQueryObjects( unsigned int contextID, double /*currentTime*/, double& availableTime ) +QueryGeometry::flushDeletedQueryObjects( unsigned int contextID, double currentTime, double& availableTime ) { - // if no time available don't try to flush objects. - if (availableTime<=0.0) return; - - const osg::Timer& timer = *osg::Timer::instance(); - osg::Timer_t start_tick = timer.tick(); - double elapsedTime = 0.0; - - { - OpenThreads::ScopedLock lock(s_mutex_deletedQueryObjectCache); - - const osg::GLExtensions* extensions = osg::GLExtensions::Get( contextID, true ); - - QueryObjectList& qol = s_deletedQueryObjectCache[contextID]; - - for(QueryObjectList::iterator titr=qol.begin(); - titr!=qol.end() && elapsedTimeglDeleteQueries( 1L, &(*titr ) ); - titr = qol.erase(titr); - elapsedTime = timer.delta_s(start_tick,timer.tick()); - } - } - - availableTime -= elapsedTime; + osg::get(contextID)->flushDeletedGLObjects(currentTime, availableTime); } void QueryGeometry::discardDeletedQueryObjects( unsigned int contextID ) { - OpenThreads::ScopedLock< OpenThreads::Mutex > lock( s_mutex_deletedQueryObjectCache ); - QueryObjectList& qol = s_deletedQueryObjectCache[ contextID ]; - qol.clear(); + osg::get(contextID)->discardAllGLObjects(); } // End support classes diff --git a/src/osg/Program.cpp b/src/osg/Program.cpp index 6014f9e00..f60130a7d 100644 --- a/src/osg/Program.cpp +++ b/src/osg/Program.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -39,62 +40,17 @@ using namespace osg; -/////////////////////////////////////////////////////////////////////////// -// static cache of glPrograms flagged for deletion, which will actually -// be deleted in the correct GL context. - -typedef std::list GlProgramHandleList; -typedef osg::buffered_object DeletedGlProgramCache; - -static OpenThreads::Mutex s_mutex_deletedGlProgramCache; -static DeletedGlProgramCache s_deletedGlProgramCache; - -void Program::deleteGlProgram(unsigned int contextID, GLuint program) +class GLProgramManager : public GLObjectManager { - if( program ) +public: + GLProgramManager(unsigned int contextID) : GLObjectManager("GLProgramManager", contextID) {} + + virtual void deleteGLObject(GLuint globj) { - OpenThreads::ScopedLock lock(s_mutex_deletedGlProgramCache); - - // add glProgram to the cache for the appropriate context. - s_deletedGlProgramCache[contextID].push_back(program); + const GLExtensions* extensions = GLExtensions::Get(_contextID,true); + if (extensions->isGlslSupported) extensions->glDeleteProgram( globj ); } -} - -void Program::flushDeletedGlPrograms(unsigned int contextID,double /*currentTime*/, double& availableTime) -{ - // if no time available don't try to flush objects. - if (availableTime<=0.0) return; - - OpenThreads::ScopedLock lock(s_mutex_deletedGlProgramCache); - const GLExtensions* extensions = GLExtensions::Get(contextID,true); - if( ! extensions->isGlslSupported ) return; - - const osg::Timer& timer = *osg::Timer::instance(); - osg::Timer_t start_tick = timer.tick(); - double elapsedTime = 0.0; - - { - - GlProgramHandleList& pList = s_deletedGlProgramCache[contextID]; - for(GlProgramHandleList::iterator titr=pList.begin(); - titr!=pList.end() && elapsedTimeglDeleteProgram( *titr ); - titr = pList.erase( titr ); - elapsedTime = timer.delta_s(start_tick,timer.tick()); - } - } - - availableTime -= elapsedTime; -} - -void Program::discardDeletedGlPrograms(unsigned int contextID) -{ - OpenThreads::ScopedLock lock(s_mutex_deletedGlProgramCache); - GlProgramHandleList& pList = s_deletedGlProgramCache[contextID]; - pList.clear(); -} +}; /////////////////////////////////////////////////////////////////////////// @@ -733,7 +689,7 @@ Program::PerContextProgram::~PerContextProgram() { if (_ownsProgramHandle) { - Program::deleteGlProgram( _contextID, _glProgramHandle ); + osg::get(_contextID)->deleteGLObject(_glProgramHandle); } } diff --git a/src/osg/Shader.cpp b/src/osg/Shader.cpp index 794201f16..4f936322f 100644 --- a/src/osg/Shader.cpp +++ b/src/osg/Shader.cpp @@ -32,12 +32,21 @@ #include #include #include - -#include -#include +#include using namespace osg; +class GLShaderManager : public GLObjectManager +{ +public: + GLShaderManager(unsigned int contextID) : GLObjectManager("GLShaderManager",contextID) {} + + virtual void deleteGLObject(GLuint globj) + { + const GLExtensions* extensions = GLExtensions::Get(_contextID,true); + if (extensions->isGlslSupported) extensions->glDeleteShader( globj ); + } +}; /////////////////////////////////////////////////////////////////////////////////// // @@ -151,64 +160,6 @@ ShaderBinary* ShaderBinary::readShaderBinaryFile(const std::string& fileName) return shaderBinary.release(); } -/////////////////////////////////////////////////////////////////////////// -// static cache of glShaders flagged for deletion, which will actually -// be deleted in the correct GL context. - -typedef std::list GlShaderHandleList; -typedef osg::buffered_object DeletedGlShaderCache; - -static OpenThreads::Mutex s_mutex_deletedGlShaderCache; -static DeletedGlShaderCache s_deletedGlShaderCache; - -void Shader::deleteGlShader(unsigned int contextID, GLuint shader) -{ - if( shader ) - { - OpenThreads::ScopedLock lock(s_mutex_deletedGlShaderCache); - - // add glShader to the cache for the appropriate context. - s_deletedGlShaderCache[contextID].push_back(shader); - } -} - -void Shader::flushDeletedGlShaders(unsigned int contextID,double /*currentTime*/, double& availableTime) -{ - // if no time available don't try to flush objects. - if (availableTime<=0.0) return; - - const GLExtensions* extensions = GLExtensions::Get(contextID,true); - if( ! extensions->isGlslSupported ) return; - - const osg::Timer& timer = *osg::Timer::instance(); - osg::Timer_t start_tick = timer.tick(); - double elapsedTime = 0.0; - - { - OpenThreads::ScopedLock lock(s_mutex_deletedGlShaderCache); - - GlShaderHandleList& pList = s_deletedGlShaderCache[contextID]; - for(GlShaderHandleList::iterator titr=pList.begin(); - titr!=pList.end() && elapsedTimeglDeleteShader( *titr ); - titr = pList.erase( titr ); - elapsedTime = timer.delta_s(start_tick,timer.tick()); - } - } - - availableTime -= elapsedTime; -} - -void Shader::discardDeletedGlShaders(unsigned int contextID) -{ - OpenThreads::ScopedLock lock(s_mutex_deletedGlShaderCache); - - GlShaderHandleList& pList = s_deletedGlShaderCache[contextID]; - pList.clear(); -} - /////////////////////////////////////////////////////////////////////////// // osg::Shader /////////////////////////////////////////////////////////////////////////// @@ -513,7 +464,7 @@ Shader::PerContextShader::PerContextShader(const Shader* shader, unsigned int co Shader::PerContextShader::~PerContextShader() { - Shader::deleteGlShader( _contextID, _glShaderHandle ); + osg::get(_contextID)->deleteGLObject(_glShaderHandle); } diff --git a/src/osg/State.cpp b/src/osg/State.cpp index f81257b09..fba546105 100644 --- a/src/osg/State.cpp +++ b/src/osg/State.cpp @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -301,14 +302,14 @@ void State::setInitialViewMatrix(const osg::RefMatrix* matrix) void State::setMaxTexturePoolSize(unsigned int size) { _maxTexturePoolSize = size; - osg::Texture::getTextureObjectManager(getContextID())->setMaxTexturePoolSize(size); + osg::get(_contextID)->setMaxTexturePoolSize(size); OSG_INFO<<"osg::State::_maxTexturePoolSize="<<_maxTexturePoolSize<setMaxGLBufferObjectPoolSize(_maxBufferObjectPoolSize); + osg::get(_contextID)->setMaxGLBufferObjectPoolSize(_maxBufferObjectPoolSize); OSG_INFO<<"osg::State::_maxBufferObjectPoolSize="<<_maxBufferObjectPoolSize< #include #include +#include #include #include @@ -247,6 +248,11 @@ void Texture::TextureObject::bind() if (_set) _set->moveToBack(this); } +void Texture::TextureObject::release() +{ + if (_set) _set->orphan(this); +} + void Texture::TextureObject::setAllocated(GLint numMipmapLevels, GLenum internalFormat, GLsizei width, @@ -347,7 +353,7 @@ void Texture::TextureProfile::computeSize() // // New texture object manager // -Texture::TextureObjectSet::TextureObjectSet(TextureObjectManager* parent, const TextureProfile& profile): +TextureObjectSet::TextureObjectSet(TextureObjectManager* parent, const Texture::TextureProfile& profile): _parent(parent), _contextID(parent->getContextID()), _profile(profile), @@ -357,7 +363,7 @@ Texture::TextureObjectSet::TextureObjectSet(TextureObjectManager* parent, const { } -Texture::TextureObjectSet::~TextureObjectSet() +TextureObjectSet::~TextureObjectSet() { #if 0 OSG_NOTICE<<"TextureObjectSet::~TextureObjectSet(), _numOfTextureObjects="<<_numOfTextureObjects<_next)->_previous != to) { - OSG_NOTICE<<"Texture::TextureObjectSet::checkConsistency() : Error (to->_next)->_previous != to "<_next)->_previous != to "<get(); + Texture::TextureObject* to = itr->get(); _orphanedTextureObjects.push_back(to); @@ -443,15 +449,15 @@ void Texture::TextureObjectSet::handlePendingOrphandedTextureObjects() } -void Texture::TextureObjectSet::deleteAllTextureObjects() +void TextureObjectSet::deleteAllTextureObjects() { - // OSG_NOTICE<<"Texture::TextureObjectSet::deleteAllTextureObjects()"< lock(_mutex); if (!_pendingOrphanedTextureObjects.empty()) { - // OSG_NOTICE<<"Texture::TextureObjectSet::flushDeletedTextureObjects(..) handling orphans"< glto = to; + ref_ptr glto = to; to = to->_next; @@ -489,14 +495,14 @@ void Texture::TextureObjectSet::deleteAllTextureObjects() // OSG_NOTICE<<"done GLBufferObjectSet::deleteAllGLBufferObjects()"< glto = to; + ref_ptr glto = to; to = to->_next; @@ -524,19 +530,19 @@ void Texture::TextureObjectSet::discardAllTextureObjects() _parent->getNumberDeleted() += numDeleted; } -void Texture::TextureObjectSet::flushAllDeletedTextureObjects() +void TextureObjectSet::flushAllDeletedTextureObjects() { - // OSG_NOTICE<<"Texture::TextureObjectSet::flushAllDeletedTextureObjects()"< lock(_mutex); if (!_pendingOrphanedTextureObjects.empty()) { - // OSG_NOTICE<<"Texture::TextureObjectSet::flushDeletedTextureObjects(..) handling orphans"< lock(_mutex); if (!_pendingOrphanedTextureObjects.empty()) { - // OSG_NOTICE<<"Texture::TextureObjectSet::flushDeletedTextureObjects(..) handling orphans"< lock(_mutex); if (!_pendingOrphanedTextureObjects.empty()) { - // OSG_NOTICE<<"Texture::TextureObjectSet::flushDeletedTextureObjects(..) handling orphans"< lock(_mutex); if (!_pendingOrphanedTextureObjects.empty()) { - // OSG_NOTICE<<"Texture::TextureObjectSet::Texture::TextureObjectSet::makeSpace(..) handling orphans"< Texture::TextureObjectSet::takeFromOrphans(Texture* texture) +osg::ref_ptr TextureObjectSet::takeFromOrphans(Texture* texture) { // take front of orphaned list. - ref_ptr to = _orphanedTextureObjects.front(); + ref_ptr to = _orphanedTextureObjects.front(); // remove from orphan list. _orphanedTextureObjects.pop_front(); @@ -704,7 +710,7 @@ osg::ref_ptr Texture::TextureObjectSet::takeFromOrphans( } -osg::ref_ptr Texture::TextureObjectSet::takeOrGenerate(Texture* texture) +osg::ref_ptr TextureObjectSet::takeOrGenerate(Texture* texture) { // see if we can recyle TextureObject from the orphan list { @@ -733,7 +739,7 @@ osg::ref_ptr Texture::TextureObjectSet::takeOrGenerate(T OpenThreads::ScopedLock lock(_mutex); - ref_ptr to = _head; + ref_ptr to = _head; ref_ptr original_texture = to->getTexture(); @@ -761,7 +767,7 @@ osg::ref_ptr Texture::TextureObjectSet::takeOrGenerate(T GLuint id; glGenTextures( 1L, &id ); - osg::ref_ptr to = new Texture::TextureObject(const_cast(texture),id,_profile); + osg::ref_ptr to = new Texture::TextureObject(const_cast(texture),id,_profile); to->_set = this; ++_numOfTextureObjects; @@ -776,7 +782,7 @@ osg::ref_ptr Texture::TextureObjectSet::takeOrGenerate(T return to; } -void Texture::TextureObjectSet::moveToBack(Texture::TextureObject* to) +void TextureObjectSet::moveToBack(Texture::TextureObject* to) { #if 0 OSG_NOTICE<<"TextureObjectSet::moveToBack("<_previous!=0) { @@ -920,7 +926,7 @@ void Texture::TextureObjectSet::remove(Texture::TextureObject* to) to->_previous = 0; } -void Texture::TextureObjectSet::moveToSet(TextureObject* to, TextureObjectSet* set) +void TextureObjectSet::moveToSet(Texture::TextureObject* to, TextureObjectSet* set) { if (set==this) return; if (!set) return; @@ -935,10 +941,10 @@ void Texture::TextureObjectSet::moveToSet(TextureObject* to, TextureObjectSet* s set->addToBack(to); } -unsigned int Texture::TextureObjectSet::computeNumTextureObjectsInList() const +unsigned int TextureObjectSet::computeNumTextureObjectsInList() const { unsigned int num=0; - TextureObject* obj = _head; + Texture::TextureObject* obj = _head; while(obj!=NULL) { ++num; @@ -948,8 +954,8 @@ unsigned int Texture::TextureObjectSet::computeNumTextureObjectsInList() const } -Texture::TextureObjectManager::TextureObjectManager(unsigned int contextID): - _contextID(contextID), +TextureObjectManager::TextureObjectManager(unsigned int contextID): + GraphicsObjectManager("TextureObjectManager", contextID), _numActiveTextureObjects(0), _numOrphanedTextureObjects(0), _currTexturePoolSize(0), @@ -959,13 +965,16 @@ Texture::TextureObjectManager::TextureObjectManager(unsigned int contextID): _numDeleted(0), _deleteTime(0.0), _numGenerated(0), - _generateTime(0.0), - _numApplied(0), - _applyTime(0.0) + _generateTime(0.0) { } -void Texture::TextureObjectManager::setMaxTexturePoolSize(unsigned int size) +TextureObjectManager::~TextureObjectManager() +{ +} + + +void TextureObjectManager::setMaxTexturePoolSize(unsigned int size) { if (_maxTexturePoolSize == size) return; @@ -977,7 +986,7 @@ void Texture::TextureObjectManager::setMaxTexturePoolSize(unsigned int size) _maxTexturePoolSize = size; } -bool Texture::TextureObjectManager::makeSpace(unsigned int size) +bool TextureObjectManager::makeSpace(unsigned int size) { for(TextureSetMap::iterator itr = _textureSetMap.begin(); itr != _textureSetMap.end() && size>0; @@ -990,12 +999,12 @@ bool Texture::TextureObjectManager::makeSpace(unsigned int size) } -osg::ref_ptr Texture::TextureObjectManager::generateTextureObject(const Texture* texture, GLenum target) +osg::ref_ptr TextureObjectManager::generateTextureObject(const Texture* texture, GLenum target) { return generateTextureObject(texture, target, 0, 0, 0, 0, 0, 0); } -osg::ref_ptr Texture::TextureObjectManager::generateTextureObject(const Texture* texture, +osg::ref_ptr TextureObjectManager::generateTextureObject(const Texture* texture, GLenum target, GLint numMipmapLevels, GLenum internalFormat, @@ -1032,14 +1041,14 @@ Texture::TextureObject* Texture::generateAndAssignTextureObject( return _textureObjectBuffer[contextID].get(); } -Texture::TextureObjectSet* Texture::TextureObjectManager::getTextureObjectSet(const TextureProfile& profile) +TextureObjectSet* TextureObjectManager::getTextureObjectSet(const Texture::TextureProfile& profile) { - osg::ref_ptr& tos = _textureSetMap[profile]; - if (!tos) tos = new Texture::TextureObjectSet(this, profile); + osg::ref_ptr& tos = _textureSetMap[profile]; + if (!tos) tos = new TextureObjectSet(this, profile); return tos.get(); } -void Texture::TextureObjectManager::handlePendingOrphandedTextureObjects() +void TextureObjectManager::handlePendingOrphandedTextureObjects() { for(TextureSetMap::iterator itr = _textureSetMap.begin(); itr != _textureSetMap.end(); @@ -1049,12 +1058,8 @@ void Texture::TextureObjectManager::handlePendingOrphandedTextureObjects() } } -void Texture::TextureObjectManager::deleteAllTextureObjects() +void TextureObjectManager::deleteAllGLObjects() { - // OSG_NOTICE<<"Texture::TextureObjectManager::deleteAllTextureObjects() _contextID="<<_contextID<getFrameNumber(); else ++_frameNumber; @@ -1128,19 +1116,18 @@ void Texture::TextureObjectManager::newFrame(osg::FrameStamp* fs) ++_numFrames; } -void Texture::TextureObjectManager::reportStats(std::ostream& out) +void TextureObjectManager::reportStats(std::ostream& out) { double numFrames(_numFrames==0 ? 1.0 : _numFrames); out<<"TextureObjectMananger::reportStats()"<& Texture::getTextureObjectManager(unsigned int contextID) -{ - typedef osg::buffered_object< ref_ptr > TextureObjectManagerBuffer; - static TextureObjectManagerBuffer s_TextureObjectManager; - if (!s_TextureObjectManager[contextID]) s_TextureObjectManager[contextID] = new Texture::TextureObjectManager(contextID); - return s_TextureObjectManager[contextID]; -} - - osg::ref_ptr Texture::generateTextureObject(const Texture* texture, unsigned int contextID, GLenum target) { - return getTextureObjectManager(contextID)->generateTextureObject(texture, target); + return osg::get(contextID)->generateTextureObject(texture, target); } osg::ref_ptr Texture::generateTextureObject(const Texture* texture, unsigned int contextID, @@ -1237,37 +1211,7 @@ osg::ref_ptr Texture::generateTextureObject(const Textur GLsizei depth, GLint border) { - 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(); -} - -void Texture::discardAllDeletedTextureObjects(unsigned int contextID) -{ - getTextureObjectManager(contextID)->discardAllDeletedTextureObjects(); -} - -void Texture::flushDeletedTextureObjects(unsigned int contextID,double currentTime, double& availbleTime) -{ - getTextureObjectManager(contextID)->flushDeletedTextureObjects(currentTime, availbleTime); -} - -void Texture::releaseTextureObject(unsigned int contextID, Texture::TextureObject* to) -{ - getTextureObjectManager(contextID)->releaseTextureObject(to); + return osg::get(contextID)->generateTextureObject(texture,target,numMipmapLevels,internalFormat,width,height,depth,border); } //////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1453,7 +1397,7 @@ void Texture::dirtyTextureObject() { if (_textureObjectBuffer[i].valid()) { - Texture::releaseTextureObject(i, _textureObjectBuffer[i].get()); + _textureObjectBuffer[i]->release(); _textureObjectBuffer[i] = 0; } } @@ -2829,7 +2773,7 @@ void Texture::releaseGLObjects(State* state) const unsigned int contextID = state->getContextID(); if (_textureObjectBuffer[contextID].valid()) { - Texture::releaseTextureObject(contextID, _textureObjectBuffer[contextID].get()); + _textureObjectBuffer[contextID]->release(); _textureObjectBuffer[contextID] = 0; } diff --git a/src/osg/Texture1D.cpp b/src/osg/Texture1D.cpp index 9971b0663..a18b23b02 100644 --- a/src/osg/Texture1D.cpp +++ b/src/osg/Texture1D.cpp @@ -134,10 +134,6 @@ void Texture1D::apply(State& state) const // current OpenGL context. const unsigned int contextID = state.getContextID(); - Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID).get(); - ElapsedTime elapsedTime(&(tom->getApplyTime())); - tom->getNumberApplied()++; - // get the texture object for the current contextID. TextureObject* textureObject = getTextureObject(contextID); @@ -153,7 +149,7 @@ void Texture1D::apply(State& state) const if (!textureObject->match(GL_TEXTURE_1D, new_numMipmapLevels, _internalFormat, new_width, 1, 1, _borderWidth)) { - Texture::releaseTextureObject(contextID, _textureObjectBuffer[contextID].get()); + _textureObjectBuffer[contextID]->release(); _textureObjectBuffer[contextID] = 0; textureObject = 0; } diff --git a/src/osg/Texture2D.cpp b/src/osg/Texture2D.cpp index 453a6bd24..5cb0a459e 100644 --- a/src/osg/Texture2D.cpp +++ b/src/osg/Texture2D.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include using namespace osg; @@ -165,10 +166,6 @@ void Texture2D::apply(State& state) const // current OpenGL context. const unsigned int contextID = state.getContextID(); - Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID).get(); - ElapsedTime elapsedTime(&(tom->getApplyTime())); - tom->getNumberApplied()++; - // get the texture object for the current contextID. TextureObject* textureObject = getTextureObject(contextID); if (textureObject) @@ -186,7 +183,7 @@ void Texture2D::apply(State& state) const if (textureObjectInvalidated) { // OSG_NOTICE<<"Discarding TextureObject"<release(); _textureObjectBuffer[contextID] = 0; textureObject = 0; } diff --git a/src/osg/Texture2DArray.cpp b/src/osg/Texture2DArray.cpp index cb308e3a7..90257fbff 100644 --- a/src/osg/Texture2DArray.cpp +++ b/src/osg/Texture2DArray.cpp @@ -235,10 +235,6 @@ void Texture2DArray::apply(State& state) const // current OpenGL context. const unsigned int contextID = state.getContextID(); - Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID).get(); - ElapsedTime elapsedTime(&(tom->getApplyTime())); - tom->getNumberApplied()++; - const GLExtensions* extensions = state.get(); // if not supported, then return @@ -268,7 +264,7 @@ void Texture2DArray::apply(State& state) const if (!textureObject->match(GL_TEXTURE_2D_ARRAY_EXT, new_numMipmapLevels, _internalFormat, new_width, new_height, textureDepth, _borderWidth)) { - Texture::releaseTextureObject(contextID, _textureObjectBuffer[contextID].get()); + _textureObjectBuffer[contextID]->release(); _textureObjectBuffer[contextID] = 0; textureObject = 0; } diff --git a/src/osg/Texture2DMultisample.cpp b/src/osg/Texture2DMultisample.cpp index dbf759521..c723da906 100644 --- a/src/osg/Texture2DMultisample.cpp +++ b/src/osg/Texture2DMultisample.cpp @@ -93,10 +93,6 @@ void Texture2DMultisample::apply(State& state) const return; } - Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID).get(); - ElapsedTime elapsedTime(&(tom->getApplyTime())); - tom->getNumberApplied()++; - // get the texture object for the current contextID. TextureObject* textureObject = getTextureObject(contextID); diff --git a/src/osg/Texture3D.cpp b/src/osg/Texture3D.cpp index 9d23494a5..68e42fe42 100644 --- a/src/osg/Texture3D.cpp +++ b/src/osg/Texture3D.cpp @@ -204,10 +204,6 @@ void Texture3D::apply(State& state) const // current OpenGL context. const unsigned int contextID = state.getContextID(); - Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID).get(); - ElapsedTime elapsedTime(&(tom->getApplyTime())); - tom->getNumberApplied()++; - const GLExtensions* extensions = state.get(); if (!extensions->isTexture3DSupported) @@ -233,7 +229,7 @@ void Texture3D::apply(State& state) const if (!textureObject->match(GL_TEXTURE_3D, new_numMipmapLevels, _internalFormat, new_width, new_height, new_depth, _borderWidth)) { - Texture::releaseTextureObject(contextID, _textureObjectBuffer[contextID].get()); + _textureObjectBuffer[contextID]->release(); _textureObjectBuffer[contextID] = 0; textureObject = 0; } diff --git a/src/osg/TextureCubeMap.cpp b/src/osg/TextureCubeMap.cpp index c750ddcf6..6e447e2e8 100644 --- a/src/osg/TextureCubeMap.cpp +++ b/src/osg/TextureCubeMap.cpp @@ -199,10 +199,6 @@ void TextureCubeMap::apply(State& state) const // current OpenGL context. const unsigned int contextID = state.getContextID(); - Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID).get(); - ElapsedTime elapsedTime(&(tom->getApplyTime())); - tom->getNumberApplied()++; - const GLExtensions* extensions = state.get(); if (!extensions->isCubeMapSupported) @@ -226,7 +222,7 @@ void TextureCubeMap::apply(State& state) const if (!textureObject->match(GL_TEXTURE_CUBE_MAP, new_numMipmapLevels, _internalFormat, new_width, new_height, 1, _borderWidth)) { - Texture::releaseTextureObject(contextID, _textureObjectBuffer[contextID].get()); + _textureObjectBuffer[contextID]->release(); _textureObjectBuffer[contextID] = 0; textureObject = 0; } diff --git a/src/osg/TextureRectangle.cpp b/src/osg/TextureRectangle.cpp index ef167a1ae..e6e741e1c 100644 --- a/src/osg/TextureRectangle.cpp +++ b/src/osg/TextureRectangle.cpp @@ -167,11 +167,6 @@ void TextureRectangle::apply(State& state) const // current OpenGL context. const unsigned int contextID = state.getContextID(); - Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID).get(); - ElapsedTime elapsedTime(&(tom->getApplyTime())); - tom->getNumberApplied()++; - - // get the texture object for the current contextID. TextureObject* textureObject = getTextureObject(contextID); @@ -189,7 +184,7 @@ void TextureRectangle::apply(State& state) const if (!textureObject->match(GL_TEXTURE_RECTANGLE, new_numMipmapLevels, _internalFormat, new_width, new_height, 1, _borderWidth)) { - Texture::releaseTextureObject(contextID, _textureObjectBuffer[contextID].get()); + _textureObjectBuffer[contextID]->release(); _textureObjectBuffer[contextID] = 0; textureObject = 0; } diff --git a/src/osg/VertexProgram.cpp b/src/osg/VertexProgram.cpp index ff22c4c22..a8a45981e 100644 --- a/src/osg/VertexProgram.cpp +++ b/src/osg/VertexProgram.cpp @@ -15,71 +15,21 @@ #include #include #include - -#include - -#include -#include +#include using namespace osg; -// static cache of deleted vertex programs which can only -// by completely deleted once the appropriate OpenGL context -// is set. -typedef std::list VertexProgramObjectList; -typedef osg::buffered_object DeletedVertexProgramObjectCache; - -static OpenThreads::Mutex s_mutex_deletedVertexProgramObjectCache; -static DeletedVertexProgramObjectCache s_deletedVertexProgramObjectCache; - -void VertexProgram::deleteVertexProgramObject(unsigned int contextID,GLuint handle) +class GLVertexProgramManager : public GLObjectManager { - if (handle!=0) +public: + GLVertexProgramManager(unsigned int contextID) : GLObjectManager("GLVertexProgramManager",contextID) {} + + virtual void deleteGLObject(GLuint globj) { - OpenThreads::ScopedLock lock(s_mutex_deletedVertexProgramObjectCache); - - // insert the handle into the cache for the appropriate context. - s_deletedVertexProgramObjectCache[contextID].push_back(handle); + const GLExtensions* extensions = GLExtensions::Get(_contextID,true); + if (extensions->isGlslSupported) extensions->glDeletePrograms(1, &globj ); } -} - - -void VertexProgram::flushDeletedVertexProgramObjects(unsigned int contextID,double /*currentTime*/, double& availableTime) -{ - // if no time available don't try to flush objects. - if (availableTime<=0.0) return; - - const osg::Timer& timer = *osg::Timer::instance(); - osg::Timer_t start_tick = timer.tick(); - double elapsedTime = 0.0; - - { - OpenThreads::ScopedLock lock(s_mutex_deletedVertexProgramObjectCache); - - const GLExtensions* extensions = GLExtensions::Get(contextID,true); - - VertexProgramObjectList& vpol = s_deletedVertexProgramObjectCache[contextID]; - - for(VertexProgramObjectList::iterator titr=vpol.begin(); - titr!=vpol.end() && elapsedTimeglDeletePrograms( 1L, &(*titr ) ); - titr = vpol.erase(titr); - elapsedTime = timer.delta_s(start_tick,timer.tick()); - } - } - - availableTime -= elapsedTime; -} - -void VertexProgram::discardDeletedVertexProgramObjects(unsigned int contextID) -{ - OpenThreads::ScopedLock lock(s_mutex_deletedVertexProgramObjectCache); - VertexProgramObjectList& vpol = s_deletedVertexProgramObjectCache[contextID]; - vpol.clear(); -} - +}; VertexProgram::VertexProgram() { @@ -117,7 +67,7 @@ void VertexProgram::dirtyVertexProgramObject() { if (_vertexProgramIDList[i] != 0) { - VertexProgram::deleteVertexProgramObject(i,_vertexProgramIDList[i]); + osg::get(i)->deleteGLObject(_vertexProgramIDList[i]); _vertexProgramIDList[i] = 0; } } @@ -207,7 +157,7 @@ void VertexProgram::releaseGLObjects(State* state) const unsigned int contextID = state->getContextID(); if (_vertexProgramIDList[contextID] != 0) { - VertexProgram::deleteVertexProgramObject(contextID,_vertexProgramIDList[contextID]); + osg::get(contextID)->deleteGLObject(_vertexProgramIDList[contextID]); _vertexProgramIDList[contextID] = 0; } } diff --git a/src/osgUtil/RenderStage.cpp b/src/osgUtil/RenderStage.cpp index a1fae308c..19a733303 100644 --- a/src/osgUtil/RenderStage.cpp +++ b/src/osgUtil/RenderStage.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -537,12 +538,8 @@ void RenderStage::runCameraSetUp(osg::RenderInfo& renderInfo) fbo = 0; // clean up. - double availableTime = 100.0f; - double currentTime = state.getFrameStamp()?state.getFrameStamp()->getReferenceTime():0.0; - osg::RenderBuffer::flushDeletedRenderBuffers(state.getContextID(),currentTime,availableTime); - osg::FrameBufferObject::flushDeletedFrameBufferObjects(state.getContextID(),currentTime,availableTime); - - + osg::get(state.getContextID())->flushAllDeletedGLObjects(); + osg::get(state.getContextID())->flushAllDeletedGLObjects(); } else { @@ -568,10 +565,8 @@ void RenderStage::runCameraSetUp(osg::RenderInfo& renderInfo) _resolveFbo = 0; // clean up. - double availableTime = 100.0f; - double currentTime = state.getFrameStamp()?state.getFrameStamp()->getReferenceTime():0.0; - osg::RenderBuffer::flushDeletedRenderBuffers(state.getContextID(),currentTime,availableTime); - osg::FrameBufferObject::flushDeletedFrameBufferObjects(state.getContextID(),currentTime,availableTime); + osg::get(state.getContextID())->flushAllDeletedGLObjects(); + osg::get(state.getContextID())->flushAllDeletedGLObjects(); } else { diff --git a/src/osgUtil/SceneView.cpp b/src/osgUtil/SceneView.cpp index 3ea078c13..fa195c450 100644 --- a/src/osgUtil/SceneView.cpp +++ b/src/osgUtil/SceneView.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include @@ -579,7 +580,7 @@ void SceneView::computeRightEyeViewport(const osg::Viewport *viewport) void SceneView::setLightingMode(LightingMode mode) { if (mode==_lightingMode) return; - + osg::StateSet* stateSetToModify = _secondaryStateSet.valid() ? _secondaryStateSet.get() : _globalStateSet.get(); if (_lightingMode!=NO_SCENEVIEW_LIGHT) @@ -963,11 +964,7 @@ void SceneView::draw() state->initializeExtensionProcs(); - osg::Texture::TextureObjectManager* tom = osg::Texture::getTextureObjectManager(state->getContextID()).get(); - tom->newFrame(state->getFrameStamp()); - - osg::GLBufferObjectManager* bom = osg::GLBufferObjectManager::getGLBufferObjectManager(state->getContextID()).get(); - bom->newFrame(state->getFrameStamp()); + osg::get(state->getContextID())->newFrame(state->getFrameStamp()); if (!_initCalled) init();