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

@ -793,11 +793,10 @@ typedef struct
// The compound tree stores child indices. Remap to the pool slot the flatten
// pass assigned, then append the child under the body transform.
static bool CompoundCullCallback( int proxyId, uint64_t userData, void* context )
static bool CompoundCullCallback( const b3CompoundData* compound, int childIndex, void* context )
{
(void)proxyId;
(void)compound;
CompoundCullContext* ctx = (CompoundCullContext*)context;
const int childIndex = (int)userData;
if ( childIndex < 0 || childIndex >= ctx->parent->compound.childMapCount )
{
return true;
@ -888,7 +887,7 @@ static void DrawShape( void* userShape, b3WorldTransform shapeTransform, b3HexCo
// If the whole compound sits inside the view the query returns every child,
// so walking the list is cheaper. Both boxes are in the draw origin frame.
b3AABB rootRel = b3AABB_Transform( shapeRelative, b3DynamicTree_GetRootBounds( &us->compound.data->tree ) );
b3AABB rootRel = b3AABB_Transform( shapeRelative, us->compound.data->bounds );
if ( b3AABB_Contains( viewRel, rootRel ) )
{
cull = false;
@ -901,7 +900,7 @@ static void DrawShape( void* userShape, b3WorldTransform shapeTransform, b3HexCo
b3AABB localCull = b3AABB_Transform( b3InvertTransform( shapeRelative ), viewRel );
CompoundCullContext ctx = { us, shapeRelative, c, metallic, roughness, shadowCast, hk, 0 };
b3DynamicTree_Query( &us->compound.data->tree, localCull, ~0ull, false, CompoundCullCallback, &ctx );
b3QueryCompound( us->compound.data, localCull, CompoundCullCallback, &ctx );
s_adapter.lastCompoundAppended += ctx.appended;
s_adapter.lastCompoundTotal += us->compound.childMapCount;

View file

@ -763,6 +763,93 @@ public:
static int sampleMeshDropBenchmark = RegisterSample( "Benchmark", "Mesh Drop", BenchmarkMeshDrop::Create );
class BenchmarkVillage : public Sample
{
public:
explicit BenchmarkVillage( SampleContext* context )
: Sample( context )
{
if ( context->restart == false )
{
m_camera->SetView( 45.0f, 30.0f, 300.0f, b3Pos_zero );
}
b3Capacity capacity = {};
GetVillageCapacity( &capacity );
CreateWorld( &capacity );
CreateVillage( m_worldId );
}
~BenchmarkVillage() override
{
DestroyVillage();
}
void Step() override
{
StepVillage( m_worldId, m_stepCount );
Sample::Step();
}
bool DrawControls() override
{
ImGui::Checkbox( "Draw Casts", &m_drawCasts );
ImGui::Checkbox( "Draw Spheres", &m_drawSpheres );
return true;
}
void Render() override
{
Sample::Render();
VillageCurtain curtain = GetVillageCurtain();
int hitCount = 0;
for ( int i = 0; i < curtain.castCount; ++i )
{
const VillageCast& cast = curtain.casts[i];
hitCount += cast.hit ? 1 : 0;
if ( m_drawCasts == false )
{
continue;
}
b3Pos end = cast.origin + cast.fraction * curtain.translation;
DrawLine( cast.origin, end, MakeColor( cast.hit ? b3_colorYellow : b3_colorGray ) );
if ( cast.hit == false )
{
continue;
}
DrawPoint( cast.point, 4.0f, MakeColor( b3_colorGreen ) );
DrawLine( cast.point, cast.point + 0.5f * cast.normal, MakeColor( b3_colorGreen ) );
if ( m_drawSpheres )
{
b3WorldTransform transform = b3WorldTransform_identity;
transform.p = end;
DrawSphereEx( transform, curtain.radius, MakeColorAlpha( b3_colorPurple, 0.5f ), 0.0f, 0.5f,
TRANSPARENT_SHADOW_NONE );
}
}
DrawTextLine( "casts = %d, hits = %d", curtain.castCount, hitCount );
}
static Sample* Create( SampleContext* context )
{
return new BenchmarkVillage( context );
}
bool m_drawCasts = true;
bool m_drawSpheres = true;
};
static int sampleVillageBenchmark = RegisterSample( "Benchmark", "Village", BenchmarkVillage::Create );
struct ShapeUserData
{
int row;

View file

@ -93,7 +93,7 @@ public:
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
DrawAxes( b3MakeWorldTransform( transform ), 1.0f );
int height = b3DynamicTree_GetHeight( &m_compound->tree );
int height = m_compound->treeHeight;
DrawTextLine( "compound tree height = %d", height );
}
@ -153,7 +153,7 @@ public:
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
DrawAxes( b3MakeWorldTransform( transform ), 1.0f );
int height = b3DynamicTree_GetHeight( &m_compound->tree );
int height = m_compound->treeHeight;
DrawTextLine( "compound tree height = %d", height );
}
@ -227,7 +227,7 @@ public:
b3Transform transform = { { 0.0f, 0.01f, 0.0f }, b3Quat_identity };
DrawAxes( b3MakeWorldTransform( transform ), 1.0f );
int height = b3DynamicTree_GetHeight( &m_compound->tree );
int height = m_compound->treeHeight;
DrawTextLine( "compound tree height = %d", height );
}
@ -345,8 +345,8 @@ public:
DrawTextLine( "compound hull count = %d, mesh count = %d", m_compound->hullCount, m_compound->meshCount );
DrawTextLine( "compound byte count = %d", m_compound->byteCount );
int treeBytes = b3DynamicTree_GetByteCount( &m_compound->tree );
int height = b3DynamicTree_GetHeight( &m_compound->tree );
int treeBytes = m_compound->nodeCount * (int)sizeof( b3TreeNode );
int height = m_compound->treeHeight;
DrawTextLine( "compound tree byte count = %d, height = %d", treeBytes, height );
}
@ -461,8 +461,8 @@ public:
DrawTextLine( "compound instance count = %d, byte count = %d", m_compound->meshCount, m_compound->byteCount );
int treeBytes = b3DynamicTree_GetByteCount( &m_compound->tree );
int height = b3DynamicTree_GetHeight( &m_compound->tree );
int treeBytes = m_compound->nodeCount * (int)sizeof( b3TreeNode );
int height = m_compound->treeHeight;
DrawTextLine( "compound tree byte count = %d, height = %d", treeBytes, height );
}
@ -700,8 +700,8 @@ public:
m_compound->hullCount, m_compound->meshCount, m_compound->sphereCount );
DrawTextLine( "compound byte count = %d", m_compound->byteCount );
int treeBytes = b3DynamicTree_GetByteCount( &m_compound->tree );
int height = b3DynamicTree_GetHeight( &m_compound->tree );
int treeBytes = m_compound->nodeCount * (int)sizeof( b3TreeNode );
int height = m_compound->treeHeight;
DrawTextLine( "compound tree byte count = %d, height = %d", treeBytes, height );
int total = 0;

View file

@ -1538,6 +1538,20 @@ public:
b3AABB aabb = b3Shape_GetAABB( shape );
ImGui::Text( "aabb (%.2f, %.2f, %.2f)", aabb.lowerBound.x, aabb.lowerBound.y, aabb.lowerBound.z );
ImGui::Text( " (%.2f, %.2f, %.2f)", aabb.upperBound.x, aabb.upperBound.y, aabb.upperBound.z );
if ( b3Shape_GetType( shape ) == b3_compoundShape )
{
const b3CompoundData* compound = b3Shape_GetCompound( shape );
int childCount = compound->capsuleCount + compound->hullCount + compound->meshCount + compound->sphereCount;
ImGui::Text( "children %d", childCount );
ImGui::Text( " capsules %d", compound->capsuleCount );
ImGui::Text( " hulls %d (%d unique)", compound->hullCount, compound->sharedHullCount );
ImGui::Text( " meshes %d (%d unique)", compound->meshCount, compound->sharedMeshCount );
ImGui::Text( " spheres %d", compound->sphereCount );
ImGui::Text( "materials %d", compound->materialCount );
ImGui::Text( "tree height %d nodes %d", compound->treeHeight, compound->nodeCount );
ImGui::Text( "bytes %d", compound->byteCount );
}
}
void DrawContactDetail( b3BodyId body )