parallelLinearBvh.cl 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /*
  2. This software is provided 'as-is', without any express or implied warranty.
  3. In no event will the authors be held liable for any damages arising from the use of this software.
  4. Permission is granted to anyone to use this software for any purpose,
  5. including commercial applications, and to alter it and redistribute it freely,
  6. subject to the following restrictions:
  7. 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.
  8. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  9. 3. This notice may not be removed or altered from any source distribution.
  10. */
  11. //Initial Author Jackson Lee, 2014
  12. typedef float b3Scalar;
  13. typedef float4 b3Vector3;
  14. #define b3Max max
  15. #define b3Min min
  16. #define b3Sqrt sqrt
  17. typedef struct
  18. {
  19. unsigned int m_key;
  20. unsigned int m_value;
  21. } SortDataCL;
  22. typedef struct
  23. {
  24. union
  25. {
  26. float4 m_min;
  27. float m_minElems[4];
  28. int m_minIndices[4];
  29. };
  30. union
  31. {
  32. float4 m_max;
  33. float m_maxElems[4];
  34. int m_maxIndices[4];
  35. };
  36. } b3AabbCL;
  37. unsigned int interleaveBits(unsigned int x)
  38. {
  39. //........ ........ ......12 3456789A //x
  40. //....1..2 ..3..4.. 5..6..7. .8..9..A //x after interleaving bits
  41. //......12 3456789A ......12 3456789A //x ^ (x << 16)
  42. //11111111 ........ ........ 11111111 //0x FF 00 00 FF
  43. //......12 ........ ........ 3456789A //x = (x ^ (x << 16)) & 0xFF0000FF;
  44. //......12 ........ 3456789A 3456789A //x ^ (x << 8)
  45. //......11 ........ 1111.... ....1111 //0x 03 00 F0 0F
  46. //......12 ........ 3456.... ....789A //x = (x ^ (x << 8)) & 0x0300F00F;
  47. //..12..12 ....3456 3456.... 789A789A //x ^ (x << 4)
  48. //......11 ....11.. ..11.... 11....11 //0x 03 0C 30 C3
  49. //......12 ....34.. ..56.... 78....9A //x = (x ^ (x << 4)) & 0x030C30C3;
  50. //....1212 ..3434.. 5656..78 78..9A9A //x ^ (x << 2)
  51. //....1..1 ..1..1.. 1..1..1. .1..1..1 //0x 09 24 92 49
  52. //....1..2 ..3..4.. 5..6..7. .8..9..A //x = (x ^ (x << 2)) & 0x09249249;
  53. //........ ........ ......11 11111111 //0x000003FF
  54. x &= 0x000003FF; //Clear all bits above bit 10
  55. x = (x ^ (x << 16)) & 0xFF0000FF;
  56. x = (x ^ (x << 8)) & 0x0300F00F;
  57. x = (x ^ (x << 4)) & 0x030C30C3;
  58. x = (x ^ (x << 2)) & 0x09249249;
  59. return x;
  60. }
  61. unsigned int getMortonCode(unsigned int x, unsigned int y, unsigned int z)
  62. {
  63. return interleaveBits(x) << 0 | interleaveBits(y) << 1 | interleaveBits(z) << 2;
  64. }
  65. __kernel void separateAabbs(__global b3AabbCL* unseparatedAabbs, __global int* aabbIndices, __global b3AabbCL* out_aabbs, int numAabbsToSeparate)
  66. {
  67. int separatedAabbIndex = get_global_id(0);
  68. if(separatedAabbIndex >= numAabbsToSeparate) return;
  69. int unseparatedAabbIndex = aabbIndices[separatedAabbIndex];
  70. out_aabbs[separatedAabbIndex] = unseparatedAabbs[unseparatedAabbIndex];
  71. }
  72. //Should replace with an optimized parallel reduction
  73. __kernel void findAllNodesMergedAabb(__global b3AabbCL* out_mergedAabb, int numAabbsNeedingMerge)
  74. {
  75. //Each time this kernel is added to the command queue,
  76. //the number of AABBs needing to be merged is halved
  77. //
  78. //Example with 159 AABBs:
  79. // numRemainingAabbs == 159 / 2 + 159 % 2 == 80
  80. // numMergedAabbs == 159 - 80 == 79
  81. //So, indices [0, 78] are merged with [0 + 80, 78 + 80]
  82. int numRemainingAabbs = numAabbsNeedingMerge / 2 + numAabbsNeedingMerge % 2;
  83. int numMergedAabbs = numAabbsNeedingMerge - numRemainingAabbs;
  84. int aabbIndex = get_global_id(0);
  85. if(aabbIndex >= numMergedAabbs) return;
  86. int otherAabbIndex = aabbIndex + numRemainingAabbs;
  87. b3AabbCL aabb = out_mergedAabb[aabbIndex];
  88. b3AabbCL otherAabb = out_mergedAabb[otherAabbIndex];
  89. b3AabbCL mergedAabb;
  90. mergedAabb.m_min = b3Min(aabb.m_min, otherAabb.m_min);
  91. mergedAabb.m_max = b3Max(aabb.m_max, otherAabb.m_max);
  92. out_mergedAabb[aabbIndex] = mergedAabb;
  93. }
  94. __kernel void assignMortonCodesAndAabbIndicies(__global b3AabbCL* worldSpaceAabbs, __global b3AabbCL* mergedAabbOfAllNodes,
  95. __global SortDataCL* out_mortonCodesAndAabbIndices, int numAabbs)
  96. {
  97. int leafNodeIndex = get_global_id(0); //Leaf node index == AABB index
  98. if(leafNodeIndex >= numAabbs) return;
  99. b3AabbCL mergedAabb = mergedAabbOfAllNodes[0];
  100. b3Vector3 gridCenter = (mergedAabb.m_min + mergedAabb.m_max) * 0.5f;
  101. b3Vector3 gridCellSize = (mergedAabb.m_max - mergedAabb.m_min) / (float)1024;
  102. b3AabbCL aabb = worldSpaceAabbs[leafNodeIndex];
  103. b3Vector3 aabbCenter = (aabb.m_min + aabb.m_max) * 0.5f;
  104. b3Vector3 aabbCenterRelativeToGrid = aabbCenter - gridCenter;
  105. //Quantize into integer coordinates
  106. //floor() is needed to prevent the center cell, at (0,0,0) from being twice the size
  107. b3Vector3 gridPosition = aabbCenterRelativeToGrid / gridCellSize;
  108. int4 discretePosition;
  109. discretePosition.x = (int)( (gridPosition.x >= 0.0f) ? gridPosition.x : floor(gridPosition.x) );
  110. discretePosition.y = (int)( (gridPosition.y >= 0.0f) ? gridPosition.y : floor(gridPosition.y) );
  111. discretePosition.z = (int)( (gridPosition.z >= 0.0f) ? gridPosition.z : floor(gridPosition.z) );
  112. //Clamp coordinates into [-512, 511], then convert range from [-512, 511] to [0, 1023]
  113. discretePosition = b3Max( -512, b3Min(discretePosition, 511) );
  114. discretePosition += 512;
  115. //Interleave bits(assign a morton code, also known as a z-curve)
  116. unsigned int mortonCode = getMortonCode(discretePosition.x, discretePosition.y, discretePosition.z);
  117. //
  118. SortDataCL mortonCodeIndexPair;
  119. mortonCodeIndexPair.m_key = mortonCode;
  120. mortonCodeIndexPair.m_value = leafNodeIndex;
  121. out_mortonCodesAndAabbIndices[leafNodeIndex] = mortonCodeIndexPair;
  122. }
  123. #define B3_PLVBH_TRAVERSE_MAX_STACK_SIZE 128
  124. //The most significant bit(0x80000000) of a int32 is used to distinguish between leaf and internal nodes.
  125. //If it is set, then the index is for an internal node; otherwise, it is a leaf node.
  126. //In both cases, the bit should be cleared to access the actual node index.
  127. int isLeafNode(int index) { return (index >> 31 == 0); }
  128. int getIndexWithInternalNodeMarkerRemoved(int index) { return index & (~0x80000000); }
  129. int getIndexWithInternalNodeMarkerSet(int isLeaf, int index) { return (isLeaf) ? index : (index | 0x80000000); }
  130. //From sap.cl
  131. #define NEW_PAIR_MARKER -1
  132. bool TestAabbAgainstAabb2(const b3AabbCL* aabb1, const b3AabbCL* aabb2)
  133. {
  134. bool overlap = true;
  135. overlap = (aabb1->m_min.x > aabb2->m_max.x || aabb1->m_max.x < aabb2->m_min.x) ? false : overlap;
  136. overlap = (aabb1->m_min.z > aabb2->m_max.z || aabb1->m_max.z < aabb2->m_min.z) ? false : overlap;
  137. overlap = (aabb1->m_min.y > aabb2->m_max.y || aabb1->m_max.y < aabb2->m_min.y) ? false : overlap;
  138. return overlap;
  139. }
  140. //From sap.cl
  141. __kernel void plbvhCalculateOverlappingPairs(__global b3AabbCL* rigidAabbs,
  142. __global int* rootNodeIndex,
  143. __global int2* internalNodeChildIndices,
  144. __global b3AabbCL* internalNodeAabbs,
  145. __global int2* internalNodeLeafIndexRanges,
  146. __global SortDataCL* mortonCodesAndAabbIndices,
  147. __global int* out_numPairs, __global int4* out_overlappingPairs,
  148. int maxPairs, int numQueryAabbs)
  149. {
  150. //Using get_group_id()/get_local_id() is Faster than get_global_id(0) since
  151. //mortonCodesAndAabbIndices[] contains rigid body indices sorted along the z-curve (more spatially coherent)
  152. int queryBvhNodeIndex = get_group_id(0) * get_local_size(0) + get_local_id(0);
  153. if(queryBvhNodeIndex >= numQueryAabbs) return;
  154. int queryRigidIndex = mortonCodesAndAabbIndices[queryBvhNodeIndex].m_value;
  155. b3AabbCL queryAabb = rigidAabbs[queryRigidIndex];
  156. int stack[B3_PLVBH_TRAVERSE_MAX_STACK_SIZE];
  157. int stackSize = 1;
  158. stack[0] = *rootNodeIndex;
  159. while(stackSize)
  160. {
  161. int internalOrLeafNodeIndex = stack[ stackSize - 1 ];
  162. --stackSize;
  163. int isLeaf = isLeafNode(internalOrLeafNodeIndex); //Internal node if false
  164. int bvhNodeIndex = getIndexWithInternalNodeMarkerRemoved(internalOrLeafNodeIndex);
  165. //Optimization - if the BVH is structured as a binary radix tree, then
  166. //each internal node corresponds to a contiguous range of leaf nodes(internalNodeLeafIndexRanges[]).
  167. //This can be used to avoid testing each AABB-AABB pair twice, including preventing each node from colliding with itself.
  168. {
  169. int highestLeafIndex = (isLeaf) ? bvhNodeIndex : internalNodeLeafIndexRanges[bvhNodeIndex].y;
  170. if(highestLeafIndex <= queryBvhNodeIndex) continue;
  171. }
  172. //bvhRigidIndex is not used if internal node
  173. int bvhRigidIndex = (isLeaf) ? mortonCodesAndAabbIndices[bvhNodeIndex].m_value : -1;
  174. b3AabbCL bvhNodeAabb = (isLeaf) ? rigidAabbs[bvhRigidIndex] : internalNodeAabbs[bvhNodeIndex];
  175. if( TestAabbAgainstAabb2(&queryAabb, &bvhNodeAabb) )
  176. {
  177. if(isLeaf)
  178. {
  179. int4 pair;
  180. pair.x = rigidAabbs[queryRigidIndex].m_minIndices[3];
  181. pair.y = rigidAabbs[bvhRigidIndex].m_minIndices[3];
  182. pair.z = NEW_PAIR_MARKER;
  183. pair.w = NEW_PAIR_MARKER;
  184. int pairIndex = atomic_inc(out_numPairs);
  185. if(pairIndex < maxPairs) out_overlappingPairs[pairIndex] = pair;
  186. }
  187. if(!isLeaf) //Internal node
  188. {
  189. if(stackSize + 2 > B3_PLVBH_TRAVERSE_MAX_STACK_SIZE)
  190. {
  191. //Error
  192. }
  193. else
  194. {
  195. stack[ stackSize++ ] = internalNodeChildIndices[bvhNodeIndex].x;
  196. stack[ stackSize++ ] = internalNodeChildIndices[bvhNodeIndex].y;
  197. }
  198. }
  199. }
  200. }
  201. }
  202. //From rayCastKernels.cl
  203. typedef struct
  204. {
  205. float4 m_from;
  206. float4 m_to;
  207. } b3RayInfo;
  208. //From rayCastKernels.cl
  209. b3Vector3 b3Vector3_normalize(b3Vector3 v)
  210. {
  211. b3Vector3 normal = (b3Vector3){v.x, v.y, v.z, 0.f};
  212. return normalize(normal); //OpenCL normalize == vector4 normalize
  213. }
  214. b3Scalar b3Vector3_length2(b3Vector3 v) { return v.x*v.x + v.y*v.y + v.z*v.z; }
  215. b3Scalar b3Vector3_dot(b3Vector3 a, b3Vector3 b) { return a.x*b.x + a.y*b.y + a.z*b.z; }
  216. int rayIntersectsAabb(b3Vector3 rayOrigin, b3Scalar rayLength, b3Vector3 rayNormalizedDirection, b3AabbCL aabb)
  217. {
  218. //AABB is considered as 3 pairs of 2 planes( {x_min, x_max}, {y_min, y_max}, {z_min, z_max} ).
  219. //t_min is the point of intersection with the closer plane, t_max is the point of intersection with the farther plane.
  220. //
  221. //if (rayNormalizedDirection.x < 0.0f), then max.x will be the near plane
  222. //and min.x will be the far plane; otherwise, it is reversed.
  223. //
  224. //In order for there to be a collision, the t_min and t_max of each pair must overlap.
  225. //This can be tested for by selecting the highest t_min and lowest t_max and comparing them.
  226. int4 isNegative = isless( rayNormalizedDirection, ((b3Vector3){0.0f, 0.0f, 0.0f, 0.0f}) ); //isless(x,y) returns (x < y)
  227. //When using vector types, the select() function checks the most signficant bit,
  228. //but isless() sets the least significant bit.
  229. isNegative <<= 31;
  230. //select(b, a, condition) == condition ? a : b
  231. //When using select() with vector types, (condition[i]) is true if its most significant bit is 1
  232. b3Vector3 t_min = ( select(aabb.m_min, aabb.m_max, isNegative) - rayOrigin ) / rayNormalizedDirection;
  233. b3Vector3 t_max = ( select(aabb.m_max, aabb.m_min, isNegative) - rayOrigin ) / rayNormalizedDirection;
  234. b3Scalar t_min_final = 0.0f;
  235. b3Scalar t_max_final = rayLength;
  236. //Must use fmin()/fmax(); if one of the parameters is NaN, then the parameter that is not NaN is returned.
  237. //Behavior of min()/max() with NaNs is undefined. (See OpenCL Specification 1.2 [6.12.2] and [6.12.4])
  238. //Since the innermost fmin()/fmax() is always not NaN, this should never return NaN.
  239. t_min_final = fmax( t_min.z, fmax(t_min.y, fmax(t_min.x, t_min_final)) );
  240. t_max_final = fmin( t_max.z, fmin(t_max.y, fmin(t_max.x, t_max_final)) );
  241. return (t_min_final <= t_max_final);
  242. }
  243. __kernel void plbvhRayTraverse(__global b3AabbCL* rigidAabbs,
  244. __global int* rootNodeIndex,
  245. __global int2* internalNodeChildIndices,
  246. __global b3AabbCL* internalNodeAabbs,
  247. __global int2* internalNodeLeafIndexRanges,
  248. __global SortDataCL* mortonCodesAndAabbIndices,
  249. __global b3RayInfo* rays,
  250. __global int* out_numRayRigidPairs,
  251. __global int2* out_rayRigidPairs,
  252. int maxRayRigidPairs, int numRays)
  253. {
  254. int rayIndex = get_global_id(0);
  255. if(rayIndex >= numRays) return;
  256. //
  257. b3Vector3 rayFrom = rays[rayIndex].m_from;
  258. b3Vector3 rayTo = rays[rayIndex].m_to;
  259. b3Vector3 rayNormalizedDirection = b3Vector3_normalize(rayTo - rayFrom);
  260. b3Scalar rayLength = b3Sqrt( b3Vector3_length2(rayTo - rayFrom) );
  261. //
  262. int stack[B3_PLVBH_TRAVERSE_MAX_STACK_SIZE];
  263. int stackSize = 1;
  264. stack[0] = *rootNodeIndex;
  265. while(stackSize)
  266. {
  267. int internalOrLeafNodeIndex = stack[ stackSize - 1 ];
  268. --stackSize;
  269. int isLeaf = isLeafNode(internalOrLeafNodeIndex); //Internal node if false
  270. int bvhNodeIndex = getIndexWithInternalNodeMarkerRemoved(internalOrLeafNodeIndex);
  271. //bvhRigidIndex is not used if internal node
  272. int bvhRigidIndex = (isLeaf) ? mortonCodesAndAabbIndices[bvhNodeIndex].m_value : -1;
  273. b3AabbCL bvhNodeAabb = (isLeaf) ? rigidAabbs[bvhRigidIndex] : internalNodeAabbs[bvhNodeIndex];
  274. if( rayIntersectsAabb(rayFrom, rayLength, rayNormalizedDirection, bvhNodeAabb) )
  275. {
  276. if(isLeaf)
  277. {
  278. int2 rayRigidPair;
  279. rayRigidPair.x = rayIndex;
  280. rayRigidPair.y = rigidAabbs[bvhRigidIndex].m_minIndices[3];
  281. int pairIndex = atomic_inc(out_numRayRigidPairs);
  282. if(pairIndex < maxRayRigidPairs) out_rayRigidPairs[pairIndex] = rayRigidPair;
  283. }
  284. if(!isLeaf) //Internal node
  285. {
  286. if(stackSize + 2 > B3_PLVBH_TRAVERSE_MAX_STACK_SIZE)
  287. {
  288. //Error
  289. }
  290. else
  291. {
  292. stack[ stackSize++ ] = internalNodeChildIndices[bvhNodeIndex].x;
  293. stack[ stackSize++ ] = internalNodeChildIndices[bvhNodeIndex].y;
  294. }
  295. }
  296. }
  297. }
  298. }
  299. __kernel void plbvhLargeAabbAabbTest(__global b3AabbCL* smallAabbs, __global b3AabbCL* largeAabbs,
  300. __global int* out_numPairs, __global int4* out_overlappingPairs,
  301. int maxPairs, int numLargeAabbRigids, int numSmallAabbRigids)
  302. {
  303. int smallAabbIndex = get_global_id(0);
  304. if(smallAabbIndex >= numSmallAabbRigids) return;
  305. b3AabbCL smallAabb = smallAabbs[smallAabbIndex];
  306. for(int i = 0; i < numLargeAabbRigids; ++i)
  307. {
  308. b3AabbCL largeAabb = largeAabbs[i];
  309. if( TestAabbAgainstAabb2(&smallAabb, &largeAabb) )
  310. {
  311. int4 pair;
  312. pair.x = largeAabb.m_minIndices[3];
  313. pair.y = smallAabb.m_minIndices[3];
  314. pair.z = NEW_PAIR_MARKER;
  315. pair.w = NEW_PAIR_MARKER;
  316. int pairIndex = atomic_inc(out_numPairs);
  317. if(pairIndex < maxPairs) out_overlappingPairs[pairIndex] = pair;
  318. }
  319. }
  320. }
  321. __kernel void plbvhLargeAabbRayTest(__global b3AabbCL* largeRigidAabbs, __global b3RayInfo* rays,
  322. __global int* out_numRayRigidPairs, __global int2* out_rayRigidPairs,
  323. int numLargeAabbRigids, int maxRayRigidPairs, int numRays)
  324. {
  325. int rayIndex = get_global_id(0);
  326. if(rayIndex >= numRays) return;
  327. b3Vector3 rayFrom = rays[rayIndex].m_from;
  328. b3Vector3 rayTo = rays[rayIndex].m_to;
  329. b3Vector3 rayNormalizedDirection = b3Vector3_normalize(rayTo - rayFrom);
  330. b3Scalar rayLength = b3Sqrt( b3Vector3_length2(rayTo - rayFrom) );
  331. for(int i = 0; i < numLargeAabbRigids; ++i)
  332. {
  333. b3AabbCL rigidAabb = largeRigidAabbs[i];
  334. if( rayIntersectsAabb(rayFrom, rayLength, rayNormalizedDirection, rigidAabb) )
  335. {
  336. int2 rayRigidPair;
  337. rayRigidPair.x = rayIndex;
  338. rayRigidPair.y = rigidAabb.m_minIndices[3];
  339. int pairIndex = atomic_inc(out_numRayRigidPairs);
  340. if(pairIndex < maxRayRigidPairs) out_rayRigidPairs[pairIndex] = rayRigidPair;
  341. }
  342. }
  343. }
  344. //Set so that it is always greater than the actual common prefixes, and never selected as a parent node.
  345. //If there are no duplicates, then the highest common prefix is 32 or 64, depending on the number of bits used for the z-curve.
  346. //Duplicate common prefixes increase the highest common prefix at most by the number of bits used to index the leaf node.
  347. //Since 32 bit ints are used to index leaf nodes, the max prefix is 64(32 + 32 bit z-curve) or 96(32 + 64 bit z-curve).
  348. #define B3_PLBVH_INVALID_COMMON_PREFIX 128
  349. #define B3_PLBVH_ROOT_NODE_MARKER -1
  350. #define b3Int64 long
  351. int computeCommonPrefixLength(b3Int64 i, b3Int64 j) { return (int)clz(i ^ j); }
  352. b3Int64 computeCommonPrefix(b3Int64 i, b3Int64 j)
  353. {
  354. //This function only needs to return (i & j) in order for the algorithm to work,
  355. //but it may help with debugging to mask out the lower bits.
  356. b3Int64 commonPrefixLength = (b3Int64)computeCommonPrefixLength(i, j);
  357. b3Int64 sharedBits = i & j;
  358. b3Int64 bitmask = ((b3Int64)(~0)) << (64 - commonPrefixLength); //Set all bits after the common prefix to 0
  359. return sharedBits & bitmask;
  360. }
  361. //Same as computeCommonPrefixLength(), but allows for prefixes with different lengths
  362. int getSharedPrefixLength(b3Int64 prefixA, int prefixLengthA, b3Int64 prefixB, int prefixLengthB)
  363. {
  364. return b3Min( computeCommonPrefixLength(prefixA, prefixB), b3Min(prefixLengthA, prefixLengthB) );
  365. }
  366. __kernel void computeAdjacentPairCommonPrefix(__global SortDataCL* mortonCodesAndAabbIndices,
  367. __global b3Int64* out_commonPrefixes,
  368. __global int* out_commonPrefixLengths,
  369. int numInternalNodes)
  370. {
  371. int internalNodeIndex = get_global_id(0);
  372. if (internalNodeIndex >= numInternalNodes) return;
  373. //Here, (internalNodeIndex + 1) is never out of bounds since it is a leaf node index,
  374. //and the number of internal nodes is always numLeafNodes - 1
  375. int leftLeafIndex = internalNodeIndex;
  376. int rightLeafIndex = internalNodeIndex + 1;
  377. int leftLeafMortonCode = mortonCodesAndAabbIndices[leftLeafIndex].m_key;
  378. int rightLeafMortonCode = mortonCodesAndAabbIndices[rightLeafIndex].m_key;
  379. //Binary radix tree construction algorithm does not work if there are duplicate morton codes.
  380. //Append the index of each leaf node to each morton code so that there are no duplicates.
  381. //The algorithm also requires that the morton codes are sorted in ascending order; this requirement
  382. //is also satisfied with this method, as (leftLeafIndex < rightLeafIndex) is always true.
  383. //
  384. //upsample(a, b) == ( ((b3Int64)a) << 32) | b
  385. b3Int64 nonduplicateLeftMortonCode = upsample(leftLeafMortonCode, leftLeafIndex);
  386. b3Int64 nonduplicateRightMortonCode = upsample(rightLeafMortonCode, rightLeafIndex);
  387. out_commonPrefixes[internalNodeIndex] = computeCommonPrefix(nonduplicateLeftMortonCode, nonduplicateRightMortonCode);
  388. out_commonPrefixLengths[internalNodeIndex] = computeCommonPrefixLength(nonduplicateLeftMortonCode, nonduplicateRightMortonCode);
  389. }
  390. __kernel void buildBinaryRadixTreeLeafNodes(__global int* commonPrefixLengths, __global int* out_leafNodeParentNodes,
  391. __global int2* out_childNodes, int numLeafNodes)
  392. {
  393. int leafNodeIndex = get_global_id(0);
  394. if (leafNodeIndex >= numLeafNodes) return;
  395. int numInternalNodes = numLeafNodes - 1;
  396. int leftSplitIndex = leafNodeIndex - 1;
  397. int rightSplitIndex = leafNodeIndex;
  398. int leftCommonPrefix = (leftSplitIndex >= 0) ? commonPrefixLengths[leftSplitIndex] : B3_PLBVH_INVALID_COMMON_PREFIX;
  399. int rightCommonPrefix = (rightSplitIndex < numInternalNodes) ? commonPrefixLengths[rightSplitIndex] : B3_PLBVH_INVALID_COMMON_PREFIX;
  400. //Parent node is the highest adjacent common prefix that is lower than the node's common prefix
  401. //Leaf nodes are considered as having the highest common prefix
  402. int isLeftHigherCommonPrefix = (leftCommonPrefix > rightCommonPrefix);
  403. //Handle cases for the edge nodes; the first and last node
  404. //For leaf nodes, leftCommonPrefix and rightCommonPrefix should never both be B3_PLBVH_INVALID_COMMON_PREFIX
  405. if(leftCommonPrefix == B3_PLBVH_INVALID_COMMON_PREFIX) isLeftHigherCommonPrefix = false;
  406. if(rightCommonPrefix == B3_PLBVH_INVALID_COMMON_PREFIX) isLeftHigherCommonPrefix = true;
  407. int parentNodeIndex = (isLeftHigherCommonPrefix) ? leftSplitIndex : rightSplitIndex;
  408. out_leafNodeParentNodes[leafNodeIndex] = parentNodeIndex;
  409. int isRightChild = (isLeftHigherCommonPrefix); //If the left node is the parent, then this node is its right child and vice versa
  410. //out_childNodesAsInt[0] == int2.x == left child
  411. //out_childNodesAsInt[1] == int2.y == right child
  412. int isLeaf = 1;
  413. __global int* out_childNodesAsInt = (__global int*)(&out_childNodes[parentNodeIndex]);
  414. out_childNodesAsInt[isRightChild] = getIndexWithInternalNodeMarkerSet(isLeaf, leafNodeIndex);
  415. }
  416. __kernel void buildBinaryRadixTreeInternalNodes(__global b3Int64* commonPrefixes, __global int* commonPrefixLengths,
  417. __global int2* out_childNodes,
  418. __global int* out_internalNodeParentNodes, __global int* out_rootNodeIndex,
  419. int numInternalNodes)
  420. {
  421. int internalNodeIndex = get_group_id(0) * get_local_size(0) + get_local_id(0);
  422. if(internalNodeIndex >= numInternalNodes) return;
  423. b3Int64 nodePrefix = commonPrefixes[internalNodeIndex];
  424. int nodePrefixLength = commonPrefixLengths[internalNodeIndex];
  425. //#define USE_LINEAR_SEARCH
  426. #ifdef USE_LINEAR_SEARCH
  427. int leftIndex = -1;
  428. int rightIndex = -1;
  429. //Find nearest element to left with a lower common prefix
  430. for(int i = internalNodeIndex - 1; i >= 0; --i)
  431. {
  432. int nodeLeftSharedPrefixLength = getSharedPrefixLength(nodePrefix, nodePrefixLength, commonPrefixes[i], commonPrefixLengths[i]);
  433. if(nodeLeftSharedPrefixLength < nodePrefixLength)
  434. {
  435. leftIndex = i;
  436. break;
  437. }
  438. }
  439. //Find nearest element to right with a lower common prefix
  440. for(int i = internalNodeIndex + 1; i < numInternalNodes; ++i)
  441. {
  442. int nodeRightSharedPrefixLength = getSharedPrefixLength(nodePrefix, nodePrefixLength, commonPrefixes[i], commonPrefixLengths[i]);
  443. if(nodeRightSharedPrefixLength < nodePrefixLength)
  444. {
  445. rightIndex = i;
  446. break;
  447. }
  448. }
  449. #else //Use binary search
  450. //Find nearest element to left with a lower common prefix
  451. int leftIndex = -1;
  452. {
  453. int lower = 0;
  454. int upper = internalNodeIndex - 1;
  455. while(lower <= upper)
  456. {
  457. int mid = (lower + upper) / 2;
  458. b3Int64 midPrefix = commonPrefixes[mid];
  459. int midPrefixLength = commonPrefixLengths[mid];
  460. int nodeMidSharedPrefixLength = getSharedPrefixLength(nodePrefix, nodePrefixLength, midPrefix, midPrefixLength);
  461. if(nodeMidSharedPrefixLength < nodePrefixLength)
  462. {
  463. int right = mid + 1;
  464. if(right < internalNodeIndex)
  465. {
  466. b3Int64 rightPrefix = commonPrefixes[right];
  467. int rightPrefixLength = commonPrefixLengths[right];
  468. int nodeRightSharedPrefixLength = getSharedPrefixLength(nodePrefix, nodePrefixLength, rightPrefix, rightPrefixLength);
  469. if(nodeRightSharedPrefixLength < nodePrefixLength)
  470. {
  471. lower = right;
  472. leftIndex = right;
  473. }
  474. else
  475. {
  476. leftIndex = mid;
  477. break;
  478. }
  479. }
  480. else
  481. {
  482. leftIndex = mid;
  483. break;
  484. }
  485. }
  486. else upper = mid - 1;
  487. }
  488. }
  489. //Find nearest element to right with a lower common prefix
  490. int rightIndex = -1;
  491. {
  492. int lower = internalNodeIndex + 1;
  493. int upper = numInternalNodes - 1;
  494. while(lower <= upper)
  495. {
  496. int mid = (lower + upper) / 2;
  497. b3Int64 midPrefix = commonPrefixes[mid];
  498. int midPrefixLength = commonPrefixLengths[mid];
  499. int nodeMidSharedPrefixLength = getSharedPrefixLength(nodePrefix, nodePrefixLength, midPrefix, midPrefixLength);
  500. if(nodeMidSharedPrefixLength < nodePrefixLength)
  501. {
  502. int left = mid - 1;
  503. if(left > internalNodeIndex)
  504. {
  505. b3Int64 leftPrefix = commonPrefixes[left];
  506. int leftPrefixLength = commonPrefixLengths[left];
  507. int nodeLeftSharedPrefixLength = getSharedPrefixLength(nodePrefix, nodePrefixLength, leftPrefix, leftPrefixLength);
  508. if(nodeLeftSharedPrefixLength < nodePrefixLength)
  509. {
  510. upper = left;
  511. rightIndex = left;
  512. }
  513. else
  514. {
  515. rightIndex = mid;
  516. break;
  517. }
  518. }
  519. else
  520. {
  521. rightIndex = mid;
  522. break;
  523. }
  524. }
  525. else lower = mid + 1;
  526. }
  527. }
  528. #endif
  529. //Select parent
  530. {
  531. int leftPrefixLength = (leftIndex != -1) ? commonPrefixLengths[leftIndex] : B3_PLBVH_INVALID_COMMON_PREFIX;
  532. int rightPrefixLength = (rightIndex != -1) ? commonPrefixLengths[rightIndex] : B3_PLBVH_INVALID_COMMON_PREFIX;
  533. int isLeftHigherPrefixLength = (leftPrefixLength > rightPrefixLength);
  534. if(leftPrefixLength == B3_PLBVH_INVALID_COMMON_PREFIX) isLeftHigherPrefixLength = false;
  535. else if(rightPrefixLength == B3_PLBVH_INVALID_COMMON_PREFIX) isLeftHigherPrefixLength = true;
  536. int parentNodeIndex = (isLeftHigherPrefixLength) ? leftIndex : rightIndex;
  537. int isRootNode = (leftIndex == -1 && rightIndex == -1);
  538. out_internalNodeParentNodes[internalNodeIndex] = (!isRootNode) ? parentNodeIndex : B3_PLBVH_ROOT_NODE_MARKER;
  539. int isLeaf = 0;
  540. if(!isRootNode)
  541. {
  542. int isRightChild = (isLeftHigherPrefixLength); //If the left node is the parent, then this node is its right child and vice versa
  543. //out_childNodesAsInt[0] == int2.x == left child
  544. //out_childNodesAsInt[1] == int2.y == right child
  545. __global int* out_childNodesAsInt = (__global int*)(&out_childNodes[parentNodeIndex]);
  546. out_childNodesAsInt[isRightChild] = getIndexWithInternalNodeMarkerSet(isLeaf, internalNodeIndex);
  547. }
  548. else *out_rootNodeIndex = getIndexWithInternalNodeMarkerSet(isLeaf, internalNodeIndex);
  549. }
  550. }
  551. __kernel void findDistanceFromRoot(__global int* rootNodeIndex, __global int* internalNodeParentNodes,
  552. __global int* out_maxDistanceFromRoot, __global int* out_distanceFromRoot, int numInternalNodes)
  553. {
  554. if( get_global_id(0) == 0 ) atomic_xchg(out_maxDistanceFromRoot, 0);
  555. int internalNodeIndex = get_global_id(0);
  556. if(internalNodeIndex >= numInternalNodes) return;
  557. //
  558. int distanceFromRoot = 0;
  559. {
  560. int parentIndex = internalNodeParentNodes[internalNodeIndex];
  561. while(parentIndex != B3_PLBVH_ROOT_NODE_MARKER)
  562. {
  563. parentIndex = internalNodeParentNodes[parentIndex];
  564. ++distanceFromRoot;
  565. }
  566. }
  567. out_distanceFromRoot[internalNodeIndex] = distanceFromRoot;
  568. //
  569. __local int localMaxDistanceFromRoot;
  570. if( get_local_id(0) == 0 ) localMaxDistanceFromRoot = 0;
  571. barrier(CLK_LOCAL_MEM_FENCE);
  572. atomic_max(&localMaxDistanceFromRoot, distanceFromRoot);
  573. barrier(CLK_LOCAL_MEM_FENCE);
  574. if( get_local_id(0) == 0 ) atomic_max(out_maxDistanceFromRoot, localMaxDistanceFromRoot);
  575. }
  576. __kernel void buildBinaryRadixTreeAabbsRecursive(__global int* distanceFromRoot, __global SortDataCL* mortonCodesAndAabbIndices,
  577. __global int2* childNodes,
  578. __global b3AabbCL* leafNodeAabbs, __global b3AabbCL* internalNodeAabbs,
  579. int maxDistanceFromRoot, int processedDistance, int numInternalNodes)
  580. {
  581. int internalNodeIndex = get_global_id(0);
  582. if(internalNodeIndex >= numInternalNodes) return;
  583. int distance = distanceFromRoot[internalNodeIndex];
  584. if(distance == processedDistance)
  585. {
  586. int leftChildIndex = childNodes[internalNodeIndex].x;
  587. int rightChildIndex = childNodes[internalNodeIndex].y;
  588. int isLeftChildLeaf = isLeafNode(leftChildIndex);
  589. int isRightChildLeaf = isLeafNode(rightChildIndex);
  590. leftChildIndex = getIndexWithInternalNodeMarkerRemoved(leftChildIndex);
  591. rightChildIndex = getIndexWithInternalNodeMarkerRemoved(rightChildIndex);
  592. //leftRigidIndex/rightRigidIndex is not used if internal node
  593. int leftRigidIndex = (isLeftChildLeaf) ? mortonCodesAndAabbIndices[leftChildIndex].m_value : -1;
  594. int rightRigidIndex = (isRightChildLeaf) ? mortonCodesAndAabbIndices[rightChildIndex].m_value : -1;
  595. b3AabbCL leftChildAabb = (isLeftChildLeaf) ? leafNodeAabbs[leftRigidIndex] : internalNodeAabbs[leftChildIndex];
  596. b3AabbCL rightChildAabb = (isRightChildLeaf) ? leafNodeAabbs[rightRigidIndex] : internalNodeAabbs[rightChildIndex];
  597. b3AabbCL mergedAabb;
  598. mergedAabb.m_min = b3Min(leftChildAabb.m_min, rightChildAabb.m_min);
  599. mergedAabb.m_max = b3Max(leftChildAabb.m_max, rightChildAabb.m_max);
  600. internalNodeAabbs[internalNodeIndex] = mergedAabb;
  601. }
  602. }
  603. __kernel void findLeafIndexRanges(__global int2* internalNodeChildNodes, __global int2* out_leafIndexRanges, int numInternalNodes)
  604. {
  605. int internalNodeIndex = get_global_id(0);
  606. if(internalNodeIndex >= numInternalNodes) return;
  607. int numLeafNodes = numInternalNodes + 1;
  608. int2 childNodes = internalNodeChildNodes[internalNodeIndex];
  609. int2 leafIndexRange; //x == min leaf index, y == max leaf index
  610. //Find lowest leaf index covered by this internal node
  611. {
  612. int lowestIndex = childNodes.x; //childNodes.x == Left child
  613. while( !isLeafNode(lowestIndex) ) lowestIndex = internalNodeChildNodes[ getIndexWithInternalNodeMarkerRemoved(lowestIndex) ].x;
  614. leafIndexRange.x = lowestIndex;
  615. }
  616. //Find highest leaf index covered by this internal node
  617. {
  618. int highestIndex = childNodes.y; //childNodes.y == Right child
  619. while( !isLeafNode(highestIndex) ) highestIndex = internalNodeChildNodes[ getIndexWithInternalNodeMarkerRemoved(highestIndex) ].y;
  620. leafIndexRange.y = highestIndex;
  621. }
  622. //
  623. out_leafIndexRanges[internalNodeIndex] = leafIndexRange;
  624. }