btSimulationIslandManagerMt.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2006 Erwin Coumans https://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_SIMULATION_ISLAND_MANAGER_MT_H
  14. #define BT_SIMULATION_ISLAND_MANAGER_MT_H
  15. #include "BulletCollision/CollisionDispatch/btSimulationIslandManager.h"
  16. class btTypedConstraint;
  17. class btConstraintSolver;
  18. struct btContactSolverInfo;
  19. class btIDebugDraw;
  20. ///
  21. /// SimulationIslandManagerMt -- Multithread capable version of SimulationIslandManager
  22. /// Splits the world up into islands which can be solved in parallel.
  23. /// In order to solve islands in parallel, an IslandDispatch function
  24. /// must be provided which will dispatch calls to multiple threads.
  25. /// The amount of parallelism that can be achieved depends on the number
  26. /// of islands. If only a single island exists, then no parallelism is
  27. /// possible.
  28. ///
  29. class btSimulationIslandManagerMt : public btSimulationIslandManager
  30. {
  31. public:
  32. struct Island
  33. {
  34. // a simulation island consisting of bodies, manifolds and constraints,
  35. // to be passed into a constraint solver.
  36. btAlignedObjectArray<btCollisionObject*> bodyArray;
  37. btAlignedObjectArray<btPersistentManifold*> manifoldArray;
  38. btAlignedObjectArray<btTypedConstraint*> constraintArray;
  39. int id; // island id
  40. bool isSleeping;
  41. void append(const Island& other); // add bodies, manifolds, constraints to my own
  42. };
  43. struct SolverParams
  44. {
  45. btConstraintSolver* m_solverPool;
  46. btConstraintSolver* m_solverMt;
  47. btContactSolverInfo* m_solverInfo;
  48. btIDebugDraw* m_debugDrawer;
  49. btDispatcher* m_dispatcher;
  50. };
  51. static void solveIsland(btConstraintSolver* solver, Island& island, const SolverParams& solverParams);
  52. typedef void (*IslandDispatchFunc)(btAlignedObjectArray<Island*>* islands, const SolverParams& solverParams);
  53. static void serialIslandDispatch(btAlignedObjectArray<Island*>* islandsPtr, const SolverParams& solverParams);
  54. static void parallelIslandDispatch(btAlignedObjectArray<Island*>* islandsPtr, const SolverParams& solverParams);
  55. protected:
  56. btAlignedObjectArray<Island*> m_allocatedIslands; // owner of all Islands
  57. btAlignedObjectArray<Island*> m_activeIslands; // islands actively in use
  58. btAlignedObjectArray<Island*> m_freeIslands; // islands ready to be reused
  59. btAlignedObjectArray<Island*> m_lookupIslandFromId; // big lookup table to map islandId to Island pointer
  60. Island* m_batchIsland;
  61. int m_minimumSolverBatchSize;
  62. int m_batchIslandMinBodyCount;
  63. IslandDispatchFunc m_islandDispatch;
  64. Island* getIsland(int id);
  65. virtual Island* allocateIsland(int id, int numBodies);
  66. virtual void initIslandPools();
  67. virtual void addBodiesToIslands(btCollisionWorld* collisionWorld);
  68. virtual void addManifoldsToIslands(btDispatcher* dispatcher);
  69. virtual void addConstraintsToIslands(btAlignedObjectArray<btTypedConstraint*>& constraints);
  70. virtual void mergeIslands();
  71. public:
  72. btSimulationIslandManagerMt();
  73. virtual ~btSimulationIslandManagerMt();
  74. virtual void buildAndProcessIslands(btDispatcher* dispatcher,
  75. btCollisionWorld* collisionWorld,
  76. btAlignedObjectArray<btTypedConstraint*>& constraints,
  77. const SolverParams& solverParams);
  78. virtual void buildIslands(btDispatcher* dispatcher, btCollisionWorld* colWorld);
  79. int getMinimumSolverBatchSize() const
  80. {
  81. return m_minimumSolverBatchSize;
  82. }
  83. void setMinimumSolverBatchSize(int sz)
  84. {
  85. m_minimumSolverBatchSize = sz;
  86. }
  87. IslandDispatchFunc getIslandDispatchFunction() const
  88. {
  89. return m_islandDispatch;
  90. }
  91. // allow users to set their own dispatch function for multithreaded dispatch
  92. void setIslandDispatchFunction(IslandDispatchFunc func)
  93. {
  94. m_islandDispatch = func;
  95. }
  96. };
  97. #endif //BT_SIMULATION_ISLAND_MANAGER_H