2 Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
3 Copyright (C) 2006, 2009 Sony Computer Entertainment Inc.
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:
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.
16 //----------------------------------------------------------------------------------------
18 #ifndef BTGPU3DGRIDBROADPHASE_H
19 #define BTGPU3DGRIDBROADPHASE_H
21 //----------------------------------------------------------------------------------------
23 #include "BulletCollision/BroadphaseCollision/btSimpleBroadphase.h"
25 #include "btGpu3DGridBroadphaseSharedTypes.h"
32 //----------------------------------------------------------------------------------------
34 ///The btGpu3DGridBroadphase uses GPU-style code compiled for CPU to compute overlapping pairs
36 class btGpu3DGridBroadphase : public btSimpleBroadphase
40 unsigned int m_numBodies;
41 unsigned int m_numCells;
42 unsigned int m_maxPairsPerBody;
43 unsigned int m_maxBodiesPerCell;
44 bt3DGridBroadphaseParams m_params;
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;
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()
64 btAssert(m_numLargeHandles < m_maxLargeHandles);
65 int freeLargeHandle = m_firstFreeLargeHandle;
66 m_firstFreeLargeHandle = m_pLargeHandles[freeLargeHandle].GetNextFree();
68 if(freeLargeHandle > m_LastLargeHandleIndex)
70 m_LastLargeHandleIndex = freeLargeHandle;
72 return freeLargeHandle;
74 void freeLargeHandle(btSimpleBroadphaseProxy* proxy)
76 int handle = int(proxy - m_pLargeHandles);
77 btAssert((handle >= 0) && (handle < m_maxHandles));
78 if(handle == m_LastLargeHandleIndex)
80 m_LastLargeHandleIndex--;
82 proxy->SetNextFree(m_firstFreeLargeHandle);
83 m_firstFreeLargeHandle = handle;
84 proxy->m_clientObject = 0;
87 bool isLargeProxy(const btVector3& aabbMin, const btVector3& aabbMax);
88 bool isLargeProxy(btBroadphaseProxy* proxy);
90 unsigned int m_numPairsAdded;
91 unsigned int m_numPairsRemoved;
92 unsigned int m_numOverflows;
95 virtual int getNumOverlap()
97 return m_hPairScanChanged[m_numHandles+1];
99 virtual MyUint2* getOverlap()
101 return m_hAllOverlappingPairs;
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);
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);
123 static int getFloorPowOfTwo(int val); // returns 2^n : 2^(n+1) > val >= 2^n
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);
132 void addPairsToCache(btDispatcher* dispatcher);
133 void addLarge2LargePairsToCache(btDispatcher* dispatcher);
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();
148 //----------------------------------------------------------------------------------------
150 #endif //BTGPU3DGRIDBROADPHASE_H
152 //----------------------------------------------------------------------------------------
153 //----------------------------------------------------------------------------------------
154 //----------------------------------------------------------------------------------------