-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
VertexArray.cc
360 lines (335 loc) · 15.5 KB
/
VertexArray.cc
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <memory>
#include <cstdio>
#include <functional>
#include "VertexArray.h"
#include "printutils.h"
void addAttributeValues(IAttributeData&) {}
void VertexData::getLastVertex(std::vector<GLbyte>& interleaved_buffer) const
{
GLbyte *dst_start = interleaved_buffer.data();
for (const auto& data : attributes_) {
size_t size = data->sizeofAttribute();
GLbyte *dst = dst_start;
const GLbyte *src = data->toBytes() + data->sizeInBytes() - data->sizeofAttribute();
std::memcpy((void *)dst, (void *)src, size);
dst_start += size;
}
}
void VertexData::remove(size_t count)
{
for (const auto& data : attributes_) {
data->remove(count);
}
}
void VertexData::append(const VertexData& data) {
size_t i = 0;
for (auto& a : attributes_) {
a->append(*(data.attributes_[i]));
i++;
}
}
void VertexArray::addSurfaceData()
{
std::shared_ptr<VertexData> vertex_data = std::make_shared<VertexData>();
vertex_data->addPositionData(std::make_shared<AttributeData<GLfloat, 3, GL_FLOAT>>());
vertex_data->addNormalData(std::make_shared<AttributeData<GLfloat, 3, GL_FLOAT>>());
vertex_data->addColorData(std::make_shared<AttributeData<GLfloat, 4, GL_FLOAT>>());
surface_index_ = vertices_.size();
addVertexData(vertex_data);
}
void VertexArray::addEdgeData()
{
std::shared_ptr<VertexData> vertex_data = std::make_shared<VertexData>();
vertex_data->addPositionData(std::make_shared<AttributeData<GLfloat, 3, GL_FLOAT>>());
vertex_data->addColorData(std::make_shared<AttributeData<GLfloat, 4, GL_FLOAT>>());
edge_index_ = vertices_.size();
addVertexData(vertex_data);
}
void VertexArray::append(const VertexArray& vertex_array)
{
size_t i = 0;
for (auto& v : vertices_) {
v->append(*(vertex_array.vertices_[i]));
i++;
}
}
void VertexArray::createVertex(const std::array<Vector3d, 3>& points,
const std::array<Vector3d, 3>& normals,
const Color4f& color,
size_t active_point_index, size_t primitive_index,
double z_offset, size_t shape_size,
size_t shape_dimensions, bool outlines,
bool mirror, const CreateVertexCallback& vertex_callback)
{
if (vertex_callback)
vertex_callback(*this, points, normals, color, active_point_index,
primitive_index, z_offset, shape_size,
shape_dimensions, outlines, mirror);
addAttributeValues(*(data()->positionData()), points[active_point_index][0], points[active_point_index][1], points[active_point_index][2]);
if (data()->hasNormalData()) {
addAttributeValues(*(data()->normalData()), normals[active_point_index][0], normals[active_point_index][1], normals[active_point_index][2]);
}
if (data()->hasColorData()) {
addAttributeValues(*(data()->colorData()), color[0], color[1], color[2], color[3]);
}
if (useElements()) {
std::vector<GLbyte> interleaved_vertex;
interleaved_vertex.resize(data()->stride());
data()->getLastVertex(interleaved_vertex);
std::pair<ElementsMap::iterator, bool> entry;
entry.first = elements_map_.find(interleaved_vertex);
if (entry.first == elements_map_.end()) {
// append vertex data if this is a new element
if (vertices_size_) {
if (interleaved_buffer_.size()) {
memcpy(interleaved_buffer_.data() + vertices_offset_, interleaved_vertex.data(), interleaved_vertex.size());
} else {
GL_TRACE("glBufferSubData(GL_ARRAY_BUFFER, %d, %d, %p)", vertices_offset_ % interleaved_vertex.size() % interleaved_vertex.data());
GL_CHECKD(glBufferSubData(GL_ARRAY_BUFFER, vertices_offset_, interleaved_vertex.size(), interleaved_vertex.data()));
}
data()->clear();
}
vertices_offset_ += interleaved_vertex.size();
entry = elements_map_.emplace(interleaved_vertex, elements_map_.size());
} else {
data()->remove();
#if 0
if (OpenSCAD::debug != "") {
// in debug, check for bad hash matches
size_t i = 0;
if (interleaved_vertex.size() != entry.first->first.size()) {
PRINTDB("vertex index = %d", entry.first->second);
assert(false && "VBORenderer invalid vertex match size!!!");
}
for (const auto& b : interleaved_vertex) {
if (b != entry.first->first[i]) {
PRINTDB("vertex index = %d", entry.first->second);
assert(false && "VBORenderer invalid vertex value hash match!!!");
}
i++;
}
}
#endif // 0
}
// append element data
if (!elements_size_ || Feature::ExperimentalVxORenderersPrealloc.is_enabled()) {
addAttributeValues(*elementsData(), entry.first->second);
} else {
if (elementsData()->sizeofAttribute() == sizeof(GLubyte)) {
auto index = (GLubyte)entry.first->second;
GL_TRACE("glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, %d, %d, %p)", elements_offset_ % elementsData()->sizeofAttribute() % (void *)&index);
GL_CHECKD(glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, elements_offset_,
elementsData()->sizeofAttribute(),
&index));
} else if (elementsData()->sizeofAttribute() == sizeof(GLushort)) {
auto index = (GLushort)entry.first->second;
GL_TRACE("glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, %d, %d, %p)", elements_offset_ % elementsData()->sizeofAttribute() % (void *)&index);
GL_CHECKD(glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, elements_offset_,
elementsData()->sizeofAttribute(),
&index));
} else if (elementsData()->sizeofAttribute() == sizeof(GLuint)) {
auto index = (GLuint)entry.first->second;
GL_TRACE("glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, %d, %d, %p)", elements_offset_ % elementsData()->sizeofAttribute() % (void *)&index);
GL_CHECKD(glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, elements_offset_,
elementsData()->sizeofAttribute(),
&index));
} else {
assert(false && "create_vertex invalid index attribute size");
}
}
elements_offset_ += elementsData()->sizeofAttribute();
} else { // !useElements()
if (!vertices_size_) {
vertices_offset_ = sizeInBytes();
} else {
std::vector<GLbyte> interleaved_vertex;
interleaved_vertex.resize(data()->stride());
data()->getLastVertex(interleaved_vertex);
if (interleaved_buffer_.size()) {
memcpy(interleaved_buffer_.data() + vertices_offset_, interleaved_vertex.data(), interleaved_vertex.size());
} else {
// This path is chosen in vertex-object-renderers-direct mode
GL_TRACE("B glBufferSubData(GL_ARRAY_BUFFER, %d, %d, %p)", vertices_offset_ % interleaved_vertex.size() % interleaved_vertex.data());
GL_CHECKD(glBufferSubData(GL_ARRAY_BUFFER, vertices_offset_, interleaved_vertex.size(), interleaved_vertex.data()));
}
vertices_offset_ += interleaved_vertex.size();
data()->clear();
}
}
}
void VertexArray::createInterleavedVBOs()
{
for (const auto& state : states_) {
size_t index = state->drawOffset();
state->drawOffset(this->indexOffset(index));
}
// If the upfront size was not known, the the buffer has to be built
size_t total_size = this->sizeInBytes();
// If VertexArray is not empty, and initial size is zero
if (!vertices_size_ && total_size) {
GL_TRACE("glBindBuffer(GL_ARRAY_BUFFER, %d)", vertices_vbo_);
GL_CHECKD(glBindBuffer(GL_ARRAY_BUFFER, vertices_vbo_));
GL_TRACE("glBufferData(GL_ARRAY_BUFFER, %d, %p, GL_STATIC_DRAW)", total_size % (void *)nullptr);
GL_CHECKD(glBufferData(GL_ARRAY_BUFFER, total_size, nullptr, GL_STATIC_DRAW));
size_t dst_start = 0;
for (const auto& vertex_data : vertices_) {
// All attribute vectors need to be the same size to interleave
size_t idx = 0, last_size = 0, stride = vertex_data->stride();
for (const auto& data : vertex_data->attributes()) {
size_t size = data->sizeofAttribute();
const GLbyte *src = data->toBytes();
size_t dst = dst_start;
if (src) {
if (idx != 0) {
if (last_size != data->size() / data->count()) {
PRINTDB("attribute data for vertex incorrect size at index %d = %d", idx % (data->size() / data->count()));
PRINTDB("last_size = %d", last_size);
assert(false);
}
}
last_size = data->size() / data->count();
for (size_t i = 0; i < last_size; ++i) {
// This path is chosen in vertex-object-renderers non-direct mode
GL_TRACE("A glBufferSubData(GL_ARRAY_BUFFER, %p, %d, %p)", (void *)dst % size % (void *)src);
GL_CHECKD(glBufferSubData(GL_ARRAY_BUFFER, dst, size, src));
src += size;
dst += stride;
}
dst_start += size;
}
idx++;
}
dst_start = vertex_data->sizeInBytes();
}
GL_TRACE0("glBindBuffer(GL_ARRAY_BUFFER, 0)");
GL_CHECKD(glBindBuffer(GL_ARRAY_BUFFER, 0));
} else if (vertices_size_ && interleaved_buffer_.size()) {
GL_TRACE("glBindBuffer(GL_ARRAY_BUFFER, %d)", vertices_vbo_);
GL_CHECKD(glBindBuffer(GL_ARRAY_BUFFER, vertices_vbo_));
GL_TRACE("glBufferData(GL_ARRAY_BUFFER, %d, %p, GL_STATIC_DRAW)", interleaved_buffer_.size() % (void *)interleaved_buffer_.data());
GL_CHECKD(glBufferData(GL_ARRAY_BUFFER, interleaved_buffer_.size(), interleaved_buffer_.data(), GL_STATIC_DRAW));
GL_TRACE0("glBindBuffer(GL_ARRAY_BUFFER, 0)");
GL_CHECKD(glBindBuffer(GL_ARRAY_BUFFER, 0));
}
PRINTDB("useElements() = %d, elements_size_ = %d", useElements() % elements_size_);
if (useElements() && (!elements_size_ || Feature::ExperimentalVxORenderersPrealloc.is_enabled())) {
GL_TRACE("glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, %d)", elements_vbo_);
GL_CHECKD(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elements_vbo_));
if (!Feature::ExperimentalVxORenderersPrealloc.is_enabled()) {
GL_TRACE("glBufferData(GL_ELEMENT_ARRAY_BUFFER, %d, %p, GL_STATIC_DRAW)", elements_.sizeInBytes() % (void *)nullptr);
GL_CHECKD(glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements_.sizeInBytes(), nullptr, GL_STATIC_DRAW));
}
size_t last_size = 0;
for (const auto& e : elements_.attributes()) {
GL_TRACE("glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, %d, %d, %p)", last_size % e->sizeInBytes() % (void *)e->toBytes());
GL_CHECKD(glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, last_size, e->sizeInBytes(), e->toBytes()));
last_size += e->sizeInBytes();
}
GL_TRACE0("glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0)");
GL_CHECKD(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
}
}
void VertexArray::addAttributePointers(size_t start_offset)
{
if (!this->data()) return;
std::shared_ptr<VertexData> vertex_data = this->data();
std::shared_ptr<VertexState> vs = this->states().back();
GLsizei count = vertex_data->positionData()->count();
GLenum type = vertex_data->positionData()->glType();
GLsizei stride = vertex_data->stride();
size_t offset = start_offset + vertex_data->interleavedOffset(vertex_data->positionIndex());
// Note: Some code, like OpenCSGRenderer::createVBOPrimitive() relies on this order of
// glBegin/glEnd functions for unlit/uncolored vertex rendering.
vs->glBegin().emplace_back([]() {
GL_TRACE0("glEnableClientState(GL_VERTEX_ARRAY)");
GL_CHECKD(glEnableClientState(GL_VERTEX_ARRAY));
});
vs->glBegin().emplace_back([count, type, stride, offset, vs_ptr = std::weak_ptr<VertexState>(vs)]() {
auto vs = vs_ptr.lock();
if (vs) {
// NOLINTBEGIN(performance-no-int-to-ptr)
GL_TRACE("glVertexPointer(%d, %d, %d, %p)",
count % type % stride % (GLvoid *)(vs->drawOffset() + offset));
GL_CHECKD(glVertexPointer(count, type, stride, (GLvoid *)(vs->drawOffset() + offset)));
// NOLINTEND(performance-no-int-to-ptr)
}
});
vs->glEnd().emplace_back([]() {
GL_TRACE0("glDisableClientState(GL_VERTEX_ARRAY)");
GL_CHECKD(glDisableClientState(GL_VERTEX_ARRAY));
});
if (vertex_data->hasNormalData()) {
type = vertex_data->normalData()->glType();
size_t offset = start_offset + vertex_data->interleavedOffset(vertex_data->normalIndex());
vs->glBegin().emplace_back([]() {
GL_TRACE0("glEnableClientState(GL_NORMAL_ARRAY)");
GL_CHECKD(glEnableClientState(GL_NORMAL_ARRAY));
});
vs->glBegin().emplace_back([type, stride, offset, vs_ptr = std::weak_ptr<VertexState>(vs)]() {
auto vs = vs_ptr.lock();
if (vs) {
// NOLINTBEGIN(performance-no-int-to-ptr)
GL_TRACE("glNormalPointer(%d, %d, %p)", type % stride % (GLvoid *)(vs->drawOffset() + offset));
GL_CHECKD(glNormalPointer(type, stride, (GLvoid *)(vs->drawOffset() + offset)));
// NOLINTEND(performance-no-int-to-ptr)
}
});
vs->glEnd().emplace_back([]() {
GL_TRACE0("glDisableClientState(GL_NORMAL_ARRAY)");
GL_CHECKD(glDisableClientState(GL_NORMAL_ARRAY));
});
}
if (vertex_data->hasColorData()) {
count = vertex_data->colorData()->count();
type = vertex_data->colorData()->glType();
size_t offset = start_offset + vertex_data->interleavedOffset(vertex_data->colorIndex());
vs->glBegin().emplace_back([]() {
GL_TRACE0("glEnableClientState(GL_COLOR_ARRAY)");
GL_CHECKD(glEnableClientState(GL_COLOR_ARRAY));
});
vs->glBegin().emplace_back([count, type, stride, offset, vs_ptr = std::weak_ptr<VertexState>(vs)]() {
auto vs = vs_ptr.lock();
if (vs) {
// NOLINTBEGIN(performance-no-int-to-ptr)
GL_TRACE("glColorPointer(%d, %d, %d, %p)", count % type % stride % (GLvoid *)(vs->drawOffset() + offset));
GL_CHECKD(glColorPointer(count, type, stride, (GLvoid *)(vs->drawOffset() + offset)));
// NOLINTEND(performance-no-int-to-ptr)
}
});
vs->glEnd().emplace_back([]() {
GL_TRACE0("glDisableClientState(GL_COLOR_ARRAY)");
GL_CHECKD(glDisableClientState(GL_COLOR_ARRAY));
});
}
}
// Allocates GPU memory for vertices (and elements if enabled)
// for holding the given number of vertices.
void VertexArray::allocateBuffers(size_t num_vertices) {
size_t vertices_size = num_vertices * stride();
setVerticesSize(vertices_size);
GL_TRACE("glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, %d)", vertices_vbo_);
GL_CHECKD(glBindBuffer(GL_ARRAY_BUFFER, vertices_vbo_));
GL_TRACE("glBufferData(GL_ARRAY_BUFFER, %d, %p, GL_STATIC_DRAW)", vertices_size % (void *)nullptr);
GL_CHECKD(glBufferData(GL_ARRAY_BUFFER, vertices_size, nullptr, GL_STATIC_DRAW));
if (Feature::ExperimentalVxORenderersIndexing.is_enabled()) {
// Use smallest possible index data type
if (num_vertices <= 0xff) {
addElementsData(std::make_shared<AttributeData<GLubyte, 1, GL_UNSIGNED_BYTE>>());
} else if (num_vertices <= 0xffff) {
addElementsData(std::make_shared<AttributeData<GLushort, 1, GL_UNSIGNED_SHORT>>());
} else {
addElementsData(std::make_shared<AttributeData<GLuint, 1, GL_UNSIGNED_INT>>());
}
// FIXME: How do we know how much to allocate?
size_t elements_size = num_vertices * elements_.stride();
setElementsSize(elements_size);
GL_TRACE("glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, %d)", elements_vbo_);
GL_CHECKD(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elements_vbo_));
GL_TRACE("glBufferData(GL_ELEMENT_ARRAY_BUFFER, %d, %p, GL_STATIC_DRAW)", elements_size % (void *)nullptr);
GL_CHECKD(glBufferData(GL_ELEMENT_ARRAY_BUFFER, elements_size, nullptr, GL_STATIC_DRAW));
}
}