Further perfomrmance optimizations and clean up on new VBO/EBO/PBO API.

This commit is contained in:
Robert Osfield
2007-05-01 18:03:32 +00:00
parent fe85a439fb
commit 859bcf3c4b
16 changed files with 306 additions and 461 deletions

View File

@@ -73,7 +73,7 @@ class OSG_EXPORT Array : public Object
_dataSize(dataSize),
_dataType(dataType),
_modifiedCount(0),
_vboIndex(0) {}
_vboOffset(0) {}
Array(const Array& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
Object(array,copyop),
@@ -81,7 +81,7 @@ class OSG_EXPORT Array : public Object
_dataSize(array._dataSize),
_dataType(array._dataType),
_modifiedCount(0),
_vboIndex(0) {}
_vboOffset(0) {}
virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const Array*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osg"; }
@@ -123,18 +123,14 @@ class OSG_EXPORT Array : public Object
if (_vbo.valid())
{
_vbo->setArray(_vboIndex,0);
_vbo->removeArray(this);
}
_vbo = vbo;
if (_vbo.valid())
{
_vboIndex = _vbo->addArray(this);
}
else
{
_vboIndex = 0;
_vbo->addArray(this);
}
}
@@ -144,11 +140,11 @@ class OSG_EXPORT Array : public Object
/** Get the const VertexBufferObject. If no VBO is assigned returns NULL*/
inline const osg::VertexBufferObject* getVertexBufferObject() const { return _vbo.get(); }
/** Set the index into the VertexBufferObject, if used.*/
inline void setVertexBufferObjectIndex(unsigned int index) { _vboIndex = index; }
/** Set the offset into the VertexBufferObject, if used.*/
void setVertexBufferObjectOffset(const GLvoid* offset ) const { _vboOffset = offset; }
/** Get the index into the VertexBufferObject, if used.*/
inline unsigned int getVertexBufferObjectIndex() const { return _vboIndex; }
/** Get the offset into the VertexBufferObject, if used.*/
const GLvoid* getVertexBufferObjectOffset() const { return _vboOffset; }
protected:
@@ -156,7 +152,7 @@ class OSG_EXPORT Array : public Object
{
if (_vbo.valid())
{
_vbo->setArray(_vboIndex,0);
_vbo->removeArray(this);
}
}
@@ -165,7 +161,7 @@ class OSG_EXPORT Array : public Object
GLenum _dataType;
unsigned int _modifiedCount;
osg::ref_ptr<osg::VertexBufferObject> _vbo;
unsigned int _vboIndex;
mutable const GLvoid* _vboOffset;
};
template<typename T, Array::Type ARRAYTYPE, int DataSize, int DataType>

View File

@@ -101,8 +101,8 @@ class OSG_EXPORT BufferObject : public Object
BufferEntry& operator = (const BufferEntry& be) { modifiedCount=be.modifiedCount; dataSize=be.dataSize; offset=be.offset; return *this; }
mutable buffered_value<unsigned int> modifiedCount;
mutable unsigned int dataSize;
mutable unsigned int offset;
mutable unsigned int dataSize;
mutable unsigned int offset;
};
inline bool isBufferObjectSupported(unsigned int contextID) const { return getExtensions(contextID,true)->isBufferObjectSupported(); }
@@ -126,16 +126,15 @@ class OSG_EXPORT BufferObject : public Object
bool isDirty(unsigned int contextID) const { return _compiledList[contextID]==0; }
virtual bool needsCompile(unsigned int contextID) const = 0;
inline void compileBuffer(unsigned int contextID, State& state) const
{
if (isDirty(contextID)) compileBufferImplementation(state);
}
virtual void compileBufferImplementation(State& state) const = 0;
virtual void compileBuffer(State& state) const = 0;
void releaseBuffer(State* state) const;
/** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int maxSize);
/** If State is non-zero, this function releases OpenGL objects for
* the specified graphics context. Otherwise, releases OpenGL objexts
* for all graphics contexts. */
void releaseGLObjects(State* state=0) const;
/** Use deleteVertexBufferObject instead of glDeleteBuffers to allow
@@ -250,6 +249,7 @@ class OSG_EXPORT VertexBufferObject : public BufferObject
typedef std::vector< BufferEntryArrayPair > BufferEntryArrayPairs;
unsigned int addArray(osg::Array* array);
void removeArray(osg::Array* array);
void setArray(unsigned int i, Array* array);
Array* getArray(unsigned int i) { return _bufferEntryArrayPairs[i].second; }
@@ -257,9 +257,10 @@ class OSG_EXPORT VertexBufferObject : public BufferObject
const GLvoid* getOffset(unsigned int i) const { return (const GLvoid*)(_bufferEntryArrayPairs[i].first.offset); }
virtual bool needsCompile(unsigned int contextID) const;
virtual void compileBuffer(State& state) const;
virtual void compileBufferImplementation(State& state) const;
/** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int maxSize);
protected:
@@ -284,6 +285,7 @@ class OSG_EXPORT ElementBufferObject : public BufferObject
typedef std::vector< BufferEntryDrawElementstPair > BufferEntryDrawElementsPairs;
unsigned int addDrawElements(osg::DrawElements* PrimitiveSet);
void removeDrawElements(osg::DrawElements* PrimitiveSet);
void setDrawElements(unsigned int i, DrawElements* PrimitiveSet);
DrawElements* getDrawElements(unsigned int i) { return _bufferEntryDrawElementsPairs[i].second; }
@@ -291,9 +293,10 @@ class OSG_EXPORT ElementBufferObject : public BufferObject
const GLvoid* getOffset(unsigned int i) const { return (const GLvoid*)(_bufferEntryDrawElementsPairs[i].first.offset); }
virtual bool needsCompile(unsigned int contextID) const;
virtual void compileBuffer(State& state) const;
virtual void compileBufferImplementation(State& state) const;
/** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int maxSize);
protected:
@@ -323,9 +326,10 @@ class OSG_EXPORT PixelBufferObject : public BufferObject
unsigned int offset() const { return _bufferEntryImagePair.first.offset; }
virtual bool needsCompile(unsigned int contextID) const;
virtual void compileBuffer(State& state) const;
virtual void compileBufferImplementation(State& state) const;
/** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int maxSize);
protected:

View File

@@ -803,7 +803,6 @@ class OSG_EXPORT Drawable : public Object
typedef osg::buffered_value<GLuint> GLObjectList;
mutable GLObjectList _globjList;
mutable GLObjectList _vboList;
ref_ptr<UpdateCallback> _updateCallback;
unsigned int _numChildrenRequiringUpdateTraversal;

View File

@@ -57,8 +57,7 @@ class OSG_EXPORT Geometry : public Drawable
{
ArrayData():
binding(BIND_OFF),
normalize(GL_FALSE),
offset(0) {}
normalize(GL_FALSE) {}
ArrayData(const ArrayData& data,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
@@ -66,15 +65,13 @@ class OSG_EXPORT Geometry : public Drawable
array(a),
indices(0),
binding(b),
normalize(n),
offset(0) {}
normalize(n) {}
ArrayData(Array* a, IndexArray* i, AttributeBinding b, GLboolean n = GL_FALSE):
array(a),
indices(i),
binding(b),
normalize(n),
offset(0) {}
normalize(n) {}
ArrayData& operator = (const ArrayData& rhs)
{
@@ -82,7 +79,6 @@ class OSG_EXPORT Geometry : public Drawable
indices = rhs.indices;
binding = rhs.binding;
normalize = rhs.normalize;
offset = rhs.offset;
return *this;
}
@@ -92,15 +88,13 @@ class OSG_EXPORT Geometry : public Drawable
ref_ptr<IndexArray> indices;
AttributeBinding binding;
GLboolean normalize;
mutable unsigned long offset;
};
struct OSG_EXPORT Vec3ArrayData
{
Vec3ArrayData():
binding(BIND_OFF),
normalize(GL_FALSE),
offset(0) {}
normalize(GL_FALSE) {}
Vec3ArrayData(const Vec3ArrayData& data,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
@@ -108,15 +102,13 @@ class OSG_EXPORT Geometry : public Drawable
array(a),
indices(0),
binding(b),
normalize(n),
offset(0) {}
normalize(n) {}
Vec3ArrayData(Vec3Array* a, IndexArray* i, AttributeBinding b, GLboolean n = GL_FALSE):
array(a),
indices(i),
binding(b),
normalize(n),
offset(0) {}
normalize(n) {}
Vec3ArrayData& operator = (const Vec3ArrayData& rhs)
{
@@ -124,7 +116,6 @@ class OSG_EXPORT Geometry : public Drawable
indices = rhs.indices;
binding = rhs.binding;
normalize = rhs.normalize;
offset = rhs.offset;
return *this;
}
@@ -134,7 +125,6 @@ class OSG_EXPORT Geometry : public Drawable
ref_ptr<IndexArray> indices;
AttributeBinding binding;
GLboolean normalize;
mutable unsigned long offset;
};
/** Static ArrayData which is returned from getTexCoordData(i) const and getVertexAttribData(i) const
@@ -300,11 +290,19 @@ class OSG_EXPORT Geometry : public Drawable
virtual void dirtyDisplayList();
/** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int maxSize);
/** If State is non-zero, this function releases OpenGL objects for
* the specified graphics context. Otherwise, releases OpenGL objexts
* for all graphics contexts. */
virtual void releaseGLObjects(State* state=0) const;
typedef std::vector<osg::Array*> ArrayList;
bool getArrayList(ArrayList& arrayList);
bool getArrayList(ArrayList& arrayList) const;
typedef std::vector<osg::DrawElements*> DrawElementsList;
bool getDrawElementsList(DrawElementsList& drawElementsList);
bool getDrawElementsList(DrawElementsList& drawElementsList) const;
osg::VertexBufferObject* getOrCreateVertexBufferObject();

View File

@@ -413,11 +413,11 @@ class DrawElements : public PrimitiveSet
DrawElements(Type primType=PrimitiveType, GLenum mode=0):
PrimitiveSet(primType,mode),
_eboIndex(0) {}
_eboOffset(0) {}
DrawElements(const DrawElements& copy,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
PrimitiveSet(copy,copyop),
_eboIndex(0) {}
_eboOffset(0) {}
virtual DrawElements* getDrawElements() { return this; }
@@ -432,17 +432,14 @@ class DrawElements : public PrimitiveSet
if (_ebo.valid())
{
_ebo->setDrawElements(_eboIndex,0);
_ebo->removeDrawElements(this);
}
_ebo = ebo;
if (_ebo.valid())
{
_eboIndex = _ebo->addDrawElements(this);
}
else
{
_eboIndex = 0;
_ebo->addDrawElements(this);
}
}
@@ -452,25 +449,40 @@ class DrawElements : public PrimitiveSet
/** Get the const ElementBufferObject. If no EBO is assigned returns NULL*/
inline const osg::ElementBufferObject* getElementBufferObject() const { return _ebo.get(); }
/** Set the index into the ElementBufferObject, if used.*/
inline void setElementBufferObjectIndex(unsigned int index) { _eboIndex = index; }
/** Set the offset into the ElementBufferObject, if used.*/
inline void setElementBufferObjectOffset(const GLvoid* offset) const { _eboOffset = offset; }
/** Get the index into the ElementBufferObject, if used.*/
inline unsigned int getElementBufferObjectIndex() const { return _eboIndex; }
/** Get the offset into the ElementBufferOffset, if used.*/
inline const GLvoid* getElementBufferObjectOffset() const { return _eboOffset; }
/** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int maxSize)
{
if (_ebo.valid()) _ebo->resizeGLObjectBuffers(maxSize);
}
/** If State is non-zero, this function releases OpenGL objects for
* the specified graphics context. Otherwise, releases OpenGL objexts
* for all graphics contexts. */
virtual void releaseGLObjects(State* state=0) const
{
if (_ebo.valid()) _ebo->releaseGLObjects(state);
}
protected:
virtual ~DrawElements()
{
if (_ebo.valid())
{
_ebo->setDrawElements(_eboIndex,0);
_ebo->removeDrawElements(this);
}
}
osg::ref_ptr<ElementBufferObject> _ebo;
unsigned int _eboIndex;
osg::ref_ptr<ElementBufferObject> _ebo;
mutable const GLvoid* _eboOffset;
};
class OSG_EXPORT DrawElementsUByte : public DrawElements, public VectorGLubyte
@@ -512,11 +524,6 @@ class OSG_EXPORT DrawElementsUByte : public DrawElements, public VectorGLubyte
virtual unsigned int getNumIndices() const { return size(); }
virtual unsigned int index(unsigned int pos) const { return (*this)[pos]; }
virtual void offsetIndices(int offset);
/** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int maxSize);
virtual void releaseGLObjects(State* state=0) const;
virtual void computeRange() const
{
@@ -545,9 +552,6 @@ class OSG_EXPORT DrawElementsUByte : public DrawElements, public VectorGLubyte
mutable unsigned int _minIndex;
mutable unsigned int _maxIndex;
mutable GLObjectList _vboList;
};
@@ -596,11 +600,6 @@ class OSG_EXPORT DrawElementsUShort : public DrawElements, public VectorGLushort
virtual unsigned int index(unsigned int pos) const { return (*this)[pos]; }
virtual void offsetIndices(int offset);
/** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int maxSize);
virtual void releaseGLObjects(State* state=0) const;
virtual void computeRange() const
{
if (empty())
@@ -628,8 +627,6 @@ class OSG_EXPORT DrawElementsUShort : public DrawElements, public VectorGLushort
mutable unsigned int _minIndex;
mutable unsigned int _maxIndex;
mutable GLObjectList _vboList;
};
class OSG_EXPORT DrawElementsUInt : public DrawElements, public VectorGLuint
@@ -677,10 +674,6 @@ class OSG_EXPORT DrawElementsUInt : public DrawElements, public VectorGLuint
virtual unsigned int index(unsigned int pos) const { return (*this)[pos]; }
virtual void offsetIndices(int offset);
/** Resize any per context GLObject buffers to specified size. */
virtual void resizeGLObjectBuffers(unsigned int maxSize);
virtual void releaseGLObjects(State* state=0) const;
virtual void computeRange() const
{
@@ -709,8 +702,6 @@ class OSG_EXPORT DrawElementsUInt : public DrawElements, public VectorGLuint
mutable unsigned int _minIndex;
mutable unsigned int _maxIndex;
mutable GLObjectList _vboList;
};
}

View File

@@ -409,7 +409,7 @@ class OSG_EXPORT State : public Referenced
inline void bindVertexBufferObject(const osg::VertexBufferObject* vbo)
{
if (vbo == _currentVBO) return;
if (vbo->isDirty(_contextID)) vbo->compileBuffer(_contextID, *this);
if (vbo->isDirty(_contextID)) vbo->compileBuffer(*this);
else _glBindBuffer(GL_ARRAY_BUFFER_ARB,vbo->buffer(_contextID));
_currentVBO = vbo;
}
@@ -427,7 +427,7 @@ class OSG_EXPORT State : public Referenced
inline void bindElementBufferObject(const osg::ElementBufferObject* ebo)
{
if (ebo == _currentEBO) return;
if (ebo->isDirty(_contextID)) ebo->compileBuffer(_contextID, *this);
if (ebo->isDirty(_contextID)) ebo->compileBuffer(*this);
else _glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB,ebo->buffer(_contextID));
_currentEBO = ebo;
}
@@ -446,7 +446,7 @@ class OSG_EXPORT State : public Referenced
{
if (pbo == _currentPBO) return;
if (pbo->isDirty(_contextID)) pbo->compileBuffer(_contextID, *this);
if (pbo->isDirty(_contextID)) pbo->compileBuffer(*this);
else _glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB,pbo->buffer(_contextID));
_currentPBO = pbo;
@@ -474,7 +474,7 @@ class OSG_EXPORT State : public Referenced
if (vbo)
{
bindVertexBufferObject(vbo);
setVertexPointer(array->getDataSize(),array->getDataType(),0,vbo->getOffset(array->getVertexBufferObjectIndex()));
setVertexPointer(array->getDataSize(),array->getDataType(),0,array->getVertexBufferObjectOffset());
}
else
{
@@ -531,7 +531,7 @@ class OSG_EXPORT State : public Referenced
if (vbo)
{
bindVertexBufferObject(vbo);
setNormalPointer(array->getDataType(),0,vbo->getOffset(array->getVertexBufferObjectIndex()));
setNormalPointer(array->getDataType(),0,array->getVertexBufferObjectOffset());
}
else
{
@@ -587,7 +587,7 @@ class OSG_EXPORT State : public Referenced
if (vbo)
{
bindVertexBufferObject(vbo);
setColorPointer(array->getDataSize(),array->getDataType(),0,vbo->getOffset(array->getVertexBufferObjectIndex()));
setColorPointer(array->getDataSize(),array->getDataType(),0,array->getVertexBufferObjectOffset());
}
else
{
@@ -648,7 +648,11 @@ class OSG_EXPORT State : public Referenced
if (vbo)
{
bindVertexBufferObject(vbo);
#if 0
setSecondaryColorPointer(array->getDataSize(),array->getDataType(),0,vbo->getOffset(array->getVertexBufferObjectIndex()));
#else
setSecondaryColorPointer(array->getDataSize(),array->getDataType(),0,array->getVertexBufferObjectOffset());
#endif
}
else
{
@@ -730,7 +734,11 @@ class OSG_EXPORT State : public Referenced
if (vbo)
{
bindVertexBufferObject(vbo);
#if 0
setFogCoordPointer(array->getDataType(),0,vbo->getOffset(array->getVertexBufferObjectIndex()));
#else
setFogCoordPointer(array->getDataType(),0,array->getVertexBufferObjectOffset());
#endif
}
else
{
@@ -775,7 +783,11 @@ class OSG_EXPORT State : public Referenced
if (vbo)
{
bindVertexBufferObject(vbo);
#if 0
setTexCoordPointer(unit, array->getDataSize(),array->getDataType(),0,vbo->getOffset(array->getVertexBufferObjectIndex()));
#else
setTexCoordPointer(unit, array->getDataSize(),array->getDataType(),0,array->getVertexBufferObjectOffset());
#endif
}
else
{
@@ -893,7 +905,11 @@ class OSG_EXPORT State : public Referenced
if (vbo)
{
bindVertexBufferObject(vbo);
#if 0
setVertexAttribPointer(unit, array->getDataSize(),array->getDataType(),normalized,0,vbo->getOffset(array->getVertexBufferObjectIndex()));
#else
setVertexAttribPointer(unit, array->getDataSize(),array->getDataType(),normalized,0,array->getVertexBufferObjectOffset());
#endif
}
else
{