SpuLibspe2Support.h

Go to the documentation of this file.
00001 /*
00002 Bullet Continuous Collision Detection and Physics Library
00003 Copyright (c) 2003-2007 Erwin Coumans  http://bulletphysics.com
00004 
00005 This software is provided 'as-is', without any express or implied warranty.
00006 In no event will the authors be held liable for any damages arising from the use of this software.
00007 Permission is granted to anyone to use this software for any purpose, 
00008 including commercial applications, and to alter it and redistribute it freely, 
00009 subject to the following restrictions:
00010 
00011 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.
00012 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00013 3. This notice may not be removed or altered from any source distribution.
00014 */
00015 
00016 
00017 #ifndef BT_SPU_LIBSPE2_SUPPORT_H
00018 #define BT_SPU_LIBSPE2_SUPPORT_H
00019 
00020 #include <LinearMath/btScalar.h> //for uint32_t etc.
00021 
00022 #ifdef USE_LIBSPE2
00023 
00024 #include <stdlib.h>
00025 #include <stdio.h>
00026 //#include "SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h"
00027 #include "PlatformDefinitions.h"
00028 
00029 
00030 //extern struct SpuGatherAndProcessPairsTaskDesc;
00031 
00032 enum
00033 {
00034         Spu_Mailbox_Event_Nothing = 0,
00035         Spu_Mailbox_Event_Task = 1,
00036         Spu_Mailbox_Event_Shutdown = 2,
00037         
00038         Spu_Mailbox_Event_ForceDword = 0xFFFFFFFF
00039         
00040 };
00041 
00042 enum
00043 {
00044         Spu_Status_Free = 0,
00045         Spu_Status_Occupied = 1,
00046         Spu_Status_Startup = 2,
00047         
00048         Spu_Status_ForceDword = 0xFFFFFFFF
00049         
00050 };
00051 
00052 
00053 struct btSpuStatus
00054 {
00055         uint32_t        m_taskId;
00056         uint32_t        m_commandId;
00057         uint32_t        m_status;
00058 
00059         addr64 m_taskDesc;
00060         addr64 m_lsMemory;
00061         
00062 }
00063 __attribute__ ((aligned (128)))
00064 ;
00065 
00066 
00067 
00068 #ifndef __SPU__
00069 
00070 #include "LinearMath/btAlignedObjectArray.h"
00071 #include "SpuCollisionTaskProcess.h"
00072 #include "SpuSampleTaskProcess.h"
00073 #include "btThreadSupportInterface.h"
00074 #include <libspe2.h>
00075 #include <pthread.h>
00076 #include <sched.h>
00077 
00078 #define MAX_SPUS 4 
00079 
00080 typedef struct ppu_pthread_data 
00081 {
00082         spe_context_ptr_t context;
00083         pthread_t pthread;
00084         unsigned int entry;
00085         unsigned int flags;
00086         addr64 argp;
00087         addr64 envp;
00088         spe_stop_info_t stopinfo;
00089 } ppu_pthread_data_t;
00090 
00091 
00092 static void *ppu_pthread_function(void *arg)
00093 {
00094     ppu_pthread_data_t * datap = (ppu_pthread_data_t *)arg;
00095     /*
00096     int rc;
00097     do 
00098     {*/
00099         spe_context_run(datap->context, &datap->entry, datap->flags, datap->argp.p, datap->envp.p, &datap->stopinfo);
00100         if (datap->stopinfo.stop_reason == SPE_EXIT) 
00101         {
00102            if (datap->stopinfo.result.spe_exit_code != 0) 
00103            {
00104              perror("FAILED: SPE returned a non-zero exit status: \n");
00105              exit(1);
00106            }
00107          } 
00108         else 
00109          {
00110            perror("FAILED: SPE abnormally terminated\n");
00111            exit(1);
00112          }
00113         
00114         
00115     //} while (rc > 0); // loop until exit or error, and while any stop & signal
00116     pthread_exit(NULL);
00117 }
00118 
00119 
00120 
00121 
00122 
00123 
00125 class SpuLibspe2Support : public btThreadSupportInterface
00126 {
00127 
00128         btAlignedObjectArray<btSpuStatus>       m_activeSpuStatus;
00129         
00130 public:
00131         //Setup and initialize SPU/CELL/Libspe2
00132         SpuLibspe2Support(spe_program_handle_t *speprog,int numThreads);
00133         
00134         // SPE program handle ptr.
00135         spe_program_handle_t *program;
00136         
00137         // SPE program data
00138         ppu_pthread_data_t data[MAX_SPUS];
00139         
00140         //cleanup/shutdown Libspe2
00141         ~SpuLibspe2Support();
00142 
00144         void sendRequest(uint32_t uiCommand, uint32_t uiArgument0, uint32_t uiArgument1=0);
00145 
00146         //check for messages from SPUs
00147         void waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1);
00148 
00149         //start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
00150         virtual void startSPU();
00151 
00152         //tell the task scheduler we are done with the SPU tasks
00153         virtual void stopSPU();
00154 
00155         virtual void setNumTasks(int numTasks)
00156         {
00157                 //changing the number of tasks after initialization is not implemented (yet)
00158         }
00159 
00160 private:
00161         
00163         void internal_startSPU();
00164 
00165 
00166         
00167         
00168         int numThreads;
00169 
00170 };
00171 
00172 #endif // NOT __SPU__
00173 
00174 #endif //USE_LIBSPE2
00175 
00176 #endif //BT_SPU_LIBSPE2_SUPPORT_H
00177 
00178 
00179 
00180