summaryrefslogtreecommitdiff
path: root/include/gal/opengl/vertex_container.h
blob: 1c7b00bd66ddc67a5bad723a61b6ab0b582a7ef1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * 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 vertex_container.h
 * @brief 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:
    /**
     * Function MakeContainer()
     * Returns a pointer to a new container of an appropriate type.
     */
    static VERTEX_CONTAINER* MakeContainer( bool aCached );

    virtual ~VERTEX_CONTAINER();

    /**
     * Function SetItem()
     * sets the item in order to modify or finishes its current modifications.
     * @param aItem is the item or NULL in case of finishing the item.
     */
    virtual void SetItem( VERTEX_ITEM* aItem ) = 0;

    /**
     * Function FinishItem()
     * does the cleaning after adding an item.
     */
    virtual void FinishItem() {};

    /**
     * Function Allocate()
     * returns allocated space (possibly resizing the reserved memory chunk or allocating a new
     * chunk if it was not stored before) for the given number of vertices associated with the
     * current item (set by SetItem()). The newly 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;

    /**
     * Function Delete()
     * erases the selected item.
     *
     * @param aItem is the item to be erased.
     */
    virtual void Delete( VERTEX_ITEM* aItem ) = 0;

    /**
     * Function Clear()
     * removes all the data stored in the container and restores its original state.
     */
    virtual void Clear() = 0;

    /**
     * Function GetAllVertices()
     * returns all the vertices stored in the container. It is especially useful for transferring
     * data to the GPU memory.
     */
    inline virtual VERTEX* GetAllVertices() const
    {
        return m_vertices;
    }

    /**
     * Function GetVertices()
     * returns vertices stored at the specific offset.
     * @param aOffset is the offset.
     */
    virtual inline VERTEX* GetVertices( unsigned int aOffset ) const
    {
        return &m_vertices[aOffset];
    }

    /**
     * Function GetSize()
     * returns amount of vertices currently stored in the container.
     */
    virtual inline unsigned int GetSize() const
    {
        return m_currentSize;
    }

    /**
     * Function IsDirty()
     * returns information about container cache state. Clears the flag after calling the function.
     * @return true in case the vertices have to be reuploaded.
     */
    inline bool IsDirty()
    {
        bool state = m_dirty;

        m_dirty = false;

        return state;
    }

    /**
     * Function SetDirty()
     * sets the dirty flag, so vertices in the container are going to be reuploaded to the GPU on
     * the next frame.
     */
    inline void SetDirty()
    {
        m_dirty = true;
    }

protected:
    VERTEX_CONTAINER( unsigned int aSize = defaultInitSize );

    ///< How many vertices we can store in the container
    unsigned int    m_freeSpace;

    ///< How big is the current container, expressed in vertices
    unsigned int    m_currentSize;

    ///< Store the initial size, so it can be resized to this on Clear()
    unsigned int    m_initialSize;

    ///< Actual storage memory (should be handled using malloc/realloc/free to speed up resizing)
    VERTEX*         m_vertices;

    ///< State flags
    bool            m_failed;
    bool            m_dirty;

    /**
     * Function reservedSpace()
     * returns size of the reserved memory space.
     * @return Size of the reserved memory space (expressed as a number of vertices).
     */
    inline unsigned int reservedSpace()
    {
        return m_currentSize - m_freeSpace;
    }

    ///< Default initial size of a container (expressed in vertices)
    static const unsigned int defaultInitSize = 1048576;
};
} // namespace KIGFX

#endif /* VERTEX_CONTAINER_H_ */