Permalink
Browse files

Added State::releaseGLObjects() and ShaderComposer::releaseGLObjects(…

…) to avoid problems with cleanup of graphics context
  • Loading branch information...
1 parent f714917 commit 67784584c6393920bba5d2ff40e3f9ef9919b081 @osg-training-guest osg-training-guest committed Oct 21, 2013
Showing with 67 additions and 4 deletions.
  1. +2 −1 include/osg/ShaderComposer
  2. +3 −0 include/osg/State
  3. +4 −2 src/osg/GraphicsContext.cpp
  4. +6 −0 src/osg/ShaderComposer.cpp
  5. +52 −1 src/osg/State.cpp
@@ -35,11 +35,12 @@ class OSG_EXPORT ShaderComposer : public osg::Object
virtual osg::Program* getOrCreateProgram(const ShaderComponents& shaderComponents);
-
typedef std::vector< const osg::Shader* > Shaders;
virtual osg::Shader* composeMain(const Shaders& shaders);
virtual void addShaderToProgram(Program* program, const Shaders& shaders);
+ void releaseGLObjects(osg::State* state);
+
protected:
virtual ~ShaderComposer();
View
@@ -218,6 +218,9 @@ class OSG_EXPORT State : public Referenced, public Observer
/** Copy the modes and attributes which capture the current state.*/
void captureCurrentState(StateSet& stateset) const;
+ /** Release all OpenGL objects associated cached by this osg::State object.*/
+ void releaseGLObjects();
+
/** reset the state object to an empty stack.*/
void reset();
@@ -546,6 +546,10 @@ void GraphicsContext::close(bool callCloseImplementation)
}
}
+ if (_state.valid())
+ {
+ _state->releaseGLObjects();
+ }
if (callCloseImplementation && _state.valid() && isRealized())
{
@@ -569,8 +573,6 @@ void GraphicsContext::close(bool callCloseImplementation)
osg::flushAllDeletedGLObjects(_state->getContextID());
}
- _state->reset();
-
releaseContext();
}
else
@@ -37,6 +37,12 @@ ShaderComposer::~ShaderComposer()
OSG_INFO<<"ShaderComposer::~ShaderComposer() "<<this<<std::endl;
}
+void ShaderComposer::releaseGLObjects(osg::State* state)
+{
+ _programMap.clear();
+ _shaderMainMap.clear();
+}
+
osg::Program* ShaderComposer::getOrCreateProgram(const ShaderComponents& shaderComponents)
{
ProgramMap::iterator itr = _programMap.find(shaderComponents);
View
@@ -155,6 +155,57 @@ void State::objectDeleted(void* object)
}
}
+
+void State::releaseGLObjects()
+{
+ // release any GL objects held by the shader composer
+ _shaderComposer->releaseGLObjects(this);
+
+ // release any StateSet's on the stack
+ for(StateSetStack::iterator itr = _stateStateStack.begin();
+ itr != _stateStateStack.end();
+ ++itr)
+ {
+ (*itr)->releaseGLObjects(this);
+ }
+
+ _modeMap.clear();
+ _textureModeMapList.clear();
+
+ // release any cached attributes
+ for(AttributeMap::iterator aitr = _attributeMap.begin();
+ aitr != _attributeMap.end();
+ ++aitr)
+ {
+ AttributeStack& as = aitr->second;
+ if (as.global_default_attribute.valid())
+ {
+ as.global_default_attribute->releaseGLObjects(this);
+ }
+ }
+ _attributeMap.clear();
+
+ // release any cached texture attributes
+ for(TextureAttributeMapList::iterator itr = _textureAttributeMapList.begin();
+ itr != _textureAttributeMapList.end();
+ ++itr)
+ {
+ AttributeMap& attributeMap = *itr;
+ for(AttributeMap::iterator aitr = attributeMap.begin();
+ aitr != attributeMap.end();
+ ++aitr)
+ {
+ AttributeStack& as = aitr->second;
+ if (as.global_default_attribute.valid())
+ {
+ as.global_default_attribute->releaseGLObjects(this);
+ }
+ }
+ }
+
+ _textureAttributeMapList.clear();
+}
+
void State::reset()
{
@@ -911,7 +962,7 @@ void State::initializeExtensionProcs()
if ( osg::getGLVersionNumber() >= 2.0 || osg::isGLExtensionSupported(_contextID,"GL_ARB_vertex_shader") || OSG_GLES2_FEATURES)
{
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS,&_glMaxTextureUnits);
- if(OSG_GLES2_FEATURES)
+ if(OSG_GLES2_FEATURES)
_glMaxTextureCoords = _glMaxTextureUnits;
else
glGetIntegerv(GL_MAX_TEXTURE_COORDS,&_glMaxTextureCoords);

0 comments on commit 6778458

Please sign in to comment.