Commit bbc0e8eb authored by Maciej Suminski's avatar Maciej Suminski

Refactorization of VBO_CONTAINER.

parent 99e88140
This diff is collapsed.
...@@ -52,9 +52,8 @@ VBO_ITEM::~VBO_ITEM() ...@@ -52,9 +52,8 @@ VBO_ITEM::~VBO_ITEM()
void VBO_ITEM::PushVertex( const VBO_VERTEX* aVertex ) void VBO_ITEM::PushVertex( const VBO_VERTEX* aVertex )
{ {
m_container->Add( this, aVertex ); m_container->Add( aVertex );
m_size++;
m_isDirty = true; m_isDirty = true;
} }
......
...@@ -35,8 +35,7 @@ ...@@ -35,8 +35,7 @@
#include <gal/opengl/vbo_item.h> #include <gal/opengl/vbo_item.h>
#include <gal/color4d.h> #include <gal/color4d.h>
#include <map> #include <map>
#include <boost/unordered_map.hpp> #include <set>
#include <wx/log.h>
namespace KiGfx namespace KiGfx
{ {
...@@ -47,23 +46,22 @@ class VBO_CONTAINER ...@@ -47,23 +46,22 @@ class VBO_CONTAINER
{ {
public: public:
VBO_CONTAINER( unsigned int aSize = defaultInitSize ); VBO_CONTAINER( unsigned int aSize = defaultInitSize );
~VBO_CONTAINER(); virtual ~VBO_CONTAINER();
///< Maps size of free memory chunks to their offsets ///< Maps size of free memory chunks to their offsets
typedef std::pair<const unsigned int, unsigned int> Chunk; typedef std::pair<const unsigned int, unsigned int> Chunk;
typedef std::multimap<const unsigned int, unsigned int> FreeChunkMap; typedef std::multimap<const unsigned int, unsigned int> FreeChunkMap;
///< Maps VBO_ITEMs to reserved memory chunks offsets & sizes /// List of all the stored items
typedef std::pair<VBO_ITEM* const, Chunk> ReservedChunk; typedef std::set<VBO_ITEM*> Items;
typedef boost::unordered_map<VBO_ITEM* const, Chunk> ReservedChunkMap;
/** /**
* Function StartItem() * Function StartItem()
* Starts an unknown sized item. After calling the function it is possible to add vertices * Sets an item to start its modifications. After calling the function it is possible to add
* using function Add(). * vertices using function Add().
* @param aVboItem is the item that is going to store vertices in the container. * @param aItem is the item that is going to store vertices in the container.
*/ */
void StartItem( VBO_ITEM* aVboItem ); void StartItem( VBO_ITEM* aItem );
/** /**
* Function EndItem() * Function EndItem()
...@@ -74,30 +72,20 @@ public: ...@@ -74,30 +72,20 @@ public:
/** /**
* Function Add() * Function Add()
* Stores given number of vertices in the container for the specific VBO_ITEM. * Stores given number of vertices in the container for the specific VBO_ITEM (started by
* @param aVboItem is the owner of the vertices. * StartItem() function).
* @param aItem is the owner of the vertices.
* @param aVertex are vertices data to be stored. * @param aVertex are vertices data to be stored.
* @param aSize is the number of vertices to be added. * @param aSize is the number of vertices to be added.
*/ */
void Add( VBO_ITEM* aVboItem, const VBO_VERTEX* aVertex, unsigned int aSize = 1 ); void Add( const VBO_VERTEX* aVertex, unsigned int aSize = 1 );
/** /**
* Function Free() * Function Free()
* Frees the chunk reserved by the aVboItem. * Frees the chunk reserved by the aItem.
* @param aVboItem is the owner of the chunk to be freed. * @param aItem is the owner of the chunk to be freed.
*/ */
inline void Free( VBO_ITEM* aVboItem ) void Free( VBO_ITEM* aItem );
{
ReservedChunkMap::iterator it = m_reservedChunks.find( aVboItem );
freeChunk( it );
// Dynamic memory freeing, there is no point in holding
// a large amount of memory when there is no use for it
if( m_freeSpace > ( m_currentSize / 2 ) )
{
resizeContainer( m_currentSize / 2 );
}
}
/** /**
* Function Clear() * Function Clear()
...@@ -115,9 +103,9 @@ public: ...@@ -115,9 +103,9 @@ public:
/** /**
* Function GetVertices() * Function GetVertices()
* Returns vertices stored by the specific item. * Returns vertices stored by the specific item.
* @aVboItem is the specific item. * @aItem is the specific item.
*/ */
VBO_VERTEX* GetVertices( const VBO_ITEM* aVboItem ) const; VBO_VERTEX* GetVertices( const VBO_ITEM* aItem ) const;
/** /**
* Function GetVertices() * Function GetVertices()
...@@ -208,51 +196,50 @@ public: ...@@ -208,51 +196,50 @@ public:
private: private:
///< Stores size & offset of free chunks. ///< Stores size & offset of free chunks.
FreeChunkMap m_freeChunks; FreeChunkMap m_freeChunks;
///< Stores owners (VBO_ITEM*) of reserved chunks and their size & offset. ///< Stored VERTEX_ITEMs
ReservedChunkMap m_reservedChunks; Items m_items;
/**
* Function allocate()
* Allocates the given amount of memory for the current VBO_ITEM (set by StartItem() function).
* @param aSize is the number of vertices that are requested to be allocated.
* @return Pointer to the allocated space.
*/
VBO_VERTEX* allocate( unsigned int aSize );
/** /**
* Function allocate() * Function reallocate()
* Finds an offset where the number of vertices can be stored in a continous space. If there is * Resizes the chunk that stores the current item to the given size.
* no such chunk, appropriate amount of memory is allocated first.
* @param aVboItem is the owner of vertices to be stored.
* @param aSize is the number of vertices to be stored. * @param aSize is the number of vertices to be stored.
* @return Offset of the new chunk.
*/ */
unsigned int allocate( VBO_ITEM* aVboItem, unsigned int aSize ); unsigned int reallocate( unsigned int aSize );
/** /**
* Function getChunkSize() * Function getChunkSize()
* Returns size of the given chunk (works both for reserved and free chunks). * Returns size of the given chunk.
* @param aChunk is the chunk. * @param aChunk is the chunk.
* @return Size of the chunk.
*/ */
inline int getChunkSize( const Chunk& aChunk ) const inline unsigned int getChunkSize( const Chunk& aChunk ) const
{ {
return aChunk.first; return aChunk.first;
} }
inline int getChunkSize( const ReservedChunk& aChunk ) const
{
return aChunk.second.first;
}
/** /**
* Function getChunkOffset() * Function getChunkOffset()
* Returns offset of the given chunk (works both for reserved and free chunks). * Returns offset of the given chunk.
* @param aChunk is the chunk. * @param aChunk is the chunk.
* @return Offset of the chunk.
*/ */
inline unsigned int getChunkOffset( const Chunk& aChunk ) const inline unsigned int getChunkOffset( const Chunk& aChunk ) const
{ {
return aChunk.second; return aChunk.second;
} }
inline unsigned int getChunkOffset( const ReservedChunk& aChunk ) const
{
return aChunk.second.second;
}
/** /**
* Function getChunkOffset() * Function setChunkOffset()
* Upadtes offset of the given chunk (works both for reserved and free chunks). * Updates offset of the given chunk.
* !! IMPORTANT: it does not reallocate the chunk, it just changes its properties. * !! IMPORTANT: it does not reallocate the chunk, it just changes its properties.
* @param aChunk is the chunk. * @param aChunk is the chunk.
*/ */
...@@ -261,51 +248,45 @@ private: ...@@ -261,51 +248,45 @@ private:
aChunk.second = aOffset; aChunk.second = aOffset;
} }
inline void setChunkOffset( ReservedChunk& aChunk, unsigned int aOffset ) const
{
aChunk.second.second = aOffset;
}
/**
* Function getChunkVboItem()
* Returns owner of the given reserved chunk.
* @param aChunk is the chunk.
*/
inline VBO_ITEM* getChunkVboItem( const ReservedChunk& aChunk ) const
{
return aChunk.first;
}
/** /**
* Function defragment() * Function defragment()
* Removes empty spaces between chunks, so after that there is a long continous space * Removes empty spaces between chunks, so after that there is a long continous space
* for storing vertices at the and of the container. * for storing vertices at the and of the container.
* @return false in case of failure (eg. memory shortage) * @return false in case of failure (eg. memory shortage).
*/ */
bool defragment( VBO_VERTEX* aTarget = NULL ); bool defragment( VBO_VERTEX* aTarget = NULL );
/** /**
* Function resizeChunk() * Function mergeFreeChunks()
* Changes size of the chunk that stores vertices of aVboItem. * Looks for consecutive free memory chunks and merges them, decreasing fragmentation of
* @param aVboItem is the item for which reserved space size should be changed. * memory.
* @param aNewSize is the new size for the aVboItem, expressed in vertices number.
*/ */
void resizeChunk( VBO_ITEM* aVboItem, int aNewSize ); void mergeFreeChunks();
/** /**
* Function resizeContainer() * Function resizeContainer()
* Prepares a bigger container of a given size. * Prepares a bigger container of a given size.
* @param aNewSize is the new size of container, expressed in vertices * @param aNewSize is the new size of container, expressed in vertices
* @return false in case of failure (eg. memory shortage) * @return false in case of failure (eg. memory shortage).
*/ */
bool resizeContainer( unsigned int aNewSize ); bool resizeContainer( unsigned int aNewSize );
/** /**
* Function freeChunk() * Function freeItem()
* Frees the space described in aChunk and returns it to the free space pool. * Frees the space occupied by the item and returns it to the free space pool.
* @param aChunk is a space to be freed. * @param aItem is the item to be freed.
*/ */
void freeChunk( const ReservedChunkMap::iterator& aChunk ); void freeItem( VBO_ITEM* aItem );
/**
* Function reservedSpace()
* Returns size of the reserved memory space.
* @return Size of the reserved memory space (expressed as a number of vertices).
*/
unsigned int reservedSpace()
{
return m_currentSize - m_freeSpace;
}
///< How many vertices we can store in the container ///< How many vertices we can store in the container
unsigned int m_freeSpace; unsigned int m_freeSpace;
...@@ -316,25 +297,26 @@ private: ...@@ -316,25 +297,26 @@ private:
///< Actual storage memory ///< Actual storage memory
VBO_VERTEX* m_vertices; VBO_VERTEX* m_vertices;
///< A flag saying if there is the item with an unknown size being added ///< Initial size, used on clearing the container
bool itemStarted; unsigned int m_initialSize;
///< Variables holding the state of the item currently being added ///< Variables holding the state of the item currently being modified
unsigned int itemSize; unsigned int m_itemSize;
unsigned int itemChunkSize; unsigned int m_chunkSize;
VBO_ITEM* item; unsigned int m_chunkOffset;
VBO_ITEM* m_item;
///< Color used for new vertices pushed. ///< Color used for the new vertices pushed.
GLubyte m_color[VBO_ITEM::ColorStride]; GLubyte m_color[VBO_ITEM::ColorStride];
///< Shader and its parameters used for new vertices pushed ///< Shader and its parameters used for new vertices pushed
GLfloat m_shader[VBO_ITEM::ShaderStride]; GLfloat m_shader[VBO_ITEM::ShaderStride];
///< Current transform matrix applied for every new vertex pushed. ///< Current transform matrix applied for every new vertex pushed.
const glm::mat4* m_transform; const glm::mat4* m_transform;
///< Failure flag ///< Failure flag
bool m_failed; bool m_failed;
/** /**
* Function getPowerOf2() * Function getPowerOf2()
...@@ -353,6 +335,9 @@ private: ...@@ -353,6 +335,9 @@ private:
///< Default initial size of a container (expressed in vertices) ///< Default initial size of a container (expressed in vertices)
static const unsigned int defaultInitSize = 1048576; static const unsigned int defaultInitSize = 1048576;
///< Basic tests for the container, use only for debugging.
void test() const;
}; };
} // namespace KiGfx } // namespace KiGfx
......
...@@ -47,6 +47,8 @@ class VBO_CONTAINER; ...@@ -47,6 +47,8 @@ class VBO_CONTAINER;
class VBO_ITEM class VBO_ITEM
{ {
friend class VBO_CONTAINER;
public: public:
VBO_ITEM( VBO_CONTAINER* aContainer ); VBO_ITEM( VBO_CONTAINER* aContainer );
~VBO_ITEM(); ~VBO_ITEM();
...@@ -89,16 +91,6 @@ public: ...@@ -89,16 +91,6 @@ public:
return m_size; return m_size;
} }
/**
* Function SetOffset()
* Sets data offset in the VBO.
* @param aOffset is the offset expressed as a number of vertices.
*/
inline void SetOffset( unsigned int aOffset )
{
m_offset = aOffset;
}
/** /**
* Function GetOffset() * Function GetOffset()
* Returns data offset in the VBO. * Returns data offset in the VBO.
...@@ -149,7 +141,7 @@ public: ...@@ -149,7 +141,7 @@ public:
static const unsigned int IndByteSize = sizeof(GLuint); static const unsigned int IndByteSize = sizeof(GLuint);
private: protected:
///< Offset and size of data stored in the VBO_CONTAINER. ///< Offset and size of data stored in the VBO_CONTAINER.
unsigned int m_offset; unsigned int m_offset;
unsigned int m_size; unsigned int m_size;
...@@ -159,6 +151,26 @@ private: ...@@ -159,6 +151,26 @@ private:
///< Flag telling if the item should be recached in VBO or not. ///< Flag telling if the item should be recached in VBO or not.
bool m_isDirty; bool m_isDirty;
/**
* Function setSize()
* Sets data size in the VBO.
* @param aSize is the size expressed as a number of vertices.
*/
void setSize( unsigned int aSize )
{
m_size = aSize;
}
/**
* Function setOffset()
* Sets data offset in the VBO.
* @param aOffset is the offset expressed as a number of vertices.
*/
inline void setOffset( unsigned int aOffset )
{
m_offset = aOffset;
}
}; };
} // namespace KiGfx } // namespace KiGfx
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment