diff options
Diffstat (limited to '3rdparty/bgfx/3rdparty/meshoptimizer/demo/tests.cpp')
-rw-r--r-- | 3rdparty/bgfx/3rdparty/meshoptimizer/demo/tests.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/3rdparty/bgfx/3rdparty/meshoptimizer/demo/tests.cpp b/3rdparty/bgfx/3rdparty/meshoptimizer/demo/tests.cpp index 2e14fda5a7f..ffab4c02354 100644 --- a/3rdparty/bgfx/3rdparty/meshoptimizer/demo/tests.cpp +++ b/3rdparty/bgfx/3rdparty/meshoptimizer/demo/tests.cpp @@ -1,6 +1,7 @@ #include "../src/meshoptimizer.h" #include <assert.h> +#include <stdlib.h> #include <string.h> #include <vector> @@ -261,6 +262,100 @@ static void clusterBoundsDegenerate() assert(bounds2.center[2] - bounds2.radius <= 0 && bounds2.center[2] + bounds2.radius >= 1); } +static size_t allocCount; +static size_t freeCount; + +static void* customAlloc(size_t size) +{ + allocCount++; + + return malloc(size); +} + +static void customFree(void* ptr) +{ + freeCount++; + + free(ptr); +} + +static void customAllocator() +{ + meshopt_setAllocator(customAlloc, customFree); + + assert(allocCount == 0 && freeCount == 0); + + float vb[] = {1, 0, 0, 0, 1, 0, 0, 0, 1}; + unsigned int ib[] = {0, 1, 2}; + unsigned short ibs[] = {0, 1, 2}; + + // meshopt_computeClusterBounds doesn't allocate + meshopt_computeClusterBounds(ib, 3, vb, 3, 12); + assert(allocCount == 0 && freeCount == 0); + + // ... unless IndexAdapter is used + meshopt_computeClusterBounds(ibs, 3, vb, 3, 12); + assert(allocCount == 1 && freeCount == 1); + + // meshopt_optimizeVertexFetch allocates internal remap table and temporary storage for in-place remaps + meshopt_optimizeVertexFetch(vb, ib, 3, vb, 3, 12); + assert(allocCount == 3 && freeCount == 3); + + // ... plus one for IndexAdapter + meshopt_optimizeVertexFetch(vb, ibs, 3, vb, 3, 12); + assert(allocCount == 6 && freeCount == 6); + + meshopt_setAllocator(operator new, operator delete); + + // customAlloc & customFree should not get called anymore + meshopt_optimizeVertexFetch(vb, ib, 3, vb, 3, 12); + assert(allocCount == 6 && freeCount == 6); +} + +static void emptyMesh() +{ + meshopt_optimizeVertexCache(0, 0, 0, 0); + meshopt_optimizeVertexCacheFifo(0, 0, 0, 0, 16); + meshopt_optimizeOverdraw(0, 0, 0, 0, 0, 12, 1.f); +} + +static void simplifyStuck() +{ + // tetrahedron can't be simplified due to collapse error restrictions + float vb1[] = {0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1}; + unsigned int ib1[] = {0, 1, 2, 0, 2, 3, 0, 3, 1, 2, 1, 3}; + + assert(meshopt_simplify(ib1, ib1, 12, vb1, 4, 12, 6, 1e-3f) == 12); + + // 5-vertex strip can't be simplified due to topology restriction since middle triangle has flipped winding + float vb2[] = {0, 0, 0, 1, 0, 0, 2, 0, 0, 0.5f, 1, 0, 1.5f, 1, 0}; + unsigned int ib2[] = {0, 1, 3, 3, 1, 4, 1, 2, 4}; // ok + unsigned int ib3[] = {0, 1, 3, 1, 3, 4, 1, 2, 4}; // flipped + + assert(meshopt_simplify(ib2, ib2, 9, vb2, 5, 12, 6, 1e-3f) == 6); + assert(meshopt_simplify(ib3, ib3, 9, vb2, 5, 12, 6, 1e-3f) == 9); +} + +static void simplifySloppyStuck() +{ + const float vb[] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; + const unsigned int ib[] = {0, 1, 2, 0, 1, 2}; + + // simplifying down to 0 triangles results in 0 immediately + assert(meshopt_simplifySloppy(0, ib, 3, vb, 3, 12, 0) == 0); + + // simplifying down to 2 triangles given that all triangles are degenerate results in 0 as well + assert(meshopt_simplifySloppy(0, ib, 6, vb, 3, 12, 6) == 0); +} + +static void simplifyPointsStuck() +{ + const float vb[] = {0, 0, 0, 0, 0, 0, 0, 0, 0}; + + // simplifying down to 0 points results in 0 immediately + assert(meshopt_simplifyPoints(0, vb, 3, 12, 0) == 0); +} + void runTests() { decodeIndexV0(); @@ -277,4 +372,12 @@ void runTests() decodeVertexRejectMalformedHeaders(); clusterBoundsDegenerate(); + + customAllocator(); + + emptyMesh(); + + simplifyStuck(); + simplifySloppyStuck(); + simplifyPointsStuck(); } |