Imported Upstream version 2.81
[platform/upstream/libbullet.git] / src / BulletMultiThreaded / SpuCollisionTaskProcess.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_COLLISION_TASK_PROCESS_H
17 #define BT_SPU_COLLISION_TASK_PROCESS_H
18
19 #include <assert.h>
20
21 #include "LinearMath/btScalar.h"
22
23 #include "PlatformDefinitions.h"
24 #include "LinearMath/btAlignedObjectArray.h"
25 #include "SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h" // for definitions processCollisionTask and createCollisionLocalStoreMemory
26
27 #include "btThreadSupportInterface.h"
28
29
30 //#include "SPUAssert.h"
31 #include <string.h>
32
33
34 #include "BulletCollision/CollisionDispatch/btCollisionObject.h"
35 #include "BulletCollision/CollisionShapes/btCollisionShape.h"
36 #include "BulletCollision/CollisionShapes/btConvexShape.h"
37
38 #include "LinearMath/btAlignedAllocator.h"
39
40 #include <stdio.h>
41
42
43 #define DEBUG_SpuCollisionTaskProcess 1
44
45
46 #define CMD_GATHER_AND_PROCESS_PAIRLIST 1
47
48 class btCollisionObject;
49 class btPersistentManifold;
50 class btDispatcher;
51
52
53 /////Task Description for SPU collision detection
54 //struct SpuGatherAndProcessPairsTaskDesc
55 //{
56 //      uint64_t        inPtr;//m_pairArrayPtr;
57 //      //mutex variable
58 //      uint32_t        m_someMutexVariableInMainMemory;
59 //
60 //      uint64_t        m_dispatcher;
61 //
62 //      uint32_t        numOnLastPage;
63 //
64 //      uint16_t numPages;
65 //      uint16_t taskId;
66 //
67 //      struct  CollisionTask_LocalStoreMemory* m_lsMemory; 
68 //}
69 //
70 //#if  defined(__CELLOS_LV2__) || defined(USE_LIBSPE2)
71 //__attribute__ ((aligned (16)))
72 //#endif
73 //;
74
75
76 ///MidphaseWorkUnitInput stores individual primitive versus mesh collision detection input, to be processed by the SPU.
77 ATTRIBUTE_ALIGNED16(struct) SpuGatherAndProcessWorkUnitInput
78 {
79         uint64_t m_pairArrayPtr;
80         int             m_startIndex;
81         int             m_endIndex;
82 };
83
84
85
86
87 /// SpuCollisionTaskProcess handles SPU processing of collision pairs.
88 /// Maintains a set of task buffers.
89 /// When the task is full, the task is issued for SPUs to process.  Contact output goes into btPersistentManifold
90 /// associated with each task.
91 /// When PPU issues a task, it will look for completed task buffers
92 /// PPU will do postprocessing, dependent on workunit output (not likely)
93 class SpuCollisionTaskProcess
94 {
95
96   unsigned char  *m_workUnitTaskBuffers;
97
98
99         // track task buffers that are being used, and total busy tasks
100         btAlignedObjectArray<bool>      m_taskBusy;
101         btAlignedObjectArray<SpuGatherAndProcessPairsTaskDesc>  m_spuGatherTaskDesc;
102
103         class   btThreadSupportInterface*       m_threadInterface;
104
105         unsigned int    m_maxNumOutstandingTasks;
106
107         unsigned int   m_numBusyTasks;
108
109         // the current task and the current entry to insert a new work unit
110         unsigned int   m_currentTask;
111         unsigned int   m_currentPage;
112         unsigned int   m_currentPageEntry;
113
114         bool m_useEpa;
115
116 #ifdef DEBUG_SpuCollisionTaskProcess
117         bool m_initialized;
118 #endif
119         void issueTask2();
120         //void postProcess(unsigned int taskId, int outputSize);
121
122 public:
123         SpuCollisionTaskProcess(btThreadSupportInterface*       threadInterface, unsigned int maxNumOutstandingTasks);
124         
125         ~SpuCollisionTaskProcess();
126         
127         ///call initialize in the beginning of the frame, before addCollisionPairToTask
128         void initialize2(bool useEpa = false);
129
130         ///batch up additional work to a current task for SPU processing. When batch is full, it issues the task.
131         void addWorkToTask(void* pairArrayPtr,int startIndex,int endIndex);
132
133         ///call flush to submit potential outstanding work to SPUs and wait for all involved SPUs to be finished
134         void flush2();
135
136         /// set the maximum number of SPU tasks allocated
137         void    setNumTasks(int maxNumTasks);
138
139         int             getNumTasks() const
140         {
141                 return m_maxNumOutstandingTasks;
142         }
143 };
144
145
146
147 #define MIDPHASE_TASK_PTR(task) (&m_workUnitTaskBuffers[0] + MIDPHASE_WORKUNIT_TASK_SIZE*task)
148 #define MIDPHASE_ENTRY_PTR(task,page,entry) (MIDPHASE_TASK_PTR(task) + MIDPHASE_WORKUNIT_PAGE_SIZE*page + sizeof(SpuGatherAndProcessWorkUnitInput)*entry)
149 #define MIDPHASE_OUTPUT_PTR(task) (&m_contactOutputBuffers[0] + MIDPHASE_MAX_CONTACT_BUFFER_SIZE*task)
150 #define MIDPHASE_TREENODES_PTR(task) (&m_complexShapeBuffers[0] + MIDPHASE_COMPLEX_SHAPE_BUFFER_SIZE*task)
151
152
153 #define MIDPHASE_WORKUNIT_PAGE_SIZE (16)
154 //#define MIDPHASE_WORKUNIT_PAGE_SIZE (128)
155
156 #define MIDPHASE_NUM_WORKUNIT_PAGES 1
157 #define MIDPHASE_WORKUNIT_TASK_SIZE (MIDPHASE_WORKUNIT_PAGE_SIZE*MIDPHASE_NUM_WORKUNIT_PAGES)
158 #define MIDPHASE_NUM_WORKUNITS_PER_PAGE (MIDPHASE_WORKUNIT_PAGE_SIZE / sizeof(SpuGatherAndProcessWorkUnitInput))
159 #define MIDPHASE_NUM_WORKUNITS_PER_TASK (MIDPHASE_NUM_WORKUNITS_PER_PAGE*MIDPHASE_NUM_WORKUNIT_PAGES)
160
161
162 #endif // BT_SPU_COLLISION_TASK_PROCESS_H
163