/
GeometryStore.cpp
468 lines (357 loc) · 14.8 KB
/
GeometryStore.cpp
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include "gtest/gtest.h"
#include <limits>
#include <random>
#include "render/GeometryStore.h"
namespace test
{
class NullSyncObjectProvider final :
public render::ISyncObjectProvider
{
public:
std::size_t invocationCount = 0;
render::ISyncObject::Ptr createSyncObject() override
{
++invocationCount;
return {};
}
static NullSyncObjectProvider& Instance()
{
static NullSyncObjectProvider _instance;
return _instance;
}
};
namespace
{
inline MeshVertex createNthVertex(int n, int id, std::size_t size)
{
auto offset = static_cast<double>(n + size * id);
return MeshVertex(
{ offset + 0.0, offset + 0.5, offset + 0.3 },
{ 0, 0, offset + 0.0 },
{ offset + 0.0, -offset + 0.0 }
);
}
inline std::vector<MeshVertex> generateVertices(int id, std::size_t size)
{
std::vector<MeshVertex> vertices;
for (int i = 0; i < size; ++i)
{
vertices.emplace_back(createNthVertex(i, id, size));
}
return vertices;
}
// Generates 3 indices per vertex, without any special meaning
inline std::vector<unsigned int> generateIndices(const std::vector<MeshVertex>& vertices)
{
std::vector<unsigned int> indices;
for (int i = 0; i < vertices.size(); ++i)
{
indices.emplace_back(i);
indices.emplace_back(static_cast<unsigned int>((i + 1) % vertices.size()));
indices.emplace_back(static_cast<unsigned int>((i + 2) % vertices.size()));
}
return indices;
}
inline void verifyAllocation(render::IGeometryStore& store, render::IGeometryStore::Slot slot,
const std::vector<MeshVertex>& vertices, const std::vector<unsigned int>& indices)
{
auto renderParms = store.getRenderParameters(slot);
auto expectedIndex = indices.begin();
auto firstVertex = renderParms.bufferStart + renderParms.firstVertex;
EXPECT_EQ(renderParms.indexCount, indices.size()) << "Index count mismatch";
for (auto idxPtr = renderParms.firstIndex; idxPtr < renderParms.firstIndex + renderParms.indexCount; ++idxPtr)
{
auto index = *idxPtr;
EXPECT_EQ(index, *expectedIndex) << "Index disorder";
// Pick the vertex from our local expectation
const auto& expectedVertex = vertices.at(index);
// Pick the vertex from the stored set
const auto& vertex = *(firstVertex + index);
EXPECT_TRUE(math::isNear(vertex.vertex, expectedVertex.vertex, 0.01)) << "Vertex data mismatch";
EXPECT_TRUE(math::isNear(vertex.texcoord, expectedVertex.texcoord, 0.01)) << "Texcoord data mismatch";
EXPECT_TRUE(math::isNear(vertex.normal, expectedVertex.normal, 0.01)) << "Texcoord data mismatch";
++expectedIndex;
}
}
struct Allocation
{
render::IGeometryStore::Slot slot;
std::vector<MeshVertex> vertices;
std::vector<unsigned int> indices;
bool operator<(const Allocation& other) const
{
return slot < other.slot;
}
};
inline void verifyAllAllocations(render::IGeometryStore& store, const std::vector<Allocation>& allocations)
{
for (auto allocation : allocations)
{
verifyAllocation(store, allocation.slot, allocation.vertices, allocation.indices);
}
}
}
TEST(GeometryStore, AllocateAndDeallocate)
{
render::GeometryStore store(NullSyncObjectProvider::Instance());
std::vector<render::IGeometryStore::Slot> allocatedSlots;
// Allocate 10 slots of various sizes
for (auto i = 0; i < 10; ++i)
{
auto slot = store.allocateSlot((i + 5) * 20, (i + 5) * 23);
EXPECT_NE(slot, std::numeric_limits<render::IGeometryStore::Slot>::max()) << "Invalid slot";
allocatedSlots.push_back(slot);
}
for (auto slot : allocatedSlots)
{
EXPECT_NO_THROW(store.deallocateSlot(slot));
}
}
TEST(GeometryStore, UpdateData)
{
render::GeometryStore store(NullSyncObjectProvider::Instance());
std::set<Allocation> allocations;
// Allocate 10 slots of various sizes, store some data in there
for (auto i = 0; i < 10; ++i)
{
auto vertices = generateVertices(i, (i + 5) * 20);
auto indices = generateIndices(vertices);
auto slot = store.allocateSlot(vertices.size(), indices.size());
EXPECT_NE(slot, std::numeric_limits<render::IGeometryStore::Slot>::max()) << "Invalid slot";
// Uploading the data should succeed
EXPECT_NO_THROW(store.updateData(slot, vertices, indices));
allocations.emplace(Allocation{ slot, vertices, indices });
// Verify the data after each allocation, it should not affect the others
for (auto allocation : allocations)
{
verifyAllocation(store, allocation.slot, allocation.vertices, allocation.indices);
}
}
// Verify the data
for (auto allocation : allocations)
{
verifyAllocation(store, allocation.slot, allocation.vertices, allocation.indices);
}
// Now de-allocate one slot after the other and verify the remaining ones
while (!allocations.empty())
{
auto slot = allocations.begin()->slot;
allocations.erase(allocations.begin());
EXPECT_NO_THROW(store.deallocateSlot(slot));
// Verify the remaining slots, they should still be intact
for (auto allocation : allocations)
{
verifyAllocation(store, allocation.slot, allocation.vertices, allocation.indices);
}
}
}
TEST(GeometryStore, UpdateSubData)
{
render::GeometryStore store(NullSyncObjectProvider::Instance());
std::set<Allocation> allocations;
// Allocate 10 slots of various sizes, store some data in there
auto margin = 13;
for (auto i = 0; i < 10; ++i)
{
auto vertices = generateVertices(13, 17 * 20);
auto indices = generateIndices(vertices);
auto slot = store.allocateSlot(vertices.size() + margin, indices.size() + margin);
EXPECT_NE(slot, std::numeric_limits<render::IGeometryStore::Slot>::max()) << "Invalid slot";
// We locally keep track of what the data should look like in the store
std::vector<MeshVertex> localVertexCopy(vertices.size());
std::vector<unsigned int> localIndexCopy(indices.size());
// Upload part of the data (with some increasing offset)
for (auto offset = 0; offset < margin; ++offset)
{
EXPECT_NO_THROW(store.updateSubData(slot, offset, vertices, offset, indices));
// Update our local copy accordingly
localVertexCopy.resize(vertices.size() + offset);
localIndexCopy.resize(indices.size() + offset);
std::copy(vertices.begin(), vertices.end(), localVertexCopy.begin() + offset);
std::copy(indices.begin(), indices.end(), localIndexCopy.begin() + offset);
verifyAllocation(store, slot, localVertexCopy, localIndexCopy);
}
// Finally, upload the whole data
store.updateData(slot, vertices, indices);
allocations.emplace(Allocation{ slot, vertices, indices });
// Verify the data after each round, it should not affect the other data
for (auto allocation : allocations)
{
verifyAllocation(store, allocation.slot, allocation.vertices, allocation.indices);
}
}
// Verify the data
for (auto allocation : allocations)
{
verifyAllocation(store, allocation.slot, allocation.vertices, allocation.indices);
}
// Now de-allocate one slot after the other and verify the remaining ones
while (!allocations.empty())
{
auto slot = allocations.begin()->slot;
allocations.erase(allocations.begin());
EXPECT_NO_THROW(store.deallocateSlot(slot));
// Verify the remaining slots, they should still be intact
for (auto allocation : allocations)
{
verifyAllocation(store, allocation.slot, allocation.vertices, allocation.indices);
}
}
}
TEST(GeometryStore, ResizeData)
{
render::GeometryStore store(NullSyncObjectProvider::Instance());
// Allocate a few dummy slots
store.allocateSlot(17, 27);
store.allocateSlot(31, 67);
store.allocateSlot(5, 37);
// Generate an indexed vertex set
auto vertices = generateVertices(13, 17 * 20);
auto indices = generateIndices(vertices);
auto slot = store.allocateSlot(vertices.size(), indices.size());
EXPECT_NE(slot, std::numeric_limits<render::IGeometryStore::Slot>::max()) << "Invalid slot";
// Store everything into the buffer
store.updateData(slot, vertices, indices);
// We locally keep track of what the data should look like in the store
std::vector<MeshVertex> localVertexCopy = vertices;
std::vector<unsigned int> localIndexCopy = indices;
// Reduce the data in the allocation, step by step
auto newVertexSize = localVertexCopy.size();
auto newIndexSize = localIndexCopy.size();
auto steps = std::min(newIndexSize, newVertexSize);
EXPECT_GT(steps, 4) << "Too few data elements";
steps -= 4;
for (auto i = 0; i < steps; ++i)
{
// Cut off one index at the end
// Keep the vertex buffer intact, we don't want out-of-bounds errors
localIndexCopy.resize(localIndexCopy.size() - 1);
--newVertexSize;
EXPECT_NO_THROW(store.resizeData(slot, newVertexSize, localIndexCopy.size()));
verifyAllocation(store, slot, localVertexCopy, localIndexCopy);
}
}
TEST(GeometryStore, FrameBufferSwitching)
{
render::GeometryStore store(NullSyncObjectProvider::Instance());
store.onFrameStart();
std::vector<Allocation> allocations;
// Allocate 10 slots of various sizes, store some data in there
for (auto i = 0; i < 10; ++i)
{
auto vertices = generateVertices(i, (i + 5) * 20);
auto indices = generateIndices(vertices);
auto slot = store.allocateSlot(vertices.size(), indices.size());
EXPECT_NE(slot, std::numeric_limits<render::IGeometryStore::Slot>::max()) << "Invalid slot";
// Uploading the data should succeed
EXPECT_NO_THROW(store.updateData(slot, vertices, indices));
allocations.emplace_back(Allocation{ slot, vertices, indices });
}
// Verify all
verifyAllAllocations(store, allocations);
store.onFrameFinished();
// Begin a new frame, the data in the new buffer should be up to date
store.onFrameStart();
verifyAllAllocations(store, allocations);
store.onFrameFinished();
auto dataUpdates = 0;
auto subDataUpdates = 0;
auto dataResizes = 0;
auto allocationCount = 0;
auto deallocationCount = 0;
std::minstd_rand rand(17); // fixed seed
// Run a few updates
for (auto frame = 0; frame < 100; ++frame)
{
store.onFrameStart();
// Verify all allocations at the start of every frame
verifyAllAllocations(store, allocations);
// Do something random with every allocation
for (auto a = 0; a < allocations.size(); ++a)
{
auto& allocation = allocations[a];
// Perform a random action
switch (rand() % 7)
{
case 1: // updateSubData
{
subDataUpdates++;
// Update 50% of the data
auto newVertices = generateVertices(rand() % 9, allocation.vertices.size() >> 2);
auto newIndices = generateIndices(newVertices);
// Overwrite some of the data
std::copy(newVertices.begin(), newVertices.end(), allocation.vertices.begin());
std::copy(newIndices.begin(), newIndices.end(), allocation.indices.begin());
store.updateSubData(allocation.slot, 0, newVertices, 0, newIndices);
break;
}
case 2: // updateData
{
dataUpdates++;
allocation.vertices = generateVertices(rand() % 9, allocation.vertices.size());
allocation.indices = generateIndices(allocation.vertices);
store.updateData(allocation.slot, allocation.vertices, allocation.indices);
break;
}
case 3: // resize
{
dataResizes++;
// Don't touch vertices below a minimum size
if (allocation.vertices.size() < 10) break;
// Allow 10% shrinking of the data
auto newSize = allocation.vertices.size() - (rand() % (allocation.vertices.size() / 10));
allocation.vertices.resize(newSize);
allocation.indices = generateIndices(allocation.vertices);
store.resizeData(allocation.slot, allocation.vertices.size(), allocation.indices.size());
// after resize, we have to update the data too, unfortunately, otherwise the indices are out of bounds
store.updateData(allocation.slot, allocation.vertices, allocation.indices);
break;
}
case 4: // allocations
{
allocationCount++;
auto vertices = generateVertices(rand() % 9, rand() % 100);
auto indices = generateIndices(vertices);
auto slot = store.allocateSlot(vertices.size(), indices.size());
EXPECT_NE(slot, std::numeric_limits<render::IGeometryStore::Slot>::max()) << "Invalid slot";
EXPECT_NO_THROW(store.updateData(slot, vertices, indices));
allocations.emplace_back(Allocation{ slot, vertices, indices });
break;
}
case 5: // dellocation
{
deallocationCount++;
store.deallocateSlot(allocations[a].slot);
allocations.erase(allocations.begin() + a);
// We're going to skip one loop iteration, but that's not very important
break;
}
} // switch
}
// Verify all allocations at the end of every frame
verifyAllAllocations(store, allocations);
store.onFrameFinished();
}
// One final check
store.onFrameStart();
verifyAllAllocations(store, allocations);
store.onFrameFinished();
EXPECT_GT(dataUpdates, 0) << "No data update operations performed";
EXPECT_GT(subDataUpdates, 0) << "No sub data update operations performed";
EXPECT_GT(dataResizes, 0) << "No resize operations performed";
EXPECT_GT(allocationCount, 0) << "No allocation operations performed";
EXPECT_GT(deallocationCount, 0) << "No deallocation operations performed";
}
TEST(GeometryStore, SyncObjectAcquisition)
{
render::GeometryStore store(NullSyncObjectProvider::Instance());
NullSyncObjectProvider::Instance().invocationCount = 0;
for (int i = 0; i < 5; ++i)
{
store.onFrameStart();
store.onFrameFinished();
}
EXPECT_EQ(NullSyncObjectProvider::Instance().invocationCount, 5) <<
"GeometryStore should have performed 5 frame buffer switches";
}
}