From 1f147f6bc69c6df464eed7df889ce1bf3a15d7ef Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 4 Aug 2016 22:00:58 +0100 Subject: [PATCH] Moved implementations from .cpp's to headers as inline methods to improve performance. --- include/osg/Drawable | 75 ++++++- include/osg/State | 402 +++++++++++++++++++++-------------- include/osg/VertexArrayState | 81 +++++-- src/osg/Drawable.cpp | 75 ------- src/osg/Geometry.cpp | 1 - src/osg/State.cpp | 117 +++++----- src/osg/VertexArrayState.cpp | 93 +------- 7 files changed, 448 insertions(+), 396 deletions(-) diff --git a/include/osg/Drawable b/include/osg/Drawable index 63f51a3a3..0a2d5700a 100644 --- a/include/osg/Drawable +++ b/include/osg/Drawable @@ -256,11 +256,7 @@ class OSG_EXPORT Drawable : public Node * \c virtual). Subclasses should override * \c drawImplementation() instead. */ -#if 0 inline void draw(RenderInfo& renderInfo) const; -#else - void draw(RenderInfo& renderInfo) const; -#endif inline void drawInner(RenderInfo& renderInfo) const { @@ -495,7 +491,76 @@ class OSG_EXPORT Drawable : public Node ref_ptr _drawCallback; }; -#if 0 +#if 1 +inline void Drawable::draw(RenderInfo& renderInfo) const +{ + State& state = *renderInfo.getState(); + bool useVertexArrayObject = _useVertexBufferObjects && state.useVertexArrayObject(); + + if (useVertexArrayObject) + { + unsigned int contextID = renderInfo.getContextID(); + + VertexArrayState* vas = _vertexArrayStateList[contextID].get(); + if (!vas) + { + _vertexArrayStateList[contextID] = vas = createVertexArrayState(renderInfo, true); + } + else + { + // vas->setRequiresSetArrays(getDataVariance()==osg::Object::DYNAMIC); + } + + State::SetCurrentVertexArrayStateProxy setVASProxy(state, vas); + + vas->bindVertexArrayObject(); + + drawInner(renderInfo); + + vas->setRequiresSetArrays(getDataVariance()==osg::Object::DYNAMIC); + + return; + } + + // TODO, add check against whether VOA is active and supported + if (state.getCurrentVertexArrayState()) state.getCurrentVertexArrayState()->bindVertexArrayObject(); + + +#ifdef OSG_GL_DISPLAYLISTS_AVAILABLE + if (_useDisplayList) + { + // get the contextID (user defined ID of 0 upwards) for the + // current OpenGL context. + unsigned int contextID = renderInfo.getContextID(); + + // get the globj for the current contextID. + GLuint& globj = _globjList[contextID]; + + if( globj == 0 ) + { + // compile the display list + globj = generateDisplayList(contextID, getGLObjectSizeHint()); + glNewList( globj, GL_COMPILE ); + + drawInner(renderInfo); + + glEndList(); + } + + // call the display list + glCallList( globj); + } + else +#endif + { + // if state.previousVertexArrayState() is different than currentVertexArrayState bind current + + // OSG_NOTICE<<"Fallback drawInner()........................"<= _texCoordArrayList.size()) _texCoordArrayList.resize(unit+1); - EnabledArrayPair& eap = _texCoordArrayList[unit]; - - if (eap._enabled || eap._dirty) - { - if(setClientActiveTextureUnit(unit)) - { - eap._lazy_disable = false; - eap._enabled = false; - eap._dirty = false; - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - } - } - } - #else - disableVertexAttribPointer(_texCoordAliasList[unit]._location); - #endif - } -#endif - -#if 1 - void dirtyTexCoordPointer( unsigned int unit ); -#else - inline void dirtyTexCoordPointer( unsigned int unit ) - { - #ifdef OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE - if (_useVertexAttributeAliasing) - { - dirtyVertexAttribPointer(_texCoordAliasList[unit]._location); - } - else - { - if ( unit >= _texCoordArrayList.size()) return; // _texCoordArrayList.resize(unit+1); - EnabledArrayPair& eap = _texCoordArrayList[unit]; - eap._pointer = 0; - eap._dirty = true; - } - #else - dirtyVertexAttribPointer(_texCoordAliasList[unit]._location); - #endif - } -#endif - -#if 1 - void disableTexCoordPointersAboveAndIncluding( unsigned int unit ); + inline void disableTexCoordPointersAboveAndIncluding( unsigned int unit ); #else inline void disableTexCoordPointersAboveAndIncluding( unsigned int unit ) { @@ -1326,31 +1205,6 @@ class OSG_EXPORT State : public Referenced } #endif -#if 1 - void dirtyTexCoordPointersAboveAndIncluding( unsigned int unit ); -#else - inline void dirtyTexCoordPointersAboveAndIncluding( unsigned int unit ) - { - #ifdef OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE - if (_useVertexAttributeAliasing) - { - dirtyVertexAttribPointersAboveAndIncluding(_texCoordAliasList[unit]._location); - } - else - { - while (unit<_texCoordArrayList.size()) - { - EnabledArrayPair& eap = _texCoordArrayList[unit]; - eap._pointer = 0; - eap._dirty = true; - ++unit; - } - } - #else - dirtyVertexAttribPointersAboveAndIncluding(_texCoordAliasList[unit]._location); - #endif - } -#endif /// For GL>=2.0 uses GL_MAX_TEXTURE_COORDS, for GL<2 uses GL_MAX_TEXTURE_UNITS inline GLint getMaxTextureCoords() const { return _glMaxTextureCoords; } @@ -1468,6 +1322,157 @@ class OSG_EXPORT State : public Referenced void disableVertexAttribPointersAboveAndIncluding( unsigned int index ); +#if 1 + void disableTexCoordPointer( unsigned int unit ); +#else + /** wrapper around glDisableClientState(GL_TEXTURE_COORD_ARRAY); + * note, only updates values that change.*/ + inline void disableTexCoordPointer( unsigned int unit ) + { + #ifdef OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE + if (_useVertexAttributeAliasing) + { + disableVertexAttribPointer(_texCoordAliasList[unit]._location); + } + else + { + if ( unit >= _texCoordArrayList.size()) _texCoordArrayList.resize(unit+1); + EnabledArrayPair& eap = _texCoordArrayList[unit]; + + if (eap._enabled || eap._dirty) + { + if(setClientActiveTextureUnit(unit)) + { + eap._lazy_disable = false; + eap._enabled = false; + eap._dirty = false; + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + } + } + } + #else + disableVertexAttribPointer(_texCoordAliasList[unit]._location); + #endif + } +#endif + + /** dirty the vertex, normal, color, tex coords, secondary color, fog coord and index arrays.*/ + void dirtyAllVertexArrays(); + +#if 0 + +#if 1 + void dirtyNormalPointer(); +#else + inline void dirtyNormalPointer() + { + #ifdef OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE + if (_useVertexAttributeAliasing) + { + dirtyVertexAttribPointer(_normalAlias._location); + } + else + { + _normalArray._pointer = 0; + _normalArray._dirty = true; + } + #else + dirtyVertexAttribPointer(_normalAlias._location); + #endif + } +#endif + +#if 1 + void dirtyVertexPointer(); +#else + inline void dirtyVertexPointer() + { + #ifdef OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE + if (_useVertexAttributeAliasing) + { + dirtyVertexAttribPointer(_vertexAlias._location); + } + else + { + _vertexArray._pointer = 0; + _vertexArray._dirty = true; + } + #else + dirtyVertexAttribPointer(_vertexAlias._location); + #endif + } +#endif + +#if 1 + void dirtyFogCoordPointer(); +#else + inline void dirtyFogCoordPointer() + { + #ifdef OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE + if (_useVertexAttributeAliasing) + { + dirtyVertexAttribPointer(_fogCoordAlias._location); + } + else + { + _fogArray._pointer = 0; + _fogArray._dirty = true; + } + #else + dirtyVertexAttribPointer(_fogCoordAlias._location); + #endif + } +#endif + +#if 1 + void dirtyTexCoordPointer( unsigned int unit ); +#else + inline void dirtyTexCoordPointer( unsigned int unit ) + { + #ifdef OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE + if (_useVertexAttributeAliasing) + { + dirtyVertexAttribPointer(_texCoordAliasList[unit]._location); + } + else + { + if ( unit >= _texCoordArrayList.size()) return; // _texCoordArrayList.resize(unit+1); + EnabledArrayPair& eap = _texCoordArrayList[unit]; + eap._pointer = 0; + eap._dirty = true; + } + #else + dirtyVertexAttribPointer(_texCoordAliasList[unit]._location); + #endif + } +#endif + +#if 1 + void dirtyTexCoordPointersAboveAndIncluding( unsigned int unit ); +#else + inline void dirtyTexCoordPointersAboveAndIncluding( unsigned int unit ) + { + #ifdef OSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE + if (_useVertexAttributeAliasing) + { + dirtyVertexAttribPointersAboveAndIncluding(_texCoordAliasList[unit]._location); + } + else + { + while (unit<_texCoordArrayList.size()) + { + EnabledArrayPair& eap = _texCoordArrayList[unit]; + eap._pointer = 0; + eap._dirty = true; + ++unit; + } + } + #else + dirtyVertexAttribPointersAboveAndIncluding(_texCoordAliasList[unit]._location); + #endif + } +#endif + #if 1 void dirtyVertexAttribPointer( unsigned int index ); #else @@ -1495,6 +1500,8 @@ class OSG_EXPORT State : public Referenced ++index; } } +#endif + #endif bool isVertexBufferObjectSupported() const { return _isVertexBufferObjectSupportResolved?_isVertexBufferObjectSupported:computeVertexBufferObjectSupported(); } @@ -3097,6 +3104,83 @@ inline bool State::setActiveTextureUnit( unsigned int unit ) return true; } + +///////////////////////////////////////////////////////////////////////// +// +// New VertexArrayState version +// +inline void State::setVertexPointer(const Array* array) +{ + _currentVertexArrayState->setVertexArray(*this, array); +} + +inline void State::disableVertexPointer() +{ + _currentVertexArrayState->disableVertexArray(*this); +} + +inline void State::setNormalPointer(const Array* array) +{ + _currentVertexArrayState->setNormalArray(*this, array); +} + +inline void State::disableNormalPointer() +{ + _currentVertexArrayState->disableNormalArray(*this); +} + + +inline void State::setColorPointer(const Array* array) +{ + _currentVertexArrayState->setColorArray(*this, array); +} + +inline void State::disableColorPointer() +{ + _currentVertexArrayState->disableColorArray(*this); +} + + + +inline void State::setSecondaryColorPointer(const Array* array) +{ + _currentVertexArrayState->setSecondaryColorArray(*this, array); +} + +inline void State::disableSecondaryColorPointer() +{ + _currentVertexArrayState->disableSecondaryColorArray(*this); +} + + +inline void State::setFogCoordPointer(const Array* array) +{ + _currentVertexArrayState->setFogCoordArray(*this, array); +} + + +inline void State::disableFogCoordPointer() +{ + _currentVertexArrayState->disableFogCoordArray(*this); +} + +inline void State::setTexCoordPointer(unsigned int unit, const Array* array) +{ + _currentVertexArrayState->setTexCoordArray(*this, unit, array); +} + +inline void State::disableTexCoordPointer( unsigned int unit ) +{ + _currentVertexArrayState->disableTexCoordArray(*this, unit); +} + +inline void State::disableTexCoordPointersAboveAndIncluding( unsigned int unit ) +{ + _currentVertexArrayState->disableTexCoordArrayAboveAndIncluding(*this, unit); +} + + + // forward declare speciailization of State::get() method template<> inline GLExtensions* State::get() { return _glExtensions.get(); } template<> inline const GLExtensions* State::get() const { return _glExtensions.get(); } diff --git a/include/osg/VertexArrayState b/include/osg/VertexArrayState index 9c61858e3..e77574ad3 100644 --- a/include/osg/VertexArrayState +++ b/include/osg/VertexArrayState @@ -92,17 +92,18 @@ public: void assignAllDispatchers(); - virtual void assignVertexArrayDispatcher(); - virtual void assignNormalArrayDispatcher(); - virtual void assignColorArrayDispatcher(); - virtual void assignSecondaryColorArrayDispatcher(); - virtual void assignFogCoordArrayDispatcher(); - virtual void assignTexCoordArrayDispatcher(unsigned int numUnits); - virtual void assignVertexAttribArrayDispatcher(unsigned int numUnits); + void assignVertexArrayDispatcher(); + void assignNormalArrayDispatcher(); + void assignColorArrayDispatcher(); + void assignSecondaryColorArrayDispatcher(); + void assignFogCoordArrayDispatcher(); + void assignTexCoordArrayDispatcher(unsigned int numUnits); + void assignVertexAttribArrayDispatcher(unsigned int numUnits); inline bool isVertexBufferObjectSupported() const { return true; } void setArray(ArrayDispatch* vad, osg::State& state, const osg::Array* new_array); + void disable(ArrayDispatch* vad, osg::State& state) { vad->disable(state); vad->array=0; vad->modifiedCount=0xffffffff; vad->active=false; } inline void setVertexArray(osg::State& state, const osg::Array* array) { setArray(_vertexArray.get(), state, array); } @@ -122,27 +123,27 @@ public: inline void setTexCoordArray(osg::State& state, unsigned int unit, const osg::Array* array) { setArray(_texCoordArrays[unit].get(), state, array); } inline void disableTexCoordArray(osg::State& state, unsigned int unit) { disable(_texCoordArrays[unit].get(),state); } - void disableTexCoordArrayAboveAndIncluding(osg::State& state, unsigned int index); + inline void disableTexCoordArrayAboveAndIncluding(osg::State& state, unsigned int index); inline void setVertexAttribArray(osg::State& state, unsigned int unit, const osg::Array* array) { setArray(_vertexAttribArrays[unit].get(), state, array); } inline void disableVertexAttribArray(osg::State& state, unsigned int unit) { disable(_vertexAttribArrays[unit].get(), state); } - void disableVertexAttribArrayAboveAndIncluding(osg::State& state, unsigned int index); + inline void disableVertexAttribArrayAboveAndIncluding(osg::State& state, unsigned int index); /** Mark all the vertex attributes as being disabled but leave the disabling till a later call to applyDisablingOfVertexAttributes.*/ - void lazyDisablingOfVertexAttributes(); + inline void lazyDisablingOfVertexAttributes(); /** Disable all the vertex attributes that have been marked as to be disabled.*/ - void applyDisablingOfVertexAttributes(osg::State& state); + inline void applyDisablingOfVertexAttributes(osg::State& state); // Verex Array Object methods. void generateVretexArrayObject(); - void bindVertexArrayObject() const; - - void unbindVertexArrayObject() const; - void deleteVertexArrayObject(); + inline void bindVertexArrayObject() const { _ext->glBindVertexArray (_vertexArrayObject); } + + inline void unbindVertexArrayObject() const { _ext->glBindVertexArray (0); } + GLint getVertexArrayObject() const { return _vertexArrayObject; } @@ -180,6 +181,56 @@ public: }; +inline void VertexArrayState::lazyDisablingOfVertexAttributes() +{ + _activeDispatchers.swap(_previous_activeDispatchers); + _activeDispatchers.clear(); + + for(ActiveDispatchers::iterator itr = _previous_activeDispatchers.begin(); + itr != _previous_activeDispatchers.end(); + ++itr) + { + ArrayDispatch* ad = (*itr); + // ad->array = 0; + ad->active = false; + } +} + +inline void VertexArrayState::applyDisablingOfVertexAttributes(osg::State& state) +{ + for(ActiveDispatchers::iterator itr = _previous_activeDispatchers.begin(); + itr != _previous_activeDispatchers.end(); + ++itr) + { + ArrayDispatch* ad = (*itr); + if (!ad->active) + { + ad->disable(state); + ad->array = 0; + ad->modifiedCount = 0xffffffff; + } + } + _previous_activeDispatchers.clear(); +} + +inline void VertexArrayState::disableTexCoordArrayAboveAndIncluding(osg::State& state, unsigned int index) +{ + for(unsigned int i=index; i<_texCoordArrays.size(); ++i) + { + disable(_texCoordArrays[i].get(), state); + } +} + +inline void VertexArrayState::disableVertexAttribArrayAboveAndIncluding(osg::State& state, unsigned int index) +{ + for(unsigned int i=index; i<_vertexAttribArrays.size(); ++i) + { + disable(_vertexAttribArrays[i].get(), state); + } +} + + + } #endif diff --git a/src/osg/Drawable.cpp b/src/osg/Drawable.cpp index 3407978b1..b2ab85250 100644 --- a/src/osg/Drawable.cpp +++ b/src/osg/Drawable.cpp @@ -624,81 +624,6 @@ void Drawable::compileGLObjects(RenderInfo& renderInfo) const #endif } -void Drawable::draw(RenderInfo& renderInfo) const -{ -// OSG_NOTICE<setRequiresSetArrays(getDataVariance()==osg::Object::DYNAMIC); - } - - State::SetCurrentVertexArrayStateProxy setVASProxy(state, vas); - - vas->bindVertexArrayObject(); - - drawInner(renderInfo); - - // vas->setRequiresSetArrays(getDataVariance()==osg::Object::DYNAMIC); - - return; - } -#endif - - // TODO, add check against whether VOA is active and supported - if (state.getCurrentVertexArrayState()) state.getCurrentVertexArrayState()->bindVertexArrayObject(); - - -#ifdef OSG_GL_DISPLAYLISTS_AVAILABLE - if (_useDisplayList) - { - // get the contextID (user defined ID of 0 upwards) for the - // current OpenGL context. - unsigned int contextID = renderInfo.getContextID(); - - // get the globj for the current contextID. - GLuint& globj = _globjList[contextID]; - - if( globj == 0 ) - { - // compile the display list - globj = generateDisplayList(contextID, getGLObjectSizeHint()); - glNewList( globj, GL_COMPILE ); - - drawInner(renderInfo); - - glEndList(); - } - - // call the display list - glCallList( globj); - } - else -#endif - { - // if state.previousVertexArrayState() is different than currentVertexArrayState bind current - - // OSG_NOTICE<<"Fallback drawInner()........................"<bindVertexArrayObject(); - drawVertexArraysImplementation(renderInfo); } } diff --git a/src/osg/State.cpp b/src/osg/State.cpp index cdee4c49b..970d52ac6 100644 --- a/src/osg/State.cpp +++ b/src/osg/State.cpp @@ -1050,16 +1050,6 @@ void State::disableAllVertexArrays() disableVertexAttribPointersAboveAndIncluding(0); } -void State::dirtyAllVertexArrays() -{ - dirtyVertexPointer(); - dirtyColorPointer(); - dirtyFogCoordPointer(); - dirtyNormalPointer(); - dirtySecondaryColorPointer(); - dirtyTexCoordPointersAboveAndIncluding(0); - dirtyVertexAttribPointersAboveAndIncluding(0); -} void State::setInterleavedArrays( GLenum format, GLsizei stride, const GLvoid* pointer) { @@ -1078,6 +1068,8 @@ void State::setInterleavedArrays( GLenum format, GLsizei stride, const GLvoid* p #if USE_VERTEXARRAYSTATE + +#if 0 ///////////////////////////////////////////////////////////////////////// // // New VertexArrayState version @@ -1092,11 +1084,6 @@ void State::disableVertexPointer() _currentVertexArrayState->disableVertexArray(*this); } -void State::dirtyVertexPointer() -{ - OSG_NOTICE<<"NOT IMPLEMENTED YET, dirtyVertexPointer() "<<__LINE__<setNormalArray(*this, array); @@ -1107,11 +1094,6 @@ void State::disableNormalPointer() _currentVertexArrayState->disableNormalArray(*this); } -void State::dirtyNormalPointer() -{ - OSG_NOTICE<<"NOT IMPLEMENTED YET, dirtyNormalPointer() "<<__LINE__<disableColorArray(*this); } -void State::dirtyColorPointer() -{ - OSG_NOTICE<<"NOT IMPLEMENTED YET, dirtyColorPointer() "<<__LINE__<setSecondaryColorArray(*this, array); @@ -1145,31 +1117,18 @@ void State::disableSecondaryColorPointer() _currentVertexArrayState->disableSecondaryColorArray(*this); } -void State::dirtySecondaryColorPointer() -{ - OSG_NOTICE<<"NOT IMPLEMENTED YET, dirtySecondaryColorPointer() "<<__LINE__<setFogCoordArray(*this, array); } -void State::setFogCoordPointer(GLenum type, GLsizei stride, const GLvoid *ptr, GLboolean normalized) -{ - OSG_NOTICE<<"NOT IMPLEMENTED YET, setFogCoordPointer "<<__LINE__<disableFogCoordArray(*this); } -void State::dirtyFogCoordPointer() -{ - OSG_NOTICE<<"NOT IMPLEMENTED YET, dirtyFogCoordPointer() "<<__LINE__<setTexCoordArray(*this, unit, array); @@ -1184,12 +1143,75 @@ void State::disableTexCoordPointersAboveAndIncluding( unsigned int unit ) { _currentVertexArrayState->disableTexCoordArrayAboveAndIncluding(*this, unit); } +#endif + +#if 0 +void State::dirtyVertexPointer() +{ + OSG_NOTICE<<"NOT IMPLEMENTED YET, dirtyVertexPointer() "<<__LINE__<disableVertexAttribArray(*this, index); @@ -1267,10 +1284,6 @@ void State::disableVertexAttribPointersAboveAndIncluding( unsigned int index ) _currentVertexArrayState->disableVertexAttribArrayAboveAndIncluding(*this, index); } -void State::dirtyVertexAttribPointer( unsigned int index ) -{ - OSG_NOTICE<<"NOT IMPLEMENTED YET, dirtyVertexAttribPointer() "<<__LINE__<glGenVertexArrays(1, &_vertexArrayObject); } -void VertexArrayState::bindVertexArrayObject() const -{ - VAS_NOTICE<<"glBindVertexArray() _vertexArrayObject="<<_vertexArrayObject<glBindVertexArray (_vertexArrayObject); -} - -void VertexArrayState::unbindVertexArrayObject() const -{ - _ext->glBindVertexArray (0); -} - void VertexArrayState::deleteVertexArrayObject() { if (_vertexArrayObject) @@ -577,60 +565,24 @@ void VertexArrayState::assignAllDispatchers() assignVertexAttribArrayDispatcher(numVertexAttrib); } -void VertexArrayState::lazyDisablingOfVertexAttributes() +void VertexArrayState::release() { - VAS_NOTICE<<" VertexArrayState<"<::lazyDisablingOfVertexAttributes() _activeDispatchers.size()="<<_activeDispatchers.size()<<", _previous_activeDispatchers.size()="<<_previous_activeDispatchers.size()<array = 0; - ad->active = false; - } -} - -void VertexArrayState::applyDisablingOfVertexAttributes(osg::State& state) -{ - VAS_NOTICE<<" VertexArrayState<"<::applyDisablingOfVertexAttributes() _activeDispatchers.size()="<<_activeDispatchers.size()<<", _previous_activeDispatchers.size()="<<_previous_activeDispatchers.size()<active) - { - ad->disable(state); - ad->array = 0; - ad->modifiedCount = 0xffffffff; - } - } - _previous_activeDispatchers.clear(); + osg::get(_ext->contextID)->release(this); } void VertexArrayState::setArray(ArrayDispatch* vad, osg::State& state, const osg::Array* new_array) { if (new_array) { - VAS_NOTICE<<" VertexArrayState<"<::setArray() "<array = new_array; vad->modifiedCount = new_array->getModifiedCount(); @@ -677,31 +617,6 @@ void VertexArrayState::setArray(ArrayDispatch* vad, osg::State& state, const osg } else if (vad->array) { - VAS_NOTICE<<" VertexArrayState::setArray() need to disable "<(_ext->contextID)->release(this); -}