btDiscreteDynamicsWorldMt.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #ifndef BT_DISCRETE_DYNAMICS_WORLD_MT_H
  14. #define BT_DISCRETE_DYNAMICS_WORLD_MT_H
  15. #include "btDiscreteDynamicsWorld.h"
  16. #include "btSimulationIslandManagerMt.h"
  17. #include "BulletDynamics/ConstraintSolver/btConstraintSolver.h"
  18. ///
  19. /// btConstraintSolverPoolMt - masquerades as a constraint solver, but really it is a threadsafe pool of them.
  20. ///
  21. /// Each solver in the pool is protected by a mutex. When solveGroup is called from a thread,
  22. /// the pool looks for a solver that isn't being used by another thread, locks it, and dispatches the
  23. /// call to the solver.
  24. /// So long as there are at least as many solvers as there are hardware threads, it should never need to
  25. /// spin wait.
  26. ///
  27. class btConstraintSolverPoolMt : public btConstraintSolver
  28. {
  29. public:
  30. // create the solvers for me
  31. explicit btConstraintSolverPoolMt(int numSolvers);
  32. // pass in fully constructed solvers (destructor will delete them)
  33. btConstraintSolverPoolMt(btConstraintSolver** solvers, int numSolvers);
  34. virtual ~btConstraintSolverPoolMt();
  35. ///solve a group of constraints
  36. virtual btScalar solveGroup(btCollisionObject** bodies,
  37. int numBodies,
  38. btPersistentManifold** manifolds,
  39. int numManifolds,
  40. btTypedConstraint** constraints,
  41. int numConstraints,
  42. const btContactSolverInfo& info,
  43. btIDebugDraw* debugDrawer,
  44. btDispatcher* dispatcher) BT_OVERRIDE;
  45. virtual void reset() BT_OVERRIDE;
  46. virtual btConstraintSolverType getSolverType() const BT_OVERRIDE { return m_solverType; }
  47. private:
  48. const static size_t kCacheLineSize = 128;
  49. struct ThreadSolver
  50. {
  51. btConstraintSolver* solver;
  52. btSpinMutex mutex;
  53. char _cachelinePadding[kCacheLineSize - sizeof(btSpinMutex) - sizeof(void*)]; // keep mutexes from sharing a cache line
  54. };
  55. btAlignedObjectArray<ThreadSolver> m_solvers;
  56. btConstraintSolverType m_solverType;
  57. ThreadSolver* getAndLockThreadSolver();
  58. void init(btConstraintSolver** solvers, int numSolvers);
  59. };
  60. ///
  61. /// btDiscreteDynamicsWorldMt -- a version of DiscreteDynamicsWorld with some minor changes to support
  62. /// solving simulation islands on multiple threads.
  63. ///
  64. /// Should function exactly like btDiscreteDynamicsWorld.
  65. /// Also 3 methods that iterate over all of the rigidbodies can run in parallel:
  66. /// - predictUnconstraintMotion
  67. /// - integrateTransforms
  68. /// - createPredictiveContacts
  69. ///
  70. ATTRIBUTE_ALIGNED16(class)
  71. btDiscreteDynamicsWorldMt : public btDiscreteDynamicsWorld
  72. {
  73. protected:
  74. btConstraintSolver* m_constraintSolverMt;
  75. virtual void solveConstraints(btContactSolverInfo & solverInfo) BT_OVERRIDE;
  76. virtual void predictUnconstraintMotion(btScalar timeStep) BT_OVERRIDE;
  77. struct UpdaterCreatePredictiveContacts : public btIParallelForBody
  78. {
  79. btScalar timeStep;
  80. btRigidBody** rigidBodies;
  81. btDiscreteDynamicsWorldMt* world;
  82. void forLoop(int iBegin, int iEnd) const BT_OVERRIDE
  83. {
  84. world->createPredictiveContactsInternal(&rigidBodies[iBegin], iEnd - iBegin, timeStep);
  85. }
  86. };
  87. virtual void createPredictiveContacts(btScalar timeStep) BT_OVERRIDE;
  88. struct UpdaterIntegrateTransforms : public btIParallelForBody
  89. {
  90. btScalar timeStep;
  91. btRigidBody** rigidBodies;
  92. btDiscreteDynamicsWorldMt* world;
  93. void forLoop(int iBegin, int iEnd) const BT_OVERRIDE
  94. {
  95. world->integrateTransformsInternal(&rigidBodies[iBegin], iEnd - iBegin, timeStep);
  96. }
  97. };
  98. virtual void integrateTransforms(btScalar timeStep) BT_OVERRIDE;
  99. public:
  100. BT_DECLARE_ALIGNED_ALLOCATOR();
  101. btDiscreteDynamicsWorldMt(btDispatcher * dispatcher,
  102. btBroadphaseInterface * pairCache,
  103. btConstraintSolverPoolMt * solverPool, // Note this should be a solver-pool for multi-threading
  104. btConstraintSolver * constraintSolverMt, // single multi-threaded solver for large islands (or NULL)
  105. btCollisionConfiguration * collisionConfiguration);
  106. virtual ~btDiscreteDynamicsWorldMt();
  107. virtual int stepSimulation(btScalar timeStep, int maxSubSteps, btScalar fixedTimeStep) BT_OVERRIDE;
  108. };
  109. #endif //BT_DISCRETE_DYNAMICS_WORLD_H