From 67ef937cd0586e0e5394a7366e61c02b57bce209 Mon Sep 17 00:00:00 2001 From: Erin Catto Date: Wed, 11 Nov 2026 13:59:56 -0800 Subject: [PATCH] fix race for tloc workaround static queries --- src/body.c | 17 +++++++++++++++++ src/body.h | 1 + src/physics_world.c | 8 +++++--- src/physics_world.h | 3 +++ src/solver_set.c | 4 ++++ src/world_snapshot.c | 2 ++ test/test_world.c | 6 +++++- 7 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/body.c b/src/body.c index 70bbc28..918552b 100644 --- a/src/body.c +++ b/src/body.c @@ -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; diff --git a/src/body.h b/src/body.h index b03063d..db82e4d 100644 --- a/src/body.h +++ b/src/body.h @@ -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. diff --git a/src/physics_world.c b/src/physics_world.c index 9ebc79e..14a5593 100644 --- a/src/physics_world.c +++ b/src/physics_world.c @@ -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 ) diff --git a/src/physics_world.h b/src/physics_world.h index 1436ee4..7c1a24b 100644 --- a/src/physics_world.h +++ b/src/physics_world.h @@ -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; diff --git a/src/solver_set.c b/src/solver_set.c index 53b75dd..8f6fa42 100644 --- a/src/solver_set.c +++ b/src/solver_set.c @@ -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; diff --git a/src/world_snapshot.c b/src/world_snapshot.c index 0f47969..d25423c 100644 --- a/src/world_snapshot.c +++ b/src/world_snapshot.c @@ -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 ) diff --git a/test/test_world.c b/test/test_world.c index c91c7ad..dafed60 100644 --- a/test/test_world.c +++ b/test/test_world.c @@ -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 };