2013-07-22 08:41:12 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 CERN
|
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file cached_container.h
|
|
|
|
* @brief Class to store instances of VERTEX with caching. It allows storing VERTEX objects and
|
|
|
|
* associates them with VERTEX_ITEMs. This leads to a possibility of caching vertices data in the
|
|
|
|
* GPU memory and a fast reuse of that data.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CACHED_CONTAINER_H_
|
|
|
|
#define CACHED_CONTAINER_H_
|
|
|
|
|
|
|
|
#include <gal/opengl/vertex_container.h>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
|
2013-10-14 14:13:35 +00:00
|
|
|
namespace KIGFX
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
|
|
|
class VERTEX_ITEM;
|
|
|
|
class SHADER;
|
|
|
|
|
|
|
|
class CACHED_CONTAINER : public VERTEX_CONTAINER
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CACHED_CONTAINER( unsigned int aSize = defaultInitSize );
|
2016-05-02 14:12:16 +00:00
|
|
|
~CACHED_CONTAINER();
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
///> @copydoc VERTEX_CONTAINER::SetItem()
|
2013-07-22 08:41:12 +00:00
|
|
|
virtual void SetItem( VERTEX_ITEM* aItem );
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
///> @copydoc VERTEX_CONTAINER::FinishItem()
|
2013-09-12 07:44:57 +00:00
|
|
|
virtual void FinishItem();
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
///> @copydoc VERTEX_CONTAINER::Allocate()
|
2013-07-22 08:41:12 +00:00
|
|
|
virtual VERTEX* Allocate( unsigned int aSize );
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
///> @copydoc VERTEX_CONTAINER::Delete()
|
2013-09-12 07:44:57 +00:00
|
|
|
virtual void Delete( VERTEX_ITEM* aItem );
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
///> @copydoc VERTEX_CONTAINER::Clear()
|
2013-07-22 08:41:12 +00:00
|
|
|
virtual void Clear();
|
|
|
|
|
2016-05-02 14:12:16 +00:00
|
|
|
/**
|
|
|
|
* Function GetBufferHandle()
|
|
|
|
* returns handle to the vertex buffer. It might be negative if the buffer is not initialized.
|
|
|
|
*/
|
|
|
|
inline unsigned int GetBufferHandle() const
|
|
|
|
{
|
|
|
|
return m_glBufferHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function IsMapped()
|
|
|
|
* returns true if vertex buffer is currently mapped.
|
|
|
|
*/
|
|
|
|
inline bool IsMapped() const
|
|
|
|
{
|
|
|
|
return m_isMapped;
|
|
|
|
}
|
|
|
|
|
|
|
|
///> @copydoc VERTEX_CONTAINER::Map()
|
|
|
|
void Map();
|
|
|
|
|
|
|
|
///> @copydoc VERTEX_CONTAINER::Unmap()
|
|
|
|
void Unmap();
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
protected:
|
2013-10-14 18:40:36 +00:00
|
|
|
///> Maps size of free memory chunks to their offsets
|
|
|
|
typedef std::pair<unsigned int, unsigned int> CHUNK;
|
|
|
|
typedef std::multimap<unsigned int, unsigned int> FREE_CHUNK_MAP;
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
/// List of all the stored items
|
2013-10-14 18:40:36 +00:00
|
|
|
typedef std::set<VERTEX_ITEM*> ITEMS;
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
///> Stores size & offset of free chunks.
|
|
|
|
FREE_CHUNK_MAP m_freeChunks;
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
///> Stored VERTEX_ITEMs
|
|
|
|
ITEMS m_items;
|
2013-07-22 08:41:12 +00:00
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
///> Currently modified item
|
2013-07-22 08:41:12 +00:00
|
|
|
VERTEX_ITEM* m_item;
|
|
|
|
|
2013-10-14 18:40:36 +00:00
|
|
|
///> Properties of currently modified chunk & item
|
2013-07-22 08:41:12 +00:00
|
|
|
unsigned int m_chunkSize;
|
|
|
|
unsigned int m_chunkOffset;
|
|
|
|
|
2016-05-02 14:15:24 +00:00
|
|
|
///> Flag saying if vertex buffer is currently mapped
|
2016-05-02 14:12:16 +00:00
|
|
|
bool m_isMapped;
|
2016-05-02 14:15:24 +00:00
|
|
|
|
|
|
|
///> Flag saying if the vertex buffer is initialized
|
2016-05-02 14:12:16 +00:00
|
|
|
bool m_isInitialized;
|
2016-05-02 14:15:24 +00:00
|
|
|
|
|
|
|
///> Vertex buffer handle
|
2016-05-02 14:12:16 +00:00
|
|
|
unsigned int m_glBufferHandle;
|
|
|
|
|
2016-05-02 14:15:24 +00:00
|
|
|
///> Flag saying whether it is safe to use glCopyBufferSubData
|
|
|
|
bool m_useCopyBuffer;
|
|
|
|
|
2016-05-02 14:12:16 +00:00
|
|
|
/**
|
|
|
|
* Function init()
|
|
|
|
* performs the GL vertex buffer initialization. It can be invoked only when an OpenGL context
|
|
|
|
* is bound.
|
|
|
|
*/
|
|
|
|
void init();
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
/**
|
2013-07-30 16:29:54 +00:00
|
|
|
* Function reallocate()
|
2016-05-02 14:12:16 +00:00
|
|
|
* resizes the chunk that stores the current item to the given size. The current item has
|
2016-05-02 14:12:17 +00:00
|
|
|
* its offset adjusted after the call, and the new chunk parameters are stored
|
|
|
|
* in m_chunkOffset and m_chunkSize.
|
2013-07-30 16:29:54 +00:00
|
|
|
*
|
2016-05-02 14:12:17 +00:00
|
|
|
* @param aSize is the requested chunk size.
|
|
|
|
* @return true in case of success, false otherwise
|
2013-07-30 16:29:54 +00:00
|
|
|
*/
|
2016-05-02 14:12:17 +00:00
|
|
|
bool reallocate( unsigned int aSize );
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
/**
|
2016-05-02 14:12:16 +00:00
|
|
|
* Function defragmentResize()
|
|
|
|
* removes empty spaces between chunks and optionally resizes the container.
|
|
|
|
* After the operation there is continous space for storing vertices at the end of the container.
|
2013-07-22 08:41:12 +00:00
|
|
|
*
|
2016-05-02 14:12:16 +00:00
|
|
|
* @param aNewSize is the new size of container, expressed in number of vertices
|
|
|
|
* @return false in case of failure (e.g. memory shortage)
|
2013-07-22 08:41:12 +00:00
|
|
|
*/
|
2016-05-02 14:12:16 +00:00
|
|
|
bool defragmentResize( unsigned int aNewSize );
|
2016-05-02 14:15:24 +00:00
|
|
|
bool defragmentResizeMemcpy( unsigned int aNewSize );
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function mergeFreeChunks()
|
|
|
|
* looks for consecutive free memory chunks and merges them, decreasing fragmentation of
|
|
|
|
* memory.
|
|
|
|
*/
|
2016-05-02 14:12:16 +00:00
|
|
|
void mergeFreeChunks();
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function getPowerOf2()
|
|
|
|
* returns the nearest power of 2, bigger than aNumber.
|
|
|
|
*
|
|
|
|
* @param aNumber is the number for which we look for a bigger power of 2.
|
|
|
|
*/
|
|
|
|
unsigned int getPowerOf2( unsigned int aNumber ) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* Function getChunkSize()
|
|
|
|
* returns size of the given chunk.
|
|
|
|
*
|
|
|
|
* @param aChunk is the chunk.
|
|
|
|
*/
|
2013-10-14 18:40:36 +00:00
|
|
|
inline int getChunkSize( const CHUNK& aChunk ) const
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
|
|
|
return aChunk.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function getChunkOffset()
|
|
|
|
* returns offset of the chunk.
|
|
|
|
*
|
|
|
|
* @param aChunk is the chunk.
|
|
|
|
*/
|
2013-10-14 18:40:36 +00:00
|
|
|
inline unsigned int getChunkOffset( const CHUNK& aChunk ) const
|
2013-07-22 08:41:12 +00:00
|
|
|
{
|
|
|
|
return aChunk.second;
|
|
|
|
}
|
|
|
|
|
2016-05-02 14:12:16 +00:00
|
|
|
/**
|
|
|
|
* Function addFreeChunk
|
|
|
|
* Adds a chunk marked as free.
|
|
|
|
*/
|
|
|
|
void addFreeChunk( unsigned int aOffset, unsigned int aSize );
|
|
|
|
|
2013-07-22 08:41:12 +00:00
|
|
|
/// Debug & test functions
|
|
|
|
void showFreeChunks();
|
2016-05-02 14:12:16 +00:00
|
|
|
void showUsedChunks();
|
2013-07-22 08:41:12 +00:00
|
|
|
void test();
|
|
|
|
};
|
2013-10-14 14:13:35 +00:00
|
|
|
} // namespace KIGFX
|
2013-07-22 08:41:12 +00:00
|
|
|
|
|
|
|
#endif /* CACHED_CONTAINER_H_ */
|