From fe85a439fb82b9f3e865e74c61472675021bcb2e Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 1 May 2007 09:29:30 +0000 Subject: [PATCH] Cleaned up the optional code paths in new VBO code --- src/osg/Geometry.cpp | 370 +-------------------------------------- src/osg/PrimitiveSet.cpp | 152 ---------------- 2 files changed, 2 insertions(+), 520 deletions(-) diff --git a/src/osg/Geometry.cpp b/src/osg/Geometry.cpp index 846ca3fe1..5735d93f1 100644 --- a/src/osg/Geometry.cpp +++ b/src/osg/Geometry.cpp @@ -17,7 +17,6 @@ using namespace osg; const Geometry::ArrayData Geometry::s_InvalidArrayData; -#if 1 class DrawVertex { public: @@ -74,29 +73,6 @@ class DrawVertex Array::Type _verticesType; Array::Type _indicesType; }; -#else -class DrawVertex : public osg::ConstValueVisitor -{ - public: - - DrawVertex(const Array* vertices,const IndexArray* indices): - _vertices(vertices), - _indices(indices) {} - - inline void operator () (unsigned int pos) - { - if (_indices) _vertices->accept(_indices->index(pos),*this); - else _vertices->accept(pos,*this); - } - - virtual void apply(const Vec2& v) { glVertex2fv(v.ptr()); } - virtual void apply(const Vec3& v) { glVertex3fv(v.ptr()); } - virtual void apply(const Vec4& v) { glVertex4fv(v.ptr()); } - - const Array* _vertices; - const IndexArray* _indices; -}; -#endif class DrawNormal { @@ -159,7 +135,6 @@ class DrawNormal Array::Type _normalsType; }; -#if 1 class DrawColor { public: @@ -215,29 +190,7 @@ class DrawColor Array::Type _colorsType; Array::Type _indicesType; }; -#else -class DrawColor : public osg::ConstValueVisitor -{ - public: - DrawColor(const Array* colors,const IndexArray* indices): - _colors(colors), - _indices(indices) {} - - inline void operator () (unsigned int pos) - { - if (_indices) _colors->accept(_indices->index(pos),*this); - else _colors->accept(pos,*this); - } - - virtual void apply(const UVec4b& v) { glColor4ubv(v.ptr()); } - virtual void apply(const Vec3& v) { glColor3fv(v.ptr()); } - virtual void apply(const Vec4& v) { glColor4fv(v.ptr()); } - - const Array* _colors; - const IndexArray* _indices; -}; -#endif class DrawVertexAttrib : public osg::Referenced, public osg::ConstValueVisitor { public: @@ -1414,12 +1367,6 @@ void Geometry::drawImplementation(RenderInfo& renderInfo) const // // Vertex Buffer Object path for defining vertex arrays. // -#if 1 - - - #if 1 - - state.setNormalPointer(_normalData.binding==BIND_PER_VERTEX ? _normalData.array.get() : 0); state.setColorPointer(_colorData.binding==BIND_PER_VERTEX ? _colorData.array.get() : 0); state.setSecondaryColorPointer(_secondaryColorData.binding==BIND_PER_VERTEX ? _secondaryColorData.array.get() : 0); @@ -1467,320 +1414,6 @@ void Geometry::drawImplementation(RenderInfo& renderInfo) const state.setVertexPointer(_vertexData.array.get()); - #else - - // first compile the VBO's - const VertexBufferObject* prev_vbo = 0; - const VertexBufferObject* new_vbo = 0; - - new_vbo = _vertexData.array.valid() ? _vertexData.array->getVertexBufferObject() : 0; - if (new_vbo && new_vbo!=prev_vbo) { new_vbo->compileBuffer(contextID, state); prev_vbo = new_vbo; } - - new_vbo = (_normalData.binding==BIND_PER_VERTEX && _normalData.array.valid()) ? _normalData.array->getVertexBufferObject() : 0; - if (new_vbo && new_vbo!=prev_vbo) { new_vbo->compileBuffer(contextID, state); prev_vbo = new_vbo; } - - new_vbo = (_colorData.binding==BIND_PER_VERTEX && _colorData.array.valid()) ? _colorData.array->getVertexBufferObject() : 0; - if (new_vbo && new_vbo!=prev_vbo) { new_vbo->compileBuffer(contextID, state); prev_vbo = new_vbo; } - - new_vbo = (_secondaryColorData.binding==BIND_PER_VERTEX && _secondaryColorData.array.valid()) ? _secondaryColorData.array->getVertexBufferObject() : 0; - if (new_vbo && new_vbo!=prev_vbo) { new_vbo->compileBuffer(contextID, state); prev_vbo = new_vbo; } - - new_vbo = (_fogCoordData.binding==BIND_PER_VERTEX && _fogCoordData.array.valid()) ? _fogCoordData.array->getVertexBufferObject() : 0; - if (new_vbo && new_vbo!=prev_vbo) { new_vbo->compileBuffer(contextID, state); prev_vbo = new_vbo; } - - unsigned int unit; - for(unit=0;unit<_texCoordList.size();++unit) - { - const Array* array = _texCoordList[unit].array.get(); - new_vbo = array ? array->getVertexBufferObject() : 0; - if (new_vbo && new_vbo!=prev_vbo) { new_vbo->compileBuffer(contextID, state); prev_vbo = new_vbo; } - } - - if( handleVertexAttributes ) - { - unsigned int index; - for( index = 0; index < _vertexAttribList.size(); ++index ) - { - const Array* array = _vertexAttribList[index].array.get(); - new_vbo = (_vertexAttribList[index].binding==BIND_PER_VERTEX && array) ? array->getVertexBufferObject() : 0; - if (new_vbo && new_vbo!=prev_vbo) { new_vbo->compileBuffer(contextID, state); prev_vbo = new_vbo; } - } - } - - prev_vbo = 0; - - SETNORMALPOINTER_IFPERVERTEXBINDING(_normalData, state.setNormalPointer, state.disableNormalPointer) - SETARRAYPOINTER_IFPERVERTEXBINDING(_colorData, state.setColorPointer, state.disableColorPointer) - SETARRAYPOINTER_IFPERVERTEXBINDING(_secondaryColorData, state.setSecondaryColorPointer, state.disableSecondaryColorPointer) - SETNORMALPOINTER_IFPERVERTEXBINDING(_fogCoordData, state.setFogCoordPointer, state.disableFogCoordPointer) - - for(unit=0;unit<_texCoordList.size();++unit) - { - SETARRAYUNITPOINTER_IFPERVERTEXBINDING(_texCoordList[unit], unit, state.setTexCoordPointer, state.disableVertexPointer) - } - state.disableTexCoordPointersAboveAndIncluding(unit); - - if( handleVertexAttributes ) - { - unsigned int index; - for( index = 0; index < _vertexAttribList.size(); ++index ) - { - const Array* array = _vertexAttribList[index].array.get(); - const AttributeBinding ab = _vertexAttribList[index].binding; - if(array && ab==BIND_PER_VERTEX) - { - new_vbo = array->getVertexBufferObject(); - if (new_vbo) - { - if (new_vbo!=prev_vbo) new_vbo->bindBuffer(contextID); - prev_vbo = new_vbo; - state.setVertexAttribPointer(index, array->getDataSize(),array->getDataType(),_vertexAttribList[index].normalize, 0,new_vbo->getOffset(array->getVertexBufferObjectIndex())); - } - else - { - extensions->glBindBuffer(GL_ARRAY_BUFFER_ARB,0); - prev_vbo = 0; - state.setVertexAttribPointer(index, array->getDataSize(),array->getDataType(),_vertexAttribList[index].normalize, 0,array->getDataPointer()); - } - } - else - state.disableVertexAttribPointer(index); - - if(array && ab!=BIND_PER_VERTEX) - { - const IndexArray* indexArray = _vertexAttribList[index].indices.get(); - - if( indexArray && indexArray->getNumElements() > 0 ) - { - drawVertexAttribMap[ab].push_back( - new DrawVertexAttrib(extensions,index,_vertexAttribList[index].normalize,array,indexArray) ); - } - else - { - drawVertexAttribMap[ab].push_back( - new DrawVertexAttrib(extensions,index,_vertexAttribList[index].normalize,array,0) ); - } - } - } - state.disableVertexAttribPointersAboveAndIncluding( index ); - - } - else if (vertexVertexAttributesSupported) - { - state.disableVertexAttribPointersAboveAndIncluding( 0 ); - } - - SETARRAYPOINTER(_vertexData, state.setVertexPointer, state.disableVertexPointer) - - const osg::ElementBufferObject* prev_ebo = 0; - const osg::ElementBufferObject* new_ebo = 0; - for(PrimitiveSetList::const_iterator itr=_primitives.begin(); - itr!=_primitives.end(); - ++itr) - { - const DrawElements* de = (*itr)->getDrawElements(); - if (de) - { - new_ebo = de->getElementBufferObject(); - if (new_ebo && new_ebo!=prev_ebo) - { - new_ebo->compileBuffer(contextID, state); - prev_ebo = new_ebo; - } - } - } - - #endif - -#else - - GLuint& buffer = _vboList[state.getContextID()]; - if (!buffer) - { - - //std::cout << "creating VertexBuffer "<glGenBuffers(1, &buffer); - extensions->glBindBuffer(GL_ARRAY_BUFFER_ARB,buffer); - - //std::cout << " gen VertexBuffer "<getTotalDataSize(); - - _normalData.offset = totalSize; - if (_normalData.array.valid()) totalSize += _normalData.array->getTotalDataSize(); - - _colorData.offset = totalSize; - if (_colorData.array.valid()) totalSize += _colorData.array->getTotalDataSize(); - - _secondaryColorData.offset = totalSize; - if (_secondaryColorData.array.valid()) totalSize += _secondaryColorData.array->getTotalDataSize(); - - _fogCoordData.offset = totalSize; - if (_fogCoordData.array.valid()) totalSize += _fogCoordData.array->getTotalDataSize(); - - unsigned int unit; - for(unit=0;unit<_texCoordList.size();++unit) - { - _texCoordList[unit].offset = totalSize; - const Array* array = _texCoordList[unit].array.get(); - if (array) - totalSize += array->getTotalDataSize(); - - } - - if( handleVertexAttributes ) - { - unsigned int index; - for( index = 0; index < _vertexAttribList.size(); ++index ) - { - _vertexAttribList[index].offset = totalSize; - const Array* array = _vertexAttribList[index].array.get(); - const AttributeBinding ab = _vertexAttribList[index].binding; - if( ab == BIND_PER_VERTEX && array ) - { - totalSize += array->getTotalDataSize(); - } - } - } - - // allocated the buffer space, but leave the copy to be done per vertex array below - extensions->glBufferData(GL_ARRAY_BUFFER_ARB,totalSize, 0, GL_STATIC_DRAW_ARB); - - //std::cout << " Created VertexBuffer "<getDataSize(),_vertexData.array->getDataType(),0,(const GLvoid*)_vertexData.offset); - else - state.disableVertexPointer(); - - if (_normalData.binding==BIND_PER_VERTEX && _normalData.array.valid()) - state.setNormalPointer(GL_FLOAT,0,(const GLvoid*)_normalData.offset); - else - state.disableNormalPointer(); - - if (_colorData.binding==BIND_PER_VERTEX && _colorData.array.valid()) - state.setColorPointer(_colorData.array->getDataSize(),_colorData.array->getDataType(),0,(const GLvoid*)_colorData.offset); - else - state.disableColorPointer(); - - if (secondaryColorBinding==BIND_PER_VERTEX && _secondaryColorData.array.valid()) - state.setSecondaryColorPointer(_secondaryColorData.array->getDataSize(),_secondaryColorData.array->getDataType(),0,(const GLvoid*)_secondaryColorData.offset); - else - state.disableSecondaryColorPointer(); - - if (fogCoordBinding==BIND_PER_VERTEX && _fogCoordData.array.valid()) - state.setFogCoordPointer(GL_FLOAT,0,(const GLvoid*)_fogCoordData.offset); - else - state.disableFogCoordPointer(); - - unsigned int unit; - for(unit=0;unit<_texCoordList.size();++unit) - { - const Array* array = _texCoordList[unit].array.get(); - if (array) - state.setTexCoordPointer(unit,array->getDataSize(),array->getDataType(),0,(const GLvoid*)_texCoordList[unit].offset); - else - state.disableTexCoordPointer(unit); - } - state.disableTexCoordPointersAboveAndIncluding(unit); - - if( handleVertexAttributes ) - { - unsigned int index; - for( index = 0; index < _vertexAttribList.size(); ++index ) - { - const Array* array = _vertexAttribList[index].array.get(); - const AttributeBinding ab = _vertexAttribList[index].binding; - - if( ab == BIND_PER_VERTEX && array ) - { - state.setVertexAttribPointer( index, array->getDataSize(), array->getDataType(), - _vertexAttribList[index].normalize, 0, (const GLvoid*)_vertexAttribList[index].offset ); - } - else - { - if( array ) - { - const IndexArray* indexArray = _vertexAttribList[index].indices.get(); - - if( indexArray && indexArray->getNumElements() > 0 ) - { - drawVertexAttribMap[ab].push_back( - new DrawVertexAttrib(extensions,index,_vertexAttribList[index].normalize,array,indexArray) ); - } - else - { - drawVertexAttribMap[ab].push_back( - new DrawVertexAttrib(extensions,index,_vertexAttribList[index].normalize,array,0) ); - } - } - - state.disableVertexAttribPointer( index ); - } - } - state.disableVertexAttribPointersAboveAndIncluding( index ); - - } - else if (vertexVertexAttributesSupported) - { - state.disableVertexAttribPointersAboveAndIncluding( 0 ); - } -#endif } else @@ -1916,9 +1549,10 @@ void Geometry::drawImplementation(RenderInfo& renderInfo) const if (usingVertexBufferObjects) { - // extensions->glBindBuffer(GL_ARRAY_BUFFER_ARB,0); +#if 1 state.unbindVertexBufferObject(); state.unbindElementBufferObject(); +#endif } } diff --git a/src/osg/PrimitiveSet.cpp b/src/osg/PrimitiveSet.cpp index 286c349ad..b4f669899 100644 --- a/src/osg/PrimitiveSet.cpp +++ b/src/osg/PrimitiveSet.cpp @@ -154,8 +154,6 @@ void DrawElementsUByte::draw(State& state, bool useVertexBufferObjects) const { if (useVertexBufferObjects) { -#if 1 - #if 1 const ElementBufferObject* ebo = getElementBufferObject(); state.bindElementBufferObject(ebo); if (ebo) @@ -166,54 +164,6 @@ void DrawElementsUByte::draw(State& state, bool useVertexBufferObjects) const { glDrawElements(_mode, size(), GL_UNSIGNED_BYTE, &front()); } - #else - unsigned int contextID = state.getContextID(); - const BufferObject::Extensions* extensions = BufferObject::getExtensions(contextID, true); - - const ElementBufferObject* ebo = getElementBufferObject(); - if (ebo) - { - //ebo->compileBuffer(state); - ebo->bindBuffer(contextID); - - glDrawElements(_mode, size(), GL_UNSIGNED_BYTE, ebo->getOffset(getElementBufferObjectIndex())); - - extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); - } - else - { - glDrawElements(_mode, size(), GL_UNSIGNED_INT, &front()); - } - #endif -#else - unsigned int contextID = state.getContextID(); - const BufferObject::Extensions* extensions = BufferObject::getExtensions(contextID, true); - - GLuint& buffer = _vboList[contextID]._objectID; - if (!buffer) - { - extensions->glGenBuffers(1, &buffer); - extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, buffer); - extensions->glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB, size() * 1, &front(), GL_STATIC_DRAW_ARB); - // osg::notify(osg::NOTICE)<<"Generating ubyte buffer"<glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, buffer); - - if (_vboList[contextID]._modifiedCount != _modifiedCount) - { - extensions->glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB, size() * 1, &front(), GL_STATIC_DRAW_ARB); - _vboList[contextID]._modifiedCount = _modifiedCount; - } - } - - glDrawElements(_mode, size(), GL_UNSIGNED_BYTE, 0); - - extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); -#endif } else { @@ -280,8 +230,6 @@ void DrawElementsUShort::draw(State& state, bool useVertexBufferObjects) const { if (useVertexBufferObjects) { -#if 1 - #if 1 const ElementBufferObject* ebo = getElementBufferObject(); state.bindElementBufferObject(ebo); if (ebo) @@ -292,55 +240,6 @@ void DrawElementsUShort::draw(State& state, bool useVertexBufferObjects) const { glDrawElements(_mode, size(), GL_UNSIGNED_SHORT, &front()); } - #else - unsigned int contextID = state.getContextID(); - const BufferObject::Extensions* extensions = BufferObject::getExtensions(contextID, true); - - const ElementBufferObject* ebo = getElementBufferObject(); - if (ebo) - { - //ebo->compileBuffer(state); - ebo->bindBuffer(contextID); - - glDrawElements(_mode, size(), GL_UNSIGNED_SHORT, ebo->getOffset(getElementBufferObjectIndex())); - - extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); - } - else - { - glDrawElements(_mode, size(), GL_UNSIGNED_INT, &front()); - } - #endif -#else - unsigned int contextID = state.getContextID(); - const BufferObject::Extensions* extensions = BufferObject::getExtensions(contextID, true); - - GLuint& buffer = _vboList[contextID]._objectID; - if (!buffer) - { - - extensions->glGenBuffers(1, &buffer); - extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, buffer); - extensions->glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB, size() * 2, &front(), GL_STATIC_DRAW_ARB); - // osg::notify(osg::NOTICE)<<"Generating ushort buffer"<glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, buffer); - - if (_vboList[contextID]._modifiedCount != _modifiedCount) - { - extensions->glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB, size() * 2, &front(), GL_STATIC_DRAW_ARB); - _vboList[contextID]._modifiedCount = _modifiedCount; - } - } - - glDrawElements(_mode, size(), GL_UNSIGNED_SHORT, 0); - - extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); -#endif } else { @@ -407,8 +306,6 @@ void DrawElementsUInt::draw(State& state, bool useVertexBufferObjects) const { if (useVertexBufferObjects) { -#if 1 - #if 1 const ElementBufferObject* ebo = getElementBufferObject(); state.bindElementBufferObject(ebo); if (ebo) @@ -419,55 +316,6 @@ void DrawElementsUInt::draw(State& state, bool useVertexBufferObjects) const { glDrawElements(_mode, size(), GL_UNSIGNED_INT, &front()); } - #else - unsigned int contextID = state.getContextID(); - const BufferObject::Extensions* extensions = BufferObject::getExtensions(contextID, true); - - const ElementBufferObject* ebo = getElementBufferObject(); - if (ebo) - { - //ebo->compileBuffer(state); - ebo->bindBuffer(contextID); - - glDrawElements(_mode, size(), GL_UNSIGNED_INT, ebo->getOffset(getElementBufferObjectIndex())); - - extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); - } - else - { - glDrawElements(_mode, size(), GL_UNSIGNED_INT, &front()); - } - #endif -#else - unsigned int contextID = state.getContextID(); - const BufferObject::Extensions* extensions = BufferObject::getExtensions(contextID, true); - - GLuint& buffer = _vboList[contextID]._objectID; - if (!buffer) - { - extensions->glGenBuffers(1, &buffer); - extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, buffer); - extensions->glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB, size() * 4, &front(), GL_STATIC_DRAW_ARB); - // osg::notify(osg::NOTICE)<<"Generating buffer int"<glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, buffer); - - if (_vboList[contextID]._modifiedCount != _modifiedCount) - { - extensions->glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB, size() * 4, &front(), GL_STATIC_DRAW_ARB); - _vboList[contextID]._modifiedCount = _modifiedCount; - } - } - - glDrawElements(_mode, size(), GL_UNSIGNED_INT, 0); - - extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); -#endif } else {