2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
16 #ifndef BT_DISCRETE_DYNAMICS_WORLD_MT_H
17 #define BT_DISCRETE_DYNAMICS_WORLD_MT_H
19 #include "btDiscreteDynamicsWorld.h"
20 #include "btSimulationIslandManagerMt.h"
21 #include "BulletDynamics/ConstraintSolver/btConstraintSolver.h"
24 /// btConstraintSolverPoolMt - masquerades as a constraint solver, but really it is a threadsafe pool of them.
26 /// Each solver in the pool is protected by a mutex. When solveGroup is called from a thread,
27 /// the pool looks for a solver that isn't being used by another thread, locks it, and dispatches the
28 /// call to the solver.
29 /// So long as there are at least as many solvers as there are hardware threads, it should never need to
32 class btConstraintSolverPoolMt : public btConstraintSolver
35 // create the solvers for me
36 explicit btConstraintSolverPoolMt(int numSolvers);
38 // pass in fully constructed solvers (destructor will delete them)
39 btConstraintSolverPoolMt(btConstraintSolver** solvers, int numSolvers);
41 virtual ~btConstraintSolverPoolMt();
43 ///solve a group of constraints
44 virtual btScalar solveGroup(btCollisionObject** bodies,
46 btPersistentManifold** manifolds,
48 btTypedConstraint** constraints,
50 const btContactSolverInfo& info,
51 btIDebugDraw* debugDrawer,
52 btDispatcher* dispatcher) BT_OVERRIDE;
54 virtual void reset() BT_OVERRIDE;
55 virtual btConstraintSolverType getSolverType() const BT_OVERRIDE { return m_solverType; }
58 const static size_t kCacheLineSize = 128;
61 btConstraintSolver* solver;
63 char _cachelinePadding[kCacheLineSize - sizeof(btSpinMutex) - sizeof(void*)]; // keep mutexes from sharing a cache line
65 btAlignedObjectArray<ThreadSolver> m_solvers;
66 btConstraintSolverType m_solverType;
68 ThreadSolver* getAndLockThreadSolver();
69 void init(btConstraintSolver** solvers, int numSolvers);
73 /// btDiscreteDynamicsWorldMt -- a version of DiscreteDynamicsWorld with some minor changes to support
74 /// solving simulation islands on multiple threads.
76 /// Should function exactly like btDiscreteDynamicsWorld.
77 /// Also 3 methods that iterate over all of the rigidbodies can run in parallel:
78 /// - predictUnconstraintMotion
79 /// - integrateTransforms
80 /// - createPredictiveContacts
82 ATTRIBUTE_ALIGNED16(class)
83 btDiscreteDynamicsWorldMt : public btDiscreteDynamicsWorld
86 btConstraintSolver* m_constraintSolverMt;
88 virtual void solveConstraints(btContactSolverInfo & solverInfo) BT_OVERRIDE;
90 virtual void predictUnconstraintMotion(btScalar timeStep) BT_OVERRIDE;
92 struct UpdaterCreatePredictiveContacts : public btIParallelForBody
95 btRigidBody** rigidBodies;
96 btDiscreteDynamicsWorldMt* world;
98 void forLoop(int iBegin, int iEnd) const BT_OVERRIDE
100 world->createPredictiveContactsInternal(&rigidBodies[iBegin], iEnd - iBegin, timeStep);
103 virtual void createPredictiveContacts(btScalar timeStep) BT_OVERRIDE;
105 struct UpdaterIntegrateTransforms : public btIParallelForBody
108 btRigidBody** rigidBodies;
109 btDiscreteDynamicsWorldMt* world;
111 void forLoop(int iBegin, int iEnd) const BT_OVERRIDE
113 world->integrateTransformsInternal(&rigidBodies[iBegin], iEnd - iBegin, timeStep);
116 virtual void integrateTransforms(btScalar timeStep) BT_OVERRIDE;
119 BT_DECLARE_ALIGNED_ALLOCATOR();
121 btDiscreteDynamicsWorldMt(btDispatcher * dispatcher,
122 btBroadphaseInterface * pairCache,
123 btConstraintSolverPoolMt * solverPool, // Note this should be a solver-pool for multi-threading
124 btConstraintSolver * constraintSolverMt, // single multi-threaded solver for large islands (or NULL)
125 btCollisionConfiguration * collisionConfiguration);
126 virtual ~btDiscreteDynamicsWorldMt();
128 virtual int stepSimulation(btScalar timeStep, int maxSubSteps, btScalar fixedTimeStep) BT_OVERRIDE;
131 #endif //BT_DISCRETE_DYNAMICS_WORLD_H