Improve compound serialization (#183)

- no more pointer fixups
- reduced storage 20% by removing leaf proxies
This commit is contained in:
Erin Catto 2026-10-07 14:48:58 -07:00 • committed by GitHub
commit 5d83df83ab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 845 additions and 376 deletions

View file

@ -77,10 +77,9 @@ static int CompoundCreateMixed( void )
ENSURE( compound->sharedHullCount == 1 );
ENSURE( compound->sharedMeshCount == 1 );
ENSURE( compound->tree.nodeEnd >= 2 );
ENSURE( compound->tree.proxyCount > 0 );
ENSURE( compound->tree.nodes != NULL );
ENSURE( compound->tree.proxies != NULL );
ENSURE( compound->nodeCount >= 2 );
ENSURE( compound->nodeOffset > 0 );
ENSURE( compound->bounds.lowerBound.x < compound->bounds.upperBound.x );
b3DestroyCompound( compound );
b3DestroyMesh( meshData );
@ -883,7 +882,8 @@ static int CompoundMeshContactMaterial( void )
ENSURE( compound != NULL );
b3ShapeDef groundShapeDef = b3DefaultShapeDef();
b3CreateBakedCompoundShape( groundBodyId, &groundShapeDef, compound );
b3ShapeId compoundShapeId = b3CreateBakedCompoundShape( groundBodyId, &groundShapeDef, compound );
ENSURE( b3Shape_GetCompound( compoundShapeId ) == compound );
b3BodyDef sphereBodyDef = b3DefaultBodyDef();
sphereBodyDef.type = b3_dynamicBody;
@ -1233,18 +1233,24 @@ static int CompoundSerializeRoundtrip( void )
uint8_t* buffer = (uint8_t*)malloc( (size_t)byteCount );
ENSURE( buffer != NULL );
uint8_t* serialized = b3ConvertCompoundToBytes( a );
memcpy( buffer, serialized, (size_t)byteCount );
uint8_t* reference = (uint8_t*)malloc( (size_t)byteCount );
ENSURE( reference != NULL );
memcpy( buffer, a, (size_t)byteCount );
memcpy( reference, a, (size_t)byteCount );
b3DestroyCompound( a );
b3CompoundData* b = b3ConvertBytesToCompound( buffer, byteCount );
const b3CompoundData* b = b3ValidateCompound( buffer, byteCount );
ENSURE( b != NULL );
ENSURE( (const uint8_t*)b == buffer );
ENSURE( b->byteCount == byteCount );
ENSURE( b->capsuleCount == 1 );
ENSURE( b->hullCount == 1 );
ENSURE( b->meshCount == 1 );
ENSURE( b->sphereCount == 1 );
ENSURE( b->tree.nodes != NULL );
// Validation must not touch the bytes
ENSURE( memcmp( buffer, reference, (size_t)byteCount ) == 0 );
// Bounds and ray cast on the deserialized compound match the original.
b3AABB aabbB = b3ComputeCompoundAABB( b, b3Transform_identity );
@ -1256,6 +1262,7 @@ static int CompoundSerializeRoundtrip( void )
ENSURE_SMALL( rayB.fraction - rayA.fraction, 1e-5f );
ENSURE( rayB.childIndex == rayA.childIndex );
free( reference );
free( buffer );
b3DestroyMesh( md );
return 0;
@ -1268,13 +1275,13 @@ static int CompoundSerializeBadVersion( void )
int byteCount = a->byteCount;
uint8_t* buffer = (uint8_t*)malloc( (size_t)byteCount );
memcpy( buffer, b3ConvertCompoundToBytes( a ), (size_t)byteCount );
memcpy( buffer, a, (size_t)byteCount );
b3DestroyCompound( a );
// Corrupt the version word (first 8 bytes of b3Compound).
( (b3CompoundData*)buffer )->version ^= 0x1ull;
b3CompoundData* b = b3ConvertBytesToCompound( buffer, byteCount );
const b3CompoundData* b = b3ValidateCompound( buffer, byteCount );
ENSURE( b == NULL );
free( buffer );
@ -1289,11 +1296,11 @@ static int CompoundSerializeWrongByteCount( void )
int byteCount = a->byteCount;
uint8_t* buffer = (uint8_t*)malloc( (size_t)byteCount );
memcpy( buffer, b3ConvertCompoundToBytes( a ), (size_t)byteCount );
memcpy( buffer, a, (size_t)byteCount );
b3DestroyCompound( a );
// Off-by-one in the declared length is rejected.
b3CompoundData* b = b3ConvertBytesToCompound( buffer, byteCount + 1 );
const b3CompoundData* b = b3ValidateCompound( buffer, byteCount + 1 );
ENSURE( b == NULL );
free( buffer );
@ -1301,6 +1308,35 @@ static int CompoundSerializeWrongByteCount( void )
return 0;
}
static int CompoundSerializeBadNodeCount( void )
{
b3MeshData* md = b3CreateBoxMesh( b3Vec3_zero, (b3Vec3){ 0.5f, 0.5f, 0.5f }, false );
b3CompoundData* a = BuildSerializableCompound( md );
int byteCount = a->byteCount;
uint8_t* buffer = (uint8_t*)malloc( (size_t)byteCount );
memcpy( buffer, a, (size_t)byteCount );
b3DestroyCompound( a );
b3CompoundData* header = (b3CompoundData*)buffer;
int nodeCount = header->nodeCount;
// Node array runs off the end of the buffer
header->nodeCount = byteCount;
ENSURE( b3ValidateCompound( buffer, byteCount ) == NULL );
// Overflow in 32 bit arithmetic must still be caught
header->nodeCount = INT32_MAX;
ENSURE( b3ValidateCompound( buffer, byteCount ) == NULL );
header->nodeCount = nodeCount;
ENSURE( b3ValidateCompound( buffer, byteCount ) != NULL );
free( buffer );
b3DestroyMesh( md );
return 0;
}
int CompoundTest( void )
{
RUN_SUBTEST( CompoundCreateMixed );
@ -1344,6 +1380,7 @@ int CompoundTest( void )
RUN_SUBTEST( CompoundSerializeRoundtrip );
RUN_SUBTEST( CompoundSerializeBadVersion );
RUN_SUBTEST( CompoundSerializeWrongByteCount );
RUN_SUBTEST( CompoundSerializeBadNodeCount );
return 0;
}

View file

@ -625,8 +625,7 @@ static int TreeQueryClosest( void )
return 0;
}
// A saved tree carries only its live node range. A baked compound tree has no parent array, so
// saving one has to bail out rather than write from a null pointer.
// A saved tree carries only its live node range.
static int TreeSaveLoadRoundtrip( void )
{
s_seed = 4242;
@ -667,34 +666,6 @@ static int TreeSaveLoadRoundtrip( void )
b3DynamicTree_Destroy( &loaded );
b3DynamicTree_Destroy( &tree );
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
b3CompoundHullDef hulls[2];
hulls[0].hull = &box.base;
hulls[0].transform = (b3Transform){ { -1.0f, 0.0f, 0.0f }, b3Quat_identity };
hulls[0].material = b3DefaultSurfaceMaterial();
hulls[1].hull = &box.base;
hulls[1].transform = (b3Transform){ { 1.0f, 0.0f, 0.0f }, b3Quat_identity };
hulls[1].material = b3DefaultSurfaceMaterial();
b3CompoundDef compoundDef = { 0 };
compoundDef.hulls = hulls;
compoundDef.hullCount = 2;
b3CompoundData* compound = b3CreateCompound( &compoundDef );
ENSURE( compound != NULL );
ENSURE( compound->tree.parents == NULL );
ENSURE( compound->tree.proxyCapacity == compound->tree.proxyCount );
const char* compoundPath = "test_dynamic_tree_compound.dat";
remove( compoundPath );
b3DynamicTree_Save( &compound->tree, compoundPath );
b3DynamicTree noTree = b3DynamicTree_Load( compoundPath, 1.0f );
remove( compoundPath );
ENSURE( noTree.nodes == NULL );
b3DestroyCompound( compound );
return 0;
}

View file

@ -1948,6 +1948,11 @@ static int CorruptCompound( void )
memcpy( patched + blobOffset - 4, &shortByteCount, sizeof( shortByteCount ) );
ENSURE( b3CreatePlayer( patched, recSize, 1 ) == NULL );
memcpy( patched, recData, (size_t)recSize );
int32_t zeroByteCount = 0;
memcpy( patched + blobOffset - 4, &zeroByteCount, sizeof( zeroByteCount ) );
ENSURE( b3CreatePlayer( patched, recSize, 1 ) == NULL );
memcpy( patched, recData, (size_t)recSize );
patched[blobOffset - 5] = (uint8_t)b3_geometryMesh;
ENSURE( b3ValidateReplay( patched, recSize, 1 ) == false );
@ -1963,13 +1968,16 @@ static int CorruptCompound( void )
// without growing the registry. Forces the collision by handing the same hash to distinct blobs.
static int GeometryHashCollision( void )
{
const int n = 16;
enum
{
n = 16
};
const uint64_t sharedHash = 0xABCD1234ull;
b3GeometryRegistry reg = { 0 };
uint8_t* blobA = (uint8_t*)b3Alloc( (size_t)n );
uint8_t* blobB = (uint8_t*)b3Alloc( (size_t)n );
uint8_t blobA[n];
uint8_t blobB[n];
memset( blobA, 0xAA, (size_t)n );
memset( blobB, 0xBB, (size_t)n );
@ -1982,12 +1990,12 @@ static int GeometryHashCollision( void )
// Re-interning either blob must find it through the hash chain and never grow the registry,
// including the one shadowed behind the bucket head. The old single-entry lookup missed the
// shadowed blob and appended a duplicate, which is exactly what tripped the keyframe assert.
uint8_t* blobA2 = (uint8_t*)b3Alloc( (size_t)n );
uint8_t blobA2[n];
memset( blobA2, 0xAA, (size_t)n );
ENSURE( b3InternGeometry( &reg, b3_geometryHull, sharedHash, blobA2, n ) == idA );
ENSURE( reg.entries.count == 2 );
uint8_t* blobB2 = (uint8_t*)b3Alloc( (size_t)n );
uint8_t blobB2[n];
memset( blobB2, 0xBB, (size_t)n );
ENSURE( b3InternGeometry( &reg, b3_geometryHull, sharedHash, blobB2, n ) == idB );
ENSURE( reg.entries.count == 2 );
@ -1997,9 +2005,9 @@ static int GeometryHashCollision( void )
// Seed-then-capture: appending byte-identical duplicate slots keeps id == slot index, and a later
// exact intern still resolves to one of them without appending a new entry.
b3GeometryRegistry seeded = { 0 };
uint8_t* slot0 = (uint8_t*)b3Alloc( (size_t)n );
uint8_t* slot1 = (uint8_t*)b3Alloc( (size_t)n );
uint8_t* slot2 = (uint8_t*)b3Alloc( (size_t)n );
uint8_t slot0[n];
uint8_t slot1[n];
uint8_t slot2[n];
memset( slot0, 0xAA, (size_t)n );
memset( slot1, 0xBB, (size_t)n );
memset( slot2, 0xAA, (size_t)n ); // duplicate of slot0
@ -2007,7 +2015,7 @@ static int GeometryHashCollision( void )
ENSURE( b3AppendGeometry( &seeded, b3_geometryHull, sharedHash, slot1, n ) == 1 );
ENSURE( b3AppendGeometry( &seeded, b3_geometryHull, sharedHash, slot2, n ) == 2 );
uint8_t* live = (uint8_t*)b3Alloc( (size_t)n );
uint8_t live[n];
memset( live, 0xAA, (size_t)n );
uint32_t resolved = b3InternGeometry( &seeded, b3_geometryHull, sharedHash, live, n );
ENSURE( seeded.entries.count == 3 ); // no growth