Added libspe2 support contribution by IBM Germany 'Extreme Blue' project:

Thanks to Benjamin Hoeferlin, Minh Cuong Tran,Martina Huellmann,Frederick Roth.
This commit is contained in:
ejcoumans
2007-09-26 23:33:56 +00:00
parent 7dbc60d931
commit b2b2ea71c6
2 changed files with 430 additions and 347 deletions

View File

@@ -1,216 +1,257 @@
/* /*
Bullet Continuous Collision Detection and Physics Library Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
This software is provided 'as-is', without any express or implied warranty. 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. 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, Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely, including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions: 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. 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. 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. 3. This notice may not be removed or altered from any source distribution.
*/ */
#ifdef USE_LIBSPE2
#ifdef USE_LIBSPE2
#include "SpuLibspe2Support.h"
#include "SpuLibspe2Support.h"
#include "SpuCollisionTaskProcess.h"
#include "SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h"
#include <Windows.h>
//SpuLibspe2Support helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
///SpuLibspe2Support helps to initialize/shutdown libspe2, start/stop SPU tasks and communication ///Setup and initialize SPU/CELL/Libspe2
///Setup and initialize SPU/CELL/Libspe2 SpuLibspe2Support::SpuLibspe2Support(spe_program_handle_t *speprog, int numThreads)
SpuLibspe2Support::SpuLibspe2Support(spe_program_handle_t *speprog,int numThreads) {
{ this->program = speprog;
program = speprog this->numThreads = ((numThreads <= spe_cpu_info_get(SPE_COUNT_PHYSICAL_SPES, -1)) ? numThreads : spe_cpu_info_get(SPE_COUNT_PHYSICAL_SPES, -1));
startSPUs(numThreads); }
N = numThreads;
} ///cleanup/shutdown Libspe2
SpuLibspe2Support::~SpuLibspe2Support()
///cleanup/shutdown Libspe2 {
SpuLibspe2Support::~SpuLibspe2Support()
{ stopSPU();
stopSPUs(); }
}
///send messages to SPUs
#include <stdio.h> void SpuLibspe2Support::sendRequest(uint32_t uiCommand, uint32_t uiArgument0, uint32_t uiArgument1)
{
#ifdef WIN32 spe_context_ptr_t context;
DWORD WINAPI Thread_no_1( LPVOID lpParam )
{ switch (uiCommand)
{
btSpuStatus* status = (btSpuStatus*)lpParam; case CMD_SAMPLE_TASK_COMMAND:
{
//get taskdescription
while (1) SpuSampleTaskDesc* taskDesc = (SpuSampleTaskDesc*) uiArgument0;
{
WaitForSingleObject(status->m_eventStartHandle,INFINITE); btAssert(taskDesc->m_taskId<m_activeSpuStatus.size());
btAssert(status->m_status);
//get status of SPU on which task should run
SpuGatherAndProcessPairsTaskDesc* taskDesc = status->m_taskDesc; btSpuStatus& spuStatus = m_activeSpuStatus[taskDesc->m_taskId];
if (taskDesc) //set data for spuStatus
{ spuStatus.m_commandId = uiCommand;
processCollisionTask(*taskDesc); spuStatus.m_status = Spu_Status_Occupied; //set SPU as "occupied"
SetEvent(status->m_eventCompletetHandle); spuStatus.m_taskDesc.p = taskDesc;
} else
{ //get context
//exit Thread context = data[taskDesc->m_taskId].context;
break;
}
taskDesc->m_mainMemoryPtr = reinterpret_cast<uint64_t> (spuStatus.m_lsMemory.p);
}
return 0; break;
}
} case CMD_GATHER_AND_PROCESS_PAIRLIST:
#endif {
///send messages to SPUs //get taskdescription
void SpuLibspe2Support::sendRequest(uint32_t uiCommand, uint32_t uiArgument0, uint32_t uiArgument1) SpuGatherAndProcessPairsTaskDesc* taskDesc = (SpuGatherAndProcessPairsTaskDesc*) uiArgument0;
{
/// gMidphaseSPU.sendRequest(CMD_GATHER_AND_PROCESS_PAIRLIST, (uint32_t) &taskDesc); btAssert(taskDesc->taskId<m_activeSpuStatus.size());
///we should spawn an SPU task here, and in 'waitForResponse' it should wait for response of the (one of) the first tasks that finished //get status of SPU on which task should run
btSpuStatus& spuStatus = m_activeSpuStatus[taskDesc->taskId];
//set data for spuStatus
switch (uiCommand) spuStatus.m_commandId = uiCommand;
{ spuStatus.m_status = Spu_Status_Occupied; //set SPU as "occupied"
case CMD_GATHER_AND_PROCESS_PAIRLIST: spuStatus.m_taskDesc.p = taskDesc;
{
//get context
SpuGatherAndProcessPairsTaskDesc* taskDesc = (SpuGatherAndProcessPairsTaskDesc*) uiArgument0 ; context = data[taskDesc->taskId].context;
//#define SINGLE_THREADED 1
#ifdef SINGLE_THREADED taskDesc->m_lsMemory = (CollisionTask_LocalStoreMemory*)spuStatus.m_lsMemory.p;
btSpuStatus& spuStatus = m_activeSpuStatus[0]; break;
taskDesc->m_lsMemory = (CollisionTask_LocalStoreMemory*)spuStatus.m_lsMemory; }
processCollisionTask(*taskDesc); default:
HANDLE handle =0; {
#else ///not implemented
btAssert(0);
btAssert(taskDesc->taskId>=0); }
btAssert(taskDesc->taskId<m_activeSpuStatus.size());
};
btSpuStatus& spuStatus = m_activeSpuStatus[taskDesc->taskId];
spuStatus.m_commandId = uiCommand; //write taskdescription in mailbox
spuStatus.m_status = 1; unsigned int event = Spu_Mailbox_Event_Task;
spuStatus.m_taskDesc = taskDesc; spe_in_mbox_write(context, &event, 1, SPE_MBOX_ANY_NONBLOCKING);
taskDesc->m_lsMemory = (CollisionTask_LocalStoreMemory*)spuStatus.m_lsMemory;
///fire event to start new task }
SetEvent(spuStatus.m_eventStartHandle);
///check for messages from SPUs
#endif //CollisionTask_LocalStoreMemory void SpuLibspe2Support::waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1)
{
///We should wait for (one of) the first tasks to finish (or other SPU messages), and report its response
break; ///A possible response can be 'yes, SPU handled it', or 'no, please do a PPU fallback'
}
default: btAssert(m_activeSpuStatus.size());
{
///not implemented
btAssert(0); int last = -1;
}
//find an active spu/thread
}; while(last < 0)
{
for (int i=0;i<m_activeSpuStatus.size();i++)
} {
if ( m_activeSpuStatus[i].m_status == Spu_Status_Free)
///check for messages from SPUs {
void SpuLibspe2Support::waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1) last = i;
{ break;
///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' if(last < 0)
sched_yield();
}
btAssert(m_activeSpuStatus.size());
int last = -1;
btSpuStatus& spuStatus = m_activeSpuStatus[last];
//find an active spu/thread
for (int i=0;i<m_activeSpuStatus.size();i++) ///need to find an active spu
{ btAssert(last>=0);
if (m_activeSpuStatus[i].m_status)
{
last = i;
break; *puiArgument0 = spuStatus.m_taskId;
} *puiArgument1 = spuStatus.m_status;
}
}
#ifndef SINGLE_THREADED
btAssert(spuStatus.m_threadHandle);
btAssert(spuStatus.m_eventCompletetHandle); void SpuLibspe2Support::startSPU()
btSpuStatus& spuStatus = m_activeSpuStatus[last]; {
WaitForSingleObject(spuStatus.m_eventCompletetHandle, INFINITE); this->internal_startSPU();
spuStatus.m_status = 0; }
///need to find an active spu
btAssert(last>=0);
///start the spus group (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
#else void SpuLibspe2Support::internal_startSPU()
last=0; {
btSpuStatus& spuStatus = m_activeSpuStatus[last]; m_activeSpuStatus.resize(numThreads);
#endif //SINGLE_THREADED
for (int i=0; i < numThreads; i++)
{
*puiArgument0 = spuStatus.m_taskId;
*puiArgument1 = spuStatus.m_status; if(data[i].context == NULL)
{
} /* Create context */
if ((data[i].context = spe_context_create(0, NULL)) == NULL)
{
///start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded) perror ("Failed creating context");
void SpuLibspe2Support::startSPUs(int numThreads) exit(1);
{ }
#ifdef WIN32
m_activeSpuStatus.resize(numThreads); /* Load program into context */
#endif if(spe_program_load(data[i].context, this->program))
{
for (int i=0;i<numThreads;i++) perror ("Failed loading program");
{ exit(1);
printf("starting thread %d\n",i); }
data[i].context = spe_context_create(0, NULL); m_activeSpuStatus[i].m_status = Spu_Status_Startup;
spe_program_load(data[i].context, program); m_activeSpuStatus[i].m_taskId = i;
data[i].entry = SPE_DEFAULT_ENTRY; m_activeSpuStatus[i].m_commandId = 0;
data[i].flags = 0; m_activeSpuStatus[i].m_lsMemory.p = NULL;
data[i].argp = NULL;
data[i].envp = NULL;
pthread_create(&data[i].pthread, NULL, &ppu_pthread_function, &data[i]); data[i].entry = SPE_DEFAULT_ENTRY;
printf("started thread %d with threadHandle %d\n",i,handle); data[i].flags = 0;
data[i].argp.p = &m_activeSpuStatus[i];
} data[i].envp.p = NULL;
} /* Create thread for each SPE context */
if (pthread_create(&data[i].pthread, NULL, &ppu_pthread_function, &(data[i]) ))
///tell the task scheduler we are done with the SPU tasks {
void SpuLibspe2Support::stopSPUs() perror ("Failed creating thread");
{ exit(1);
// wait for all threads to finish }
for ( i=0; i<N; i++ ) { /*
pthread_join (data[i].pthread, NULL); else
} {
// close SPE program printf("started thread %d\n",i);
spe_image_close(program); }*/
// destroy SPE contexts }
for ( i=0; i<N; i++ ) { }
spe_context_destroy (data[i].context);
}
for (int i=0; i < numThreads; i++)
{
} if(data[i].context != NULL)
{
#endif// USE_LIBSPE2 while( m_activeSpuStatus[i].m_status == Spu_Status_Startup)
{
// wait for spu to set up
sched_yield();
}
printf("Spu %d is ready\n", i);
}
}
}
///tell the task scheduler we are done with the SPU tasks
void SpuLibspe2Support::stopSPU()
{
// wait for all threads to finish
int i;
for ( i = 0; i < this->numThreads; i++ )
{
unsigned int event = Spu_Mailbox_Event_Shutdown;
spe_context_ptr_t context = data[i].context;
spe_in_mbox_write(context, &event, 1, SPE_MBOX_ALL_BLOCKING);
pthread_join (data[i].pthread, NULL);
}
// close SPE program
spe_image_close(program);
// destroy SPE contexts
for ( i = 0; i < this->numThreads; i++ )
{
if(data[i].context != NULL)
{
spe_context_destroy (data[i].context);
}
}
m_activeSpuStatus.clear();
}
#endif //USE_LIBSPE2

View File

@@ -1,131 +1,173 @@
/* /*
Bullet Continuous Collision Detection and Physics Library Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
This software is provided 'as-is', without any express or implied warranty. 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. 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, Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely, including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions: 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. 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. 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. 3. This notice may not be removed or altered from any source distribution.
*/ */
///USE_LIBSPE2: this define should be in the build system, or in <LinearMath/btScalar.h>
//#define USE_LIBSPE2 1 #ifndef SPU_LIBSPE2_SUPPORT_H
#ifdef USE_LIBSPE2 #define SPU_LIBSPE2_SUPPORT_H
#ifndef SPU_LIBSPE2_SUPPORT_H #include <LinearMath/btScalar.h> //for uint32_t etc.
#define SPU_LIBSPE2_SUPPORT_H
#ifdef USE_LIBSPE2
#include "LinearMath/btAlignedObjectArray.h"
#include <libspe2.h> #include <stdlib.h>
#include <pthread.h> #include <stdio.h>
//#include "SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h"
#include "PlatformDefinitions.h"
/**
* Note:
* The order of elements in this enum are important, that's why each one is explicitly //extern struct SpuGatherAndProcessPairsTaskDesc;
* given a value. They will correspond to the .elf names/addresses that will be
* loaded into Libspe2. enum
* Mixing up these values will cause the wrong code to execute, for instance, the {
* solver may be asked to do a collision detection job. Spu_Mailbox_Event_Nothing = 0,
*/ Spu_Mailbox_Event_Task = 1,
#ifdef WIN32 // original enum, but for libspe2 will pass the SPE program handle ptr directly Spu_Mailbox_Event_Shutdown = 2,
typedef enum {
SPU_ELF_COLLISION_DETECTION=0, Spu_Mailbox_Event_ForceDword = 0xFFFFFFFF
SPU_ELF_SAMPLE,
//SPU_ELF_INTEGRATION, };
//SPU_ELF_SOLVER,
SPU_ELF_LAST, enum
} SpuLibspe2ElfId_t; {
#endif Spu_Status_Free = 0,
#ifdef WIN32 Spu_Status_Occupied = 1,
#include <malloc.h> Spu_Status_Startup = 2,
#define memalign(alignment, size) malloc(size);
#else Spu_Status_ForceDword = 0xFFFFFFFF
#include <stdlib.h>
#endif // WIN32 };
#define MAXSPUS 16
typedef struct ppu_pthread_data { struct btSpuStatus
spe_context_ptr_t context; {
pthread_t pthread; uint32_t m_taskId;
unsigned int entry; uint32_t m_commandId;
unsigned int flags; uint32_t m_status;
void *argp;
void *envp; addr64 m_taskDesc;
spe_stop_info_t stopinfo; addr64 m_lsMemory;
} ppu_pthread_data_t;
}
void *ppu_pthread_function(void *arg) __attribute__ ((aligned (128)))
{ ;
ppu_pthread_data_t datap = *(ppu_pthread_data_t *)arg;
int rc;
do {
rc = spe_context_run(datap->context, &datap->entry, datap->flags, datap->argp, datap->envp, &datap->stopinfo); #ifndef __SPU__
} while (rc > 0); // loop until exit or error, and while any stop & signal
pthread_exit(NULL); #include "LinearMath/btAlignedObjectArray.h"
} #include "SpuCollisionTaskProcess.h"
#include "SpuSampleTaskProcess.h"
#include "btThreadSupportInterface.h"
#include <libspe2.h>
#include <LinearMath/btScalar.h> //for uint32_t etc. #include <pthread.h>
#include <sched.h>
///placeholder, until libspe2 support is there
struct btSpuStatus #define MAX_SPUS 4
{
uint32_t m_taskId; typedef struct ppu_pthread_data
uint32_t m_commandId; {
uint32_t m_status; spe_context_ptr_t context;
pthread_t pthread;
struct SpuGatherAndProcessPairsTaskDesc* m_taskDesc; unsigned int entry;
unsigned int flags;
void* m_threadHandle; addr64 argp;
void* m_lsMemory; addr64 envp;
spe_stop_info_t stopinfo;
void* m_eventStartHandle; } ppu_pthread_data_t;
char m_eventStartHandleName[32];
void* m_eventCompletetHandle; static void *ppu_pthread_function(void *arg)
char m_eventCompletetHandleName[32]; {
ppu_pthread_data_t * datap = (ppu_pthread_data_t *)arg;
/*
}; int rc;
do
///SpuLibspe2Support helps to initialize/shutdown libspe2, start/stop SPU tasks and communication {*/
class SpuLibspe2Support { spe_context_run(datap->context, &datap->entry, datap->flags, datap->argp.p, datap->envp.p, &datap->stopinfo);
if (datap->stopinfo.stop_reason == SPE_EXIT)
btAlignedObjectArray<btSpuStatus> m_activeSpuStatus; {
if (datap->stopinfo.result.spe_exit_code != 0)
public: {
///Setup and initialize SPU/CELL/Libspe2 perror("FAILED: SPE returned a non-zero exit status: \n");
SpuLibspe2Support(spe_program_handle_t *speprog,int numThreads); exit(1);
// SPE program handle ptr. }
spe_program_handle_t *program; }
// SPE program data else
ppu_pthread_data_t data[MAX_SPUS]; {
// num SPE Threads perror("FAILED: SPE abnormally terminated\n");
unsigned int N; exit(1);
///cleanup/shutdown Libspe2 }
~SpuLibspe2Support();
///send messages to SPUs //} while (rc > 0); // loop until exit or error, and while any stop & signal
void sendRequest(uint32_t uiCommand, uint32_t uiArgument0, uint32_t uiArgument1=0); pthread_exit(NULL);
}
///check for messages from SPUs
void waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1);
///start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
void startSPUs(int numThreads);
///tell the task scheduler we are done with the SPU tasks ///SpuLibspe2Support helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
void stopSPUs(); class SpuLibspe2Support : public btThreadSupportInterface
{
};
btAlignedObjectArray<btSpuStatus> m_activeSpuStatus;
#endif //SPU_LIBSPE2_SUPPORT_H
public:
#endif //USE_LIBSPE2 //Setup and initialize SPU/CELL/Libspe2
SpuLibspe2Support(spe_program_handle_t *speprog,int numThreads);
// SPE program handle ptr.
spe_program_handle_t *program;
// SPE program data
ppu_pthread_data_t data[MAX_SPUS];
//cleanup/shutdown Libspe2
~SpuLibspe2Support();
///send messages to SPUs
void sendRequest(uint32_t uiCommand, uint32_t uiArgument0, uint32_t uiArgument1=0);
//check for messages from SPUs
void waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1);
//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();
//tell the task scheduler we are done with the SPU tasks
virtual void stopSPU();
private:
///start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
void internal_startSPU();
int numThreads;
};
#endif // NOT __SPU__
#endif //USE_LIBSPE2
#endif //SPU_LIBSPE2_SUPPORT_H