SIMD based hull collision inspired by example code from @cairnc in #54 <img width="1676" height="1332" alt="image" src="https://github.com/user-attachments/assets/58b137bd-bf7b-4c99-863c-64341b973a3d" /> - Fixed #78 - Fixed #85 and fixed CI to error on UB. - Updated benchmarks - Added AMD scalar benchmarks - Added Neon benchmarks - More determinism unit tests - Sample leak detection - Added b3Body_AllowFastRotation - Added b3Body_IsFastRotationAllowed - Bumped b3HullData version - Bumped b3CompoundData version
295 lines
7.1 KiB
C
295 lines
7.1 KiB
C
// SPDX-FileCopyrightText: 2025 Erin Catto
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#include "box3d/box3d.h"
|
|
#include "determinism.h"
|
|
#include "stability.h"
|
|
#include "test_macros.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#ifdef BOX3D_PROFILE
|
|
#include <tracy/TracyC.h>
|
|
#else
|
|
#define TracyCFrameMark
|
|
#endif
|
|
|
|
// Double precision accumulates body positions in double, so the settle/sleep step and the
|
|
// state hash differ from the float build. Both modes are internally deterministic.
|
|
#if defined( BOX3D_DOUBLE_PRECISION )
|
|
#define EXPECTED_SLEEP_STEP 297
|
|
#define EXPECTED_HASH 0x27FF38C1
|
|
#define WAVE_PILE_SLEEP_STEP 287
|
|
#define WAVE_PILE_HASH 0xFFC8DA49
|
|
#define QUERY_SPAWN_SLEEP_STEP 242
|
|
#define QUERY_SPAWN_HASH 0x1737F5BC
|
|
#define QUERY_SPAWN_HIT_COUNT 59
|
|
#define QUERY_SPAWN_QUERY_HASH 0x31F090DC
|
|
#define MESH_DROP_SLEEP_STEP 206
|
|
#define MESH_DROP_HASH 0xB6A9E1DE
|
|
#else
|
|
#define EXPECTED_SLEEP_STEP 308
|
|
#define EXPECTED_HASH 0x1E5EDD79
|
|
#define WAVE_PILE_SLEEP_STEP 285
|
|
#define WAVE_PILE_HASH 0x4057173C
|
|
#define QUERY_SPAWN_SLEEP_STEP 242
|
|
#define QUERY_SPAWN_HASH 0xB9F993A5
|
|
#define QUERY_SPAWN_HIT_COUNT 59
|
|
#define QUERY_SPAWN_QUERY_HASH 0xB9D3863D
|
|
#define MESH_DROP_SLEEP_STEP 205
|
|
#define MESH_DROP_HASH 0x8F55FB2D
|
|
#endif
|
|
|
|
static int SingleMultithreadingTest( int workerCount )
|
|
{
|
|
b3WorldDef worldDef = b3DefaultWorldDef();
|
|
worldDef.workerCount = workerCount;
|
|
|
|
b3WorldId worldId = b3CreateWorld( &worldDef );
|
|
|
|
FallingRagdollData data = CreateFallingRagdolls( worldId );
|
|
|
|
float timeStep = 1.0f / 60.0f;
|
|
|
|
int stepLimit = 500;
|
|
for ( int i = 0; i < stepLimit; ++i )
|
|
{
|
|
int subStepCount = 4;
|
|
b3World_Step( worldId, timeStep, subStepCount );
|
|
TracyCFrameMark;
|
|
|
|
bool done = UpdateFallingRagdolls( worldId, &data );
|
|
if ( done )
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
b3DestroyWorld( worldId );
|
|
|
|
if ( data.sleepStep != EXPECTED_SLEEP_STEP || data.hash != EXPECTED_HASH )
|
|
{
|
|
printf( " workers=%d sleepStep=%d hash=0x%08X\n", workerCount, data.sleepStep, data.hash );
|
|
}
|
|
|
|
ENSURE( data.sleepStep == EXPECTED_SLEEP_STEP );
|
|
ENSURE( data.hash == EXPECTED_HASH );
|
|
|
|
DestroyFallingRagdolls( &data );
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Test multithreaded determinism.
|
|
static int MultithreadingTest( void )
|
|
{
|
|
for ( int workerCount = 1; workerCount < 6; ++workerCount )
|
|
{
|
|
int result = SingleMultithreadingTest( workerCount );
|
|
ENSURE( result == 0 );
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Test cross platform determinism.
|
|
static int CrossPlatformTest( void )
|
|
{
|
|
b3WorldDef worldDef = b3DefaultWorldDef();
|
|
b3WorldId worldId = b3CreateWorld( &worldDef );
|
|
|
|
FallingRagdollData data = CreateFallingRagdolls( worldId );
|
|
|
|
float timeStep = 1.0f / 60.0f;
|
|
|
|
bool done = false;
|
|
while ( done == false )
|
|
{
|
|
int subStepCount = 4;
|
|
b3World_Step( worldId, timeStep, subStepCount );
|
|
TracyCFrameMark;
|
|
|
|
done = UpdateFallingRagdolls( worldId, &data );
|
|
}
|
|
|
|
if ( data.sleepStep != EXPECTED_SLEEP_STEP || data.hash != EXPECTED_HASH )
|
|
{
|
|
printf( " cross-platform sleepStep=%d hash=0x%08X\n", data.sleepStep, data.hash );
|
|
}
|
|
|
|
ENSURE( data.sleepStep == EXPECTED_SLEEP_STEP );
|
|
ENSURE( data.hash == EXPECTED_HASH );
|
|
|
|
DestroyFallingRagdolls( &data );
|
|
|
|
b3DestroyWorld( worldId );
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int SingleWavePileTest( int workerCount )
|
|
{
|
|
b3WorldDef worldDef = b3DefaultWorldDef();
|
|
worldDef.workerCount = workerCount;
|
|
|
|
b3WorldId worldId = b3CreateWorld( &worldDef );
|
|
|
|
WavePileData data = CreateWavePile( worldId );
|
|
|
|
float timeStep = 1.0f / 60.0f;
|
|
|
|
// Rolling resistance must put the pile to sleep within 500 steps
|
|
bool done = false;
|
|
for ( int i = 0; i < 500 && done == false; ++i )
|
|
{
|
|
int subStepCount = 4;
|
|
b3World_Step( worldId, timeStep, subStepCount );
|
|
TracyCFrameMark;
|
|
|
|
done = UpdateWavePile( worldId, &data );
|
|
}
|
|
|
|
b3DestroyWorld( worldId );
|
|
|
|
if ( data.sleepStep != WAVE_PILE_SLEEP_STEP || data.hash != WAVE_PILE_HASH )
|
|
{
|
|
printf( " wave pile workers=%d sleepStep=%d hash=0x%08X\n", workerCount, data.sleepStep, data.hash );
|
|
}
|
|
|
|
ENSURE( done == true );
|
|
ENSURE( data.sleepStep == WAVE_PILE_SLEEP_STEP );
|
|
ENSURE( data.hash == WAVE_PILE_HASH );
|
|
|
|
DestroyWavePile( &data );
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Test multithreaded determinism of a mixed convex pile on a wave height field.
|
|
static int WavePileTest( void )
|
|
{
|
|
for ( int workerCount = 1; workerCount <= 4; ++workerCount )
|
|
{
|
|
int result = SingleWavePileTest( workerCount );
|
|
ENSURE( result == 0 );
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int SingleQuerySpawnTest( int workerCount )
|
|
{
|
|
b3WorldDef worldDef = b3DefaultWorldDef();
|
|
worldDef.workerCount = workerCount;
|
|
|
|
b3WorldId worldId = b3CreateWorld( &worldDef );
|
|
|
|
QuerySpawnData data = CreateQuerySpawn( worldId );
|
|
|
|
float timeStep = 1.0f / 60.0f;
|
|
|
|
bool done = false;
|
|
for ( int i = 0; i < 1000 && done == false; ++i )
|
|
{
|
|
int subStepCount = 4;
|
|
b3World_Step( worldId, timeStep, subStepCount );
|
|
TracyCFrameMark;
|
|
|
|
done = UpdateQuerySpawn( worldId, &data );
|
|
}
|
|
|
|
b3DestroyWorld( worldId );
|
|
|
|
if ( data.sleepStep != QUERY_SPAWN_SLEEP_STEP || data.hash != QUERY_SPAWN_HASH || data.queryHitCount != QUERY_SPAWN_HIT_COUNT ||
|
|
data.queryHash != QUERY_SPAWN_QUERY_HASH )
|
|
{
|
|
printf( " query spawn workers=%d sleepStep=%d hash=0x%08X hits=%d queryHash=0x%08X\n", workerCount, data.sleepStep,
|
|
data.hash, data.queryHitCount, data.queryHash );
|
|
}
|
|
|
|
ENSURE( done == true );
|
|
ENSURE( data.spawnCount == QUERY_SPAWN_COUNT );
|
|
ENSURE( data.sleepStep == QUERY_SPAWN_SLEEP_STEP );
|
|
ENSURE( data.hash == QUERY_SPAWN_HASH );
|
|
ENSURE( data.queryHitCount == QUERY_SPAWN_HIT_COUNT );
|
|
ENSURE( data.queryHash == QUERY_SPAWN_QUERY_HASH );
|
|
|
|
DestroyQuerySpawn( &data );
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Test determinism of world queries by feeding their results back into the simulation.
|
|
static int QuerySpawnTest( void )
|
|
{
|
|
for ( int workerCount = 1; workerCount <= 4; ++workerCount )
|
|
{
|
|
int result = SingleQuerySpawnTest( workerCount );
|
|
ENSURE( result == 0 );
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int SingleMeshDropTest( int workerCount )
|
|
{
|
|
b3WorldDef worldDef = b3DefaultWorldDef();
|
|
worldDef.workerCount = workerCount;
|
|
|
|
b3WorldId worldId = b3CreateWorld( &worldDef );
|
|
|
|
MeshDropData data = CreateMeshDrop( worldId, b3Pos_zero );
|
|
|
|
float timeStep = 1.0f / 60.0f;
|
|
|
|
bool done = false;
|
|
for ( int i = 0; i < 400 && done == false; ++i )
|
|
{
|
|
int subStepCount = 4;
|
|
b3World_Step( worldId, timeStep, subStepCount );
|
|
TracyCFrameMark;
|
|
|
|
done = UpdateMeshDrop( worldId, &data );
|
|
}
|
|
|
|
b3DestroyWorld( worldId );
|
|
|
|
if ( data.sleepStep != MESH_DROP_SLEEP_STEP || data.hash != MESH_DROP_HASH )
|
|
{
|
|
printf( " mesh drop workers=%d sleepStep=%d hash=0x%08X\n", workerCount, data.sleepStep, data.hash );
|
|
}
|
|
|
|
ENSURE( done == true );
|
|
ENSURE( data.sleepStep == MESH_DROP_SLEEP_STEP );
|
|
ENSURE( data.hash == MESH_DROP_HASH );
|
|
|
|
DestroyMeshDrop( &data );
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Test continuous collision determinism. Thin fast boxes need CCD against the wave mesh.
|
|
// The scene is large, so only the single threaded and widest schedules run.
|
|
static int MeshDropTest( void )
|
|
{
|
|
int workerCounts[2] = { 1, 4 };
|
|
for ( int i = 0; i < 2; ++i )
|
|
{
|
|
int result = SingleMeshDropTest( workerCounts[i] );
|
|
ENSURE( result == 0 );
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
int DeterminismTest( void )
|
|
{
|
|
RUN_SUBTEST( MultithreadingTest );
|
|
RUN_SUBTEST( CrossPlatformTest );
|
|
RUN_SUBTEST( WavePileTest );
|
|
RUN_SUBTEST( QuerySpawnTest );
|
|
RUN_SUBTEST( MeshDropTest );
|
|
|
|
return 0;
|
|
}
|