Imported Upstream version 2.81
[platform/upstream/libbullet.git] / src / BulletMultiThreaded / SpuSampleTaskProcess.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 #ifndef BT_SPU_SAMPLE_TASK_PROCESS_H
17 #define BT_SPU_SAMPLE_TASK_PROCESS_H
18
19 #include <assert.h>
20
21
22 #include "PlatformDefinitions.h"
23
24 #include <stdlib.h>
25
26 #include "LinearMath/btAlignedObjectArray.h"
27
28
29 #include "SpuSampleTask/SpuSampleTask.h"
30
31
32 //just add your commands here, try to keep them globally unique for debugging purposes
33 #define CMD_SAMPLE_TASK_COMMAND 10
34
35
36
37 /// SpuSampleTaskProcess handles SPU processing of collision pairs.
38 /// When PPU issues a task, it will look for completed task buffers
39 /// PPU will do postprocessing, dependent on workunit output (not likely)
40 class SpuSampleTaskProcess
41 {
42         // track task buffers that are being used, and total busy tasks
43         btAlignedObjectArray<bool>      m_taskBusy;
44         btAlignedObjectArray<SpuSampleTaskDesc>m_spuSampleTaskDesc;
45         
46         int   m_numBusyTasks;
47
48         // the current task and the current entry to insert a new work unit
49         int   m_currentTask;
50
51         bool m_initialized;
52
53         void postProcess(int taskId, int outputSize);
54         
55         class   btThreadSupportInterface*       m_threadInterface;
56
57         int     m_maxNumOutstandingTasks;
58
59
60
61 public:
62         SpuSampleTaskProcess(btThreadSupportInterface*  threadInterface, int maxNumOutstandingTasks);
63         
64         ~SpuSampleTaskProcess();
65         
66         ///call initialize in the beginning of the frame, before addCollisionPairToTask
67         void initialize();
68
69         void issueTask(void* sampleMainMemPtr,int sampleValue,int sampleCommand);
70
71         ///call flush to submit potential outstanding work to SPUs and wait for all involved SPUs to be finished
72         void flush();
73 };
74
75
76 #if defined(USE_LIBSPE2) && defined(__SPU__)
77 ////////////////////MAIN/////////////////////////////
78 #include "../SpuLibspe2Support.h"
79 #include <spu_intrinsics.h>
80 #include <spu_mfcio.h>
81 #include <SpuFakeDma.h>
82
83 void * SamplelsMemoryFunc();
84 void SampleThreadFunc(void* userPtr,void* lsMemory);
85
86 //#define DEBUG_LIBSPE2_MAINLOOP
87
88 int main(unsigned long long speid, addr64 argp, addr64 envp)
89 {
90         printf("SPU is up \n");
91         
92         ATTRIBUTE_ALIGNED128(btSpuStatus status);
93         ATTRIBUTE_ALIGNED16( SpuSampleTaskDesc taskDesc ) ;
94         unsigned int received_message = Spu_Mailbox_Event_Nothing;
95         bool shutdown = false;
96
97         cellDmaGet(&status, argp.ull, sizeof(btSpuStatus), DMA_TAG(3), 0, 0);
98         cellDmaWaitTagStatusAll(DMA_MASK(3));
99
100         status.m_status = Spu_Status_Free;
101         status.m_lsMemory.p = SamplelsMemoryFunc();
102
103         cellDmaLargePut(&status, argp.ull, sizeof(btSpuStatus), DMA_TAG(3), 0, 0);
104         cellDmaWaitTagStatusAll(DMA_MASK(3));
105         
106         
107         while (!shutdown)
108         {
109                 received_message = spu_read_in_mbox();
110                 
111
112                 
113                 switch(received_message)
114                 {
115                 case Spu_Mailbox_Event_Shutdown:
116                         shutdown = true;
117                         break; 
118                 case Spu_Mailbox_Event_Task:
119                         // refresh the status
120 #ifdef DEBUG_LIBSPE2_MAINLOOP
121                         printf("SPU recieved Task \n");
122 #endif //DEBUG_LIBSPE2_MAINLOOP
123                         cellDmaGet(&status, argp.ull, sizeof(btSpuStatus), DMA_TAG(3), 0, 0);
124                         cellDmaWaitTagStatusAll(DMA_MASK(3));
125                 
126                         btAssert(status.m_status==Spu_Status_Occupied);
127                         
128                         cellDmaGet(&taskDesc, status.m_taskDesc.p, sizeof(SpuSampleTaskDesc), DMA_TAG(3), 0, 0);
129                         cellDmaWaitTagStatusAll(DMA_MASK(3));
130                         
131                         SampleThreadFunc((void*)&taskDesc, reinterpret_cast<void*> (taskDesc.m_mainMemoryPtr) );
132                         break;
133                 case Spu_Mailbox_Event_Nothing:
134                 default:
135                         break;
136                 }
137
138                 // set to status free and wait for next task
139                 status.m_status = Spu_Status_Free;
140                 cellDmaLargePut(&status, argp.ull, sizeof(btSpuStatus), DMA_TAG(3), 0, 0);
141                 cellDmaWaitTagStatusAll(DMA_MASK(3));           
142                                 
143                 
144         }
145         return 0;
146 }
147 //////////////////////////////////////////////////////
148 #endif
149
150
151
152 #endif // BT_SPU_SAMPLE_TASK_PROCESS_H
153