Added ability to initializer GL vertex array object id with Geometry::compileGLObjects().

Improved the handling of buffer object state when not using VAO's
This commit is contained in:
Robert Osfield
2016-07-18 20:09:22 +01:00
parent 14d1483b06
commit 4d8a29b987
4 changed files with 124 additions and 45 deletions

View File

@@ -238,6 +238,8 @@ class OSG_EXPORT Geometry : public Drawable
bool _containsDeprecatedData;
virtual VertexArrayState* setUpVertexArrayState(RenderInfo& renderInfo, bool usingVBOs) const;
public:

View File

@@ -155,7 +155,7 @@ public:
void setCurrentVertexBufferObject(osg::GLBufferObject* vbo) { _currentVBO = vbo; }
const GLBufferObject* getCurrentVertexBufferObject() { return _currentVBO; }
GLBufferObject* getCurrentVertexBufferObject() { return _currentVBO; }
inline void bindVertexBufferObject(osg::GLBufferObject* vbo)
{
if (vbo)
@@ -177,13 +177,13 @@ public:
void setCurrentElementBufferObject(osg::GLBufferObject* ebo) { _currentEBO = ebo; }
const GLBufferObject* getCurrentElementBufferObject() { return _currentEBO; }
GLBufferObject* getCurrentElementBufferObject() { return _currentEBO; }
inline void bindElementBufferObject(osg::GLBufferObject* ebo)
{
if (ebo)
{
if (ebo == _currentEBO) return;
//if (ebo == _currentEBO) return;
if (ebo->isDirty()) ebo->compileBuffer();
else ebo->bindBuffer();
_currentEBO = ebo;
@@ -193,7 +193,7 @@ public:
inline void unbindElementBufferObject()
{
if (!_currentEBO) return;
//if (!_currentEBO) return;
_ext->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB,0);
_currentEBO = 0;
}

View File

@@ -23,9 +23,14 @@ using namespace osg;
Geometry::Geometry():
_containsDeprecatedData(false)
{
#if 0
_supportsVertexBufferObjects = true;
// temporary test
// setSupportsDisplayList(false);
#else
_supportsVertexBufferObjects = true;
_useVertexBufferObjects = true;
#endif
}
Geometry::Geometry(const Geometry& geometry,const CopyOp& copyop):
@@ -660,14 +665,56 @@ void Geometry::releaseGLObjects(State* state) const
}
VertexArrayState* Geometry::setUpVertexArrayState(RenderInfo& renderInfo, bool usingVBOs) const
{
OSG_NOTICE<<"Creating new osg::VertexArrayState"<<std::endl;
State& state = *renderInfo.getState();
const DisplaySettings* ds = state.getDisplaySettings() ? state.getDisplaySettings() : osg::DisplaySettings::instance();
VertexArrayState* vas = 0;
_vertexArrayStateList[state.getContextID()] = vas = new osg::VertexArrayState(state.get<GLExtensions>());
if (_vertexArray.valid()) vas->assignVertexArray(_vertexArray.get());
if (_colorArray.valid()) vas->assignColorArray(_colorArray.get());
if (_normalArray.valid()) vas->assignNormalArray(_normalArray.get());
if (_secondaryColorArray.valid()) vas->assignSecondaryColorArray(_secondaryColorArray.get());
if (_fogCoordArray.valid()) vas->assignFogCoordArray(_fogCoordArray.get());
for(unsigned int i=0; i<_texCoordList.size(); ++i)
{
if (_texCoordList[i].valid()) vas->assignTexCoordArray(i, _texCoordList[i].get());
}
for(unsigned int i=0; i<_vertexAttribList.size(); ++i)
{
if (_vertexAttribList[i].valid()) vas->assignVertexAttribArray(i, _vertexAttribList[i].get());
}
if (usingVBOs && ds->getGeometryImplementation()==DisplaySettings::VERTEX_ARRAY_OBJECT)
{
OSG_NOTICE<<" Setup VertexArrayState to use VAO"<<std::endl;
vas->generateVretexArrayObject();
}
else
{
OSG_NOTICE<<" Setup VertexArrayState to without using VAO"<<std::endl;
}
return vas;
}
void Geometry::compileGLObjects(RenderInfo& renderInfo) const
{
State& state = *renderInfo.getState();
const DisplaySettings* ds = state.getDisplaySettings() ? state.getDisplaySettings() : osg::DisplaySettings::instance();
bool useVertexArrays = _supportsVertexBufferObjects &&
_useVertexBufferObjects &&
renderInfo.getState()->isVertexBufferObjectSupported();
if (useVertexArrays)
{
State& state = *renderInfo.getState();
unsigned int contextID = state.getContextID();
GLExtensions* extensions = state.get<GLExtensions>();
if (!extensions) return;
@@ -724,9 +771,18 @@ void Geometry::compileGLObjects(RenderInfo& renderInfo) const
extensions->glBindBuffer(GL_ARRAY_BUFFER_ARB,0);
extensions->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB,0);
if (ds->getGeometryImplementation()!=DisplaySettings::OLD_GEOMETRY_IMPLEMENTATION)
{
setUpVertexArrayState(renderInfo, !bufferObjects.empty());
}
}
else
{
if (ds->getGeometryImplementation()!=DisplaySettings::OLD_GEOMETRY_IMPLEMENTATION)
{
setUpVertexArrayState(renderInfo, false);
}
Drawable::compileGLObjects(renderInfo);
}
}
@@ -879,12 +935,16 @@ void Geometry::new_drawImplementation(RenderInfo& renderInfo) const
unsigned int contextID = state.getContextID();
bool usingVertexBufferObjects = _useVertexBufferObjects && state.isVertexBufferObjectSupported();
bool usingVertexBufferObjects = _supportsVertexBufferObjects && _useVertexBufferObjects && state.isVertexBufferObjectSupported();
bool useVAO = usingVertexBufferObjects && (ds->getGeometryImplementation()==DisplaySettings::VERTEX_ARRAY_OBJECT);
bool dispatchIfDirty = useVAO;
VertexArrayState* vas = _vertexArrayStateList[contextID].get();
OSG_NOTICE<<"Geometry::new_drawImplementation() "<<this<<" _vertexArray.get()="<<_vertexArray.get()<<std::endl;
bool checkForGLErrors = state.getCheckForGLErrors()==osg::State::ONCE_PER_ATTRIBUTE;
if (checkForGLErrors) state.checkGLErrors("Geometry::new_drawImplementation() before vertex arrays setup.");
@@ -896,37 +956,7 @@ void Geometry::new_drawImplementation(RenderInfo& renderInfo) const
if (!vas)
{
OSG_NOTICE<<"Creating new osg::VertexArrayState"<<std::endl;
_vertexArrayStateList[contextID] = vas = new osg::VertexArrayState(state.get<GLExtensions>());
if (_vertexArray.valid()) vas->assignVertexArray(_vertexArray.get());
if (_colorArray.valid()) vas->assignColorArray(_colorArray.get());
if (_normalArray.valid()) vas->assignNormalArray(_normalArray.get());
if (_secondaryColorArray.valid()) vas->assignSecondaryColorArray(_secondaryColorArray.get());
if (_fogCoordArray.valid()) vas->assignFogCoordArray(_fogCoordArray.get());
for(unsigned int i=0; i<_texCoordList.size(); ++i)
{
if (_texCoordList[i].valid()) vas->assignTexCoordArray(i, _texCoordList[i].get());
}
for(unsigned int i=0; i<_vertexAttribList.size(); ++i)
{
if (_vertexAttribList[i].valid()) vas->assignVertexAttribArray(i, _vertexAttribList[i].get());
}
if (useVAO)
{
OSG_NOTICE<<" Setup VertexArrayState to use VAO"<<std::endl;
vas->generateVretexArrayObject();
dispatchIfDirty = false;
}
else
{
OSG_NOTICE<<" Setup VertexArrayState to wihtout using VAO"<<std::endl;
}
vas = setUpVertexArrayState(renderInfo, usingVertexBufferObjects);
}
if (useVAO)
@@ -973,6 +1003,18 @@ void Geometry::new_drawImplementation(RenderInfo& renderInfo) const
state.unbindElementBufferObject();
}
if (useVAO)
{
vas->unbindVertexArrayObject();
state.setCurrentVertexBufferObject(vas->getCurrentVertexBufferObject());
state.setCurrentElementBufferObject(vas->getCurrentElementBufferObject());
// unbind the VBO's if any are used.
state.unbindVertexBufferObject();
state.unbindElementBufferObject();
}
if (checkForGLErrors) state.checkGLErrors("Geometry::new_drawImplementation() after primitive dispatch.");
}

View File

@@ -78,12 +78,17 @@ struct VertexArrayWithVBODispatch : public VertexArrayState::ArrayDispatch
{
VertexArrayWithVBODispatch(Array* in_array, GLBufferObject* in_vbo) : ArrayDispatch(in_array), vbo(in_vbo) {}
virtual void dispatch(osg::State&, unsigned int)
virtual void dispatch(osg::State& state, unsigned int)
{
OSG_INFO<<"VertexArrayWithVBODispatch::dispatch()"<<std::endl;
OSG_NOTICE<<"VertexArrayWithVBODispatch::dispatch() "<<array->getNumElements()<<", "<<array->getDataPointer()<<std::endl;
#if 1
state.getCurrentVertexArrayState()->bindVertexBufferObject(vbo);
#else
if (vbo->isDirty()) vbo->compileBuffer();
else vbo->bindBuffer();
#endif
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(array->getDataSize(), array->getDataType(), 0, (const GLvoid *)(vbo->getOffset(array->getBufferIndex())));
@@ -93,7 +98,7 @@ struct VertexArrayWithVBODispatch : public VertexArrayState::ArrayDispatch
virtual void dispatchDeprecated(osg::State& state, unsigned int)
{
OSG_INFO<<"VertexArrayDispatchWitVBO::dispatchDeprecated() "<<array->getNumElements()<<", "<<array->getDataPointer()<<std::endl;
OSG_NOTICE<<"VertexArrayWithVBODispatch::dispatchDeprecated() "<<array->getNumElements()<<", "<<array->getDataPointer()<<std::endl;
state.setVertexPointer(array);
@@ -208,13 +213,15 @@ struct ColorArrayWithVBODispatch : public VertexArrayState::ArrayDispatch
{
ColorArrayWithVBODispatch(Array* in_array, GLBufferObject* in_vbo) : ArrayDispatch(in_array), vbo(in_vbo) {}
virtual void dispatch(osg::State&, unsigned int)
virtual void dispatch(osg::State& state, unsigned int)
{
// OSG_INFO<<"ColorArrayWithVBODispatch::dispatch()"<<std::endl;
#if 1
state.getCurrentVertexArrayState()->bindVertexBufferObject(vbo);
#else
if (vbo->isDirty()) vbo->compileBuffer();
else vbo->bindBuffer();
#endif
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(array->getDataSize(), array->getDataType(), 0, (const GLvoid *)(vbo->getOffset(array->getBufferIndex())));
@@ -337,12 +344,16 @@ struct NormalArrayWithVBODispatch : public VertexArrayState::ArrayDispatch
{
NormalArrayWithVBODispatch(Array* in_array, GLBufferObject* in_vbo) : ArrayDispatch(in_array), vbo(in_vbo) {}
virtual void dispatch(osg::State&, unsigned int)
virtual void dispatch(osg::State& state, unsigned int)
{
OSG_INFO<<"NormalArrayWithVBODispatch::dispatch()"<<std::endl;
#if 1
state.getCurrentVertexArrayState()->bindVertexBufferObject(vbo);
#else
if (vbo->isDirty()) vbo->compileBuffer();
else vbo->bindBuffer();
#endif
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(array->getDataType(), 0, (const GLvoid *)(vbo->getOffset(array->getBufferIndex())));
@@ -445,8 +456,12 @@ struct SecondaryColorArrayWithVBODispatch : public VertexArrayState::ArrayDispat
{
OSG_INFO<<"SecondaryColorArrayWithVBODispatch::dispatch()"<<std::endl;
#if 1
state.getCurrentVertexArrayState()->bindVertexBufferObject(vbo);
#else
if (vbo->isDirty()) vbo->compileBuffer();
else vbo->bindBuffer();
#endif
glEnableClientState(GL_FOG_COORDINATE_ARRAY);
state.get<GLExtensions>()->glSecondaryColorPointer(array->getDataSize(), array->getDataType(), 0, (const GLvoid *)(vbo->getOffset(array->getBufferIndex())));
@@ -555,8 +570,12 @@ struct FogCoordArrayWithVBODispatch : public VertexArrayState::ArrayDispatch
{
OSG_INFO<<"FogCoordArrayWithVBODispatch::dispatch()"<<std::endl;
#if 1
state.getCurrentVertexArrayState()->bindVertexBufferObject(vbo);
#else
if (vbo->isDirty()) vbo->compileBuffer();
else vbo->bindBuffer();
#endif
glEnableClientState(GL_FOG_COORDINATE_ARRAY);
state.get<GLExtensions>()->glFogCoordPointer(array->getDataType(), 0, (const GLvoid *)(vbo->getOffset(array->getBufferIndex())));
@@ -665,12 +684,16 @@ struct TexCoordArrayWithVBODispatch : public VertexArrayState::ArrayDispatch
{
TexCoordArrayWithVBODispatch(unsigned int in_unit, Array* in_array, GLBufferObject* in_vbo) : ArrayDispatch(in_array), unit(in_unit), vbo(in_vbo) {}
virtual void dispatch(osg::State&, unsigned int)
virtual void dispatch(osg::State& state, unsigned int)
{
OSG_INFO<<"TexCoordArrayWithVBODispatch::dispatch()"<<std::endl;
#if 1
state.getCurrentVertexArrayState()->bindVertexBufferObject(vbo);
#else
if (vbo->isDirty()) vbo->compileBuffer();
else vbo->bindBuffer();
#endif
glClientActiveTexture(static_cast<GLenum>(GL_TEXTURE0+unit));
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
@@ -805,8 +828,12 @@ struct VertexAttribArrayWithVBODispatch : public VertexArrayState::ArrayDispatch
virtual void dispatch(osg::State& state, unsigned int)
{
#if 1
state.getCurrentVertexArrayState()->bindVertexBufferObject(vbo);
#else
if (vbo->isDirty()) vbo->compileBuffer();
else vbo->bindBuffer();
#endif
GLExtensions* ext = state.get<GLExtensions>();
ext->glEnableVertexAttribArray( unit );
@@ -856,8 +883,12 @@ struct VertexAttribLArrayWithVBODispatch : public VertexArrayState::ArrayDispatc
virtual void dispatch(osg::State& state, unsigned int)
{
#if 1
state.getCurrentVertexArrayState()->bindVertexBufferObject(vbo);
#else
if (vbo->isDirty()) vbo->compileBuffer();
else vbo->bindBuffer();
#endif
GLExtensions* ext = state.get<GLExtensions>();
ext->glEnableVertexAttribArray( unit );
@@ -907,8 +938,12 @@ struct VertexAttribIArrayWithVBODispatch : public VertexArrayState::ArrayDispatc
virtual void dispatch(osg::State& state, unsigned int)
{
#if 1
state.getCurrentVertexArrayState()->bindVertexBufferObject(vbo);
#else
if (vbo->isDirty()) vbo->compileBuffer();
else vbo->bindBuffer();
#endif
GLExtensions* ext = state.get<GLExtensions>();
ext->glEnableVertexAttribArray( unit );