Imported Upstream version 2.81
[platform/upstream/libbullet.git] / src / BulletMultiThreaded / SpuLibspe2Support.h
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2007 Erwin Coumans  http://bulletphysics.com
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose, 
8 including commercial applications, and to alter it and redistribute it freely, 
9 subject to the following restrictions:
10
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16
17 #ifndef BT_SPU_LIBSPE2_SUPPORT_H
18 #define BT_SPU_LIBSPE2_SUPPORT_H
19
20 #include <LinearMath/btScalar.h> //for uint32_t etc.
21
22 #ifdef USE_LIBSPE2
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 //#include "SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h"
27 #include "PlatformDefinitions.h"
28
29
30 //extern struct SpuGatherAndProcessPairsTaskDesc;
31
32 enum
33 {
34         Spu_Mailbox_Event_Nothing = 0,
35         Spu_Mailbox_Event_Task = 1,
36         Spu_Mailbox_Event_Shutdown = 2,
37         
38         Spu_Mailbox_Event_ForceDword = 0xFFFFFFFF
39         
40 };
41
42 enum
43 {
44         Spu_Status_Free = 0,
45         Spu_Status_Occupied = 1,
46         Spu_Status_Startup = 2,
47         
48         Spu_Status_ForceDword = 0xFFFFFFFF
49         
50 };
51
52
53 struct btSpuStatus
54 {
55         uint32_t        m_taskId;
56         uint32_t        m_commandId;
57         uint32_t        m_status;
58
59         addr64 m_taskDesc;
60         addr64 m_lsMemory;
61         
62 }
63 __attribute__ ((aligned (128)))
64 ;
65
66
67
68 #ifndef __SPU__
69
70 #include "LinearMath/btAlignedObjectArray.h"
71 #include "SpuCollisionTaskProcess.h"
72 #include "SpuSampleTaskProcess.h"
73 #include "btThreadSupportInterface.h"
74 #include <libspe2.h>
75 #include <pthread.h>
76 #include <sched.h>
77
78 #define MAX_SPUS 4 
79
80 typedef struct ppu_pthread_data 
81 {
82         spe_context_ptr_t context;
83         pthread_t pthread;
84         unsigned int entry;
85         unsigned int flags;
86         addr64 argp;
87         addr64 envp;
88         spe_stop_info_t stopinfo;
89 } ppu_pthread_data_t;
90
91
92 static void *ppu_pthread_function(void *arg)
93 {
94     ppu_pthread_data_t * datap = (ppu_pthread_data_t *)arg;
95     /*
96     int rc;
97     do 
98     {*/
99         spe_context_run(datap->context, &datap->entry, datap->flags, datap->argp.p, datap->envp.p, &datap->stopinfo);
100         if (datap->stopinfo.stop_reason == SPE_EXIT) 
101         {
102            if (datap->stopinfo.result.spe_exit_code != 0) 
103            {
104              perror("FAILED: SPE returned a non-zero exit status: \n");
105              exit(1);
106            }
107          } 
108         else 
109          {
110            perror("FAILED: SPE abnormally terminated\n");
111            exit(1);
112          }
113         
114         
115     //} while (rc > 0); // loop until exit or error, and while any stop & signal
116     pthread_exit(NULL);
117 }
118
119
120
121
122
123
124 ///SpuLibspe2Support helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
125 class SpuLibspe2Support : public btThreadSupportInterface
126 {
127
128         btAlignedObjectArray<btSpuStatus>       m_activeSpuStatus;
129         
130 public:
131         //Setup and initialize SPU/CELL/Libspe2
132         SpuLibspe2Support(spe_program_handle_t *speprog,int numThreads);
133         
134         // SPE program handle ptr.
135         spe_program_handle_t *program;
136         
137         // SPE program data
138         ppu_pthread_data_t data[MAX_SPUS];
139         
140         //cleanup/shutdown Libspe2
141         ~SpuLibspe2Support();
142
143         ///send messages to SPUs
144         void sendRequest(uint32_t uiCommand, uint32_t uiArgument0, uint32_t uiArgument1=0);
145
146         //check for messages from SPUs
147         void waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1);
148
149         //start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
150         virtual void startSPU();
151
152         //tell the task scheduler we are done with the SPU tasks
153         virtual void stopSPU();
154
155         virtual void setNumTasks(int numTasks)
156         {
157                 //changing the number of tasks after initialization is not implemented (yet)
158         }
159
160 private:
161         
162         ///start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
163         void internal_startSPU();
164
165
166         
167         
168         int numThreads;
169
170 };
171
172 #endif // NOT __SPU__
173
174 #endif //USE_LIBSPE2
175
176 #endif //BT_SPU_LIBSPE2_SUPPORT_H
177
178
179
180