Imported Upstream version 2.81
[platform/upstream/libbullet.git] / src / BulletMultiThreaded / 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
27 //----------------------------------------------------------------------------------------
28
29 ///The btGpu3DGridBroadphase uses GPU-style code compiled for CPU to compute overlapping pairs
30
31 class btGpu3DGridBroadphase : public btSimpleBroadphase
32 {
33 protected:
34         bool                    m_bInitialized;
35     unsigned int        m_numBodies;
36     unsigned int        m_numCells;
37         unsigned int    m_maxPairsPerBody;
38         btScalar                m_cellFactorAABB;
39     unsigned int        m_maxBodiesPerCell;
40         bt3DGridBroadphaseParams m_params;
41         btScalar                m_maxRadius;
42         // CPU data
43     unsigned int*       m_hBodiesHash;
44     unsigned int*       m_hCellStart;
45         unsigned int*   m_hPairBuffStartCurr;
46         bt3DGrid3F1U*           m_hAABB;
47         unsigned int*   m_hPairBuff;
48         unsigned int*   m_hPairScan;
49         unsigned int*   m_hPairOut;
50 // large proxies
51         int             m_numLargeHandles;                                              
52         int             m_maxLargeHandles;                                              
53         int             m_LastLargeHandleIndex;                                                 
54         btSimpleBroadphaseProxy* m_pLargeHandles;
55         void* m_pLargeHandlesRawPtr;
56         int             m_firstFreeLargeHandle;
57         int allocLargeHandle()
58         {
59                 btAssert(m_numLargeHandles < m_maxLargeHandles);
60                 int freeLargeHandle = m_firstFreeLargeHandle;
61                 m_firstFreeLargeHandle = m_pLargeHandles[freeLargeHandle].GetNextFree();
62                 m_numLargeHandles++;
63                 if(freeLargeHandle > m_LastLargeHandleIndex)
64                 {
65                         m_LastLargeHandleIndex = freeLargeHandle;
66                 }
67                 return freeLargeHandle;
68         }
69         void freeLargeHandle(btSimpleBroadphaseProxy* proxy)
70         {
71                 int handle = int(proxy - m_pLargeHandles);
72                 btAssert((handle >= 0) && (handle < m_maxHandles));
73                 if(handle == m_LastLargeHandleIndex)
74                 {
75                         m_LastLargeHandleIndex--;
76                 }
77                 proxy->SetNextFree(m_firstFreeLargeHandle);
78                 m_firstFreeLargeHandle = handle;
79                 proxy->m_clientObject = 0;
80                 m_numLargeHandles--;
81         }
82         bool isLargeProxy(const btVector3& aabbMin,  const btVector3& aabbMax);
83         bool isLargeProxy(btBroadphaseProxy* proxy);
84 // debug
85         unsigned int    m_numPairsAdded;
86         unsigned int    m_numPairsRemoved;
87         unsigned int    m_numOverflows;
88 // 
89 public:
90         btGpu3DGridBroadphase(const btVector3& worldAabbMin,const btVector3& worldAabbMax, 
91                                            int gridSizeX, int gridSizeY, int gridSizeZ, 
92                                            int maxSmallProxies, int maxLargeProxies, int maxPairsPerBody,
93                                            int maxBodiesPerCell = 8,
94                                            btScalar cellFactorAABB = btScalar(1.0f));
95         btGpu3DGridBroadphase(  btOverlappingPairCache* overlappingPairCache,
96                                                 const btVector3& worldAabbMin,const btVector3& worldAabbMax, 
97                                                 int gridSizeX, int gridSizeY, int gridSizeZ, 
98                                                 int maxSmallProxies, int maxLargeProxies, int maxPairsPerBody,
99                                                 int maxBodiesPerCell = 8,
100                                                 btScalar cellFactorAABB = btScalar(1.0f));
101         virtual ~btGpu3DGridBroadphase();
102         virtual void    calculateOverlappingPairs(btDispatcher* dispatcher);
103
104         virtual btBroadphaseProxy*      createProxy(const btVector3& aabbMin,  const btVector3& aabbMax,int shapeType,void* userPtr ,short int collisionFilterGroup,short int collisionFilterMask, btDispatcher* dispatcher,void* multiSapProxy);
105         virtual void    destroyProxy(btBroadphaseProxy* proxy,btDispatcher* dispatcher);
106         virtual void    rayTest(const btVector3& rayFrom,const btVector3& rayTo, btBroadphaseRayCallback& rayCallback, const btVector3& aabbMin=btVector3(0,0,0),const btVector3& aabbMax=btVector3(0,0,0));
107
108     
109         virtual void    resetPool(btDispatcher* dispatcher);
110
111 protected:
112         void _initialize(       const btVector3& worldAabbMin,const btVector3& worldAabbMax, 
113                                                 int gridSizeX, int gridSizeY, int gridSizeZ, 
114                                                 int maxSmallProxies, int maxLargeProxies, int maxPairsPerBody,
115                                                 int maxBodiesPerCell = 8,
116                                                 btScalar cellFactorAABB = btScalar(1.0f));
117         void _finalize();
118         void addPairsToCache(btDispatcher* dispatcher);
119         void addLarge2LargePairsToCache(btDispatcher* dispatcher);
120
121 // overrides for CPU version
122         virtual void setParameters(bt3DGridBroadphaseParams* hostParams);
123         virtual void prepareAABB();
124         virtual void calcHashAABB();
125         virtual void sortHash();        
126         virtual void findCellStart();
127         virtual void findOverlappingPairs();
128         virtual void findPairsLarge();
129         virtual void computePairCacheChanges();
130         virtual void scanOverlappingPairBuff();
131         virtual void squeezeOverlappingPairBuff();
132 };
133
134 //----------------------------------------------------------------------------------------
135
136 #endif //BTGPU3DGRIDBROADPHASE_H
137
138 //----------------------------------------------------------------------------------------
139 //----------------------------------------------------------------------------------------
140 //----------------------------------------------------------------------------------------