Refactor NarrowPhaseAlgorithm and make possible to use single frame allocator for NarrowPhaseInput memory allocation

This commit is contained in:
Daniel Chappuis 2018-11-09 17:19:28 +01:00
parent 62a72eadac
commit 5e57ea896a
12 changed files with 66 additions and 33 deletions

View File

@ -52,7 +52,7 @@ CollisionDetection::CollisionDetection(CollisionWorld* world, MemoryManager& mem
: mMemoryManager(memoryManager), mCollisionDispatch(mMemoryManager.getPoolAllocator()), mWorld(world),
mOverlappingPairs(mMemoryManager.getPoolAllocator()), mBroadPhaseAlgorithm(*this),
mNoCollisionPairs(mMemoryManager.getPoolAllocator()), mIsCollisionShapesAdded(false),
mNarrowPhaseInput(mMemoryManager.getPoolAllocator()) {
mNarrowPhaseInput(mMemoryManager.getSingleFrameAllocator()) {
#ifdef IS_PROFILING_ACTIVE
@ -98,6 +98,9 @@ void CollisionDetection::computeMiddlePhase() {
RP3D_PROFILE("CollisionDetection::computeMiddlePhase()", mProfiler);
// Reserve memory for the narrow-phase input using cached capacity from previous frame
mNarrowPhaseInput.reserveMemory();
// For each possible collision pair of bodies
for (auto it = mOverlappingPairs.begin(); it != mOverlappingPairs.end(); ) {
@ -639,8 +642,6 @@ bool CollisionDetection::testOverlap(CollisionBody* body1, CollisionBody* body2)
// Test narrow-phase collision
bool isCollisionFound = testNarrowPhaseCollision(narrowPhaseInput, true, false, mMemoryManager.getPoolAllocator());
narrowPhaseInput.clear();
// No overlap has been found
return isCollisionFound;
}

View File

@ -102,6 +102,20 @@ void NarrowPhaseInfoBatch::resetContactPoints(uint index) {
contactPoints[index].clear();
}
// Initialize the containers using cached capacity
void NarrowPhaseInfoBatch::reserveMemory() {
overlappingPairs.reserve(mCachedCapacity);
collisionShapes1.reserve(mCachedCapacity);
collisionShapes2.reserve(mCachedCapacity);
shape1ToWorldTransforms.reserve(mCachedCapacity);
shape2ToWorldTransforms.reserve(mCachedCapacity);
collisionShapeAllocators.reserve(mCachedCapacity);
lastFrameCollisionInfos.reserve(mCachedCapacity);
isColliding.reserve(mCachedCapacity);
contactPoints.reserve(mCachedCapacity);
}
// Clear all the objects in the batch
void NarrowPhaseInfoBatch::clear() {
@ -121,13 +135,20 @@ void NarrowPhaseInfoBatch::clear() {
}
}
overlappingPairs.clear();
collisionShapes1.clear();
collisionShapes2.clear();
shape1ToWorldTransforms.clear();
shape2ToWorldTransforms.clear();
collisionShapeAllocators.clear();
lastFrameCollisionInfos.clear();
isColliding.clear();
contactPoints.clear();
// Note that we clear the following containers and we release their allocated memory. Therefore,
// if the memory allocator is a single frame allocator, the memory is deallocated and will be
// allocated in the next frame at a possibly different location in memory (remember that the
// location of the allocated memory of a single frame allocator might change between two frames)
mCachedCapacity = overlappingPairs.size();
overlappingPairs.clear(true);
collisionShapes1.clear(true);
collisionShapes2.clear(true);
shape1ToWorldTransforms.clear(true);
shape2ToWorldTransforms.clear(true);
collisionShapeAllocators.clear(true);
lastFrameCollisionInfos.clear(true);
isColliding.clear(true);
contactPoints.clear(true);
}

View File

@ -52,6 +52,9 @@ struct NarrowPhaseInfoBatch {
/// Memory allocator
MemoryAllocator& mMemoryAllocator;
/// Cached capacity
uint mCachedCapacity = 0;
public:
/// List of Broadphase overlapping pairs
@ -102,6 +105,9 @@ struct NarrowPhaseInfoBatch {
/// Reset the remaining contact points
void resetContactPoints(uint index);
// Initialize the containers using cached capacity
void reserveMemory();
/// Clear all the objects in the batch
void clear();
};

View File

@ -67,9 +67,9 @@ class CapsuleVsCapsuleAlgorithm : public NarrowPhaseAlgorithm {
CapsuleVsCapsuleAlgorithm& operator=(const CapsuleVsCapsuleAlgorithm& algorithm) = delete;
/// Compute the narrow-phase collision detection between two capsules
virtual bool testCollision(NarrowPhaseInfoBatch& narrowPhaseInfoBatch, uint batchStartIndex,
bool testCollision(NarrowPhaseInfoBatch& narrowPhaseInfoBatch, uint batchStartIndex,
uint batchNbItems, bool reportContacts, bool stopFirstContactFound,
MemoryAllocator& memoryAllocator) override;
MemoryAllocator& memoryAllocator);
};
}

View File

@ -70,9 +70,9 @@ class CapsuleVsConvexPolyhedronAlgorithm : public NarrowPhaseAlgorithm {
CapsuleVsConvexPolyhedronAlgorithm& operator=(const CapsuleVsConvexPolyhedronAlgorithm& algorithm) = delete;
/// Compute the narrow-phase collision detection between a capsule and a polyhedron
virtual bool testCollision(NarrowPhaseInfoBatch& narrowPhaseInfoBatch, uint batchStartIndex,
bool testCollision(NarrowPhaseInfoBatch& narrowPhaseInfoBatch, uint batchStartIndex,
uint batchNbItems, bool reportContacts, bool stopFirstContactFound,
MemoryAllocator& memoryAllocator) override;
MemoryAllocator& memoryAllocator);
};
}

View File

@ -65,9 +65,9 @@ class ConvexPolyhedronVsConvexPolyhedronAlgorithm : public NarrowPhaseAlgorithm
ConvexPolyhedronVsConvexPolyhedronAlgorithm& operator=(const ConvexPolyhedronVsConvexPolyhedronAlgorithm& algorithm) = delete;
/// Compute the narrow-phase collision detection between two convex polyhedra
virtual bool testCollision(NarrowPhaseInfoBatch& narrowPhaseInfoBatch, uint batchStartIndex,
bool testCollision(NarrowPhaseInfoBatch& narrowPhaseInfoBatch, uint batchStartIndex,
uint batchNbItems, bool reportContacts, bool stopFirstContactFound,
MemoryAllocator& memoryAllocator) override;
MemoryAllocator& memoryAllocator);
};
}

View File

@ -72,9 +72,6 @@ class NarrowPhaseAlgorithm {
// -------------------- Attributes -------------------- //
// Id of the algorithm (computed by hasing the algorithm name)
size_t mId;
#ifdef IS_PROFILING_ACTIVE
/// Pointer to the profiler
@ -99,11 +96,6 @@ class NarrowPhaseAlgorithm {
/// Deleted assignment operator
NarrowPhaseAlgorithm& operator=(const NarrowPhaseAlgorithm& algorithm) = delete;
/// Compute a contact info if the two bounding volumes collide
virtual bool testCollision(NarrowPhaseInfoBatch& narrowPhaseInfoBatch, uint batchStartIndex,
uint batchNbItems, bool reportContacts, bool stopFirstContactFound,
MemoryAllocator& memoryAllocator)=0;
#ifdef IS_PROFILING_ACTIVE
/// Set the profiler

View File

@ -68,6 +68,17 @@ void NarrowPhaseInput::addNarrowPhaseTest(OverlappingPair* pair, CollisionShape*
}
}
/// Reserve memory for the containers with cached capacity
void NarrowPhaseInput::reserveMemory() {
mSphereVsSphereBatch.reserveMemory();
mSphereVsCapsuleBatch.reserveMemory();
mCapsuleVsCapsuleBatch.reserveMemory();
mSphereVsConvexPolyhedronBatch.reserveMemory();
mCapsuleVsConvexPolyhedronBatch.reserveMemory();
mConvexPolyhedronVsConvexPolyhedronBatch.reserveMemory();
}
// Clear
void NarrowPhaseInput::clear() {

View File

@ -58,7 +58,6 @@ class NarrowPhaseInput {
NarrowPhaseInfoBatch mSphereVsConvexPolyhedronBatch;
NarrowPhaseInfoBatch mCapsuleVsConvexPolyhedronBatch;
NarrowPhaseInfoBatch mConvexPolyhedronVsConvexPolyhedronBatch;
public:
/// Constructor
@ -88,6 +87,9 @@ class NarrowPhaseInput {
/// Get a reference to the convex polyhedron vs convex polyhedron batch
NarrowPhaseInfoBatch& getConvexPolyhedronVsConvexPolyhedronBatch();
/// Reserve memory for the containers with cached capacity
void reserveMemory();
/// Clear
void clear();
};

View File

@ -66,9 +66,9 @@ class SphereVsCapsuleAlgorithm : public NarrowPhaseAlgorithm {
SphereVsCapsuleAlgorithm& operator=(const SphereVsCapsuleAlgorithm& algorithm) = delete;
/// Compute the narrow-phase collision detection between a sphere and a capsule
virtual bool testCollision(NarrowPhaseInfoBatch& narrowPhaseInfoBatch, uint batchStartIndex,
bool testCollision(NarrowPhaseInfoBatch& narrowPhaseInfoBatch, uint batchStartIndex,
uint batchNbItems, bool reportContacts, bool stopFirstContactFound,
MemoryAllocator& memoryAllocator) override;
MemoryAllocator& memoryAllocator);
};
}

View File

@ -71,9 +71,9 @@ class SphereVsConvexPolyhedronAlgorithm : public NarrowPhaseAlgorithm {
SphereVsConvexPolyhedronAlgorithm& operator=(const SphereVsConvexPolyhedronAlgorithm& algorithm) = delete;
/// Compute the narrow-phase collision detection between a sphere and a convex polyhedron
virtual bool testCollision(NarrowPhaseInfoBatch& narrowPhaseInfoBatch, uint batchStartIndex,
bool testCollision(NarrowPhaseInfoBatch& narrowPhaseInfoBatch, uint batchStartIndex,
uint batchNbItems, bool reportContacts, bool stopFirstContactFound,
MemoryAllocator& memoryAllocator) override;
MemoryAllocator& memoryAllocator);
};
}

View File

@ -65,9 +65,9 @@ class SphereVsSphereAlgorithm : public NarrowPhaseAlgorithm {
SphereVsSphereAlgorithm& operator=(const SphereVsSphereAlgorithm& algorithm) = delete;
/// Compute a contact info if the two bounding volume collide
virtual bool testCollision(NarrowPhaseInfoBatch& narrowPhaseInfoBatch, uint batchStartIndex,
bool testCollision(NarrowPhaseInfoBatch& narrowPhaseInfoBatch, uint batchStartIndex,
uint batchNbItems, bool reportContacts, bool stopFirstContactFound,
MemoryAllocator& memoryAllocator) override;
MemoryAllocator& memoryAllocator);
};
}