2017-08-01 14:30:58 +02:00
|
|
|
/*
|
|
|
|
Bullet Continuous Collision Detection and Physics Library
|
|
|
|
Copyright (c) 2003-2009 Erwin Coumans http://bulletphysics.org
|
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied warranty.
|
|
|
|
In no event will the authors be held liable for any damages arising from the use of this software.
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it freely,
|
|
|
|
subject to the following restrictions:
|
|
|
|
|
|
|
|
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.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef BT_DISCRETE_DYNAMICS_WORLD_MT_H
|
|
|
|
#define BT_DISCRETE_DYNAMICS_WORLD_MT_H
|
|
|
|
|
|
|
|
#include "btDiscreteDynamicsWorld.h"
|
|
|
|
#include "btSimulationIslandManagerMt.h"
|
|
|
|
#include "BulletDynamics/ConstraintSolver/btConstraintSolver.h"
|
|
|
|
|
|
|
|
///
|
|
|
|
/// btConstraintSolverPoolMt - masquerades as a constraint solver, but really it is a threadsafe pool of them.
|
|
|
|
///
|
|
|
|
/// Each solver in the pool is protected by a mutex. When solveGroup is called from a thread,
|
|
|
|
/// the pool looks for a solver that isn't being used by another thread, locks it, and dispatches the
|
|
|
|
/// call to the solver.
|
|
|
|
/// So long as there are at least as many solvers as there are hardware threads, it should never need to
|
|
|
|
/// spin wait.
|
|
|
|
///
|
|
|
|
class btConstraintSolverPoolMt : public btConstraintSolver
|
|
|
|
{
|
|
|
|
public:
|
2019-01-03 14:26:51 +01:00
|
|
|
// create the solvers for me
|
|
|
|
explicit btConstraintSolverPoolMt(int numSolvers);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
// pass in fully constructed solvers (destructor will delete them)
|
|
|
|
btConstraintSolverPoolMt(btConstraintSolver** solvers, int numSolvers);
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
virtual ~btConstraintSolverPoolMt();
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
///solve a group of constraints
|
|
|
|
virtual btScalar solveGroup(btCollisionObject** bodies,
|
|
|
|
int numBodies,
|
|
|
|
btPersistentManifold** manifolds,
|
|
|
|
int numManifolds,
|
|
|
|
btTypedConstraint** constraints,
|
|
|
|
int numConstraints,
|
|
|
|
const btContactSolverInfo& info,
|
|
|
|
btIDebugDraw* debugDrawer,
|
|
|
|
btDispatcher* dispatcher) BT_OVERRIDE;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
virtual void reset() BT_OVERRIDE;
|
|
|
|
virtual btConstraintSolverType getSolverType() const BT_OVERRIDE { return m_solverType; }
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
private:
|
2019-01-03 14:26:51 +01:00
|
|
|
const static size_t kCacheLineSize = 128;
|
|
|
|
struct ThreadSolver
|
|
|
|
{
|
|
|
|
btConstraintSolver* solver;
|
|
|
|
btSpinMutex mutex;
|
|
|
|
char _cachelinePadding[kCacheLineSize - sizeof(btSpinMutex) - sizeof(void*)]; // keep mutexes from sharing a cache line
|
|
|
|
};
|
|
|
|
btAlignedObjectArray<ThreadSolver> m_solvers;
|
|
|
|
btConstraintSolverType m_solverType;
|
|
|
|
|
|
|
|
ThreadSolver* getAndLockThreadSolver();
|
|
|
|
void init(btConstraintSolver** solvers, int numSolvers);
|
2017-08-01 14:30:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
///
|
|
|
|
/// btDiscreteDynamicsWorldMt -- a version of DiscreteDynamicsWorld with some minor changes to support
|
|
|
|
/// solving simulation islands on multiple threads.
|
|
|
|
///
|
|
|
|
/// Should function exactly like btDiscreteDynamicsWorld.
|
|
|
|
/// Also 3 methods that iterate over all of the rigidbodies can run in parallel:
|
|
|
|
/// - predictUnconstraintMotion
|
|
|
|
/// - integrateTransforms
|
|
|
|
/// - createPredictiveContacts
|
|
|
|
///
|
2019-01-03 14:26:51 +01:00
|
|
|
ATTRIBUTE_ALIGNED16(class)
|
|
|
|
btDiscreteDynamicsWorldMt : public btDiscreteDynamicsWorld
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
protected:
|
2019-01-03 14:26:51 +01:00
|
|
|
btConstraintSolver* m_constraintSolverMt;
|
|
|
|
|
|
|
|
virtual void solveConstraints(btContactSolverInfo & solverInfo) BT_OVERRIDE;
|
|
|
|
|
|
|
|
virtual void predictUnconstraintMotion(btScalar timeStep) BT_OVERRIDE;
|
|
|
|
|
|
|
|
struct UpdaterCreatePredictiveContacts : public btIParallelForBody
|
|
|
|
{
|
|
|
|
btScalar timeStep;
|
|
|
|
btRigidBody** rigidBodies;
|
|
|
|
btDiscreteDynamicsWorldMt* world;
|
|
|
|
|
|
|
|
void forLoop(int iBegin, int iEnd) const BT_OVERRIDE
|
|
|
|
{
|
|
|
|
world->createPredictiveContactsInternal(&rigidBodies[iBegin], iEnd - iBegin, timeStep);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
virtual void createPredictiveContacts(btScalar timeStep) BT_OVERRIDE;
|
|
|
|
|
|
|
|
struct UpdaterIntegrateTransforms : public btIParallelForBody
|
|
|
|
{
|
|
|
|
btScalar timeStep;
|
|
|
|
btRigidBody** rigidBodies;
|
|
|
|
btDiscreteDynamicsWorldMt* world;
|
|
|
|
|
|
|
|
void forLoop(int iBegin, int iEnd) const BT_OVERRIDE
|
|
|
|
{
|
|
|
|
world->integrateTransformsInternal(&rigidBodies[iBegin], iEnd - iBegin, timeStep);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
virtual void integrateTransforms(btScalar timeStep) BT_OVERRIDE;
|
2017-08-01 14:30:58 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
BT_DECLARE_ALIGNED_ALLOCATOR();
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
btDiscreteDynamicsWorldMt(btDispatcher * dispatcher,
|
|
|
|
btBroadphaseInterface * pairCache,
|
|
|
|
btConstraintSolverPoolMt * solverPool, // Note this should be a solver-pool for multi-threading
|
|
|
|
btConstraintSolver * constraintSolverMt, // single multi-threaded solver for large islands (or NULL)
|
|
|
|
btCollisionConfiguration * collisionConfiguration);
|
2017-08-01 14:30:58 +02:00
|
|
|
virtual ~btDiscreteDynamicsWorldMt();
|
2018-09-07 16:11:04 +02:00
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
virtual int stepSimulation(btScalar timeStep, int maxSubSteps, btScalar fixedTimeStep) BT_OVERRIDE;
|
2017-08-01 14:30:58 +02:00
|
|
|
};
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
#endif //BT_DISCRETE_DYNAMICS_WORLD_H
|