File: WindingRendering.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (632 lines) | stat: -rw-r--r-- 25,087 bytes parent folder | download | duplicates (2)
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
#include "gtest/gtest.h"
#include "RadiantTest.h"
#include "scenelib.h"
#include "render/MeshVertex.h"
#include "render/CompactWindingVertexBuffer.h"
#include "render/WindingRenderer.h"
#include "render/GeometryStore.h"
#include "testutil/RenderUtils.h"
#include "testutil/TestBufferObjectProvider.h"
#include "testutil/TestObjectRenderer.h"
#include "testutil/TestSyncObjectProvider.h"
#include "algorithm/Entity.h"

namespace test
{

using WindingRendererTest = RadiantTest;

// Local test implementations of renderer interfaces
namespace
{
    TestBufferObjectProvider bufferObjectProvider;
    TestSyncObjectProvider syncObjectProvider;
    TestObjectRenderer testObjectRenderer;
}

constexpr int SmallestWindingSize = 3;
constexpr int LargestWindingSize = 12;

using VertexBuffer = render::CompactWindingVertexBuffer<MeshVertex>;

inline MeshVertex createNthVertexOfWinding(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> createWinding(int id, std::size_t size)
{
    std::vector<MeshVertex> winding;

    for (int i = 0; i < size; ++i)
    {
        winding.emplace_back(createNthVertexOfWinding(i, id, size));
    }

    return winding;
}

inline void checkWindingIndices(const VertexBuffer& buffer, VertexBuffer::Slot slot)
{
    // Slot must be within range
    auto windingSize = buffer.getWindingSize();

    auto numWindingsInBuffer = buffer.getVertices().size() / windingSize;
    EXPECT_LT(slot, numWindingsInBuffer) << "Slot out of bounds";

    // Assume the indices are within bounds
    auto indexStart = buffer.getNumIndicesPerWinding() * slot;
    EXPECT_LE(indexStart + buffer.getNumIndicesPerWinding(), buffer.getIndices().size());

    // Check the indices, they must be referencing vertices in that slot
    for (auto i = indexStart; i < indexStart + buffer.getNumIndicesPerWinding(); ++i)
    {
        auto index = buffer.getIndices().at(i);
        EXPECT_GE(index, slot * windingSize) << "Winding index out of lower bounds";
        EXPECT_LT(index, (slot + 1) * windingSize) << "Winding index out of upper bounds";
    }
}

inline void checkWindingDataInSlot(const VertexBuffer& buffer, VertexBuffer::Slot slot, int expectedId)
{
    // Slot must be within range
    auto windingSize = buffer.getWindingSize();

    auto numWindingsInBuffer = buffer.getVertices().size() / windingSize;
    EXPECT_LT(slot, numWindingsInBuffer) << "Slot out of bounds";

    for (auto i = 0; i < windingSize; ++i)
    {
        auto position = slot * windingSize + i;

        auto expectedVertex = createNthVertexOfWinding(i, expectedId, windingSize);
        auto vertex = buffer.getVertices().at(position);

        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";
    }
}

TEST(CompactWindingVertexBuffer, NumIndicesPerWinding)
{
    for (auto i = 0; i < 10; ++i)
    {
        VertexBuffer buffer(i);
        EXPECT_EQ(buffer.getWindingSize(), i);
        EXPECT_EQ(buffer.getNumIndicesPerWinding(), 3 * (buffer.getWindingSize() - 2));
    }
}

TEST(CompactWindingVertexBuffer, AddSingleWinding)
{
    for (auto size = SmallestWindingSize; size < LargestWindingSize; ++size)
    {
        VertexBuffer buffer(size);

        auto winding1 = createWinding(1, size);
        auto slot = buffer.pushWinding(winding1);

        EXPECT_EQ(slot, 0) << "Wrong slot assignment";
        EXPECT_EQ(buffer.getVertices().size(), size);
        EXPECT_EQ(buffer.getIndices().size(), buffer.getNumIndicesPerWinding());

        // Assume that the indices have been correctly calculated
        checkWindingIndices(buffer, slot);
        checkWindingDataInSlot(buffer, slot, 1); // ID is the same as in createWinding
    }
}

TEST(CompactWindingVertexBuffer, AddMultipleWindings)
{
    for (auto size = SmallestWindingSize; size < LargestWindingSize; ++size)
    {
        VertexBuffer buffer(size);

        // Add 10 windings to the buffer
        for (auto n = 1; n <= 10; ++n)
        {
            auto winding1 = createWinding(n, size);
            auto slot = buffer.pushWinding(winding1);

            EXPECT_EQ(slot, n-1) << "Unexpected slot assignment";
            EXPECT_EQ(buffer.getVertices().size(), n * size);
            EXPECT_EQ(buffer.getIndices().size(), n * buffer.getNumIndicesPerWinding());

            // Assume that the indices have been correctly calculated
            checkWindingIndices(buffer, slot);
            checkWindingDataInSlot(buffer, slot, n); // ID is the same as in createWinding
        }
    }
}

TEST(CompactWindingVertexBuffer, RemoveOneWinding)
{
    for (auto size = SmallestWindingSize; size < LargestWindingSize; ++size)
    {
        // We will work with a buffer containing 13 windings,
        // the test will remove a single winding from every possible position
        constexpr auto NumWindings = 13;

        for (int slotToRemove = 0; slotToRemove < NumWindings; ++slotToRemove)
        {
            VertexBuffer buffer(size);

            // Add the desired number of windings to the buffer
            for (auto n = 1; n <= NumWindings; ++n)
            {
                buffer.pushWinding(createWinding(n, size));
            }

            // Remove a winding from the slot, this should move all
            // windings in greater slot numbers towards the left
            buffer.removeWinding(slotToRemove);

            // Check the resized vectors
            EXPECT_EQ(buffer.getVertices().size(), (NumWindings - 1) * buffer.getWindingSize()) << "Vertex array not resized";
            EXPECT_EQ(buffer.getIndices().size(), (NumWindings - 1) * buffer.getNumIndicesPerWinding()) << "Index array not resized";

            // All winding indices must still be correct
            auto remainingSlots = NumWindings - 1;
            for (auto slot = 0; slot < remainingSlots; ++slot)
            {
                checkWindingIndices(buffer, slot);

                // We expect the winding vertex data to be unchanged if the slot has not been touched
                auto id = slot + 1;
                checkWindingDataInSlot(buffer, slot, slot < slotToRemove ? id : id + 1);
            }
        }
    }
}

TEST(CompactWindingVertexBuffer, RemoveNonExistentSlot)
{
    VertexBuffer buffer(4);

    // Add a few windings to the buffer
    for (auto n = 1; n <= 20; ++n)
    {
        auto winding1 = createWinding(n, 4);
        buffer.pushWinding(winding1);
    }

    // Pass a non-existent slot number to removeWinding(), it should throw
    auto numSlots = static_cast<VertexBuffer::Slot>(buffer.getVertices().size() / buffer.getWindingSize());

    EXPECT_THROW(buffer.removeWinding(numSlots), std::logic_error);
    EXPECT_THROW(buffer.removeWinding(numSlots + 1), std::logic_error);
    EXPECT_THROW(buffer.removeWinding(numSlots + 200), std::logic_error);
}

TEST(CompactWindingVertexBuffer, ReplaceWinding)
{
    for (auto size = SmallestWindingSize; size < LargestWindingSize; ++size)
    {
        // We will work with a buffer containing 13 windings,
        // the test will replace a single winding from every possible position
        constexpr auto NumWindings = 13;
        constexpr auto IdForReplacement = NumWindings * 2;

        for (int slotToReplace = 0; slotToReplace < NumWindings; ++slotToReplace)
        {
            VertexBuffer buffer(size);

            // Add the desired number of windings to the buffer
            for (auto n = 1; n <= NumWindings; ++n)
            {
                buffer.pushWinding(createWinding(n, size));
            }

            auto replacementWinding = createWinding(IdForReplacement, size);

            // Replace a winding in the desired slot
            buffer.replaceWinding(slotToReplace, replacementWinding);

            // Check the unchanged vector sizes
            EXPECT_EQ(buffer.getVertices().size(), NumWindings * buffer.getWindingSize()) << "Vertex array should remain unchanged";
            EXPECT_EQ(buffer.getIndices().size(), NumWindings * buffer.getNumIndicesPerWinding()) << "Index array should remain unchanged";

            // Check the slot data
            for (auto n = 1; n <= NumWindings; ++n)
            {
                auto slot = n - 1;

                checkWindingDataInSlot(buffer, slot, slot == slotToReplace ? IdForReplacement : n);
            }
        }
    }
}

TEST(CompactWindingVertexBuffer, RemoveMultipleWindings)
{
    for (auto size = SmallestWindingSize; size < LargestWindingSize; ++size)
    {
        // We will work with a buffer containing N windings,
        // the test will remove 1 to N windings from every possible position (2^N - 1)
        constexpr auto NumWindings = 9;

        for (int combination = 1; combination <= (1 << NumWindings) - 1; ++combination)
        {
            VertexBuffer buffer(size);

            // Add the desired number of windings to the buffer
            for (auto n = 1; n <= NumWindings; ++n)
            {
                buffer.pushWinding(createWinding(n, size));
            }

            std::vector<VertexBuffer::Slot> slotsToRemove;

            // Translate the bit combination to a set of slots to remove
            for (int pos = 0; pos < NumWindings; ++pos)
            {
                if (combination & (1 << pos))
                {
                    slotsToRemove.push_back(pos);
                }
            }

            buffer.removeWindings(slotsToRemove);

            // The buffer should be smaller now
            auto remainingWindings = NumWindings - slotsToRemove.size();

            EXPECT_EQ(buffer.getVertices().size(), remainingWindings * buffer.getWindingSize()) <<
                "Winding vertex array has the wrong size after removal";
            EXPECT_EQ(buffer.getIndices().size(), remainingWindings * buffer.getNumIndicesPerWinding()) <<
                "Winding index array has the wrong size after removal";

            // Check the winding data, the ones we removed should be missing now
            for (int i = 0, slot = 0; i < NumWindings; ++i)
            {
                if (std::find(slotsToRemove.begin(), slotsToRemove.end(), i) != slotsToRemove.end())
                {
                    continue; // this id got removed, skip it
                }

                checkWindingIndices(buffer, slot);
                checkWindingDataInSlot(buffer, slot, i + 1);
                ++slot;
            }
        }
    }
}

TEST(CompactWindingVertexBuffer, TriangleIndexerSize3) // Winding size == 3
{
    render::CompactWindingVertexBuffer<MeshVertex, render::WindingIndexer_Triangles> buffer(3);

    EXPECT_EQ(buffer.getNumIndicesPerWinding(), 3);

    // Generate winding indices and check the result
    std::vector<unsigned int> indices;
    render::WindingIndexer_Triangles::GenerateAndAssignIndices(std::back_inserter(indices), buffer.getWindingSize(), 80);

    EXPECT_EQ(indices.size(), buffer.getNumIndicesPerWinding()) << "Wrong number of indices generated";

    EXPECT_EQ(indices[0], 80) << "Index 0 mismatch";
    EXPECT_EQ(indices[1], 81) << "Index 1 mismatch";
    EXPECT_EQ(indices[2], 82) << "Index 2 mismatch";
}

TEST(CompactWindingVertexBuffer, TriangleIndexerSize4) // Winding size == 4
{
    render::CompactWindingVertexBuffer<MeshVertex, render::WindingIndexer_Triangles> buffer(4);

    EXPECT_EQ(buffer.getNumIndicesPerWinding(), 6);

    // Generate winding indices and check the result
    std::vector<unsigned int> indices;
    render::WindingIndexer_Triangles::GenerateAndAssignIndices(std::back_inserter(indices), buffer.getWindingSize(), 80);

    EXPECT_EQ(indices.size(), buffer.getNumIndicesPerWinding()) << "Wrong number of indices generated";

    EXPECT_EQ(indices[0], 80) << "Index 0 mismatch";
    EXPECT_EQ(indices[1], 82) << "Index 1 mismatch";
    EXPECT_EQ(indices[2], 83) << "Index 2 mismatch";

    EXPECT_EQ(indices[3], 80) << "Index 3 mismatch";
    EXPECT_EQ(indices[4], 81) << "Index 4 mismatch";
    EXPECT_EQ(indices[5], 82) << "Index 5 mismatch";
}

TEST(CompactWindingVertexBuffer, TriangleIndexerSize5) // Winding size == 5
{
    render::CompactWindingVertexBuffer<MeshVertex, render::WindingIndexer_Triangles> buffer(5);

    EXPECT_EQ(buffer.getNumIndicesPerWinding(), 9);

    // Generate winding indices and check the result
    std::vector<unsigned int> indices;
    render::WindingIndexer_Triangles::GenerateAndAssignIndices(std::back_inserter(indices), buffer.getWindingSize(), 80);

    EXPECT_EQ(indices.size(), buffer.getNumIndicesPerWinding()) << "Wrong number of indices generated";

    EXPECT_EQ(indices[0], 80) << "Index 0 mismatch";
    EXPECT_EQ(indices[1], 83) << "Index 1 mismatch";
    EXPECT_EQ(indices[2], 84) << "Index 2 mismatch";

    EXPECT_EQ(indices[3], 80) << "Index 3 mismatch";
    EXPECT_EQ(indices[4], 82) << "Index 4 mismatch";
    EXPECT_EQ(indices[5], 83) << "Index 5 mismatch";

    EXPECT_EQ(indices[6], 80) << "Index 6 mismatch";
    EXPECT_EQ(indices[7], 81) << "Index 7 mismatch";
    EXPECT_EQ(indices[8], 82) << "Index 8 mismatch";
}

TEST(CompactWindingVertexBuffer, LineIndexerSize3) // Winding size == 3
{
    render::CompactWindingVertexBuffer<MeshVertex, render::WindingIndexer_Lines> buffer(3);

    EXPECT_EQ(buffer.getNumIndicesPerWinding(), 6);

    // Generate winding indices and check the result
    std::vector<unsigned int> indices;
    render::WindingIndexer_Lines::GenerateAndAssignIndices(std::back_inserter(indices), buffer.getWindingSize(), 80);

    EXPECT_EQ(indices.size(), buffer.getNumIndicesPerWinding()) << "Wrong number of indices generated";

    EXPECT_EQ(indices[0], 80) << "Index 0 mismatch";
    EXPECT_EQ(indices[1], 81) << "Index 1 mismatch";

    EXPECT_EQ(indices[2], 81) << "Index 2 mismatch";
    EXPECT_EQ(indices[3], 82) << "Index 3 mismatch";

    EXPECT_EQ(indices[4], 82) << "Index 4 mismatch";
    EXPECT_EQ(indices[5], 80) << "Index 5 mismatch";
}

TEST(CompactWindingVertexBuffer, LineIndexerSize4) // Winding size == 4
{
    render::CompactWindingVertexBuffer<MeshVertex, render::WindingIndexer_Lines> buffer(4);

    EXPECT_EQ(buffer.getNumIndicesPerWinding(), 8);

    // Generate winding indices and check the result
    std::vector<unsigned int> indices;
    render::WindingIndexer_Lines::GenerateAndAssignIndices(std::back_inserter(indices), buffer.getWindingSize(), 80);

    EXPECT_EQ(indices.size(), buffer.getNumIndicesPerWinding()) << "Wrong number of indices generated";

    EXPECT_EQ(indices[0], 80) << "Index 0 mismatch";
    EXPECT_EQ(indices[1], 81) << "Index 1 mismatch";

    EXPECT_EQ(indices[2], 81) << "Index 2 mismatch";
    EXPECT_EQ(indices[3], 82) << "Index 3 mismatch";

    EXPECT_EQ(indices[4], 82) << "Index 4 mismatch";
    EXPECT_EQ(indices[5], 83) << "Index 5 mismatch";

    EXPECT_EQ(indices[6], 83) << "Index 6 mismatch";
    EXPECT_EQ(indices[7], 80) << "Index 7 mismatch";
}

TEST(CompactWindingVertexBuffer, LineIndexerSize5) // Winding size == 5
{
    render::CompactWindingVertexBuffer<MeshVertex, render::WindingIndexer_Lines> buffer(5);

    EXPECT_EQ(buffer.getNumIndicesPerWinding(), 10);

    // Generate winding indices and check the result
    std::vector<unsigned int> indices;
    render::WindingIndexer_Lines::GenerateAndAssignIndices(std::back_inserter(indices), buffer.getWindingSize(), 80);

    EXPECT_EQ(indices.size(), buffer.getNumIndicesPerWinding()) << "Wrong number of indices generated";

    EXPECT_EQ(indices[0], 80) << "Index 0 mismatch";
    EXPECT_EQ(indices[1], 81) << "Index 1 mismatch";

    EXPECT_EQ(indices[2], 81) << "Index 2 mismatch";
    EXPECT_EQ(indices[3], 82) << "Index 3 mismatch";

    EXPECT_EQ(indices[4], 82) << "Index 4 mismatch";
    EXPECT_EQ(indices[5], 83) << "Index 5 mismatch";

    EXPECT_EQ(indices[6], 83) << "Index 6 mismatch";
    EXPECT_EQ(indices[7], 84) << "Index 7 mismatch";

    EXPECT_EQ(indices[8], 84) << "Index 8 mismatch";
    EXPECT_EQ(indices[9], 80) << "Index 9 mismatch";
}

TEST(CompactWindingVertexBuffer, PolygonIndexerSize5) // Winding size == 5
{
    render::CompactWindingVertexBuffer<MeshVertex, render::WindingIndexer_Polygon> buffer(5);

    EXPECT_EQ(buffer.getNumIndicesPerWinding(), 5);

    // Generate winding indices and check the result
    std::vector<unsigned int> indices;
    render::WindingIndexer_Polygon::GenerateAndAssignIndices(std::back_inserter(indices), buffer.getWindingSize(), 80);

    EXPECT_EQ(indices.size(), buffer.getNumIndicesPerWinding()) << "Wrong number of indices generated";

    EXPECT_EQ(indices[0], 80) << "Index 0 mismatch";
    EXPECT_EQ(indices[1], 81) << "Index 1 mismatch";
    EXPECT_EQ(indices[2], 82) << "Index 2 mismatch";
    EXPECT_EQ(indices[3], 83) << "Index 3 mismatch";
    EXPECT_EQ(indices[4], 84) << "Index 4 mismatch";
}

inline std::size_t getNumEntitySurfaces(IRenderEntity* entity)
{
    std::size_t count = 0;

    entity->foreachRenderable([&](const render::IRenderableObject::Ptr& object, Shader* shader)
    {
        ++count;
    });

    return count;
}

inline render::IRenderableObject::Ptr getNthEntitySurface(IRenderEntity* entity, std::size_t index)
{
    std::size_t count = 0;
    render::IRenderableObject::Ptr foundObject;

    entity->foreachRenderable([&](const render::IRenderableObject::Ptr& object, Shader* _)
    {
        if (count++ == index)
        {
            foundObject = object;
        }
    });

    return foundObject;
}

TEST_F(WindingRendererTest, EntitySurfaceCreation)
{
    render::GeometryStore geometryStore(syncObjectProvider, bufferObjectProvider);
    auto shader = GlobalRenderSystem().capture("textures/common/caulk");
    auto entity = algorithm::createEntityByClassName("func_static");
    scene::addNodeToContainer(entity, GlobalMapModule().getRoot());

    // Create a  new triangle WindingRenderer
    render::WindingRenderer<render::WindingIndexer_Triangles> renderer(geometryStore, testObjectRenderer, shader.get());

    // Test windings of various sizes
    std::vector<render::RenderVertex> winding4 = generateVertices(1, 4);
    std::vector<render::RenderVertex> winding3 = generateVertices(1, 3);

    EXPECT_EQ(getNumEntitySurfaces(entity.get()), 0) << "Entity should start with no surfaces";

    auto winding4Slot = renderer.addWinding(winding4, entity.get());
    EXPECT_EQ(getNumEntitySurfaces(entity.get()), 1) << "Entity should have 1 surface now";

    auto winding3Slot = renderer.addWinding(winding3, entity.get());
    EXPECT_EQ(getNumEntitySurfaces(entity.get()), 2) << "Entity should have 2 surfaces now";

    renderer.removeWinding(winding4Slot);
    EXPECT_EQ(getNumEntitySurfaces(entity.get()), 1) << "Entity should have 1 surface now";

    renderer.removeWinding(winding3Slot);
    EXPECT_EQ(getNumEntitySurfaces(entity.get()), 0) << "Entity should be empty again";
}

TEST_F(WindingRendererTest, EntitySurfacesRemovedInDestructor)
{
    render::GeometryStore geometryStore(syncObjectProvider, bufferObjectProvider);
    auto shader = GlobalRenderSystem().capture("textures/common/caulk");
    auto entity = algorithm::createEntityByClassName("func_static");
    scene::addNodeToContainer(entity, GlobalMapModule().getRoot());

    // Create a  new triangle WindingRenderer
    auto renderer = std::make_unique<render::WindingRenderer<render::WindingIndexer_Triangles>>(geometryStore, testObjectRenderer, shader.get());

    // Test windings of various sizes
    std::vector<render::RenderVertex> winding4 = generateVertices(1, 4);
    std::vector<render::RenderVertex> winding3 = generateVertices(1, 3);

    renderer->addWinding(winding4, entity.get());
    renderer->addWinding(winding3, entity.get());
    EXPECT_EQ(getNumEntitySurfaces(entity.get()), 2) << "Entity should have 2 surfaces now";

    // All entity surfaces should be removed in the WindingRenderer's destructor
    renderer.reset();
    EXPECT_EQ(getNumEntitySurfaces(entity.get()), 0) << "Entity should be empty again";
}

// Checks that the given vertex slot contains the given set of vertices
void verifyVertexSlot(render::IGeometryStore& store, render::IGeometryStore::Slot slot,
    const std::vector<render::RenderVertex>& vertices)
{
    auto renderParms = store.getBufferAddresses(slot);

    auto expectedVertex = vertices.begin();

    for (auto vertex = renderParms.clientBufferStart + renderParms.firstVertex;
         expectedVertex != vertices.end();
         ++vertex, ++expectedVertex)
    {
        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)) << "Normal data mismatch";
    }
}

// #5963: Entity surface IndexRemap slots not updated after moving the CompactWindingVertexBuffer to a different vertex geometry slot
TEST_F(WindingRendererTest, IndexRemapsUpdatedAfterReallocation)
{
    render::GeometryStore geometryStore(syncObjectProvider, bufferObjectProvider);
    auto shader = GlobalRenderSystem().capture("textures/common/caulk");
    auto entity = algorithm::createEntityByClassName("func_static");
    auto entity2 = algorithm::createEntityByClassName("func_static");
    scene::addNodeToContainer(entity, GlobalMapModule().getRoot());
    scene::addNodeToContainer(entity2, GlobalMapModule().getRoot());

    // Create a  new triangle WindingRenderer
    auto renderer = std::make_unique<render::WindingRenderer<render::WindingIndexer_Triangles>>(geometryStore, testObjectRenderer, shader.get());

    // Generate a couple of windings
    std::vector<render::RenderVertex> entity1Windings;
    for (int i = 0; i < 3; ++i)
    {
        std::vector<render::RenderVertex> winding4 = generateVertices(i, 4);
        renderer->addWinding(winding4, entity.get());
        std::copy(winding4.begin(), winding4.end(), std::back_inserter(entity1Windings));
    }

    // Make sure the windings are stored in the geometry store
    renderer->prepareForRendering();

    // Acquire a few unrelated blocks in the geometry store to simulate other objects allocating memory
    geometryStore.allocateSlot(15, 24);
    geometryStore.allocateSlot(50, 60);
    geometryStore.allocateSlot(5, 12);
    geometryStore.allocateSlot(100, 150);

    EXPECT_EQ(getNumEntitySurfaces(entity.get()), 1) << "Entity should have 1 surface now";

    auto surfaceEntity1 = getNthEntitySurface(entity.get(), 0);
    EXPECT_TRUE(surfaceEntity1) << "Entity Surface not found";

    // Verify the winding geometry in the given slot
    verifyVertexSlot(geometryStore, surfaceEntity1->getStorageLocation(), entity1Windings);

    // Remember the offset of the first vertex
    auto firstVertexOffsetBeforeMove = geometryStore.getBufferAddresses(surfaceEntity1->getStorageLocation()).firstVertex;

    // Now add a few more windings for the second entity, this should invalidate all
    // the entity surfaces of the renderer (it failed to do so before #5963).
    // The WindingRenderer will have to reallocate its geometry slot, so the vertices
    // will be located at a different memory location. Since we added unrelated blocks, we force the
    // larger allocation to be moved to the end of the geometry store
    std::vector<render::RenderVertex> entity2Windings;
    for (int i = 0; i < 5; ++i)
    {
        std::vector<render::RenderVertex> winding4 = generateVertices(i + 10, 4);
        renderer->addWinding(winding4, entity2.get());
        std::copy(winding4.begin(), winding4.end(), std::back_inserter(entity2Windings));
    }

    // Force another geometry store update
    renderer->prepareForRendering();

    // Get the surface again
    surfaceEntity1 = getNthEntitySurface(entity.get(), 0);
    auto surfaceEntity2 = getNthEntitySurface(entity2.get(), 0);

    // Now check the connection of the IndexRemap slots of entity 1's surface,
    // it should be correctly referring to the moved vertex slot
    verifyVertexSlot(geometryStore, surfaceEntity1->getStorageLocation(), entity1Windings);

    // The first vertex' offset of entity 1's surface ought to be different now
    auto firstVertexOffsetAfterMove = geometryStore.getBufferAddresses(surfaceEntity1->getStorageLocation()).firstVertex;
    EXPECT_NE(firstVertexOffsetAfterMove, firstVertexOffsetBeforeMove) << "First vertex offset didn't change, this is very suspicious";

    // Compare the vertex offset of entity1's surface to that of entity2, they should match
    auto entity2VertexOffset = geometryStore.getBufferAddresses(surfaceEntity2->getStorageLocation()).firstVertex;
    EXPECT_EQ(entity2VertexOffset, firstVertexOffsetAfterMove) << "First vertex offset of both entities should match";
}

}