- removed warnings in GLUI, thanks Jorrit Tyberghein for the patch!

- removed memory leaks from BasicDemo and Bullet library (other demos needs to be cleaned up!)
- added memory leak debugging functionality in btAlignedAlloc.h: #define BT_DEBUG_MEMORY_ALLOCATIONS
This commit is contained in:
ejcoumans
2007-10-31 08:00:03 +00:00
parent 42fc5b37cc
commit a101719687
15 changed files with 374 additions and 152 deletions

View File

@@ -27,22 +27,84 @@ subject to the following restrictions:
#include "../VehicleDemo/VehicleDemo.h"
#include "../ConstraintDemo/ConstraintDemo.h"
#include "GlutStuff.h"//OpenGL stuff
#include "BMF_Api.h"//font stuff
btDemoEntry g_demoEntries[] =
extern int gNumAlignedAllocs;
extern int gNumAlignedFree;
extern int gTotalBytesAlignedAllocs;
class btEmptyDebugDemo : public DemoApplication
{
{"RagdollDemo",RagdollDemo::Create},
{"ConvexDecomposition",ConvexDecompositionDemo::Create},
{"CcdPhysicsDemo", CcdPhysicsDemo::Create},
{"BasicDemo", BasicDemo::Create},
{"BspDemo", BspDemo::Create},
{"ConcaveDemo",ConcaveDemo::Create},
{"Gimpact Test", GimpactConcaveDemo::Create},
{"Raytracer Test",Raytracer::Create},
{"GjkConvexCast",LinearConvexCastDemo::Create},
{"VehicleDemo",VehicleDemo::Create},
{"ConstraintDemo",ConstraintDemo::Create},
{0, 0}
public:
btEmptyDebugDemo()
{
}
virtual void clientMoveAndDisplay()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float xOffset = 10.f;
float yStart = 20.f;
float yIncr = 20.f;
char buf[124];
glColor3f(0, 0, 0);
setOrthographicProjection();
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;
#ifdef BT_DEBUG_MEMORY_ALLOCATIONS
glRasterPos3f(xOffset,yStart,0);
sprintf(buf,"gTotalBytesAlignedAllocs = %d",gTotalBytesAlignedAllocs);
BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf);
yStart += yIncr;
#endif //BT_DEBUG_MEMORY_ALLOCATIONS
glFlush();
glutSwapBuffers();
}
static DemoApplication* Create()
{
btEmptyDebugDemo* demo = new btEmptyDebugDemo();
demo->myinit();
return demo;
}
};
btDemoEntry g_demoEntries[] =
{
{"BasicDemo", BasicDemo::Create},
{"RagdollDemo",RagdollDemo::Create},
{"ConvexDecomposition",ConvexDecompositionDemo::Create},
{"CcdPhysicsDemo", CcdPhysicsDemo::Create},
{"BspDemo", BspDemo::Create},
{"ConcaveDemo",ConcaveDemo::Create},
{"Gimpact Test", GimpactConcaveDemo::Create},
{"Raytracer Test",Raytracer::Create},
{"GjkConvexCast",LinearConvexCastDemo::Create},
{"VehicleDemo",VehicleDemo::Create},
{"ConstraintDemo",ConstraintDemo::Create},
{"MemoryLeakChecker",btEmptyDebugDemo::Create},
{0, 0}
};

View File

@@ -124,7 +124,7 @@ void BasicDemo::initPhysics()
setCameraDistance(btScalar(50.));
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
m_collisionConfiguration = new btDefaultCollisionConfiguration();
#ifdef USE_PARALLEL_DISPATCHER
@@ -147,7 +147,7 @@ void BasicDemo::initPhysics()
m_dispatcher = new SpuGatheringCollisionDispatcher(threadSupport,maxNumOutstandingTasks,collisionConfiguration);
#else
m_dispatcher = new btCollisionDispatcher(collisionConfiguration);
m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
#endif //USE_PARALLEL_DISPATCHER
#define USE_SWEEP_AND_PRUNE 1
@@ -172,9 +172,9 @@ void BasicDemo::initPhysics()
#ifdef USE_SIMPLE_DYNAMICS_WORLD
//btSimpleDynamicsWorld doesn't support 'cache friendly' optimization, so disable this
sol->setSolverMode(btSequentialImpulseConstraintSolver::SOLVER_RANDMIZE_ORDER);
m_dynamicsWorld = new btSimpleDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_solver,collisionConfiguration);
m_dynamicsWorld = new btSimpleDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_solver,m_collisionConfiguration);
#else
m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_solver,collisionConfiguration);
m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_solver,m_collisionConfiguration);
#endif //USE_SIMPLE_DYNAMICS_WORLD
m_dynamicsWorld->getDispatchInfo().m_enableSPU = true;
@@ -246,6 +246,11 @@ void BasicDemo::exitPhysics()
for (i=m_dynamicsWorld->getNumCollisionObjects()-1; i>=0 ;i--)
{
btCollisionObject* obj = m_dynamicsWorld->getCollisionObjectArray()[i];
btRigidBody* body = btRigidBody::upcast(obj);
if (body && body->getMotionState())
{
delete body->getMotionState();
}
m_dynamicsWorld->removeCollisionObject( obj );
delete obj;
}
@@ -269,6 +274,8 @@ void BasicDemo::exitPhysics()
//delete dispatcher
delete m_dispatcher;
delete m_collisionConfiguration;
}

View File

@@ -24,6 +24,7 @@ class btOverlappingPairCache;
class btCollisionDispatcher;
class btConstraintSolver;
struct btCollisionAlgorithmCreateFunc;
class btDefaultCollisionConfiguration;
///BasicDemo is good starting point for learning the code base and porting.
class BasicDemo : public DemoApplication
@@ -42,8 +43,17 @@ class BasicDemo : public DemoApplication
btCollisionAlgorithmCreateFunc* m_sphereBoxCF;
btCollisionAlgorithmCreateFunc* m_boxSphereCF;
btDefaultCollisionConfiguration* m_collisionConfiguration;
public:
BasicDemo()
{
}
virtual ~BasicDemo()
{
exitPhysics();
}
void initPhysics();
void exitPhysics();

View File

@@ -116,39 +116,7 @@ int shapeIndex[maxNumObjects];
// btPoint3(50,0,0));
static const int numShapes = 4;
btCollisionShape* shapePtr[numShapes] =
{
///Please don't make the box sizes larger then 1000: the collision detection will be inaccurate.
///See http://www.continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=346
//#define USE_GROUND_PLANE 1
#ifdef USE_GROUND_PLANE
new btStaticPlaneShape(btVector3(0,1,0),0.5),
#else
new btBoxShape (btVector3(200,CUBE_HALF_EXTENTS,200)),
#endif
#ifdef DO_BENCHMARK_PYRAMIDS
new btBoxShape (btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS)),
#else
new btCylinderShape (btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS)),
#endif
//new btSphereShape (CUBE_HALF_EXTENTS),
//new btCapsuleShape(0.5*CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS),
//new btCylinderShape (btVector3(1-gCollisionMargin,CUBE_HALF_EXTENTS-gCollisionMargin,1-gCollisionMargin)),
//new btConeShapeX(CUBE_HALF_EXTENTS,2.f*CUBE_HALF_EXTENTS),
new btSphereShape (CUBE_HALF_EXTENTS),
//new btBU_Simplex1to4(btPoint3(-1,-1,-1),btPoint3(1,-1,-1),btPoint3(-1,1,-1),btPoint3(0,0,1)),
//new btEmptyShape(),
new btBoxShape (btVector3(0.4,1,0.8))
};
btCollisionShape* shapePtr[numShapes] = {0,0,0,0};
void CcdPhysicsDemo::createStack( btCollisionShape* boxShape, float halfCubeSize, int size, float zPos )
{
@@ -363,6 +331,39 @@ float myFrictionModel( btRigidBody& body1, btRigidBody& body2, btManifoldPoint&
void CcdPhysicsDemo::initPhysics()
{
shapePtr[0] =
///Please don't make the box sizes larger then 1000: the collision detection will be inaccurate.
///See http://www.continuousphysics.com/Bullet/phpBB2/viewtopic.php?t=346
//#define USE_GROUND_PLANE 1
#ifdef USE_GROUND_PLANE
new btStaticPlaneShape(btVector3(0,1,0),0.5);
#else
new btBoxShape (btVector3(200,CUBE_HALF_EXTENTS,200));
#endif
shapePtr[1] =
#ifdef DO_BENCHMARK_PYRAMIDS
new btBoxShape (btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS));
#else
new btCylinderShape (btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS));
#endif
shapePtr[2] =
//new btSphereShape (CUBE_HALF_EXTENTS);
//new btCapsuleShape(0.5*CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS);
//new btCylinderShape (btVector3(1-gCollisionMargin,CUBE_HALF_EXTENTS-gCollisionMargin,1-gCollisionMargin));
//new btConeShapeX(CUBE_HALF_EXTENTS,2.f*CUBE_HALF_EXTENTS);
new btSphereShape (CUBE_HALF_EXTENTS);
//new btBU_Simplex1to4(btPoint3(-1,-1,-1),btPoint3(1,-1,-1),btPoint3(-1,1,-1),btPoint3(0,0,1));
//new btEmptyShape();
shapePtr[3] = new btBoxShape (btVector3(0.4,1,0.8));
#ifdef DO_BENCHMARK_PYRAMIDS
setCameraDistance(2.5f);
#endif

View File

@@ -43,6 +43,7 @@ extern int gNumDeepPenetrationChecks;
extern int gNumGjkChecks;
extern int gNumAlignedAllocs;
extern int gNumAlignedFree;
extern int gTotalBytesAlignedAllocs;
#endif //
@@ -52,6 +53,7 @@ DemoApplication::DemoApplication()
:
m_dynamicsWorld(0),
m_pickConstraint(0),
m_shootBoxShape(0),
m_cameraDistance(15.0),
m_debugMode(0),
m_ele(20.f),
@@ -75,6 +77,8 @@ m_pickConstraint(0),
DemoApplication::~DemoApplication()
{
if (m_shootBoxShape)
delete m_shootBoxShape;
}
@@ -461,14 +465,19 @@ void DemoApplication::shootBox(const btVector3& destination)
startTransform.setIdentity();
btVector3 camPos = getCameraPosition();
startTransform.setOrigin(camPos);
//#define TEST_UNIFORM_SCALING_SHAPE 1
if (!m_shootBoxShape)
{
//#define TEST_UNIFORM_SCALING_SHAPE 1
#ifdef TEST_UNIFORM_SCALING_SHAPE
btConvexShape* childShape = new btBoxShape(btVector3(1.f,1.f,1.f));
btUniformScalingShape* boxShape = new btUniformScalingShape(childShape,0.5f);
m_shootBoxShape = new btUniformScalingShape(childShape,0.5f);
#else
btCollisionShape* boxShape = new btBoxShape(btVector3(0.5f,0.5f,0.5f));
m_shootBoxShape = new btBoxShape(btVector3(0.5f,0.5f,0.5f));
#endif//
btRigidBody* body = this->localCreateRigidBody(mass, startTransform,boxShape);
}
btRigidBody* body = this->localCreateRigidBody(mass, startTransform,m_shootBoxShape);
btVector3 linVel(destination[0]-camPos[0],destination[1]-camPos[1],destination[2]-camPos[2]);
linVel.normalize();
@@ -701,7 +710,8 @@ btRigidBody* DemoApplication::localCreateRigidBody(float mass, const btTransform
btRigidBody* body = new btRigidBody(mass,myMotionState,shape,localInertia);
#else
btRigidBody* body = new btRigidBody(mass,startTransform,shape,localInertia);
btRigidBody* body = new btRigidBody(mass,0,shape,localInertia);
body->setWorldTransform(startTransform);
#endif//
m_dynamicsWorld->addRigidBody(body);
@@ -923,6 +933,28 @@ void DemoApplication::renderme()
BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf);
yStart += yIncr;
//enable BT_DEBUG_MEMORY_ALLOCATIONS define in Bullet/src/LinearMath/btAlignedAllocator.h for memory leak detection
#ifdef BT_DEBUG_MEMORY_ALLOCATIONS
glRasterPos3f(xOffset,yStart,0);
sprintf(buf,"gTotalBytesAlignedAllocs = %d",gTotalBytesAlignedAllocs);
BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf);
yStart += yIncr;
#endif //BT_DEBUG_MEMORY_ALLOCATIONS
if (getDynamicsWorld())
{
glRasterPos3f(xOffset,yStart,0);
sprintf(buf,"# objects = %d",getDynamicsWorld()->getNumCollisionObjects());
BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf);
yStart += yIncr;
glRasterPos3f(xOffset,yStart,0);
sprintf(buf,"# pairs = %d",getDynamicsWorld()->getBroadphase()->getOverlappingPairCache()->getNumOverlappingPairs());
BMF_DrawString(BMF_GetFont(BMF_kHelvetica10),buf);
yStart += yIncr;
}
#endif //SHOW_NUM_DEEP_PENETRATIONS
resetPerspectiveProjection();

View File

@@ -58,6 +58,8 @@ class DemoApplication
///constraint for mouse picking
btTypedConstraint* m_pickConstraint;
btCollisionShape* m_shootBoxShape;
float m_cameraDistance;
int m_debugMode;

View File

@@ -757,7 +757,7 @@ public:
void *font;
int curr_modifiers;
void adjust_glut_xy( int &x, int &y ) { x; y = h-y; }
void adjust_glut_xy( int &x, int &y ) { (void)x; y = h-y; }
void activate_control( GLUI_Control *control, int how );
void align_controls( GLUI_Control *control );
void deactivate_current_control( void );
@@ -884,19 +884,19 @@ public:
virtual int get_id( void ) const { return user_id; }
virtual void set_id( int id ) { user_id=id; }
virtual int mouse_down_handler( int local_x, int local_y ) { local_x; local_y; return false; }
virtual int mouse_up_handler( int local_x, int local_y, bool inside ) { local_x; local_y; inside; return false; }
virtual int mouse_held_down_handler( int local_x, int local_y, bool inside) { local_x; local_y; inside; return false; }
virtual int key_handler( unsigned char key, int modifiers ) { key; modifiers; return false; }
virtual int special_handler( int key,int modifiers ) { key; modifiers; return false; }
virtual int mouse_down_handler( int local_x, int local_y ) { (void)local_x; (void)local_y; return false; }
virtual int mouse_up_handler( int local_x, int local_y, bool inside ) { (void)local_x; (void)local_y; (void)inside; return false; }
virtual int mouse_held_down_handler( int local_x, int local_y, bool inside) { (void)local_x; (void)local_y; (void)inside; return false; }
virtual int key_handler( unsigned char key, int modifiers ) { (void)key; (void)modifiers; return false; }
virtual int special_handler( int key,int modifiers ) { (void)key; (void)modifiers; return false; }
virtual void update_size( void ) { }
virtual void idle( void ) { }
virtual int mouse_over( int state, int x, int y ) { state; x; y; return false; }
virtual int mouse_over( int state, int x, int y ) { (void)state; (void)x; (void)y; return false; }
virtual void enable( void );
virtual void disable( void );
virtual void activate( int how ) { how; active = true; }
virtual void activate( int how ) { (void)how; active = true; }
virtual void deactivate( void ) { active = false; }
/** Hide (shrink into a rollout) and unhide (expose from a rollout) */
@@ -955,7 +955,7 @@ public:
void sync_live( int recurse, int draw ); /* Reads live variable */
void init_live( void );
void output_live( int update_main_gfx ); /** Writes live variable **/
virtual void set_text( const char *t ) { t; }
virtual void set_text( const char *t ) { (void)t; }
void execute_callback( void );
void get_this_column_dims( int *col_x, int *col_y,
int *col_w, int *col_h,

View File

@@ -288,6 +288,7 @@ btAxisSweep3Internal<BP_FP_INT_TYPE>::~btAxisSweep3Internal()
if (m_ownsPairCache)
{
m_pairCache->~btOverlappingPairCache();
btAlignedFree(m_pairCache);
}
}

View File

@@ -124,30 +124,52 @@ btDefaultCollisionConfiguration::~btDefaultCollisionConfiguration()
if (m_ownsStackAllocator)
{
m_stackAlloc->destroy();
m_stackAlloc->~btStackAlloc();
btAlignedFree(m_stackAlloc);
}
if (m_ownsCollisionAlgorithmPool)
{
m_collisionAlgorithmPool->~btPoolAllocator();
btAlignedFree(m_collisionAlgorithmPool);
}
if (m_ownsPersistentManifoldPool)
{
m_persistentManifoldPool->~btPoolAllocator();
btAlignedFree(m_persistentManifoldPool);
}
m_convexConvexCreateFunc->~btCollisionAlgorithmCreateFunc();
btAlignedFree( m_convexConvexCreateFunc);
m_convexConcaveCreateFunc->~btCollisionAlgorithmCreateFunc();
btAlignedFree( m_convexConcaveCreateFunc);
m_swappedConvexConcaveCreateFunc->~btCollisionAlgorithmCreateFunc();
btAlignedFree( m_swappedConvexConcaveCreateFunc);
m_compoundCreateFunc->~btCollisionAlgorithmCreateFunc();
btAlignedFree( m_compoundCreateFunc);
m_swappedCompoundCreateFunc->~btCollisionAlgorithmCreateFunc();
btAlignedFree( m_swappedCompoundCreateFunc);
m_emptyCreateFunc->~btCollisionAlgorithmCreateFunc();
btAlignedFree( m_emptyCreateFunc);
m_sphereSphereCF->~btCollisionAlgorithmCreateFunc();
btAlignedFree( m_sphereSphereCF);
m_sphereBoxCF->~btCollisionAlgorithmCreateFunc();
btAlignedFree( m_sphereBoxCF);
m_boxSphereCF->~btCollisionAlgorithmCreateFunc();
btAlignedFree( m_boxSphereCF);
m_sphereTriangleCF->~btCollisionAlgorithmCreateFunc();
btAlignedFree( m_sphereTriangleCF);
m_triangleSphereCF->~btCollisionAlgorithmCreateFunc();
btAlignedFree( m_triangleSphereCF);
m_simplexSolver->~btVoronoiSimplexSolver();
btAlignedFree(m_simplexSolver);
m_pdSolver->~btGjkEpaPenetrationDepthSolver();
btAlignedFree(m_pdSolver);

View File

@@ -124,6 +124,10 @@ m_btSeed2(0)
}
}
btSequentialImpulseConstraintSolver::~btSequentialImpulseConstraintSolver()
{
}
void initSolverBody(btSolverBody* solverBody, btRigidBody* rigidbody)
{
@@ -360,18 +364,14 @@ btScalar resolveSingleFrictionCacheFriendly(
#endif //NO_FRICTION_TANGENTIALS
btAlignedObjectArray<btSolverBody> tmpSolverBodyPool;
btAlignedObjectArray<btSolverConstraint> tmpSolverConstraintPool;
btAlignedObjectArray<btSolverConstraint> tmpSolverFrictionConstraintPool;
btAlignedObjectArray<int> gOrderTmpConstraintPool;
btAlignedObjectArray<int> gOrderFrictionConstraintPool;
void addFrictionConstraint(const btVector3& normalAxis,int solverBodyIdA,int solverBodyIdB,int frictionIndex,btManifoldPoint& cp,const btVector3& rel_pos1,const btVector3& rel_pos2,btRigidBody* rb0,btRigidBody* rb1, btScalar relaxation)
void btSequentialImpulseConstraintSolver::addFrictionConstraint(const btVector3& normalAxis,int solverBodyIdA,int solverBodyIdB,int frictionIndex,btManifoldPoint& cp,const btVector3& rel_pos1,const btVector3& rel_pos2,btRigidBody* rb0,btRigidBody* rb1, btScalar relaxation)
{
btSolverConstraint& solverConstraint = tmpSolverFrictionConstraintPool.expand();
btSolverConstraint& solverConstraint = m_tmpSolverFrictionConstraintPool.expand();
solverConstraint.m_contactNormal = normalAxis;
solverConstraint.m_solverBodyIdA = solverBodyIdA;
@@ -469,7 +469,7 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol
//todo: use stack allocator for this temp memory
int minReservation = numManifolds*2;
//tmpSolverBodyPool.reserve(minReservation);
//m_tmpSolverBodyPool.reserve(minReservation);
//don't convert all bodies, only the one we need so solver the constraints
/*
@@ -480,8 +480,8 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol
if (rb && (rb->getIslandTag() >= 0))
{
btAssert(rb->getCompanionId() < 0);
int solverBodyId = tmpSolverBodyPool.size();
btSolverBody& solverBody = tmpSolverBodyPool.expand();
int solverBodyId = m_tmpSolverBodyPool.size();
btSolverBody& solverBody = m_tmpSolverBodyPool.expand();
initSolverBody(&solverBody,rb);
rb->setCompanionId(solverBodyId);
}
@@ -489,8 +489,8 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol
}
*/
//tmpSolverConstraintPool.reserve(minReservation);
//tmpSolverFrictionConstraintPool.reserve(minReservation);
//m_tmpSolverConstraintPool.reserve(minReservation);
//m_tmpSolverFrictionConstraintPool.reserve(minReservation);
{
int i;
@@ -519,16 +519,16 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol
solverBodyIdA = rb0->getCompanionId();
} else
{
solverBodyIdA = tmpSolverBodyPool.size();
btSolverBody& solverBody = tmpSolverBodyPool.expand();
solverBodyIdA = m_tmpSolverBodyPool.size();
btSolverBody& solverBody = m_tmpSolverBodyPool.expand();
initSolverBody(&solverBody,rb0);
rb0->setCompanionId(solverBodyIdA);
}
} else
{
//create a static body
solverBodyIdA = tmpSolverBodyPool.size();
btSolverBody& solverBody = tmpSolverBodyPool.expand();
solverBodyIdA = m_tmpSolverBodyPool.size();
btSolverBody& solverBody = m_tmpSolverBodyPool.expand();
initSolverBody(&solverBody,rb0);
}
@@ -539,16 +539,16 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol
solverBodyIdB = rb1->getCompanionId();
} else
{
solverBodyIdB = tmpSolverBodyPool.size();
btSolverBody& solverBody = tmpSolverBodyPool.expand();
solverBodyIdB = m_tmpSolverBodyPool.size();
btSolverBody& solverBody = m_tmpSolverBodyPool.expand();
initSolverBody(&solverBody,rb1);
rb1->setCompanionId(solverBodyIdB);
}
} else
{
//create a static body
solverBodyIdB = tmpSolverBodyPool.size();
btSolverBody& solverBody = tmpSolverBodyPool.expand();
solverBodyIdB = m_tmpSolverBodyPool.size();
btSolverBody& solverBody = m_tmpSolverBodyPool.expand();
initSolverBody(&solverBody,rb1);
}
}
@@ -580,10 +580,10 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol
btScalar rel_vel;
btVector3 vel;
int frictionIndex = tmpSolverConstraintPool.size();
int frictionIndex = m_tmpSolverConstraintPool.size();
{
btSolverConstraint& solverConstraint = tmpSolverConstraintPool.expand();
btSolverConstraint& solverConstraint = m_tmpSolverConstraintPool.expand();
solverConstraint.m_solverBodyIdA = solverBodyIdA;
solverConstraint.m_solverBodyIdB = solverBodyIdB;
@@ -690,21 +690,21 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol
int numConstraintPool = tmpSolverConstraintPool.size();
int numFrictionPool = tmpSolverFrictionConstraintPool.size();
int numConstraintPool = m_tmpSolverConstraintPool.size();
int numFrictionPool = m_tmpSolverFrictionConstraintPool.size();
///todo: use stack allocator for such temporarily memory, same for solver bodies/constraints
gOrderTmpConstraintPool.resize(numConstraintPool);
gOrderFrictionConstraintPool.resize(numFrictionPool);
m_orderTmpConstraintPool.resize(numConstraintPool);
m_orderFrictionConstraintPool.resize(numFrictionPool);
{
int i;
for (i=0;i<numConstraintPool;i++)
{
gOrderTmpConstraintPool[i] = i;
m_orderTmpConstraintPool[i] = i;
}
for (i=0;i<numFrictionPool;i++)
{
gOrderFrictionConstraintPool[i] = i;
m_orderFrictionConstraintPool[i] = i;
}
}
@@ -719,8 +719,8 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlySetup(btCol
btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlyIterations(btCollisionObject** bodies,int numBodies,btPersistentManifold** manifoldPtr, int numManifolds,btTypedConstraint** constraints,int numConstraints,const btContactSolverInfo& infoGlobal,btIDebugDraw* debugDrawer,btStackAlloc* stackAlloc)
{
BEGIN_PROFILE("solveConstraintsIterations");
int numConstraintPool = tmpSolverConstraintPool.size();
int numFrictionPool = tmpSolverFrictionConstraintPool.size();
int numConstraintPool = m_tmpSolverConstraintPool.size();
int numFrictionPool = m_tmpSolverFrictionConstraintPool.size();
//should traverse the contacts random order...
int iteration;
@@ -733,17 +733,17 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlyIterations(
{
if ((iteration & 7) == 0) {
for (j=0; j<numConstraintPool; ++j) {
int tmp = gOrderTmpConstraintPool[j];
int tmp = m_orderTmpConstraintPool[j];
int swapi = btRandInt2(j+1);
gOrderTmpConstraintPool[j] = gOrderTmpConstraintPool[swapi];
gOrderTmpConstraintPool[swapi] = tmp;
m_orderTmpConstraintPool[j] = m_orderTmpConstraintPool[swapi];
m_orderTmpConstraintPool[swapi] = tmp;
}
for (j=0; j<numFrictionPool; ++j) {
int tmp = gOrderFrictionConstraintPool[j];
int tmp = m_orderFrictionConstraintPool[j];
int swapi = btRandInt2(j+1);
gOrderFrictionConstraintPool[j] = gOrderFrictionConstraintPool[swapi];
gOrderFrictionConstraintPool[swapi] = tmp;
m_orderFrictionConstraintPool[j] = m_orderFrictionConstraintPool[swapi];
m_orderFrictionConstraintPool[swapi] = tmp;
}
}
}
@@ -755,47 +755,47 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendlyIterations(
if ((constraint->getRigidBodyA().getIslandTag() >= 0) && (constraint->getRigidBodyA().getCompanionId() >= 0))
{
tmpSolverBodyPool[constraint->getRigidBodyA().getCompanionId()].writebackVelocity();
m_tmpSolverBodyPool[constraint->getRigidBodyA().getCompanionId()].writebackVelocity();
}
if ((constraint->getRigidBodyB().getIslandTag() >= 0) && (constraint->getRigidBodyB().getCompanionId() >= 0))
{
tmpSolverBodyPool[constraint->getRigidBodyB().getCompanionId()].writebackVelocity();
m_tmpSolverBodyPool[constraint->getRigidBodyB().getCompanionId()].writebackVelocity();
}
constraint->solveConstraint(infoGlobal.m_timeStep);
if ((constraint->getRigidBodyA().getIslandTag() >= 0) && (constraint->getRigidBodyA().getCompanionId() >= 0))
{
tmpSolverBodyPool[constraint->getRigidBodyA().getCompanionId()].readVelocity();
m_tmpSolverBodyPool[constraint->getRigidBodyA().getCompanionId()].readVelocity();
}
if ((constraint->getRigidBodyB().getIslandTag() >= 0) && (constraint->getRigidBodyB().getCompanionId() >= 0))
{
tmpSolverBodyPool[constraint->getRigidBodyB().getCompanionId()].readVelocity();
m_tmpSolverBodyPool[constraint->getRigidBodyB().getCompanionId()].readVelocity();
}
}
{
int numPoolConstraints = tmpSolverConstraintPool.size();
int numPoolConstraints = m_tmpSolverConstraintPool.size();
for (j=0;j<numPoolConstraints;j++)
{
const btSolverConstraint& solveManifold = tmpSolverConstraintPool[gOrderTmpConstraintPool[j]];
resolveSingleCollisionCombinedCacheFriendly(tmpSolverBodyPool[solveManifold.m_solverBodyIdA],
tmpSolverBodyPool[solveManifold.m_solverBodyIdB],solveManifold,infoGlobal);
const btSolverConstraint& solveManifold = m_tmpSolverConstraintPool[m_orderTmpConstraintPool[j]];
resolveSingleCollisionCombinedCacheFriendly(m_tmpSolverBodyPool[solveManifold.m_solverBodyIdA],
m_tmpSolverBodyPool[solveManifold.m_solverBodyIdB],solveManifold,infoGlobal);
}
}
{
int numFrictionPoolConstraints = tmpSolverFrictionConstraintPool.size();
int numFrictionPoolConstraints = m_tmpSolverFrictionConstraintPool.size();
for (j=0;j<numFrictionPoolConstraints;j++)
{
const btSolverConstraint& solveManifold = tmpSolverFrictionConstraintPool[gOrderFrictionConstraintPool[j]];
const btSolverConstraint& solveManifold = m_tmpSolverFrictionConstraintPool[m_orderFrictionConstraintPool[j]];
resolveSingleFrictionCacheFriendly(tmpSolverBodyPool[solveManifold.m_solverBodyIdA],
tmpSolverBodyPool[solveManifold.m_solverBodyIdB],solveManifold,infoGlobal,
tmpSolverConstraintPool[solveManifold.m_frictionIndex].m_appliedImpulse);
resolveSingleFrictionCacheFriendly(m_tmpSolverBodyPool[solveManifold.m_solverBodyIdA],
m_tmpSolverBodyPool[solveManifold.m_solverBodyIdB],solveManifold,infoGlobal,
m_tmpSolverConstraintPool[solveManifold.m_frictionIndex].m_appliedImpulse);
}
}
@@ -819,29 +819,29 @@ btScalar btSequentialImpulseConstraintSolver::solveGroupCacheFriendly(btCollisio
BEGIN_PROFILE("solveWriteBackVelocity");
for ( i=0;i<tmpSolverBodyPool.size();i++)
for ( i=0;i<m_tmpSolverBodyPool.size();i++)
{
tmpSolverBodyPool[i].writebackVelocity();
m_tmpSolverBodyPool[i].writebackVelocity();
}
END_PROFILE("solveWriteBackVelocity");
// printf("tmpSolverConstraintPool.size() = %i\n",tmpSolverConstraintPool.size());
// printf("m_tmpSolverConstraintPool.size() = %i\n",m_tmpSolverConstraintPool.size());
/*
printf("tmpSolverBodyPool.size() = %i\n",tmpSolverBodyPool.size());
printf("tmpSolverConstraintPool.size() = %i\n",tmpSolverConstraintPool.size());
printf("tmpSolverFrictionConstraintPool.size() = %i\n",tmpSolverFrictionConstraintPool.size());
printf("m_tmpSolverBodyPool.size() = %i\n",m_tmpSolverBodyPool.size());
printf("m_tmpSolverConstraintPool.size() = %i\n",m_tmpSolverConstraintPool.size());
printf("m_tmpSolverFrictionConstraintPool.size() = %i\n",m_tmpSolverFrictionConstraintPool.size());
printf("tmpSolverBodyPool.capacity() = %i\n",tmpSolverBodyPool.capacity());
printf("tmpSolverConstraintPool.capacity() = %i\n",tmpSolverConstraintPool.capacity());
printf("tmpSolverFrictionConstraintPool.capacity() = %i\n",tmpSolverFrictionConstraintPool.capacity());
printf("m_tmpSolverBodyPool.capacity() = %i\n",m_tmpSolverBodyPool.capacity());
printf("m_tmpSolverConstraintPool.capacity() = %i\n",m_tmpSolverConstraintPool.capacity());
printf("m_tmpSolverFrictionConstraintPool.capacity() = %i\n",m_tmpSolverFrictionConstraintPool.capacity());
*/
tmpSolverBodyPool.resize(0);
tmpSolverConstraintPool.resize(0);
tmpSolverFrictionConstraintPool.resize(0);
m_tmpSolverBodyPool.resize(0);
m_tmpSolverConstraintPool.resize(0);
m_tmpSolverFrictionConstraintPool.resize(0);
return 0.f;

View File

@@ -19,7 +19,8 @@ subject to the following restrictions:
#include "btConstraintSolver.h"
class btIDebugDraw;
#include "btContactConstraint.h"
#include "btSolverBody.h"
#include "btSolverConstraint.h"
/// btSequentialImpulseConstraintSolver uses a Propagation Method and Sequentially applies impulses
@@ -29,10 +30,18 @@ class btIDebugDraw;
class btSequentialImpulseConstraintSolver : public btConstraintSolver
{
btAlignedObjectArray<btSolverBody> m_tmpSolverBodyPool;
btAlignedObjectArray<btSolverConstraint> m_tmpSolverConstraintPool;
btAlignedObjectArray<btSolverConstraint> m_tmpSolverFrictionConstraintPool;
btAlignedObjectArray<int> m_orderTmpConstraintPool;
btAlignedObjectArray<int> m_orderFrictionConstraintPool;
protected:
btScalar solve(btRigidBody* body0,btRigidBody* body1, btManifoldPoint& cp, const btContactSolverInfo& info,int iter,btIDebugDraw* debugDrawer);
btScalar solveFriction(btRigidBody* body0,btRigidBody* body1, btManifoldPoint& cp, const btContactSolverInfo& info,int iter,btIDebugDraw* debugDrawer);
void prepareConstraints(btPersistentManifold* manifoldPtr, const btContactSolverInfo& info,btIDebugDraw* debugDrawer);
void addFrictionConstraint(const btVector3& normalAxis,int solverBodyIdA,int solverBodyIdB,int frictionIndex,btManifoldPoint& cp,const btVector3& rel_pos1,const btVector3& rel_pos2,btRigidBody* rb0,btRigidBody* rb1, btScalar relaxation);
ContactSolverFunc m_contactDispatch[MAX_CONTACT_SOLVER_TYPES][MAX_CONTACT_SOLVER_TYPES];
ContactSolverFunc m_frictionDispatch[MAX_CONTACT_SOLVER_TYPES][MAX_CONTACT_SOLVER_TYPES];
@@ -68,7 +77,7 @@ public:
m_frictionDispatch[type0][type1] = func;
}
virtual ~btSequentialImpulseConstraintSolver() {}
virtual ~btSequentialImpulseConstraintSolver();
virtual btScalar solveGroup(btCollisionObject** bodies,int numBodies,btPersistentManifold** manifold,int numManifolds,btTypedConstraint** constraints,int numConstraints,const btContactSolverInfo& info, btIDebugDraw* debugDrawer, btStackAlloc* stackAlloc,btDispatcher* dispatcher);

View File

@@ -65,7 +65,7 @@ m_gravity(0,-10,0),
m_localTime(btScalar(1.)/btScalar(60.)),
m_profileTimings(0)
{
if (m_constraintSolver)
if (!m_constraintSolver)
{
void* mem = btAlignedAlloc(sizeof(btSequentialImpulseConstraintSolver),16);
m_constraintSolver = new (mem) btSequentialImpulseConstraintSolver;
@@ -88,9 +88,16 @@ btDiscreteDynamicsWorld::~btDiscreteDynamicsWorld()
{
//only delete it when we created it
if (m_ownsIslandManager)
{
m_islandManager->~btSimulationIslandManager();
btAlignedFree( m_islandManager);
}
if (m_ownsConstraintSolver)
btAlignedFree(m_constraintSolver);
{
m_constraintSolver->~btConstraintSolver();
btAlignedFree(m_constraintSolver);
}
}
void btDiscreteDynamicsWorld::saveKinematicState(btScalar timeStep)

View File

@@ -17,6 +17,60 @@ subject to the following restrictions:
int gNumAlignedAllocs = 0;
int gNumAlignedFree = 0;
int gTotalBytesAlignedAllocs = 0;//detect memory leaks
#ifdef BT_DEBUG_MEMORY_ALLOCATIONS
//this generic allocator provides the total allocated number of bytes
#include <stdio.h>
void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filename)
{
void *ret;
char *real;
unsigned long offset;
gTotalBytesAlignedAllocs += size;
gNumAlignedAllocs++;
printf("allocation#%d from %s,line %d, size %d\n",gNumAlignedAllocs,filename,line,size);
real = (char *)malloc(size + 2*sizeof(void *) + (alignment-1));
if (real) {
offset = (alignment - (unsigned long)(real + 2*sizeof(void *))) &
(alignment-1);
ret = (void *)((real + 2*sizeof(void *)) + offset);
*((void **)(ret)-1) = (void *)(real);
*((int*)(ret)-2) = size;
} else {
ret = (void *)(real);//??
}
int* ptr = (int*)ret;
*ptr = 12;
return (ret);
}
#include <stdio.h>
void btAlignedFreeInternal (void* ptr,int line,char* filename)
{
void* real;
gNumAlignedFree++;
if (ptr) {
real = *((void **)(ptr)-1);
int size = *((int*)(ptr)-2);
gTotalBytesAlignedAllocs -= size;
printf("free #%d from %s,line %d, size %d\n",gNumAlignedFree,filename,line,size);
free(real);
} else
{
printf("NULL ptr\n");
}
}
#else //BT_DEBUG_MEMORY_ALLOCATIONS
#if defined (BT_HAS_ALIGNED_ALLOCATOR)
@@ -25,19 +79,19 @@ int gNumAlignedFree = 0;
#include <malloc.h>
void* btAlignedAlloc (size_t size, int alignment)
void* btAlignedAllocInternal (size_t size, int alignment)
{
gNumAlignedAllocs++;
void* ptr = _aligned_malloc(size,alignment);
// printf("btAlignedAlloc %d, %x\n",size,ptr);
// printf("btAlignedAllocInternal %d, %x\n",size,ptr);
return ptr;
}
void btAlignedFree (void* ptr)
void btAlignedFreeInternal (void* ptr)
{
gNumAlignedFree++;
// printf("btAlignedFree %x\n",ptr);
// printf("btAlignedFreeInternal %x\n",ptr);
_aligned_free(ptr);
}
@@ -49,13 +103,13 @@ void btAlignedFree (void* ptr)
void* btAlignedAlloc (size_t size, int alignment)
void* btAlignedAllocInternal (size_t size, int alignment)
{
gNumAlignedAllocs++;
return memalign(alignment, size);
}
void btAlignedFree (void* ptr)
void btAlignedFreeInternal (void* ptr)
{
gNumAlignedFree++;
free(ptr);
@@ -63,7 +117,7 @@ void btAlignedFree (void* ptr)
#else
void* btAlignedAlloc (size_t size, int alignment)
void* btAlignedAllocInternal (size_t size, int alignment)
{
void *ret;
char *real;
@@ -82,7 +136,7 @@ void* btAlignedAlloc (size_t size, int alignment)
return (ret);
}
void btAlignedFree (void* ptr)
void btAlignedFreeInternal (void* ptr)
{
void* real;
@@ -97,4 +151,5 @@ void btAlignedFree (void* ptr)
#endif
#endif //BT_DEBUG_MEMORY_ALLOCATIONS

View File

@@ -21,12 +21,26 @@ subject to the following restrictions:
///that is better portable and more predictable
#include "btScalar.h"
//#define BT_DEBUG_MEMORY_ALLOCATIONS 1
#ifdef BT_DEBUG_MEMORY_ALLOCATIONS
void* btAlignedAlloc (size_t size, int alignment);
#define btAlignedAlloc(a,b) \
btAlignedAllocInternal(a,b,__LINE__,__FILE__)
void btAlignedFree (void* ptr);
#define btAlignedFree(ptr) \
btAlignedFreeInternal(ptr,__LINE__,__FILE__)
void* btAlignedAllocInternal (size_t size, int alignment,int line,char* filename);
void btAlignedFreeInternal (void* ptr,int line,char* filename);
#else
void* btAlignedAllocInternal (size_t size, int alignment);
void btAlignedFreeInternal (void* ptr);
#define btAlignedAlloc(a,b) btAlignedAllocInternal(a,b)
#define btAlignedFree(ptr) btAlignedFreeInternal(ptr)
#endif
typedef int size_type;

View File

@@ -50,7 +50,7 @@ public:
if(usedsize==0)
{
if(!ischild)
if(!ischild && data)
btAlignedFree(data);
data = 0;