From 2c942f4771bf5fc8ef29046c65bac4077cee3a94 Mon Sep 17 00:00:00 2001 From: mp3butcher Date: Tue, 30 Jan 2018 17:52:27 +0100 Subject: [PATCH] persistance mapping proposal --- include/osg/BufferObject | 6 ++- src/osg/BufferObject.cpp | 106 +++++++++++++++------------------------ 2 files changed, 45 insertions(+), 67 deletions(-) diff --git a/include/osg/BufferObject b/include/osg/BufferObject index 45d17a2aa..8fe891256 100644 --- a/include/osg/BufferObject +++ b/include/osg/BufferObject @@ -98,7 +98,6 @@ class BufferObject; class BufferObjectProfile { public: - BufferObjectProfile(): _target(0), _usage(0), @@ -240,6 +239,10 @@ class OSG_EXPORT GLBufferObject : public GraphicsObject void setBufferDataHasBeenRead(const osg::BufferData* bd); + void downloadBuffer(unsigned int entryidx); + + void commitDMA(unsigned int entryidx); + protected: virtual ~GLBufferObject(); @@ -588,7 +591,6 @@ class OSG_EXPORT BufferData : public Object void addClient(osg::Object * /*client*/) { ++_numClients; } void removeClient(osg::Object * /*client*/) { --_numClients; } - protected: virtual ~BufferData(); diff --git a/src/osg/BufferObject.cpp b/src/osg/BufferObject.cpp index b5a9d2e78..8fea6fc43 100644 --- a/src/osg/BufferObject.cpp +++ b/src/osg/BufferObject.cpp @@ -1,6 +1,6 @@ /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield * Copyright (C) 2012 David Callu - * Copyright (C) 2018 Julien Valentin + * Copyright (C) 2019 Julien Valentin * * This library is open source and may be redistributed and/or modified under * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or @@ -122,7 +122,7 @@ void GLBufferObject::clear() void GLBufferObject::compileBuffer() { _dirty = false; -_persistantDMA=0; + _bufferEntries.reserve(_bufferObject->getNumBufferData()); bool compileAll = false; @@ -214,30 +214,20 @@ _persistantDMA=0; _allocatedSize = _profile._size; OSG_INFO<<" Allocating new glBufferData(), _allocatedSize="<<_allocatedSize<dataSource->asArray(); - if(array)_profile._mappingbitfield= GL_MAP_PERSISTENT_BIT| GL_MAP_WRITE_BIT| GL_MAP_COHERENT_BIT; //DEBUG if(_profile._mappingbitfield != 0) { _extensions->glBufferStorage(_profile._target, _profile._size, NULL, _profile._mappingbitfield); - if(_profile._mappingbitfield & GL_MAP_PERSISTENT_BIT ){ - ///debug invalidate mapping - if(_persistantDMA){ - _extensions->glUnmapBuffer(_profile._target); - _persistantDMA=0; + if(_profile._mappingbitfield & GL_MAP_PERSISTENT_BIT ) + { + /// invalidate mapping of previously allocated + if(_persistantDMA) + { + _extensions->glUnmapBuffer(_profile._target); + _persistantDMA = 0; } - _persistantDMA = _extensions->glMapBufferRange( - _profile._target, - 0, - _profile._size, - _profile._mappingbitfield |GL_MAP_INVALIDATE_RANGE_BIT|GL_MAP_FLUSH_EXPLICIT_BIT - ); - GLenum err=glGetError(); - if(err!=GL_NO_ERROR){ - OSG_WARN<<" error"<glMapBufferRange( _profile._target, 0, _profile._size, _profile._mappingbitfield ); + } } else @@ -272,54 +262,22 @@ _persistantDMA=0; { if(_profile._mappingbitfield != 0 ) { -///GL_DYNAMIC_STORAGE_BIT, GL_MAP_READ_BIT GL_MAP_WRITE_BIT, GL_MAP_PERSISTENT_BIT, GL_MAP_COHERENT_BIT, and GL_CLIENT_STORAGE_BIT. if(_profile._mappingbitfield & GL_MAP_PERSISTENT_BIT) { - ///TODO AVOID dirty - if(_persistantDMA){ - GLvoid* src=const_cast(entry.dataSource->getDataPointer()); - - if( src)memcpy((unsigned char*)_persistantDMA+entry.offset,src,entry.dataSize); + if(_persistantDMA) + { + GLvoid* src = const_cast(entry.dataSource->getDataPointer()); + memcpy((unsigned char*)_persistantDMA + entry.offset, src, entry.dataSize); + _extensions->glFlushMappedBufferRange(_profile._target, (GLintptr)entry.offset, (GLsizeiptr)entry.dataSize ); + } else OSG_WARN<<" GL_MAP_PERSISTENT_BIT problem"<glFlushMappedBufferRange(_profile._target, - (GLintptr)entry.offset, - (GLsizeiptr)entry.dataSize - ); - GLenum err=glGetError(); - if(err!=GL_NO_ERROR){ - OSG_WARN<<" error"<glUnmapBuffer(_profile._target); } - else - if(_profile._mappingbitfield & GL_MAP_WRITE_BIT) - { ///GL_MAP_WRITE_BIT classic upload - GLvoid* src=const_cast(entry.dataSource->getDataPointer()), - *dst = _extensions->glMapBufferRange( - _profile._target, - (GLintptr)entry.offset, - (GLsizeiptr)entry.dataSize, - _profile._mappingbitfield - ); - memcpy(dst,src,entry.dataSize); - _extensions->glUnmapBuffer(_profile._target); - - } - else + else if(_profile._mappingbitfield & GL_MAP_WRITE_BIT) { - /// Readback buffer - GLvoid* dst=const_cast(entry.dataSource->getDataPointer()), - *src = _extensions->glMapBufferRange( - _profile._target, - (GLintptr)entry.offset, - (GLsizeiptr)entry.dataSize, - _profile._mappingbitfield - ); - memcpy(dst,src,entry.dataSize); - _extensions->glUnmapBuffer(_profile._target); + GLvoid *src = const_cast(entry.dataSource->getDataPointer()), + *dst = _extensions->glMapBufferRange( _profile._target, (GLintptr)entry.offset, (GLsizeiptr)entry.dataSize, _profile._mappingbitfield); + memcpy(dst, src, entry.dataSize); + _extensions->glUnmapBuffer(_profile._target); } } else @@ -331,6 +289,25 @@ _persistantDMA=0; } } +void GLBufferObject::commitDMA(unsigned int entryidx) +{ + if( !(_profile._mappingbitfield & GL_MAP_PERSISTENT_BIT) ) return; + if (entryidx>=_bufferEntries.size()) compileBuffer(); + BufferEntry& entry = _bufferEntries[entryidx]; + _extensions->glFlushMappedBufferRange(_profile._target, (GLintptr)entry.offset, (GLsizeiptr)entry.dataSize); +} + +void GLBufferObject::downloadBuffer(unsigned int entryidx) +{ + if( !(_profile._mappingbitfield & GL_MAP_READ_BIT) ) return; + if (entryidx>=_bufferEntries.size()) compileBuffer(); + BufferEntry& entry = _bufferEntries[entryidx]; + GLvoid *dst = const_cast(entry.dataSource->getDataPointer()), + *src = _extensions->glMapBufferRange( _profile._target, (GLintptr)entry.offset, (GLsizeiptr)entry.dataSize, _profile._mappingbitfield); + memcpy(dst, src, entry.dataSize); + _extensions->glUnmapBuffer(_profile._target); +} + void GLBufferObject::deleteGLObject() { OSG_DEBUG<<"GLBufferObject::deleteGLObject() "<<_glObjectID<