diff --git a/include/osg/State b/include/osg/State index b79420dfd..28a2880c4 100644 --- a/include/osg/State +++ b/include/osg/State @@ -591,6 +591,27 @@ class OSG_EXPORT State : public Referenced } + inline void bindDrawIndirectBufferObject(osg::GLBufferObject* ibo) + { + if (ibo->isDirty()) + { + ibo->compileBuffer(); + _currentDIBO = ibo; + } + else if (ibo != _currentDIBO) + { + _glBindBuffer(GL_DRAW_INDIRECT_BUFFER, ibo->getGLObjectID()); + _currentDIBO = ibo; + } + } + + inline void unbindDrawIndirectBufferObject() + { + if (!_currentDIBO) return; + _glBindBuffer(GL_DRAW_INDIRECT_BUFFER,0); + _currentDIBO = 0; + } + void setCurrentVertexArrayObject(GLuint vao) { _currentVAO = vao; } GLuint getCurrentVertexArrayObject() const { return _currentVAO; } @@ -1259,6 +1280,7 @@ class OSG_EXPORT State : public Referenced unsigned int _currentActiveTextureUnit; unsigned int _currentClientActiveTextureUnit; GLBufferObject* _currentPBO; + GLBufferObject* _currentDIBO; GLuint _currentVAO; diff --git a/src/osg/PrimitiveSetIndirect.cpp b/src/osg/PrimitiveSetIndirect.cpp index add1c98a4..796769392 100644 --- a/src/osg/PrimitiveSetIndirect.cpp +++ b/src/osg/PrimitiveSetIndirect.cpp @@ -64,7 +64,7 @@ unsigned int DrawElementsIndirectUShort::getNumPrimitives() const{return getNumP void DrawElementsIndirectUInt::draw(State& state, bool useVertexBufferObjects) const { GLBufferObject* dibo = _indirectCommandArray->getBufferObject()->getOrCreateGLBufferObject( state.getContextID() ); - state.get()->glBindBuffer(GL_DRAW_INDIRECT_BUFFER, dibo->getGLObjectID()); + state.bindDrawIndirectBufferObject(dibo); GLenum mode = _mode; #if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE) @@ -122,7 +122,7 @@ void DrawElementsIndirectUInt::accept(PrimitiveIndexFunctor& functor) const } void DrawElementsIndirectUByte::draw(State& state, bool useVertexBufferObjects) const { GLBufferObject* dibo = _indirectCommandArray->getBufferObject()->getOrCreateGLBufferObject( state.getContextID() ); - state.get()->glBindBuffer(GL_DRAW_INDIRECT_BUFFER, dibo->getGLObjectID()); + state.bindDrawIndirectBufferObject(dibo); GLenum mode = _mode; #if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE) @@ -176,7 +176,7 @@ void DrawElementsIndirectUByte::accept(PrimitiveIndexFunctor& functor) const } void DrawElementsIndirectUShort::draw(State& state, bool useVertexBufferObjects) const { GLBufferObject* dibo = _indirectCommandArray->getBufferObject()->getOrCreateGLBufferObject( state.getContextID() ); - state.get()->glBindBuffer(GL_DRAW_INDIRECT_BUFFER, dibo->getGLObjectID()); + state.bindDrawIndirectBufferObject(dibo); GLenum mode = _mode; #if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE) @@ -282,7 +282,7 @@ void MultiDrawElementsIndirectUByte::draw(State& state, bool useVertexBufferObje { GLBufferObject* dibo = _indirectCommandArray->getBufferObject()->getOrCreateGLBufferObject( state.getContextID() ); - state.get()->glBindBuffer(GL_DRAW_INDIRECT_BUFFER, dibo->getGLObjectID()); + state.bindDrawIndirectBufferObject(dibo); GLenum mode = _mode; #if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE) if (mode==GL_POLYGON) mode = GL_TRIANGLE_FAN; @@ -340,7 +340,7 @@ MultiDrawElementsIndirectUShort::~MultiDrawElementsIndirectUShort() void MultiDrawElementsIndirectUShort::draw(State& state, bool useVertexBufferObjects) const { GLBufferObject* dibo = _indirectCommandArray->getBufferObject()->getOrCreateGLBufferObject( state.getContextID() ); - state.get()->glBindBuffer(GL_DRAW_INDIRECT_BUFFER, dibo->getGLObjectID()); + state.bindDrawIndirectBufferObject(dibo); GLenum mode = _mode; #if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE) @@ -397,7 +397,7 @@ MultiDrawElementsIndirectUInt::~MultiDrawElementsIndirectUInt() void MultiDrawElementsIndirectUInt::draw(State& state, bool useVertexBufferObjects) const { GLBufferObject* dibo = _indirectCommandArray->getBufferObject()->getOrCreateGLBufferObject( state.getContextID() ); - state.get()->glBindBuffer(GL_DRAW_INDIRECT_BUFFER, dibo->getGLObjectID()); + state.bindDrawIndirectBufferObject(dibo); GLenum mode = _mode; #if defined(OSG_GLES1_AVAILABLE) || defined(OSG_GLES2_AVAILABLE) if (mode==GL_POLYGON) mode = GL_TRIANGLE_FAN; @@ -450,7 +450,7 @@ void MultiDrawElementsIndirectUInt::accept(PrimitiveIndexFunctor& functor) const void DrawArraysIndirect::draw(osg::State& state, bool) const { GLBufferObject* dibo = _indirectCommandArray->getBufferObject()->getOrCreateGLBufferObject( state.getContextID() ); - state.get()->glBindBuffer(GL_DRAW_INDIRECT_BUFFER, dibo->getGLObjectID()); + state.bindDrawIndirectBufferObject(dibo); GLExtensions* ext = state.get(); @@ -517,7 +517,7 @@ unsigned int DrawArraysIndirect::getNumPrimitives() const void MultiDrawArraysIndirect::draw(osg::State& state, bool) const { GLBufferObject* dibo = _indirectCommandArray->getBufferObject()->getOrCreateGLBufferObject( state.getContextID() ); - state.get()->glBindBuffer(GL_DRAW_INDIRECT_BUFFER, dibo->getGLObjectID()); + state.bindDrawIndirectBufferObject(dibo); GLExtensions* ext = state.get(); diff --git a/src/osg/State.cpp b/src/osg/State.cpp index e8c2446ca..c652d4cc6 100644 --- a/src/osg/State.cpp +++ b/src/osg/State.cpp @@ -89,6 +89,7 @@ State::State(): _currentClientActiveTextureUnit=0; _currentPBO = 0; + _currentDIBO = 0; _currentVAO = 0; _isSecondaryColorSupported = false;