solveFriction.cl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*
  2. Copyright (c) 2012 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 Takahiro Harada
  13. //#pragma OPENCL EXTENSION cl_amd_printf : enable
  14. #pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable
  15. #pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
  16. #pragma OPENCL EXTENSION cl_khr_local_int32_extended_atomics : enable
  17. #pragma OPENCL EXTENSION cl_khr_global_int32_extended_atomics : enable
  18. #ifdef cl_ext_atomic_counters_32
  19. #pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable
  20. #else
  21. #define counter32_t volatile global int*
  22. #endif
  23. typedef unsigned int u32;
  24. typedef unsigned short u16;
  25. typedef unsigned char u8;
  26. #define GET_GROUP_IDX get_group_id(0)
  27. #define GET_LOCAL_IDX get_local_id(0)
  28. #define GET_GLOBAL_IDX get_global_id(0)
  29. #define GET_GROUP_SIZE get_local_size(0)
  30. #define GET_NUM_GROUPS get_num_groups(0)
  31. #define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)
  32. #define GROUP_MEM_FENCE mem_fence(CLK_LOCAL_MEM_FENCE)
  33. #define AtomInc(x) atom_inc(&(x))
  34. #define AtomInc1(x, out) out = atom_inc(&(x))
  35. #define AppendInc(x, out) out = atomic_inc(x)
  36. #define AtomAdd(x, value) atom_add(&(x), value)
  37. #define AtomCmpxhg(x, cmp, value) atom_cmpxchg( &(x), cmp, value )
  38. #define AtomXhg(x, value) atom_xchg ( &(x), value )
  39. #define SELECT_UINT4( b, a, condition ) select( b,a,condition )
  40. #define mymake_float4 (float4)
  41. //#define make_float2 (float2)
  42. //#define make_uint4 (uint4)
  43. //#define make_int4 (int4)
  44. //#define make_uint2 (uint2)
  45. //#define make_int2 (int2)
  46. #define max2 max
  47. #define min2 min
  48. ///////////////////////////////////////
  49. // Vector
  50. ///////////////////////////////////////
  51. __inline
  52. float4 fastNormalize4(float4 v)
  53. {
  54. return fast_normalize(v);
  55. }
  56. __inline
  57. float4 cross3(float4 a, float4 b)
  58. {
  59. return cross(a,b);
  60. }
  61. __inline
  62. float dot3F4(float4 a, float4 b)
  63. {
  64. float4 a1 = mymake_float4(a.xyz,0.f);
  65. float4 b1 = mymake_float4(b.xyz,0.f);
  66. return dot(a1, b1);
  67. }
  68. __inline
  69. float4 normalize3(const float4 a)
  70. {
  71. float4 n = mymake_float4(a.x, a.y, a.z, 0.f);
  72. return fastNormalize4( n );
  73. // float length = sqrtf(dot3F4(a, a));
  74. // return 1.f/length * a;
  75. }
  76. ///////////////////////////////////////
  77. // Matrix3x3
  78. ///////////////////////////////////////
  79. typedef struct
  80. {
  81. float4 m_row[3];
  82. }Matrix3x3;
  83. __inline
  84. float4 mtMul1(Matrix3x3 a, float4 b);
  85. __inline
  86. float4 mtMul3(float4 a, Matrix3x3 b);
  87. __inline
  88. float4 mtMul1(Matrix3x3 a, float4 b)
  89. {
  90. float4 ans;
  91. ans.x = dot3F4( a.m_row[0], b );
  92. ans.y = dot3F4( a.m_row[1], b );
  93. ans.z = dot3F4( a.m_row[2], b );
  94. ans.w = 0.f;
  95. return ans;
  96. }
  97. __inline
  98. float4 mtMul3(float4 a, Matrix3x3 b)
  99. {
  100. float4 colx = mymake_float4(b.m_row[0].x, b.m_row[1].x, b.m_row[2].x, 0);
  101. float4 coly = mymake_float4(b.m_row[0].y, b.m_row[1].y, b.m_row[2].y, 0);
  102. float4 colz = mymake_float4(b.m_row[0].z, b.m_row[1].z, b.m_row[2].z, 0);
  103. float4 ans;
  104. ans.x = dot3F4( a, colx );
  105. ans.y = dot3F4( a, coly );
  106. ans.z = dot3F4( a, colz );
  107. return ans;
  108. }
  109. ///////////////////////////////////////
  110. // Quaternion
  111. ///////////////////////////////////////
  112. typedef float4 Quaternion;
  113. #define WG_SIZE 64
  114. typedef struct
  115. {
  116. float4 m_pos;
  117. Quaternion m_quat;
  118. float4 m_linVel;
  119. float4 m_angVel;
  120. u32 m_shapeIdx;
  121. float m_invMass;
  122. float m_restituitionCoeff;
  123. float m_frictionCoeff;
  124. } Body;
  125. typedef struct
  126. {
  127. Matrix3x3 m_invInertia;
  128. Matrix3x3 m_initInvInertia;
  129. } Shape;
  130. typedef struct
  131. {
  132. float4 m_linear;
  133. float4 m_worldPos[4];
  134. float4 m_center;
  135. float m_jacCoeffInv[4];
  136. float m_b[4];
  137. float m_appliedRambdaDt[4];
  138. float m_fJacCoeffInv[2];
  139. float m_fAppliedRambdaDt[2];
  140. u32 m_bodyA;
  141. u32 m_bodyB;
  142. int m_batchIdx;
  143. u32 m_paddings[1];
  144. } Constraint4;
  145. typedef struct
  146. {
  147. int m_nConstraints;
  148. int m_start;
  149. int m_batchIdx;
  150. int m_nSplit;
  151. // int m_paddings[1];
  152. } ConstBuffer;
  153. typedef struct
  154. {
  155. int m_solveFriction;
  156. int m_maxBatch; // long batch really kills the performance
  157. int m_batchIdx;
  158. int m_nSplit;
  159. // int m_paddings[1];
  160. } ConstBufferBatchSolve;
  161. void setLinearAndAngular( float4 n, float4 r0, float4 r1, float4* linear, float4* angular0, float4* angular1);
  162. void setLinearAndAngular( float4 n, float4 r0, float4 r1, float4* linear, float4* angular0, float4* angular1)
  163. {
  164. *linear = mymake_float4(-n.xyz,0.f);
  165. *angular0 = -cross3(r0, n);
  166. *angular1 = cross3(r1, n);
  167. }
  168. float calcRelVel( float4 l0, float4 l1, float4 a0, float4 a1, float4 linVel0, float4 angVel0, float4 linVel1, float4 angVel1 );
  169. float calcRelVel( float4 l0, float4 l1, float4 a0, float4 a1, float4 linVel0, float4 angVel0, float4 linVel1, float4 angVel1 )
  170. {
  171. return dot3F4(l0, linVel0) + dot3F4(a0, angVel0) + dot3F4(l1, linVel1) + dot3F4(a1, angVel1);
  172. }
  173. float calcJacCoeff(const float4 linear0, const float4 linear1, const float4 angular0, const float4 angular1,
  174. float invMass0, const Matrix3x3* invInertia0, float invMass1, const Matrix3x3* invInertia1);
  175. float calcJacCoeff(const float4 linear0, const float4 linear1, const float4 angular0, const float4 angular1,
  176. float invMass0, const Matrix3x3* invInertia0, float invMass1, const Matrix3x3* invInertia1)
  177. {
  178. // linear0,1 are normlized
  179. float jmj0 = invMass0;//dot3F4(linear0, linear0)*invMass0;
  180. float jmj1 = dot3F4(mtMul3(angular0,*invInertia0), angular0);
  181. float jmj2 = invMass1;//dot3F4(linear1, linear1)*invMass1;
  182. float jmj3 = dot3F4(mtMul3(angular1,*invInertia1), angular1);
  183. return -1.f/(jmj0+jmj1+jmj2+jmj3);
  184. }
  185. void btPlaneSpace1 (const float4* n, float4* p, float4* q);
  186. void btPlaneSpace1 (const float4* n, float4* p, float4* q)
  187. {
  188. if (fabs(n[0].z) > 0.70710678f) {
  189. // choose p in y-z plane
  190. float a = n[0].y*n[0].y + n[0].z*n[0].z;
  191. float k = 1.f/sqrt(a);
  192. p[0].x = 0;
  193. p[0].y = -n[0].z*k;
  194. p[0].z = n[0].y*k;
  195. // set q = n x p
  196. q[0].x = a*k;
  197. q[0].y = -n[0].x*p[0].z;
  198. q[0].z = n[0].x*p[0].y;
  199. }
  200. else {
  201. // choose p in x-y plane
  202. float a = n[0].x*n[0].x + n[0].y*n[0].y;
  203. float k = 1.f/sqrt(a);
  204. p[0].x = -n[0].y*k;
  205. p[0].y = n[0].x*k;
  206. p[0].z = 0;
  207. // set q = n x p
  208. q[0].x = -n[0].z*p[0].y;
  209. q[0].y = n[0].z*p[0].x;
  210. q[0].z = a*k;
  211. }
  212. }
  213. void solveFrictionConstraint(__global Body* gBodies, __global Shape* gShapes, __global Constraint4* ldsCs);
  214. void solveFrictionConstraint(__global Body* gBodies, __global Shape* gShapes, __global Constraint4* ldsCs)
  215. {
  216. float frictionCoeff = ldsCs[0].m_linear.w;
  217. int aIdx = ldsCs[0].m_bodyA;
  218. int bIdx = ldsCs[0].m_bodyB;
  219. float4 posA = gBodies[aIdx].m_pos;
  220. float4 linVelA = gBodies[aIdx].m_linVel;
  221. float4 angVelA = gBodies[aIdx].m_angVel;
  222. float invMassA = gBodies[aIdx].m_invMass;
  223. Matrix3x3 invInertiaA = gShapes[aIdx].m_invInertia;
  224. float4 posB = gBodies[bIdx].m_pos;
  225. float4 linVelB = gBodies[bIdx].m_linVel;
  226. float4 angVelB = gBodies[bIdx].m_angVel;
  227. float invMassB = gBodies[bIdx].m_invMass;
  228. Matrix3x3 invInertiaB = gShapes[bIdx].m_invInertia;
  229. {
  230. float maxRambdaDt[4] = {FLT_MAX,FLT_MAX,FLT_MAX,FLT_MAX};
  231. float minRambdaDt[4] = {0.f,0.f,0.f,0.f};
  232. float sum = 0;
  233. for(int j=0; j<4; j++)
  234. {
  235. sum +=ldsCs[0].m_appliedRambdaDt[j];
  236. }
  237. frictionCoeff = 0.7f;
  238. for(int j=0; j<4; j++)
  239. {
  240. maxRambdaDt[j] = frictionCoeff*sum;
  241. minRambdaDt[j] = -maxRambdaDt[j];
  242. }
  243. // solveFriction( ldsCs, posA, &linVelA, &angVelA, invMassA, invInertiaA,
  244. // posB, &linVelB, &angVelB, invMassB, invInertiaB, maxRambdaDt, minRambdaDt );
  245. {
  246. __global Constraint4* cs = ldsCs;
  247. if( cs->m_fJacCoeffInv[0] == 0 && cs->m_fJacCoeffInv[0] == 0 ) return;
  248. const float4 center = cs->m_center;
  249. float4 n = -cs->m_linear;
  250. float4 tangent[2];
  251. btPlaneSpace1(&n,&tangent[0],&tangent[1]);
  252. float4 angular0, angular1, linear;
  253. float4 r0 = center - posA;
  254. float4 r1 = center - posB;
  255. for(int i=0; i<2; i++)
  256. {
  257. setLinearAndAngular( tangent[i], r0, r1, &linear, &angular0, &angular1 );
  258. float rambdaDt = calcRelVel(linear, -linear, angular0, angular1,
  259. linVelA, angVelA, linVelB, angVelB );
  260. rambdaDt *= cs->m_fJacCoeffInv[i];
  261. {
  262. float prevSum = cs->m_fAppliedRambdaDt[i];
  263. float updated = prevSum;
  264. updated += rambdaDt;
  265. updated = max2( updated, minRambdaDt[i] );
  266. updated = min2( updated, maxRambdaDt[i] );
  267. rambdaDt = updated - prevSum;
  268. cs->m_fAppliedRambdaDt[i] = updated;
  269. }
  270. float4 linImp0 = invMassA*linear*rambdaDt;
  271. float4 linImp1 = invMassB*(-linear)*rambdaDt;
  272. float4 angImp0 = mtMul1(invInertiaA, angular0)*rambdaDt;
  273. float4 angImp1 = mtMul1(invInertiaB, angular1)*rambdaDt;
  274. linVelA += linImp0;
  275. angVelA += angImp0;
  276. linVelB += linImp1;
  277. angVelB += angImp1;
  278. }
  279. { // angular damping for point constraint
  280. float4 ab = normalize3( posB - posA );
  281. float4 ac = normalize3( center - posA );
  282. if( dot3F4( ab, ac ) > 0.95f || (invMassA == 0.f || invMassB == 0.f))
  283. {
  284. float angNA = dot3F4( n, angVelA );
  285. float angNB = dot3F4( n, angVelB );
  286. angVelA -= (angNA*0.1f)*n;
  287. angVelB -= (angNB*0.1f)*n;
  288. }
  289. }
  290. }
  291. }
  292. if (gBodies[aIdx].m_invMass)
  293. {
  294. gBodies[aIdx].m_linVel = linVelA;
  295. gBodies[aIdx].m_angVel = angVelA;
  296. } else
  297. {
  298. gBodies[aIdx].m_linVel = mymake_float4(0,0,0,0);
  299. gBodies[aIdx].m_angVel = mymake_float4(0,0,0,0);
  300. }
  301. if (gBodies[bIdx].m_invMass)
  302. {
  303. gBodies[bIdx].m_linVel = linVelB;
  304. gBodies[bIdx].m_angVel = angVelB;
  305. } else
  306. {
  307. gBodies[bIdx].m_linVel = mymake_float4(0,0,0,0);
  308. gBodies[bIdx].m_angVel = mymake_float4(0,0,0,0);
  309. }
  310. }
  311. typedef struct
  312. {
  313. int m_valInt0;
  314. int m_valInt1;
  315. int m_valInt2;
  316. int m_valInt3;
  317. float m_val0;
  318. float m_val1;
  319. float m_val2;
  320. float m_val3;
  321. } SolverDebugInfo;
  322. __kernel
  323. __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
  324. void BatchSolveKernelFriction(__global Body* gBodies,
  325. __global Shape* gShapes,
  326. __global Constraint4* gConstraints,
  327. __global int* gN,
  328. __global int* gOffsets,
  329. __global int* batchSizes,
  330. int maxBatch1,
  331. int cellBatch,
  332. int4 nSplit
  333. )
  334. {
  335. //__local int ldsBatchIdx[WG_SIZE+1];
  336. __local int ldsCurBatch;
  337. __local int ldsNextBatch;
  338. __local int ldsStart;
  339. int lIdx = GET_LOCAL_IDX;
  340. int wgIdx = GET_GROUP_IDX;
  341. // int gIdx = GET_GLOBAL_IDX;
  342. // debugInfo[gIdx].m_valInt0 = gIdx;
  343. //debugInfo[gIdx].m_valInt1 = GET_GROUP_SIZE;
  344. int zIdx = (wgIdx/((nSplit.x*nSplit.y)/4))*2+((cellBatch&4)>>2);
  345. int remain= (wgIdx%((nSplit.x*nSplit.y)/4));
  346. int yIdx = (remain/(nSplit.x/2))*2 + ((cellBatch&2)>>1);
  347. int xIdx = (remain%(nSplit.x/2))*2 + (cellBatch&1);
  348. int cellIdx = xIdx+yIdx*nSplit.x+zIdx*(nSplit.x*nSplit.y);
  349. if( gN[cellIdx] == 0 )
  350. return;
  351. int maxBatch = batchSizes[cellIdx];
  352. const int start = gOffsets[cellIdx];
  353. const int end = start + gN[cellIdx];
  354. if( lIdx == 0 )
  355. {
  356. ldsCurBatch = 0;
  357. ldsNextBatch = 0;
  358. ldsStart = start;
  359. }
  360. GROUP_LDS_BARRIER;
  361. int idx=ldsStart+lIdx;
  362. while (ldsCurBatch < maxBatch)
  363. {
  364. for(; idx<end; )
  365. {
  366. if (gConstraints[idx].m_batchIdx == ldsCurBatch)
  367. {
  368. solveFrictionConstraint( gBodies, gShapes, &gConstraints[idx] );
  369. idx+=64;
  370. } else
  371. {
  372. break;
  373. }
  374. }
  375. GROUP_LDS_BARRIER;
  376. if( lIdx == 0 )
  377. {
  378. ldsCurBatch++;
  379. }
  380. GROUP_LDS_BARRIER;
  381. }
  382. }
  383. __kernel void solveSingleFrictionKernel(__global Body* gBodies,
  384. __global Shape* gShapes,
  385. __global Constraint4* gConstraints,
  386. int cellIdx,
  387. int batchOffset,
  388. int numConstraintsInBatch
  389. )
  390. {
  391. int index = get_global_id(0);
  392. if (index < numConstraintsInBatch)
  393. {
  394. int idx=batchOffset+index;
  395. solveFrictionConstraint( gBodies, gShapes, &gConstraints[idx] );
  396. }
  397. }