From 1b457b45e22aec9f39d05a3fe9592f15f8fa8d34 Mon Sep 17 00:00:00 2001 From: "erwin.coumans" Date: Tue, 4 Aug 2009 00:03:14 +0000 Subject: [PATCH] Minor fixes in multi-threaded narrowphase Bullet version: allow to dynamically switch number of threads/SPUs Disable USE_SEPDISTANCE_UTIL for now --- .../SpuCollisionTaskProcess.cpp | 35 ++++++++++++------- .../SpuCollisionTaskProcess.h | 8 +++++ .../SpuContactManifoldCollisionAlgorithm.h | 2 +- .../SpuGatheringCollisionDispatcher.cpp | 6 ++++ src/BulletMultiThreaded/Win32ThreadSupport.h | 7 ++++ .../btThreadSupportInterface.h | 2 ++ 6 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/BulletMultiThreaded/SpuCollisionTaskProcess.cpp b/src/BulletMultiThreaded/SpuCollisionTaskProcess.cpp index 6b0c11d6b..8d77240ba 100644 --- a/src/BulletMultiThreaded/SpuCollisionTaskProcess.cpp +++ b/src/BulletMultiThreaded/SpuCollisionTaskProcess.cpp @@ -24,6 +24,28 @@ subject to the following restrictions: +void SpuCollisionTaskProcess::setNumTasks(int maxNumTasks) +{ + m_maxNumOutstandingTasks = maxNumTasks; + m_taskBusy.resize(m_maxNumOutstandingTasks); + m_spuGatherTaskDesc.resize(m_maxNumOutstandingTasks); + + for (int i = 0; i < m_taskBusy.size(); i++) + { + m_taskBusy[i] = false; + } + + ///re-allocate task memory buffers + if (m_workUnitTaskBuffers != 0) + { + btAlignedFree(m_workUnitTaskBuffers); + } + + m_workUnitTaskBuffers = (unsigned char *)btAlignedAlloc(MIDPHASE_WORKUNIT_TASK_SIZE*m_maxNumOutstandingTasks, 128); + m_workUnitTaskBuffers = (unsigned char *)btAlignedAlloc(MIDPHASE_WORKUNIT_TASK_SIZE*6, 128); + +} + SpuCollisionTaskProcess::SpuCollisionTaskProcess(class btThreadSupportInterface* threadInterface, unsigned int maxNumOutstandingTasks) @@ -31,13 +53,7 @@ SpuCollisionTaskProcess::SpuCollisionTaskProcess(class btThreadSupportInterface* m_maxNumOutstandingTasks(maxNumOutstandingTasks) { m_workUnitTaskBuffers = (unsigned char *)0; - m_taskBusy.resize(m_maxNumOutstandingTasks); - m_spuGatherTaskDesc.resize(m_maxNumOutstandingTasks); - - for (int i = 0; i < m_maxNumOutstandingTasks; i++) - { - m_taskBusy[i] = false; - } + setNumTasks(maxNumOutstandingTasks); m_numBusyTasks = 0; m_currentTask = 0; m_currentPage = 0; @@ -77,11 +93,6 @@ void SpuCollisionTaskProcess::initialize2(bool useEpa) #ifdef DEBUG_SPU_TASK_SCHEDULING printf("SpuCollisionTaskProcess::initialize()\n"); #endif //DEBUG_SPU_TASK_SCHEDULING - if (!m_workUnitTaskBuffers) - { - m_workUnitTaskBuffers = (unsigned char *)btAlignedAlloc(MIDPHASE_WORKUNIT_TASK_SIZE*m_maxNumOutstandingTasks, 128); - } - for (int i = 0; i < m_maxNumOutstandingTasks; i++) { diff --git a/src/BulletMultiThreaded/SpuCollisionTaskProcess.h b/src/BulletMultiThreaded/SpuCollisionTaskProcess.h index 30bdc5780..a39f4c778 100644 --- a/src/BulletMultiThreaded/SpuCollisionTaskProcess.h +++ b/src/BulletMultiThreaded/SpuCollisionTaskProcess.h @@ -132,6 +132,14 @@ public: ///call flush to submit potential outstanding work to SPUs and wait for all involved SPUs to be finished void flush2(); + + /// set the maximum number of SPU tasks allocated + void setNumTasks(int maxNumTasks); + + int getNumTasks() const + { + return m_maxNumOutstandingTasks; + } }; diff --git a/src/BulletMultiThreaded/SpuContactManifoldCollisionAlgorithm.h b/src/BulletMultiThreaded/SpuContactManifoldCollisionAlgorithm.h index 5ce285c8f..151cb2c79 100644 --- a/src/BulletMultiThreaded/SpuContactManifoldCollisionAlgorithm.h +++ b/src/BulletMultiThreaded/SpuContactManifoldCollisionAlgorithm.h @@ -24,7 +24,7 @@ subject to the following restrictions: class btPersistentManifold; -#define USE_SEPDISTANCE_UTIL 1 +//#define USE_SEPDISTANCE_UTIL 1 /// SpuContactManifoldCollisionAlgorithm provides contact manifold and should be processed on SPU. ATTRIBUTE_ALIGNED16(class) SpuContactManifoldCollisionAlgorithm : public btCollisionAlgorithm diff --git a/src/BulletMultiThreaded/SpuGatheringCollisionDispatcher.cpp b/src/BulletMultiThreaded/SpuGatheringCollisionDispatcher.cpp index 9d9186e28..4f1e6d1ec 100644 --- a/src/BulletMultiThreaded/SpuGatheringCollisionDispatcher.cpp +++ b/src/BulletMultiThreaded/SpuGatheringCollisionDispatcher.cpp @@ -150,10 +150,16 @@ void SpuGatheringCollisionDispatcher::dispatchAllCollisionPairs(btOverlappingPai if (dispatchInfo.m_enableSPU) { + m_maxNumOutstandingTasks = m_threadInterface->getNumTasks(); + if (!m_spuCollisionTaskProcess) m_spuCollisionTaskProcess = new SpuCollisionTaskProcess(m_threadInterface,m_maxNumOutstandingTasks); + m_spuCollisionTaskProcess->setNumTasks(m_maxNumOutstandingTasks); +// printf("m_maxNumOutstandingTasks =%d\n",m_maxNumOutstandingTasks); + m_spuCollisionTaskProcess->initialize2(dispatchInfo.m_useEpa); + ///modified version of btCollisionDispatcher::dispatchAllCollisionPairs: { diff --git a/src/BulletMultiThreaded/Win32ThreadSupport.h b/src/BulletMultiThreaded/Win32ThreadSupport.h index 182bf82f1..c61ad901c 100644 --- a/src/BulletMultiThreaded/Win32ThreadSupport.h +++ b/src/BulletMultiThreaded/Win32ThreadSupport.h @@ -64,6 +64,7 @@ private: btAlignedObjectArray m_activeSpuStatus; btAlignedObjectArray m_completeHandles; + int m_maxNumTasks; public: ///Setup and initialize SPU/CELL/Libspe2 @@ -116,6 +117,12 @@ public: virtual void setNumTasks(int numTasks) { + m_maxNumTasks = numTasks; + } + + virtual int getNumTasks() const + { + return m_maxNumTasks; } }; diff --git a/src/BulletMultiThreaded/btThreadSupportInterface.h b/src/BulletMultiThreaded/btThreadSupportInterface.h index 0bb775e0c..730ffa9ea 100644 --- a/src/BulletMultiThreaded/btThreadSupportInterface.h +++ b/src/BulletMultiThreaded/btThreadSupportInterface.h @@ -42,6 +42,8 @@ public: ///tell the task scheduler to use no more than numTasks tasks virtual void setNumTasks(int numTasks)=0; + virtual int getNumTasks() const = 0; + }; #endif //THREAD_SUPPORT_INTERFACE_H