solverSetup2.cl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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. #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 a, float4 b)
  92. {
  93. return cross(a,b);
  94. }
  95. __inline
  96. float dot3F4(float4 a, float4 b)
  97. {
  98. float4 a1 = make_float4(a.xyz,0.f);
  99. float4 b1 = make_float4(b.xyz,0.f);
  100. return dot(a1, b1);
  101. }
  102. __inline
  103. float length3(const float4 a)
  104. {
  105. return sqrtf(dot3F4(a,a));
  106. }
  107. __inline
  108. float dot4(const float4 a, const float4 b)
  109. {
  110. return dot( a, b );
  111. }
  112. // for height
  113. __inline
  114. float dot3w1(const float4 point, const float4 eqn)
  115. {
  116. return dot3F4(point,eqn) + eqn.w;
  117. }
  118. __inline
  119. float4 normalize3(const float4 a)
  120. {
  121. float4 n = make_float4(a.x, a.y, a.z, 0.f);
  122. return fastNormalize4( n );
  123. // float length = sqrtf(dot3F4(a, a));
  124. // return 1.f/length * a;
  125. }
  126. __inline
  127. float4 normalize4(const float4 a)
  128. {
  129. float length = sqrtf(dot4(a, a));
  130. return 1.f/length * a;
  131. }
  132. __inline
  133. float4 createEquation(const float4 a, const float4 b, const float4 c)
  134. {
  135. float4 eqn;
  136. float4 ab = b-a;
  137. float4 ac = c-a;
  138. eqn = normalize3( cross3(ab, ac) );
  139. eqn.w = -dot3F4(eqn,a);
  140. return eqn;
  141. }
  142. ///////////////////////////////////////
  143. // Matrix3x3
  144. ///////////////////////////////////////
  145. typedef struct
  146. {
  147. float4 m_row[3];
  148. }Matrix3x3;
  149. __inline
  150. Matrix3x3 mtZero();
  151. __inline
  152. Matrix3x3 mtIdentity();
  153. __inline
  154. Matrix3x3 mtTranspose(Matrix3x3 m);
  155. __inline
  156. Matrix3x3 mtMul(Matrix3x3 a, Matrix3x3 b);
  157. __inline
  158. float4 mtMul1(Matrix3x3 a, float4 b);
  159. __inline
  160. float4 mtMul3(float4 a, Matrix3x3 b);
  161. __inline
  162. Matrix3x3 mtZero()
  163. {
  164. Matrix3x3 m;
  165. m.m_row[0] = (float4)(0.f);
  166. m.m_row[1] = (float4)(0.f);
  167. m.m_row[2] = (float4)(0.f);
  168. return m;
  169. }
  170. __inline
  171. Matrix3x3 mtIdentity()
  172. {
  173. Matrix3x3 m;
  174. m.m_row[0] = (float4)(1,0,0,0);
  175. m.m_row[1] = (float4)(0,1,0,0);
  176. m.m_row[2] = (float4)(0,0,1,0);
  177. return m;
  178. }
  179. __inline
  180. Matrix3x3 mtTranspose(Matrix3x3 m)
  181. {
  182. Matrix3x3 out;
  183. out.m_row[0] = (float4)(m.m_row[0].x, m.m_row[1].x, m.m_row[2].x, 0.f);
  184. out.m_row[1] = (float4)(m.m_row[0].y, m.m_row[1].y, m.m_row[2].y, 0.f);
  185. out.m_row[2] = (float4)(m.m_row[0].z, m.m_row[1].z, m.m_row[2].z, 0.f);
  186. return out;
  187. }
  188. __inline
  189. Matrix3x3 mtMul(Matrix3x3 a, Matrix3x3 b)
  190. {
  191. Matrix3x3 transB;
  192. transB = mtTranspose( b );
  193. Matrix3x3 ans;
  194. // why this doesn't run when 0ing in the for{}
  195. a.m_row[0].w = 0.f;
  196. a.m_row[1].w = 0.f;
  197. a.m_row[2].w = 0.f;
  198. for(int i=0; i<3; i++)
  199. {
  200. // a.m_row[i].w = 0.f;
  201. ans.m_row[i].x = dot3F4(a.m_row[i],transB.m_row[0]);
  202. ans.m_row[i].y = dot3F4(a.m_row[i],transB.m_row[1]);
  203. ans.m_row[i].z = dot3F4(a.m_row[i],transB.m_row[2]);
  204. ans.m_row[i].w = 0.f;
  205. }
  206. return ans;
  207. }
  208. __inline
  209. float4 mtMul1(Matrix3x3 a, float4 b)
  210. {
  211. float4 ans;
  212. ans.x = dot3F4( a.m_row[0], b );
  213. ans.y = dot3F4( a.m_row[1], b );
  214. ans.z = dot3F4( a.m_row[2], b );
  215. ans.w = 0.f;
  216. return ans;
  217. }
  218. __inline
  219. float4 mtMul3(float4 a, Matrix3x3 b)
  220. {
  221. float4 colx = make_float4(b.m_row[0].x, b.m_row[1].x, b.m_row[2].x, 0);
  222. float4 coly = make_float4(b.m_row[0].y, b.m_row[1].y, b.m_row[2].y, 0);
  223. float4 colz = make_float4(b.m_row[0].z, b.m_row[1].z, b.m_row[2].z, 0);
  224. float4 ans;
  225. ans.x = dot3F4( a, colx );
  226. ans.y = dot3F4( a, coly );
  227. ans.z = dot3F4( a, colz );
  228. return ans;
  229. }
  230. ///////////////////////////////////////
  231. // Quaternion
  232. ///////////////////////////////////////
  233. typedef float4 Quaternion;
  234. __inline
  235. Quaternion qtMul(Quaternion a, Quaternion b);
  236. __inline
  237. Quaternion qtNormalize(Quaternion in);
  238. __inline
  239. float4 qtRotate(Quaternion q, float4 vec);
  240. __inline
  241. Quaternion qtInvert(Quaternion q);
  242. __inline
  243. Quaternion qtMul(Quaternion a, Quaternion b)
  244. {
  245. Quaternion ans;
  246. ans = cross3( a, b );
  247. ans += a.w*b+b.w*a;
  248. // ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);
  249. ans.w = a.w*b.w - dot3F4(a, b);
  250. return ans;
  251. }
  252. __inline
  253. Quaternion qtNormalize(Quaternion in)
  254. {
  255. return fastNormalize4(in);
  256. // in /= length( in );
  257. // return in;
  258. }
  259. __inline
  260. float4 qtRotate(Quaternion q, float4 vec)
  261. {
  262. Quaternion qInv = qtInvert( q );
  263. float4 vcpy = vec;
  264. vcpy.w = 0.f;
  265. float4 out = qtMul(qtMul(q,vcpy),qInv);
  266. return out;
  267. }
  268. __inline
  269. Quaternion qtInvert(Quaternion q)
  270. {
  271. return (Quaternion)(-q.xyz, q.w);
  272. }
  273. __inline
  274. float4 qtInvRotate(const Quaternion q, float4 vec)
  275. {
  276. return qtRotate( qtInvert( q ), vec );
  277. }
  278. #define WG_SIZE 64
  279. typedef struct
  280. {
  281. float4 m_pos;
  282. Quaternion m_quat;
  283. float4 m_linVel;
  284. float4 m_angVel;
  285. u32 m_shapeIdx;
  286. float m_invMass;
  287. float m_restituitionCoeff;
  288. float m_frictionCoeff;
  289. } Body;
  290. typedef struct
  291. {
  292. Matrix3x3 m_invInertia;
  293. Matrix3x3 m_initInvInertia;
  294. } Shape;
  295. typedef struct
  296. {
  297. float4 m_linear;
  298. float4 m_worldPos[4];
  299. float4 m_center;
  300. float m_jacCoeffInv[4];
  301. float m_b[4];
  302. float m_appliedRambdaDt[4];
  303. float m_fJacCoeffInv[2];
  304. float m_fAppliedRambdaDt[2];
  305. u32 m_bodyA;
  306. u32 m_bodyB;
  307. int m_batchIdx;
  308. u32 m_paddings[1];
  309. } Constraint4;
  310. typedef struct
  311. {
  312. int m_nConstraints;
  313. int m_start;
  314. int m_batchIdx;
  315. int m_nSplit;
  316. // int m_paddings[1];
  317. } ConstBuffer;
  318. typedef struct
  319. {
  320. int m_solveFriction;
  321. int m_maxBatch; // long batch really kills the performance
  322. int m_batchIdx;
  323. int m_nSplit;
  324. // int m_paddings[1];
  325. } ConstBufferBatchSolve;
  326. typedef struct
  327. {
  328. int m_valInt0;
  329. int m_valInt1;
  330. int m_valInt2;
  331. int m_valInt3;
  332. float m_val0;
  333. float m_val1;
  334. float m_val2;
  335. float m_val3;
  336. } SolverDebugInfo;
  337. // others
  338. __kernel
  339. __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
  340. void ReorderContactKernel(__global struct b3Contact4Data* in, __global struct b3Contact4Data* out, __global int2* sortData, int4 cb )
  341. {
  342. int nContacts = cb.x;
  343. int gIdx = GET_GLOBAL_IDX;
  344. if( gIdx < nContacts )
  345. {
  346. int srcIdx = sortData[gIdx].y;
  347. out[gIdx] = in[srcIdx];
  348. }
  349. }
  350. __kernel __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
  351. void SetDeterminismSortDataChildShapeB(__global struct b3Contact4Data* contactsIn, __global int2* sortDataOut, int nContacts)
  352. {
  353. int gIdx = GET_GLOBAL_IDX;
  354. if( gIdx < nContacts )
  355. {
  356. int2 sd;
  357. sd.x = contactsIn[gIdx].m_childIndexB;
  358. sd.y = gIdx;
  359. sortDataOut[gIdx] = sd;
  360. }
  361. }
  362. __kernel __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
  363. void SetDeterminismSortDataChildShapeA(__global struct b3Contact4Data* contactsIn, __global int2* sortDataInOut, int nContacts)
  364. {
  365. int gIdx = GET_GLOBAL_IDX;
  366. if( gIdx < nContacts )
  367. {
  368. int2 sdIn;
  369. sdIn = sortDataInOut[gIdx];
  370. int2 sdOut;
  371. sdOut.x = contactsIn[sdIn.y].m_childIndexA;
  372. sdOut.y = sdIn.y;
  373. sortDataInOut[gIdx] = sdOut;
  374. }
  375. }
  376. __kernel __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
  377. void SetDeterminismSortDataBodyA(__global struct b3Contact4Data* contactsIn, __global int2* sortDataInOut, int nContacts)
  378. {
  379. int gIdx = GET_GLOBAL_IDX;
  380. if( gIdx < nContacts )
  381. {
  382. int2 sdIn;
  383. sdIn = sortDataInOut[gIdx];
  384. int2 sdOut;
  385. sdOut.x = contactsIn[sdIn.y].m_bodyAPtrAndSignBit;
  386. sdOut.y = sdIn.y;
  387. sortDataInOut[gIdx] = sdOut;
  388. }
  389. }
  390. __kernel
  391. __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
  392. void SetDeterminismSortDataBodyB(__global struct b3Contact4Data* contactsIn, __global int2* sortDataInOut, int nContacts)
  393. {
  394. int gIdx = GET_GLOBAL_IDX;
  395. if( gIdx < nContacts )
  396. {
  397. int2 sdIn;
  398. sdIn = sortDataInOut[gIdx];
  399. int2 sdOut;
  400. sdOut.x = contactsIn[sdIn.y].m_bodyBPtrAndSignBit;
  401. sdOut.y = sdIn.y;
  402. sortDataInOut[gIdx] = sdOut;
  403. }
  404. }
  405. typedef struct
  406. {
  407. int m_nContacts;
  408. int m_staticIdx;
  409. float m_scale;
  410. int m_nSplit;
  411. } ConstBufferSSD;
  412. __constant const int gridTable4x4[] =
  413. {
  414. 0,1,17,16,
  415. 1,2,18,19,
  416. 17,18,32,3,
  417. 16,19,3,34
  418. };
  419. __constant const int gridTable8x8[] =
  420. {
  421. 0, 2, 3, 16, 17, 18, 19, 1,
  422. 66, 64, 80, 67, 82, 81, 65, 83,
  423. 131,144,128,130,147,129,145,146,
  424. 208,195,194,192,193,211,210,209,
  425. 21, 22, 23, 5, 4, 6, 7, 20,
  426. 86, 85, 69, 87, 70, 68, 84, 71,
  427. 151,133,149,150,135,148,132,134,
  428. 197,27,214,213,212,199,198,196
  429. };
  430. #define USE_SPATIAL_BATCHING 1
  431. #define USE_4x4_GRID 1
  432. __kernel
  433. __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
  434. void SetSortDataKernel(__global struct b3Contact4Data* gContact, __global Body* gBodies, __global int2* gSortDataOut,
  435. int nContacts,float scale,int4 nSplit,int staticIdx)
  436. {
  437. int gIdx = GET_GLOBAL_IDX;
  438. if( gIdx < nContacts )
  439. {
  440. int aPtrAndSignBit = gContact[gIdx].m_bodyAPtrAndSignBit;
  441. int bPtrAndSignBit = gContact[gIdx].m_bodyBPtrAndSignBit;
  442. int aIdx = abs(aPtrAndSignBit );
  443. int bIdx = abs(bPtrAndSignBit);
  444. bool aStatic = (aPtrAndSignBit<0) ||(aPtrAndSignBit==staticIdx);
  445. bool bStatic = (bPtrAndSignBit<0) ||(bPtrAndSignBit==staticIdx);
  446. #if USE_SPATIAL_BATCHING
  447. int idx = (aStatic)? bIdx: aIdx;
  448. float4 p = gBodies[idx].m_pos;
  449. int xIdx = (int)((p.x-((p.x<0.f)?1.f:0.f))*scale) & (nSplit.x-1);
  450. int yIdx = (int)((p.y-((p.y<0.f)?1.f:0.f))*scale) & (nSplit.y-1);
  451. int zIdx = (int)((p.z-((p.z<0.f)?1.f:0.f))*scale) & (nSplit.z-1);
  452. int newIndex = (xIdx+yIdx*nSplit.x+zIdx*nSplit.x*nSplit.y);
  453. #else//USE_SPATIAL_BATCHING
  454. #if USE_4x4_GRID
  455. int aa = aIdx&3;
  456. int bb = bIdx&3;
  457. if (aStatic)
  458. aa = bb;
  459. if (bStatic)
  460. bb = aa;
  461. int gridIndex = aa + bb*4;
  462. int newIndex = gridTable4x4[gridIndex];
  463. #else//USE_4x4_GRID
  464. int aa = aIdx&7;
  465. int bb = bIdx&7;
  466. if (aStatic)
  467. aa = bb;
  468. if (bStatic)
  469. bb = aa;
  470. int gridIndex = aa + bb*8;
  471. int newIndex = gridTable8x8[gridIndex];
  472. #endif//USE_4x4_GRID
  473. #endif//USE_SPATIAL_BATCHING
  474. gSortDataOut[gIdx].x = newIndex;
  475. gSortDataOut[gIdx].y = gIdx;
  476. }
  477. else
  478. {
  479. gSortDataOut[gIdx].x = 0xffffffff;
  480. }
  481. }
  482. __kernel
  483. __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
  484. void CopyConstraintKernel(__global struct b3Contact4Data* gIn, __global struct b3Contact4Data* gOut, int4 cb )
  485. {
  486. int gIdx = GET_GLOBAL_IDX;
  487. if( gIdx < cb.x )
  488. {
  489. gOut[gIdx] = gIn[gIdx];
  490. }
  491. }