Tizen 2.1 base
[platform/upstream/libbullet.git] / Extras / RigidBodyGpuPipeline / opencl / 3dGridBroadphase / Shared / btGpu3DGridBroadphase.h
1 /*
2 Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
3 Copyright (C) 2006, 2009 Sony Computer Entertainment Inc. 
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
18 #ifndef BTGPU3DGRIDBROADPHASE_H
19 #define BTGPU3DGRIDBROADPHASE_H
20
21 //----------------------------------------------------------------------------------------
22
23 #include "BulletCollision/BroadphaseCollision/btSimpleBroadphase.h"
24
25 #include "btGpu3DGridBroadphaseSharedTypes.h"
26 struct MyUint2
27 {
28         int x;
29         int y;
30 };
31
32 //----------------------------------------------------------------------------------------
33
34 ///The btGpu3DGridBroadphase uses GPU-style code compiled for CPU to compute overlapping pairs
35
36 class btGpu3DGridBroadphase : public btSimpleBroadphase
37 {
38 protected:
39         bool                    m_bInitialized;
40     unsigned int        m_numBodies;
41     unsigned int        m_numCells;
42         unsigned int    m_maxPairsPerBody;
43     unsigned int        m_maxBodiesPerCell;
44         bt3DGridBroadphaseParams m_params;
45         btScalar                m_maxRadius;
46         // CPU data
47     unsigned int*       m_hBodiesHash;
48     unsigned int*       m_hCellStart;
49         unsigned int*   m_hPairBuffStartCurr;
50         bt3DGrid3F1U*   m_hAABB;
51         unsigned int*   m_hPairBuff;
52         unsigned int*   m_hPairScanChanged;
53         unsigned int*   m_hPairsChanged;
54         MyUint2*                m_hAllOverlappingPairs;
55 // large proxies
56         int             m_numLargeHandles;                                              
57         int             m_maxLargeHandles;                                              
58         int             m_LastLargeHandleIndex;                                                 
59         btSimpleBroadphaseProxy* m_pLargeHandles;
60         void* m_pLargeHandlesRawPtr;
61         int             m_firstFreeLargeHandle;
62         int allocLargeHandle()
63         {
64                 btAssert(m_numLargeHandles < m_maxLargeHandles);
65                 int freeLargeHandle = m_firstFreeLargeHandle;
66                 m_firstFreeLargeHandle = m_pLargeHandles[freeLargeHandle].GetNextFree();
67                 m_numLargeHandles++;
68                 if(freeLargeHandle > m_LastLargeHandleIndex)
69                 {
70                         m_LastLargeHandleIndex = freeLargeHandle;
71                 }
72                 return freeLargeHandle;
73         }
74         void freeLargeHandle(btSimpleBroadphaseProxy* proxy)
75         {
76                 int handle = int(proxy - m_pLargeHandles);
77                 btAssert((handle >= 0) && (handle < m_maxHandles));
78                 if(handle == m_LastLargeHandleIndex)
79                 {
80                         m_LastLargeHandleIndex--;
81                 }
82                 proxy->SetNextFree(m_firstFreeLargeHandle);
83                 m_firstFreeLargeHandle = handle;
84                 proxy->m_clientObject = 0;
85                 m_numLargeHandles--;
86         }
87         bool isLargeProxy(const btVector3& aabbMin,  const btVector3& aabbMax);
88         bool isLargeProxy(btBroadphaseProxy* proxy);
89 // debug
90         unsigned int    m_numPairsAdded;
91         unsigned int    m_numPairsRemoved;
92         unsigned int    m_numOverflows;
93 // 
94 public:
95         virtual int getNumOverlap()
96         {
97                 return m_hPairScanChanged[m_numHandles+1];
98         }
99         virtual MyUint2* getOverlap()
100         {
101                 return m_hAllOverlappingPairs;
102         }
103         // NOTE : for better results gridSizeX, gridSizeY and gridSizeZ should be powers of 2 
104         btGpu3DGridBroadphase(const btVector3& cellSize, 
105                                            int gridSizeX, int gridSizeY, int gridSizeZ, 
106                                            int maxSmallProxies, int maxLargeProxies, int maxPairsPerBody,
107                                                 btScalar maxSmallProxySize,
108                                            int maxBodiesPerCell = 8);
109         btGpu3DGridBroadphase(  btOverlappingPairCache* overlappingPairCache,
110                                                 const btVector3& cellSize, 
111                                                 int gridSizeX, int gridSizeY, int gridSizeZ, 
112                                                 int maxSmallProxies, int maxLargeProxies, int maxPairsPerBody,
113                                                 btScalar maxSmallProxySize,
114                                                 int maxBodiesPerCell = 8);
115         virtual ~btGpu3DGridBroadphase();
116         virtual void    calculateOverlappingPairs(btDispatcher* dispatcher);
117
118         virtual btBroadphaseProxy*      createProxy(const btVector3& aabbMin,  const btVector3& aabbMax,int shapeType,void* userPtr ,short int collisionFilterGroup,short int collisionFilterMask, btDispatcher* dispatcher,void* multiSapProxy);
119         virtual void    destroyProxy(btBroadphaseProxy* proxy,btDispatcher* dispatcher);
120         virtual void    rayTest(const btVector3& rayFrom,const btVector3& rayTo, btBroadphaseRayCallback& rayCallback);
121         virtual void    resetPool(btDispatcher* dispatcher);
122
123         static int              getFloorPowOfTwo(int val); // returns 2^n : 2^(n+1) > val >= 2^n
124
125 protected:
126         void _initialize(       const btVector3& cellSize, 
127                                                 int gridSizeX, int gridSizeY, int gridSizeZ, 
128                                                 int maxSmallProxies, int maxLargeProxies, int maxPairsPerBody,
129                                                 btScalar maxSmallProxySize,
130                                                 int maxBodiesPerCell);
131         void _finalize();
132         void addPairsToCache(btDispatcher* dispatcher);
133         void addLarge2LargePairsToCache(btDispatcher* dispatcher);
134
135 // overrides for CPU version
136         virtual void setParameters(bt3DGridBroadphaseParams* hostParams);
137         virtual void prepareAABB();
138         virtual void calcHashAABB();
139         virtual void sortHash();        
140         virtual void findCellStart();
141         virtual void findOverlappingPairs();
142         virtual void findPairsLarge();
143         virtual void computePairCacheChanges();
144         virtual void scanOverlappingPairBuff(bool copyToCpu=true);
145         virtual void squeezeOverlappingPairBuff();
146 };
147
148 //----------------------------------------------------------------------------------------
149
150 #endif //BTGPU3DGRIDBROADPHASE_H
151
152 //----------------------------------------------------------------------------------------
153 //----------------------------------------------------------------------------------------
154 //----------------------------------------------------------------------------------------