diff --git a/common/gal/opengl/cached_container.cpp b/common/gal/opengl/cached_container.cpp index cadeed9f91..d9e966164e 100644 --- a/common/gal/opengl/cached_container.cpp +++ b/common/gal/opengl/cached_container.cpp @@ -45,7 +45,7 @@ using namespace KIGFX; CACHED_CONTAINER::CACHED_CONTAINER( unsigned int aSize ) : - VERTEX_CONTAINER( aSize ), m_item( NULL ), m_chunkSize( 0 ), m_chunkOffset( 0 ) + VERTEX_CONTAINER( aSize ), m_item( NULL ), m_chunkSize( 0 ), m_chunkOffset( 0 ), m_maxIndex( 0 ) { // In the beginning there is only free space m_freeChunks.insert( std::make_pair( aSize, 0 ) ); @@ -84,6 +84,8 @@ void CACHED_CONTAINER::FinishItem() // Add the not used memory back to the pool addFreeChunk( itemOffset + itemSize, m_chunkSize - itemSize ); // mergeFreeChunks(); // veery slow and buggy + + m_maxIndex = std::max( itemOffset + itemSize, m_maxIndex ); } if( itemSize > 0 ) @@ -189,6 +191,7 @@ void CACHED_CONTAINER::Delete( VERTEX_ITEM* aItem ) void CACHED_CONTAINER::Clear() { m_freeSpace = m_currentSize; + m_maxIndex = 0; m_failed = false; // Set the size of all the stored VERTEX_ITEMs to 0, so it is clear that they are not held @@ -305,6 +308,8 @@ void CACHED_CONTAINER::defragment( VERTEX* aTarget ) m_item->setOffset( newOffset ); m_chunkOffset = newOffset; } + + m_maxIndex = usedSpace(); } diff --git a/common/gal/opengl/cached_container_ram.cpp b/common/gal/opengl/cached_container_ram.cpp index b6eff40b2a..a306373dfa 100644 --- a/common/gal/opengl/cached_container_ram.cpp +++ b/common/gal/opengl/cached_container_ram.cpp @@ -64,7 +64,7 @@ void CACHED_CONTAINER_RAM::Unmap() // Upload vertices coordinates and shader types to GPU memory glBindBuffer( GL_ARRAY_BUFFER, m_verticesBuffer ); checkGlError( "binding vertices buffer" ); - glBufferData( GL_ARRAY_BUFFER, usedSpace() * VERTEX_SIZE, m_vertices, GL_STREAM_DRAW ); + glBufferData( GL_ARRAY_BUFFER, m_maxIndex * VERTEX_SIZE, m_vertices, GL_STREAM_DRAW ); checkGlError( "transferring vertices" ); glBindBuffer( GL_ARRAY_BUFFER, 0 ); checkGlError( "unbinding vertices buffer" ); @@ -110,6 +110,7 @@ bool CACHED_CONTAINER_RAM::defragmentResize( unsigned int aNewSize ) // Now there is only one big chunk of free memory m_freeChunks.clear(); m_freeChunks.insert( std::make_pair( m_freeSpace, m_currentSize - m_freeSpace ) ); + m_dirty = true; return true; } diff --git a/include/gal/opengl/cached_container.h b/include/gal/opengl/cached_container.h index 299c997262..70cd32e28a 100644 --- a/include/gal/opengl/cached_container.h +++ b/include/gal/opengl/cached_container.h @@ -103,6 +103,9 @@ protected: unsigned int m_chunkSize; unsigned int m_chunkOffset; + ///> Maximal vertex index number stored in the container + unsigned int m_maxIndex; + /** * Resizes the chunk that stores the current item to the given size. The current item has * its offset adjusted after the call, and the new chunk parameters are stored