rayCastKernels.cl 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. #define SHAPE_CONVEX_HULL 3
  2. #define SHAPE_PLANE 4
  3. #define SHAPE_CONCAVE_TRIMESH 5
  4. #define SHAPE_COMPOUND_OF_CONVEX_HULLS 6
  5. #define SHAPE_SPHERE 7
  6. typedef struct
  7. {
  8. float4 m_from;
  9. float4 m_to;
  10. } b3RayInfo;
  11. typedef struct
  12. {
  13. float m_hitFraction;
  14. int m_hitResult0;
  15. int m_hitResult1;
  16. int m_hitResult2;
  17. float4 m_hitPoint;
  18. float4 m_hitNormal;
  19. } b3RayHit;
  20. typedef struct
  21. {
  22. float4 m_pos;
  23. float4 m_quat;
  24. float4 m_linVel;
  25. float4 m_angVel;
  26. unsigned int m_collidableIdx;
  27. float m_invMass;
  28. float m_restituitionCoeff;
  29. float m_frictionCoeff;
  30. } Body;
  31. typedef struct Collidable
  32. {
  33. union {
  34. int m_numChildShapes;
  35. int m_bvhIndex;
  36. };
  37. float m_radius;
  38. int m_shapeType;
  39. int m_shapeIndex;
  40. } Collidable;
  41. typedef struct
  42. {
  43. float4 m_localCenter;
  44. float4 m_extents;
  45. float4 mC;
  46. float4 mE;
  47. float m_radius;
  48. int m_faceOffset;
  49. int m_numFaces;
  50. int m_numVertices;
  51. int m_vertexOffset;
  52. int m_uniqueEdgesOffset;
  53. int m_numUniqueEdges;
  54. int m_unused;
  55. } ConvexPolyhedronCL;
  56. typedef struct
  57. {
  58. float4 m_plane;
  59. int m_indexOffset;
  60. int m_numIndices;
  61. } b3GpuFace;
  62. ///////////////////////////////////////
  63. // Quaternion
  64. ///////////////////////////////////////
  65. typedef float4 Quaternion;
  66. __inline
  67. Quaternion qtMul(Quaternion a, Quaternion b);
  68. __inline
  69. Quaternion qtNormalize(Quaternion in);
  70. __inline
  71. Quaternion qtInvert(Quaternion q);
  72. __inline
  73. float dot3F4(float4 a, float4 b)
  74. {
  75. float4 a1 = (float4)(a.xyz,0.f);
  76. float4 b1 = (float4)(b.xyz,0.f);
  77. return dot(a1, b1);
  78. }
  79. __inline
  80. Quaternion qtMul(Quaternion a, Quaternion b)
  81. {
  82. Quaternion ans;
  83. ans = cross( a, b );
  84. ans += a.w*b+b.w*a;
  85. // ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);
  86. ans.w = a.w*b.w - dot3F4(a, b);
  87. return ans;
  88. }
  89. __inline
  90. Quaternion qtNormalize(Quaternion in)
  91. {
  92. return fast_normalize(in);
  93. // in /= length( in );
  94. // return in;
  95. }
  96. __inline
  97. float4 qtRotate(Quaternion q, float4 vec)
  98. {
  99. Quaternion qInv = qtInvert( q );
  100. float4 vcpy = vec;
  101. vcpy.w = 0.f;
  102. float4 out = qtMul(q,vcpy);
  103. out = qtMul(out,qInv);
  104. return out;
  105. }
  106. __inline
  107. Quaternion qtInvert(Quaternion q)
  108. {
  109. return (Quaternion)(-q.xyz, q.w);
  110. }
  111. __inline
  112. float4 qtInvRotate(const Quaternion q, float4 vec)
  113. {
  114. return qtRotate( qtInvert( q ), vec );
  115. }
  116. void trInverse(float4 translationIn, Quaternion orientationIn,
  117. float4* translationOut, Quaternion* orientationOut)
  118. {
  119. *orientationOut = qtInvert(orientationIn);
  120. *translationOut = qtRotate(*orientationOut, -translationIn);
  121. }
  122. bool rayConvex(float4 rayFromLocal, float4 rayToLocal, int numFaces, int faceOffset,
  123. __global const b3GpuFace* faces, float* hitFraction, float4* hitNormal)
  124. {
  125. rayFromLocal.w = 0.f;
  126. rayToLocal.w = 0.f;
  127. bool result = true;
  128. float exitFraction = hitFraction[0];
  129. float enterFraction = -0.3f;
  130. float4 curHitNormal = (float4)(0,0,0,0);
  131. for (int i=0;i<numFaces && result;i++)
  132. {
  133. b3GpuFace face = faces[faceOffset+i];
  134. float fromPlaneDist = dot(rayFromLocal,face.m_plane)+face.m_plane.w;
  135. float toPlaneDist = dot(rayToLocal,face.m_plane)+face.m_plane.w;
  136. if (fromPlaneDist<0.f)
  137. {
  138. if (toPlaneDist >= 0.f)
  139. {
  140. float fraction = fromPlaneDist / (fromPlaneDist-toPlaneDist);
  141. if (exitFraction>fraction)
  142. {
  143. exitFraction = fraction;
  144. }
  145. }
  146. } else
  147. {
  148. if (toPlaneDist<0.f)
  149. {
  150. float fraction = fromPlaneDist / (fromPlaneDist-toPlaneDist);
  151. if (enterFraction <= fraction)
  152. {
  153. enterFraction = fraction;
  154. curHitNormal = face.m_plane;
  155. curHitNormal.w = 0.f;
  156. }
  157. } else
  158. {
  159. result = false;
  160. }
  161. }
  162. if (exitFraction <= enterFraction)
  163. result = false;
  164. }
  165. if (enterFraction < 0.f)
  166. {
  167. result = false;
  168. }
  169. if (result)
  170. {
  171. hitFraction[0] = enterFraction;
  172. hitNormal[0] = curHitNormal;
  173. }
  174. return result;
  175. }
  176. bool sphere_intersect(float4 spherePos, float radius, float4 rayFrom, float4 rayTo, float* hitFraction)
  177. {
  178. float4 rs = rayFrom - spherePos;
  179. rs.w = 0.f;
  180. float4 rayDir = rayTo-rayFrom;
  181. rayDir.w = 0.f;
  182. float A = dot(rayDir,rayDir);
  183. float B = dot(rs, rayDir);
  184. float C = dot(rs, rs) - (radius * radius);
  185. float D = B * B - A*C;
  186. if (D > 0.0f)
  187. {
  188. float t = (-B - sqrt(D))/A;
  189. if ( (t >= 0.0f) && (t < (*hitFraction)) )
  190. {
  191. *hitFraction = t;
  192. return true;
  193. }
  194. }
  195. return false;
  196. }
  197. float4 setInterpolate3(float4 from, float4 to, float t)
  198. {
  199. float s = 1.0f - t;
  200. float4 result;
  201. result = s * from + t * to;
  202. result.w = 0.f;
  203. return result;
  204. }
  205. __kernel void rayCastKernel(
  206. int numRays,
  207. const __global b3RayInfo* rays,
  208. __global b3RayHit* hitResults,
  209. const int numBodies,
  210. __global Body* bodies,
  211. __global Collidable* collidables,
  212. __global const b3GpuFace* faces,
  213. __global const ConvexPolyhedronCL* convexShapes )
  214. {
  215. int i = get_global_id(0);
  216. if (i>=numRays)
  217. return;
  218. hitResults[i].m_hitFraction = 1.f;
  219. float4 rayFrom = rays[i].m_from;
  220. float4 rayTo = rays[i].m_to;
  221. float hitFraction = 1.f;
  222. float4 hitPoint;
  223. float4 hitNormal;
  224. int hitBodyIndex= -1;
  225. int cachedCollidableIndex = -1;
  226. Collidable cachedCollidable;
  227. for (int b=0;b<numBodies;b++)
  228. {
  229. if (hitResults[i].m_hitResult2==b)
  230. continue;
  231. Body body = bodies[b];
  232. float4 pos = body.m_pos;
  233. float4 orn = body.m_quat;
  234. if (cachedCollidableIndex != body.m_collidableIdx)
  235. {
  236. cachedCollidableIndex = body.m_collidableIdx;
  237. cachedCollidable = collidables[cachedCollidableIndex];
  238. }
  239. if (cachedCollidable.m_shapeType == SHAPE_CONVEX_HULL)
  240. {
  241. float4 invPos = (float4)(0,0,0,0);
  242. float4 invOrn = (float4)(0,0,0,0);
  243. float4 rayFromLocal = (float4)(0,0,0,0);
  244. float4 rayToLocal = (float4)(0,0,0,0);
  245. invOrn = qtInvert(orn);
  246. invPos = qtRotate(invOrn, -pos);
  247. rayFromLocal = qtRotate( invOrn, rayFrom ) + invPos;
  248. rayToLocal = qtRotate( invOrn, rayTo) + invPos;
  249. rayFromLocal.w = 0.f;
  250. rayToLocal.w = 0.f;
  251. int numFaces = convexShapes[cachedCollidable.m_shapeIndex].m_numFaces;
  252. int faceOffset = convexShapes[cachedCollidable.m_shapeIndex].m_faceOffset;
  253. if (numFaces)
  254. {
  255. if (rayConvex(rayFromLocal, rayToLocal, numFaces, faceOffset,faces, &hitFraction, &hitNormal))
  256. {
  257. hitBodyIndex = b;
  258. }
  259. }
  260. }
  261. if (cachedCollidable.m_shapeType == SHAPE_SPHERE)
  262. {
  263. float radius = cachedCollidable.m_radius;
  264. if (sphere_intersect(pos, radius, rayFrom, rayTo, &hitFraction))
  265. {
  266. hitBodyIndex = b;
  267. hitNormal = (float4) (hitPoint-bodies[b].m_pos);
  268. }
  269. }
  270. }
  271. if (hitBodyIndex>=0)
  272. {
  273. hitPoint = setInterpolate3(rayFrom, rayTo,hitFraction);
  274. hitResults[i].m_hitFraction = hitFraction;
  275. hitResults[i].m_hitPoint = hitPoint;
  276. hitResults[i].m_hitNormal = normalize(hitNormal);
  277. hitResults[i].m_hitResult0 = hitBodyIndex;
  278. }
  279. }
  280. __kernel void findRayRigidPairIndexRanges(__global int2* rayRigidPairs,
  281. __global int* out_firstRayRigidPairIndexPerRay,
  282. __global int* out_numRayRigidPairsPerRay,
  283. int numRayRigidPairs)
  284. {
  285. int rayRigidPairIndex = get_global_id(0);
  286. if (rayRigidPairIndex >= numRayRigidPairs) return;
  287. int rayIndex = rayRigidPairs[rayRigidPairIndex].x;
  288. atomic_min(&out_firstRayRigidPairIndexPerRay[rayIndex], rayRigidPairIndex);
  289. atomic_inc(&out_numRayRigidPairsPerRay[rayIndex]);
  290. }
  291. __kernel void rayCastPairsKernel(const __global b3RayInfo* rays,
  292. __global b3RayHit* hitResults,
  293. __global int* firstRayRigidPairIndexPerRay,
  294. __global int* numRayRigidPairsPerRay,
  295. __global Body* bodies,
  296. __global Collidable* collidables,
  297. __global const b3GpuFace* faces,
  298. __global const ConvexPolyhedronCL* convexShapes,
  299. __global int2* rayRigidPairs,
  300. int numRays)
  301. {
  302. int i = get_global_id(0);
  303. if (i >= numRays) return;
  304. float4 rayFrom = rays[i].m_from;
  305. float4 rayTo = rays[i].m_to;
  306. hitResults[i].m_hitFraction = 1.f;
  307. float hitFraction = 1.f;
  308. float4 hitPoint;
  309. float4 hitNormal;
  310. int hitBodyIndex = -1;
  311. //
  312. for(int pair = 0; pair < numRayRigidPairsPerRay[i]; ++pair)
  313. {
  314. int rayRigidPairIndex = pair + firstRayRigidPairIndexPerRay[i];
  315. int b = rayRigidPairs[rayRigidPairIndex].y;
  316. if (hitResults[i].m_hitResult2 == b) continue;
  317. Body body = bodies[b];
  318. Collidable rigidCollidable = collidables[body.m_collidableIdx];
  319. float4 pos = body.m_pos;
  320. float4 orn = body.m_quat;
  321. if (rigidCollidable.m_shapeType == SHAPE_CONVEX_HULL)
  322. {
  323. float4 invPos = (float4)(0,0,0,0);
  324. float4 invOrn = (float4)(0,0,0,0);
  325. float4 rayFromLocal = (float4)(0,0,0,0);
  326. float4 rayToLocal = (float4)(0,0,0,0);
  327. invOrn = qtInvert(orn);
  328. invPos = qtRotate(invOrn, -pos);
  329. rayFromLocal = qtRotate( invOrn, rayFrom ) + invPos;
  330. rayToLocal = qtRotate( invOrn, rayTo) + invPos;
  331. rayFromLocal.w = 0.f;
  332. rayToLocal.w = 0.f;
  333. int numFaces = convexShapes[rigidCollidable.m_shapeIndex].m_numFaces;
  334. int faceOffset = convexShapes[rigidCollidable.m_shapeIndex].m_faceOffset;
  335. if (numFaces && rayConvex(rayFromLocal, rayToLocal, numFaces, faceOffset,faces, &hitFraction, &hitNormal))
  336. {
  337. hitBodyIndex = b;
  338. hitPoint = setInterpolate3(rayFrom, rayTo, hitFraction);
  339. }
  340. }
  341. if (rigidCollidable.m_shapeType == SHAPE_SPHERE)
  342. {
  343. float radius = rigidCollidable.m_radius;
  344. if (sphere_intersect(pos, radius, rayFrom, rayTo, &hitFraction))
  345. {
  346. hitBodyIndex = b;
  347. hitPoint = setInterpolate3(rayFrom, rayTo, hitFraction);
  348. hitNormal = (float4) (hitPoint - bodies[b].m_pos);
  349. }
  350. }
  351. }
  352. if (hitBodyIndex >= 0)
  353. {
  354. hitResults[i].m_hitFraction = hitFraction;
  355. hitResults[i].m_hitPoint = hitPoint;
  356. hitResults[i].m_hitNormal = normalize(hitNormal);
  357. hitResults[i].m_hitResult0 = hitBodyIndex;
  358. }
  359. }