/
WindingRenderer.h
369 lines (289 loc) · 11.8 KB
/
WindingRenderer.h
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
361
362
363
364
365
366
367
368
369
#pragma once
#include "igl.h"
#include <limits>
#include "iwindingrenderer.h"
#include "CompactWindingVertexBuffer.h"
#include "debugging/gl.h"
namespace render
{
class IBackendWindingRenderer :
public IWindingRenderer
{
public:
virtual ~IBackendWindingRenderer()
{}
// Returns true if the vertex buffers are empty
virtual bool empty() const = 0;
// Issues the openGL calls to render the vertex buffers
virtual void renderAllWindings() = 0;
};
// Traits class to retrieve the GLenum render mode based on the indexer type
template<typename IndexerT> struct RenderingTraits
{};
template<>
struct RenderingTraits<WindingIndexer_Lines>
{
constexpr static GLenum Mode() { return GL_LINES; }
};
template<>
struct RenderingTraits<WindingIndexer_Triangles>
{
constexpr static GLenum Mode() { return GL_TRIANGLES; }
};
template<>
struct RenderingTraits<WindingIndexer_Polygon>
{
constexpr static GLenum Mode() { return GL_POLYGON; }
};
template<class WindingIndexerT>
class WindingRenderer :
public IBackendWindingRenderer
{
private:
using VertexBuffer = CompactWindingVertexBuffer<ArbitraryMeshVertex, WindingIndexerT>;
struct Bucket
{
Bucket(std::size_t size) :
buffer(size)
{}
VertexBuffer buffer;
std::vector<typename VertexBuffer::Slot> pendingDeletions;
};
// Maintain one bucket per winding size, allocated on demand
std::vector<Bucket> _buckets;
using BucketIndex = std::uint16_t;
static constexpr BucketIndex InvalidBucketIndex = std::numeric_limits<BucketIndex>::max();
static constexpr typename VertexBuffer::Slot InvalidVertexBufferSlot = std::numeric_limits<typename VertexBuffer::Slot>::max();
// Stores the indices to a winding slot into a bucket, client code receives an index to a SlotMapping
struct SlotMapping
{
BucketIndex bucketIndex = InvalidBucketIndex;
typename VertexBuffer::Slot slotNumber = InvalidVertexBufferSlot;
};
std::vector<SlotMapping> _slots;
static constexpr std::size_t InvalidSlotMapping = std::numeric_limits<std::size_t>::max();
std::size_t _freeSlotMappingHint;
std::size_t _windings;
public:
WindingRenderer() :
_windings(0),
_freeSlotMappingHint(InvalidSlotMapping)
{}
bool empty() const
{
return _windings == 0;
}
Slot addWinding(const std::vector<ArbitraryMeshVertex>& vertices) override
{
auto windingSize = vertices.size();
if (windingSize >= std::numeric_limits<BucketIndex>::max()) throw std::logic_error("Winding too large");
// Get the Bucket this Slot is referring to
auto bucketIndex = getBucketIndexForWindingSize(windingSize);
auto& bucket = ensureBucketForWindingSize(windingSize);
// Allocate a new slot descriptor, we can't hand out absolute indices to clients
auto slotMappingIndex = allocateSlotMapping();
auto& slotMapping = _slots[slotMappingIndex];
slotMapping.bucketIndex = bucketIndex;
// Check if we have a free slot in this buffer (marked for deletion)
if (!bucket.pendingDeletions.empty())
{
slotMapping.slotNumber = bucket.pendingDeletions.back();
bucket.pendingDeletions.pop_back();
// Use the replace method to load the data
bucket.buffer.replaceWinding(slotMapping.slotNumber, vertices);
}
else
{
// No deleted slot available, allocate a new one
slotMapping.slotNumber = bucket.buffer.pushWinding(vertices);
}
++_windings;
return slotMappingIndex;
}
void removeWinding(Slot slot) override
{
assert(slot < _slots.size());
auto& slotMapping = _slots[slot];
auto bucketIndex = slotMapping.bucketIndex;
assert(bucketIndex != InvalidBucketIndex);
#if 1
// Mark this winding slot as pending for deletion
_buckets[bucketIndex].pendingDeletions.push_back(slotMapping.slotNumber);
#else
// Remove the winding from the bucket
_buckets[bucketIndex].removeWinding(slotMapping.slotNumber);
// Update the value in other slot mappings, now that the bucket shrunk
for (auto& mapping : _slots)
{
// Every index in the same bucket beyond the removed winding needs to be shifted to left
if (mapping.bucketIndex == bucketIndex && mapping.slotNumber > slotMapping.slotNumber)
{
--mapping.slotNumber;
}
}
#endif
// Invalidate the slot mapping
slotMapping.bucketIndex = InvalidBucketIndex;
slotMapping.slotNumber = InvalidVertexBufferSlot;
// Update the free slot hint, for the next round we allocate one
if (slot < _freeSlotMappingHint)
{
_freeSlotMappingHint = slot;
}
--_windings;
}
void updateWinding(Slot slot, const std::vector<ArbitraryMeshVertex>& vertices) override
{
assert(slot < _slots.size());
auto& slotMapping = _slots[slot];
assert(slotMapping.bucketIndex != InvalidBucketIndex);
auto& bucket = _buckets[slotMapping.bucketIndex];
if (bucket.buffer.getWindingSize() != vertices.size())
{
throw std::logic_error("Winding size changes are not supported through updateWinding.");
}
bucket.buffer.replaceWinding(slotMapping.slotNumber, vertices);
}
void renderAllWindings() override
{
for (auto bucketIndex = 0; bucketIndex < _buckets.size(); ++bucketIndex)
{
auto& bucket = _buckets[bucketIndex];
commitDeletions(bucketIndex);
if (bucket.buffer.getVertices().empty()) continue;
const auto& vertices = bucket.buffer.getVertices();
const auto& indices = bucket.buffer.getIndices();
glDisableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_DOUBLE, sizeof(ArbitraryMeshVertex), &vertices.front().vertex);
glTexCoordPointer(2, GL_DOUBLE, sizeof(ArbitraryMeshVertex), &vertices.front().texcoord);
glNormalPointer(GL_DOUBLE, sizeof(ArbitraryMeshVertex), &vertices.front().normal);
auto primitiveMode = RenderingTraits<WindingIndexerT>::Mode();
glDrawElements(primitiveMode, static_cast<GLsizei>(indices.size()), GL_UNSIGNED_INT, &indices.front());
debug::checkGLErrors();
}
}
void renderWinding(IWindingRenderer::RenderMode mode, IWindingRenderer::Slot slot) override
{
assert(slot < _slots.size());
auto& slotMapping = _slots[slot];
assert(slotMapping.bucketIndex != InvalidBucketIndex);
auto& bucket = _buckets[slotMapping.bucketIndex];
commitDeletions(slotMapping.bucketIndex);
const auto& vertices = bucket.buffer.getVertices();
const auto& indices = bucket.buffer.getIndices();
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glFrontFace(GL_CCW);
glVertexPointer(3, GL_DOUBLE, sizeof(ArbitraryMeshVertex), &vertices.front().vertex);
glTexCoordPointer(2, GL_DOUBLE, sizeof(ArbitraryMeshVertex), &vertices.front().texcoord);
glNormalPointer(GL_DOUBLE, sizeof(ArbitraryMeshVertex), &vertices.front().normal);
if (mode == IWindingRenderer::RenderMode::Triangles)
{
renderElements<WindingIndexer_Triangles>(bucket.buffer, slotMapping.slotNumber);
}
else if (mode == IWindingRenderer::RenderMode::Polygon)
{
renderElements<WindingIndexer_Polygon>(bucket.buffer, slotMapping.slotNumber);
}
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
private:
void commitDeletions(std::size_t bucketIndex)
{
auto& bucket = _buckets[bucketIndex];
if (bucket.pendingDeletions.empty()) return;
std::sort(bucket.pendingDeletions.begin(), bucket.pendingDeletions.end());
#if 1
// Remove the winding from the bucket
bucket.buffer.removeWindings(bucket.pendingDeletions);
// A mapping to quickly know which mapping has been shifted by how many positions
std::map<typename VertexBuffer::Slot, typename VertexBuffer::Slot> offsets;
auto offsetToApply = 0;
for (auto removed : bucket.pendingDeletions)
{
offsets[removed] = offsetToApply++;
}
auto maxOffsetToApply = offsetToApply;
for (auto& mapping : _slots)
{
// Every index in the same bucket beyond the first removed winding needs to be shifted
if (mapping.bucketIndex != bucketIndex || mapping.slotNumber == InvalidVertexBufferSlot)
{
continue;
}
// lower_bound yields the item that is equal to or larger
auto offset = offsets.lower_bound(mapping.slotNumber);
if (offset != offsets.end())
{
mapping.slotNumber -= offset->second;
}
else
{
mapping.slotNumber -= maxOffsetToApply;
}
}
#else
for (auto s = bucket.pendingDeletions.rbegin(); s != bucket.pendingDeletions.rend(); ++s)
{
auto slotNumber = *s;
// Remove the winding from the bucket
bucket.buffer.removeWinding(slotNumber);
// Update the value in other slot mappings, now that the bucket shrank
for (auto& mapping : _slots)
{
// Every index in the same bucket beyond the removed winding needs to be shifted to left
if (mapping.bucketIndex == bucketIndex && mapping.slotNumber > slotNumber)
{
--mapping.slotNumber;
}
}
}
#endif
bucket.pendingDeletions.clear();
}
template<class CustomWindingIndexerT>
void renderElements(const VertexBuffer& buffer, typename VertexBuffer::Slot slotNumber) const
{
std::vector<unsigned int> indices;
indices.reserve(CustomWindingIndexerT::GetNumberOfIndicesPerWinding(buffer.getWindingSize()));
auto firstVertex = static_cast<unsigned int>(buffer.getWindingSize() * slotNumber);
CustomWindingIndexerT::GenerateAndAssignIndices(std::back_inserter(indices), buffer.getWindingSize(), firstVertex);
auto mode = RenderingTraits<CustomWindingIndexerT>::Mode();
glDrawElements(mode, static_cast<GLsizei>(indices.size()), GL_UNSIGNED_INT, &indices.front());
}
IWindingRenderer::Slot allocateSlotMapping()
{
auto numSlots = static_cast<IWindingRenderer::Slot>(_slots.size());
for (auto i = _freeSlotMappingHint; i < numSlots; ++i)
{
if (_slots[i].bucketIndex == InvalidBucketIndex)
{
_freeSlotMappingHint = i + 1; // start searching here next time
return i;
}
}
_slots.emplace_back();
return numSlots; // == the size before we emplaced the new slot
}
inline static BucketIndex getBucketIndexForWindingSize(std::size_t windingSize)
{
// Since there are no windings with sizes 0, 1, 2, we can deduct 3 to get the bucket index
if (windingSize < 3) throw std::logic_error("No winding sizes < 3 are supported");
return static_cast<BucketIndex>(windingSize - 3);
}
Bucket& ensureBucketForWindingSize(std::size_t windingSize)
{
auto bucketIndex = getBucketIndexForWindingSize(windingSize);
// Keep adding buckets until we have the matching one
while (bucketIndex >= _buckets.size())
{
auto nextWindingSize = _buckets.size() + 3;
_buckets.emplace_back(nextWindingSize);
}
return _buckets.at(bucketIndex);
}
};
}