optimize mesh builder
This commit is contained in:
parent
e06e511f7c
commit
ae05b8f85a
9 changed files with 641 additions and 256 deletions
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
set(BOX3D_BENCHMARK_FILES
|
||||
main.c
|
||||
mesh_benchmark.c
|
||||
mesh_benchmark.h
|
||||
)
|
||||
|
||||
add_executable(benchmark ${BOX3D_BENCHMARK_FILES})
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
#endif
|
||||
|
||||
#include "benchmarks.h"
|
||||
#include "mesh_benchmark.h"
|
||||
#include "utils.h"
|
||||
|
||||
#ifdef BOX3D_INTERNAL_BENCHMARKS
|
||||
|
|
@ -215,6 +216,9 @@ static void PrintBenchmarks( const Benchmark* benchmarks, int count )
|
|||
// Compare GJK and SAT on hull pairs in the speculative regime. Static Box3D only.
|
||||
// start /affinity 0x5555 .\build\bin\Release\benchmark.exe --sat -r=5
|
||||
|
||||
// Time mesh creation.
|
||||
// start /affinity 0x5555 .\build\bin\Release\benchmark.exe --mesh -r=5
|
||||
|
||||
int main( int argc, char** argv )
|
||||
{
|
||||
#ifdef TRACY_ENABLE
|
||||
|
|
@ -286,6 +290,8 @@ int main( int argc, char** argv )
|
|||
bool enableContinuous = true;
|
||||
bool recordStepTimes = false;
|
||||
bool runSat = false;
|
||||
bool runMesh = false;
|
||||
const char* meshCase = NULL;
|
||||
int simdWidth = 0;
|
||||
|
||||
assert( maxThreadCount <= B3_MAX_WORKERS );
|
||||
|
|
@ -343,6 +349,15 @@ int main( int argc, char** argv )
|
|||
exit( 1 );
|
||||
#endif
|
||||
}
|
||||
else if ( ( value = MatchValue( arg, "-mesh=", "--mesh=" ) ) != NULL )
|
||||
{
|
||||
runMesh = true;
|
||||
meshCase = value;
|
||||
}
|
||||
else if ( strcmp( arg, "-mesh" ) == 0 || strcmp( arg, "--mesh" ) == 0 )
|
||||
{
|
||||
runMesh = true;
|
||||
}
|
||||
else if ( ( value = MatchValue( arg, "-simd=", "--simd-width=" ) ) != NULL )
|
||||
{
|
||||
simdWidth = atoi( value );
|
||||
|
|
@ -372,6 +387,7 @@ int main( int argc, char** argv )
|
|||
"-nc, --no-continuous: disable continuous collision\n"
|
||||
"-s, --record-steps: record step times\n"
|
||||
"-sat, --sat: compare GJK and SAT on hull pairs, then exit\n"
|
||||
"-mesh, --mesh[=<case>]: time b3CreateMesh on several meshes, or one case, then exit\n"
|
||||
"-simd, --simd-width=<4|8>: force the SIMD width (static Box3D only)\n"
|
||||
"-l, --list: list the registered benchmarks\n"
|
||||
"-h, --help: print this help\n" );
|
||||
|
|
@ -402,6 +418,13 @@ int main( int argc, char** argv )
|
|||
}
|
||||
#endif
|
||||
|
||||
if ( runMesh )
|
||||
{
|
||||
RunMeshBenchmarks( runCount, meshCase );
|
||||
free( profiles );
|
||||
return 0;
|
||||
}
|
||||
|
||||
printf( "Starting benchmarks\n" );
|
||||
printf( "======================================\n" );
|
||||
#ifdef BOX3D_INTERNAL_BENCHMARKS
|
||||
|
|
|
|||
233
benchmark/mesh_benchmark.c
Normal file
233
benchmark/mesh_benchmark.c
Normal file
|
|
@ -0,0 +1,233 @@
|
|||
// SPDX-FileCopyrightText: 2025 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "mesh_benchmark.h"
|
||||
|
||||
#include "box3d/box3d.h"
|
||||
#include "box3d/collision.h"
|
||||
#include "box3d/math_functions.h"
|
||||
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef enum MeshShape
|
||||
{
|
||||
meshShape_wave,
|
||||
meshShape_torus,
|
||||
meshShape_soup,
|
||||
} MeshShape;
|
||||
|
||||
typedef struct MeshCase
|
||||
{
|
||||
const char* name;
|
||||
MeshShape shape;
|
||||
int countA;
|
||||
int countB;
|
||||
bool useMedianSplit;
|
||||
bool identifyEdges;
|
||||
bool weldVertices;
|
||||
} MeshCase;
|
||||
|
||||
typedef struct MeshSource
|
||||
{
|
||||
b3Vec3* vertices;
|
||||
int32_t* indices;
|
||||
int vertexCount;
|
||||
int triangleCount;
|
||||
} MeshSource;
|
||||
|
||||
static float WaveHeight( int i, int j )
|
||||
{
|
||||
return 0.5f * sinf( 0.31f * (float)i ) * cosf( 0.23f * (float)j ) + 0.25f * sinf( 0.07f * (float)( i + 2 * j ) );
|
||||
}
|
||||
|
||||
// Shared-vertex grid, or a triangle soup with three vertices per triangle when unshared is set
|
||||
static MeshSource MakeWave( int xCount, int zCount, bool unshared )
|
||||
{
|
||||
float cellWidth = 0.5f;
|
||||
MeshSource source = { 0 };
|
||||
source.triangleCount = 2 * xCount * zCount;
|
||||
source.indices = malloc( 3 * source.triangleCount * sizeof( int32_t ) );
|
||||
|
||||
if ( unshared == false )
|
||||
{
|
||||
source.vertexCount = ( xCount + 1 ) * ( zCount + 1 );
|
||||
source.vertices = malloc( source.vertexCount * sizeof( b3Vec3 ) );
|
||||
for ( int i = 0; i <= xCount; ++i )
|
||||
{
|
||||
for ( int j = 0; j <= zCount; ++j )
|
||||
{
|
||||
source.vertices[i * ( zCount + 1 ) + j] = (b3Vec3){ cellWidth * i, WaveHeight( i, j ), cellWidth * j };
|
||||
}
|
||||
}
|
||||
|
||||
int index = 0;
|
||||
for ( int i = 0; i < xCount; ++i )
|
||||
{
|
||||
for ( int j = 0; j < zCount; ++j )
|
||||
{
|
||||
int a = i * ( zCount + 1 ) + j;
|
||||
int b = a + 1;
|
||||
int c = a + ( zCount + 1 ) + 1;
|
||||
int d = a + ( zCount + 1 );
|
||||
source.indices[index++] = a;
|
||||
source.indices[index++] = b;
|
||||
source.indices[index++] = c;
|
||||
source.indices[index++] = a;
|
||||
source.indices[index++] = c;
|
||||
source.indices[index++] = d;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
source.vertexCount = 3 * source.triangleCount;
|
||||
source.vertices = malloc( source.vertexCount * sizeof( b3Vec3 ) );
|
||||
int index = 0;
|
||||
for ( int i = 0; i < xCount; ++i )
|
||||
{
|
||||
for ( int j = 0; j < zCount; ++j )
|
||||
{
|
||||
b3Vec3 a = { cellWidth * i, WaveHeight( i, j ), cellWidth * j };
|
||||
b3Vec3 b = { cellWidth * i, WaveHeight( i, j + 1 ), cellWidth * ( j + 1 ) };
|
||||
b3Vec3 c = { cellWidth * ( i + 1 ), WaveHeight( i + 1, j + 1 ), cellWidth * ( j + 1 ) };
|
||||
b3Vec3 d = { cellWidth * ( i + 1 ), WaveHeight( i + 1, j ), cellWidth * j };
|
||||
b3Vec3 tris[6] = { a, b, c, a, c, d };
|
||||
for ( int k = 0; k < 6; ++k )
|
||||
{
|
||||
source.vertices[index] = tris[k];
|
||||
source.indices[index] = index;
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
static MeshSource MakeTorus( int radialCount, int tubularCount )
|
||||
{
|
||||
float radius = 8.0f;
|
||||
float thickness = 2.0f;
|
||||
MeshSource source = { 0 };
|
||||
source.vertexCount = radialCount * tubularCount;
|
||||
source.triangleCount = 2 * radialCount * tubularCount;
|
||||
source.vertices = malloc( source.vertexCount * sizeof( b3Vec3 ) );
|
||||
source.indices = malloc( 3 * source.triangleCount * sizeof( int32_t ) );
|
||||
|
||||
for ( int i = 0; i < radialCount; ++i )
|
||||
{
|
||||
float u = 2.0f * B3_PI * i / radialCount;
|
||||
for ( int j = 0; j < tubularCount; ++j )
|
||||
{
|
||||
float v = 2.0f * B3_PI * j / tubularCount;
|
||||
float r = radius + thickness * cosf( v );
|
||||
source.vertices[i * tubularCount + j] = (b3Vec3){ r * cosf( u ), thickness * sinf( v ), r * sinf( u ) };
|
||||
}
|
||||
}
|
||||
|
||||
int index = 0;
|
||||
for ( int i = 0; i < radialCount; ++i )
|
||||
{
|
||||
int i2 = ( i + 1 ) % radialCount;
|
||||
for ( int j = 0; j < tubularCount; ++j )
|
||||
{
|
||||
int j2 = ( j + 1 ) % tubularCount;
|
||||
int a = i * tubularCount + j;
|
||||
int b = i2 * tubularCount + j;
|
||||
int c = i2 * tubularCount + j2;
|
||||
int d = i * tubularCount + j2;
|
||||
source.indices[index++] = a;
|
||||
source.indices[index++] = c;
|
||||
source.indices[index++] = b;
|
||||
source.indices[index++] = a;
|
||||
source.indices[index++] = d;
|
||||
source.indices[index++] = c;
|
||||
}
|
||||
}
|
||||
|
||||
return source;
|
||||
}
|
||||
|
||||
void RunMeshBenchmarks( int runCount, const char* caseName )
|
||||
{
|
||||
MeshCase cases[] = {
|
||||
{ "wave_64", meshShape_wave, 64, 64, true, true, false },
|
||||
{ "wave_128", meshShape_wave, 128, 128, true, true, false },
|
||||
{ "wave_256", meshShape_wave, 256, 256, true, true, false },
|
||||
{ "wave_128_no_edges", meshShape_wave, 128, 128, true, false, false },
|
||||
{ "wave_128_sah", meshShape_wave, 128, 128, false, true, false },
|
||||
{ "torus_128x64", meshShape_torus, 128, 64, false, true, false },
|
||||
{ "soup_128_weld", meshShape_soup, 128, 128, true, true, true },
|
||||
};
|
||||
int caseCount = sizeof( cases ) / sizeof( cases[0] );
|
||||
|
||||
#ifdef NDEBUG
|
||||
int targetTriangles = 1 << 20;
|
||||
#else
|
||||
int targetTriangles = 1 << 14;
|
||||
#endif
|
||||
|
||||
printf( "benchmark: mesh creation, runs = %d\n", runCount );
|
||||
printf( "%-20s %10s %8s %12s %12s\n", "case", "triangles", "builds", "ms / build", "ns / tri" );
|
||||
|
||||
for ( int caseIndex = 0; caseIndex < caseCount; ++caseIndex )
|
||||
{
|
||||
const MeshCase* mc = cases + caseIndex;
|
||||
if ( caseName != NULL && strcmp( caseName, mc->name ) != 0 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
MeshSource source;
|
||||
if ( mc->shape == meshShape_torus )
|
||||
{
|
||||
source = MakeTorus( mc->countA, mc->countB );
|
||||
}
|
||||
else
|
||||
{
|
||||
source = MakeWave( mc->countA, mc->countB, mc->shape == meshShape_soup );
|
||||
}
|
||||
|
||||
b3MeshDef def = { 0 };
|
||||
def.vertices = source.vertices;
|
||||
def.vertexCount = source.vertexCount;
|
||||
def.indices = source.indices;
|
||||
def.triangleCount = source.triangleCount;
|
||||
def.useMedianSplit = mc->useMedianSplit;
|
||||
def.identifyEdges = mc->identifyEdges;
|
||||
def.weldVertices = mc->weldVertices;
|
||||
def.weldTolerance = mc->weldVertices ? 0.001f : 0.0f;
|
||||
|
||||
int buildCount = targetTriangles / source.triangleCount;
|
||||
if ( buildCount < 1 )
|
||||
{
|
||||
buildCount = 1;
|
||||
}
|
||||
|
||||
float bestMs = FLT_MAX;
|
||||
for ( int runIndex = 0; runIndex < runCount; ++runIndex )
|
||||
{
|
||||
uint64_t ticks = b3GetTicks();
|
||||
for ( int buildIndex = 0; buildIndex < buildCount; ++buildIndex )
|
||||
{
|
||||
b3MeshData* mesh = b3CreateMesh( &def, NULL, 0 );
|
||||
b3DestroyMesh( mesh );
|
||||
}
|
||||
float ms = b3GetMilliseconds( ticks ) / buildCount;
|
||||
bestMs = b3MinFloat( bestMs, ms );
|
||||
}
|
||||
|
||||
float nsPerTriangle = 1.0e6f * bestMs / source.triangleCount;
|
||||
printf( "%-20s %10d %8d %12.3f %12.1f\n", mc->name, source.triangleCount, buildCount, bestMs, nsPerTriangle );
|
||||
|
||||
free( source.vertices );
|
||||
free( source.indices );
|
||||
}
|
||||
|
||||
printf( "\n" );
|
||||
}
|
||||
6
benchmark/mesh_benchmark.h
Normal file
6
benchmark/mesh_benchmark.h
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
// SPDX-FileCopyrightText: 2025 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#pragma once
|
||||
|
||||
void RunMeshBenchmarks( int runCount, const char* caseName );
|
||||
Loading…
Reference in a new issue