solverUtils.cl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /*
  2. Copyright (c) 2013 Advanced Micro Devices, Inc.
  3. This software is provided 'as-is', without any express or implied warranty.
  4. In no event will the authors be held liable for any damages arising from the use of this software.
  5. Permission is granted to anyone to use this software for any purpose,
  6. including commercial applications, and to alter it and redistribute it freely,
  7. subject to the following restrictions:
  8. 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.
  9. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  10. 3. This notice may not be removed or altered from any source distribution.
  11. */
  12. //Originally written by Erwin Coumans
  13. #include "Bullet3Collision/NarrowPhaseCollision/shared/b3Contact4Data.h"
  14. #pragma OPENCL EXTENSION cl_amd_printf : enable
  15. #pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable
  16. #pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
  17. #pragma OPENCL EXTENSION cl_khr_local_int32_extended_atomics : enable
  18. #pragma OPENCL EXTENSION cl_khr_global_int32_extended_atomics : enable
  19. #ifdef cl_ext_atomic_counters_32
  20. #pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable
  21. #else
  22. #define counter32_t volatile global int*
  23. #endif
  24. typedef unsigned int u32;
  25. typedef unsigned short u16;
  26. typedef unsigned char u8;
  27. #define GET_GROUP_IDX get_group_id(0)
  28. #define GET_LOCAL_IDX get_local_id(0)
  29. #define GET_GLOBAL_IDX get_global_id(0)
  30. #define GET_GROUP_SIZE get_local_size(0)
  31. #define GET_NUM_GROUPS get_num_groups(0)
  32. #define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)
  33. #define GROUP_MEM_FENCE mem_fence(CLK_LOCAL_MEM_FENCE)
  34. #define AtomInc(x) atom_inc(&(x))
  35. #define AtomInc1(x, out) out = atom_inc(&(x))
  36. #define AppendInc(x, out) out = atomic_inc(x)
  37. #define AtomAdd(x, value) atom_add(&(x), value)
  38. #define AtomCmpxhg(x, cmp, value) atom_cmpxchg( &(x), cmp, value )
  39. #define AtomXhg(x, value) atom_xchg ( &(x), value )
  40. #define SELECT_UINT4( b, a, condition ) select( b,a,condition )
  41. #define make_float4 (float4)
  42. #define make_float2 (float2)
  43. #define make_uint4 (uint4)
  44. #define make_int4 (int4)
  45. #define make_uint2 (uint2)
  46. #define make_int2 (int2)
  47. #define max2 max
  48. #define min2 min
  49. ///////////////////////////////////////
  50. // Vector
  51. ///////////////////////////////////////
  52. __inline
  53. float fastDiv(float numerator, float denominator)
  54. {
  55. return native_divide(numerator, denominator);
  56. // return numerator/denominator;
  57. }
  58. __inline
  59. float4 fastDiv4(float4 numerator, float4 denominator)
  60. {
  61. return native_divide(numerator, denominator);
  62. }
  63. __inline
  64. float fastSqrtf(float f2)
  65. {
  66. return native_sqrt(f2);
  67. // return sqrt(f2);
  68. }
  69. __inline
  70. float fastRSqrt(float f2)
  71. {
  72. return native_rsqrt(f2);
  73. }
  74. __inline
  75. float fastLength4(float4 v)
  76. {
  77. return fast_length(v);
  78. }
  79. __inline
  80. float4 fastNormalize4(float4 v)
  81. {
  82. return fast_normalize(v);
  83. }
  84. __inline
  85. float sqrtf(float a)
  86. {
  87. // return sqrt(a);
  88. return native_sqrt(a);
  89. }
  90. __inline
  91. float4 cross3(float4 a1, float4 b1)
  92. {
  93. float4 a=make_float4(a1.xyz,0.f);
  94. float4 b=make_float4(b1.xyz,0.f);
  95. //float4 a=a1;
  96. //float4 b=b1;
  97. return cross(a,b);
  98. }
  99. __inline
  100. float dot3F4(float4 a, float4 b)
  101. {
  102. float4 a1 = make_float4(a.xyz,0.f);
  103. float4 b1 = make_float4(b.xyz,0.f);
  104. return dot(a1, b1);
  105. }
  106. __inline
  107. float length3(const float4 a)
  108. {
  109. return sqrtf(dot3F4(a,a));
  110. }
  111. __inline
  112. float dot4(const float4 a, const float4 b)
  113. {
  114. return dot( a, b );
  115. }
  116. // for height
  117. __inline
  118. float dot3w1(const float4 point, const float4 eqn)
  119. {
  120. return dot3F4(point,eqn) + eqn.w;
  121. }
  122. __inline
  123. float4 normalize3(const float4 a)
  124. {
  125. float4 n = make_float4(a.x, a.y, a.z, 0.f);
  126. return fastNormalize4( n );
  127. // float length = sqrtf(dot3F4(a, a));
  128. // return 1.f/length * a;
  129. }
  130. __inline
  131. float4 normalize4(const float4 a)
  132. {
  133. float length = sqrtf(dot4(a, a));
  134. return 1.f/length * a;
  135. }
  136. __inline
  137. float4 createEquation(const float4 a, const float4 b, const float4 c)
  138. {
  139. float4 eqn;
  140. float4 ab = b-a;
  141. float4 ac = c-a;
  142. eqn = normalize3( cross3(ab, ac) );
  143. eqn.w = -dot3F4(eqn,a);
  144. return eqn;
  145. }
  146. ///////////////////////////////////////
  147. // Matrix3x3
  148. ///////////////////////////////////////
  149. typedef struct
  150. {
  151. float4 m_row[3];
  152. }Matrix3x3;
  153. __inline
  154. Matrix3x3 mtZero();
  155. __inline
  156. Matrix3x3 mtIdentity();
  157. __inline
  158. Matrix3x3 mtTranspose(Matrix3x3 m);
  159. __inline
  160. Matrix3x3 mtMul(Matrix3x3 a, Matrix3x3 b);
  161. __inline
  162. float4 mtMul1(Matrix3x3 a, float4 b);
  163. __inline
  164. float4 mtMul3(float4 a, Matrix3x3 b);
  165. __inline
  166. Matrix3x3 mtZero()
  167. {
  168. Matrix3x3 m;
  169. m.m_row[0] = (float4)(0.f);
  170. m.m_row[1] = (float4)(0.f);
  171. m.m_row[2] = (float4)(0.f);
  172. return m;
  173. }
  174. __inline
  175. Matrix3x3 mtIdentity()
  176. {
  177. Matrix3x3 m;
  178. m.m_row[0] = (float4)(1,0,0,0);
  179. m.m_row[1] = (float4)(0,1,0,0);
  180. m.m_row[2] = (float4)(0,0,1,0);
  181. return m;
  182. }
  183. __inline
  184. Matrix3x3 mtTranspose(Matrix3x3 m)
  185. {
  186. Matrix3x3 out;
  187. out.m_row[0] = (float4)(m.m_row[0].x, m.m_row[1].x, m.m_row[2].x, 0.f);
  188. out.m_row[1] = (float4)(m.m_row[0].y, m.m_row[1].y, m.m_row[2].y, 0.f);
  189. out.m_row[2] = (float4)(m.m_row[0].z, m.m_row[1].z, m.m_row[2].z, 0.f);
  190. return out;
  191. }
  192. __inline
  193. Matrix3x3 mtMul(Matrix3x3 a, Matrix3x3 b)
  194. {
  195. Matrix3x3 transB;
  196. transB = mtTranspose( b );
  197. Matrix3x3 ans;
  198. // why this doesn't run when 0ing in the for{}
  199. a.m_row[0].w = 0.f;
  200. a.m_row[1].w = 0.f;
  201. a.m_row[2].w = 0.f;
  202. for(int i=0; i<3; i++)
  203. {
  204. // a.m_row[i].w = 0.f;
  205. ans.m_row[i].x = dot3F4(a.m_row[i],transB.m_row[0]);
  206. ans.m_row[i].y = dot3F4(a.m_row[i],transB.m_row[1]);
  207. ans.m_row[i].z = dot3F4(a.m_row[i],transB.m_row[2]);
  208. ans.m_row[i].w = 0.f;
  209. }
  210. return ans;
  211. }
  212. __inline
  213. float4 mtMul1(Matrix3x3 a, float4 b)
  214. {
  215. float4 ans;
  216. ans.x = dot3F4( a.m_row[0], b );
  217. ans.y = dot3F4( a.m_row[1], b );
  218. ans.z = dot3F4( a.m_row[2], b );
  219. ans.w = 0.f;
  220. return ans;
  221. }
  222. __inline
  223. float4 mtMul3(float4 a, Matrix3x3 b)
  224. {
  225. float4 colx = make_float4(b.m_row[0].x, b.m_row[1].x, b.m_row[2].x, 0);
  226. float4 coly = make_float4(b.m_row[0].y, b.m_row[1].y, b.m_row[2].y, 0);
  227. float4 colz = make_float4(b.m_row[0].z, b.m_row[1].z, b.m_row[2].z, 0);
  228. float4 ans;
  229. ans.x = dot3F4( a, colx );
  230. ans.y = dot3F4( a, coly );
  231. ans.z = dot3F4( a, colz );
  232. return ans;
  233. }
  234. ///////////////////////////////////////
  235. // Quaternion
  236. ///////////////////////////////////////
  237. typedef float4 Quaternion;
  238. __inline
  239. Quaternion qtMul(Quaternion a, Quaternion b);
  240. __inline
  241. Quaternion qtNormalize(Quaternion in);
  242. __inline
  243. float4 qtRotate(Quaternion q, float4 vec);
  244. __inline
  245. Quaternion qtInvert(Quaternion q);
  246. __inline
  247. Quaternion qtMul(Quaternion a, Quaternion b)
  248. {
  249. Quaternion ans;
  250. ans = cross3( a, b );
  251. ans += a.w*b+b.w*a;
  252. // ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);
  253. ans.w = a.w*b.w - dot3F4(a, b);
  254. return ans;
  255. }
  256. __inline
  257. Quaternion qtNormalize(Quaternion in)
  258. {
  259. return fastNormalize4(in);
  260. // in /= length( in );
  261. // return in;
  262. }
  263. __inline
  264. float4 qtRotate(Quaternion q, float4 vec)
  265. {
  266. Quaternion qInv = qtInvert( q );
  267. float4 vcpy = vec;
  268. vcpy.w = 0.f;
  269. float4 out = qtMul(qtMul(q,vcpy),qInv);
  270. return out;
  271. }
  272. __inline
  273. Quaternion qtInvert(Quaternion q)
  274. {
  275. return (Quaternion)(-q.xyz, q.w);
  276. }
  277. __inline
  278. float4 qtInvRotate(const Quaternion q, float4 vec)
  279. {
  280. return qtRotate( qtInvert( q ), vec );
  281. }
  282. #define WG_SIZE 64
  283. typedef struct
  284. {
  285. float4 m_pos;
  286. Quaternion m_quat;
  287. float4 m_linVel;
  288. float4 m_angVel;
  289. u32 m_shapeIdx;
  290. float m_invMass;
  291. float m_restituitionCoeff;
  292. float m_frictionCoeff;
  293. } Body;
  294. typedef struct
  295. {
  296. Matrix3x3 m_invInertia;
  297. Matrix3x3 m_initInvInertia;
  298. } Shape;
  299. typedef struct
  300. {
  301. float4 m_linear;
  302. float4 m_worldPos[4];
  303. float4 m_center;
  304. float m_jacCoeffInv[4];
  305. float m_b[4];
  306. float m_appliedRambdaDt[4];
  307. float m_fJacCoeffInv[2];
  308. float m_fAppliedRambdaDt[2];
  309. u32 m_bodyA;
  310. u32 m_bodyB;
  311. int m_batchIdx;
  312. u32 m_paddings;
  313. } Constraint4;
  314. __kernel void CountBodiesKernel(__global struct b3Contact4Data* manifoldPtr, __global unsigned int* bodyCount, __global int2* contactConstraintOffsets, int numContactManifolds, int fixedBodyIndex)
  315. {
  316. int i = GET_GLOBAL_IDX;
  317. if( i < numContactManifolds)
  318. {
  319. int pa = manifoldPtr[i].m_bodyAPtrAndSignBit;
  320. bool isFixedA = (pa <0) || (pa == fixedBodyIndex);
  321. int bodyIndexA = abs(pa);
  322. if (!isFixedA)
  323. {
  324. AtomInc1(bodyCount[bodyIndexA],contactConstraintOffsets[i].x);
  325. }
  326. barrier(CLK_GLOBAL_MEM_FENCE);
  327. int pb = manifoldPtr[i].m_bodyBPtrAndSignBit;
  328. bool isFixedB = (pb <0) || (pb == fixedBodyIndex);
  329. int bodyIndexB = abs(pb);
  330. if (!isFixedB)
  331. {
  332. AtomInc1(bodyCount[bodyIndexB],contactConstraintOffsets[i].y);
  333. }
  334. }
  335. }
  336. __kernel void ClearVelocitiesKernel(__global float4* linearVelocities,__global float4* angularVelocities, int numSplitBodies)
  337. {
  338. int i = GET_GLOBAL_IDX;
  339. if( i < numSplitBodies)
  340. {
  341. linearVelocities[i] = make_float4(0);
  342. angularVelocities[i] = make_float4(0);
  343. }
  344. }
  345. __kernel void AverageVelocitiesKernel(__global Body* gBodies,__global int* offsetSplitBodies,__global const unsigned int* bodyCount,
  346. __global float4* deltaLinearVelocities, __global float4* deltaAngularVelocities, int numBodies)
  347. {
  348. int i = GET_GLOBAL_IDX;
  349. if (i<numBodies)
  350. {
  351. if (gBodies[i].m_invMass)
  352. {
  353. int bodyOffset = offsetSplitBodies[i];
  354. int count = bodyCount[i];
  355. float factor = 1.f/((float)count);
  356. float4 averageLinVel = make_float4(0.f);
  357. float4 averageAngVel = make_float4(0.f);
  358. for (int j=0;j<count;j++)
  359. {
  360. averageLinVel += deltaLinearVelocities[bodyOffset+j]*factor;
  361. averageAngVel += deltaAngularVelocities[bodyOffset+j]*factor;
  362. }
  363. for (int j=0;j<count;j++)
  364. {
  365. deltaLinearVelocities[bodyOffset+j] = averageLinVel;
  366. deltaAngularVelocities[bodyOffset+j] = averageAngVel;
  367. }
  368. }//bodies[i].m_invMass
  369. }//i<numBodies
  370. }
  371. void setLinearAndAngular( float4 n, float4 r0, float4 r1, float4* linear, float4* angular0, float4* angular1)
  372. {
  373. *linear = make_float4(n.xyz,0.f);
  374. *angular0 = cross3(r0, n);
  375. *angular1 = -cross3(r1, n);
  376. }
  377. float calcRelVel( float4 l0, float4 l1, float4 a0, float4 a1, float4 linVel0, float4 angVel0, float4 linVel1, float4 angVel1 )
  378. {
  379. return dot3F4(l0, linVel0) + dot3F4(a0, angVel0) + dot3F4(l1, linVel1) + dot3F4(a1, angVel1);
  380. }
  381. float calcJacCoeff(const float4 linear0, const float4 linear1, const float4 angular0, const float4 angular1,
  382. float invMass0, const Matrix3x3* invInertia0, float invMass1, const Matrix3x3* invInertia1, float countA, float countB)
  383. {
  384. // linear0,1 are normlized
  385. float jmj0 = invMass0;//dot3F4(linear0, linear0)*invMass0;
  386. float jmj1 = dot3F4(mtMul3(angular0,*invInertia0), angular0);
  387. float jmj2 = invMass1;//dot3F4(linear1, linear1)*invMass1;
  388. float jmj3 = dot3F4(mtMul3(angular1,*invInertia1), angular1);
  389. return -1.f/((jmj0+jmj1)*countA+(jmj2+jmj3)*countB);
  390. }
  391. void btPlaneSpace1 (float4 n, float4* p, float4* q);
  392. void btPlaneSpace1 (float4 n, float4* p, float4* q)
  393. {
  394. if (fabs(n.z) > 0.70710678f) {
  395. // choose p in y-z plane
  396. float a = n.y*n.y + n.z*n.z;
  397. float k = 1.f/sqrt(a);
  398. p[0].x = 0;
  399. p[0].y = -n.z*k;
  400. p[0].z = n.y*k;
  401. // set q = n x p
  402. q[0].x = a*k;
  403. q[0].y = -n.x*p[0].z;
  404. q[0].z = n.x*p[0].y;
  405. }
  406. else {
  407. // choose p in x-y plane
  408. float a = n.x*n.x + n.y*n.y;
  409. float k = 1.f/sqrt(a);
  410. p[0].x = -n.y*k;
  411. p[0].y = n.x*k;
  412. p[0].z = 0;
  413. // set q = n x p
  414. q[0].x = -n.z*p[0].y;
  415. q[0].y = n.z*p[0].x;
  416. q[0].z = a*k;
  417. }
  418. }
  419. void solveContact(__global Constraint4* cs,
  420. float4 posA, float4* linVelA, float4* angVelA, float invMassA, Matrix3x3 invInertiaA,
  421. float4 posB, float4* linVelB, float4* angVelB, float invMassB, Matrix3x3 invInertiaB,
  422. float4* dLinVelA, float4* dAngVelA, float4* dLinVelB, float4* dAngVelB)
  423. {
  424. float minRambdaDt = 0;
  425. float maxRambdaDt = FLT_MAX;
  426. for(int ic=0; ic<4; ic++)
  427. {
  428. if( cs->m_jacCoeffInv[ic] == 0.f ) continue;
  429. float4 angular0, angular1, linear;
  430. float4 r0 = cs->m_worldPos[ic] - posA;
  431. float4 r1 = cs->m_worldPos[ic] - posB;
  432. setLinearAndAngular( cs->m_linear, r0, r1, &linear, &angular0, &angular1 );
  433. float rambdaDt = calcRelVel( cs->m_linear, -cs->m_linear, angular0, angular1,
  434. *linVelA+*dLinVelA, *angVelA+*dAngVelA, *linVelB+*dLinVelB, *angVelB+*dAngVelB ) + cs->m_b[ic];
  435. rambdaDt *= cs->m_jacCoeffInv[ic];
  436. {
  437. float prevSum = cs->m_appliedRambdaDt[ic];
  438. float updated = prevSum;
  439. updated += rambdaDt;
  440. updated = max2( updated, minRambdaDt );
  441. updated = min2( updated, maxRambdaDt );
  442. rambdaDt = updated - prevSum;
  443. cs->m_appliedRambdaDt[ic] = updated;
  444. }
  445. float4 linImp0 = invMassA*linear*rambdaDt;
  446. float4 linImp1 = invMassB*(-linear)*rambdaDt;
  447. float4 angImp0 = mtMul1(invInertiaA, angular0)*rambdaDt;
  448. float4 angImp1 = mtMul1(invInertiaB, angular1)*rambdaDt;
  449. if (invMassA)
  450. {
  451. *dLinVelA += linImp0;
  452. *dAngVelA += angImp0;
  453. }
  454. if (invMassB)
  455. {
  456. *dLinVelB += linImp1;
  457. *dAngVelB += angImp1;
  458. }
  459. }
  460. }
  461. // solveContactConstraint( gBodies, gShapes, &gConstraints[i] ,contactConstraintOffsets,offsetSplitBodies, deltaLinearVelocities, deltaAngularVelocities);
  462. void solveContactConstraint(__global Body* gBodies, __global Shape* gShapes, __global Constraint4* ldsCs,
  463. __global int2* contactConstraintOffsets,__global unsigned int* offsetSplitBodies,
  464. __global float4* deltaLinearVelocities, __global float4* deltaAngularVelocities)
  465. {
  466. //float frictionCoeff = ldsCs[0].m_linear.w;
  467. int aIdx = ldsCs[0].m_bodyA;
  468. int bIdx = ldsCs[0].m_bodyB;
  469. float4 posA = gBodies[aIdx].m_pos;
  470. float4 linVelA = gBodies[aIdx].m_linVel;
  471. float4 angVelA = gBodies[aIdx].m_angVel;
  472. float invMassA = gBodies[aIdx].m_invMass;
  473. Matrix3x3 invInertiaA = gShapes[aIdx].m_invInertia;
  474. float4 posB = gBodies[bIdx].m_pos;
  475. float4 linVelB = gBodies[bIdx].m_linVel;
  476. float4 angVelB = gBodies[bIdx].m_angVel;
  477. float invMassB = gBodies[bIdx].m_invMass;
  478. Matrix3x3 invInertiaB = gShapes[bIdx].m_invInertia;
  479. float4 dLinVelA = make_float4(0,0,0,0);
  480. float4 dAngVelA = make_float4(0,0,0,0);
  481. float4 dLinVelB = make_float4(0,0,0,0);
  482. float4 dAngVelB = make_float4(0,0,0,0);
  483. int bodyOffsetA = offsetSplitBodies[aIdx];
  484. int constraintOffsetA = contactConstraintOffsets[0].x;
  485. int splitIndexA = bodyOffsetA+constraintOffsetA;
  486. if (invMassA)
  487. {
  488. dLinVelA = deltaLinearVelocities[splitIndexA];
  489. dAngVelA = deltaAngularVelocities[splitIndexA];
  490. }
  491. int bodyOffsetB = offsetSplitBodies[bIdx];
  492. int constraintOffsetB = contactConstraintOffsets[0].y;
  493. int splitIndexB= bodyOffsetB+constraintOffsetB;
  494. if (invMassB)
  495. {
  496. dLinVelB = deltaLinearVelocities[splitIndexB];
  497. dAngVelB = deltaAngularVelocities[splitIndexB];
  498. }
  499. solveContact( ldsCs, posA, &linVelA, &angVelA, invMassA, invInertiaA,
  500. posB, &linVelB, &angVelB, invMassB, invInertiaB ,&dLinVelA, &dAngVelA, &dLinVelB, &dAngVelB);
  501. if (invMassA)
  502. {
  503. deltaLinearVelocities[splitIndexA] = dLinVelA;
  504. deltaAngularVelocities[splitIndexA] = dAngVelA;
  505. }
  506. if (invMassB)
  507. {
  508. deltaLinearVelocities[splitIndexB] = dLinVelB;
  509. deltaAngularVelocities[splitIndexB] = dAngVelB;
  510. }
  511. }
  512. __kernel void SolveContactJacobiKernel(__global Constraint4* gConstraints, __global Body* gBodies, __global Shape* gShapes ,
  513. __global int2* contactConstraintOffsets,__global unsigned int* offsetSplitBodies,__global float4* deltaLinearVelocities, __global float4* deltaAngularVelocities,
  514. float deltaTime, float positionDrift, float positionConstraintCoeff, int fixedBodyIndex, int numManifolds
  515. )
  516. {
  517. int i = GET_GLOBAL_IDX;
  518. if (i<numManifolds)
  519. {
  520. solveContactConstraint( gBodies, gShapes, &gConstraints[i] ,&contactConstraintOffsets[i],offsetSplitBodies, deltaLinearVelocities, deltaAngularVelocities);
  521. }
  522. }
  523. void solveFrictionConstraint(__global Body* gBodies, __global Shape* gShapes, __global Constraint4* ldsCs,
  524. __global int2* contactConstraintOffsets,__global unsigned int* offsetSplitBodies,
  525. __global float4* deltaLinearVelocities, __global float4* deltaAngularVelocities)
  526. {
  527. float frictionCoeff = 0.7f;//ldsCs[0].m_linear.w;
  528. int aIdx = ldsCs[0].m_bodyA;
  529. int bIdx = ldsCs[0].m_bodyB;
  530. float4 posA = gBodies[aIdx].m_pos;
  531. float4 linVelA = gBodies[aIdx].m_linVel;
  532. float4 angVelA = gBodies[aIdx].m_angVel;
  533. float invMassA = gBodies[aIdx].m_invMass;
  534. Matrix3x3 invInertiaA = gShapes[aIdx].m_invInertia;
  535. float4 posB = gBodies[bIdx].m_pos;
  536. float4 linVelB = gBodies[bIdx].m_linVel;
  537. float4 angVelB = gBodies[bIdx].m_angVel;
  538. float invMassB = gBodies[bIdx].m_invMass;
  539. Matrix3x3 invInertiaB = gShapes[bIdx].m_invInertia;
  540. float4 dLinVelA = make_float4(0,0,0,0);
  541. float4 dAngVelA = make_float4(0,0,0,0);
  542. float4 dLinVelB = make_float4(0,0,0,0);
  543. float4 dAngVelB = make_float4(0,0,0,0);
  544. int bodyOffsetA = offsetSplitBodies[aIdx];
  545. int constraintOffsetA = contactConstraintOffsets[0].x;
  546. int splitIndexA = bodyOffsetA+constraintOffsetA;
  547. if (invMassA)
  548. {
  549. dLinVelA = deltaLinearVelocities[splitIndexA];
  550. dAngVelA = deltaAngularVelocities[splitIndexA];
  551. }
  552. int bodyOffsetB = offsetSplitBodies[bIdx];
  553. int constraintOffsetB = contactConstraintOffsets[0].y;
  554. int splitIndexB= bodyOffsetB+constraintOffsetB;
  555. if (invMassB)
  556. {
  557. dLinVelB = deltaLinearVelocities[splitIndexB];
  558. dAngVelB = deltaAngularVelocities[splitIndexB];
  559. }
  560. {
  561. float maxRambdaDt[4] = {FLT_MAX,FLT_MAX,FLT_MAX,FLT_MAX};
  562. float minRambdaDt[4] = {0.f,0.f,0.f,0.f};
  563. float sum = 0;
  564. for(int j=0; j<4; j++)
  565. {
  566. sum +=ldsCs[0].m_appliedRambdaDt[j];
  567. }
  568. frictionCoeff = 0.7f;
  569. for(int j=0; j<4; j++)
  570. {
  571. maxRambdaDt[j] = frictionCoeff*sum;
  572. minRambdaDt[j] = -maxRambdaDt[j];
  573. }
  574. // solveFriction( ldsCs, posA, &linVelA, &angVelA, invMassA, invInertiaA,
  575. // posB, &linVelB, &angVelB, invMassB, invInertiaB, maxRambdaDt, minRambdaDt );
  576. {
  577. __global Constraint4* cs = ldsCs;
  578. if( cs->m_fJacCoeffInv[0] == 0 && cs->m_fJacCoeffInv[0] == 0 ) return;
  579. const float4 center = cs->m_center;
  580. float4 n = -cs->m_linear;
  581. float4 tangent[2];
  582. btPlaneSpace1(n,&tangent[0],&tangent[1]);
  583. float4 angular0, angular1, linear;
  584. float4 r0 = center - posA;
  585. float4 r1 = center - posB;
  586. for(int i=0; i<2; i++)
  587. {
  588. setLinearAndAngular( tangent[i], r0, r1, &linear, &angular0, &angular1 );
  589. float rambdaDt = calcRelVel(linear, -linear, angular0, angular1,
  590. linVelA+dLinVelA, angVelA+dAngVelA, linVelB+dLinVelB, angVelB+dAngVelB );
  591. rambdaDt *= cs->m_fJacCoeffInv[i];
  592. {
  593. float prevSum = cs->m_fAppliedRambdaDt[i];
  594. float updated = prevSum;
  595. updated += rambdaDt;
  596. updated = max2( updated, minRambdaDt[i] );
  597. updated = min2( updated, maxRambdaDt[i] );
  598. rambdaDt = updated - prevSum;
  599. cs->m_fAppliedRambdaDt[i] = updated;
  600. }
  601. float4 linImp0 = invMassA*linear*rambdaDt;
  602. float4 linImp1 = invMassB*(-linear)*rambdaDt;
  603. float4 angImp0 = mtMul1(invInertiaA, angular0)*rambdaDt;
  604. float4 angImp1 = mtMul1(invInertiaB, angular1)*rambdaDt;
  605. dLinVelA += linImp0;
  606. dAngVelA += angImp0;
  607. dLinVelB += linImp1;
  608. dAngVelB += angImp1;
  609. }
  610. { // angular damping for point constraint
  611. float4 ab = normalize3( posB - posA );
  612. float4 ac = normalize3( center - posA );
  613. if( dot3F4( ab, ac ) > 0.95f || (invMassA == 0.f || invMassB == 0.f))
  614. {
  615. float angNA = dot3F4( n, angVelA );
  616. float angNB = dot3F4( n, angVelB );
  617. dAngVelA -= (angNA*0.1f)*n;
  618. dAngVelB -= (angNB*0.1f)*n;
  619. }
  620. }
  621. }
  622. }
  623. if (invMassA)
  624. {
  625. deltaLinearVelocities[splitIndexA] = dLinVelA;
  626. deltaAngularVelocities[splitIndexA] = dAngVelA;
  627. }
  628. if (invMassB)
  629. {
  630. deltaLinearVelocities[splitIndexB] = dLinVelB;
  631. deltaAngularVelocities[splitIndexB] = dAngVelB;
  632. }
  633. }
  634. __kernel void SolveFrictionJacobiKernel(__global Constraint4* gConstraints, __global Body* gBodies, __global Shape* gShapes ,
  635. __global int2* contactConstraintOffsets,__global unsigned int* offsetSplitBodies,
  636. __global float4* deltaLinearVelocities, __global float4* deltaAngularVelocities,
  637. float deltaTime, float positionDrift, float positionConstraintCoeff, int fixedBodyIndex, int numManifolds
  638. )
  639. {
  640. int i = GET_GLOBAL_IDX;
  641. if (i<numManifolds)
  642. {
  643. solveFrictionConstraint( gBodies, gShapes, &gConstraints[i] ,&contactConstraintOffsets[i],offsetSplitBodies, deltaLinearVelocities, deltaAngularVelocities);
  644. }
  645. }
  646. __kernel void UpdateBodyVelocitiesKernel(__global Body* gBodies,__global int* offsetSplitBodies,__global const unsigned int* bodyCount,
  647. __global float4* deltaLinearVelocities, __global float4* deltaAngularVelocities, int numBodies)
  648. {
  649. int i = GET_GLOBAL_IDX;
  650. if (i<numBodies)
  651. {
  652. if (gBodies[i].m_invMass)
  653. {
  654. int bodyOffset = offsetSplitBodies[i];
  655. int count = bodyCount[i];
  656. if (count)
  657. {
  658. gBodies[i].m_linVel += deltaLinearVelocities[bodyOffset];
  659. gBodies[i].m_angVel += deltaAngularVelocities[bodyOffset];
  660. }
  661. }
  662. }
  663. }
  664. void setConstraint4( const float4 posA, const float4 linVelA, const float4 angVelA, float invMassA, const Matrix3x3 invInertiaA,
  665. const float4 posB, const float4 linVelB, const float4 angVelB, float invMassB, const Matrix3x3 invInertiaB,
  666. __global struct b3Contact4Data* src, float dt, float positionDrift, float positionConstraintCoeff,float countA, float countB,
  667. Constraint4* dstC )
  668. {
  669. dstC->m_bodyA = abs(src->m_bodyAPtrAndSignBit);
  670. dstC->m_bodyB = abs(src->m_bodyBPtrAndSignBit);
  671. float dtInv = 1.f/dt;
  672. for(int ic=0; ic<4; ic++)
  673. {
  674. dstC->m_appliedRambdaDt[ic] = 0.f;
  675. }
  676. dstC->m_fJacCoeffInv[0] = dstC->m_fJacCoeffInv[1] = 0.f;
  677. dstC->m_linear = src->m_worldNormalOnB;
  678. dstC->m_linear.w = 0.7f ;//src->getFrictionCoeff() );
  679. for(int ic=0; ic<4; ic++)
  680. {
  681. float4 r0 = src->m_worldPosB[ic] - posA;
  682. float4 r1 = src->m_worldPosB[ic] - posB;
  683. if( ic >= src->m_worldNormalOnB.w )//npoints
  684. {
  685. dstC->m_jacCoeffInv[ic] = 0.f;
  686. continue;
  687. }
  688. float relVelN;
  689. {
  690. float4 linear, angular0, angular1;
  691. setLinearAndAngular(src->m_worldNormalOnB, r0, r1, &linear, &angular0, &angular1);
  692. dstC->m_jacCoeffInv[ic] = calcJacCoeff(linear, -linear, angular0, angular1,
  693. invMassA, &invInertiaA, invMassB, &invInertiaB , countA, countB);
  694. relVelN = calcRelVel(linear, -linear, angular0, angular1,
  695. linVelA, angVelA, linVelB, angVelB);
  696. float e = 0.f;//src->getRestituitionCoeff();
  697. if( relVelN*relVelN < 0.004f ) e = 0.f;
  698. dstC->m_b[ic] = e*relVelN;
  699. //float penetration = src->m_worldPosB[ic].w;
  700. dstC->m_b[ic] += (src->m_worldPosB[ic].w + positionDrift)*positionConstraintCoeff*dtInv;
  701. dstC->m_appliedRambdaDt[ic] = 0.f;
  702. }
  703. }
  704. if( src->m_worldNormalOnB.w > 0 )//npoints
  705. { // prepare friction
  706. float4 center = make_float4(0.f);
  707. for(int i=0; i<src->m_worldNormalOnB.w; i++)
  708. center += src->m_worldPosB[i];
  709. center /= (float)src->m_worldNormalOnB.w;
  710. float4 tangent[2];
  711. btPlaneSpace1(-src->m_worldNormalOnB,&tangent[0],&tangent[1]);
  712. float4 r[2];
  713. r[0] = center - posA;
  714. r[1] = center - posB;
  715. for(int i=0; i<2; i++)
  716. {
  717. float4 linear, angular0, angular1;
  718. setLinearAndAngular(tangent[i], r[0], r[1], &linear, &angular0, &angular1);
  719. dstC->m_fJacCoeffInv[i] = calcJacCoeff(linear, -linear, angular0, angular1,
  720. invMassA, &invInertiaA, invMassB, &invInertiaB ,countA, countB);
  721. dstC->m_fAppliedRambdaDt[i] = 0.f;
  722. }
  723. dstC->m_center = center;
  724. }
  725. for(int i=0; i<4; i++)
  726. {
  727. if( i<src->m_worldNormalOnB.w )
  728. {
  729. dstC->m_worldPos[i] = src->m_worldPosB[i];
  730. }
  731. else
  732. {
  733. dstC->m_worldPos[i] = make_float4(0.f);
  734. }
  735. }
  736. }
  737. __kernel
  738. __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
  739. void ContactToConstraintSplitKernel(__global const struct b3Contact4Data* gContact, __global const Body* gBodies, __global const Shape* gShapes, __global Constraint4* gConstraintOut,
  740. __global const unsigned int* bodyCount,
  741. int nContacts,
  742. float dt,
  743. float positionDrift,
  744. float positionConstraintCoeff
  745. )
  746. {
  747. int gIdx = GET_GLOBAL_IDX;
  748. if( gIdx < nContacts )
  749. {
  750. int aIdx = abs(gContact[gIdx].m_bodyAPtrAndSignBit);
  751. int bIdx = abs(gContact[gIdx].m_bodyBPtrAndSignBit);
  752. float4 posA = gBodies[aIdx].m_pos;
  753. float4 linVelA = gBodies[aIdx].m_linVel;
  754. float4 angVelA = gBodies[aIdx].m_angVel;
  755. float invMassA = gBodies[aIdx].m_invMass;
  756. Matrix3x3 invInertiaA = gShapes[aIdx].m_invInertia;
  757. float4 posB = gBodies[bIdx].m_pos;
  758. float4 linVelB = gBodies[bIdx].m_linVel;
  759. float4 angVelB = gBodies[bIdx].m_angVel;
  760. float invMassB = gBodies[bIdx].m_invMass;
  761. Matrix3x3 invInertiaB = gShapes[bIdx].m_invInertia;
  762. Constraint4 cs;
  763. float countA = invMassA != 0.f ? (float)bodyCount[aIdx] : 1;
  764. float countB = invMassB != 0.f ? (float)bodyCount[bIdx] : 1;
  765. setConstraint4( posA, linVelA, angVelA, invMassA, invInertiaA, posB, linVelB, angVelB, invMassB, invInertiaB,
  766. &gContact[gIdx], dt, positionDrift, positionConstraintCoeff,countA,countB,
  767. &cs );
  768. cs.m_batchIdx = gContact[gIdx].m_batchIdx;
  769. gConstraintOut[gIdx] = cs;
  770. }
  771. }