btGpu3DGridBroadphaseSharedCode.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
  3. Copyright (C) 2006, 2009 Sony Computer Entertainment Inc.
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. //----------------------------------------------------------------------------------------
  14. //----------------------------------------------------------------------------------------
  15. //----------------------------------------------------------------------------------------
  16. //----------------------------------------------------------------------------------------
  17. //----------------------------------------------------------------------------------------
  18. // K E R N E L F U N C T I O N S
  19. //----------------------------------------------------------------------------------------
  20. //----------------------------------------------------------------------------------------
  21. //----------------------------------------------------------------------------------------
  22. //----------------------------------------------------------------------------------------
  23. //----------------------------------------------------------------------------------------
  24. //----------------------------------------------------------------------------------------
  25. // calculate position in uniform grid
  26. BT_GPU___device__ int3 bt3DGrid_calcGridPos(float4 p)
  27. {
  28. int3 gridPos;
  29. gridPos.x = (int)floor((p.x - BT_GPU_params.m_worldOriginX) / BT_GPU_params.m_cellSizeX);
  30. gridPos.y = (int)floor((p.y - BT_GPU_params.m_worldOriginY) / BT_GPU_params.m_cellSizeY);
  31. gridPos.z = (int)floor((p.z - BT_GPU_params.m_worldOriginZ) / BT_GPU_params.m_cellSizeZ);
  32. return gridPos;
  33. } // bt3DGrid_calcGridPos()
  34. //----------------------------------------------------------------------------------------
  35. // calculate address in grid from position (clamping to edges)
  36. BT_GPU___device__ uint bt3DGrid_calcGridHash(int3 gridPos)
  37. {
  38. gridPos.x = BT_GPU_max(0, BT_GPU_min(gridPos.x, (int)BT_GPU_params.m_gridSizeX - 1));
  39. gridPos.y = BT_GPU_max(0, BT_GPU_min(gridPos.y, (int)BT_GPU_params.m_gridSizeY - 1));
  40. gridPos.z = BT_GPU_max(0, BT_GPU_min(gridPos.z, (int)BT_GPU_params.m_gridSizeZ - 1));
  41. return BT_GPU___mul24(BT_GPU___mul24(gridPos.z, BT_GPU_params.m_gridSizeY), BT_GPU_params.m_gridSizeX) + BT_GPU___mul24(gridPos.y, BT_GPU_params.m_gridSizeX) + gridPos.x;
  42. } // bt3DGrid_calcGridHash()
  43. //----------------------------------------------------------------------------------------
  44. // calculate grid hash value for each body using its AABB
  45. BT_GPU___global__ void calcHashAABBD(bt3DGrid3F1U* pAABB, uint2* pHash, uint numBodies)
  46. {
  47. int index = BT_GPU___mul24(BT_GPU_blockIdx.x, BT_GPU_blockDim.x) + BT_GPU_threadIdx.x;
  48. if(index >= (int)numBodies)
  49. {
  50. return;
  51. }
  52. bt3DGrid3F1U bbMin = pAABB[index*2];
  53. bt3DGrid3F1U bbMax = pAABB[index*2 + 1];
  54. float4 pos;
  55. pos.x = (bbMin.fx + bbMax.fx) * 0.5f;
  56. pos.y = (bbMin.fy + bbMax.fy) * 0.5f;
  57. pos.z = (bbMin.fz + bbMax.fz) * 0.5f;
  58. // get address in grid
  59. int3 gridPos = bt3DGrid_calcGridPos(pos);
  60. uint gridHash = bt3DGrid_calcGridHash(gridPos);
  61. // store grid hash and body index
  62. pHash[index] = BT_GPU_make_uint2(gridHash, index);
  63. } // calcHashAABBD()
  64. //----------------------------------------------------------------------------------------
  65. BT_GPU___global__ void findCellStartD(uint2* pHash, uint* cellStart, uint numBodies)
  66. {
  67. int index = BT_GPU___mul24(BT_GPU_blockIdx.x, BT_GPU_blockDim.x) + BT_GPU_threadIdx.x;
  68. if(index >= (int)numBodies)
  69. {
  70. return;
  71. }
  72. uint2 sortedData = pHash[index];
  73. // Load hash data into shared memory so that we can look
  74. // at neighboring body's hash value without loading
  75. // two hash values per thread
  76. BT_GPU___shared__ uint sharedHash[257];
  77. sharedHash[BT_GPU_threadIdx.x+1] = sortedData.x;
  78. if((index > 0) && (BT_GPU_threadIdx.x == 0))
  79. {
  80. // first thread in block must load neighbor body hash
  81. volatile uint2 prevData = pHash[index-1];
  82. sharedHash[0] = prevData.x;
  83. }
  84. BT_GPU___syncthreads();
  85. if((index == 0) || (sortedData.x != sharedHash[BT_GPU_threadIdx.x]))
  86. {
  87. cellStart[sortedData.x] = index;
  88. }
  89. } // findCellStartD()
  90. //----------------------------------------------------------------------------------------
  91. BT_GPU___device__ uint cudaTestAABBOverlap(bt3DGrid3F1U min0, bt3DGrid3F1U max0, bt3DGrid3F1U min1, bt3DGrid3F1U max1)
  92. {
  93. return (min0.fx <= max1.fx)&& (min1.fx <= max0.fx) &&
  94. (min0.fy <= max1.fy)&& (min1.fy <= max0.fy) &&
  95. (min0.fz <= max1.fz)&& (min1.fz <= max0.fz);
  96. } // cudaTestAABBOverlap()
  97. //----------------------------------------------------------------------------------------
  98. BT_GPU___device__ void findPairsInCell( int3 gridPos,
  99. uint index,
  100. uint2* pHash,
  101. uint* pCellStart,
  102. bt3DGrid3F1U* pAABB,
  103. uint* pPairBuff,
  104. uint2* pPairBuffStartCurr,
  105. uint numBodies)
  106. {
  107. if ( (gridPos.x < 0) || (gridPos.x > (int)BT_GPU_params.m_gridSizeX - 1)
  108. || (gridPos.y < 0) || (gridPos.y > (int)BT_GPU_params.m_gridSizeY - 1)
  109. || (gridPos.z < 0) || (gridPos.z > (int)BT_GPU_params.m_gridSizeZ - 1))
  110. {
  111. return;
  112. }
  113. uint gridHash = bt3DGrid_calcGridHash(gridPos);
  114. // get start of bucket for this cell
  115. uint bucketStart = pCellStart[gridHash];
  116. if (bucketStart == 0xffffffff)
  117. {
  118. return; // cell empty
  119. }
  120. // iterate over bodies in this cell
  121. uint2 sortedData = pHash[index];
  122. uint unsorted_indx = sortedData.y;
  123. bt3DGrid3F1U min0 = BT_GPU_FETCH(pAABB, unsorted_indx*2);
  124. bt3DGrid3F1U max0 = BT_GPU_FETCH(pAABB, unsorted_indx*2 + 1);
  125. uint handleIndex = min0.uw;
  126. uint2 start_curr = pPairBuffStartCurr[handleIndex];
  127. uint start = start_curr.x;
  128. uint curr = start_curr.y;
  129. uint2 start_curr_next = pPairBuffStartCurr[handleIndex+1];
  130. uint curr_max = start_curr_next.x - start - 1;
  131. uint bucketEnd = bucketStart + BT_GPU_params.m_maxBodiesPerCell;
  132. bucketEnd = (bucketEnd > numBodies) ? numBodies : bucketEnd;
  133. for(uint index2 = bucketStart; index2 < bucketEnd; index2++)
  134. {
  135. uint2 cellData = pHash[index2];
  136. if (cellData.x != gridHash)
  137. {
  138. break; // no longer in same bucket
  139. }
  140. uint unsorted_indx2 = cellData.y;
  141. if (unsorted_indx2 < unsorted_indx) // check not colliding with self
  142. {
  143. bt3DGrid3F1U min1 = BT_GPU_FETCH(pAABB, unsorted_indx2*2);
  144. bt3DGrid3F1U max1 = BT_GPU_FETCH(pAABB, unsorted_indx2*2 + 1);
  145. if(cudaTestAABBOverlap(min0, max0, min1, max1))
  146. {
  147. uint handleIndex2 = min1.uw;
  148. uint k;
  149. for(k = 0; k < curr; k++)
  150. {
  151. uint old_pair = pPairBuff[start+k] & (~BT_3DGRID_PAIR_ANY_FLG);
  152. if(old_pair == handleIndex2)
  153. {
  154. pPairBuff[start+k] |= BT_3DGRID_PAIR_FOUND_FLG;
  155. break;
  156. }
  157. }
  158. if(k == curr)
  159. {
  160. if(curr >= curr_max)
  161. { // not a good solution, but let's avoid crash
  162. break;
  163. }
  164. pPairBuff[start+curr] = handleIndex2 | BT_3DGRID_PAIR_NEW_FLG;
  165. curr++;
  166. }
  167. }
  168. }
  169. }
  170. pPairBuffStartCurr[handleIndex] = BT_GPU_make_uint2(start, curr);
  171. return;
  172. } // findPairsInCell()
  173. //----------------------------------------------------------------------------------------
  174. BT_GPU___global__ void findOverlappingPairsD( bt3DGrid3F1U* pAABB, uint2* pHash, uint* pCellStart,
  175. uint* pPairBuff, uint2* pPairBuffStartCurr, uint numBodies)
  176. {
  177. int index = BT_GPU___mul24(BT_GPU_blockIdx.x, BT_GPU_blockDim.x) + BT_GPU_threadIdx.x;
  178. if(index >= (int)numBodies)
  179. {
  180. return;
  181. }
  182. uint2 sortedData = pHash[index];
  183. uint unsorted_indx = sortedData.y;
  184. bt3DGrid3F1U bbMin = BT_GPU_FETCH(pAABB, unsorted_indx*2);
  185. bt3DGrid3F1U bbMax = BT_GPU_FETCH(pAABB, unsorted_indx*2 + 1);
  186. float4 pos;
  187. pos.x = (bbMin.fx + bbMax.fx) * 0.5f;
  188. pos.y = (bbMin.fy + bbMax.fy) * 0.5f;
  189. pos.z = (bbMin.fz + bbMax.fz) * 0.5f;
  190. // get address in grid
  191. int3 gridPos = bt3DGrid_calcGridPos(pos);
  192. // examine only neighbouring cells
  193. for(int z=-1; z<=1; z++) {
  194. for(int y=-1; y<=1; y++) {
  195. for(int x=-1; x<=1; x++) {
  196. findPairsInCell(gridPos + BT_GPU_make_int3(x, y, z), index, pHash, pCellStart, pAABB, pPairBuff, pPairBuffStartCurr, numBodies);
  197. }
  198. }
  199. }
  200. } // findOverlappingPairsD()
  201. //----------------------------------------------------------------------------------------
  202. BT_GPU___global__ void findPairsLargeD( bt3DGrid3F1U* pAABB, uint2* pHash, uint* pCellStart, uint* pPairBuff,
  203. uint2* pPairBuffStartCurr, uint numBodies, uint numLarge)
  204. {
  205. int index = BT_GPU___mul24(BT_GPU_blockIdx.x, BT_GPU_blockDim.x) + BT_GPU_threadIdx.x;
  206. if(index >= (int)numBodies)
  207. {
  208. return;
  209. }
  210. uint2 sortedData = pHash[index];
  211. uint unsorted_indx = sortedData.y;
  212. bt3DGrid3F1U min0 = BT_GPU_FETCH(pAABB, unsorted_indx*2);
  213. bt3DGrid3F1U max0 = BT_GPU_FETCH(pAABB, unsorted_indx*2 + 1);
  214. uint handleIndex = min0.uw;
  215. uint2 start_curr = pPairBuffStartCurr[handleIndex];
  216. uint start = start_curr.x;
  217. uint curr = start_curr.y;
  218. uint2 start_curr_next = pPairBuffStartCurr[handleIndex+1];
  219. uint curr_max = start_curr_next.x - start - 1;
  220. for(uint i = 0; i < numLarge; i++)
  221. {
  222. uint indx2 = numBodies + i;
  223. bt3DGrid3F1U min1 = BT_GPU_FETCH(pAABB, indx2*2);
  224. bt3DGrid3F1U max1 = BT_GPU_FETCH(pAABB, indx2*2 + 1);
  225. if(cudaTestAABBOverlap(min0, max0, min1, max1))
  226. {
  227. uint k;
  228. uint handleIndex2 = min1.uw;
  229. for(k = 0; k < curr; k++)
  230. {
  231. uint old_pair = pPairBuff[start+k] & (~BT_3DGRID_PAIR_ANY_FLG);
  232. if(old_pair == handleIndex2)
  233. {
  234. pPairBuff[start+k] |= BT_3DGRID_PAIR_FOUND_FLG;
  235. break;
  236. }
  237. }
  238. if(k == curr)
  239. {
  240. pPairBuff[start+curr] = handleIndex2 | BT_3DGRID_PAIR_NEW_FLG;
  241. if(curr >= curr_max)
  242. { // not a good solution, but let's avoid crash
  243. break;
  244. }
  245. curr++;
  246. }
  247. }
  248. }
  249. pPairBuffStartCurr[handleIndex] = BT_GPU_make_uint2(start, curr);
  250. return;
  251. } // findPairsLargeD()
  252. //----------------------------------------------------------------------------------------
  253. BT_GPU___global__ void computePairCacheChangesD(uint* pPairBuff, uint2* pPairBuffStartCurr,
  254. uint* pPairScan, bt3DGrid3F1U* pAABB, uint numBodies)
  255. {
  256. int index = BT_GPU___mul24(BT_GPU_blockIdx.x, BT_GPU_blockDim.x) + BT_GPU_threadIdx.x;
  257. if(index >= (int)numBodies)
  258. {
  259. return;
  260. }
  261. bt3DGrid3F1U bbMin = pAABB[index * 2];
  262. uint handleIndex = bbMin.uw;
  263. uint2 start_curr = pPairBuffStartCurr[handleIndex];
  264. uint start = start_curr.x;
  265. uint curr = start_curr.y;
  266. uint *pInp = pPairBuff + start;
  267. uint num_changes = 0;
  268. for(uint k = 0; k < curr; k++, pInp++)
  269. {
  270. if(!((*pInp) & BT_3DGRID_PAIR_FOUND_FLG))
  271. {
  272. num_changes++;
  273. }
  274. }
  275. pPairScan[index+1] = num_changes;
  276. } // computePairCacheChangesD()
  277. //----------------------------------------------------------------------------------------
  278. BT_GPU___global__ void squeezeOverlappingPairBuffD(uint* pPairBuff, uint2* pPairBuffStartCurr, uint* pPairScan,
  279. uint* pPairOut, bt3DGrid3F1U* pAABB, uint numBodies)
  280. {
  281. int index = BT_GPU___mul24(BT_GPU_blockIdx.x, BT_GPU_blockDim.x) + BT_GPU_threadIdx.x;
  282. if(index >= (int)numBodies)
  283. {
  284. return;
  285. }
  286. bt3DGrid3F1U bbMin = pAABB[index * 2];
  287. uint handleIndex = bbMin.uw;
  288. uint2 start_curr = pPairBuffStartCurr[handleIndex];
  289. uint start = start_curr.x;
  290. uint curr = start_curr.y;
  291. uint* pInp = pPairBuff + start;
  292. uint* pOut = pPairOut + pPairScan[index];
  293. uint* pOut2 = pInp;
  294. uint num = 0;
  295. for(uint k = 0; k < curr; k++, pInp++)
  296. {
  297. if(!((*pInp) & BT_3DGRID_PAIR_FOUND_FLG))
  298. {
  299. *pOut = *pInp;
  300. pOut++;
  301. }
  302. if((*pInp) & BT_3DGRID_PAIR_ANY_FLG)
  303. {
  304. *pOut2 = (*pInp) & (~BT_3DGRID_PAIR_ANY_FLG);
  305. pOut2++;
  306. num++;
  307. }
  308. }
  309. pPairBuffStartCurr[handleIndex] = BT_GPU_make_uint2(start, num);
  310. } // squeezeOverlappingPairBuffD()
  311. //----------------------------------------------------------------------------------------
  312. //----------------------------------------------------------------------------------------
  313. //----------------------------------------------------------------------------------------
  314. //----------------------------------------------------------------------------------------
  315. // E N D O F K E R N E L F U N C T I O N S
  316. //----------------------------------------------------------------------------------------
  317. //----------------------------------------------------------------------------------------
  318. //----------------------------------------------------------------------------------------
  319. //----------------------------------------------------------------------------------------
  320. extern "C"
  321. {
  322. //----------------------------------------------------------------------------------------
  323. void BT_GPU_PREF(calcHashAABB)(bt3DGrid3F1U* pAABB, unsigned int* hash, unsigned int numBodies)
  324. {
  325. int numThreads, numBlocks;
  326. BT_GPU_PREF(computeGridSize)(numBodies, 256, numBlocks, numThreads);
  327. // execute the kernel
  328. BT_GPU_EXECKERNEL(numBlocks, numThreads, calcHashAABBD, (pAABB, (uint2*)hash, numBodies));
  329. // check if kernel invocation generated an error
  330. BT_GPU_CHECK_ERROR("calcHashAABBD kernel execution failed");
  331. } // calcHashAABB()
  332. //----------------------------------------------------------------------------------------
  333. void BT_GPU_PREF(findCellStart(unsigned int* hash, unsigned int* cellStart, unsigned int numBodies, unsigned int numCells))
  334. {
  335. int numThreads, numBlocks;
  336. BT_GPU_PREF(computeGridSize)(numBodies, 256, numBlocks, numThreads);
  337. BT_GPU_SAFE_CALL(BT_GPU_Memset(cellStart, 0xffffffff, numCells*sizeof(uint)));
  338. BT_GPU_EXECKERNEL(numBlocks, numThreads, findCellStartD, ((uint2*)hash, (uint*)cellStart, numBodies));
  339. BT_GPU_CHECK_ERROR("Kernel execution failed: findCellStartD");
  340. } // findCellStart()
  341. //----------------------------------------------------------------------------------------
  342. void BT_GPU_PREF(findOverlappingPairs(bt3DGrid3F1U* pAABB, unsigned int* pHash, unsigned int* pCellStart, unsigned int* pPairBuff, unsigned int* pPairBuffStartCurr, unsigned int numBodies))
  343. {
  344. #if B_CUDA_USE_TEX
  345. BT_GPU_SAFE_CALL(cudaBindTexture(0, pAABBTex, pAABB, numBodies * 2 * sizeof(bt3DGrid3F1U)));
  346. #endif
  347. int numThreads, numBlocks;
  348. BT_GPU_PREF(computeGridSize)(numBodies, 64, numBlocks, numThreads);
  349. BT_GPU_EXECKERNEL(numBlocks, numThreads, findOverlappingPairsD, (pAABB,(uint2*)pHash,(uint*)pCellStart,(uint*)pPairBuff,(uint2*)pPairBuffStartCurr,numBodies));
  350. BT_GPU_CHECK_ERROR("Kernel execution failed: bt_CudaFindOverlappingPairsD");
  351. #if B_CUDA_USE_TEX
  352. BT_GPU_SAFE_CALL(cudaUnbindTexture(pAABBTex));
  353. #endif
  354. } // findOverlappingPairs()
  355. //----------------------------------------------------------------------------------------
  356. void BT_GPU_PREF(findPairsLarge(bt3DGrid3F1U* pAABB, unsigned int* pHash, unsigned int* pCellStart, unsigned int* pPairBuff, unsigned int* pPairBuffStartCurr, unsigned int numBodies, unsigned int numLarge))
  357. {
  358. #if B_CUDA_USE_TEX
  359. BT_GPU_SAFE_CALL(cudaBindTexture(0, pAABBTex, pAABB, (numBodies+numLarge) * 2 * sizeof(bt3DGrid3F1U)));
  360. #endif
  361. int numThreads, numBlocks;
  362. BT_GPU_PREF(computeGridSize)(numBodies, 64, numBlocks, numThreads);
  363. BT_GPU_EXECKERNEL(numBlocks, numThreads, findPairsLargeD, (pAABB,(uint2*)pHash,(uint*)pCellStart,(uint*)pPairBuff,(uint2*)pPairBuffStartCurr,numBodies,numLarge));
  364. BT_GPU_CHECK_ERROR("Kernel execution failed: btCuda_findPairsLargeD");
  365. #if B_CUDA_USE_TEX
  366. BT_GPU_SAFE_CALL(cudaUnbindTexture(pAABBTex));
  367. #endif
  368. } // findPairsLarge()
  369. //----------------------------------------------------------------------------------------
  370. void BT_GPU_PREF(computePairCacheChanges(unsigned int* pPairBuff, unsigned int* pPairBuffStartCurr, unsigned int* pPairScan, bt3DGrid3F1U* pAABB, unsigned int numBodies))
  371. {
  372. int numThreads, numBlocks;
  373. BT_GPU_PREF(computeGridSize)(numBodies, 256, numBlocks, numThreads);
  374. BT_GPU_EXECKERNEL(numBlocks, numThreads, computePairCacheChangesD, ((uint*)pPairBuff,(uint2*)pPairBuffStartCurr,(uint*)pPairScan,pAABB,numBodies));
  375. BT_GPU_CHECK_ERROR("Kernel execution failed: btCudaComputePairCacheChangesD");
  376. } // computePairCacheChanges()
  377. //----------------------------------------------------------------------------------------
  378. void BT_GPU_PREF(squeezeOverlappingPairBuff(unsigned int* pPairBuff, unsigned int* pPairBuffStartCurr, unsigned int* pPairScan, unsigned int* pPairOut, bt3DGrid3F1U* pAABB, unsigned int numBodies))
  379. {
  380. int numThreads, numBlocks;
  381. BT_GPU_PREF(computeGridSize)(numBodies, 256, numBlocks, numThreads);
  382. BT_GPU_EXECKERNEL(numBlocks, numThreads, squeezeOverlappingPairBuffD, ((uint*)pPairBuff,(uint2*)pPairBuffStartCurr,(uint*)pPairScan,(uint*)pPairOut,pAABB,numBodies));
  383. BT_GPU_CHECK_ERROR("Kernel execution failed: btCudaSqueezeOverlappingPairBuffD");
  384. } // btCuda_squeezeOverlappingPairBuff()
  385. //------------------------------------------------------------------------------------------------
  386. } // extern "C"
  387. //------------------------------------------------------------------------------------------------
  388. //------------------------------------------------------------------------------------------------
  389. //------------------------------------------------------------------------------------------------