2013-05-22 20:49:46 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2013 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.
|
|
|
|
*/
|
|
|
|
#include <osg/TextureBuffer>
|
|
|
|
#include <osg/State>
|
2016-06-16 02:05:35 +08:00
|
|
|
#include <osg/PrimitiveSet>
|
|
|
|
|
|
|
|
#ifndef GL_TEXTURE_BUFFER
|
|
|
|
#define GL_TEXTURE_BUFFER 0x8C2A
|
|
|
|
#endif
|
2013-05-22 20:49:46 +08:00
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
|
|
|
TextureBuffer::TextureBuffer():
|
2016-06-16 02:05:35 +08:00
|
|
|
_textureWidth(0)
|
2013-05-22 20:49:46 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-06-16 02:05:35 +08:00
|
|
|
TextureBuffer::TextureBuffer(osg::BufferData* image):
|
|
|
|
_textureWidth(0)
|
2013-05-22 20:49:46 +08:00
|
|
|
{
|
2016-06-16 02:05:35 +08:00
|
|
|
setBufferData(image);
|
2013-05-22 20:49:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureBuffer::TextureBuffer(const TextureBuffer& text,const CopyOp& copyop):
|
2016-06-16 02:05:35 +08:00
|
|
|
Texture(text,copyop),
|
|
|
|
_textureWidth(text._textureWidth)
|
2013-05-22 20:49:46 +08:00
|
|
|
{
|
2016-08-23 20:33:24 +08:00
|
|
|
if (text._bufferData.valid()) {
|
|
|
|
setBufferData(osg::clone(text._bufferData.get(), copyop));
|
|
|
|
}
|
2013-05-22 20:49:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureBuffer::~TextureBuffer()
|
|
|
|
{
|
2016-06-16 02:05:35 +08:00
|
|
|
_bufferData=NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureBuffer::setBufferData(BufferData *bufferdata)
|
|
|
|
{
|
|
|
|
if (_bufferData == bufferdata) return;
|
|
|
|
|
|
|
|
if (_bufferData.valid())
|
|
|
|
{
|
|
|
|
_bufferData->removeClient(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_bufferData=bufferdata;
|
|
|
|
_modifiedCount.setAllElementsTo(0);
|
|
|
|
|
|
|
|
|
|
|
|
if (_bufferData.valid())
|
|
|
|
{
|
|
|
|
_bufferData->addClient(this);
|
|
|
|
|
2017-08-16 00:23:49 +08:00
|
|
|
///set BufferObject if not set by user
|
2016-06-16 02:05:35 +08:00
|
|
|
if(!_bufferData->getBufferObject())
|
|
|
|
{
|
|
|
|
VertexBufferObject* bo=new VertexBufferObject();
|
|
|
|
bo->setUsage(GL_STREAM_DRAW_ARB);
|
|
|
|
bufferdata->setBufferObject(bo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-22 20:49:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int TextureBuffer::compare(const StateAttribute& sa) const
|
|
|
|
{
|
|
|
|
// check the types are equal and then create the rhs variable
|
|
|
|
// used by the COMPARE_StateAttribute_Parameter macros below.
|
|
|
|
COMPARE_StateAttribute_Types(TextureBuffer,sa)
|
|
|
|
|
2016-06-16 02:05:35 +08:00
|
|
|
|
|
|
|
if (_bufferData!=rhs._bufferData) // smart pointer comparison.
|
2013-05-22 20:49:46 +08:00
|
|
|
{
|
2016-06-16 02:05:35 +08:00
|
|
|
if (_bufferData.valid())
|
2013-05-22 20:49:46 +08:00
|
|
|
{
|
2016-06-16 02:05:35 +08:00
|
|
|
if (rhs._bufferData.valid())
|
2013-05-22 20:49:46 +08:00
|
|
|
{
|
2016-06-16 02:05:35 +08:00
|
|
|
//int result = _bufferData->compare(*rhs._bufferData);
|
|
|
|
int result=0;
|
|
|
|
if(_bufferData.get()<rhs._bufferData.get())result=1;
|
|
|
|
if(_bufferData.get()>rhs._bufferData.get())result=-1;
|
2013-05-22 20:49:46 +08:00
|
|
|
if (result!=0) return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1; // valid lhs._image is greater than null.
|
|
|
|
}
|
|
|
|
}
|
2016-06-16 02:05:35 +08:00
|
|
|
else if (rhs._bufferData.valid())
|
2013-05-22 20:49:46 +08:00
|
|
|
{
|
|
|
|
return -1; // valid rhs._image is greater than null.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 02:05:35 +08:00
|
|
|
if (!_bufferData && !rhs._bufferData)
|
2013-05-22 20:49:46 +08:00
|
|
|
{
|
|
|
|
int result = compareTextureObjects(rhs);
|
|
|
|
if (result!=0) return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int result = compareTexture(rhs);
|
|
|
|
if (result!=0) return result;
|
|
|
|
|
|
|
|
// compare each parameter in turn against the rhs.
|
|
|
|
COMPARE_StateAttribute_Parameter(_textureWidth)
|
2016-06-16 02:05:35 +08:00
|
|
|
//COMPARE_StateAttribute_Parameter(_usageHint)
|
2013-05-22 20:49:46 +08:00
|
|
|
|
|
|
|
return 0;
|
2016-06-16 02:05:35 +08:00
|
|
|
|
2013-05-22 20:49:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextureBuffer::setImage(Image* image)
|
|
|
|
{
|
2016-06-16 02:05:35 +08:00
|
|
|
if (getImage() == image) return;
|
2013-05-22 20:49:46 +08:00
|
|
|
|
2016-06-16 02:05:35 +08:00
|
|
|
setBufferData(image);
|
2013-06-28 00:44:32 +08:00
|
|
|
|
2013-05-22 20:49:46 +08:00
|
|
|
|
2013-06-28 00:44:32 +08:00
|
|
|
}
|
2013-05-22 20:49:46 +08:00
|
|
|
|
|
|
|
void TextureBuffer::apply(State& state) const
|
|
|
|
{
|
|
|
|
#if !defined(OSG_GLES1_AVAILABLE) && !defined(OSG_GLES2_AVAILABLE)
|
|
|
|
const unsigned int contextID = state.getContextID();
|
|
|
|
|
|
|
|
TextureObject* textureObject = getTextureObject(contextID);
|
2014-12-09 18:05:59 +08:00
|
|
|
|
2016-10-06 00:10:46 +08:00
|
|
|
///This code could be useful but would require to watch BufferData changes
|
2016-06-16 02:05:35 +08:00
|
|
|
///perhaps a mutable percontext dirtyflag (all reset to true onTextureObjectChanged) would do the trick
|
|
|
|
#if 0
|
|
|
|
if (textureObject)
|
|
|
|
{
|
|
|
|
bool textureObjectInvalidated = false;
|
|
|
|
if (textureObject->_profile._internalFormat != _internalFormat )
|
|
|
|
{
|
|
|
|
textureObjectInvalidated=true;
|
|
|
|
}
|
2013-05-22 20:49:46 +08:00
|
|
|
|
2016-06-16 02:05:35 +08:00
|
|
|
if (textureObjectInvalidated)
|
|
|
|
{
|
|
|
|
//OSG_NOTICE<<"Discarding TextureObject"<<std::endl;
|
|
|
|
_textureObjectBuffer[contextID]->release();
|
|
|
|
_textureObjectBuffer[contextID] = 0;
|
|
|
|
textureObject = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2013-05-22 20:49:46 +08:00
|
|
|
if (textureObject)
|
|
|
|
{
|
2016-06-16 02:05:35 +08:00
|
|
|
if(_bufferData.valid() &&_modifiedCount[contextID]!=_bufferData->getModifiedCount() )
|
2013-05-22 20:49:46 +08:00
|
|
|
{
|
2017-10-20 03:57:25 +08:00
|
|
|
_modifiedCount[contextID]=_bufferData->getModifiedCount() ;
|
|
|
|
|
2016-06-16 02:05:35 +08:00
|
|
|
GLBufferObject* glBufferObject = _bufferData->getBufferObject()->getOrCreateGLBufferObject(contextID);
|
|
|
|
if (glBufferObject)
|
|
|
|
{
|
|
|
|
if( glBufferObject->isDirty() )
|
|
|
|
{
|
|
|
|
//OSG_NOTICE<<"buffer upload"<<glBufferObject<<std::endl;
|
|
|
|
glBufferObject->compileBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-22 20:49:46 +08:00
|
|
|
}
|
2016-11-22 23:12:56 +08:00
|
|
|
textureObject->bind(state);
|
2013-05-22 20:49:46 +08:00
|
|
|
}
|
2016-06-16 02:05:35 +08:00
|
|
|
else if (_bufferData.valid() &&_bufferData->getBufferObject() )//&& _bufferObject->getNumBufferData()>0 )
|
2013-05-22 20:49:46 +08:00
|
|
|
{
|
2016-06-16 02:05:35 +08:00
|
|
|
/// now compile bufferobject if required
|
|
|
|
GLBufferObject* glBufferObject = _bufferData->getBufferObject()->getOrCreateGLBufferObject(contextID);
|
|
|
|
if (glBufferObject )
|
|
|
|
{
|
|
|
|
const GLExtensions* extensions = state.get<GLExtensions>();
|
2014-12-09 18:05:59 +08:00
|
|
|
|
2017-10-20 03:57:25 +08:00
|
|
|
_modifiedCount[contextID] = _bufferData->getModifiedCount();
|
|
|
|
|
2016-06-16 02:05:35 +08:00
|
|
|
textureObject = generateAndAssignTextureObject(contextID, GL_TEXTURE_BUFFER);
|
|
|
|
textureObject->_profile._internalFormat=_internalFormat;
|
2016-11-22 23:12:56 +08:00
|
|
|
textureObject->bind(state);
|
2014-12-09 18:05:59 +08:00
|
|
|
|
2016-06-16 02:05:35 +08:00
|
|
|
getTextureParameterDirty(state.getContextID()) = false;
|
2014-12-09 18:05:59 +08:00
|
|
|
|
2016-06-16 02:05:35 +08:00
|
|
|
computeInternalFormat();
|
2014-12-09 18:05:59 +08:00
|
|
|
|
2016-06-16 02:05:35 +08:00
|
|
|
if( glBufferObject->isDirty())
|
|
|
|
glBufferObject->compileBuffer();
|
|
|
|
|
|
|
|
textureObject->setAllocated(true);
|
|
|
|
extensions->glBindBuffer(_bufferData->getBufferObject()->getTarget(),0);
|
|
|
|
|
2016-11-22 23:12:56 +08:00
|
|
|
textureObject->bind(state);
|
2016-06-16 02:05:35 +08:00
|
|
|
extensions->glTexBuffer(GL_TEXTURE_BUFFER, _internalFormat, glBufferObject->getGLObjectID());
|
|
|
|
}
|
2014-12-09 18:05:59 +08:00
|
|
|
|
2013-05-22 20:49:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
OSG_NOTICE<<"Warning: TextureBuffer::apply(State& state) not supported."<<std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TextureBuffer::computeInternalFormat() const
|
|
|
|
{
|
2016-06-16 02:05:35 +08:00
|
|
|
if (getImage() ) computeInternalFormatWithImage(*getImage());
|
2013-05-22 20:49:46 +08:00
|
|
|
else computeInternalFormatType();
|
|
|
|
}
|