MultiThreaded Demo:

- fixing various race conditions throughout (usage of static vars, etc)
 - addition of a few lightweight mutexes (which are compiled out by default)
 - slight code rearrangement in discreteDynamicsWorld to facilitate multithreading
 - PoolAllocator::allocate() can now be called when pool is full without
     crashing (null pointer returned)
 - PoolAllocator allocate and freeMemory, are OPTIONALLY threadsafe
     (default is un-threadsafe)
 - CollisionDispatcher no longer checks if the pool allocator is full
     before calling allocate(), instead it just calls allocate() and
     checks if the return is null -- this avoids a race condition
 - SequentialImpulseConstraintSolver OPTIONALLY uses different logic in
     getOrInitSolverBody() to avoid a race condition with kinematic bodies
 - addition of 2 classes which together allow simulation islands to be run
   in parallel:
    - btSimulationIslandManagerMt
    - btDiscreteDynamicsWorldMt
 - MultiThreadedDemo example in the example browser demonstrating use of
   OpenMP, Microsoft PPL, and Intel TBB
 - use multithreading for other demos
 - benchmark demo: add parallel raycasting
This commit is contained in:
Lunkhound
2016-09-27 00:01:45 -07:00
parent f01389ded2
commit 1c3686ca51
48 changed files with 3168 additions and 197 deletions

View File

@@ -0,0 +1,229 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
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.
*/
#include "btBulletDynamicsCommon.h"
#include "LinearMath/btQuickprof.h"
#include "LinearMath/btIDebugDraw.h"
#include <stdio.h> //printf debugging
#include <algorithm>
class btCollisionShape;
#include "../CommonInterfaces/CommonExampleInterface.h"
#include "../CommonInterfaces/CommonRigidBodyBase.h"
#include "../CommonInterfaces/CommonParameterInterface.h"
#include "../CommonInterfaces/CommonGUIHelperInterface.h"
#include "../CommonInterfaces/CommonRenderInterface.h"
#include "../CommonInterfaces/CommonWindowInterface.h"
#include "../CommonInterfaces/CommonGraphicsAppInterface.h"
#include "MultiThreadedDemo.h"
#include "LinearMath/btAlignedObjectArray.h"
#include "btBulletCollisionCommon.h"
#define BT_OVERRIDE
/// MultiThreadedDemo shows how to setup and use multithreading
class MultiThreadedDemo : public CommonRigidBodyBase
{
static const int kUpAxis = 1;
btRigidBody* localCreateRigidBody(btScalar mass, const btTransform& worldTransform, btCollisionShape* colSape);
btVector3 m_cameraTargetPos;
float m_cameraPitch;
float m_cameraYaw;
float m_cameraDist;
void createStack( const btVector3& pos, btCollisionShape* boxShape, const btVector3& halfBoxSize, int size );
void createSceneObjects();
void destroySceneObjects();
public:
BT_DECLARE_ALIGNED_ALLOCATOR();
MultiThreadedDemo( struct GUIHelperInterface* helper );
virtual ~MultiThreadedDemo() {}
virtual void stepSimulation( float deltaTime ) BT_OVERRIDE
{
if ( m_dynamicsWorld )
{
// always step by 1/60 for benchmarking
m_dynamicsWorld->stepSimulation( 1.0f / 60.0f, 0 );
}
}
virtual void initPhysics() BT_OVERRIDE;
virtual void resetCamera() BT_OVERRIDE
{
m_guiHelper->resetCamera( m_cameraDist,
m_cameraPitch,
m_cameraYaw,
m_cameraTargetPos.x(),
m_cameraTargetPos.y(),
m_cameraTargetPos.z()
);
}
};
MultiThreadedDemo::MultiThreadedDemo(struct GUIHelperInterface* helper)
: CommonRigidBodyBase( helper )
{
m_cameraTargetPos = btVector3( 0.0f, 0.0f, 0.0f );
m_cameraPitch = 90.0f;
m_cameraYaw = 30.0f;
m_cameraDist = 48.0f;
helper->setUpAxis( kUpAxis );
}
static btScalar gSliderStackRows = 8.0f;
static btScalar gSliderStackColumns = 6.0f;
void MultiThreadedDemo::initPhysics()
{
createEmptyDynamicsWorld();
m_dynamicsWorld->setGravity( btVector3( 0, -10, 0 ) );
{
SliderParams slider( "Stack rows", &gSliderStackRows );
slider.m_minVal = 1.0f;
slider.m_maxVal = 20.0f;
slider.m_clampToNotches = false;
m_guiHelper->getParameterInterface()->registerSliderFloatParameter( slider );
}
{
SliderParams slider( "Stack columns", &gSliderStackColumns );
slider.m_minVal = 1.0f;
slider.m_maxVal = 20.0f;
slider.m_clampToNotches = false;
m_guiHelper->getParameterInterface()->registerSliderFloatParameter( slider );
}
createSceneObjects();
m_guiHelper->createPhysicsDebugDrawer( m_dynamicsWorld );
}
btRigidBody* MultiThreadedDemo::localCreateRigidBody(btScalar mass, const btTransform& startTransform, btCollisionShape* shape)
{
btRigidBody* body = createRigidBody(mass, startTransform, shape);
if ( mass > 0.0f )
{
// prevent bodies from sleeping to make profiling/benchmarking easier
body->forceActivationState( DISABLE_DEACTIVATION );
}
return body;
}
void MultiThreadedDemo::createStack( const btVector3& center, btCollisionShape* boxShape, const btVector3& halfBoxSize, int size )
{
btTransform trans;
trans.setIdentity();
float halfBoxHeight = halfBoxSize.y();
float halfBoxWidth = halfBoxSize.x();
for ( int i = 0; i<size; i++ )
{
// This constructs a row, from left to right
int rowSize = size - i;
for ( int j = 0; j< rowSize; j++ )
{
btVector3 pos = center + btVector3( halfBoxWidth*( 1 + j * 2 - rowSize ),
halfBoxHeight * ( 1 + i * 2),
0.0f
);
trans.setOrigin( pos );
btScalar mass = 1.f;
btRigidBody* body = localCreateRigidBody( mass, trans, boxShape );
}
}
}
void MultiThreadedDemo::createSceneObjects()
{
{
// create ground box
btTransform tr;
tr.setIdentity();
tr.setOrigin( btVector3( 0, -3, 0 ) );
//either use heightfield or triangle mesh
btVector3 groundExtents( 400, 400, 400 );
groundExtents[ kUpAxis ] = 3;
btCollisionShape* groundShape = new btBoxShape( groundExtents );
m_collisionShapes.push_back( groundShape );
//create ground object
localCreateRigidBody( 0, tr, groundShape );
}
{
// create walls of cubes
const btVector3 halfExtents = btVector3( 0.5f, 0.25f, 0.5f );
int numStackRows = btMax(1, int(gSliderStackRows));
int numStackCols = btMax(1, int(gSliderStackColumns));
int stackHeight = 15;
float stackZSpacing = 3.0f;
float stackXSpacing = 20.0f;
btBoxShape* boxShape = new btBoxShape( halfExtents );
m_collisionShapes.push_back( boxShape );
for ( int iX = 0; iX < numStackCols; ++iX )
{
for ( int iZ = 0; iZ < numStackRows; ++iZ )
{
btVector3 center = btVector3( iX * stackXSpacing, 0.0f, ( iZ - numStackRows / 2 ) * stackZSpacing );
createStack( center, boxShape, halfExtents, stackHeight );
}
}
}
if ( false )
{
// destroyer ball
btTransform sphereTrans;
sphereTrans.setIdentity();
sphereTrans.setOrigin( btVector3( 0, 2, 40 ) );
btSphereShape* ball = new btSphereShape( 2.f );
m_collisionShapes.push_back( ball );
btRigidBody* ballBody = localCreateRigidBody( 10000.f, sphereTrans, ball );
ballBody->setLinearVelocity( btVector3( 0, 0, -10 ) );
}
m_guiHelper->autogenerateGraphicsObjects( m_dynamicsWorld );
}
CommonExampleInterface* MultiThreadedDemoCreateFunc( struct CommonExampleOptions& options )
{
return new MultiThreadedDemo(options.m_guiHelper);
}

View File

@@ -0,0 +1,22 @@
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
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 MULTITHREADED_DEMO_H
#define MULTITHREADED_DEMO_H
class CommonExampleInterface* MultiThreadedDemoCreateFunc(struct CommonExampleOptions& options);
#endif // MULTITHREADED_DEMO_H