Move some code from Branches/OpenCL to trunk, related to threading and OpenCL:

Added Demos/ThreadingDemo showing how to use the cross-platform btThreadSupportInterface under Windows.
Added Demos/ParticlesOpenCL showing how to run the NVidia particle demo under OpenCL implementations by AMD, NVidia and MiniCL (CPU)
This commit is contained in:
erwin.coumans
2010-06-24 22:54:00 +00:00
parent b2798eaae5
commit 498da0721b
36 changed files with 4020 additions and 38 deletions

View File

@@ -176,6 +176,53 @@ void Win32ThreadSupport::waitForResponse(unsigned int *puiArgument0, unsigned in
}
///check for messages from SPUs
bool Win32ThreadSupport::isTaskCompleted(unsigned int *puiArgument0, unsigned int *puiArgument1, int timeOutInMilliseconds)
{
///We should wait for (one of) the first tasks to finish (or other SPU messages), and report its response
///A possible response can be 'yes, SPU handled it', or 'no, please do a PPU fallback'
btAssert(m_activeSpuStatus.size());
int last = -1;
#ifndef SINGLE_THREADED
DWORD res = WaitForMultipleObjects(m_completeHandles.size(), &m_completeHandles[0], FALSE, timeOutInMilliseconds);
if ((res != STATUS_TIMEOUT) && (res != WAIT_FAILED))
{
btAssert(res != WAIT_FAILED);
last = res - WAIT_OBJECT_0;
btSpuStatus& spuStatus = m_activeSpuStatus[last];
btAssert(spuStatus.m_threadHandle);
btAssert(spuStatus.m_eventCompletetHandle);
//WaitForSingleObject(spuStatus.m_eventCompletetHandle, INFINITE);
btAssert(spuStatus.m_status > 1);
spuStatus.m_status = 0;
///need to find an active spu
btAssert(last>=0);
#else
last=0;
btSpuStatus& spuStatus = m_activeSpuStatus[last];
#endif //SINGLE_THREADED
*puiArgument0 = spuStatus.m_taskId;
*puiArgument1 = spuStatus.m_status;
return true;
}
return false;
}
void Win32ThreadSupport::startThreads(const Win32ThreadConstructionInfo& threadConstructionInfo)
{
@@ -259,4 +306,141 @@ void Win32ThreadSupport::stopSPU()
}
class btWin32Barrier : public btBarrier
{
private:
CRITICAL_SECTION mExternalCriticalSection;
CRITICAL_SECTION mLocalCriticalSection;
HANDLE mRunEvent,mNotifyEvent;
int mCounter,mEnableCounter;
int mMaxCount;
public:
btWin32Barrier()
{
mCounter = 0;
mMaxCount = 1;
mEnableCounter = 0;
InitializeCriticalSection(&mExternalCriticalSection);
InitializeCriticalSection(&mLocalCriticalSection);
mRunEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
mNotifyEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
}
virtual ~btWin32Barrier()
{
DeleteCriticalSection(&mExternalCriticalSection);
DeleteCriticalSection(&mLocalCriticalSection);
CloseHandle(mRunEvent);
CloseHandle(mNotifyEvent);
}
void sync()
{
int eventId;
EnterCriticalSection(&mExternalCriticalSection);
//PFX_PRINTF("enter taskId %d count %d stage %d phase %d mEnableCounter %d\n",taskId,mCounter,debug&0xff,debug>>16,mEnableCounter);
if(mEnableCounter > 0) {
ResetEvent(mNotifyEvent);
LeaveCriticalSection(&mExternalCriticalSection);
WaitForSingleObject(mNotifyEvent,INFINITE);
EnterCriticalSection(&mExternalCriticalSection);
}
eventId = mCounter;
mCounter++;
if(eventId == mMaxCount-1) {
SetEvent(mRunEvent);
mEnableCounter = mCounter-1;
mCounter = 0;
}
else {
ResetEvent(mRunEvent);
LeaveCriticalSection(&mExternalCriticalSection);
WaitForSingleObject(mRunEvent,INFINITE);
EnterCriticalSection(&mExternalCriticalSection);
mEnableCounter--;
}
if(mEnableCounter == 0) {
SetEvent(mNotifyEvent);
}
//PFX_PRINTF("leave taskId %d count %d stage %d phase %d mEnableCounter %d\n",taskId,mCounter,debug&0xff,debug>>16,mEnableCounter);
LeaveCriticalSection(&mExternalCriticalSection);
}
virtual void setMaxCount(int n) {mMaxCount = n;}
virtual int getMaxCount() {return mMaxCount;}
};
class btWin32CriticalSection : public btCriticalSection
{
private:
CRITICAL_SECTION mCriticalSection;
public:
btWin32CriticalSection()
{
InitializeCriticalSection(&mCriticalSection);
}
~btWin32CriticalSection()
{
DeleteCriticalSection(&mCriticalSection);
}
unsigned int getSharedParam(int i)
{
btAssert(i>=0&&i<31);
return mCommonBuff[i+1];
}
void setSharedParam(int i,unsigned int p)
{
btAssert(i>=0&&i<31);
mCommonBuff[i+1] = p;
}
void lock()
{
EnterCriticalSection(&mCriticalSection);
mCommonBuff[0] = 1;
}
void unlock()
{
mCommonBuff[0] = 0;
LeaveCriticalSection(&mCriticalSection);
}
};
btBarrier* Win32ThreadSupport::createBarrier()
{
unsigned char* mem = (unsigned char*)btAlignedAlloc(sizeof(btWin32Barrier),16);
btWin32Barrier* barrier = new(mem) btWin32Barrier();
barrier->setMaxCount(getNumTasks());
return barrier;
}
btCriticalSection* Win32ThreadSupport::createCriticalSection()
{
unsigned char* mem = (unsigned char*) btAlignedAlloc(sizeof(btWin32CriticalSection),16);
btWin32CriticalSection* cs = new(mem) btWin32CriticalSection();
return cs;
}
#endif //USE_WIN32_THREADING

View File

@@ -30,10 +30,6 @@ typedef void (*Win32ThreadFunc)(void* userPtr,void* lsMemory);
typedef void* (*Win32lsMemorySetupFunc)();
///Win32ThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
class Win32ThreadSupport : public btThreadSupportInterface
{
@@ -109,6 +105,8 @@ public:
///check for messages from SPUs
virtual void waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1);
virtual bool isTaskCompleted(unsigned int *puiArgument0, unsigned int *puiArgument1, int timeOutInMilliseconds);
///start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
virtual void startSPU();
@@ -125,6 +123,14 @@ public:
return m_maxNumTasks;
}
virtual void* getThreadLocalMemory(int taskId)
{
return m_activeSpuStatus[taskId].m_lsMemory;
}
virtual btBarrier* createBarrier();
virtual btCriticalSection* createCriticalSection();
};
#endif //WIN32_THREAD_SUPPORT_H

View File

@@ -17,10 +17,35 @@ subject to the following restrictions:
#define THREAD_SUPPORT_INTERFACE_H
//#include <LinearMath/btScalar.h> //for uint32_t etc.
#include <LinearMath/btScalar.h> //for ATTRIBUTE_ALIGNED16
#include "PlatformDefinitions.h"
#include "PpuAddressSpace.h"
class btBarrier {
public:
btBarrier() {}
virtual ~btBarrier() {}
virtual void sync() = 0;
virtual void setMaxCount(int n) = 0;
virtual int getMaxCount() = 0;
};
class btCriticalSection {
public:
btCriticalSection() {}
virtual ~btCriticalSection() {}
ATTRIBUTE_ALIGNED16(unsigned int mCommonBuff[32]);
virtual unsigned int getSharedParam(int i) = 0;
virtual void setSharedParam(int i,unsigned int p) = 0;
virtual void lock() = 0;
virtual void unlock() = 0;
};
class btThreadSupportInterface
{
public:
@@ -33,6 +58,10 @@ public:
///check for messages from SPUs
virtual void waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1) =0;
///non-blocking test if a task is completed. First implement all versions, and then enable this API
///virtual bool isTaskCompleted(unsigned int *puiArgument0, unsigned int *puiArgument1, int timeOutInMilliseconds)=0;
///start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
virtual void startSPU() =0;
@@ -44,6 +73,10 @@ public:
virtual int getNumTasks() const = 0;
virtual btBarrier* createBarrier() = 0;
virtual btCriticalSection* createCriticalSection() = 0;
};
#endif //THREAD_SUPPORT_INTERFACE_H