fix race for tloc workaround static queries

This commit is contained in:
Erin Catto 2026-11-11 13:59:56 -08:00
commit 67ef937cd0
7 changed files with 37 additions and 4 deletions

View file

@ -38,6 +38,18 @@ b3WorldTransform b3GetBodyTransformQuick( b3World* world, b3Body* body )
return bodySim->transform;
}
b3WorldTransform b3GetQueryBodyTransform( b3World* world, b3Body* body )
{
if ( body->setIndex == b3_staticSet )
{
// todo comment
B3_ASSERT( 0 <= body->localIndex );
return world->staticBodySims[body->localIndex].transform;
}
return b3GetBodyTransformQuick( world, body );
}
b3WorldTransform b3GetBodyTransform( b3World* world, int bodyId )
{
b3Body* body = b3Array_Get( world->bodies, bodyId );
@ -244,6 +256,11 @@ b3BodyId b3CreateBody( b3WorldId worldId, const b3BodyDef* def )
b3SolverSet* set = b3Array_Get( world->solverSets, setId );
b3BodySim* bodySim = b3Array_Emplace( set->bodySims );
if ( setId == b3_staticSet )
{
world->staticBodySims = set->bodySims.data;
}
*bodySim = (b3BodySim){ 0 };
bodySim->transform.p = def->position;
bodySim->transform.q = def->rotation;

View file

@ -232,6 +232,7 @@ typedef struct b3BodySim
b3Body* b3GetBodyFullId( b3World* world, b3BodyId bodyId );
b3WorldTransform b3GetBodyTransformQuick( b3World* world, b3Body* body );
b3WorldTransform b3GetQueryBodyTransform( b3World* world, b3Body* body );
b3WorldTransform b3GetBodyTransform( b3World* world, int bodyId );
// Create a b3BodyId from a raw id.

View file

@ -275,6 +275,7 @@ b3WorldId b3CreateWorld( const b3WorldDef* def )
b3Array_Push( world->solverSets, set );
b3Array_Reserve( world->solverSets.data[b3_staticSet].bodySims, b3MaxInt( 16, def->capacity.staticBodyCount ) );
B3_ASSERT( world->solverSets.data[b3_staticSet].setIndex == b3_staticSet );
world->staticBodySims = world->solverSets.data[b3_staticSet].bodySims.data;
// disabled set
set.setIndex = b3AllocId( &world->solverSetIdPool );
@ -2800,7 +2801,7 @@ static bool b3TreeOverlapCallback( int proxyId, uint64_t userData, void* context
// Re-center on the query origin so the overlap test stays in float precision far from the origin
b3Body* body = b3Array_Get( world->bodies, shape->bodyId );
b3Transform transform = b3ToRelativeTransform( b3GetBodyTransformQuick( world, body ), worldContext->origin );
b3Transform transform = b3ToRelativeTransform( b3GetQueryBodyTransform( world, body ), worldContext->origin );
bool overlapping = b3OverlapShape( shape, transform, &worldContext->proxy );
if ( overlapping == false )
@ -3003,7 +3004,7 @@ static float RayCastCallback( const b3RayCastInput* input, int proxyId, uint64_t
}
b3Body* body = b3Array_Get( world->bodies, shape->bodyId );
b3WorldTransform bodyTransform = b3GetBodyTransformQuick( world, body );
b3WorldTransform bodyTransform = b3GetQueryBodyTransform( world, body );
b3Transform transform = b3ToRelativeTransform( bodyTransform, worldContext->origin );
b3RayCastInput localInput = *input;
@ -3223,7 +3224,7 @@ static float b3ShapeCastCallback( const b3BoxCastInput* input, int proxyId, uint
// Re-center on the query origin so the per-shape cast stays in float precision far from the origin
b3Body* body = b3Array_Get( world->bodies, shape->bodyId );
b3Transform transform = b3ToRelativeTransform( b3GetBodyTransformQuick( world, body ), worldContext->origin );
b3Transform transform = b3ToRelativeTransform( b3GetQueryBodyTransform( world, body ), worldContext->origin );
b3CastOutput output = b3ShapeCastShape( shape, transform, &localInput );
@ -3767,6 +3768,7 @@ void b3ValidateSolverSets( b3World* world )
B3_ASSERT( b3GetIdCapacity( &world->jointIdPool ) == world->joints.count );
B3_ASSERT( b3GetIdCapacity( &world->islandIdPool ) == world->islands.count );
B3_ASSERT( b3GetIdCapacity( &world->solverSetIdPool ) == world->solverSets.count );
B3_ASSERT( world->staticBodySims == world->solverSets.data[b3_staticSet].bodySims.data );
// Ensure the encoded body sim indices are correct.
for ( int contactIndex = 0; contactIndex < world->contacts.count; ++contactIndex )

View file

@ -152,6 +152,9 @@ typedef struct b3World
// sims. The remaining sets are sleeping islands.
b3Array( b3SolverSet ) solverSets;
// todo comment
b3BodySim* staticBodySims;
// Used to create stable ids for joints
b3IdPool jointIdPool;

View file

@ -579,6 +579,10 @@ void b3TransferBody( b3World* world, b3SolverSet* targetSet, b3SolverSet* source
int targetIndex = targetSet->bodySims.count;
b3BodySim* targetSim = b3Array_Emplace( targetSet->bodySims );
memcpy( targetSim, sourceSim, sizeof( b3BodySim ) );
if ( targetSet->setIndex == b3_staticSet )
{
world->staticBodySims = targetSet->bodySims.data;
}
// Clear transient body flags
body->flags &= ~b3_bodyTransientFlags;

View file

@ -1223,6 +1223,8 @@ bool b3DeserializeIntoShell( const uint8_t* data, int size, b3World* world, b3Re
{
b3DesSolverSet( r, world->solverSets.data + i );
}
world->staticBodySims = setCount > b3_staticSet ? world->solverSets.data[b3_staticSet].bodySims.data : NULL;
}
if ( !r->ok )

View file

@ -1958,12 +1958,15 @@ static int TestOverlapStaticDuringStep( void )
b3CreateHullShape( bodyId, &shapeDef, &cube.base );
}
// Islands falling asleep during the step grow the solver set array under the reader
int setCapacity = b3GetWorldFromId( worldId )->solverSets.capacity;
StaticQueryShared shared = { 0 };
shared.worldId = worldId;
b3Thread* thread = b3CreateThread( StaticQueryThread, &shared, "static query" );
b3BoxHull post = b3MakeBoxHull( 0.1f, 1.0f, 0.1f );
for ( int step = 0; step < 60; ++step )
for ( int step = 0; step < 120; ++step )
{
// The query thread is idle, so the static tree may change.
b3BodyDef bodyDef = b3DefaultBodyDef();
@ -1994,6 +1997,7 @@ static int TestOverlapStaticDuringStep( void )
ENSURE( b3AtomicLoadInt( &shared.queryCount ) > 0 );
ENSURE( b3AtomicLoadInt( &shared.failCount ) == 0 );
ENSURE( b3GetWorldFromId( worldId )->solverSets.capacity > setCapacity );
// The serial path agrees with the regular overlap filtered to static bodies.
StaticQueryHits hits = { worldId, 0, false };