kicad/include/gal/opengl/vertex_container.h

192 lines
5.0 KiB
C
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright 2013-2017 CERN
2020-12-21 23:42:21 +00:00
* Copyright (C) 2020 KiCad Developers, see AUTHORS.txt for contributors.
*
* @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 vertex_container.h
2020-12-21 23:42:21 +00:00
* Class to store vertices and handle transfers between system memory and GPU memory.
*/
#ifndef VERTEX_CONTAINER_H_
#define VERTEX_CONTAINER_H_
#include <gal/opengl/vertex_common.h>
namespace KIGFX
{
class VERTEX_ITEM;
class SHADER;
class VERTEX_CONTAINER
{
public:
/**
2020-12-21 23:42:21 +00:00
* Return a pointer to a new container of an appropriate type.
*/
static VERTEX_CONTAINER* MakeContainer( bool aCached );
virtual ~VERTEX_CONTAINER();
/**
2020-12-21 23:42:21 +00:00
* Return true if the container caches vertex data in RAM or video memory.
* Otherwise it is a single batch draw which is later discarded.
*/
virtual bool IsCached() const = 0;
/**
2020-12-21 23:42:21 +00:00
* Prepare the container for vertices updates.
*/
virtual void Map() {}
/**
2020-12-21 23:42:21 +00:00
* Finish the vertices updates stage.
*/
virtual void Unmap() {}
/**
2020-12-21 23:42:21 +00:00
* Set the item for the further actions.
*
* @param aItem is the item or NULL in case of finishing the item.
*/
virtual void SetItem( VERTEX_ITEM* aItem ) = 0;
2013-09-12 07:44:57 +00:00
/**
2017-08-21 11:30:35 +00:00
* Clean up after adding an item.
2013-09-12 07:44:57 +00:00
*/
virtual void FinishItem() {};
/**
2020-12-21 23:42:21 +00:00
* Return allocated space for the requested number of vertices associated with the
* current item (set with SetItem()).
*
* The allocated space is added at the end of the chunk used by the current item and
* may serve to store new vertices.
*
* @param aSize is the number of vertices to be allocated.
* @return Pointer to the allocated space or NULL in case of failure.
*/
virtual VERTEX* Allocate( unsigned int aSize ) = 0;
/**
2020-12-21 23:42:21 +00:00
* Erase the data related to an item.
*
2013-09-12 07:44:57 +00:00
* @param aItem is the item to be erased.
*/
2013-09-12 07:44:57 +00:00
virtual void Delete( VERTEX_ITEM* aItem ) = 0;
/**
2020-12-21 23:42:21 +00:00
* Remove all data stored in the container and restores its original state.
*/
virtual void Clear() = 0;
/**
2020-12-21 23:42:21 +00:00
* Return pointer to the vertices stored in the container.
*/
2017-08-21 11:30:35 +00:00
VERTEX* GetAllVertices() const
{
return m_vertices;
}
/**
2020-12-21 23:42:21 +00:00
* Return vertices stored at the specific offset.
*
* @param aOffset is the offset.
*/
2017-08-21 11:30:35 +00:00
virtual VERTEX* GetVertices( unsigned int aOffset ) const
{
return &m_vertices[aOffset];
}
/**
2020-12-21 23:42:21 +00:00
* Return amount of vertices currently stored in the container.
*/
2017-08-21 11:30:35 +00:00
virtual unsigned int GetSize() const
{
return m_currentSize;
}
/**
2020-12-21 23:42:21 +00:00
* Return information about the container cache state.
*
2017-08-21 11:30:35 +00:00
* @return True in case the vertices have to be reuploaded.
*/
2017-08-21 11:30:35 +00:00
bool IsDirty() const
{
2017-08-21 11:30:35 +00:00
return m_dirty;
}
/**
2020-12-21 23:42:21 +00:00
* Set the dirty flag, so vertices in the container are going to be reuploaded to the GPU on
* the next frame.
*/
2017-08-21 11:30:35 +00:00
void SetDirty()
{
m_dirty = true;
}
/**
2020-12-21 23:42:21 +00:00
* Clear the dirty flag to prevent reuploading vertices to the GPU memory.
*/
void ClearDirty()
{
m_dirty = false;
}
protected:
VERTEX_CONTAINER( unsigned int aSize = DEFAULT_SIZE );
2020-12-21 23:42:21 +00:00
/**
* Return size of the used memory space.
*
* @return Size of the used memory space (expressed as a number of vertices).
*/
unsigned int usedSpace() const
{
return m_currentSize - m_freeSpace;
}
2017-08-21 11:30:35 +00:00
///> Free space left in the container, expressed in vertices
unsigned int m_freeSpace;
2017-08-21 11:30:35 +00:00
///> Current container size, expressed in vertices
unsigned int m_currentSize;
2017-08-21 11:30:35 +00:00
///> Store the initial size, so it can be resized to this on Clear()
unsigned int m_initialSize;
2017-08-21 11:30:35 +00:00
///> Actual storage memory
VERTEX* m_vertices;
2017-08-21 11:30:35 +00:00
// Status flags
bool m_failed;
bool m_dirty;
///< Default initial size of a container (expressed in vertices)
static constexpr unsigned int DEFAULT_SIZE = 1048576;
};
} // namespace KIGFX
#endif /* VERTEX_CONTAINER_H_ */