From 78a01ce2a59a1a2b547eac2b4ab837258d47f000 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 1 Jun 2015 10:50:44 +0000 Subject: [PATCH] From Jannik Heller, "I have added new functions Texture::generateAndAssignTextureObject mirroring the Texture::generateTextureObject functions. I have left the Texture::generateTextureObject functions intact as I'm not sure if/how it's used outside the core OSG. If you feel that compatibility isn't important in that area feel free to drop it. While testing the build with OSG_USE_REF_PTR_IMPLICIT_OUTPUT_CONVERSION=OFF I found a compile error in GlyphGeometry.cpp that was entirely unrelated to the changes I've made. The fix is included in the patch. There is one thing left to fix and that is Texture2D::SubloadCallback: class OSG_EXPORT SubloadCallback : public Referenced { public: .... virtual TextureObject* generateTextureObject(const Texture2D& texture, State& state) const { return osg::Texture::generateTextureObject(&texture, state.getContextID(), GL_TEXTURE_2D); } ... }" git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14879 16af8721-9629-0410-8352-f15c8da7e697 --- include/osg/Texture | 23 +++++++++++++----- include/osg/Texture2D | 2 +- src/osg/Texture.cpp | 40 ++++++++++++++++++++++++-------- src/osg/Texture1D.cpp | 9 ++++--- src/osg/Texture2D.cpp | 8 +++---- src/osg/Texture2DArray.cpp | 12 ++++------ src/osg/Texture2DMultisample.cpp | 3 +-- src/osg/Texture3D.cpp | 10 ++++---- src/osg/TextureBuffer.cpp | 3 +-- src/osg/TextureCubeMap.cpp | 12 ++++------ src/osg/TextureRectangle.cpp | 12 +++++----- src/osgText/Glyph.cpp | 4 ++-- 12 files changed, 79 insertions(+), 59 deletions(-) diff --git a/include/osg/Texture b/include/osg/Texture index 8ed68f3e8..cd6cd753b 100644 --- a/include/osg/Texture +++ b/include/osg/Texture @@ -1101,8 +1101,8 @@ class OSG_EXPORT Texture : public osg::StateAttribute void discardAllDeletedTextureObjects(); void flushDeletedTextureObjects(double currentTime, double& availableTime); - TextureObject* takeFromOrphans(Texture* texture); - TextureObject* takeOrGenerate(Texture* texture); + osg::ref_ptr takeFromOrphans(Texture* texture); + osg::ref_ptr takeOrGenerate(Texture* texture); void moveToBack(TextureObject* to); void addToBack(TextureObject* to); void orphan(TextureObject* to); @@ -1167,8 +1167,8 @@ class OSG_EXPORT Texture : public osg::StateAttribute bool hasSpace(unsigned int size) const { return (_currTexturePoolSize+size)<=_maxTexturePoolSize; } bool makeSpace(unsigned int size); - TextureObject* generateTextureObject(const Texture* texture, GLenum target); - TextureObject* generateTextureObject(const Texture* texture, + osg::ref_ptr generateTextureObject(const Texture* texture, GLenum target); + osg::ref_ptr generateTextureObject(const Texture* texture, GLenum target, GLint numMipmapLevels, GLenum internalFormat, @@ -1231,9 +1231,9 @@ class OSG_EXPORT Texture : public osg::StateAttribute static osg::ref_ptr& getTextureObjectManager(unsigned int contextID); - static TextureObject* generateTextureObject(const Texture* texture, unsigned int contextID,GLenum target); + static osg::ref_ptr generateTextureObject(const Texture* texture, unsigned int contextID,GLenum target); - static TextureObject* generateTextureObject(const Texture* texture, + static osg::ref_ptr generateTextureObject(const Texture* texture, unsigned int contextID, GLenum target, GLint numMipmapLevels, @@ -1243,6 +1243,17 @@ class OSG_EXPORT Texture : public osg::StateAttribute GLsizei depth, GLint border); + TextureObject* generateAndAssignTextureObject(unsigned int contextID, GLenum target) const; + + TextureObject* generateAndAssignTextureObject(unsigned int contextID, + GLenum target, + GLint numMipmapLevels, + GLenum internalFormat, + GLsizei width, + GLsizei height, + GLsizei depth, + GLint border) const; + static void deleteAllTextureObjects(unsigned int contextID); static void discardAllTextureObjects(unsigned int contextID); static void flushAllDeletedTextureObjects(unsigned int contextID); diff --git a/include/osg/Texture2D b/include/osg/Texture2D index 199e55790..d04deb41f 100644 --- a/include/osg/Texture2D +++ b/include/osg/Texture2D @@ -95,7 +95,7 @@ class OSG_EXPORT Texture2D : public Texture virtual TextureObject* generateTextureObject(const Texture2D& texture, State& state) const { - return osg::Texture::generateTextureObject(&texture, state.getContextID(), GL_TEXTURE_2D); + return osg::Texture::generateTextureObject(&texture, state.getContextID(), GL_TEXTURE_2D).release(); } virtual void load(const Texture2D& texture,State& state) const = 0; diff --git a/src/osg/Texture.cpp b/src/osg/Texture.cpp index b04055d50..5b973c112 100644 --- a/src/osg/Texture.cpp +++ b/src/osg/Texture.cpp @@ -680,7 +680,7 @@ bool Texture::TextureObjectSet::makeSpace(unsigned int& size) return size==0; } -Texture::TextureObject* Texture::TextureObjectSet::takeFromOrphans(Texture* texture) +osg::ref_ptr Texture::TextureObjectSet::takeFromOrphans(Texture* texture) { // take front of orphaned list. ref_ptr to = _orphanedTextureObjects.front(); @@ -700,11 +700,11 @@ Texture::TextureObject* Texture::TextureObjectSet::takeFromOrphans(Texture* text OSG_INFO<<"Reusing orphaned TextureObject, _numOfTextureObjects="<<_numOfTextureObjects< Texture::TextureObjectSet::takeOrGenerate(Texture* texture) { // see if we can recyle TextureObject from the orphan list { @@ -752,7 +752,7 @@ Texture::TextureObject* Texture::TextureObjectSet::takeOrGenerate(Texture* textu // assign to new texture to->setTexture(texture); - return to.release(); + return to; } // @@ -761,7 +761,7 @@ Texture::TextureObject* Texture::TextureObjectSet::takeOrGenerate(Texture* textu GLuint id; glGenTextures( 1L, &id ); - TextureObject* 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; @@ -769,7 +769,7 @@ Texture::TextureObject* Texture::TextureObjectSet::takeOrGenerate(Texture* textu _parent->getCurrTexturePoolSize() += _profile._size; _parent->getNumberActiveTextureObjects() += 1; - addToBack(to); + addToBack(to.get()); OSG_INFO<<"Created new " << this << " TextureObject, _numOfTextureObjects "<<_numOfTextureObjects< Texture::TextureObjectManager::generateTextureObject(const Texture* texture, GLenum target) { return generateTextureObject(texture, target, 0, 0, 0, 0, 0, 0); } -Texture::TextureObject* Texture::TextureObjectManager::generateTextureObject(const Texture* texture, +osg::ref_ptr Texture::TextureObjectManager::generateTextureObject(const Texture* texture, GLenum target, GLint numMipmapLevels, GLenum internalFormat, @@ -1012,6 +1012,26 @@ Texture::TextureObject* Texture::TextureObjectManager::generateTextureObject(con return tos->takeOrGenerate(const_cast(texture)); } +Texture::TextureObject* Texture::generateAndAssignTextureObject(unsigned int contextID, GLenum target) const +{ + _textureObjectBuffer[contextID] = generateTextureObject(this, contextID, target); + return _textureObjectBuffer[contextID].get(); +} + +Texture::TextureObject* Texture::generateAndAssignTextureObject( + unsigned int contextID, + GLenum target, + GLint numMipmapLevels, + GLenum internalFormat, + GLsizei width, + GLsizei height, + GLsizei depth, + GLint border) const +{ + _textureObjectBuffer[contextID] = generateTextureObject(this, contextID, target, numMipmapLevels, internalFormat, width, height, depth, border); + return _textureObjectBuffer[contextID].get(); +} + Texture::TextureObjectSet* Texture::TextureObjectManager::getTextureObjectSet(const TextureProfile& profile) { osg::ref_ptr& tos = _textureSetMap[profile]; @@ -1203,12 +1223,12 @@ osg::ref_ptr& Texture::getTextureObjectManager(un } -Texture::TextureObject* Texture::generateTextureObject(const Texture* texture, unsigned int contextID, GLenum target) +osg::ref_ptr Texture::generateTextureObject(const Texture* texture, unsigned int contextID, GLenum target) { return getTextureObjectManager(contextID)->generateTextureObject(texture, target); } -Texture::TextureObject* Texture::generateTextureObject(const Texture* texture, unsigned int contextID, +osg::ref_ptr Texture::generateTextureObject(const Texture* texture, unsigned int contextID, GLenum target, GLint numMipmapLevels, GLenum internalFormat, diff --git a/src/osg/Texture1D.cpp b/src/osg/Texture1D.cpp index 822988a93..e4970e3ed 100644 --- a/src/osg/Texture1D.cpp +++ b/src/osg/Texture1D.cpp @@ -183,7 +183,7 @@ void Texture1D::apply(State& state) const { // we don't have a applyTexImage1D_subload yet so can't reuse.. so just generate a new texture object. - _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID, GL_TEXTURE_1D); + textureObject = generateAndAssignTextureObject(contextID, GL_TEXTURE_1D); textureObject->bind(); @@ -204,7 +204,7 @@ void Texture1D::apply(State& state) const { // 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_1D); + textureObject = generateAndAssignTextureObject(contextID,GL_TEXTURE_1D); textureObject->bind(); @@ -229,8 +229,7 @@ void Texture1D::apply(State& state) const } else if ( (_textureWidth!=0) && (_internalFormat!=0) ) { - _textureObjectBuffer[contextID] = textureObject = generateTextureObject( - this,contextID,GL_TEXTURE_1D,_numMipmapLevels,_internalFormat,_textureWidth,1,1,0); + textureObject = generateAndAssignTextureObject(contextID, GL_TEXTURE_1D,_numMipmapLevels,_internalFormat,_textureWidth,1,1,0); textureObject->bind(); @@ -418,7 +417,7 @@ void Texture1D::copyTexImage1D(State& state, int x, int y, int width) _min_filter = LINEAR; _mag_filter = LINEAR; - _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID,GL_TEXTURE_1D); + textureObject = generateAndAssignTextureObject(contextID, GL_TEXTURE_1D); textureObject->bind(); diff --git a/src/osg/Texture2D.cpp b/src/osg/Texture2D.cpp index 68df35b4b..495894d4a 100644 --- a/src/osg/Texture2D.cpp +++ b/src/osg/Texture2D.cpp @@ -251,8 +251,7 @@ void Texture2D::apply(State& state) const // compute the dimensions of the texture. computeRequiredTextureDimensions(state,*image,_textureWidth, _textureHeight, _numMipmapLevels); - _textureObjectBuffer[contextID] = textureObject = generateTextureObject( - this, contextID,GL_TEXTURE_2D,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,_borderWidth); + textureObject = generateAndAssignTextureObject(contextID,GL_TEXTURE_2D,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,_borderWidth); textureObject->bind(); @@ -292,8 +291,7 @@ void Texture2D::apply(State& state) const } else if ( (_textureWidth!=0) && (_textureHeight!=0) && (_internalFormat!=0) ) { - _textureObjectBuffer[contextID] = textureObject = generateTextureObject( - this, contextID,GL_TEXTURE_2D,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,_borderWidth); + textureObject = generateAndAssignTextureObject(contextID,GL_TEXTURE_2D,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,_borderWidth); textureObject->bind(); @@ -390,7 +388,7 @@ void Texture2D::copyTexImage2D(State& state, int x, int y, int width, int height for(int s=1; sbind(); diff --git a/src/osg/Texture2DArray.cpp b/src/osg/Texture2DArray.cpp index 2aee9e1c4..864a9f486 100644 --- a/src/osg/Texture2DArray.cpp +++ b/src/osg/Texture2DArray.cpp @@ -315,7 +315,7 @@ void Texture2DArray::apply(State& state) const else if (_subloadCallback.valid()) { // generate texture (i.e. glGenTexture) and apply parameters - _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID, GL_TEXTURE_2D_ARRAY_EXT); + textureObject = generateAndAssignTextureObject(contextID, GL_TEXTURE_2D_ARRAY_EXT); textureObject->bind(); applyTexParameters(GL_TEXTURE_2D_ARRAY_EXT, state); _subloadCallback->load(*this,state); @@ -333,15 +333,13 @@ void Texture2DArray::apply(State& state) const computeRequiredTextureDimensions(state,*_images[0],_textureWidth, _textureHeight, _numMipmapLevels); // create texture object - textureObject = generateTextureObject( - this, contextID, GL_TEXTURE_2D_ARRAY_EXT,_numMipmapLevels, _internalFormat, _textureWidth, _textureHeight, textureDepth,0); + textureObject = generateAndAssignTextureObject( + contextID, GL_TEXTURE_2D_ARRAY_EXT,_numMipmapLevels, _internalFormat, _textureWidth, _textureHeight, textureDepth,0); // bind texture textureObject->bind(); applyTexParameters(GL_TEXTURE_2D_ARRAY_EXT, state); - _textureObjectBuffer[contextID] = textureObject; - // First we need to allocate the texture memory int sourceFormat = _sourceFormat ? _sourceFormat : _internalFormat; @@ -426,8 +424,8 @@ void Texture2DArray::apply(State& state) const else if ( (_textureWidth > 0) && (_textureHeight > 0) && (_textureDepth > 0) && (_internalFormat!=0) ) { // generate texture - _textureObjectBuffer[contextID] = textureObject = generateTextureObject( - this, contextID, GL_TEXTURE_2D_ARRAY_EXT,_numMipmapLevels,_internalFormat, _textureWidth, _textureHeight, _textureDepth,0); + textureObject = generateAndAssignTextureObject( + contextID, GL_TEXTURE_2D_ARRAY_EXT,_numMipmapLevels,_internalFormat, _textureWidth, _textureHeight, _textureDepth,0); textureObject->bind(); applyTexParameters(GL_TEXTURE_2D_ARRAY_EXT,state); diff --git a/src/osg/Texture2DMultisample.cpp b/src/osg/Texture2DMultisample.cpp index bf41e3fcd..dbf759521 100644 --- a/src/osg/Texture2DMultisample.cpp +++ b/src/osg/Texture2DMultisample.cpp @@ -106,8 +106,7 @@ void Texture2DMultisample::apply(State& state) const } else if ( (_textureWidth!=0) && (_textureHeight!=0) && (_numSamples!=0) ) { - _textureObjectBuffer[contextID] = textureObject = - generateTextureObject( this, + textureObject = generateAndAssignTextureObject( contextID, getTextureTarget(), 1, diff --git a/src/osg/Texture3D.cpp b/src/osg/Texture3D.cpp index 8dffee9fa..15e0a23de 100644 --- a/src/osg/Texture3D.cpp +++ b/src/osg/Texture3D.cpp @@ -265,7 +265,7 @@ void Texture3D::apply(State& state) const else if (_subloadCallback.valid()) { - _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID,GL_TEXTURE_3D); + textureObject = generateAndAssignTextureObject(contextID,GL_TEXTURE_3D); textureObject->bind(); @@ -291,7 +291,7 @@ void Texture3D::apply(State& state) const // compute the dimensions of the texture. computeRequiredTextureDimensions(state,*_image,_textureWidth, _textureHeight, _textureDepth,_numMipmapLevels); - textureObject = generateTextureObject(this, contextID,GL_TEXTURE_3D); + textureObject = generateAndAssignTextureObject(contextID,GL_TEXTURE_3D); textureObject->bind(); @@ -305,8 +305,6 @@ void Texture3D::apply(State& state) const // update the modified count to show that it is upto date. getModifiedCount(contextID) = _image->getModifiedCount(); - _textureObjectBuffer[contextID] = textureObject; - // unref image data? if (isSafeToUnrefImageData(state) && _image->getDataVariance()==STATIC) { @@ -317,8 +315,8 @@ void Texture3D::apply(State& state) const } else if ( (_textureWidth!=0) && (_textureHeight!=0) && (_textureDepth!=0) && (_internalFormat!=0) ) { - _textureObjectBuffer[contextID] = textureObject = generateTextureObject( - this, contextID,GL_TEXTURE_3D,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,_textureDepth,0); + textureObject = generateAndAssignTextureObject( + contextID,GL_TEXTURE_3D,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,_textureDepth,0); textureObject->bind(); diff --git a/src/osg/TextureBuffer.cpp b/src/osg/TextureBuffer.cpp index 8cf70e303..04c319f88 100644 --- a/src/osg/TextureBuffer.cpp +++ b/src/osg/TextureBuffer.cpp @@ -135,8 +135,7 @@ void TextureBuffer::apply(State& state) const } else if (_image.valid() && _image->data()) { - textureObject = generateTextureObject(this, contextID, GL_TEXTURE_BUFFER); - _textureObjectBuffer[contextID] = textureObject; + textureObject = generateAndAssignTextureObject(contextID, GL_TEXTURE_BUFFER); textureObject->bind(); textureBufferObject = new TextureBufferObject(contextID,_usageHint); diff --git a/src/osg/TextureCubeMap.cpp b/src/osg/TextureCubeMap.cpp index 60b99ceec..59efec07d 100644 --- a/src/osg/TextureCubeMap.cpp +++ b/src/osg/TextureCubeMap.cpp @@ -259,7 +259,7 @@ void TextureCubeMap::apply(State& state) const } else if (_subloadCallback.valid()) { - _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID,GL_TEXTURE_CUBE_MAP); + textureObject = generateAndAssignTextureObject(contextID,GL_TEXTURE_CUBE_MAP); textureObject->bind(); @@ -289,8 +289,8 @@ void TextureCubeMap::apply(State& state) const _textureWidth = _textureHeight = minimum( _textureWidth , _textureHeight ); } - textureObject = generateTextureObject( - this, contextID,GL_TEXTURE_CUBE_MAP,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,0); + textureObject = generateAndAssignTextureObject( + contextID,GL_TEXTURE_CUBE_MAP,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,0); textureObject->bind(); @@ -315,8 +315,6 @@ void TextureCubeMap::apply(State& state) const } - _textureObjectBuffer[contextID] = textureObject; - // unref image data? if (isSafeToUnrefImageData(state)) { @@ -333,8 +331,8 @@ void TextureCubeMap::apply(State& state) const } else if ( (_textureWidth!=0) && (_textureHeight!=0) && (_internalFormat!=0) ) { - _textureObjectBuffer[contextID] = textureObject = generateTextureObject( - this, contextID,GL_TEXTURE_CUBE_MAP,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,0); + textureObject = generateAndAssignTextureObject( + contextID,GL_TEXTURE_CUBE_MAP,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,0); textureObject->bind(); diff --git a/src/osg/TextureRectangle.cpp b/src/osg/TextureRectangle.cpp index 84a021aef..ff9ac7ed1 100644 --- a/src/osg/TextureRectangle.cpp +++ b/src/osg/TextureRectangle.cpp @@ -218,7 +218,7 @@ void TextureRectangle::apply(State& state) const else if (_subloadCallback.valid()) { // we don't have a applyTexImage1D_subload yet so can't reuse.. so just generate a new texture object. - _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID,GL_TEXTURE_RECTANGLE); + textureObject = generateAndAssignTextureObject(contextID,GL_TEXTURE_RECTANGLE); textureObject->bind(); @@ -247,8 +247,8 @@ void TextureRectangle::apply(State& state) const _textureWidth = image->s(); _textureHeight = image->t(); - _textureObjectBuffer[contextID] = textureObject = generateTextureObject( - this, contextID,GL_TEXTURE_RECTANGLE,1,_internalFormat,_textureWidth,_textureHeight,1,0); + textureObject = generateAndAssignTextureObject( + contextID,GL_TEXTURE_RECTANGLE,1,_internalFormat,_textureWidth,_textureHeight,1,0); textureObject->bind(); @@ -273,8 +273,8 @@ void TextureRectangle::apply(State& state) const } else if ( (_textureWidth!=0) && (_textureHeight!=0) && (_internalFormat!=0) ) { - _textureObjectBuffer[contextID] = textureObject = generateTextureObject( - this, contextID,GL_TEXTURE_RECTANGLE,0,_internalFormat,_textureWidth,_textureHeight,1,0); + textureObject = generateAndAssignTextureObject( + contextID,GL_TEXTURE_RECTANGLE,0,_internalFormat,_textureWidth,_textureHeight,1,0); textureObject->bind(); @@ -493,7 +493,7 @@ void TextureRectangle::copyTexImage2D(State& state, int x, int y, int width, int // switch off mip-mapping. // - _textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID,GL_TEXTURE_RECTANGLE); + textureObject = generateAndAssignTextureObject(contextID,GL_TEXTURE_RECTANGLE); textureObject->bind(); diff --git a/src/osgText/Glyph.cpp b/src/osgText/Glyph.cpp index 325dbc89b..27566ff4a 100644 --- a/src/osgText/Glyph.cpp +++ b/src/osgText/Glyph.cpp @@ -186,8 +186,8 @@ void GlyphTexture::apply(osg::State& state) const // being bound for the first time, need to allocate the texture - _textureObjectBuffer[contextID] = textureObject = osg::Texture::generateTextureObject( - this, contextID, GL_TEXTURE_2D, 1, OSGTEXT_GLYPH_INTERNALFORMAT, getTextureWidth(), getTextureHeight(), 1, 0); + textureObject = osg::Texture::generateAndAssignTextureObject( + contextID, GL_TEXTURE_2D, 1, OSGTEXT_GLYPH_INTERNALFORMAT, getTextureWidth(), getTextureHeight(), 1, 0); textureObject->bind();