Manifold allocation cap (#227)
Degenerate scenarios can lead to a huge number of manifolds per contact. This creates memory and performance problems. So I'm capping them to 8 using cheap heuristics for culling.
This commit is contained in:
parent
be8c731890
commit
8f3b2e12eb
18 changed files with 1161 additions and 138 deletions
|
|
@ -154,7 +154,7 @@ void b3AddContactToGraph( b3World* world, b3Contact* contact )
|
|||
|
||||
if ( isScalar )
|
||||
{
|
||||
B3_ASSERT( contact->manifoldCount < UINT16_MAX );
|
||||
B3_ASSERT( contact->manifoldCount <= B3_MAX_MANIFOLDS );
|
||||
b3ContactSpec spec = {
|
||||
.contactId = contact->contactId,
|
||||
.manifoldStart = 0,
|
||||
|
|
|
|||
|
|
@ -1154,8 +1154,8 @@ static void b3CollideBoundaryEdge( b3LocalManifold* manifold, const b3Shape* sha
|
|||
|
||||
static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shapeA, const b3Shape* shapeB,
|
||||
b3Transform transformAtoB, b3TriangleCache* triangleCaches, int triangleCount,
|
||||
bool enableSpeculative, bool handleGhosts, bool isFast, b3TaskContext* context,
|
||||
b3Arena* arena )
|
||||
bool enableSpeculative, bool handleGhosts, bool isFast, int manifoldCapacity,
|
||||
b3TaskContext* context, b3Arena* arena )
|
||||
{
|
||||
*clustersOut = NULL;
|
||||
|
||||
|
|
@ -1647,6 +1647,100 @@ static int b3CollideMeshTriangles( b3Cluster** clustersOut, const b3Shape* shape
|
|||
return 0;
|
||||
}
|
||||
|
||||
if ( clusterCount > manifoldCapacity )
|
||||
{
|
||||
float* clusterSeparations = b3Bump( arena, clusterCount * sizeof( float ) );
|
||||
int* clusterRemap = b3Bump( arena, clusterCount * sizeof( int ) );
|
||||
for ( int i = 0; i < clusterCount; ++i )
|
||||
{
|
||||
clusterSeparations[i] = FLT_MAX;
|
||||
clusterRemap[i] = B3_NULL_INDEX;
|
||||
}
|
||||
|
||||
// Compute cluster separations.
|
||||
for ( int i = 0; i < acceptedManifoldCount; ++i )
|
||||
{
|
||||
const b3LocalManifold* manifold = acceptedManifolds[i];
|
||||
int clusterIndex = clusterMemberships[i];
|
||||
for ( int j = 0; j < manifold->pointCount; ++j )
|
||||
{
|
||||
clusterSeparations[clusterIndex] = b3MinFloat( clusterSeparations[clusterIndex], manifold->points[j].separation );
|
||||
}
|
||||
}
|
||||
|
||||
int keptCount = 0;
|
||||
|
||||
// If there are 8 or more manifolds then find the best per octant region.
|
||||
if ( manifoldCapacity >= 8 )
|
||||
{
|
||||
int octantClusters[8] = { B3_NULL_INDEX, B3_NULL_INDEX, B3_NULL_INDEX, B3_NULL_INDEX,
|
||||
B3_NULL_INDEX, B3_NULL_INDEX, B3_NULL_INDEX, B3_NULL_INDEX };
|
||||
for ( int i = 0; i < clusterCount; ++i )
|
||||
{
|
||||
b3Vec3 normal = b3InvRotateVector( transformAtoB.q, clusters[i].manifoldNormal );
|
||||
int octant = ( normal.x > 0.0f ? 1 : 0 ) | ( normal.y > 0.0f ? 2 : 0 ) | ( normal.z > 0.0f ? 4 : 0 );
|
||||
int best = octantClusters[octant];
|
||||
if ( best == B3_NULL_INDEX || clusterSeparations[i] < clusterSeparations[best] )
|
||||
{
|
||||
octantClusters[octant] = i;
|
||||
}
|
||||
}
|
||||
|
||||
for ( int i = 0; i < 8; ++i )
|
||||
{
|
||||
if ( octantClusters[i] != B3_NULL_INDEX )
|
||||
{
|
||||
// This cluster wins the octant.
|
||||
clusterRemap[octantClusters[i]] = 0;
|
||||
keptCount += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If the octant culling left some capacity then find the deepest remaining clusters.
|
||||
while ( keptCount < manifoldCapacity )
|
||||
{
|
||||
int best = B3_NULL_INDEX;
|
||||
for ( int i = 0; i < clusterCount; ++i )
|
||||
{
|
||||
if ( clusterRemap[i] != B3_NULL_INDEX )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( best == B3_NULL_INDEX || clusterSeparations[i] < clusterSeparations[best] )
|
||||
{
|
||||
best = i;
|
||||
}
|
||||
}
|
||||
|
||||
B3_ASSERT( best != B3_NULL_INDEX );
|
||||
|
||||
// This cluster wins the separation test.
|
||||
clusterRemap[best] = 0;
|
||||
keptCount += 1;
|
||||
}
|
||||
|
||||
// Remap the winning clusters.
|
||||
keptCount = 0;
|
||||
for ( int i = 0; i < clusterCount; ++i )
|
||||
{
|
||||
if ( clusterRemap[i] != B3_NULL_INDEX )
|
||||
{
|
||||
clusters[keptCount] = clusters[i];
|
||||
clusterRemap[i] = keptCount;
|
||||
keptCount += 1;
|
||||
}
|
||||
}
|
||||
|
||||
for ( int i = 0; i < acceptedManifoldCount; ++i )
|
||||
{
|
||||
clusterMemberships[i] = clusterRemap[clusterMemberships[i]];
|
||||
}
|
||||
|
||||
clusterCount = keptCount;
|
||||
}
|
||||
|
||||
// Setup clusters
|
||||
b3LocalManifoldPoint* clusterPoints = b3Bump( arena, clusterPointCount * sizeof( b3LocalManifoldPoint ) );
|
||||
int pointOffset = 0;
|
||||
|
|
@ -1718,7 +1812,8 @@ bool b3ComputeMeshManifolds( b3World* world, int workerIndex, b3Contact* contact
|
|||
b3Cluster* clusters;
|
||||
int clusterCount = b3CollideMeshTriangles( &clusters, shapeA, shapeB, transformAtoB,
|
||||
meshContact->triangleCache.data, meshContact->triangleCache.count,
|
||||
enableSpeculative, handleGhosts, isFast, context, &arena );
|
||||
enableSpeculative, handleGhosts, isFast, B3_MAX_MANIFOLDS, context,
|
||||
&arena );
|
||||
|
||||
if ( clusterCount == 0 )
|
||||
{
|
||||
|
|
@ -1985,8 +2080,10 @@ static int b3CollideMeshAndShape( b3Manifold* manifolds, int capacity, const b3M
|
|||
b3Transform transformAtoB = b3InvertTransform( transformBtoA );
|
||||
|
||||
b3Cluster* clusters;
|
||||
int manifoldCapacity = b3MinInt( capacity, B3_MAX_MANIFOLDS );
|
||||
int clusterCount = b3CollideMeshTriangles( &clusters, &meshShape, shapeB, transformAtoB, triangleCaches,
|
||||
triangleCount, enableSpeculative, handleGhosts, false, NULL, &arena );
|
||||
triangleCount, enableSpeculative, handleGhosts, false, manifoldCapacity,
|
||||
NULL, &arena );
|
||||
|
||||
int count = b3MinInt( clusterCount, capacity );
|
||||
b3Matrix3 matrixB = b3MakeMatrixFromQuat( transformBtoA.q );
|
||||
|
|
|
|||
|
|
@ -724,7 +724,7 @@ static void b3CollideTask( int startIndex, int endIndex, int workerIndex, void*
|
|||
|
||||
// Diagnostics
|
||||
taskContext->recycledContactCount += 1;
|
||||
int bucketIndex = b3MinInt( manifoldCount, B3_CONTACT_MANIFOLD_COUNT_BUCKETS - 1 );
|
||||
int bucketIndex = b3MinInt( manifoldCount, B3_MAX_MANIFOLDS );
|
||||
if ( bucketIndex > 0 )
|
||||
{
|
||||
taskContext->manifoldCounts[bucketIndex - 1] += 1;
|
||||
|
|
@ -748,7 +748,7 @@ static void b3CollideTask( int startIndex, int endIndex, int workerIndex, void*
|
|||
transformA, shapeB,
|
||||
bodySimB->localCenter, transformB, isFast, taskContext->arena );
|
||||
|
||||
int bucketIndex = b3MinInt( contact->manifoldCount, B3_CONTACT_MANIFOLD_COUNT_BUCKETS - 1 );
|
||||
int bucketIndex = b3MinInt( contact->manifoldCount, B3_MAX_MANIFOLDS );
|
||||
if ( bucketIndex > 0 )
|
||||
{
|
||||
taskContext->manifoldCounts[bucketIndex - 1] += 1;
|
||||
|
|
@ -910,15 +910,14 @@ static void b3Collide( b3StepContext* context )
|
|||
b3BitSet* bitSet = &world->taskContexts.data[0].contactStateBitSet;
|
||||
world->satCallCount = satMultiplier * world->taskContexts.data[0].satCallCount;
|
||||
world->satCacheHitCount = satMultiplier * world->taskContexts.data[0].satCacheHitCount;
|
||||
memcpy( world->manifoldCounts, world->taskContexts.data[0].manifoldCounts,
|
||||
B3_CONTACT_MANIFOLD_COUNT_BUCKETS * sizeof( int ) );
|
||||
memcpy( world->manifoldCounts, world->taskContexts.data[0].manifoldCounts, B3_MAX_MANIFOLDS * sizeof( int ) );
|
||||
|
||||
for ( int i = 1; i < world->workerCount; ++i )
|
||||
{
|
||||
b3InPlaceUnion( bitSet, &world->taskContexts.data[i].contactStateBitSet );
|
||||
world->satCallCount += world->taskContexts.data[i].satCallCount;
|
||||
world->satCacheHitCount += world->taskContexts.data[i].satCacheHitCount;
|
||||
for ( int j = 0; j < B3_CONTACT_MANIFOLD_COUNT_BUCKETS; ++j )
|
||||
for ( int j = 0; j < B3_MAX_MANIFOLDS; ++j )
|
||||
{
|
||||
world->manifoldCounts[j] += world->taskContexts.data[i].manifoldCounts[j];
|
||||
}
|
||||
|
|
@ -2642,7 +2641,7 @@ b3Counters b3World_GetCounters( b3WorldId worldId )
|
|||
|
||||
s.satCallCount = world->satCallCount;
|
||||
s.satCacheHitCount = world->satCacheHitCount;
|
||||
memcpy( s.manifoldCounts, world->manifoldCounts, B3_CONTACT_MANIFOLD_COUNT_BUCKETS * sizeof( int ) );
|
||||
memcpy( s.manifoldCounts, world->manifoldCounts, B3_MAX_MANIFOLDS * sizeof( int ) );
|
||||
s.stackUsed = world->stack.maxAllocation;
|
||||
s.byteCount = (int64_t)b3GetWorldByteCount( world, false );
|
||||
s.taskCount = world->taskCount;
|
||||
|
|
|
|||
|
|
@ -115,7 +115,7 @@ typedef struct b3TaskContext
|
|||
b3DebugLine lines[B3_DEBUG_LINE_CAPACITY];
|
||||
int lineCount;
|
||||
|
||||
int manifoldCounts[B3_CONTACT_MANIFOLD_COUNT_BUCKETS];
|
||||
int manifoldCounts[B3_MAX_MANIFOLDS];
|
||||
// Prevent false sharing
|
||||
char cacheLine[64];
|
||||
} b3TaskContext;
|
||||
|
|
@ -254,7 +254,7 @@ typedef struct b3World
|
|||
b3Profile profile;
|
||||
int satCallCount;
|
||||
int satCacheHitCount;
|
||||
int manifoldCounts[B3_CONTACT_MANIFOLD_COUNT_BUCKETS];
|
||||
int manifoldCounts[B3_MAX_MANIFOLDS];
|
||||
|
||||
b3Capacity maxCapacity;
|
||||
|
||||
|
|
@ -353,6 +353,8 @@ static inline b3Manifold* b3AllocateManifolds( b3World* world, int count )
|
|||
return NULL;
|
||||
}
|
||||
|
||||
B3_ASSERT( count <= B3_MAX_MANIFOLDS );
|
||||
|
||||
int index = count - 1;
|
||||
|
||||
// Need lock because this is called from the parallel narrow phase
|
||||
|
|
@ -378,6 +380,8 @@ static inline void b3FreeManifolds( b3World* world, b3Manifold* manifolds, int c
|
|||
return;
|
||||
}
|
||||
|
||||
B3_ASSERT( count <= B3_MAX_MANIFOLDS );
|
||||
|
||||
int index = count - 1;
|
||||
b3LockMutex( world->manifoldAllocatorMutex );
|
||||
b3BlockAllocator* allocator = b3Array_Get( world->manifoldAllocators, index );
|
||||
|
|
|
|||
|
|
@ -925,7 +925,8 @@ static void b3DesContacts( b3SnapReader* r, b3World* world )
|
|||
|
||||
if ( isLive && manifoldCount > 0 )
|
||||
{
|
||||
if ( b3SnapCheckCount( r, manifoldCount, (int)sizeof( b3Manifold ), (int)sizeof( b3Manifold ) ) == false )
|
||||
if ( manifoldCount > B3_MAX_MANIFOLDS ||
|
||||
b3SnapCheckCount( r, manifoldCount, (int)sizeof( b3Manifold ), (int)sizeof( b3Manifold ) ) == false )
|
||||
{
|
||||
r->ok = false;
|
||||
break;
|
||||
|
|
|
|||
Loading…
Reference in a new issue