added Bullet's Multithreading Support - Win32 only till now

Wuempftlbruempftl

10-06-2010 17:28:05

Hi,
I implemented MultiThreading like it is made in Bullet's MultiThreadedDemo to OgreBullet. Unfortunately this doesn't use Ogre's threading capabilites but creates some new Win32 Threads - yes by that it's only available for Win32-Systems so far. As far as I understood it would be possible to create a new "btThreadSupportInterface" with a connection to Ogre's threading system. But this seems quite difficult for me.

I did some tests with MultiThreading on and off but I didn't get clear results. In fact it seems that the work is more distributed to the CPU Cores (I've got a 4 core 4 threads machine) but there's no clear cut performance boost. But I think this is because of my to graphics-card bound scene (I let ~3000 cubes fall on a terrain). Maybe I will do more expressive tests soon.

Here is the patch-file- the changes are of very convenient size:
Index: Collisions/include/OgreBulletCollisionsWorld.h
===================================================================
--- Collisions/include/OgreBulletCollisionsWorld.h (revision 2911)
+++ Collisions/include/OgreBulletCollisionsWorld.h (working copy)
@@ -29,6 +29,7 @@

#include "OgreBulletCollisionsPreRequisites.h"

+class btThreadSupportInterface;

namespace OgreBulletCollisions
{
@@ -37,7 +38,10 @@
class CollisionsWorld
{
public:
- CollisionsWorld(Ogre::SceneManager *scn, const Ogre::AxisAlignedBox &bounds, bool init = true, bool set32bitsAxisSweep = true);
+ /// @param Multithreading So far only available for Win32!
+ /// @param MaxParallelTasks If Multithreading activated, the maximum number of performed parallel tasks
+ CollisionsWorld(Ogre::SceneManager *scn, const Ogre::AxisAlignedBox &bounds, bool init = true, bool set32bitsAxisSweep = true,
+ const bool Multithreading = false, const unsigned int MaxParallelTasks = 4);
virtual ~CollisionsWorld();

void addObject(Object *obj, int filterGrp = 1, short int collisionFilter = -1);
@@ -77,6 +81,10 @@
btCollisionWorld* mWorld;
btCollisionDispatcher* mDispatcher;

+#ifdef _WIN32
+ btThreadSupportInterface* mThreadSupportCollision;
+#endif
+
btBroadphaseInterface* mBroadphase;

Ogre::AxisAlignedBox mBounds;
Index: Collisions/src/OgreBulletCollisionsWorld.cpp
===================================================================
--- Collisions/src/OgreBulletCollisionsWorld.cpp (revision 2911)
+++ Collisions/src/OgreBulletCollisionsWorld.cpp (working copy)
@@ -36,6 +36,11 @@
#include "OgreBulletCollisionsRay.h"


+#ifdef _WIN32
+#include "BulletMultiThreaded/Win32ThreadSupport.h"
+#include "BulletMultiThreaded/SpuGatheringCollisionDispatcher.h"
+#include "BulletMultiThreaded/SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h"
+#endif

using namespace Ogre;
using namespace OgreBulletCollisions;
@@ -43,7 +48,9 @@
namespace OgreBulletCollisions
{
// -------------------------------------------------------------------------
- CollisionsWorld::CollisionsWorld(SceneManager *scn, const AxisAlignedBox &bounds, bool init, bool set32bitsAxisSweep):
+ CollisionsWorld::CollisionsWorld(SceneManager *scn, const AxisAlignedBox &bounds, bool init, bool set32bitsAxisSweep,
+ const bool Multithreading, const unsigned int MaxParallelTasks) :
+mThreadSupportCollision(0),
mScnMgr(scn),
mBounds(bounds),
mShowDebugShapes(false),
@@ -51,8 +58,22 @@
mDebugContactPoints(0),
mDebugDrawer(0)
{
- mDispatcher = new btCollisionDispatcher(&mDefaultCollisionConfiguration);
+#ifdef _WIN32
+ if(!Multithreading)
+#endif
+ mDispatcher = new btCollisionDispatcher(&mDefaultCollisionConfiguration);
+#ifdef _WIN32
+ else
+ {
+ mThreadSupportCollision = new Win32ThreadSupport(Win32ThreadSupport::Win32ThreadConstructionInfo("OgreBulletCollision",
+ processCollisionTask,
+ createCollisionLocalStoreMemory,
+ MaxParallelTasks));

+ mDispatcher = new SpuGatheringCollisionDispatcher(mThreadSupportCollision, MaxParallelTasks, &mDefaultCollisionConfiguration);
+ }
+#endif
+
if (set32bitsAxisSweep)
{
mBroadphase = new bt32BitAxisSweep3(
@@ -93,6 +114,7 @@
delete mWorld;
delete mBroadphase;
delete mDispatcher;
+ delete mThreadSupportCollision;
}
// -------------------------------------------------------------------------
void CollisionsWorld::setShowDebugContactPoints(bool show)
Index: Dynamics/include/OgreBulletDynamicsWorld.h
===================================================================
--- Dynamics/include/OgreBulletDynamicsWorld.h (revision 2911)
+++ Dynamics/include/OgreBulletDynamicsWorld.h (working copy)
@@ -32,6 +32,8 @@
#include "OgreBulletCollisionsWorld.h"
#include "Debug/OgreBulletCollisionsDebugDrawer.h"

+class btThreadSupportInterface;
+
namespace OgreBulletDynamics
{
// -------------------------------------------------------------------------
@@ -39,10 +41,14 @@
class DynamicsWorld : public OgreBulletCollisions::CollisionsWorld
{
public:
+ /// @param Multithreading So far only available for Win32!
+ /// @param MaxParallelTasks If Multithreading activated, the maximum number of performed parallel tasks
DynamicsWorld(Ogre::SceneManager *mgr,
const Ogre::AxisAlignedBox &bounds,
const Ogre::Vector3 &gravity,
- bool init = true);
+ bool init = true,
+ const bool Multithreading = false,
+ const unsigned int MaxParallelTasks = 4);

~DynamicsWorld();

@@ -67,10 +73,10 @@
void addVehicle(RaycastVehicle *v);

private:
- btConstraintSolver *mConstraintsolver;
+ btConstraintSolver *mConstraintsolver;

- std::deque <TypedConstraint *> mConstraints;
- std::deque <ActionInterface *> mActionInterface;
+ std::deque <TypedConstraint *> mConstraints;
+ std::deque <ActionInterface *> mActionInterface;
};
}
#endif //_OGREBULLETDYNAMICS_DynamicWorld_H
Index: Dynamics/src/OgreBulletDynamicsWorld.cpp
===================================================================
--- Dynamics/src/OgreBulletDynamicsWorld.cpp (revision 2911)
+++ Dynamics/src/OgreBulletDynamicsWorld.cpp (working copy)
@@ -45,10 +45,12 @@
{

DynamicsWorld::DynamicsWorld(Ogre::SceneManager *mgr,
- const Ogre::AxisAlignedBox &bounds,
- const Ogre::Vector3 &gravity,
- bool init) :
- CollisionsWorld(mgr, bounds, false)
+ const Ogre::AxisAlignedBox &bounds,
+ const Ogre::Vector3 &gravity,
+ bool init,
+ const bool Multithreading,
+ const unsigned int MaxParallelTasks) :
+ CollisionsWorld(mgr, bounds, false, true, Multithreading, MaxParallelTasks)
{
//btSequentialImpulseConstraintSolver
//btSequentialImpulseConstraintSolver3


Maybe it is helpful for somebody.

tuan kuranes

11-06-2010 09:17:15

please post on ogreaddons sourceforge patch tracker http://sourceforge.net/tracker/?group_i ... id=1064241

Wuempftlbruempftl

11-06-2010 09:27:37

Okay, done
I didn't this before, because I thought it might be not appropriate for such a change.

tuan kuranes

11-06-2010 09:40:51

Thanks a lot.