From 56c69bc42e9adeb913bbfb82a33fbc9861cbc0e1 Mon Sep 17 00:00:00 2001 From: ejcoumans Date: Sun, 11 Nov 2007 22:48:08 +0000 Subject: [PATCH] - stop threads when exiting demo that uses multi threading - improved friction model for parallel solver (align the friction direction with projected velocity, unless the projection is close to zero (only then use 2 arbitrary axis orthogonal to contact normal) --- ChangeLog.txt | 9 +++++ Demos/AllBulletDemos/DemoEntries.cpp | 6 ++-- Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp | 19 ++++++++-- Demos/CcdPhysicsDemo/CcdPhysicsDemo.h | 5 +++ .../SpuSolverTask/SpuParallellSolverTask.cpp | 20 +++++++---- .../Win32ThreadSupport.cpp | 36 +++++++++++++++---- 6 files changed, 76 insertions(+), 19 deletions(-) diff --git a/ChangeLog.txt b/ChangeLog.txt index 607ffceb1..24fb55fcb 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1,6 +1,15 @@ Bullet Continuous Collision Detection and Physics Library Primary author and maintainer: Erwin Coumans +2007 Nov 11 + - Fixed parallel solver (BulletMultiThreaded) friction issue + - Terminate Win32 Threads when closing the CcdPhysicsDemo (when USE_PARALLEL_SOLVER/USE_PARALLEL_DISPATCHER is defined) + +2007 Nov 6 + - Added support for 16-bit indices for triangle meshes + - Added support for multiple mesh parts using btBvhTriangleMeshShape. + Thanks to Tim Johansson + 2007 Oct 22 - All memory allocations go through btAlignedAlloc/btAlignedFree. User can override this to verify memory leaks - added a few more demos to AllBulletDemos diff --git a/Demos/AllBulletDemos/DemoEntries.cpp b/Demos/AllBulletDemos/DemoEntries.cpp index 6592ddc40..6d4a549d3 100644 --- a/Demos/AllBulletDemos/DemoEntries.cpp +++ b/Demos/AllBulletDemos/DemoEntries.cpp @@ -93,12 +93,12 @@ public: btDemoEntry g_demoEntries[] = { - {"BasicDemo", BasicDemo::Create}, {"RagdollDemo",RagdollDemo::Create}, - {"ConvexDecomposition",ConvexDecompositionDemo::Create}, {"CcdPhysicsDemo", CcdPhysicsDemo::Create}, - {"BspDemo", BspDemo::Create}, {"ConcaveDemo",ConcaveDemo::Create}, + {"ConvexDecomposition",ConvexDecompositionDemo::Create}, + {"BasicDemo", BasicDemo::Create}, + {"BspDemo", BspDemo::Create}, {"Gimpact Test", GimpactConcaveDemo::Create}, {"Raytracer Test",Raytracer::Create}, {"GjkConvexCast",LinearConvexCastDemo::Create}, diff --git a/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp b/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp index 59ea526f3..5a09c1c0e 100644 --- a/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp +++ b/Demos/CcdPhysicsDemo/CcdPhysicsDemo.cpp @@ -322,6 +322,9 @@ float myFrictionModel( btRigidBody& body1, btRigidBody& body2, btManifoldPoint& void CcdPhysicsDemo::initPhysics() { + m_threadSupportSolver = 0; + m_threadSupportCollision = 0; + //#define USE_GROUND_PLANE 1 #ifdef USE_GROUND_PLANE m_collisionShapes.push_back(new btStaticPlaneShape(btVector3(0,1,0),0.5)); @@ -356,7 +359,7 @@ int maxNumOutstandingTasks = 4; #ifdef USE_WIN32_THREADING - Win32ThreadSupport* threadSupportCollision = new Win32ThreadSupport(Win32ThreadSupport::Win32ThreadConstructionInfo( + m_threadSupportCollision = new Win32ThreadSupport(Win32ThreadSupport::Win32ThreadConstructionInfo( "collision", processCollisionTask, createCollisionLocalStoreMemory, @@ -389,7 +392,7 @@ int maxNumOutstandingTasks = 4; #endif - m_dispatcher = new SpuGatheringCollisionDispatcher(threadSupportCollision,maxNumOutstandingTasks,m_collisionConfiguration); + m_dispatcher = new SpuGatheringCollisionDispatcher(m_threadSupportCollision,maxNumOutstandingTasks,m_collisionConfiguration); // m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration); #else @@ -422,7 +425,7 @@ int maxNumOutstandingTasks = 4; #ifdef USE_PARALLEL_SOLVER - Win32ThreadSupport* threadSupportSolver = new Win32ThreadSupport(Win32ThreadSupport::Win32ThreadConstructionInfo( + m_threadSupportSolver = new Win32ThreadSupport(Win32ThreadSupport::Win32ThreadConstructionInfo( "solver", processSolverTask, createSolverLocalStoreMemory, @@ -648,12 +651,22 @@ void CcdPhysicsDemo::exitPhysics() //delete solver delete m_solver; + if (m_threadSupportSolver) + { + delete m_threadSupportSolver; + } + //delete broadphase delete m_broadphase; //delete dispatcher delete m_dispatcher; + if (m_threadSupportCollision) + { + delete m_threadSupportCollision; + } + delete m_collisionConfiguration; diff --git a/Demos/CcdPhysicsDemo/CcdPhysicsDemo.h b/Demos/CcdPhysicsDemo/CcdPhysicsDemo.h index 71d6313e3..128e558e6 100644 --- a/Demos/CcdPhysicsDemo/CcdPhysicsDemo.h +++ b/Demos/CcdPhysicsDemo/CcdPhysicsDemo.h @@ -25,6 +25,7 @@ class btCollisionDispatcher; class btConstraintSolver; struct btCollisionAlgorithmCreateFunc; class btDefaultCollisionConfiguration; +class Win32ThreadSupport; ///CcdPhysicsDemo shows basic stacking using Bullet physics, and allows toggle of Ccd (using key '1') class CcdPhysicsDemo : public DemoApplication @@ -37,6 +38,10 @@ class CcdPhysicsDemo : public DemoApplication btCollisionDispatcher* m_dispatcher; + Win32ThreadSupport* m_threadSupportCollision; + + Win32ThreadSupport* m_threadSupportSolver; + btConstraintSolver* m_solver; btCollisionAlgorithmCreateFunc* m_boxBoxCF; diff --git a/Extras/BulletMultiThreaded/SpuSolverTask/SpuParallellSolverTask.cpp b/Extras/BulletMultiThreaded/SpuSolverTask/SpuParallellSolverTask.cpp index ae74c9773..3bd8fc859 100644 --- a/Extras/BulletMultiThreaded/SpuSolverTask/SpuParallellSolverTask.cpp +++ b/Extras/BulletMultiThreaded/SpuSolverTask/SpuParallellSolverTask.cpp @@ -1026,6 +1026,9 @@ void processSolverTask(void* userPtr, void* lsMemory) btVector3 rel_pos1 = pos1 - rb0->getCenterOfMassPosition(); btVector3 rel_pos2 = pos2 - rb1->getCenterOfMassPosition(); + btScalar rel_vel; + btVector3 vel; + // De-penetration { SpuSolverInternalConstraint& constraint = localMemory->m_tempInternalConstr[0]; @@ -1034,7 +1037,6 @@ void processSolverTask(void* userPtr, void* lsMemory) constraint.m_localOffsetBodyB = offsB; constraint.m_normal = cp.m_normalWorldOnB; - { //can be optimized, the cross products are already calculated constraint.m_jacDiagABInv = computeJacobianInverse (rb0, rb1, pos1, pos2, cp.m_normalWorldOnB); @@ -1046,11 +1048,9 @@ void processSolverTask(void* userPtr, void* lsMemory) btVector3 vel1 = rb0->getVelocityInLocalPoint(rel_pos1); btVector3 vel2 = rb1->getVelocityInLocalPoint(rel_pos2); - btVector3 vel = vel1 - vel2; - btScalar rel_vel; + vel = vel1 - vel2; rel_vel = cp.m_normalWorldOnB.dot(vel); - constraint.m_penetration = cp.getDistance();///btScalar(infoGlobal.m_numIterations); constraint.m_friction = cp.m_combinedFriction; float rest = - rel_vel * cp.m_combinedRestitution; @@ -1082,8 +1082,16 @@ void processSolverTask(void* userPtr, void* lsMemory) btVector3 frictionTangential0a, frictionTangential1b; - btPlaneSpace1(cp.m_normalWorldOnB,frictionTangential0a,frictionTangential1b); - + frictionTangential0a = vel - cp.m_normalWorldOnB * rel_vel; + btScalar lat_rel_vel = frictionTangential0a.length2(); + if (lat_rel_vel > SIMD_EPSILON)//0.0f) + { + frictionTangential0a /= btSqrt(lat_rel_vel); + frictionTangential1b = frictionTangential0a.cross(cp.m_normalWorldOnB); + } else + { + btPlaneSpace1(cp.m_normalWorldOnB,frictionTangential0a,frictionTangential1b); + } { SpuSolverInternalConstraint& constraint = localMemory->m_tempInternalConstr[1]; diff --git a/Extras/BulletMultiThreaded/Win32ThreadSupport.cpp b/Extras/BulletMultiThreaded/Win32ThreadSupport.cpp index e81753aeb..81a001342 100644 --- a/Extras/BulletMultiThreaded/Win32ThreadSupport.cpp +++ b/Extras/BulletMultiThreaded/Win32ThreadSupport.cpp @@ -52,23 +52,27 @@ DWORD WINAPI Thread_no_1( LPVOID lpParam ) while (1) { WaitForSingleObject(status->m_eventStartHandle,INFINITE); - btAssert(status->m_status); - + void* userPtr = status->m_userPtr; if (userPtr) { + btAssert(status->m_status); status->m_userThreadFunc(userPtr,status->m_lsMemory); status->m_status = 2; SetEvent(status->m_eventCompletetHandle); } else { //exit Thread + status->m_status = 3; + SetEvent(status->m_eventCompletetHandle); + printf("Thread with taskId %i with handle %i exiting\n",status->m_taskId, status->m_threadHandle); break; } } + printf("Thread TERMINATED\n"); return 0; } @@ -202,7 +206,7 @@ void Win32ThreadSupport::startThreads(Win32ThreadConstructionInfo& threadConstru SetThreadPriority(handle,THREAD_PRIORITY_HIGHEST); //SetThreadPriority(handle,THREAD_PRIORITY_TIME_CRITICAL); - //SetThreadAffinityMask(handle, 1<0) + { + WaitForSingleObject(spuStatus.m_eventCompletetHandle, INFINITE); + } + + + spuStatus.m_userPtr = 0; + SetEvent(spuStatus.m_eventStartHandle); + WaitForSingleObject(spuStatus.m_eventCompletetHandle, INFINITE); + + CloseHandle(spuStatus.m_eventCompletetHandle); + CloseHandle(spuStatus.m_eventStartHandle); + CloseHandle(spuStatus.m_threadHandle); + } + + m_activeSpuStatus.clear(); + m_completeHandles.clear(); + } #endif //USE_WIN32_THREADING