From ffbc1167de4d3761a4c7574add76db50e3098901 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 13 Mar 2017 11:44:34 +0000 Subject: [PATCH] Added lazy state updating for vertex array object binding/unbinding --- include/osg/State | 14 +++++++++++++- include/osg/VertexArrayState | 4 ---- src/osg/Drawable.cpp | 8 ++++++-- src/osg/Geometry.cpp | 4 +++- src/osg/State.cpp | 1 + src/osgTerrain/GeometryPool.cpp | 4 +++- 6 files changed, 26 insertions(+), 9 deletions(-) diff --git a/include/osg/State b/include/osg/State index 4560bccf6..b79420dfd 100644 --- a/include/osg/State +++ b/include/osg/State @@ -563,7 +563,7 @@ class OSG_EXPORT State : public Referenced void setCurrentPixelBufferObject(osg::GLBufferObject* pbo) { _currentPBO = pbo; } - const GLBufferObject* getCurrentPixelBufferObject() { return _currentPBO; } + const GLBufferObject* getCurrentPixelBufferObject() const { return _currentPBO; } inline void bindPixelBufferObject(osg::GLBufferObject* pbo) { @@ -590,6 +590,17 @@ class OSG_EXPORT State : public Referenced _currentPBO = 0; } + + void setCurrentVertexArrayObject(GLuint vao) { _currentVAO = vao; } + GLuint getCurrentVertexArrayObject() const { return _currentVAO; } + + inline void bindVertexArrayObject(const VertexArrayState* vas) { bindVertexArrayObject(vas->getVertexArrayObject()); } + + inline void bindVertexArrayObject(GLuint vao) { if (_currentVAO!=vao) { _glExtensions->glBindVertexArray(vao); _currentVAO = vao; } } + + inline void unbindVertexArrayObject() { if (_currentVAO!=0) { _glExtensions->glBindVertexArray(0); _currentVAO = 0; } } + + typedef std::vector IndicesGLushort; IndicesGLushort _quadIndicesGLushort[4]; @@ -1248,6 +1259,7 @@ class OSG_EXPORT State : public Referenced unsigned int _currentActiveTextureUnit; unsigned int _currentClientActiveTextureUnit; GLBufferObject* _currentPBO; + GLuint _currentVAO; inline ModeMap& getOrCreateTextureModeMap(unsigned int unit) diff --git a/include/osg/VertexArrayState b/include/osg/VertexArrayState index d7f99c269..cfd8dae86 100644 --- a/include/osg/VertexArrayState +++ b/include/osg/VertexArrayState @@ -164,10 +164,6 @@ public: void deleteVertexArrayObject(); - inline void bindVertexArrayObject() const { _ext->glBindVertexArray (_vertexArrayObject); } - - inline void unbindVertexArrayObject() const { _ext->glBindVertexArray (0); } - GLuint getVertexArrayObject() const { return _vertexArrayObject; } diff --git a/src/osg/Drawable.cpp b/src/osg/Drawable.cpp index 3f8d381ae..857e4546d 100644 --- a/src/osg/Drawable.cpp +++ b/src/osg/Drawable.cpp @@ -642,7 +642,7 @@ void Drawable::draw(RenderInfo& renderInfo) const State::SetCurrentVertexArrayStateProxy setVASProxy(state, vas); - vas->bindVertexArrayObject(); + state.bindVertexArrayObject(vas); drawInner(renderInfo); @@ -652,7 +652,11 @@ void Drawable::draw(RenderInfo& renderInfo) const } // TODO, add check against whether VAO is active and supported - if (state.getCurrentVertexArrayState()) state.getCurrentVertexArrayState()->bindVertexArrayObject(); + if (state.getCurrentVertexArrayState()) + { + //OSG_NOTICE<<"state.getCurrentVertexArrayState()->getVertexArrayObject()="<< state.getCurrentVertexArrayState()->getVertexArrayObject()<bindVertexArrayObject(); + state.bindVertexArrayObject(vas); drawVertexArraysImplementation(renderInfo); + + state.unbindVertexArrayObject(); } } else diff --git a/src/osg/State.cpp b/src/osg/State.cpp index 3162d6561..43eed6cb6 100644 --- a/src/osg/State.cpp +++ b/src/osg/State.cpp @@ -89,6 +89,7 @@ State::State(): _currentClientActiveTextureUnit=0; _currentPBO = 0; + _currentVAO = 0; _isSecondaryColorSupported = false; _isFogCoordSupported = false; diff --git a/src/osgTerrain/GeometryPool.cpp b/src/osgTerrain/GeometryPool.cpp index 905d156aa..c7fb3c5f8 100644 --- a/src/osgTerrain/GeometryPool.cpp +++ b/src/osgTerrain/GeometryPool.cpp @@ -861,10 +861,12 @@ void SharedGeometry::compileGLObjects(osg::RenderInfo& renderInfo) const osg::State::SetCurrentVertexArrayStateProxy setVASProxy(state, vas); - vas->bindVertexArrayObject(); + state.bindVertexArrayObject(vas); if (vbo_glBufferObject) vas->bindVertexBufferObject(vbo_glBufferObject); if (ebo_glBufferObject) vas->bindElementBufferObject(ebo_glBufferObject); + + state.unbindVertexArrayObject(); } }