gridBroadphase.cl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. int getPosHash(int4 gridPos, __global float4* pParams)
  2. {
  3. int4 gridDim = *((__global int4*)(pParams + 1));
  4. gridPos.x &= gridDim.x - 1;
  5. gridPos.y &= gridDim.y - 1;
  6. gridPos.z &= gridDim.z - 1;
  7. int hash = gridPos.z * gridDim.y * gridDim.x + gridPos.y * gridDim.x + gridPos.x;
  8. return hash;
  9. }
  10. int4 getGridPos(float4 worldPos, __global float4* pParams)
  11. {
  12. int4 gridPos;
  13. int4 gridDim = *((__global int4*)(pParams + 1));
  14. gridPos.x = (int)floor(worldPos.x * pParams[0].x) & (gridDim.x - 1);
  15. gridPos.y = (int)floor(worldPos.y * pParams[0].y) & (gridDim.y - 1);
  16. gridPos.z = (int)floor(worldPos.z * pParams[0].z) & (gridDim.z - 1);
  17. return gridPos;
  18. }
  19. // calculate grid hash value for each body using its AABB
  20. __kernel void kCalcHashAABB(int numObjects, __global float4* allpAABB, __global const int* smallAabbMapping, __global int2* pHash, __global float4* pParams )
  21. {
  22. int index = get_global_id(0);
  23. if(index >= numObjects)
  24. {
  25. return;
  26. }
  27. float4 bbMin = allpAABB[smallAabbMapping[index]*2];
  28. float4 bbMax = allpAABB[smallAabbMapping[index]*2 + 1];
  29. float4 pos;
  30. pos.x = (bbMin.x + bbMax.x) * 0.5f;
  31. pos.y = (bbMin.y + bbMax.y) * 0.5f;
  32. pos.z = (bbMin.z + bbMax.z) * 0.5f;
  33. pos.w = 0.f;
  34. // get address in grid
  35. int4 gridPos = getGridPos(pos, pParams);
  36. int gridHash = getPosHash(gridPos, pParams);
  37. // store grid hash and body index
  38. int2 hashVal;
  39. hashVal.x = gridHash;
  40. hashVal.y = index;
  41. pHash[index] = hashVal;
  42. }
  43. __kernel void kClearCellStart( int numCells,
  44. __global int* pCellStart )
  45. {
  46. int index = get_global_id(0);
  47. if(index >= numCells)
  48. {
  49. return;
  50. }
  51. pCellStart[index] = -1;
  52. }
  53. __kernel void kFindCellStart(int numObjects, __global int2* pHash, __global int* cellStart )
  54. {
  55. __local int sharedHash[513];
  56. int index = get_global_id(0);
  57. int2 sortedData;
  58. if(index < numObjects)
  59. {
  60. sortedData = pHash[index];
  61. // Load hash data into shared memory so that we can look
  62. // at neighboring body's hash value without loading
  63. // two hash values per thread
  64. sharedHash[get_local_id(0) + 1] = sortedData.x;
  65. if((index > 0) && (get_local_id(0) == 0))
  66. {
  67. // first thread in block must load neighbor body hash
  68. sharedHash[0] = pHash[index-1].x;
  69. }
  70. }
  71. barrier(CLK_LOCAL_MEM_FENCE);
  72. if(index < numObjects)
  73. {
  74. if((index == 0) || (sortedData.x != sharedHash[get_local_id(0)]))
  75. {
  76. cellStart[sortedData.x] = index;
  77. }
  78. }
  79. }
  80. int testAABBOverlap(float4 min0, float4 max0, float4 min1, float4 max1)
  81. {
  82. return (min0.x <= max1.x)&& (min1.x <= max0.x) &&
  83. (min0.y <= max1.y)&& (min1.y <= max0.y) &&
  84. (min0.z <= max1.z)&& (min1.z <= max0.z);
  85. }
  86. //search for AABB 'index' against other AABBs' in this cell
  87. void findPairsInCell( int numObjects,
  88. int4 gridPos,
  89. int index,
  90. __global int2* pHash,
  91. __global int* pCellStart,
  92. __global float4* allpAABB,
  93. __global const int* smallAabbMapping,
  94. __global float4* pParams,
  95. volatile __global int* pairCount,
  96. __global int4* pPairBuff2,
  97. int maxPairs
  98. )
  99. {
  100. int4 pGridDim = *((__global int4*)(pParams + 1));
  101. int maxBodiesPerCell = pGridDim.w;
  102. int gridHash = getPosHash(gridPos, pParams);
  103. // get start of bucket for this cell
  104. int bucketStart = pCellStart[gridHash];
  105. if (bucketStart == -1)
  106. {
  107. return; // cell empty
  108. }
  109. // iterate over bodies in this cell
  110. int2 sortedData = pHash[index];
  111. int unsorted_indx = sortedData.y;
  112. float4 min0 = allpAABB[smallAabbMapping[unsorted_indx]*2 + 0];
  113. float4 max0 = allpAABB[smallAabbMapping[unsorted_indx]*2 + 1];
  114. int handleIndex = as_int(min0.w);
  115. int bucketEnd = bucketStart + maxBodiesPerCell;
  116. bucketEnd = (bucketEnd > numObjects) ? numObjects : bucketEnd;
  117. for(int index2 = bucketStart; index2 < bucketEnd; index2++)
  118. {
  119. int2 cellData = pHash[index2];
  120. if (cellData.x != gridHash)
  121. {
  122. break; // no longer in same bucket
  123. }
  124. int unsorted_indx2 = cellData.y;
  125. //if (unsorted_indx2 < unsorted_indx) // check not colliding with self
  126. if (unsorted_indx2 != unsorted_indx) // check not colliding with self
  127. {
  128. float4 min1 = allpAABB[smallAabbMapping[unsorted_indx2]*2 + 0];
  129. float4 max1 = allpAABB[smallAabbMapping[unsorted_indx2]*2 + 1];
  130. if(testAABBOverlap(min0, max0, min1, max1))
  131. {
  132. if (pairCount)
  133. {
  134. int handleIndex2 = as_int(min1.w);
  135. if (handleIndex<handleIndex2)
  136. {
  137. int curPair = atomic_add(pairCount,1);
  138. if (curPair<maxPairs)
  139. {
  140. int4 newpair;
  141. newpair.x = handleIndex;
  142. newpair.y = handleIndex2;
  143. newpair.z = -1;
  144. newpair.w = -1;
  145. pPairBuff2[curPair] = newpair;
  146. }
  147. }
  148. }
  149. }
  150. }
  151. }
  152. }
  153. __kernel void kFindOverlappingPairs( int numObjects,
  154. __global float4* allpAABB,
  155. __global const int* smallAabbMapping,
  156. __global int2* pHash,
  157. __global int* pCellStart,
  158. __global float4* pParams ,
  159. volatile __global int* pairCount,
  160. __global int4* pPairBuff2,
  161. int maxPairs
  162. )
  163. {
  164. int index = get_global_id(0);
  165. if(index >= numObjects)
  166. {
  167. return;
  168. }
  169. int2 sortedData = pHash[index];
  170. int unsorted_indx = sortedData.y;
  171. float4 bbMin = allpAABB[smallAabbMapping[unsorted_indx]*2 + 0];
  172. float4 bbMax = allpAABB[smallAabbMapping[unsorted_indx]*2 + 1];
  173. float4 pos;
  174. pos.x = (bbMin.x + bbMax.x) * 0.5f;
  175. pos.y = (bbMin.y + bbMax.y) * 0.5f;
  176. pos.z = (bbMin.z + bbMax.z) * 0.5f;
  177. // get address in grid
  178. int4 gridPosA = getGridPos(pos, pParams);
  179. int4 gridPosB;
  180. // examine only neighbouring cells
  181. for(int z=-1; z<=1; z++)
  182. {
  183. gridPosB.z = gridPosA.z + z;
  184. for(int y=-1; y<=1; y++)
  185. {
  186. gridPosB.y = gridPosA.y + y;
  187. for(int x=-1; x<=1; x++)
  188. {
  189. gridPosB.x = gridPosA.x + x;
  190. findPairsInCell(numObjects, gridPosB, index, pHash, pCellStart, allpAABB,smallAabbMapping, pParams, pairCount,pPairBuff2, maxPairs);
  191. }
  192. }
  193. }
  194. }