diff --git a/Demos/OpenGL/DemoApplication.cpp b/Demos/OpenGL/DemoApplication.cpp index 5dc6d0de6..7a60e5eeb 100644 --- a/Demos/OpenGL/DemoApplication.cpp +++ b/Demos/OpenGL/DemoApplication.cpp @@ -41,6 +41,9 @@ btCollisionShape* gShapePtr[maxNumObjects];//1 rigidbody has 1 shape (no re-use #ifdef SHOW_NUM_DEEP_PENETRATIONS extern int gNumDeepPenetrationChecks; extern int gNumGjkChecks; +extern int gNumAlignedAllocs; +extern int gNumAlignedFree; + #endif // @@ -885,6 +888,7 @@ void DemoApplication::renderme() #ifdef SHOW_NUM_DEEP_PENETRATIONS + glRasterPos3f(xOffset,yStart,0); sprintf(buf,"gNumDeepPenetrationChecks = %d",gNumDeepPenetrationChecks); BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf); @@ -895,6 +899,22 @@ void DemoApplication::renderme() BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf); yStart += yIncr; + + glRasterPos3f(xOffset,yStart,0); + sprintf(buf,"gNumAlignedAllocs = %d",gNumAlignedAllocs); + BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf); + yStart += yIncr; + + glRasterPos3f(xOffset,yStart,0); + sprintf(buf,"gNumAlignedFree= %d",gNumAlignedFree); + BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf); + yStart += yIncr; + + glRasterPos3f(xOffset,yStart,0); + sprintf(buf,"# alloc-free = %d",gNumAlignedAllocs-gNumAlignedFree); + BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf); + yStart += yIncr; + #endif //SHOW_NUM_DEEP_PENETRATIONS resetPerspectiveProjection(); diff --git a/src/BulletCollision/BroadphaseCollision/btMultiSapBroadphase.cpp b/src/BulletCollision/BroadphaseCollision/btMultiSapBroadphase.cpp index 8ac77b2f3..a75689c99 100644 --- a/src/BulletCollision/BroadphaseCollision/btMultiSapBroadphase.cpp +++ b/src/BulletCollision/BroadphaseCollision/btMultiSapBroadphase.cpp @@ -52,11 +52,12 @@ m_invalidPair(0) } }; - m_filterCallback = new btMultiSapOverlapFilterCallback(); + void* mem = btAlignedAlloc(sizeof(btMultiSapOverlapFilterCallback),16); + m_filterCallback = new (mem)btMultiSapOverlapFilterCallback(); m_overlappingPairs->setOverlapFilterCallback(m_filterCallback); - - m_simpleBroadphase = new btSimpleBroadphase(maxProxies,m_overlappingPairs); + mem = btAlignedAlloc(sizeof(btSimpleBroadphase),16); + m_simpleBroadphase = new (mem) btSimpleBroadphase(maxProxies,m_overlappingPairs); } btMultiSapBroadphase::~btMultiSapBroadphase() @@ -69,7 +70,8 @@ btMultiSapBroadphase::~btMultiSapBroadphase() btBroadphaseProxy* btMultiSapBroadphase::createProxy( const btVector3& aabbMin, const btVector3& aabbMax,int shapeType,void* userPtr, short int collisionFilterGroup,short int collisionFilterMask, btDispatcher* dispatcher) { - btMultiSapProxy* proxy = new btMultiSapProxy(aabbMin, aabbMax,shapeType,userPtr, collisionFilterGroup,collisionFilterMask); + void* mem = btAlignedAlloc(sizeof(btMultiSapProxy),16); + btMultiSapProxy* proxy = new (mem)btMultiSapProxy(aabbMin, aabbMax,shapeType,userPtr, collisionFilterGroup,collisionFilterMask); m_multiSapProxies.push_back(proxy); ///we don't pass the userPtr but our multisap proxy. We need to patch this, before processing an actual collision @@ -77,6 +79,7 @@ btBroadphaseProxy* btMultiSapBroadphase::createProxy( const btVector3& aabbMin, btBroadphaseProxy* simpleProxy = m_simpleBroadphase->createProxy(aabbMin,aabbMax,shapeType,userPtr,collisionFilterGroup,collisionFilterMask, dispatcher); simpleProxy->m_multiSapParentProxy = proxy; + mem = btAlignedAlloc(sizeof(btChildProxy),16); btChildProxy* childProxyRef = new btChildProxy(); childProxyRef->m_proxy = simpleProxy; childProxyRef->m_childBroadphase = m_simpleBroadphase; diff --git a/src/BulletCollision/CollisionDispatch/btCollisionWorld.cpp b/src/BulletCollision/CollisionDispatch/btCollisionWorld.cpp index ba3f52143..b4828508b 100644 --- a/src/BulletCollision/CollisionDispatch/btCollisionWorld.cpp +++ b/src/BulletCollision/CollisionDispatch/btCollisionWorld.cpp @@ -40,9 +40,7 @@ subject to the following restrictions: btCollisionWorld::btCollisionWorld(btDispatcher* dispatcher,btBroadphaseInterface* pairCache, btCollisionConfiguration* collisionConfiguration) :m_dispatcher1(dispatcher), -m_broadphasePairCache(pairCache), -m_ownsDispatcher(false), -m_ownsBroadphasePairCache(false) +m_broadphasePairCache(pairCache) { m_stackAlloc = collisionConfiguration->getStackAllocator(); m_dispatchInfo.m_stackAllocator = m_stackAlloc; @@ -69,10 +67,6 @@ btCollisionWorld::~btCollisionWorld() } } - if (m_ownsDispatcher) - delete m_dispatcher1; - if (m_ownsBroadphasePairCache) - delete m_broadphasePairCache; } diff --git a/src/BulletCollision/CollisionDispatch/btCollisionWorld.h b/src/BulletCollision/CollisionDispatch/btCollisionWorld.h index 0ad2e89f4..bda03cced 100644 --- a/src/BulletCollision/CollisionDispatch/btCollisionWorld.h +++ b/src/BulletCollision/CollisionDispatch/btCollisionWorld.h @@ -92,9 +92,6 @@ protected: btBroadphaseInterface* m_broadphasePairCache; - bool m_ownsDispatcher; - bool m_ownsBroadphasePairCache; - public: //this constructor doesn't own the dispatcher and paircache/broadphase diff --git a/src/BulletCollision/CollisionDispatch/btConvexConvexAlgorithm.cpp b/src/BulletCollision/CollisionDispatch/btConvexConvexAlgorithm.cpp index 76dc2a84d..d1692cdac 100644 --- a/src/BulletCollision/CollisionDispatch/btConvexConvexAlgorithm.cpp +++ b/src/BulletCollision/CollisionDispatch/btConvexConvexAlgorithm.cpp @@ -48,26 +48,16 @@ subject to the following restrictions: -btConvexConvexAlgorithm::CreateFunc::CreateFunc() -{ - m_ownsSolvers = true; - m_simplexSolver = new btVoronoiSimplexSolver(); - m_pdSolver = new btGjkEpaPenetrationDepthSolver; -} + btConvexConvexAlgorithm::CreateFunc::CreateFunc(btSimplexSolverInterface* simplexSolver, btConvexPenetrationDepthSolver* pdSolver) { - m_ownsSolvers = false; m_simplexSolver = simplexSolver; m_pdSolver = pdSolver; } btConvexConvexAlgorithm::CreateFunc::~CreateFunc() { - if (m_ownsSolvers){ - delete m_simplexSolver; - delete m_pdSolver; - } } btConvexConvexAlgorithm::btConvexConvexAlgorithm(btPersistentManifold* mf,const btCollisionAlgorithmConstructionInfo& ci,btCollisionObject* body0,btCollisionObject* body1,btSimplexSolverInterface* simplexSolver, btConvexPenetrationDepthSolver* pdSolver) diff --git a/src/BulletCollision/CollisionDispatch/btConvexConvexAlgorithm.h b/src/BulletCollision/CollisionDispatch/btConvexConvexAlgorithm.h index acc1e1cf7..ca58bce25 100644 --- a/src/BulletCollision/CollisionDispatch/btConvexConvexAlgorithm.h +++ b/src/BulletCollision/CollisionDispatch/btConvexConvexAlgorithm.h @@ -59,10 +59,9 @@ public: { btConvexPenetrationDepthSolver* m_pdSolver; btSimplexSolverInterface* m_simplexSolver; - bool m_ownsSolvers; CreateFunc(btSimplexSolverInterface* simplexSolver, btConvexPenetrationDepthSolver* pdSolver); - CreateFunc(); + virtual ~CreateFunc(); virtual btCollisionAlgorithm* CreateCollisionAlgorithm(btCollisionAlgorithmConstructionInfo& ci, btCollisionObject* body0,btCollisionObject* body1) diff --git a/src/BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.cpp b/src/BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.cpp index 2dfdf240b..ce05dc153 100644 --- a/src/BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.cpp +++ b/src/BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.cpp @@ -23,6 +23,10 @@ subject to the following restrictions: #include "BulletCollision/CollisionDispatch/btSphereSphereCollisionAlgorithm.h" #include "BulletCollision/CollisionDispatch/btSphereBoxCollisionAlgorithm.h" #include "BulletCollision/CollisionDispatch/btSphereTriangleCollisionAlgorithm.h" +#include "BulletCollision/NarrowPhaseCollision/btGjkEpaPenetrationDepthSolver.h" +#include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h" + + #include "LinearMath/btStackAlloc.h" #include "LinearMath/btPoolAllocator.h" @@ -36,20 +40,36 @@ subject to the following restrictions: btDefaultCollisionConfiguration::btDefaultCollisionConfiguration(btStackAlloc* stackAlloc,btPoolAllocator* persistentManifoldPool,btPoolAllocator* collisionAlgorithmPool) { + void* mem = btAlignedAlloc(sizeof(btVoronoiSimplexSolver),16); + m_simplexSolver = new (mem)btVoronoiSimplexSolver(); + mem = btAlignedAlloc(sizeof(btGjkEpaPenetrationDepthSolver),16); + m_pdSolver = new (mem)btGjkEpaPenetrationDepthSolver; + //default CreationFunctions, filling the m_doubleDispatch table - m_convexConvexCreateFunc = new btConvexConvexAlgorithm::CreateFunc; - m_convexConcaveCreateFunc = new btConvexConcaveCollisionAlgorithm::CreateFunc; - m_swappedConvexConcaveCreateFunc = new btConvexConcaveCollisionAlgorithm::SwappedCreateFunc; - m_compoundCreateFunc = new btCompoundCollisionAlgorithm::CreateFunc; - m_swappedCompoundCreateFunc = new btCompoundCollisionAlgorithm::SwappedCreateFunc; - m_emptyCreateFunc = new btEmptyAlgorithm::CreateFunc; - m_sphereSphereCF = new btSphereSphereCollisionAlgorithm::CreateFunc; - m_sphereBoxCF = new btSphereBoxCollisionAlgorithm::CreateFunc; - m_boxSphereCF = new btSphereBoxCollisionAlgorithm::CreateFunc; + mem = btAlignedAlloc(sizeof(btConvexConvexAlgorithm::CreateFunc),16); + m_convexConvexCreateFunc = new(mem) btConvexConvexAlgorithm::CreateFunc(m_simplexSolver,m_pdSolver); + mem = btAlignedAlloc(sizeof(btConvexConcaveCollisionAlgorithm::CreateFunc),16); + m_convexConcaveCreateFunc = new (mem)btConvexConcaveCollisionAlgorithm::CreateFunc; + mem = btAlignedAlloc(sizeof(btConvexConcaveCollisionAlgorithm::CreateFunc),16); + m_swappedConvexConcaveCreateFunc = new (mem)btConvexConcaveCollisionAlgorithm::SwappedCreateFunc; + mem = btAlignedAlloc(sizeof(btCompoundCollisionAlgorithm::CreateFunc),16); + m_compoundCreateFunc = new (mem)btCompoundCollisionAlgorithm::CreateFunc; + mem = btAlignedAlloc(sizeof(btCompoundCollisionAlgorithm::SwappedCreateFunc),16); + m_swappedCompoundCreateFunc = new (mem)btCompoundCollisionAlgorithm::SwappedCreateFunc; + mem = btAlignedAlloc(sizeof(btEmptyAlgorithm::CreateFunc),16); + m_emptyCreateFunc = new(mem) btEmptyAlgorithm::CreateFunc; + + mem = btAlignedAlloc(sizeof(btSphereSphereCollisionAlgorithm::CreateFunc),16); + m_sphereSphereCF = new(mem) btSphereSphereCollisionAlgorithm::CreateFunc; + mem = btAlignedAlloc(sizeof(btSphereBoxCollisionAlgorithm::CreateFunc),16); + m_sphereBoxCF = new(mem) btSphereBoxCollisionAlgorithm::CreateFunc; + mem = btAlignedAlloc(sizeof(btSphereBoxCollisionAlgorithm::CreateFunc),16); + m_boxSphereCF = new (mem)btSphereBoxCollisionAlgorithm::CreateFunc; m_boxSphereCF->m_swapped = true; - - m_sphereTriangleCF = new btSphereTriangleCollisionAlgorithm::CreateFunc; - m_triangleSphereCF = new btSphereTriangleCollisionAlgorithm::CreateFunc; + mem = btAlignedAlloc(sizeof(btSphereTriangleCollisionAlgorithm::CreateFunc),16); + m_sphereTriangleCF = new (mem)btSphereTriangleCollisionAlgorithm::CreateFunc; + mem = btAlignedAlloc(sizeof(btSphereTriangleCollisionAlgorithm::CreateFunc),16); + m_triangleSphereCF = new (mem)btSphereTriangleCollisionAlgorithm::CreateFunc; m_triangleSphereCF->m_swapped = true; @@ -115,17 +135,20 @@ btDefaultCollisionConfiguration::~btDefaultCollisionConfiguration() btAlignedFree(m_persistentManifoldPool); } - delete m_convexConvexCreateFunc; - delete m_convexConcaveCreateFunc; - delete m_swappedConvexConcaveCreateFunc; - delete m_compoundCreateFunc; - delete m_swappedCompoundCreateFunc; - delete m_emptyCreateFunc; - delete m_sphereSphereCF; - delete m_sphereBoxCF; - delete m_boxSphereCF; - delete m_sphereTriangleCF; - delete m_triangleSphereCF; + btAlignedFree( m_convexConvexCreateFunc); + btAlignedFree( m_convexConcaveCreateFunc); + btAlignedFree( m_swappedConvexConcaveCreateFunc); + btAlignedFree( m_compoundCreateFunc); + btAlignedFree( m_swappedCompoundCreateFunc); + btAlignedFree( m_emptyCreateFunc); + btAlignedFree( m_sphereSphereCF); + btAlignedFree( m_sphereBoxCF); + btAlignedFree( m_boxSphereCF); + btAlignedFree( m_sphereTriangleCF); + btAlignedFree( m_triangleSphereCF); + + btAlignedFree(m_simplexSolver); + btAlignedFree(m_pdSolver); } diff --git a/src/BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h b/src/BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h index 20f6ecd6e..2e99f1db1 100644 --- a/src/BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h +++ b/src/BulletCollision/CollisionDispatch/btDefaultCollisionConfiguration.h @@ -17,6 +17,9 @@ subject to the following restrictions: #define BT_DEFAULT_COLLISION_CONFIGURATION #include "btCollisionConfiguration.h" +class btVoronoiSimplexSolver; +class btGjkEpaPenetrationDepthSolver; + ///btCollisionConfiguration allows to configure Bullet collision detection ///stack allocator, pool memory allocators @@ -35,6 +38,9 @@ class btDefaultCollisionConfiguration : public btCollisionConfiguration btPoolAllocator* m_collisionAlgorithmPool; bool m_ownsCollisionAlgorithmPool; + //default simplex/penetration depth solvers + btVoronoiSimplexSolver* m_simplexSolver; + btGjkEpaPenetrationDepthSolver* m_pdSolver; //default CreationFunctions, filling the m_doubleDispatch table btCollisionAlgorithmCreateFunc* m_convexConvexCreateFunc; diff --git a/src/BulletCollision/CollisionDispatch/btSimulationIslandManager.cpp b/src/BulletCollision/CollisionDispatch/btSimulationIslandManager.cpp index b69fdb279..0cc3ac599 100644 --- a/src/BulletCollision/CollisionDispatch/btSimulationIslandManager.cpp +++ b/src/BulletCollision/CollisionDispatch/btSimulationIslandManager.cpp @@ -241,8 +241,7 @@ void btSimulationIslandManager::buildAndProcessIslands(btDispatcher* dispatcher, #define SPLIT_ISLANDS 1 #ifdef SPLIT_ISLANDS - btAlignedObjectArray islandmanifold; - islandmanifold.reserve(maxNumManifolds); + #endif //SPLIT_ISLANDS @@ -270,7 +269,7 @@ void btSimulationIslandManager::buildAndProcessIslands(btDispatcher* dispatcher, #ifdef SPLIT_ISLANDS // //filtering for response if (dispatcher->needsResponse(colObj0,colObj1)) - islandmanifold.push_back(manifold); + m_islandmanifold.push_back(manifold); #endif //SPLIT_ISLANDS } } @@ -284,10 +283,10 @@ void btSimulationIslandManager::buildAndProcessIslands(btDispatcher* dispatcher, // Sort the vector using predicate and std::sort //std::sort(islandmanifold.begin(), islandmanifold.end(), btPersistentManifoldSortPredicate); - int numManifolds = int (islandmanifold.size()); + int numManifolds = int (m_islandmanifold.size()); //we should do radix sort, it it much faster (O(n) instead of O (n log2(n)) - islandmanifold.heapSort(btPersistentManifoldSortPredicate()); + m_islandmanifold.heapSort(btPersistentManifoldSortPredicate()); //now process all active islands (sets of manifolds for now) @@ -298,7 +297,7 @@ void btSimulationIslandManager::buildAndProcessIslands(btDispatcher* dispatcher, END_PROFILE("islandUnionFindAndHeapSort"); - btAlignedObjectArray islandBodies; + // printf("Start Islands\n"); @@ -314,7 +313,7 @@ void btSimulationIslandManager::buildAndProcessIslands(btDispatcher* dispatcher, { int i = getUnionFind().getElement(endIslandIndex).m_sz; btCollisionObject* colObj0 = collisionObjects[i]; - islandBodies.push_back(colObj0); + m_islandBodies.push_back(colObj0); if (!colObj0->isActive()) islandSleeping = true; } @@ -326,12 +325,12 @@ void btSimulationIslandManager::buildAndProcessIslands(btDispatcher* dispatcher, if (startManifoldIndexProcessIsland(&islandBodies[0],islandBodies.size(),startManifold,numIslandManifolds, islandId); + callback->ProcessIsland(&m_islandBodies[0],m_islandBodies.size(),startManifold,numIslandManifolds, islandId); // printf("Island callback of size:%d bodies, %d manifolds\n",islandBodies.size(),numIslandManifolds); } @@ -352,8 +351,9 @@ void btSimulationIslandManager::buildAndProcessIslands(btDispatcher* dispatcher, startManifoldIndex = endManifoldIndex; } - islandBodies.resize(0); + m_islandBodies.resize(0); } #endif //SPLIT_ISLANDS - + + m_islandmanifold.resize(0); } diff --git a/src/BulletCollision/CollisionDispatch/btSimulationIslandManager.h b/src/BulletCollision/CollisionDispatch/btSimulationIslandManager.h index da00f6fe2..0dffd3e2a 100644 --- a/src/BulletCollision/CollisionDispatch/btSimulationIslandManager.h +++ b/src/BulletCollision/CollisionDispatch/btSimulationIslandManager.h @@ -18,16 +18,24 @@ subject to the following restrictions: #include "BulletCollision/CollisionDispatch/btUnionFind.h" #include "btCollisionCreateFunc.h" +#include "LinearMath/btAlignedObjectArray.h" + class btCollisionObject; class btCollisionWorld; class btDispatcher; +class btPersistentManifold; + ///SimulationIslandManager creates and handles simulation islands, using btUnionFind class btSimulationIslandManager { btUnionFind m_unionFind; + btAlignedObjectArray m_islandmanifold; + btAlignedObjectArray m_islandBodies; + + public: btSimulationIslandManager(); virtual ~btSimulationIslandManager(); diff --git a/src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.cpp b/src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.cpp index 54af5c02a..6d9035a91 100644 --- a/src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.cpp +++ b/src/BulletCollision/CollisionShapes/btBvhTriangleMeshShape.cpp @@ -35,7 +35,8 @@ m_ownsBvh(false) if (buildBvh) { - m_bvh = new btOptimizedBvh(); + void* mem = btAlignedAlloc(sizeof(btOptimizedBvh),16); + m_bvh = new (mem) btOptimizedBvh(); m_bvh->build(meshInterface,m_useQuantizedAabbCompression,bvhAabbMin,bvhAabbMax); m_ownsBvh = true; } @@ -55,7 +56,9 @@ m_ownsBvh(false) if (buildBvh) { - m_bvh = new btOptimizedBvh(); + void* mem = btAlignedAlloc(sizeof(btOptimizedBvh),16); + m_bvh = new (mem) btOptimizedBvh(); + m_bvh->build(meshInterface,m_useQuantizedAabbCompression,bvhAabbMin,bvhAabbMax); m_ownsBvh = true; } @@ -83,7 +86,9 @@ void btBvhTriangleMeshShape::refitTree() btBvhTriangleMeshShape::~btBvhTriangleMeshShape() { if (m_ownsBvh) - delete m_bvh; + { + btAlignedFree(m_bvh); + } } //perform bvh tree traversal and report overlapping triangles to 'callback' @@ -180,9 +185,12 @@ void btBvhTriangleMeshShape::setLocalScaling(const btVector3& scaling) { btTriangleMeshShape::setLocalScaling(scaling); if (m_ownsBvh) - delete m_bvh; + { + btAlignedFree(m_bvh); + } ///m_localAabbMin/m_localAabbMax is already re-calculated in btTriangleMeshShape. We could just scale aabb, but this needs some more work - m_bvh = new btOptimizedBvh(); + void* mem = btAlignedAlloc(sizeof(btOptimizedBvh),16); + m_bvh = new(mem) btOptimizedBvh(); //rebuild the bvh... m_bvh->build(m_meshInterface,m_useQuantizedAabbCompression,m_localAabbMin,m_localAabbMax); diff --git a/src/BulletDynamics/ConstraintSolver/btHingeConstraint.cpp b/src/BulletDynamics/ConstraintSolver/btHingeConstraint.cpp index 51a4e7da0..f71698fa6 100644 --- a/src/BulletDynamics/ConstraintSolver/btHingeConstraint.cpp +++ b/src/BulletDynamics/ConstraintSolver/btHingeConstraint.cpp @@ -139,11 +139,14 @@ btHingeConstraint::btHingeConstraint(btRigidBody& rbA, const btTransform& rbAFra m_angularOnly(false), m_enableAngularMotor(false) { + ///not providing rigidbody B means implicitly using worldspace for body B + // flip axis m_rbBFrame.getBasis()[0][2] *= btScalar(-1.); m_rbBFrame.getBasis()[1][2] *= btScalar(-1.); m_rbBFrame.getBasis()[2][2] *= btScalar(-1.); + m_rbBFrame.getOrigin() = m_rbA.getCenterOfMassTransform()(m_rbAFrame.getOrigin()); //start with free m_lowerLimit = btScalar(1e30); diff --git a/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.cpp b/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.cpp index 8e7fd6f72..d7f46c015 100644 --- a/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.cpp +++ b/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.cpp @@ -99,7 +99,7 @@ bool MyContactDestroyedCallback(void* userPersistentData) { assert (userPersistentData); btConstraintPersistentData* cpd = (btConstraintPersistentData*)userPersistentData; - delete cpd; + btAlignedFree(cpd); totalCpd--; //printf("totalCpd = %i. DELETED Ptr %x\n",totalCpd,userPersistentData); return true; @@ -469,7 +469,7 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol //todo: use stack allocator for this temp memory int minReservation = numManifolds*2; - tmpSolverBodyPool.reserve(minReservation); + //tmpSolverBodyPool.reserve(minReservation); //don't convert all bodies, only the one we need so solver the constraints /* @@ -489,8 +489,9 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol } */ - tmpSolverConstraintPool.reserve(minReservation); - tmpSolverFrictionConstraintPool.reserve(minReservation); + //tmpSolverConstraintPool.reserve(minReservation); + //tmpSolverFrictionConstraintPool.reserve(minReservation); + { int i; @@ -1024,7 +1025,9 @@ void btSequentialImpulseConstraintSolver::prepareConstraints(btPersistentManifol } else { - cpd = new btConstraintPersistentData; + //todo: should this be in a pool? + void* mem = btAlignedAlloc(sizeof(btConstraintPersistentData),16); + cpd = new (mem)btConstraintPersistentData; assert(cpd); totalCpd ++; @@ -1071,7 +1074,6 @@ void btSequentialImpulseConstraintSolver::prepareConstraints(btPersistentManifol cpd->m_penetration = btScalar(0.); } - btScalar relaxation = info.m_damping; if (m_solverMode & SOLVER_USE_WARMSTARTING) diff --git a/src/BulletDynamics/Dynamics/Bullet-C-API.cpp b/src/BulletDynamics/Dynamics/Bullet-C-API.cpp index 24ff8ed6c..1ef88f710 100644 --- a/src/BulletDynamics/Dynamics/Bullet-C-API.cpp +++ b/src/BulletDynamics/Dynamics/Bullet-C-API.cpp @@ -22,6 +22,7 @@ subject to the following restrictions: #include "Bullet-C-Api.h" #include "btBulletDynamicsCommon.h" +#include "LinearMath/btAlignedAllocator.h" /* Create and Delete a Physics SDK @@ -51,13 +52,14 @@ struct btPhysicsSdk plPhysicsSdkHandle plNewBulletSdk() { - return (plPhysicsSdkHandle)new btPhysicsSdk; + void* mem = btAlignedAlloc(sizeof(btPhysicsSdk),16); + return (plPhysicsSdkHandle)new (mem)btPhysicsSdk; } void plDeletePhysicsSdk(plPhysicsSdkHandle physicsSdk) { btPhysicsSdk* phys = reinterpret_cast(physicsSdk); - delete phys; + btAlignedFree(phys); } @@ -65,17 +67,23 @@ void plDeletePhysicsSdk(plPhysicsSdkHandle physicsSdk) plDynamicsWorldHandle plCreateDynamicsWorld(plPhysicsSdkHandle physicsSdkHandle) { btPhysicsSdk* physicsSdk = reinterpret_cast(physicsSdkHandle); - btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration(); - btDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration); - btBroadphaseInterface* pairCache = new btAxisSweep3(physicsSdk->m_worldAabbMin,physicsSdk->m_worldAabbMax); - btConstraintSolver* constraintSolver = new btSequentialImpulseConstraintSolver(); + void* mem = btAlignedAlloc(sizeof(btDefaultCollisionConfiguration),16); + btDefaultCollisionConfiguration* collisionConfiguration = new (mem)btDefaultCollisionConfiguration(); + mem = btAlignedAlloc(sizeof(btCollisionDispatcher),16); + btDispatcher* dispatcher = new (mem)btCollisionDispatcher(collisionConfiguration); + mem = btAlignedAlloc(sizeof(btAxisSweep3),16); + btBroadphaseInterface* pairCache = new (mem)btAxisSweep3(physicsSdk->m_worldAabbMin,physicsSdk->m_worldAabbMax); + mem = btAlignedAlloc(sizeof(btSequentialImpulseConstraintSolver),16); + btConstraintSolver* constraintSolver = new(mem) btSequentialImpulseConstraintSolver(); - return (plDynamicsWorldHandle) new btDiscreteDynamicsWorld(dispatcher,pairCache,constraintSolver,collisionConfiguration); + mem = btAlignedAlloc(sizeof(btDiscreteDynamicsWorld),16); + return (plDynamicsWorldHandle) new (mem)btDiscreteDynamicsWorld(dispatcher,pairCache,constraintSolver,collisionConfiguration); } void plDeleteDynamicsWorld(plDynamicsWorldHandle world) { + //todo: also clean up the other allocations, axisSweep, pairCache,dispatcher,constraintSolver,collisionConfiguration btDynamicsWorld* dynamicsWorld = reinterpret_cast< btDynamicsWorld* >(world); - delete dynamicsWorld; + btAlignedFree(dynamicsWorld); } void plStepSimulation(plDynamicsWorldHandle world, plReal timeStep) @@ -118,7 +126,8 @@ plRigidBodyHandle plCreateRigidBody( void* user_data, float mass, plCollisionSh { shape->calculateLocalInertia(mass,localInertia); } - btRigidBody* body = new btRigidBody(mass, 0,shape,localInertia); + void* mem = btAlignedAlloc(sizeof(btRigidBody),16); + btRigidBody* body = new (mem)btRigidBody(mass, 0,shape,localInertia); body->setWorldTransform(trans); body->setUserPointer(user_data); return (plRigidBodyHandle) body; @@ -128,7 +137,7 @@ void plDeleteRigidBody(plRigidBodyHandle cbody) { btRigidBody* body = reinterpret_cast< btRigidBody* >(cbody); assert(body); - delete body; + btAlignedFree( body); } @@ -136,13 +145,15 @@ void plDeleteRigidBody(plRigidBodyHandle cbody) plCollisionShapeHandle plNewSphereShape(plReal radius) { - return (plCollisionShapeHandle) new btSphereShape(radius); + void* mem = btAlignedAlloc(sizeof(btSphereShape),16); + return (plCollisionShapeHandle) new (mem)btSphereShape(radius); } plCollisionShapeHandle plNewBoxShape(plReal x, plReal y, plReal z) { - return (plCollisionShapeHandle) new btBoxShape(btVector3(x,y,z)); + void* mem = btAlignedAlloc(sizeof(btBoxShape),16); + return (plCollisionShapeHandle) new (mem)btBoxShape(btVector3(x,y,z)); } plCollisionShapeHandle plNewCapsuleShape(plReal radius, plReal height) @@ -152,22 +163,26 @@ plCollisionShapeHandle plNewCapsuleShape(plReal radius, plReal height) const int numSpheres = 2; btVector3 positions[numSpheres] = {btVector3(0,height,0),btVector3(0,-height,0)}; btScalar radi[numSpheres] = {radius,radius}; - return (plCollisionShapeHandle) new btMultiSphereShape(inertiaHalfExtents,positions,radi,numSpheres); + void* mem = btAlignedAlloc(sizeof(btMultiSphereShape),16); + return (plCollisionShapeHandle) new (mem)btMultiSphereShape(inertiaHalfExtents,positions,radi,numSpheres); } plCollisionShapeHandle plNewConeShape(plReal radius, plReal height) { - return (plCollisionShapeHandle) new btConeShape(radius,height); + void* mem = btAlignedAlloc(sizeof(btConeShape),16); + return (plCollisionShapeHandle) new (mem)btConeShape(radius,height); } plCollisionShapeHandle plNewCylinderShape(plReal radius, plReal height) { - return (plCollisionShapeHandle) new btCylinderShape(btVector3(radius,height,radius)); + void* mem = btAlignedAlloc(sizeof(btCylinderShape),16); + return (plCollisionShapeHandle) new (mem)btCylinderShape(btVector3(radius,height,radius)); } /* Convex Meshes */ plCollisionShapeHandle plNewConvexHullShape() { - return (plCollisionShapeHandle) new btConvexHullShape(); + void* mem = btAlignedAlloc(sizeof(btConvexHullShape),16); + return (plCollisionShapeHandle) new (mem)btConvexHullShape(); } @@ -179,7 +194,8 @@ plMeshInterfaceHandle plNewMeshInterface() plCollisionShapeHandle plNewCompoundShape() { - return (plCollisionShapeHandle) new btCompoundShape(); + void* mem = btAlignedAlloc(sizeof(btCompoundShape),16); + return (plCollisionShapeHandle) new (mem)btCompoundShape(); } void plAddChildShape(plCollisionShapeHandle compoundShapeHandle,plCollisionShapeHandle childShapeHandle, plVector3 childPos,plQuaternion childOrn) @@ -224,7 +240,7 @@ void plDeleteShape(plCollisionShapeHandle cshape) { btCollisionShape* shape = reinterpret_cast( cshape); assert(shape); - delete shape; + btAlignedFree(shape); } void plSetScaling(plCollisionShapeHandle cshape, plVector3 cscaling) { diff --git a/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp b/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp index f0cd1b399..5d5cc5f84 100644 --- a/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp +++ b/src/BulletDynamics/Dynamics/btDiscreteDynamicsWorld.cpp @@ -59,15 +59,28 @@ subject to the following restrictions: btDiscreteDynamicsWorld::btDiscreteDynamicsWorld(btDispatcher* dispatcher,btBroadphaseInterface* pairCache,btConstraintSolver* constraintSolver, btCollisionConfiguration* collisionConfiguration) :btDynamicsWorld(dispatcher,pairCache,collisionConfiguration), -m_constraintSolver(constraintSolver? constraintSolver: new btSequentialImpulseConstraintSolver), +m_constraintSolver(constraintSolver), m_debugDrawer(0), m_gravity(0,-10,0), m_localTime(btScalar(1.)/btScalar(60.)), m_profileTimings(0) { - m_islandManager = new btSimulationIslandManager(); + if (m_constraintSolver) + { + void* mem = btAlignedAlloc(sizeof(btSequentialImpulseConstraintSolver),16); + m_constraintSolver = new (mem) btSequentialImpulseConstraintSolver; + m_ownsConstraintSolver = true; + } else + { + m_ownsConstraintSolver = false; + } + + { + void* mem = btAlignedAlloc(sizeof(btSimulationIslandManager),16); + m_islandManager = new (mem) btSimulationIslandManager(); + } + m_ownsIslandManager = true; - m_ownsConstraintSolver = (constraintSolver==0); } @@ -75,9 +88,9 @@ btDiscreteDynamicsWorld::~btDiscreteDynamicsWorld() { //only delete it when we created it if (m_ownsIslandManager) - delete m_islandManager; + btAlignedFree( m_islandManager); if (m_ownsConstraintSolver) - delete m_constraintSolver; + btAlignedFree(m_constraintSolver); } void btDiscreteDynamicsWorld::saveKinematicState(btScalar timeStep) @@ -958,7 +971,7 @@ void btDiscreteDynamicsWorld::setConstraintSolver(btConstraintSolver* solver) { if (m_ownsConstraintSolver) { - delete m_constraintSolver; + btAlignedFree( m_constraintSolver); } m_ownsConstraintSolver = false; m_constraintSolver = solver; diff --git a/src/BulletDynamics/Dynamics/btSimpleDynamicsWorld.cpp b/src/BulletDynamics/Dynamics/btSimpleDynamicsWorld.cpp index b6cb958d4..3bd7671ee 100644 --- a/src/BulletDynamics/Dynamics/btSimpleDynamicsWorld.cpp +++ b/src/BulletDynamics/Dynamics/btSimpleDynamicsWorld.cpp @@ -46,7 +46,7 @@ m_gravity(0,0,-10) btSimpleDynamicsWorld::~btSimpleDynamicsWorld() { if (m_ownsConstraintSolver) - delete m_constraintSolver; + btAlignedFree( m_constraintSolver); } int btSimpleDynamicsWorld::stepSimulation( btScalar timeStep,int maxSubSteps, btScalar fixedTimeStep) @@ -205,7 +205,7 @@ void btSimpleDynamicsWorld::setConstraintSolver(btConstraintSolver* solver) { if (m_ownsConstraintSolver) { - delete m_constraintSolver; + btAlignedFree(m_constraintSolver); } m_ownsConstraintSolver = false; m_constraintSolver = solver; diff --git a/src/BulletDynamics/Vehicle/btRaycastVehicle.cpp b/src/BulletDynamics/Vehicle/btRaycastVehicle.cpp index be3afaa41..90e4e9962 100644 --- a/src/BulletDynamics/Vehicle/btRaycastVehicle.cpp +++ b/src/BulletDynamics/Vehicle/btRaycastVehicle.cpp @@ -527,10 +527,14 @@ void btRaycastVehicle::updateFriction(btScalar timeStep) return; - btVector3* forwardWS = new btVector3[numWheel]; - btVector3* axle = new btVector3[numWheel]; - btScalar* forwardImpulse = new btScalar[numWheel]; - btScalar* sideImpulse = new btScalar[numWheel]; + void* mem = btAlignedAlloc(numWheel*sizeof(btVector3),16); + btVector3* forwardWS = new (mem)btVector3[numWheel]; + mem = btAlignedAlloc(numWheel*sizeof(btVector3),16); + btVector3* axle = new (mem)btVector3[numWheel]; + mem = btAlignedAlloc(numWheel*sizeof(btScalar),16); + btScalar* forwardImpulse = new (mem)btScalar[numWheel]; + mem = btAlignedAlloc(numWheel*sizeof(btScalar),16); + btScalar* sideImpulse = new(mem) btScalar[numWheel]; int numWheelsOnGround = 0; @@ -702,10 +706,10 @@ void btRaycastVehicle::updateFriction(btScalar timeStep) } } - delete []forwardWS; - delete [] axle; - delete[]forwardImpulse; - delete[] sideImpulse; + btAlignedFree(forwardWS); + btAlignedFree(axle); + btAlignedFree(forwardImpulse); + btAlignedFree(sideImpulse); } diff --git a/src/LinearMath/btAlignedAllocator.cpp b/src/LinearMath/btAlignedAllocator.cpp index e9d897dca..526ab3261 100644 --- a/src/LinearMath/btAlignedAllocator.cpp +++ b/src/LinearMath/btAlignedAllocator.cpp @@ -15,12 +15,20 @@ subject to the following restrictions: #include "btAlignedAllocator.h" +int gNumAlignedAllocs = 0; +int gNumAlignedFree = 0; #if defined (BT_HAS_ALIGNED_ALLOCATOR) + + + + #include void* btAlignedAlloc (size_t size, int alignment) { + gNumAlignedAllocs++; + void* ptr = _aligned_malloc(size,alignment); // printf("btAlignedAlloc %d, %x\n",size,ptr); return ptr; @@ -28,6 +36,7 @@ void* btAlignedAlloc (size_t size, int alignment) void btAlignedFree (void* ptr) { + gNumAlignedFree++; // printf("btAlignedFree %x\n",ptr); _aligned_free(ptr); } @@ -38,18 +47,17 @@ void btAlignedFree (void* ptr) #include -int numAllocs = 0; -int numFree = 0; + void* btAlignedAlloc (size_t size, int alignment) { - numAllocs++; + gNumAlignedAllocs++; return memalign(alignment, size); } void btAlignedFree (void* ptr) { - numFree++; + gNumAlignedFree++; free(ptr); } @@ -60,7 +68,9 @@ void* btAlignedAlloc (size_t size, int alignment) void *ret; char *real; unsigned long offset; - + + gNumAlignedAllocs++; + real = (char *)malloc(size + sizeof(void *) + (alignment-1)); if (real) { offset = (alignment - (unsigned long)(real + sizeof(void *))) & (alignment-1); @@ -76,6 +86,7 @@ void btAlignedFree (void* ptr) { void* real; + gNumAlignedFree++; if (ptr) { real = *((void **)(ptr)-1);