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), : mMemoryManager(memoryManager), mCollisionDispatch(mMemoryManager.getPoolAllocator()), mWorld(world),
mOverlappingPairs(mMemoryManager.getPoolAllocator()), mBroadPhaseAlgorithm(*this), mOverlappingPairs(mMemoryManager.getPoolAllocator()), mBroadPhaseAlgorithm(*this),
mNoCollisionPairs(mMemoryManager.getPoolAllocator()), mIsCollisionShapesAdded(false), mNoCollisionPairs(mMemoryManager.getPoolAllocator()), mIsCollisionShapesAdded(false),
mNarrowPhaseInput(mMemoryManager.getPoolAllocator()) { mNarrowPhaseInput(mMemoryManager.getSingleFrameAllocator()) {
#ifdef IS_PROFILING_ACTIVE #ifdef IS_PROFILING_ACTIVE
@ -98,6 +98,9 @@ void CollisionDetection::computeMiddlePhase() {
RP3D_PROFILE("CollisionDetection::computeMiddlePhase()", mProfiler); 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 each possible collision pair of bodies
for (auto it = mOverlappingPairs.begin(); it != mOverlappingPairs.end(); ) { for (auto it = mOverlappingPairs.begin(); it != mOverlappingPairs.end(); ) {
@ -639,8 +642,6 @@ bool CollisionDetection::testOverlap(CollisionBody* body1, CollisionBody* body2)
// Test narrow-phase collision // Test narrow-phase collision
bool isCollisionFound = testNarrowPhaseCollision(narrowPhaseInput, true, false, mMemoryManager.getPoolAllocator()); bool isCollisionFound = testNarrowPhaseCollision(narrowPhaseInput, true, false, mMemoryManager.getPoolAllocator());
narrowPhaseInput.clear();
// No overlap has been found // No overlap has been found
return isCollisionFound; return isCollisionFound;
} }

View File

@ -102,6 +102,20 @@ void NarrowPhaseInfoBatch::resetContactPoints(uint index) {
contactPoints[index].clear(); 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 // Clear all the objects in the batch
void NarrowPhaseInfoBatch::clear() { void NarrowPhaseInfoBatch::clear() {
@ -121,13 +135,20 @@ void NarrowPhaseInfoBatch::clear() {
} }
} }
overlappingPairs.clear(); // Note that we clear the following containers and we release their allocated memory. Therefore,
collisionShapes1.clear(); // if the memory allocator is a single frame allocator, the memory is deallocated and will be
collisionShapes2.clear(); // allocated in the next frame at a possibly different location in memory (remember that the
shape1ToWorldTransforms.clear(); // location of the allocated memory of a single frame allocator might change between two frames)
shape2ToWorldTransforms.clear();
collisionShapeAllocators.clear(); mCachedCapacity = overlappingPairs.size();
lastFrameCollisionInfos.clear();
isColliding.clear(); overlappingPairs.clear(true);
contactPoints.clear(); 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 /// Memory allocator
MemoryAllocator& mMemoryAllocator; MemoryAllocator& mMemoryAllocator;
/// Cached capacity
uint mCachedCapacity = 0;
public: public:
/// List of Broadphase overlapping pairs /// List of Broadphase overlapping pairs
@ -102,6 +105,9 @@ struct NarrowPhaseInfoBatch {
/// Reset the remaining contact points /// Reset the remaining contact points
void resetContactPoints(uint index); void resetContactPoints(uint index);
// Initialize the containers using cached capacity
void reserveMemory();
/// Clear all the objects in the batch /// Clear all the objects in the batch
void clear(); void clear();
}; };

View File

@ -67,9 +67,9 @@ class CapsuleVsCapsuleAlgorithm : public NarrowPhaseAlgorithm {
CapsuleVsCapsuleAlgorithm& operator=(const CapsuleVsCapsuleAlgorithm& algorithm) = delete; CapsuleVsCapsuleAlgorithm& operator=(const CapsuleVsCapsuleAlgorithm& algorithm) = delete;
/// Compute the narrow-phase collision detection between two capsules /// 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, 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; CapsuleVsConvexPolyhedronAlgorithm& operator=(const CapsuleVsConvexPolyhedronAlgorithm& algorithm) = delete;
/// Compute the narrow-phase collision detection between a capsule and a polyhedron /// 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, 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; ConvexPolyhedronVsConvexPolyhedronAlgorithm& operator=(const ConvexPolyhedronVsConvexPolyhedronAlgorithm& algorithm) = delete;
/// Compute the narrow-phase collision detection between two convex polyhedra /// 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, uint batchNbItems, bool reportContacts, bool stopFirstContactFound,
MemoryAllocator& memoryAllocator) override; MemoryAllocator& memoryAllocator);
}; };
} }

View File

@ -72,9 +72,6 @@ class NarrowPhaseAlgorithm {
// -------------------- Attributes -------------------- // // -------------------- Attributes -------------------- //
// Id of the algorithm (computed by hasing the algorithm name)
size_t mId;
#ifdef IS_PROFILING_ACTIVE #ifdef IS_PROFILING_ACTIVE
/// Pointer to the profiler /// Pointer to the profiler
@ -99,11 +96,6 @@ class NarrowPhaseAlgorithm {
/// Deleted assignment operator /// Deleted assignment operator
NarrowPhaseAlgorithm& operator=(const NarrowPhaseAlgorithm& algorithm) = delete; 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 #ifdef IS_PROFILING_ACTIVE
/// Set the profiler /// 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 // Clear
void NarrowPhaseInput::clear() { void NarrowPhaseInput::clear() {

View File

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

View File

@ -66,9 +66,9 @@ class SphereVsCapsuleAlgorithm : public NarrowPhaseAlgorithm {
SphereVsCapsuleAlgorithm& operator=(const SphereVsCapsuleAlgorithm& algorithm) = delete; SphereVsCapsuleAlgorithm& operator=(const SphereVsCapsuleAlgorithm& algorithm) = delete;
/// Compute the narrow-phase collision detection between a sphere and a capsule /// 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, 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; SphereVsConvexPolyhedronAlgorithm& operator=(const SphereVsConvexPolyhedronAlgorithm& algorithm) = delete;
/// Compute the narrow-phase collision detection between a sphere and a convex polyhedron /// 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, 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; SphereVsSphereAlgorithm& operator=(const SphereVsSphereAlgorithm& algorithm) = delete;
/// Compute a contact info if the two bounding volume collide /// 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, uint batchNbItems, bool reportContacts, bool stopFirstContactFound,
MemoryAllocator& memoryAllocator) override; MemoryAllocator& memoryAllocator);
}; };
} }