initial commit

This commit is contained in:
Erin Catto 2026-05-10 13:36:52 -07:00
commit 573e1a9fa3
220 changed files with 150440 additions and 0 deletions

13
benchmark/CMakeLists.txt Normal file
View file

@ -0,0 +1,13 @@
# Benchmark app
set(BOX3D_BENCHMARK_FILES
main.c
)
add_executable(benchmark ${BOX3D_BENCHMARK_FILES})
set_target_properties(benchmark PROPERTIES C_STANDARD 17)
target_link_libraries(benchmark PRIVATE box3d shared)
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} PREFIX "" FILES ${BOX3D_BENCHMARK_FILES})

View file

@ -0,0 +1,9 @@
threads,ms
1,0
2,0
3,0
4,0
5,0
6,0
7,0
8,5488.14
1 threads ms
2 1 0
3 2 0
4 3 0
5 4 0
6 5 0
7 6 0
8 7 0
9 8 5488.14

View file

@ -0,0 +1,9 @@
threads,ms
1,730.575
2,424.323
3,324.045
4,276.202
5,242.944
6,226.599
7,211.694
8,205.856
1 threads ms
2 1 730.575
3 2 424.323
4 3 324.045
5 4 276.202
6 5 242.944
7 6 226.599
8 7 211.694
9 8 205.856

View file

@ -0,0 +1,9 @@
threads,ms
1,462.769
2,292.812
3,230.578
4,197.733
5,178.241
6,166.567
7,161.298
8,157.091
1 threads ms
2 1 462.769
3 2 292.812
4 3 230.578
5 4 197.733
6 5 178.241
7 6 166.567
8 7 161.298
9 8 157.091

View file

@ -0,0 +1,9 @@
threads,ms
1,1796.4
2,954.375
3,641.176
4,481.819
5,390.785
6,328.059
7,287.637
8,258.124
1 threads ms
2 1 1796.4
3 2 954.375
4 3 641.176
5 4 481.819
6 5 390.785
7 6 328.059
8 7 287.637
9 8 258.124

View file

@ -0,0 +1,9 @@
threads,ms
1,24717.7
2,13513.9
3,9716.35
4,7493.39
5,6140.22
6,5221.34
7,4550.24
8,4106.65
1 threads ms
2 1 24717.7
3 2 13513.9
4 3 9716.35
5 4 7493.39
6 5 6140.22
7 6 5221.34
8 7 4550.24
9 8 4106.65

View file

@ -0,0 +1,9 @@
threads,ms
1,1682.1
2,878.51
3,613.252
4,478.075
5,387.426
6,337.026
7,294.737
8,270.737
1 threads ms
2 1 1682.1
3 2 878.51
4 3 613.252
5 4 478.075
6 5 387.426
7 6 337.026
8 7 294.737
9 8 270.737

View file

@ -0,0 +1,9 @@
threads,ms
1,2575.34
2,1278.03
3,878.515
4,629.084
5,507.933
6,413.473
7,362.456
8,320.241
1 threads ms
2 1 2575.34
3 2 1278.03
4 3 878.515
5 4 629.084
6 5 507.933
7 6 413.473
8 7 362.456
9 8 320.241

View file

@ -0,0 +1,9 @@
threads,ms
1,2627.76
2,1427.39
3,1012.48
4,796.119
5,664.231
6,577.576
7,518.164
8,483.436
1 threads ms
2 1 2627.76
3 2 1427.39
4 3 1012.48
5 4 796.119
6 5 664.231
7 6 577.576
8 7 518.164
9 8 483.436

View file

@ -0,0 +1,9 @@
threads,ms
1,31480.4
2,17010.7
3,11890.1
4,9295.49
5,7696.31
6,6645.53
7,5843.28
8,5249.02
1 threads ms
2 1 31480.4
3 2 17010.7
4 3 11890.1
5 4 9295.49
6 5 7696.31
7 6 6645.53
8 7 5843.28
9 8 5249.02

View file

@ -0,0 +1,9 @@
threads,ms
1,726.988
2,430.503
3,324.672
4,268.082
5,232.24
6,213.849
7,199.971
8,193.328
1 threads ms
2 1 726.988
3 2 430.503
4 3 324.672
5 4 268.082
6 5 232.24
7 6 213.849
8 7 199.971
9 8 193.328

344
benchmark/main.c Normal file
View file

@ -0,0 +1,344 @@
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-License-Identifier: MIT
#if defined( _MSC_VER ) && !defined( _CRT_SECURE_NO_WARNINGS )
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "benchmarks.h"
#include "utils.h"
#include "box3d/box3d.h"
#include "box3d/constants.h"
#include "box3d/math_functions.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef TRACY_ENABLE
#include <tracy/TracyC.h>
#endif
#define ARRAY_COUNT( A ) (int)( sizeof( A ) / sizeof( A[0] ) )
#define MAYBE_UNUSED( x ) ( (void)( x ) )
typedef void CapacityFcn( b3Capacity* capacity );
typedef void CreateFcn( b3WorldId worldId );
typedef void DestroyFcn( void );
typedef void StepFcn( b3WorldId worldId, int stepCount );
typedef struct Benchmark
{
const char* name;
CapacityFcn* capacityFcn;
CreateFcn* createFcn;
DestroyFcn* destroyFcn;
StepFcn* stepFcn;
int totalStepCount;
} Benchmark;
static void MinProfile( b3Profile* p1, const b3Profile* p2 )
{
p1->step = b3MinFloat( p1->step, p2->step );
p1->pairs = b3MinFloat( p1->pairs, p2->pairs );
p1->collide = b3MinFloat( p1->collide, p2->collide );
p1->constraints = b3MinFloat( p1->constraints, p2->constraints );
p1->transforms = b3MinFloat( p1->transforms, p2->transforms );
p1->refit = b3MinFloat( p1->refit, p2->refit );
p1->sleepIslands = b3MinFloat( p1->sleepIslands, p2->sleepIslands );
}
// Box3D benchmark application. On Windows it is important to use affinity avoid cross CCD
// usage or efficiency cores. Also on Windows create a power plan with Processor power management
// Min/Max of 99%. This prevents boosting and makes the benchmarks more repeatable.
// Affinity [0x01 0x02 0x04 0x08 0x10 0x20 0x40 0x80]
// Run all benchmarks with 1 to 8 threads.
// start /affinity 0x5555 .\build\bin\Release\benchmark.exe -t=8
// Run all benchmarks with 4 workers only.
// start /affinity 0x5555 .\build\bin\Release\benchmark.exe -t=4 -w=4
// start /affinity 0x5555 .\build\bin\Release\benchmark.exe -t=4 -w=4 -b=5 -r=5
// Run benchmark 3 with 4 workers and repeat 20 times. Record the step times.
// start /affinity 0x5555 .\build\bin\Release\benchmark.exe -t=4 -w=4 -b=3 -r=20 -s
// Run benchmark 3 with 4 workers and run once. Disable continuous collision. Record the step times.
// start /affinity 0x5555 .\build\bin\Release\benchmark.exe -t=4 -w=4 -b=3 -r=1 -nc -s
int main( int argc, char** argv )
{
#ifdef TRACY_ENABLE
___tracy_startup_profiler();
#endif
Benchmark benchmarks[] = {
{ "compound_capsules", NULL, CreateCompoundCapsules, DestroyCompoundCapsules, NULL, 500 },
{ "compound_hulls", NULL, CreateCompoundHulls, DestroyCompoundHulls, NULL, 500 },
{ "joint_grid", NULL, CreateJointGrid, NULL, NULL, 100 },
{ "junkyard", NULL, CreateJunkyard, NULL, StepJunkyard, 500 },
{ "large_pyramid", NULL, CreateLargePyramid, NULL, NULL, 200 },
{ "many_pyramids", NULL, CreateManyPyramids, NULL, NULL, 100 },
{ "rain", GetRainCapacity, CreateRain, DestroyRain, StepRain, 400 },
{ "washer", GetWasherCapacity, CreateWasher, NULL, NULL, 1000 },
{ "welded_hulls", NULL, CreateWeldedHulls, DestroyWeldedHulls, NULL, 500 },
//{ "smash", CreateSmash, NULL, 300 },
//{ "spinner", CreateSpinner, StepSpinner, 1400 },
//{ "tumbler", CreateTumbler, NULL, 750 },
};
int benchmarkCount = ARRAY_COUNT( benchmarks );
int maxSteps = benchmarks[0].totalStepCount;
for ( int i = 1; i < benchmarkCount; ++i )
{
maxSteps = b3MaxInt( maxSteps, benchmarks[i].totalStepCount );
}
b3Profile maxProfile = {
.step = FLT_MAX,
.pairs = FLT_MAX,
.collide = FLT_MAX,
.solve = FLT_MAX,
.solverSetup = FLT_MAX,
.constraints = FLT_MAX,
.prepareConstraints = FLT_MAX,
.integrateVelocities = FLT_MAX,
.warmStart = FLT_MAX,
.solveImpulses = FLT_MAX,
.integratePositions = FLT_MAX,
.relaxImpulses = FLT_MAX,
.applyRestitution = FLT_MAX,
.storeImpulses = FLT_MAX,
.splitIslands = FLT_MAX,
.transforms = FLT_MAX,
.hitEvents = FLT_MAX,
.refit = FLT_MAX,
.bullets = FLT_MAX,
.sleepIslands = FLT_MAX,
};
b3Profile* profiles = malloc( maxSteps * sizeof( b3Profile ) );
for ( int i = 0; i < maxSteps; ++i )
{
profiles[i] = maxProfile;
}
int maxThreadCount = GetNumberOfCores();
int runCount = 4;
int singleBenchmark = -1;
int singleWorkerCount = -1;
b3Counters counters = { 0 };
bool enableContinuous = true;
bool recordStepTimes = false;
assert( maxThreadCount <= B3_MAX_WORKERS );
for ( int i = 1; i < argc; ++i )
{
const char* arg = argv[i];
if ( strncmp( arg, "-t=", 3 ) == 0 )
{
int threadCount = atoi( arg + 3 );
maxThreadCount = b3ClampInt( threadCount, 1, maxThreadCount );
}
else if ( strncmp( arg, "-b=", 3 ) == 0 )
{
singleBenchmark = atoi( arg + 3 );
singleBenchmark = b3ClampInt( singleBenchmark, 0, benchmarkCount - 1 );
}
else if ( strncmp( arg, "-w=", 3 ) == 0 )
{
singleWorkerCount = atoi( arg + 3 );
}
else if ( strncmp( arg, "-r=", 3 ) == 0 )
{
runCount = b3ClampInt( atoi( arg + 3 ), 1, 1000 );
}
else if ( strncmp( arg, "-nc", 3 ) == 0 )
{
enableContinuous = false;
printf( "Continuous disabled\n" );
}
else if ( strncmp( arg, "-s", 3 ) == 0 )
{
recordStepTimes = true;
}
else if ( strcmp( arg, "-h" ) == 0 )
{
printf( "Usage\n"
"-t=<integer>: the maximum number of threads to use\n"
"-b=<integer>: run a single benchmark\n"
"-w=<integer>: run a single worker count\n"
"-r=<integer>: number of repeats (default is 4)\n"
"-s: record step times\n" );
exit( 0 );
}
}
if ( singleWorkerCount != -1 )
{
singleWorkerCount = b3ClampInt( singleWorkerCount, 1, maxThreadCount );
}
printf( "Starting benchmarks\n" );
printf( "======================================\n" );
for ( int benchmarkIndex = 0; benchmarkIndex < benchmarkCount; ++benchmarkIndex )
{
if ( singleBenchmark != -1 && benchmarkIndex != singleBenchmark )
{
continue;
}
#ifdef NDEBUG
int stepCount = benchmarks[benchmarkIndex].totalStepCount;
#else
int stepCount = 10;
#endif
Benchmark* benchmark = benchmarks + benchmarkIndex;
bool countersAcquired = false;
printf( "benchmark: %s, steps = %d\n", benchmarks[benchmarkIndex].name, stepCount );
float minTime[B3_MAX_WORKERS] = { 0 };
for ( int threadCount = 1; threadCount <= maxThreadCount; ++threadCount )
{
if ( singleWorkerCount != -1 && singleWorkerCount != threadCount )
{
continue;
}
printf( "thread count: %d\n", threadCount );
for ( int runIndex = 0; runIndex < runCount; ++runIndex )
{
b3WorldDef worldDef = b3DefaultWorldDef();
worldDef.enableContinuous = enableContinuous;
worldDef.workerCount = threadCount;
if (benchmark->capacityFcn != NULL)
{
benchmark->capacityFcn( &worldDef.capacity );
}
b3WorldId worldId = b3CreateWorld( &worldDef );
benchmark->createFcn( worldId );
float timeStep = 1.0f / 60.0f;
int subStepCount = 4;
// Initial step can be expensive and skew benchmark
if ( benchmark->stepFcn != NULL )
{
benchmark->stepFcn( worldId, 0 );
}
assert( stepCount <= maxSteps );
b3World_Step( worldId, timeStep, subStepCount );
\
b3Profile profile = b3World_GetProfile( worldId );
MinProfile( profiles + 0, &profile );
uint64_t ticks = b3GetTicks();
for ( int stepIndex = 1; stepIndex < stepCount; ++stepIndex )
{
if ( benchmark->stepFcn != NULL )
{
benchmark->stepFcn( worldId, stepIndex );
}
b3World_Step( worldId, timeStep, subStepCount );
\
profile = b3World_GetProfile( worldId );
MinProfile( profiles + stepIndex, &profile );
}
float ms = b3GetMilliseconds( ticks );
printf( "run %d : %g (ms)\n", runIndex, ms );
if ( runIndex == 0 )
{
minTime[threadCount - 1] = ms;
}
else
{
minTime[threadCount - 1] = b3MinFloat( minTime[threadCount - 1], ms );
}
if ( countersAcquired == false )
{
counters = b3World_GetCounters( worldId );
countersAcquired = true;
}
if ( benchmark->destroyFcn != NULL )
{
benchmark->destroyFcn();
}
b3DestroyWorld( worldId );
}
if ( recordStepTimes )
{
char fileName[64] = { 0 };
snprintf( fileName, 64, "%s_t%d.dat", benchmarks[benchmarkIndex].name, threadCount );
FILE* file = fopen( fileName, "w" );
if ( file == NULL )
{
continue;
}
for ( int stepIndex = 0; stepIndex < stepCount; ++stepIndex )
{
b3Profile p = profiles[stepIndex];
fprintf( file, "%g %g %g %g %g %g %g\n", p.step, p.pairs, p.collide, p.constraints, p.transforms,
p.refit, p.sleepIslands );
}
fclose( file );
}
}
printf( "body %d / shape %d / contact %d / joint %d / stack %d\n\n", counters.bodyCount, counters.shapeCount,
counters.contactCount, counters.jointCount, counters.stackUsed );
char fileName[64] = { 0 };
snprintf( fileName, 64, "%s.csv", benchmarks[benchmarkIndex].name );
FILE* file = fopen( fileName, "w" );
if ( file == NULL )
{
continue;
}
fprintf( file, "threads,ms\n" );
for ( int threadIndex = 1; threadIndex <= maxThreadCount; ++threadIndex )
{
fprintf( file, "%d,%g\n", threadIndex, minTime[threadIndex - 1] );
}
fclose( file );
}
printf( "======================================\n" );
printf( "All benchmarks complete!\n" );
free( profiles );
#ifdef TRACY_ENABLE
___tracy_shutdown_profiler();
#endif
return 0;
}