Add support for AVX2 and runtime feature detection (#174)
- [x] run unit tests - [x] run all samples
This commit is contained in:
parent
51f056e0a9
commit
e77352cd60
53 changed files with 4681 additions and 3376 deletions
|
|
@ -52,6 +52,10 @@ if(BOX3D_DISABLE_SIMD)
|
|||
target_compile_definitions(test PRIVATE BOX3D_DISABLE_SIMD)
|
||||
endif()
|
||||
|
||||
if(NOT BOX3D_AVX2)
|
||||
target_compile_definitions(test PRIVATE BOX3D_DISABLE_AVX2)
|
||||
endif()
|
||||
|
||||
target_link_libraries(test PRIVATE box3d shared)
|
||||
|
||||
source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" PREFIX "" FILES ${BOX3D_TEST_FILES})
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include "box3d/box3d.h"
|
||||
#include "determinism.h"
|
||||
#include "simd.h"
|
||||
#include "stability.h"
|
||||
#include "test_macros.h"
|
||||
|
||||
|
|
@ -331,6 +332,155 @@ static int MeshDropTest( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
static uint32_t HashFloats( uint32_t hash, const float* values, int count )
|
||||
{
|
||||
return b3Hash( hash, (const uint8_t*)values, count * (int)sizeof( float ) );
|
||||
}
|
||||
|
||||
// Rolling resistance is mixed per contact and the manifolds have one, two, and four points, so a width 8
|
||||
// constraint can mix lanes that width 4 keeps in separate constraints. The hash covers the stored contact
|
||||
// impulses bitwise, including the sign of zero, because those survive into warm starting and contact data.
|
||||
static int SingleRollingMixTest( int workerCount, DeterminismResult* reference )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
worldDef.workerCount = workerCount;
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef groundDef = b3DefaultBodyDef();
|
||||
b3BodyId groundId = b3CreateBody( worldId, &groundDef );
|
||||
b3BoxHull groundBox = b3MakeBoxHull( 40.0f, 1.0f, 40.0f );
|
||||
b3ShapeDef groundShapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( groundId, &groundShapeDef, &groundBox.base );
|
||||
|
||||
enum
|
||||
{
|
||||
rowCount = 8,
|
||||
columnCount = 8,
|
||||
bodyCount = rowCount * columnCount
|
||||
};
|
||||
|
||||
b3BodyId bodyIds[bodyCount];
|
||||
b3BoxHull box = b3MakeBoxHull( 0.4f, 0.4f, 0.4f );
|
||||
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 0.4f };
|
||||
b3Capsule capsule = { { -0.3f, 0.0f, 0.0f }, { 0.3f, 0.0f, 0.0f }, 0.25f };
|
||||
|
||||
uint32_t seed = 271828u;
|
||||
for ( int i = 0; i < bodyCount; ++i )
|
||||
{
|
||||
int row = i / columnCount;
|
||||
int column = i % columnCount;
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.position = (b3Pos){ 2.0f * (float)column - 7.0f, 1.45f, 2.0f * (float)row - 7.0f };
|
||||
bodyIds[i] = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
seed = seed * 1664525u + 1013904223u;
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.density = 1.0f;
|
||||
shapeDef.baseMaterial.rollingResistance = ( seed >> 28 ) < 5 ? 0.2f : 0.0f;
|
||||
|
||||
switch ( i % 3 )
|
||||
{
|
||||
case 0:
|
||||
b3CreateSphereShape( bodyIds[i], &shapeDef, &sphere );
|
||||
break;
|
||||
case 1:
|
||||
b3CreateCapsuleShape( bodyIds[i], &shapeDef, &capsule );
|
||||
break;
|
||||
default:
|
||||
b3CreateHullShape( bodyIds[i], &shapeDef, &box.base );
|
||||
break;
|
||||
}
|
||||
|
||||
float sign = ( i & 1 ) ? -1.0f : 1.0f;
|
||||
b3Body_SetLinearVelocity( bodyIds[i], (b3Vec3){ -0.0f, -0.5f, sign * 0.5f } );
|
||||
b3Body_SetAngularVelocity( bodyIds[i], (b3Vec3){ sign * 3.0f, -0.0f, -2.0f * sign } );
|
||||
}
|
||||
|
||||
uint32_t hash = B3_HASH_INIT;
|
||||
int stepCount = 120;
|
||||
for ( int step = 0; step < stepCount; ++step )
|
||||
{
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 4 );
|
||||
|
||||
for ( int i = 0; i < bodyCount; ++i )
|
||||
{
|
||||
b3WorldTransform xf = b3Body_GetTransform( bodyIds[i] );
|
||||
hash = b3Hash( hash, (const uint8_t*)&xf, sizeof( b3WorldTransform ) );
|
||||
|
||||
b3ContactData contactData[4];
|
||||
int contactCount = b3Body_GetContactData( bodyIds[i], contactData, ARRAY_COUNT( contactData ) );
|
||||
for ( int c = 0; c < contactCount; ++c )
|
||||
{
|
||||
for ( int m = 0; m < contactData[c].manifoldCount; ++m )
|
||||
{
|
||||
const b3Manifold* manifold = contactData[c].manifolds + m;
|
||||
hash = HashFloats( hash, &manifold->rollingImpulse.x, 3 );
|
||||
hash = HashFloats( hash, &manifold->frictionImpulse.x, 3 );
|
||||
hash = HashFloats( hash, &manifold->twistImpulse, 1 );
|
||||
|
||||
for ( int p = 0; p < manifold->pointCount; ++p )
|
||||
{
|
||||
hash = HashFloats( hash, &manifold->points[p].normalImpulse, 1 );
|
||||
hash = HashFloats( hash, &manifold->points[p].totalNormalImpulse, 1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
|
||||
ENSURE( EnsureRepeatable( reference, (DeterminismResult){ .sleepStep = stepCount, .hash = hash } ) == 0 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef int SceneFcn( int workerCount, DeterminismResult* reference );
|
||||
|
||||
static int RunSceneAtWidth( SceneFcn* scene, int width, DeterminismResult* result )
|
||||
{
|
||||
b3SetSIMDWidth( width );
|
||||
int status = scene( 1, result );
|
||||
b3SetSIMDWidth( 0 );
|
||||
return status;
|
||||
}
|
||||
|
||||
// Width 4 and the native width must produce bitwise identical simulations.
|
||||
static int SIMDWidthTest( void )
|
||||
{
|
||||
b3SetSIMDWidth( 0 );
|
||||
int nativeWidth = b3GetSIMDWidth();
|
||||
if ( nativeWidth == 4 )
|
||||
{
|
||||
printf( " subtest skipped: SIMDWidthTest, native SIMD width is 4\n" );
|
||||
return 0;
|
||||
}
|
||||
|
||||
SceneFcn* scenes[] = {
|
||||
SingleMultithreadingTest, SingleWavePileTest, SingleQuerySpawnTest, SingleMeshDropTest, SingleRollingMixTest,
|
||||
};
|
||||
|
||||
for ( int i = 0; i < ARRAY_COUNT( scenes ); ++i )
|
||||
{
|
||||
DeterminismResult narrow = { 0 };
|
||||
DeterminismResult wide = { 0 };
|
||||
|
||||
int narrowStatus = RunSceneAtWidth( scenes[i], 4, &narrow );
|
||||
int wideStatus = RunSceneAtWidth( scenes[i], nativeWidth, &wide );
|
||||
|
||||
ENSURE( narrowStatus == 0 );
|
||||
ENSURE( wideStatus == 0 );
|
||||
ENSURE( narrow.seeded && wide.seeded );
|
||||
ENSURE( narrow.sleepStep == wide.sleepStep );
|
||||
ENSURE( narrow.hash == wide.hash );
|
||||
ENSURE( narrow.queryHitCount == wide.queryHitCount );
|
||||
ENSURE( narrow.queryHash == wide.queryHash );
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DeterminismTest( void )
|
||||
{
|
||||
RUN_SUBTEST( MultithreadingTest );
|
||||
|
|
@ -338,6 +488,7 @@ int DeterminismTest( void )
|
|||
RUN_SUBTEST( WavePileTest );
|
||||
RUN_SUBTEST( QuerySpawnTest );
|
||||
RUN_SUBTEST( MeshDropTest );
|
||||
RUN_SUBTEST( SIMDWidthTest );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include "physics_world.h"
|
||||
#include "recording.h"
|
||||
#include "simd.h"
|
||||
#include "test_macros.h"
|
||||
|
||||
#include "box3d/box3d.h"
|
||||
|
|
@ -133,6 +134,106 @@ static int HullDedup( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
static int ValidateReplayAtWidth( const b3Recording* rec, int width )
|
||||
{
|
||||
b3SetSIMDWidth( width );
|
||||
bool valid = b3ValidateReplay( b3Recording_GetData( rec ), b3Recording_GetSize( rec ), 1 );
|
||||
b3SetSIMDWidth( 0 );
|
||||
return valid ? 0 : 1;
|
||||
}
|
||||
|
||||
// A recording made at the native SIMD width must replay at width 4 and vice versa.
|
||||
static int CrossWidthReplay( void )
|
||||
{
|
||||
b3SetSIMDWidth( 0 );
|
||||
int nativeWidth = b3GetSIMDWidth();
|
||||
if ( nativeWidth == 4 )
|
||||
{
|
||||
printf( " subtest skipped: CrossWidthReplay, native SIMD width is 4\n" );
|
||||
return 0;
|
||||
}
|
||||
|
||||
enum
|
||||
{
|
||||
hullCount = 6
|
||||
};
|
||||
|
||||
b3HullData* hulls[hullCount] = { 0 };
|
||||
hulls[0] = b3CreateRock( 0.5f );
|
||||
hulls[1] = b3CreateComplexHull( 0.5f );
|
||||
hulls[2] = b3CreateCylinder( 0.8f, 0.4f, 0.0f, 8 );
|
||||
hulls[3] = b3CreateCone( 0.8f, 0.5f, 0.2f, 7 );
|
||||
|
||||
uint32_t seed = 12345u;
|
||||
for ( int hullIndex = 4; hullIndex < hullCount; ++hullIndex )
|
||||
{
|
||||
b3Vec3 points[20];
|
||||
for ( int i = 0; i < 20; ++i )
|
||||
{
|
||||
float v[3];
|
||||
for ( int k = 0; k < 3; ++k )
|
||||
{
|
||||
seed = seed * 1664525u + 1013904223u;
|
||||
v[k] = ( (float)( seed >> 8 ) / (float)( 1 << 24 ) - 0.5f ) * ( k == 1 ? 0.6f : 1.0f );
|
||||
}
|
||||
points[i] = (b3Vec3){ v[0], v[1], v[2] };
|
||||
}
|
||||
hulls[hullIndex] = b3CreateHull( points, 20, 20 );
|
||||
}
|
||||
|
||||
b3Recording* rec = b3CreateRecording( 0 );
|
||||
|
||||
b3SetSIMDWidth( nativeWidth );
|
||||
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3World_StartRecording( worldId, rec );
|
||||
|
||||
b3BodyDef groundDef = b3DefaultBodyDef();
|
||||
groundDef.type = b3_staticBody;
|
||||
b3BodyId groundId = b3CreateBody( worldId, &groundDef );
|
||||
b3BoxHull groundBox = b3MakeBoxHull( 20.0f, 1.0f, 20.0f );
|
||||
b3ShapeDef groundShape = b3DefaultShapeDef();
|
||||
b3CreateHullShape( groundId, &groundShape, &groundBox.base );
|
||||
|
||||
b3ShapeDef dynamicShape = b3DefaultShapeDef();
|
||||
dynamicShape.density = 1.0f;
|
||||
|
||||
for ( int i = 0; i < 18; ++i )
|
||||
{
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.position = (b3Pos){ 0.4f * (float)( i % 3 ) - 0.4f, 1.5f + 0.9f * (float)i, 0.3f * (float)( i % 2 ) };
|
||||
bodyDef.rotation = b3MakeQuatFromAxisAngle( (b3Vec3){ 0.0f, 1.0f, 0.0f }, 0.37f * (float)i );
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
b3CreateHullShape( bodyId, &dynamicShape, hulls[i % hullCount] );
|
||||
}
|
||||
|
||||
float timeStep = 1.0f / 60.0f;
|
||||
for ( int i = 0; i < 120; ++i )
|
||||
{
|
||||
b3World_Step( worldId, timeStep, 4 );
|
||||
}
|
||||
|
||||
b3World_StopRecording( worldId );
|
||||
b3DestroyWorld( worldId );
|
||||
b3SetSIMDWidth( 0 );
|
||||
|
||||
int narrowStatus = ValidateReplayAtWidth( rec, 4 );
|
||||
int nativeStatus = ValidateReplayAtWidth( rec, nativeWidth );
|
||||
|
||||
b3DestroyRecording( rec );
|
||||
for ( int i = 0; i < hullCount; ++i )
|
||||
{
|
||||
b3DestroyHull( hulls[i] );
|
||||
}
|
||||
|
||||
ENSURE( narrowStatus == 0 );
|
||||
ENSURE( nativeStatus == 0 );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Mid-stream snapshot with only dynamic bodies floating in air (no contacts).
|
||||
static int MidStreamNoContacts( void )
|
||||
{
|
||||
|
|
@ -2147,6 +2248,7 @@ int RecordingTest( void )
|
|||
RUN_SUBTEST( HullDedup );
|
||||
RUN_SUBTEST( MidStreamNoContacts );
|
||||
RUN_SUBTEST( MidStreamContacts );
|
||||
RUN_SUBTEST( CrossWidthReplay );
|
||||
RUN_SUBTEST( StagedStepCreationPose );
|
||||
RUN_SUBTEST( ScrubBackward );
|
||||
RUN_SUBTEST( SeekWithHull );
|
||||
|
|
|
|||
Loading…
Reference in a new issue