From e14ee802826316aea346457949d36c2dde77667b Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 10 Oct 2016 20:49:19 +0100 Subject: [PATCH] Streamlined the dispatch and activation of attribute dispatchers --- include/osg/ArrayDispatchers | 42 ++++++++++++++++++++------------- src/osg/ArrayDispatchers.cpp | 7 +----- src/osg/Geometry.cpp | 9 +++---- src/osgTerrain/GeometryPool.cpp | 3 --- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/include/osg/ArrayDispatchers b/include/osg/ArrayDispatchers index 2d5d7b57f..2044520bb 100644 --- a/include/osg/ArrayDispatchers +++ b/include/osg/ArrayDispatchers @@ -45,16 +45,28 @@ class OSG_EXPORT ArrayDispatchers : public osg::Referenced void setUseVertexAttribAlias(bool flag) { _useVertexAttribAlias = flag; } bool getUseVertexAttribAlias() const { return _useVertexAttribAlias; } - void activate(unsigned int binding, AttributeDispatch* at) - { - if (at) _activeDispatchList[binding].push_back(at); - } - void activateColorArray(osg::Array* array) { if (array && array->getBinding()>osg::Array::BIND_OFF) activate(array->getBinding(), colorDispatcher(array)); } - void activateNormalArray(osg::Array* array) { if (array && array->getBinding()>osg::Array::BIND_OFF) activate(array->getBinding(), normalDispatcher(array)); } - void activateSecondaryColorArray(osg::Array* array) { if (array && array->getBinding()>osg::Array::BIND_OFF) activate(array->getBinding(), secondaryColorDispatcher(array)); } - void activateFogCoordArray(osg::Array* array) { if (array && array->getBinding()>osg::Array::BIND_OFF) activate(array->getBinding(), fogCoordDispatcher(array)); } - void activateVertexAttribArray(unsigned int unit, osg::Array* array) { if (array && array->getBinding()>osg::Array::BIND_OFF) activate(array->getBinding(), vertexAttribDispatcher(unit, array)); } + #define DISPATCH_OR_ACTIVATE(array, dispatcher) \ + if (array) { \ + unsigned int binding = array->getBinding(); \ + if (binding==osg::Array::BIND_OVERALL) \ + { \ + AttributeDispatch* at = dispatcher; \ + if (at) (*at)(0); \ + } \ + else if (binding==osg::Array:: BIND_PER_PRIMITIVE_SET) \ + { \ + AttributeDispatch* at = dispatcher; \ + if (at) _activeDispatchList.push_back(at); \ + } \ + } + + + void activateColorArray(osg::Array* array) { DISPATCH_OR_ACTIVATE(array, colorDispatcher(array)); } + void activateNormalArray(osg::Array* array) { DISPATCH_OR_ACTIVATE(array, normalDispatcher(array)); } + void activateSecondaryColorArray(osg::Array* array) { DISPATCH_OR_ACTIVATE(array, secondaryColorDispatcher(array)); } + void activateFogCoordArray(osg::Array* array) { DISPATCH_OR_ACTIVATE(array, fogCoordDispatcher(array)); } + void activateVertexAttribArray(unsigned int unit, osg::Array* array) { DISPATCH_OR_ACTIVATE(array, vertexAttribDispatcher(unit , array)); } AttributeDispatch* normalDispatcher(Array* array); AttributeDispatch* colorDispatcher(Array* array); @@ -62,18 +74,17 @@ class OSG_EXPORT ArrayDispatchers : public osg::Referenced AttributeDispatch* fogCoordDispatcher(Array* array); AttributeDispatch* vertexAttribDispatcher(unsigned int unit, Array* array); - void dispatch(unsigned int binding, unsigned int index) + void dispatch(unsigned int index) { - AttributeDispatchList& ad = _activeDispatchList[binding]; - for(AttributeDispatchList::iterator itr = ad.begin(); - itr != ad.end(); + for(AttributeDispatchList::iterator itr = _activeDispatchList.begin(); + itr != _activeDispatchList.end(); ++itr) { (*(*itr))(index); } } - bool active(unsigned int binding) const { return !_activeDispatchList[binding].empty(); } + bool active() const { return !_activeDispatchList.empty(); } protected: @@ -95,8 +106,7 @@ class OSG_EXPORT ArrayDispatchers : public osg::Referenced typedef std::vector AttributeDispatchList; - typedef std::vector ActiveDispatchList; - ActiveDispatchList _activeDispatchList; + AttributeDispatchList _activeDispatchList; bool _useVertexAttribAlias; }; diff --git a/src/osg/ArrayDispatchers.cpp b/src/osg/ArrayDispatchers.cpp index f0fb650db..e9acfc9fc 100644 --- a/src/osg/ArrayDispatchers.cpp +++ b/src/osg/ArrayDispatchers.cpp @@ -268,12 +268,7 @@ void ArrayDispatchers::reset() _useVertexAttribAlias = false; - for(ActiveDispatchList::iterator itr = _activeDispatchList.begin(); - itr != _activeDispatchList.end(); - ++itr) - { - (*itr).clear(); - } + _activeDispatchList.clear(); } } diff --git a/src/osg/Geometry.cpp b/src/osg/Geometry.cpp index 54d637365..58ea71c6a 100644 --- a/src/osg/Geometry.cpp +++ b/src/osg/Geometry.cpp @@ -840,15 +840,12 @@ void Geometry::drawVertexArraysImplementation(RenderInfo& renderInfo) const } } + // activate or dispatch any attributes that are bound overall arrayDispatchers.activateNormalArray(_normalArray.get()); arrayDispatchers.activateColorArray(_colorArray.get()); arrayDispatchers.activateSecondaryColorArray(_secondaryColorArray.get()); arrayDispatchers.activateFogCoordArray(_fogCoordArray.get()); - // dispatch any attributes that are bound overall - arrayDispatchers.dispatch(osg::Array::BIND_OVERALL,0); - - if (state.useVertexArrayObject(_useVertexArrayObject)) { if (!vas->getRequiresSetArrays()) return; @@ -902,11 +899,11 @@ void Geometry::drawPrimitivesImplementation(RenderInfo& renderInfo) const ArrayDispatchers& arrayDispatchers = state.getArrayDispatchers(); bool usingVertexBufferObjects = state.useVertexBufferObject(_supportsVertexBufferObjects && _useVertexBufferObjects); - bool bindPerPrimitiveSetActive = arrayDispatchers.active(osg::Array::BIND_PER_PRIMITIVE_SET); + bool bindPerPrimitiveSetActive = arrayDispatchers.active(); for(unsigned int primitiveSetNum=0; primitiveSetNum!=_primitives.size(); ++primitiveSetNum) { // dispatch any attributes that are bound per primitive - if (bindPerPrimitiveSetActive) arrayDispatchers.dispatch(osg::Array::BIND_PER_PRIMITIVE_SET, primitiveSetNum); + if (bindPerPrimitiveSetActive) arrayDispatchers.dispatch(primitiveSetNum); const PrimitiveSet* primitiveset = _primitives[primitiveSetNum].get(); diff --git a/src/osgTerrain/GeometryPool.cpp b/src/osgTerrain/GeometryPool.cpp index e1d00e3ff..0ec6f46ff 100644 --- a/src/osgTerrain/GeometryPool.cpp +++ b/src/osgTerrain/GeometryPool.cpp @@ -876,9 +876,6 @@ void SharedGeometry::drawImplementation(osg::RenderInfo& renderInfo) const arrayDispatchers.activateNormalArray(_normalArray.get()); arrayDispatchers.activateColorArray(_colorArray.get()); - // dispatch any attributes that are bound overall - arrayDispatchers.dispatch(osg::Array::BIND_OVERALL,0); - state.lazyDisablingOfVertexAttributes(); // set up arrays