From 4f1e6b28e8cfb7a190c2a38b497d195d78ffa66e Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 5 Jun 2013 07:27:35 +0000 Subject: [PATCH] Moved the Array::AttribDivisor into the Array::Binding enum to avoid conflicts in settings between Binding and AttribDivisor. Removed the vertify bindings/shared arrays handling from GeometryNew --- include/osg/Array | 27 +-- include/osg/GeometryNew | 18 +- src/osg/GeometryNew.cpp | 495 ---------------------------------------- 3 files changed, 15 insertions(+), 525 deletions(-) diff --git a/include/osg/Array b/include/osg/Array index 36eec5f91..cd6ac71cd 100644 --- a/include/osg/Array +++ b/include/osg/Array @@ -78,11 +78,19 @@ class OSG_EXPORT Array : public BufferData enum Binding { + BIND_UNDEFINED=-1, BIND_OFF=0, BIND_OVERALL=1, BIND_PER_PRIMITIVE_SET=2, BIND_PER_VERTEX=4, - BIND_AUTO=5 + BIND_INSTANCE_DIVISOR_0=6, + BIND_INSTANCE_DIVISOR_1=BIND_INSTANCE_DIVISOR_0+1, + BIND_INSTANCE_DIVISOR_2=BIND_INSTANCE_DIVISOR_0+2, + BIND_INSTANCE_DIVISOR_3=BIND_INSTANCE_DIVISOR_0+3, + BIND_INSTANCE_DIVISOR_4=BIND_INSTANCE_DIVISOR_0+4, + BIND_INSTANCE_DIVISOR_5=BIND_INSTANCE_DIVISOR_0+5, + BIND_INSTANCE_DIVISOR_6=BIND_INSTANCE_DIVISOR_0+6, + BIND_INSTANCE_DIVISOR_7=BIND_INSTANCE_DIVISOR_0+7 }; Array(Type arrayType=ArrayType,GLint dataSize=0,GLenum dataType=0): @@ -91,8 +99,7 @@ class OSG_EXPORT Array : public BufferData _dataType(dataType), _normalize(false), _preserveDataType(false), - _attribDivisor(false), - _binding(BIND_AUTO) {} + _binding(BIND_UNDEFINED) {} Array(const Array& array,const CopyOp& copyop=CopyOp::SHALLOW_COPY): BufferData(array,copyop), @@ -101,7 +108,6 @@ class OSG_EXPORT Array : public BufferData _dataType(array._dataType), _normalize(array._normalize), _preserveDataType(array._preserveDataType), - _attribDivisor(array._attribDivisor), _binding(array._binding) {} virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast(obj)!=NULL; } @@ -140,16 +146,12 @@ class OSG_EXPORT Array : public BufferData /** Get hint to ask that the array data is passed via integer or double, or normal setVertexAttribPointer function.*/ bool getPreserveDataType() const { return _preserveDataType; } - /** Set the rate at which generic vertex attributes advance during instanced rendering. Uses the glVertexAttribDivisor feature of OpenGL 4.0*/ - void setAttribDivisor(GLuint divisor) { _attribDivisor = divisor; } - /** Get the rate at which generic vertex attributes advance during instanced rendering.*/ - GLuint getAttribDivisor() const { return _attribDivisor; } - + /** Specify how this array should be passed to OpenGL.*/ - void setBinding(Binding binding) { _binding = binding; } + void setBinding(int binding) { _binding = binding; } /** Get how this array should be passed to OpenGL.*/ - Binding getBinding() const { return _binding; } + int getBinding() const { return _binding; } /** Frees unused space on this vector - i.e. the difference between size() and max_size() of the underlying vector.*/ @@ -173,8 +175,7 @@ class OSG_EXPORT Array : public BufferData GLenum _dataType; bool _normalize; bool _preserveDataType; - GLuint _attribDivisor; - Binding _binding; + int _binding; }; template diff --git a/include/osg/GeometryNew b/include/osg/GeometryNew index 2e69a6f10..5b41817ab 100644 --- a/include/osg/GeometryNew +++ b/include/osg/GeometryNew @@ -83,7 +83,7 @@ class OSG_EXPORT GeometryNew : public Drawable void setSecondaryColorArray(Array* array); Array* getSecondaryColorArray() { return _secondaryColorArray.get(); } - const Array* getSecondaryColorArray() const; + const Array* getSecondaryColorArray() const { return _secondaryColorArray.get(); } void setFogCoordBinding(AttributeBinding ab); @@ -177,19 +177,6 @@ class OSG_EXPORT GeometryNew : public Drawable osg::ElementBufferObject* getOrCreateElementBufferObject(); - bool verifyBindings() const; - - void computeCorrectBindingsAndArraySizes(); - - /** check whether the arrays, indices, bindings and primitives all match correctly, return false is .*/ - bool verifyArrays(std::ostream& out) const; - - - bool containsSharedArrays() const; - - void duplicateSharedArrays(); - - /** Return the estimated size of GLObjects (display lists/vertex buffer objects) that are associated with this drawable. * This size is used a hint for reuse of deleted display lists/vertex buffer objects. */ @@ -237,9 +224,6 @@ class OSG_EXPORT GeometryNew : public Drawable virtual ~GeometryNew(); - bool verifyBindings(const osg::ref_ptr& array) const; - - void computeCorrectBindingsAndArraySizes(osg::ref_ptr& ,const char* arrayName); void addVertexBufferObjectIfRequired(osg::Array* array); void addElementBufferObjectIfRequired(osg::PrimitiveSet* primitiveSet); diff --git a/src/osg/GeometryNew.cpp b/src/osg/GeometryNew.cpp index 62f3418d8..bdc58181a 100644 --- a/src/osg/GeometryNew.cpp +++ b/src/osg/GeometryNew.cpp @@ -769,14 +769,6 @@ void GeometryNew::compileGLObjects(RenderInfo& renderInfo) const void GeometryNew::drawImplementation(RenderInfo& renderInfo) const { -#if 0 - if (!validGeometryNew()) - { - OSG_NOTICE<<"Error, osg::Geometry has invalid array/primitive set usage"<get(); - GLenum mode=primitiveset->getMode(); - - unsigned int primLength; - switch(mode) - { - case(GL_POINTS): primLength=1; OSG_INFO<<"prim=GL_POINTS"<getType()) - { - case(PrimitiveSet::DrawArrayLengthsPrimitiveType): - { - - const DrawArrayLengths* drawArrayLengths = static_cast(primitiveset); - for(DrawArrayLengths::const_iterator primItr=drawArrayLengths->begin(); - primItr!=drawArrayLengths->end(); - ++primItr) - { - if (primLength==0) totalNumberOfPrimitives += 1; - else totalNumberOfPrimitives += *primItr/primLength; - } - break; - } - default: - { - if (primLength==0) { totalNumberOfPrimitives += 1; OSG_INFO<<" totalNumberOfPrimitives="<getNumIndices()/primLength; OSG_INFO<<" primitiveset->getNumIndices()="<getNumIndices()<<" totalNumberOfPrimitives="< -bool _verifyBindings(const osg::GeometryNew& geom, const A& array) -{ - unsigned int numElements = array.valid()?array->getNumElements():0; - - switch(array->getBinding()) - { - case(osg::Array::BIND_OFF): - if (numElements>0) return false; - break; - case(osg::Array::BIND_OVERALL): - if (numElements!=1) return false; - break; - case(osg::Array::BIND_PER_PRIMITIVE_SET): - if (numElements!=geom.getPrimitiveSetList().size()) return false; - break; - case(osg::Array::BIND_PER_VERTEX): - { - unsigned int numVertices = geom.getVertexArray()?geom.getVertexArray()->getNumElements():0; - if (numElements!=numVertices) return false; - break; - } - case(osg::Array::BIND_AUTO): - { - return true; - } - } - return true; -} - -template -void _computeCorrectBindingsAndArraySizes(std::ostream& out, const osg::GeometryNew& geom, A& array, const char* arrayName) -{ - unsigned int numElements = array.valid()?array->getNumElements():0; - - // check to see if binding matches 0 elements required. - if (numElements==0) - { - // correct binding if not correct. - if (array->getBinding()!=osg::Array::BIND_OFF) - { - out<<"Warning: in osg::GeometryNew::computeCorrectBindingsAndArraySizes() "<setBinding(osg::Array::BIND_OFF); - } - return; - } - - // check to see if binding matches 1 elements required. - if (numElements==1) - { - // correct binding if not correct. - if (array->getBinding()!=osg::Array::BIND_OVERALL) - { - out<<"Warning: in osg::GeometryNew::computeCorrectBindingsAndArraySizes() "<setBinding(osg::Array::BIND_OVERALL); - } - return; - } - - - unsigned int numVertices = geom.getVertexArray()?geom.getVertexArray()->getNumElements():0; - - if ( numVertices==0 ) - { - if (array->getBinding()!=osg::Array::BIND_OFF) - { - array = 0; - out<<"Warning: in osg::GeometryNew::computeCorrectBindingsAndArraySizes() vertex array is empty but "<getBinding()!=osg::Array::BIND_PER_VERTEX) - { - out<<"Warning: in osg::GeometryNew::computeCorrectBindingsAndArraySizes() "<setBinding(osg::Array::BIND_PER_VERTEX); - } - return; - } - - - - // check to see if binding might be per primitive set - unsigned int numPrimitiveSets = geom.getPrimitiveSetList().size(); - - if (numElements==numPrimitiveSets) - { - if (array->getBinding() != osg::Array::BIND_PER_PRIMITIVE_SET) - { - out<<"Warning: in osg::GeometryNew::computeCorrectBindingsAndArraySizes() "<setBinding(osg::Array::BIND_PER_PRIMITIVE_SET); - } - return; - } - - if (numElements>=numVertices) - { - array->setBinding(osg::Array::BIND_PER_VERTEX); - return; - } - if (numElements>=numPrimitiveSets) - { - array->setBinding(osg::Array::BIND_PER_PRIMITIVE_SET); - return; - } - if (numElements>=1) - { - array->setBinding(osg::Array::BIND_OVERALL); - return; - } - array = 0; -} - -bool GeometryNew::verifyBindings(const osg::ref_ptr& array) const -{ - return _verifyBindings(*this, array); -} - -bool GeometryNew::verifyBindings() const -{ - if (!verifyBindings(_normalArray)) return false; - if (!verifyBindings(_colorArray)) return false; - if (!verifyBindings(_secondaryColorArray)) return false; - if (!verifyBindings(_fogCoordArray)) return false; - - for(ArrayList::const_iterator titr=_texCoordList.begin(); - titr!=_texCoordList.end(); - ++titr) - { - if (!verifyBindings(*titr)) return false; - } - - for(ArrayList::const_iterator vitr=_vertexAttribList.begin(); - vitr!=_vertexAttribList.end(); - ++vitr) - { - if (!verifyBindings(*vitr)) return false; - } - - return true; -} - -void GeometryNew::computeCorrectBindingsAndArraySizes(osg::ref_ptr& array,const char* arrayName) -{ - return _computeCorrectBindingsAndArraySizes(osg::notify(osg::INFO), *this, array, arrayName); -} - -void GeometryNew::computeCorrectBindingsAndArraySizes() -{ - // if (verifyBindings()) return; - - computeCorrectBindingsAndArraySizes(_normalArray,"_normalArray"); - computeCorrectBindingsAndArraySizes(_colorArray,"_colorArray"); - computeCorrectBindingsAndArraySizes(_secondaryColorArray,"_secondaryColorArray"); - computeCorrectBindingsAndArraySizes(_fogCoordArray,"_fogCoordArray"); - - for(ArrayList::iterator titr=_texCoordList.begin(); - titr!=_texCoordList.end(); - ++titr) - { - computeCorrectBindingsAndArraySizes(*titr,"_texCoordList[]"); - } - - for(ArrayList::iterator vitr=_vertexAttribList.begin(); - vitr!=_vertexAttribList.end(); - ++vitr) - { - computeCorrectBindingsAndArraySizes(*vitr,"_vertAttribList[]"); - } -} - - -bool GeometryNew::containsSharedArrays() const -{ - unsigned int numSharedArrays = 0; - - if (getVertexArray() && getVertexArray()->referenceCount()>1) ++numSharedArrays; - if (getNormalArray() && getNormalArray()->referenceCount()>1) ++numSharedArrays; - if (getColorArray() && getColorArray()->referenceCount()>1) ++numSharedArrays; - if (getSecondaryColorArray() && getSecondaryColorArray()->referenceCount()>1) ++numSharedArrays; - if (getFogCoordArray() && getFogCoordArray()->referenceCount()>1) ++numSharedArrays; - - for(unsigned int ti=0;tireferenceCount()>1) ++numSharedArrays; - } - - for(unsigned int vi=0;vi<_vertexAttribList.size();++vi) - { - const Array* array = _vertexAttribList[vi].get(); - if (array && array->referenceCount()>1) ++numSharedArrays; - } - return numSharedArrays!=0; -} - -void GeometryNew::duplicateSharedArrays() -{ - #define DUPLICATE_IF_REQUIRED(A) \ - if (get##A() && get##A()->referenceCount()>1) \ - { \ - set##A(osg::clone(get##A(), osg::CopyOp::DEEP_COPY_ARRAYS)); \ - } - - DUPLICATE_IF_REQUIRED(VertexArray) - DUPLICATE_IF_REQUIRED(NormalArray) - DUPLICATE_IF_REQUIRED(ColorArray) - DUPLICATE_IF_REQUIRED(SecondaryColorArray) - DUPLICATE_IF_REQUIRED(FogCoordArray) - - for(unsigned int ti=0;tireferenceCount()>1) - { - setTexCoordArray(ti, osg::clone(getTexCoordArray(ti),osg::CopyOp::DEEP_COPY_ARRAYS)); - } - } - - for(unsigned int vi=0;vi<_vertexAttribList.size();++vi) - { - Array* array = _vertexAttribList[vi].get(); - if (array && array->referenceCount()>1) - { - _vertexAttribList[vi] = osg::clone(array, osg::CopyOp::DEEP_COPY_ARRAYS); - } - } -} - -class CheckArrayValidity -{ -public: - CheckArrayValidity(const osg::GeometryNew* geometry) - { - numPrimitiveSets = geometry->getNumPrimitiveSets(); - primitiveNum = 0; - maxVertexNumber = 0; - - /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // - // draw the primitives themselves. - // - for(unsigned int primitiveSetNum=0; primitiveSetNum != numPrimitiveSets; ++primitiveSetNum) - { - const PrimitiveSet* primitiveset = geometry->getPrimitiveSet(primitiveSetNum); - - GLenum mode=primitiveset->getMode(); - - unsigned int primLength; - switch(mode) - { - case(GL_POINTS): primLength=1; break; - case(GL_LINES): primLength=2; break; - case(GL_TRIANGLES): primLength=3; break; - case(GL_QUADS): primLength=4; break; - default: primLength=0; break; // compute later when =0. - } - - // draw primitives by the more flexible "slow" path, - // sending OpenGL glBegin/glVertex.../glEnd(). - switch(primitiveset->getType()) - { - case(PrimitiveSet::DrawArraysPrimitiveType): - { - if (primLength==0) primLength=primitiveset->getNumIndices(); - - const DrawArrays* drawArray = static_cast(primitiveset); - - unsigned int primCount=0; - unsigned int indexEnd = drawArray->getFirst()+drawArray->getCount(); - for(unsigned int vindex=drawArray->getFirst(); - vindex maxVertexNumber) maxVertexNumber = (indexEnd-1); - break; - } - case(PrimitiveSet::DrawArrayLengthsPrimitiveType): - { - const DrawArrayLengths* drawArrayLengths = static_cast(primitiveset); - unsigned int vindex=drawArrayLengths->getFirst(); - for(DrawArrayLengths::const_iterator primItr=drawArrayLengths->begin(); - primItr!=drawArrayLengths->end(); - ++primItr) - { - unsigned int localPrimLength; - if (primLength==0) localPrimLength=*primItr; - else localPrimLength=primLength; - - for(GLsizei primCount=0; - primCount<*primItr; - ++vindex,++primCount) - { - if ((primCount%localPrimLength)==0) - { - primitiveNum++; - } - } - - } - if ((vindex-1) > maxVertexNumber) maxVertexNumber = (vindex-1); - break; - } - case(PrimitiveSet::DrawElementsUBytePrimitiveType): - { - if (primLength==0) primLength=primitiveset->getNumIndices(); - - const DrawElementsUByte* drawElements = static_cast(primitiveset); - - unsigned int primCount=0; - for(DrawElementsUByte::const_iterator primItr=drawElements->begin(); - primItr!=drawElements->end(); - ++primCount,++primItr) - { - if ((primCount%primLength)==0) - { - primitiveNum++; - } - unsigned int vindex = *primItr; - if (vindex > maxVertexNumber) maxVertexNumber = vindex; - } - - break; - } - case(PrimitiveSet::DrawElementsUShortPrimitiveType): - { - if (primLength==0) primLength=primitiveset->getNumIndices(); - - const DrawElementsUShort* drawElements = static_cast(primitiveset); - unsigned int primCount=0; - for(DrawElementsUShort::const_iterator primItr=drawElements->begin(); - primItr!=drawElements->end(); - ++primCount,++primItr) - { - if ((primCount%primLength)==0) - { - primitiveNum++; - } - unsigned int vindex = *primItr; - if (vindex > maxVertexNumber) maxVertexNumber = vindex; - } - - break; - } - case(PrimitiveSet::DrawElementsUIntPrimitiveType): - { - if (primLength==0) primLength=primitiveset->getNumIndices(); - - const DrawElementsUInt* drawElements = static_cast(primitiveset); - unsigned int primCount=0; - for(DrawElementsUInt::const_iterator primItr=drawElements->begin(); - primItr!=drawElements->end(); - ++primCount,++primItr) - { - if ((primCount%primLength)==0) - { - primitiveNum++; - } - unsigned int vindex = *primItr; - if (vindex > maxVertexNumber) maxVertexNumber = vindex; - } - break; - } - default: - { - break; - } - } - } - } - - bool validArray(std::ostream& out, const osg::Array* array, const char* arrayName) - { - unsigned int numRequired = 0; - switch(array->getBinding()) - { - case(osg::Array::BIND_OFF): numRequired = 0; break; - case(osg::Array::BIND_OVERALL): numRequired = 1; break; - case(osg::Array::BIND_PER_PRIMITIVE_SET): numRequired = numPrimitiveSets; break; - case(osg::Array::BIND_PER_VERTEX): numRequired = maxVertexNumber+1; break; - case(osg::Array::BIND_AUTO): numRequired = maxVertexNumber+1; break; - } - - { - unsigned int numElements = array ? array->getNumElements() : 0; - if (numElements