various recording fixes (#182)

This commit is contained in:
Erin Catto 2026-10-06 22:45:33 -07:00 • committed by GitHub
commit 3d9db79ee1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 213 additions and 73 deletions

View file

@ -19,19 +19,19 @@ set(BOX3D_SOURCE_FILES
compound.h
constraint_graph.c
constraint_graph.h
contact.c
contact.h
contact_solver_wide.inl
contact_solver.c
contact_solver.h
contact.c
contact.h
container.h
convex_manifold_wide.inl
convex_manifold.c
core.c
core.h
ctz.h
distance.c
distance_joint.c
distance.c
dynamic_tree.c
dynamic_tree.h
height_field.c
@ -47,8 +47,8 @@ set(BOX3D_SOURCE_FILES
manifold.h
math_functions.c
math_internal.h
mesh.c
mesh_contact.c
mesh.c
motor_joint.c
mover.c
name_cache.c
@ -62,13 +62,11 @@ set(BOX3D_SOURCE_FILES
prismatic_joint.c
qsort.h
rapidhash.h
recording_ops.inl
recording.c
recording.h
recording_ops.inl
recording_replay.c
recording_replay.h
world_snapshot.c
world_snapshot.h
replay.c
replay.h
revolute_joint.c
scheduler.c
scheduler.h
@ -76,17 +74,17 @@ set(BOX3D_SOURCE_FILES
sensor.h
shape.c
shape.h
simd.c
simd.h
simd_avx2.h
simd_neon.h
simd_scalar.h
simd_sse2.h
simd_wide.h
solver.c
solver.h
simd.c
simd.h
solver_set.c
solver_set.h
solver.c
solver.h
sphere.c
spherical_joint.c
table.c
@ -99,6 +97,8 @@ set(BOX3D_SOURCE_FILES
wheel_joint.c
wide4.c
wide8.c
world_snapshot.c
world_snapshot.h
box3d.natvis
)

View file

@ -563,24 +563,14 @@ b3CompoundData* b3CreateCompound( const b3CompoundDef* def )
compound->byteCount = (int)byteCount;
compound->nodeOffset = nodeOffset;
compound->proxyOffset = proxyOffset;
memcpy( &compound->tree, &tree, sizeof( b3DynamicTree ) );
// todo clean up this mess
compound->tree.version = tree.version;
compound->tree.nodeEnd = tree.nodeEnd;
compound->tree.nodeCapacity = tree.nodeEnd;
compound->tree.pairFreeList = B3_NULL_INDEX;
compound->tree.proxyCount = tree.proxyCount;
compound->tree.proxyCapacity = tree.proxyCount;
compound->tree.proxyFreeList = B3_NULL_INDEX;
compound->tree.swapNodes = NULL;
compound->tree.leafIndices = NULL;
compound->tree.leafNodes = NULL;
compound->tree.leafBoxes = NULL;
compound->tree.leafCenters = NULL;
compound->tree.binIndices = NULL;
compound->tree.rebuildCapacity = 0;
compound->tree.nodes = NULL;
compound->tree.parents = NULL;
compound->tree.proxies = NULL;
compound->tree.dfsOrdered = tree.dfsOrdered;
compound->materialOffset = materialOffset;
compound->materialCount = materialCount;
compound->capsuleOffset = capsuleOffset;

View file

@ -189,7 +189,7 @@ void b3Free( void* mem, size_t size )
if ( b3_freeFcn != NULL )
{
size_t alignedSize = ( ( size - 1 ) | ( B3_ALIGNMENT - 1 ) ) + 1;
size_t alignedSize = size > 0 ? ( ( size - 1 ) | ( B3_ALIGNMENT - 1 ) ) + 1 : 0;
b3_freeFcn( mem, alignedSize );
}
else

View file

@ -1178,7 +1178,7 @@ uint32_t b3RecInternCompound( b3Recording* rec, const b3CompoundData* compound )
int byteCount = compound->byteCount;
uint8_t* bytes = b3Alloc( (size_t)byteCount );
memcpy( bytes, compound, (size_t)byteCount );
// Null the tree node pointer in the copy so the canonical bytes are pointer-free.
// Null the tree pointers in the copy so the canonical bytes are pointer-free.
// b3ConvertBytesToCompound fixes it back on load via nodeOffset.
b3ConvertCompoundToBytes( (b3CompoundData*)bytes );
uint64_t h = b3Hash64NonZero( bytes, byteCount );

View file

@ -5,7 +5,7 @@
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "recording_replay.h"
#include "replay.h"
#include "body.h"
#include "physics_world.h"
@ -713,6 +713,15 @@ static void b3RecCheckJointId( b3RecReader* rdr, b3JointId got, b3JointId rec )
// Registry slot reconstruction. Returns the live pointer for the given slot, building it
// on first use. The hull case is handled inline at the call site since it doesn't cache.
b3RegistrySlot* b3RecGetSlot( b3RecReader* rdr, uint32_t id, b3GeometryKind kind )
{
if ( rdr == NULL || id >= (uint32_t)rdr->slotCount || rdr->slots[id].kind != kind )
{
return NULL;
}
return rdr->slots + id;
}
static void* b3RecGetLiveMesh( b3RegistrySlot* slot )
{
// Mesh is a self-contained blob used by reference, with no pointer fixup. Hand back the pristine
@ -728,19 +737,25 @@ static void* b3RecGetLiveHeightField( b3RegistrySlot* slot )
return slot->bytes;
}
static void* b3RecGetLiveCompound( b3RegistrySlot* slot )
const b3CompoundData* b3RecGetLiveCompound( b3RegistrySlot* slot )
{
B3_ASSERT( slot->kind == b3_geometryCompound );
if ( slot->live != NULL )
{
return slot->live;
return (const b3CompoundData*)slot->live;
}
// The copy is unavoidable here: b3ConvertBytesToCompound rewrites its input in place, while the
// pristine bytes must survive for keyframe registry seeding (b3RecSeedKeyframeRegistry). So we
// keep both the serialized bytes and a separate converted live object.
slot->live = b3Alloc( (size_t)slot->byteCount );
memcpy( slot->live, slot->bytes, (size_t)slot->byteCount );
b3ConvertBytesToCompound( (uint8_t*)slot->live, slot->byteCount );
return slot->live;
uint8_t* live = (uint8_t*)b3Alloc( (size_t)slot->byteCount );
memcpy( live, slot->bytes, (size_t)slot->byteCount );
if ( b3ConvertBytesToCompound( live, slot->byteCount ) == NULL )
{
b3Free( live, (size_t)slot->byteCount );
return NULL;
}
slot->live = live;
return (const b3CompoundData*)live;
}
// Dispatch functions, one per op
@ -1015,13 +1030,13 @@ static void b3RecDispatch_CreateHullShape( const b3RecArgs_CreateHullShape* a, b
return;
}
uint32_t id = a->geometryId;
if ( id >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, id, b3_geometryHull );
if ( slot == NULL )
{
printf( "b3ReplayFile: hull geometryId %u out of range\n", id );
printf( "b3ReplayFile: hull geometryId %u is invalid\n", id );
rdr->ok = false;
return;
}
b3RegistrySlot* slot = rdr->slots + id;
b3BodyId bodyId = b3RecMakeBodyId( rdr, a->body );
// Hull is cloned by b3CreateHullShape into the world DB; no caching needed.
b3ShapeId gotId = b3CreateHullShape( bodyId, &a->def, (const b3HullData*)slot->bytes );
@ -1036,13 +1051,13 @@ static void b3RecDispatch_CreateMeshShape( const b3RecArgs_CreateMeshShape* a, b
return;
}
uint32_t id = a->geometryId;
if ( id >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, id, b3_geometryMesh );
if ( slot == NULL )
{
printf( "b3ReplayFile: mesh geometryId %u out of range\n", id );
printf( "b3ReplayFile: mesh geometryId %u is invalid\n", id );
rdr->ok = false;
return;
}
b3RegistrySlot* slot = rdr->slots + id;
const b3MeshData* mesh = b3RecGetLiveMesh( slot );
b3BodyId bodyId = b3RecMakeBodyId( rdr, a->body );
b3ShapeId gotId = b3CreateMeshShape( bodyId, &a->def, mesh, a->scale );
@ -1057,13 +1072,13 @@ static void b3RecDispatch_CreateHeightFieldShape( const b3RecArgs_CreateHeightFi
return;
}
uint32_t id = a->geometryId;
if ( id >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, id, b3_geometryHeightField );
if ( slot == NULL )
{
printf( "b3ReplayFile: heightfield geometryId %u out of range\n", id );
printf( "b3ReplayFile: heightfield geometryId %u is invalid\n", id );
rdr->ok = false;
return;
}
b3RegistrySlot* slot = rdr->slots + id;
const b3HeightFieldData* hf = (const b3HeightFieldData*)b3RecGetLiveHeightField( slot );
if ( hf == NULL )
{
@ -1084,14 +1099,20 @@ static void b3RecDispatch_CreateCompoundShape( const b3RecArgs_CreateCompoundSha
return;
}
uint32_t id = a->geometryId;
if ( id >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, id, b3_geometryCompound );
if ( slot == NULL )
{
printf( "b3ReplayFile: compound geometryId %u out of range\n", id );
printf( "b3ReplayFile: compound geometryId %u is invalid\n", id );
rdr->ok = false;
return;
}
const b3CompoundData* compound = b3RecGetLiveCompound( slot );
if ( compound == NULL )
{
printf( "b3ReplayFile: compound geometry %u is corrupt\n", id );
rdr->ok = false;
return;
}
b3RegistrySlot* slot = rdr->slots + id;
const b3CompoundData* compound = (const b3CompoundData*)b3RecGetLiveCompound( slot );
b3BodyId bodyId = b3RecMakeBodyId( rdr, a->body );
// b3CreateCompoundShape takes a non-const def pointer; cast away const for the scratch def
b3ShapeDef shapeDef = a->def;
@ -1172,13 +1193,13 @@ static void b3RecDispatch_ShapeSetCapsule( const b3RecArgs_ShapeSetCapsule* a, b
static void b3RecDispatch_ShapeSetHull( const b3RecArgs_ShapeSetHull* a, b3RecReader* rdr )
{
uint32_t id = a->geometryId;
if ( id >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, id, b3_geometryHull );
if ( slot == NULL )
{
printf( "b3ReplayFile: hull geometryId %u out of range\n", id );
printf( "b3ReplayFile: hull geometryId %u is invalid\n", id );
rdr->ok = false;
return;
}
b3RegistrySlot* slot = rdr->slots + id;
b3ShapeId shapeId = b3RecMakeShapeId( rdr, a->shape );
b3Shape_SetHull( shapeId, (const b3HullData*)slot->bytes );
}
@ -1186,13 +1207,13 @@ static void b3RecDispatch_ShapeSetHull( const b3RecArgs_ShapeSetHull* a, b3RecRe
static void b3RecDispatch_ShapeSetMesh( const b3RecArgs_ShapeSetMesh* a, b3RecReader* rdr )
{
uint32_t id = a->geometryId;
if ( id >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, id, b3_geometryMesh );
if ( slot == NULL )
{
printf( "b3ReplayFile: mesh geometryId %u out of range\n", id );
printf( "b3ReplayFile: mesh geometryId %u is invalid\n", id );
rdr->ok = false;
return;
}
b3RegistrySlot* slot = rdr->slots + id;
b3ShapeId shapeId = b3RecMakeShapeId( rdr, a->shape );
const b3MeshData* mesh = b3RecGetLiveMesh( slot );
b3Shape_SetMesh( shapeId, mesh, a->scale );
@ -2485,9 +2506,18 @@ static bool b3RecLoadSlots( b3RecReader* rdr, const void* data, int size, uint64
uint8_t kind = rp[0];
uint32_t byteCount = (uint32_t)rp[1] | ( (uint32_t)rp[2] << 8 ) | ( (uint32_t)rp[3] << 16 ) | ( (uint32_t)rp[4] << 24 );
rp += 5;
const char* error = NULL;
if ( rp + byteCount > dataEnd )
{
printf( "b3ReplayFile: registry entry %u bytes out of bounds\n", i );
error = "bytes out of bounds";
}
else if ( kind == b3_geometryCompound && byteCount < (uint32_t)sizeof( b3CompoundData ) )
{
error = "too small for a compound";
}
if ( error != NULL )
{
printf( "b3ReplayFile: registry entry %u %s\n", i, error );
for ( uint32_t j = 0; j < i; ++j )
{
if ( slots[j].bytes != NULL )

View file

@ -256,3 +256,9 @@ b3PlaneResult b3RecR_PLANERESULT( b3RecReader* rdr );
// Grow the reader's hit scratch to at least n entries, preserving contents. n is bounded by the
// file size since every recorded hit consumes at least one byte.
void b3RecEnsureHits( b3RecReader* rdr, int n );
// Get a geometry slot with kind validation.
b3RegistrySlot* b3RecGetSlot( b3RecReader* rdr, uint32_t id, b3GeometryKind kind );
// Get the recorded compound with the pointers fixed up.
const b3CompoundData* b3RecGetLiveCompound( b3RegistrySlot* slot );

View file

@ -725,13 +725,13 @@ static void b3DesShapes( b3SnapReader* r, b3World* world, b3RecReader* rdr )
{
break;
}
if ( rdr == NULL || gid >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, gid, b3_geometryHull );
if ( slot == NULL )
{
r->ok = false;
break;
}
// Hull is cloned into the world DB; pass raw bytes directly
b3RegistrySlot* slot = rdr->slots + gid;
dst->hull = b3AddHullToDatabase( world, (const b3HullData*)slot->bytes );
break;
}
@ -744,12 +744,12 @@ static void b3DesShapes( b3SnapReader* r, b3World* world, b3RecReader* rdr )
{
break;
}
if ( rdr == NULL || gid >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, gid, b3_geometryMesh );
if ( slot == NULL )
{
r->ok = false;
break;
}
b3RegistrySlot* slot = rdr->slots + gid;
// Mesh is a self-contained blob used by reference; point straight at the pristine bytes.
dst->mesh.data = (const b3MeshData*)slot->bytes;
dst->mesh.scale = scale;
@ -762,12 +762,12 @@ static void b3DesShapes( b3SnapReader* r, b3World* world, b3RecReader* rdr )
{
break;
}
if ( rdr == NULL || gid >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, gid, b3_geometryHeightField );
if ( slot == NULL )
{
r->ok = false;
break;
}
b3RegistrySlot* slot = rdr->slots + gid;
// Self-contained blob used by reference; point straight at the pristine bytes.
dst->heightField = (const b3HeightFieldData*)slot->bytes;
break;
@ -779,19 +779,12 @@ static void b3DesShapes( b3SnapReader* r, b3World* world, b3RecReader* rdr )
{
break;
}
if ( rdr == NULL || gid >= (uint32_t)rdr->slotCount )
b3RegistrySlot* slot = b3RecGetSlot( rdr, gid, b3_geometryCompound );
dst->compound = slot != NULL ? b3RecGetLiveCompound( slot ) : NULL;
if ( dst->compound == NULL )
{
r->ok = false;
break;
}
b3RegistrySlot* slot = rdr->slots + gid;
if ( slot->live == NULL )
{
slot->live = b3Alloc( (size_t)slot->byteCount );
memcpy( slot->live, slot->bytes, (size_t)slot->byteCount );
b3ConvertBytesToCompound( (uint8_t*)slot->live, slot->byteCount );
}
dst->compound = (const b3CompoundData*)slot->live;
break;
}
default:

View file

@ -4,7 +4,7 @@
#pragma once
#include "recording.h"
#include "recording_replay.h"
#include "replay.h"
#include <stdbool.h>
#include <stdint.h>