btDiscreteDynamicsWorldMt.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #include "btDiscreteDynamicsWorldMt.h"
  14. //collision detection
  15. #include "BulletCollision/CollisionDispatch/btCollisionDispatcher.h"
  16. #include "BulletCollision/BroadphaseCollision/btSimpleBroadphase.h"
  17. #include "BulletCollision/BroadphaseCollision/btCollisionAlgorithm.h"
  18. #include "BulletCollision/CollisionShapes/btCollisionShape.h"
  19. #include "btSimulationIslandManagerMt.h"
  20. #include "LinearMath/btTransformUtil.h"
  21. #include "LinearMath/btQuickprof.h"
  22. //rigidbody & constraints
  23. #include "BulletDynamics/Dynamics/btRigidBody.h"
  24. #include "BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"
  25. #include "BulletDynamics/ConstraintSolver/btContactSolverInfo.h"
  26. #include "BulletDynamics/ConstraintSolver/btTypedConstraint.h"
  27. #include "BulletDynamics/ConstraintSolver/btPoint2PointConstraint.h"
  28. #include "BulletDynamics/ConstraintSolver/btHingeConstraint.h"
  29. #include "BulletDynamics/ConstraintSolver/btConeTwistConstraint.h"
  30. #include "BulletDynamics/ConstraintSolver/btGeneric6DofConstraint.h"
  31. #include "BulletDynamics/ConstraintSolver/btGeneric6DofSpring2Constraint.h"
  32. #include "BulletDynamics/ConstraintSolver/btSliderConstraint.h"
  33. #include "BulletDynamics/ConstraintSolver/btContactConstraint.h"
  34. #include "LinearMath/btIDebugDraw.h"
  35. #include "BulletCollision/CollisionShapes/btSphereShape.h"
  36. #include "BulletDynamics/Dynamics/btActionInterface.h"
  37. #include "LinearMath/btQuickprof.h"
  38. #include "LinearMath/btMotionState.h"
  39. #include "LinearMath/btSerializer.h"
  40. ///
  41. /// btConstraintSolverPoolMt
  42. ///
  43. btConstraintSolverPoolMt::ThreadSolver* btConstraintSolverPoolMt::getAndLockThreadSolver()
  44. {
  45. int i = 0;
  46. #if BT_THREADSAFE
  47. i = btGetCurrentThreadIndex() % m_solvers.size();
  48. #endif // #if BT_THREADSAFE
  49. while (true)
  50. {
  51. ThreadSolver& solver = m_solvers[i];
  52. if (solver.mutex.tryLock())
  53. {
  54. return &solver;
  55. }
  56. // failed, try the next one
  57. i = (i + 1) % m_solvers.size();
  58. }
  59. return NULL;
  60. }
  61. void btConstraintSolverPoolMt::init(btConstraintSolver** solvers, int numSolvers)
  62. {
  63. m_solverType = BT_SEQUENTIAL_IMPULSE_SOLVER;
  64. m_solvers.resize(numSolvers);
  65. for (int i = 0; i < numSolvers; ++i)
  66. {
  67. m_solvers[i].solver = solvers[i];
  68. }
  69. if (numSolvers > 0)
  70. {
  71. m_solverType = solvers[0]->getSolverType();
  72. }
  73. }
  74. // create the solvers for me
  75. btConstraintSolverPoolMt::btConstraintSolverPoolMt(int numSolvers)
  76. {
  77. btAlignedObjectArray<btConstraintSolver*> solvers;
  78. solvers.reserve(numSolvers);
  79. for (int i = 0; i < numSolvers; ++i)
  80. {
  81. btConstraintSolver* solver = new btSequentialImpulseConstraintSolver();
  82. solvers.push_back(solver);
  83. }
  84. init(&solvers[0], numSolvers);
  85. }
  86. // pass in fully constructed solvers (destructor will delete them)
  87. btConstraintSolverPoolMt::btConstraintSolverPoolMt(btConstraintSolver** solvers, int numSolvers)
  88. {
  89. init(solvers, numSolvers);
  90. }
  91. btConstraintSolverPoolMt::~btConstraintSolverPoolMt()
  92. {
  93. // delete all solvers
  94. for (int i = 0; i < m_solvers.size(); ++i)
  95. {
  96. ThreadSolver& solver = m_solvers[i];
  97. delete solver.solver;
  98. solver.solver = NULL;
  99. }
  100. }
  101. ///solve a group of constraints
  102. btScalar btConstraintSolverPoolMt::solveGroup(btCollisionObject** bodies,
  103. int numBodies,
  104. btPersistentManifold** manifolds,
  105. int numManifolds,
  106. btTypedConstraint** constraints,
  107. int numConstraints,
  108. const btContactSolverInfo& info,
  109. btIDebugDraw* debugDrawer,
  110. btDispatcher* dispatcher)
  111. {
  112. ThreadSolver* ts = getAndLockThreadSolver();
  113. ts->solver->solveGroup(bodies, numBodies, manifolds, numManifolds, constraints, numConstraints, info, debugDrawer, dispatcher);
  114. ts->mutex.unlock();
  115. return 0.0f;
  116. }
  117. void btConstraintSolverPoolMt::reset()
  118. {
  119. for (int i = 0; i < m_solvers.size(); ++i)
  120. {
  121. ThreadSolver& solver = m_solvers[i];
  122. solver.mutex.lock();
  123. solver.solver->reset();
  124. solver.mutex.unlock();
  125. }
  126. }
  127. ///
  128. /// btDiscreteDynamicsWorldMt
  129. ///
  130. btDiscreteDynamicsWorldMt::btDiscreteDynamicsWorldMt(btDispatcher* dispatcher,
  131. btBroadphaseInterface* pairCache,
  132. btConstraintSolverPoolMt* solverPool,
  133. btConstraintSolver* constraintSolverMt,
  134. btCollisionConfiguration* collisionConfiguration)
  135. : btDiscreteDynamicsWorld(dispatcher, pairCache, solverPool, collisionConfiguration)
  136. {
  137. if (m_ownsIslandManager)
  138. {
  139. m_islandManager->~btSimulationIslandManager();
  140. btAlignedFree(m_islandManager);
  141. }
  142. {
  143. void* mem = btAlignedAlloc(sizeof(btSimulationIslandManagerMt), 16);
  144. btSimulationIslandManagerMt* im = new (mem) btSimulationIslandManagerMt();
  145. im->setMinimumSolverBatchSize(m_solverInfo.m_minimumSolverBatchSize);
  146. m_islandManager = im;
  147. }
  148. m_constraintSolverMt = constraintSolverMt;
  149. }
  150. btDiscreteDynamicsWorldMt::~btDiscreteDynamicsWorldMt()
  151. {
  152. }
  153. void btDiscreteDynamicsWorldMt::solveConstraints(btContactSolverInfo& solverInfo)
  154. {
  155. BT_PROFILE("solveConstraints");
  156. m_constraintSolver->prepareSolve(getCollisionWorld()->getNumCollisionObjects(), getCollisionWorld()->getDispatcher()->getNumManifolds());
  157. /// solve all the constraints for this island
  158. btSimulationIslandManagerMt* im = static_cast<btSimulationIslandManagerMt*>(m_islandManager);
  159. btSimulationIslandManagerMt::SolverParams solverParams;
  160. solverParams.m_solverPool = m_constraintSolver;
  161. solverParams.m_solverMt = m_constraintSolverMt;
  162. solverParams.m_solverInfo = &solverInfo;
  163. solverParams.m_debugDrawer = m_debugDrawer;
  164. solverParams.m_dispatcher = getCollisionWorld()->getDispatcher();
  165. im->buildAndProcessIslands(getCollisionWorld()->getDispatcher(), getCollisionWorld(), m_constraints, solverParams);
  166. m_constraintSolver->allSolved(solverInfo, m_debugDrawer);
  167. }
  168. struct UpdaterUnconstrainedMotion : public btIParallelForBody
  169. {
  170. btScalar timeStep;
  171. btRigidBody** rigidBodies;
  172. void forLoop(int iBegin, int iEnd) const BT_OVERRIDE
  173. {
  174. for (int i = iBegin; i < iEnd; ++i)
  175. {
  176. btRigidBody* body = rigidBodies[i];
  177. if (!body->isStaticOrKinematicObject())
  178. {
  179. //don't integrate/update velocities here, it happens in the constraint solver
  180. body->applyDamping(timeStep);
  181. body->predictIntegratedTransform(timeStep, body->getInterpolationWorldTransform());
  182. }
  183. }
  184. }
  185. };
  186. void btDiscreteDynamicsWorldMt::predictUnconstraintMotion(btScalar timeStep)
  187. {
  188. BT_PROFILE("predictUnconstraintMotion");
  189. if (m_nonStaticRigidBodies.size() > 0)
  190. {
  191. UpdaterUnconstrainedMotion update;
  192. update.timeStep = timeStep;
  193. update.rigidBodies = &m_nonStaticRigidBodies[0];
  194. int grainSize = 50; // num of iterations per task for task scheduler
  195. btParallelFor(0, m_nonStaticRigidBodies.size(), grainSize, update);
  196. }
  197. }
  198. void btDiscreteDynamicsWorldMt::createPredictiveContacts(btScalar timeStep)
  199. {
  200. BT_PROFILE("createPredictiveContacts");
  201. releasePredictiveContacts();
  202. if (m_nonStaticRigidBodies.size() > 0)
  203. {
  204. UpdaterCreatePredictiveContacts update;
  205. update.world = this;
  206. update.timeStep = timeStep;
  207. update.rigidBodies = &m_nonStaticRigidBodies[0];
  208. int grainSize = 50; // num of iterations per task for task scheduler
  209. btParallelFor(0, m_nonStaticRigidBodies.size(), grainSize, update);
  210. }
  211. }
  212. void btDiscreteDynamicsWorldMt::integrateTransforms(btScalar timeStep)
  213. {
  214. BT_PROFILE("integrateTransforms");
  215. if (m_nonStaticRigidBodies.size() > 0)
  216. {
  217. UpdaterIntegrateTransforms update;
  218. update.world = this;
  219. update.timeStep = timeStep;
  220. update.rigidBodies = &m_nonStaticRigidBodies[0];
  221. int grainSize = 50; // num of iterations per task for task scheduler
  222. btParallelFor(0, m_nonStaticRigidBodies.size(), grainSize, update);
  223. }
  224. }
  225. int btDiscreteDynamicsWorldMt::stepSimulation(btScalar timeStep, int maxSubSteps, btScalar fixedTimeStep)
  226. {
  227. int numSubSteps = btDiscreteDynamicsWorld::stepSimulation(timeStep, maxSubSteps, fixedTimeStep);
  228. if (btITaskScheduler* scheduler = btGetTaskScheduler())
  229. {
  230. // tell Bullet's threads to sleep, so other threads can run
  231. scheduler->sleepWorkerThreadsHint();
  232. }
  233. return numSubSteps;
  234. }