-
Notifications
You must be signed in to change notification settings - Fork 0
/
vgl_buffer.hpp
197 lines (169 loc) · 4.34 KB
/
vgl_buffer.hpp
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#ifndef VGL_BUFFER_HPP
#define VGL_BUFFER_HPP
#include "vgl_extern_opengl.hpp"
#include "vgl_packed_data.hpp"
namespace vgl
{
namespace detail
{
/// OpenGL buffer state.
class OpenGlBufferState
{
private:
/// Currently bound vertex buffer ID.
GLuint g_current_vertex_buffer = 0;
/// Currently bound index buffer ID.
GLuint g_current_index_buffer = 0;
public:
/// Global state.
static OpenGlBufferState g_opengl_buffer_state;
public:
/// Updates bound vertex buffer ID.
///
/// \param op Buffer ID.
/// \return True if ID was changed.
constexpr bool updateBoundVertexBuffer(GLuint op)
{
if(g_current_vertex_buffer == op)
{
return false;
}
g_current_vertex_buffer = op;
return true;
}
/// Updates bound index buffer ID.
///
/// \param op Buffer ID.
/// \return True if ID was changed.
constexpr bool updateBoundIndexBuffer(GLuint op)
{
if(g_current_index_buffer == op)
{
return false;
}
g_current_index_buffer = op;
return true;
}
public:
/// Updates bound buffer ID.
///
/// \param op Buffer ID.
/// \return True if ID was changed.
template<GLenum BufferType> static constexpr bool update_bound_buffer(GLuint op);
};
/// \cond
template<> constexpr bool OpenGlBufferState::update_bound_buffer<GL_ARRAY_BUFFER>(GLuint op)
{
return g_opengl_buffer_state.updateBoundVertexBuffer(op);
}
/// \endcond
/// \cond
template<> constexpr bool OpenGlBufferState::update_bound_buffer<GL_ELEMENT_ARRAY_BUFFER>(GLuint op)
{
return g_opengl_buffer_state.updateBoundIndexBuffer(op);
}
/// \endcond
}
/// Represents a GL array/element buffer
template<GLenum BufferType> class Buffer
{
static_assert((BufferType == GL_ARRAY_BUFFER) || (BufferType == GL_ELEMENT_ARRAY_BUFFER));
private:
/// Buffer id.
GLuint m_id;
private:
/// Deleted copy constructor.
Buffer(const Buffer&) = delete;
/// Deleted assignment.
Buffer& operator=(const Buffer&) = delete;
public:
/// Constructor.
Buffer()
{
dnload_glGenBuffers(1, &m_id);
}
/// Destructor.
~Buffer()
{
dnload_glDeleteBuffers(1, &m_id);
}
private:
/// Update data to GPU.
///
/// \param ptr Pointer to data to update.
/// \param count Number of bytes to update.
void update(const void* ptr, unsigned count) const
{
bind();
dnload_glBufferData(BufferType, count, ptr, GL_STATIC_DRAW);
}
/// Update sub-data to GPU.
///
/// \param ptr Pointer to data to update.
/// \param count Number of bytes to update.
/// \param offset Offset into the buffer.
void update(const void* ptr, unsigned count, unsigned offset) const
{
bind();
dnload_glBufferSubData(BufferType, offset, count, ptr);
}
public:
/// Accessor.
///
/// \return Identifier.
GLuint getId() const
{
return m_id;
}
/// Bind this buffer.
///
/// \return True if the buffer was bound, false if no action was necessary.
bool bind() const
{
if(detail::OpenGlBufferState::update_bound_buffer<BufferType>(m_id))
{
dnload_glBindBuffer(BufferType, m_id);
return true;
}
return false;
}
/// Update data to GPU.
///
/// \param op Data to update.
void update(const PackedData& op) const
{
update(op.data(), op.size());
}
/// Update data to GPU.
///
/// \param data Data to update.
/// \param offset Offset to update into.
void update(const PackedData& data, unsigned offset) const
{
update(data.data(), data.size(), offset);
}
/// Update data to GPU.
///
/// \param op Data to update.
void update(const vector<uint16_t>& op) const
{
update(op.data(), op.getSizeBytes());
}
/// Update data to GPU.
///
/// \param data Data to update.
/// \param offset Offset to update into.
void update(const vector<uint16_t>& data, unsigned offset) const
{
update(data.data(), data.getSizeBytes(), offset);
}
};
/// Specialization of Buffer.
using VertexBuffer = Buffer<GL_ARRAY_BUFFER>;
/// Specialization of Buffer.
using IndexBuffer = Buffer<GL_ELEMENT_ARRAY_BUFFER>;
}
#if !defined(USE_LD)
#include "vgl_buffer.cpp"
#endif
#endif