From 69557c060b650c140e709ee7353c2ab1c6dae4bc Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 6 Oct 2009 13:42:12 +0000 Subject: [PATCH] Fixed the TextureObject's + GLBufferObject reassignment to a new set. Disabled the unref after apply for a Texture classes when the texture pool is enabled --- include/osg/BufferObject | 1 + include/osg/Texture | 1 + src/osg/BufferObject.cpp | 27 +++++++++++++++++++-------- src/osg/Texture.cpp | 32 ++++++++++++++++++++------------ src/osg/Texture1D.cpp | 2 +- src/osg/Texture2DArray.cpp | 2 +- src/osg/Texture3D.cpp | 2 +- src/osg/TextureCubeMap.cpp | 2 +- src/osg/TextureRectangle.cpp | 33 +++++++++++++++++++++++---------- 9 files changed, 68 insertions(+), 34 deletions(-) diff --git a/include/osg/BufferObject b/include/osg/BufferObject index b69000cc8..b185746d1 100644 --- a/include/osg/BufferObject +++ b/include/osg/BufferObject @@ -362,6 +362,7 @@ class OSG_EXPORT GLBufferObjectSet : public Referenced void addToBack(GLBufferObject* to); void orphan(GLBufferObject* to); void remove(GLBufferObject* to); + void moveToSet(GLBufferObject* to, GLBufferObjectSet* set); unsigned int size() const { return _profile._size * _numOfGLBufferObjects; } diff --git a/include/osg/Texture b/include/osg/Texture index 90e051873..ec6e5f07e 100644 --- a/include/osg/Texture +++ b/include/osg/Texture @@ -1016,6 +1016,7 @@ class OSG_EXPORT Texture : public osg::StateAttribute 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; } diff --git a/src/osg/BufferObject.cpp b/src/osg/BufferObject.cpp index d6acf478e..6db92e18b 100644 --- a/src/osg/BufferObject.cpp +++ b/src/osg/BufferObject.cpp @@ -174,16 +174,10 @@ void GLBufferObject::compileBuffer() osg::notify(osg::NOTICE)<<"newTotalSize="<_previous = 0; } +void Texture::TextureObjectSet::moveToSet(TextureObject* to, TextureObjectSet* set) +{ + if (set==this) return; + if (!set) return; + + // remove 'to' from original set + --_numOfTextureObjects; + remove(to); + + // register 'to' with new set. + to->_set = set; + ++set->_numOfTextureObjects; + set->addToBack(to); +} + Texture::TextureObjectManager::TextureObjectManager(unsigned int contextID): _contextID(contextID), diff --git a/src/osg/Texture1D.cpp b/src/osg/Texture1D.cpp index ca3821a37..3bd02e1ba 100644 --- a/src/osg/Texture1D.cpp +++ b/src/osg/Texture1D.cpp @@ -190,7 +190,7 @@ void Texture1D::apply(State& state) const _textureObjectBuffer[contextID] = textureObject; - if (_unrefImageDataAfterApply && areAllTextureObjectsLoaded() && _image->getDataVariance()==STATIC) + if (state.getMaxTexturePoolSize()==0 && _unrefImageDataAfterApply && areAllTextureObjectsLoaded() && _image->getDataVariance()==STATIC) { Texture1D* non_const_this = const_cast(this); non_const_this->_image = 0; diff --git a/src/osg/Texture2DArray.cpp b/src/osg/Texture2DArray.cpp index c2411f941..12ccb2184 100644 --- a/src/osg/Texture2DArray.cpp +++ b/src/osg/Texture2DArray.cpp @@ -311,7 +311,7 @@ void Texture2DArray::apply(State& state) const _textureObjectBuffer[contextID] = textureObject; // no idea what this for ;-) - if (_unrefImageDataAfterApply && areAllTextureObjectsLoaded()) + if (state.getMaxTexturePoolSize()==0 && _unrefImageDataAfterApply && areAllTextureObjectsLoaded()) { Texture2DArray* non_const_this = const_cast(this); for (int n=0; n<_textureDepth; n++) diff --git a/src/osg/Texture3D.cpp b/src/osg/Texture3D.cpp index 10d202dab..23d110adc 100644 --- a/src/osg/Texture3D.cpp +++ b/src/osg/Texture3D.cpp @@ -278,7 +278,7 @@ void Texture3D::apply(State& state) const _textureObjectBuffer[contextID] = textureObject; - if (_unrefImageDataAfterApply && areAllTextureObjectsLoaded() && _image->getDataVariance()==STATIC) + if (state.getMaxTexturePoolSize()==0 && _unrefImageDataAfterApply && areAllTextureObjectsLoaded() && _image->getDataVariance()==STATIC) { Texture3D* non_const_this = const_cast(this); non_const_this->_image = 0; diff --git a/src/osg/TextureCubeMap.cpp b/src/osg/TextureCubeMap.cpp index 079346c59..03094336c 100644 --- a/src/osg/TextureCubeMap.cpp +++ b/src/osg/TextureCubeMap.cpp @@ -293,7 +293,7 @@ void TextureCubeMap::apply(State& state) const _textureObjectBuffer[contextID] = textureObject; - if (_unrefImageDataAfterApply && areAllTextureObjectsLoaded()) + if (state.getMaxTexturePoolSize()==0 && _unrefImageDataAfterApply && areAllTextureObjectsLoaded()) { TextureCubeMap* non_const_this = const_cast(this); for (int n=0; n<6; n++) diff --git a/src/osg/TextureRectangle.cpp b/src/osg/TextureRectangle.cpp index b87cc485d..b6eab0935 100644 --- a/src/osg/TextureRectangle.cpp +++ b/src/osg/TextureRectangle.cpp @@ -211,26 +211,39 @@ void TextureRectangle::apply(State& state) const else if (_image.valid() && _image->data()) { - // we don't have a applyTexImage1D_subload yet so can't reuse.. so just generate a new texture object. - textureObject = generateTextureObject(this, contextID,GL_TEXTURE_RECTANGLE); - + + // keep the image around at least till we go out of scope. + osg::ref_ptr image = _image; + + // compute the internal texture format, this set the _internalFormat to an appropriate value. + computeInternalFormat(); + + _textureWidth = image->s(); + _textureHeight = image->t(); + + _textureObjectBuffer[contextID] = textureObject = generateTextureObject( + this, contextID,GL_TEXTURE_RECTANGLE,1,_internalFormat,_textureWidth,_textureHeight,1,0); + textureObject->bind(); applyTexParameters(GL_TEXTURE_RECTANGLE, state); - - applyTexImage_load(GL_TEXTURE_RECTANGLE, _image.get(), state, _textureWidth, _textureHeight); - textureObject->setAllocated(1,_internalFormat,_textureWidth,_textureHeight,1,0); - - _textureObjectBuffer[contextID] = textureObject; + if (textureObject->isAllocated()) + { + applyTexImage_subload(GL_TEXTURE_RECTANGLE, _image.get(), state, _textureWidth, _textureHeight, _internalFormat); + } + else + { + applyTexImage_load(GL_TEXTURE_RECTANGLE, _image.get(), state, _textureWidth, _textureHeight); + textureObject->setAllocated(true); + } - if (_unrefImageDataAfterApply && areAllTextureObjectsLoaded() && _image->getDataVariance()==STATIC) + if (state.getMaxTexturePoolSize()==0 && _unrefImageDataAfterApply && areAllTextureObjectsLoaded() && _image->getDataVariance()==STATIC) { TextureRectangle* non_const_this = const_cast(this); non_const_this->_image = 0; } - } else if ( (_textureWidth!=0) && (_textureHeight!=0) && (_internalFormat!=0) ) {