jointSolver.cl 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. #define B3_CONSTRAINT_FLAG_ENABLED 1
  14. #define B3_GPU_POINT2POINT_CONSTRAINT_TYPE 3
  15. #define B3_GPU_FIXED_CONSTRAINT_TYPE 4
  16. #define MOTIONCLAMP 100000 //unused, for debugging/safety in case constraint solver fails
  17. #define B3_INFINITY 1e30f
  18. #define mymake_float4 (float4)
  19. __inline float dot3F4(float4 a, float4 b)
  20. {
  21. float4 a1 = mymake_float4(a.xyz,0.f);
  22. float4 b1 = mymake_float4(b.xyz,0.f);
  23. return dot(a1, b1);
  24. }
  25. typedef float4 Quaternion;
  26. typedef struct
  27. {
  28. float4 m_row[3];
  29. }Matrix3x3;
  30. __inline
  31. float4 mtMul1(Matrix3x3 a, float4 b);
  32. __inline
  33. float4 mtMul3(float4 a, Matrix3x3 b);
  34. __inline
  35. float4 mtMul1(Matrix3x3 a, float4 b)
  36. {
  37. float4 ans;
  38. ans.x = dot3F4( a.m_row[0], b );
  39. ans.y = dot3F4( a.m_row[1], b );
  40. ans.z = dot3F4( a.m_row[2], b );
  41. ans.w = 0.f;
  42. return ans;
  43. }
  44. __inline
  45. float4 mtMul3(float4 a, Matrix3x3 b)
  46. {
  47. float4 colx = mymake_float4(b.m_row[0].x, b.m_row[1].x, b.m_row[2].x, 0);
  48. float4 coly = mymake_float4(b.m_row[0].y, b.m_row[1].y, b.m_row[2].y, 0);
  49. float4 colz = mymake_float4(b.m_row[0].z, b.m_row[1].z, b.m_row[2].z, 0);
  50. float4 ans;
  51. ans.x = dot3F4( a, colx );
  52. ans.y = dot3F4( a, coly );
  53. ans.z = dot3F4( a, colz );
  54. return ans;
  55. }
  56. typedef struct
  57. {
  58. Matrix3x3 m_invInertiaWorld;
  59. Matrix3x3 m_initInvInertia;
  60. } BodyInertia;
  61. typedef struct
  62. {
  63. Matrix3x3 m_basis;//orientation
  64. float4 m_origin;//transform
  65. }b3Transform;
  66. typedef struct
  67. {
  68. // b3Transform m_worldTransformUnused;
  69. float4 m_deltaLinearVelocity;
  70. float4 m_deltaAngularVelocity;
  71. float4 m_angularFactor;
  72. float4 m_linearFactor;
  73. float4 m_invMass;
  74. float4 m_pushVelocity;
  75. float4 m_turnVelocity;
  76. float4 m_linearVelocity;
  77. float4 m_angularVelocity;
  78. union
  79. {
  80. void* m_originalBody;
  81. int m_originalBodyIndex;
  82. };
  83. int padding[3];
  84. } b3GpuSolverBody;
  85. typedef struct
  86. {
  87. float4 m_pos;
  88. Quaternion m_quat;
  89. float4 m_linVel;
  90. float4 m_angVel;
  91. unsigned int m_shapeIdx;
  92. float m_invMass;
  93. float m_restituitionCoeff;
  94. float m_frictionCoeff;
  95. } b3RigidBodyCL;
  96. typedef struct
  97. {
  98. float4 m_relpos1CrossNormal;
  99. float4 m_contactNormal;
  100. float4 m_relpos2CrossNormal;
  101. //float4 m_contactNormal2;//usually m_contactNormal2 == -m_contactNormal
  102. float4 m_angularComponentA;
  103. float4 m_angularComponentB;
  104. float m_appliedPushImpulse;
  105. float m_appliedImpulse;
  106. int m_padding1;
  107. int m_padding2;
  108. float m_friction;
  109. float m_jacDiagABInv;
  110. float m_rhs;
  111. float m_cfm;
  112. float m_lowerLimit;
  113. float m_upperLimit;
  114. float m_rhsPenetration;
  115. int m_originalConstraint;
  116. int m_overrideNumSolverIterations;
  117. int m_frictionIndex;
  118. int m_solverBodyIdA;
  119. int m_solverBodyIdB;
  120. } b3SolverConstraint;
  121. typedef struct
  122. {
  123. int m_bodyAPtrAndSignBit;
  124. int m_bodyBPtrAndSignBit;
  125. int m_originalConstraintIndex;
  126. int m_batchId;
  127. } b3BatchConstraint;
  128. typedef struct
  129. {
  130. int m_constraintType;
  131. int m_rbA;
  132. int m_rbB;
  133. float m_breakingImpulseThreshold;
  134. float4 m_pivotInA;
  135. float4 m_pivotInB;
  136. Quaternion m_relTargetAB;
  137. int m_flags;
  138. int m_padding[3];
  139. } b3GpuGenericConstraint;
  140. /*b3Transform getWorldTransform(b3RigidBodyCL* rb)
  141. {
  142. b3Transform newTrans;
  143. newTrans.setOrigin(rb->m_pos);
  144. newTrans.setRotation(rb->m_quat);
  145. return newTrans;
  146. }*/
  147. __inline
  148. float4 cross3(float4 a, float4 b)
  149. {
  150. return cross(a,b);
  151. }
  152. __inline
  153. float4 fastNormalize4(float4 v)
  154. {
  155. v = mymake_float4(v.xyz,0.f);
  156. return fast_normalize(v);
  157. }
  158. __inline
  159. Quaternion qtMul(Quaternion a, Quaternion b);
  160. __inline
  161. Quaternion qtNormalize(Quaternion in);
  162. __inline
  163. float4 qtRotate(Quaternion q, float4 vec);
  164. __inline
  165. Quaternion qtInvert(Quaternion q);
  166. __inline
  167. Quaternion qtMul(Quaternion a, Quaternion b)
  168. {
  169. Quaternion ans;
  170. ans = cross3( a, b );
  171. ans += a.w*b+b.w*a;
  172. // ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);
  173. ans.w = a.w*b.w - dot3F4(a, b);
  174. return ans;
  175. }
  176. __inline
  177. Quaternion qtNormalize(Quaternion in)
  178. {
  179. return fastNormalize4(in);
  180. // in /= length( in );
  181. // return in;
  182. }
  183. __inline
  184. float4 qtRotate(Quaternion q, float4 vec)
  185. {
  186. Quaternion qInv = qtInvert( q );
  187. float4 vcpy = vec;
  188. vcpy.w = 0.f;
  189. float4 out = qtMul(qtMul(q,vcpy),qInv);
  190. return out;
  191. }
  192. __inline
  193. Quaternion qtInvert(Quaternion q)
  194. {
  195. return (Quaternion)(-q.xyz, q.w);
  196. }
  197. __inline void internalApplyImpulse(__global b3GpuSolverBody* body, float4 linearComponent, float4 angularComponent,float impulseMagnitude)
  198. {
  199. body->m_deltaLinearVelocity += linearComponent*impulseMagnitude*body->m_linearFactor;
  200. body->m_deltaAngularVelocity += angularComponent*(impulseMagnitude*body->m_angularFactor);
  201. }
  202. void resolveSingleConstraintRowGeneric(__global b3GpuSolverBody* body1, __global b3GpuSolverBody* body2, __global b3SolverConstraint* c)
  203. {
  204. float deltaImpulse = c->m_rhs-c->m_appliedImpulse*c->m_cfm;
  205. float deltaVel1Dotn = dot3F4(c->m_contactNormal,body1->m_deltaLinearVelocity) + dot3F4(c->m_relpos1CrossNormal,body1->m_deltaAngularVelocity);
  206. float deltaVel2Dotn = -dot3F4(c->m_contactNormal,body2->m_deltaLinearVelocity) + dot3F4(c->m_relpos2CrossNormal,body2->m_deltaAngularVelocity);
  207. deltaImpulse -= deltaVel1Dotn*c->m_jacDiagABInv;
  208. deltaImpulse -= deltaVel2Dotn*c->m_jacDiagABInv;
  209. float sum = c->m_appliedImpulse + deltaImpulse;
  210. if (sum < c->m_lowerLimit)
  211. {
  212. deltaImpulse = c->m_lowerLimit-c->m_appliedImpulse;
  213. c->m_appliedImpulse = c->m_lowerLimit;
  214. }
  215. else if (sum > c->m_upperLimit)
  216. {
  217. deltaImpulse = c->m_upperLimit-c->m_appliedImpulse;
  218. c->m_appliedImpulse = c->m_upperLimit;
  219. }
  220. else
  221. {
  222. c->m_appliedImpulse = sum;
  223. }
  224. internalApplyImpulse(body1,c->m_contactNormal*body1->m_invMass,c->m_angularComponentA,deltaImpulse);
  225. internalApplyImpulse(body2,-c->m_contactNormal*body2->m_invMass,c->m_angularComponentB,deltaImpulse);
  226. }
  227. __kernel void solveJointConstraintRows(__global b3GpuSolverBody* solverBodies,
  228. __global b3BatchConstraint* batchConstraints,
  229. __global b3SolverConstraint* rows,
  230. __global unsigned int* numConstraintRowsInfo1,
  231. __global unsigned int* rowOffsets,
  232. __global b3GpuGenericConstraint* constraints,
  233. int batchOffset,
  234. int numConstraintsInBatch
  235. )
  236. {
  237. int b = get_global_id(0);
  238. if (b>=numConstraintsInBatch)
  239. return;
  240. __global b3BatchConstraint* c = &batchConstraints[b+batchOffset];
  241. int originalConstraintIndex = c->m_originalConstraintIndex;
  242. if (constraints[originalConstraintIndex].m_flags&B3_CONSTRAINT_FLAG_ENABLED)
  243. {
  244. int numConstraintRows = numConstraintRowsInfo1[originalConstraintIndex];
  245. int rowOffset = rowOffsets[originalConstraintIndex];
  246. for (int jj=0;jj<numConstraintRows;jj++)
  247. {
  248. __global b3SolverConstraint* constraint = &rows[rowOffset+jj];
  249. resolveSingleConstraintRowGeneric(&solverBodies[constraint->m_solverBodyIdA],&solverBodies[constraint->m_solverBodyIdB],constraint);
  250. }
  251. }
  252. };
  253. __kernel void initSolverBodies(__global b3GpuSolverBody* solverBodies,__global b3RigidBodyCL* bodiesCL, int numBodies)
  254. {
  255. int i = get_global_id(0);
  256. if (i>=numBodies)
  257. return;
  258. __global b3GpuSolverBody* solverBody = &solverBodies[i];
  259. __global b3RigidBodyCL* bodyCL = &bodiesCL[i];
  260. solverBody->m_deltaLinearVelocity = (float4)(0.f,0.f,0.f,0.f);
  261. solverBody->m_deltaAngularVelocity = (float4)(0.f,0.f,0.f,0.f);
  262. solverBody->m_pushVelocity = (float4)(0.f,0.f,0.f,0.f);
  263. solverBody->m_pushVelocity = (float4)(0.f,0.f,0.f,0.f);
  264. solverBody->m_invMass = (float4)(bodyCL->m_invMass,bodyCL->m_invMass,bodyCL->m_invMass,0.f);
  265. solverBody->m_originalBodyIndex = i;
  266. solverBody->m_angularFactor = (float4)(1,1,1,0);
  267. solverBody->m_linearFactor = (float4) (1,1,1,0);
  268. solverBody->m_linearVelocity = bodyCL->m_linVel;
  269. solverBody->m_angularVelocity = bodyCL->m_angVel;
  270. }
  271. __kernel void breakViolatedConstraintsKernel(__global b3GpuGenericConstraint* constraints, __global unsigned int* numConstraintRows, __global unsigned int* rowOffsets, __global b3SolverConstraint* rows, int numConstraints)
  272. {
  273. int cid = get_global_id(0);
  274. if (cid>=numConstraints)
  275. return;
  276. int numRows = numConstraintRows[cid];
  277. if (numRows)
  278. {
  279. for (int i=0;i<numRows;i++)
  280. {
  281. int rowIndex = rowOffsets[cid]+i;
  282. float breakingThreshold = constraints[cid].m_breakingImpulseThreshold;
  283. if (fabs(rows[rowIndex].m_appliedImpulse) >= breakingThreshold)
  284. {
  285. constraints[cid].m_flags =0;//&= ~B3_CONSTRAINT_FLAG_ENABLED;
  286. }
  287. }
  288. }
  289. }
  290. __kernel void getInfo1Kernel(__global unsigned int* infos, __global b3GpuGenericConstraint* constraints, int numConstraints)
  291. {
  292. int i = get_global_id(0);
  293. if (i>=numConstraints)
  294. return;
  295. __global b3GpuGenericConstraint* constraint = &constraints[i];
  296. switch (constraint->m_constraintType)
  297. {
  298. case B3_GPU_POINT2POINT_CONSTRAINT_TYPE:
  299. {
  300. infos[i] = 3;
  301. break;
  302. }
  303. case B3_GPU_FIXED_CONSTRAINT_TYPE:
  304. {
  305. infos[i] = 6;
  306. break;
  307. }
  308. default:
  309. {
  310. }
  311. }
  312. }
  313. __kernel void initBatchConstraintsKernel(__global unsigned int* numConstraintRows, __global unsigned int* rowOffsets,
  314. __global b3BatchConstraint* batchConstraints,
  315. __global b3GpuGenericConstraint* constraints,
  316. __global b3RigidBodyCL* bodies,
  317. int numConstraints)
  318. {
  319. int i = get_global_id(0);
  320. if (i>=numConstraints)
  321. return;
  322. int rbA = constraints[i].m_rbA;
  323. int rbB = constraints[i].m_rbB;
  324. batchConstraints[i].m_bodyAPtrAndSignBit = bodies[rbA].m_invMass != 0.f ? rbA : -rbA;
  325. batchConstraints[i].m_bodyBPtrAndSignBit = bodies[rbB].m_invMass != 0.f ? rbB : -rbB;
  326. batchConstraints[i].m_batchId = -1;
  327. batchConstraints[i].m_originalConstraintIndex = i;
  328. }
  329. typedef struct
  330. {
  331. // integrator parameters: frames per second (1/stepsize), default error
  332. // reduction parameter (0..1).
  333. float fps,erp;
  334. // for the first and second body, pointers to two (linear and angular)
  335. // n*3 jacobian sub matrices, stored by rows. these matrices will have
  336. // been initialized to 0 on entry. if the second body is zero then the
  337. // J2xx pointers may be 0.
  338. union
  339. {
  340. __global float4* m_J1linearAxisFloat4;
  341. __global float* m_J1linearAxis;
  342. };
  343. union
  344. {
  345. __global float4* m_J1angularAxisFloat4;
  346. __global float* m_J1angularAxis;
  347. };
  348. union
  349. {
  350. __global float4* m_J2linearAxisFloat4;
  351. __global float* m_J2linearAxis;
  352. };
  353. union
  354. {
  355. __global float4* m_J2angularAxisFloat4;
  356. __global float* m_J2angularAxis;
  357. };
  358. // elements to jump from one row to the next in J's
  359. int rowskip;
  360. // right hand sides of the equation J*v = c + cfm * lambda. cfm is the
  361. // "constraint force mixing" vector. c is set to zero on entry, cfm is
  362. // set to a constant value (typically very small or zero) value on entry.
  363. __global float* m_constraintError;
  364. __global float* cfm;
  365. // lo and hi limits for variables (set to -/+ infinity on entry).
  366. __global float* m_lowerLimit;
  367. __global float* m_upperLimit;
  368. // findex vector for variables. see the LCP solver interface for a
  369. // description of what this does. this is set to -1 on entry.
  370. // note that the returned indexes are relative to the first index of
  371. // the constraint.
  372. __global int *findex;
  373. // number of solver iterations
  374. int m_numIterations;
  375. //damping of the velocity
  376. float m_damping;
  377. } b3GpuConstraintInfo2;
  378. void getSkewSymmetricMatrix(float4 vecIn, __global float4* v0,__global float4* v1,__global float4* v2)
  379. {
  380. *v0 = (float4)(0. ,-vecIn.z ,vecIn.y,0.f);
  381. *v1 = (float4)(vecIn.z ,0. ,-vecIn.x,0.f);
  382. *v2 = (float4)(-vecIn.y ,vecIn.x ,0.f,0.f);
  383. }
  384. void getInfo2Point2Point(__global b3GpuGenericConstraint* constraint,b3GpuConstraintInfo2* info,__global b3RigidBodyCL* bodies)
  385. {
  386. float4 posA = bodies[constraint->m_rbA].m_pos;
  387. Quaternion rotA = bodies[constraint->m_rbA].m_quat;
  388. float4 posB = bodies[constraint->m_rbB].m_pos;
  389. Quaternion rotB = bodies[constraint->m_rbB].m_quat;
  390. // anchor points in global coordinates with respect to body PORs.
  391. // set jacobian
  392. info->m_J1linearAxis[0] = 1;
  393. info->m_J1linearAxis[info->rowskip+1] = 1;
  394. info->m_J1linearAxis[2*info->rowskip+2] = 1;
  395. float4 a1 = qtRotate(rotA,constraint->m_pivotInA);
  396. {
  397. __global float4* angular0 = (__global float4*)(info->m_J1angularAxis);
  398. __global float4* angular1 = (__global float4*)(info->m_J1angularAxis+info->rowskip);
  399. __global float4* angular2 = (__global float4*)(info->m_J1angularAxis+2*info->rowskip);
  400. float4 a1neg = -a1;
  401. getSkewSymmetricMatrix(a1neg,angular0,angular1,angular2);
  402. }
  403. if (info->m_J2linearAxis)
  404. {
  405. info->m_J2linearAxis[0] = -1;
  406. info->m_J2linearAxis[info->rowskip+1] = -1;
  407. info->m_J2linearAxis[2*info->rowskip+2] = -1;
  408. }
  409. float4 a2 = qtRotate(rotB,constraint->m_pivotInB);
  410. {
  411. // float4 a2n = -a2;
  412. __global float4* angular0 = (__global float4*)(info->m_J2angularAxis);
  413. __global float4* angular1 = (__global float4*)(info->m_J2angularAxis+info->rowskip);
  414. __global float4* angular2 = (__global float4*)(info->m_J2angularAxis+2*info->rowskip);
  415. getSkewSymmetricMatrix(a2,angular0,angular1,angular2);
  416. }
  417. // set right hand side
  418. // float currERP = (m_flags & B3_P2P_FLAGS_ERP) ? m_erp : info->erp;
  419. float currERP = info->erp;
  420. float k = info->fps * currERP;
  421. int j;
  422. float4 result = a2 + posB - a1 - posA;
  423. float* resultPtr = &result;
  424. for (j=0; j<3; j++)
  425. {
  426. info->m_constraintError[j*info->rowskip] = k * (resultPtr[j]);
  427. }
  428. }
  429. Quaternion nearest( Quaternion first, Quaternion qd)
  430. {
  431. Quaternion diff,sum;
  432. diff = first- qd;
  433. sum = first + qd;
  434. if( dot(diff,diff) < dot(sum,sum) )
  435. return qd;
  436. return (-qd);
  437. }
  438. float b3Acos(float x)
  439. {
  440. if (x<-1)
  441. x=-1;
  442. if (x>1)
  443. x=1;
  444. return acos(x);
  445. }
  446. float getAngle(Quaternion orn)
  447. {
  448. if (orn.w>=1.f)
  449. orn.w=1.f;
  450. float s = 2.f * b3Acos(orn.w);
  451. return s;
  452. }
  453. void calculateDiffAxisAngleQuaternion( Quaternion orn0,Quaternion orn1a,float4* axis,float* angle)
  454. {
  455. Quaternion orn1 = nearest(orn0,orn1a);
  456. Quaternion dorn = qtMul(orn1,qtInvert(orn0));
  457. *angle = getAngle(dorn);
  458. *axis = (float4)(dorn.x,dorn.y,dorn.z,0.f);
  459. //check for axis length
  460. float len = dot3F4(*axis,*axis);
  461. if (len < FLT_EPSILON*FLT_EPSILON)
  462. *axis = (float4)(1,0,0,0);
  463. else
  464. *axis /= sqrt(len);
  465. }
  466. void getInfo2FixedOrientation(__global b3GpuGenericConstraint* constraint,b3GpuConstraintInfo2* info,__global b3RigidBodyCL* bodies, int start_row)
  467. {
  468. Quaternion worldOrnA = bodies[constraint->m_rbA].m_quat;
  469. Quaternion worldOrnB = bodies[constraint->m_rbB].m_quat;
  470. int s = info->rowskip;
  471. int start_index = start_row * s;
  472. // 3 rows to make body rotations equal
  473. info->m_J1angularAxis[start_index] = 1;
  474. info->m_J1angularAxis[start_index + s + 1] = 1;
  475. info->m_J1angularAxis[start_index + s*2+2] = 1;
  476. if ( info->m_J2angularAxis)
  477. {
  478. info->m_J2angularAxis[start_index] = -1;
  479. info->m_J2angularAxis[start_index + s+1] = -1;
  480. info->m_J2angularAxis[start_index + s*2+2] = -1;
  481. }
  482. float currERP = info->erp;
  483. float k = info->fps * currERP;
  484. float4 diff;
  485. float angle;
  486. float4 qrelCur = qtMul(worldOrnA,qtInvert(worldOrnB));
  487. calculateDiffAxisAngleQuaternion(constraint->m_relTargetAB,qrelCur,&diff,&angle);
  488. diff*=-angle;
  489. float* resultPtr = &diff;
  490. for (int j=0; j<3; j++)
  491. {
  492. info->m_constraintError[(3+j)*info->rowskip] = k * resultPtr[j];
  493. }
  494. }
  495. __kernel void writeBackVelocitiesKernel(__global b3RigidBodyCL* bodies,__global b3GpuSolverBody* solverBodies,int numBodies)
  496. {
  497. int i = get_global_id(0);
  498. if (i>=numBodies)
  499. return;
  500. if (bodies[i].m_invMass)
  501. {
  502. // if (length(solverBodies[i].m_deltaLinearVelocity)<MOTIONCLAMP)
  503. {
  504. bodies[i].m_linVel += solverBodies[i].m_deltaLinearVelocity;
  505. }
  506. // if (length(solverBodies[i].m_deltaAngularVelocity)<MOTIONCLAMP)
  507. {
  508. bodies[i].m_angVel += solverBodies[i].m_deltaAngularVelocity;
  509. }
  510. }
  511. }
  512. __kernel void getInfo2Kernel(__global b3SolverConstraint* solverConstraintRows,
  513. __global unsigned int* infos,
  514. __global unsigned int* constraintRowOffsets,
  515. __global b3GpuGenericConstraint* constraints,
  516. __global b3BatchConstraint* batchConstraints,
  517. __global b3RigidBodyCL* bodies,
  518. __global BodyInertia* inertias,
  519. __global b3GpuSolverBody* solverBodies,
  520. float timeStep,
  521. float globalErp,
  522. float globalCfm,
  523. float globalDamping,
  524. int globalNumIterations,
  525. int numConstraints)
  526. {
  527. int i = get_global_id(0);
  528. if (i>=numConstraints)
  529. return;
  530. //for now, always initialize the batch info
  531. int info1 = infos[i];
  532. __global b3SolverConstraint* currentConstraintRow = &solverConstraintRows[constraintRowOffsets[i]];
  533. __global b3GpuGenericConstraint* constraint = &constraints[i];
  534. __global b3RigidBodyCL* rbA = &bodies[ constraint->m_rbA];
  535. __global b3RigidBodyCL* rbB = &bodies[ constraint->m_rbB];
  536. int solverBodyIdA = constraint->m_rbA;
  537. int solverBodyIdB = constraint->m_rbB;
  538. __global b3GpuSolverBody* bodyAPtr = &solverBodies[solverBodyIdA];
  539. __global b3GpuSolverBody* bodyBPtr = &solverBodies[solverBodyIdB];
  540. if (rbA->m_invMass)
  541. {
  542. batchConstraints[i].m_bodyAPtrAndSignBit = solverBodyIdA;
  543. } else
  544. {
  545. // if (!solverBodyIdA)
  546. // m_staticIdx = 0;
  547. batchConstraints[i].m_bodyAPtrAndSignBit = -solverBodyIdA;
  548. }
  549. if (rbB->m_invMass)
  550. {
  551. batchConstraints[i].m_bodyBPtrAndSignBit = solverBodyIdB;
  552. } else
  553. {
  554. // if (!solverBodyIdB)
  555. // m_staticIdx = 0;
  556. batchConstraints[i].m_bodyBPtrAndSignBit = -solverBodyIdB;
  557. }
  558. if (info1)
  559. {
  560. int overrideNumSolverIterations = 0;//constraint->getOverrideNumSolverIterations() > 0 ? constraint->getOverrideNumSolverIterations() : infoGlobal.m_numIterations;
  561. // if (overrideNumSolverIterations>m_maxOverrideNumSolverIterations)
  562. // m_maxOverrideNumSolverIterations = overrideNumSolverIterations;
  563. int j;
  564. for ( j=0;j<info1;j++)
  565. {
  566. // memset(&currentConstraintRow[j],0,sizeof(b3SolverConstraint));
  567. currentConstraintRow[j].m_angularComponentA = (float4)(0,0,0,0);
  568. currentConstraintRow[j].m_angularComponentB = (float4)(0,0,0,0);
  569. currentConstraintRow[j].m_appliedImpulse = 0.f;
  570. currentConstraintRow[j].m_appliedPushImpulse = 0.f;
  571. currentConstraintRow[j].m_cfm = 0.f;
  572. currentConstraintRow[j].m_contactNormal = (float4)(0,0,0,0);
  573. currentConstraintRow[j].m_friction = 0.f;
  574. currentConstraintRow[j].m_frictionIndex = 0;
  575. currentConstraintRow[j].m_jacDiagABInv = 0.f;
  576. currentConstraintRow[j].m_lowerLimit = 0.f;
  577. currentConstraintRow[j].m_upperLimit = 0.f;
  578. currentConstraintRow[j].m_originalConstraint = i;
  579. currentConstraintRow[j].m_overrideNumSolverIterations = 0;
  580. currentConstraintRow[j].m_relpos1CrossNormal = (float4)(0,0,0,0);
  581. currentConstraintRow[j].m_relpos2CrossNormal = (float4)(0,0,0,0);
  582. currentConstraintRow[j].m_rhs = 0.f;
  583. currentConstraintRow[j].m_rhsPenetration = 0.f;
  584. currentConstraintRow[j].m_solverBodyIdA = 0;
  585. currentConstraintRow[j].m_solverBodyIdB = 0;
  586. currentConstraintRow[j].m_lowerLimit = -B3_INFINITY;
  587. currentConstraintRow[j].m_upperLimit = B3_INFINITY;
  588. currentConstraintRow[j].m_appliedImpulse = 0.f;
  589. currentConstraintRow[j].m_appliedPushImpulse = 0.f;
  590. currentConstraintRow[j].m_solverBodyIdA = solverBodyIdA;
  591. currentConstraintRow[j].m_solverBodyIdB = solverBodyIdB;
  592. currentConstraintRow[j].m_overrideNumSolverIterations = overrideNumSolverIterations;
  593. }
  594. bodyAPtr->m_deltaLinearVelocity = (float4)(0,0,0,0);
  595. bodyAPtr->m_deltaAngularVelocity = (float4)(0,0,0,0);
  596. bodyAPtr->m_pushVelocity = (float4)(0,0,0,0);
  597. bodyAPtr->m_turnVelocity = (float4)(0,0,0,0);
  598. bodyBPtr->m_deltaLinearVelocity = (float4)(0,0,0,0);
  599. bodyBPtr->m_deltaAngularVelocity = (float4)(0,0,0,0);
  600. bodyBPtr->m_pushVelocity = (float4)(0,0,0,0);
  601. bodyBPtr->m_turnVelocity = (float4)(0,0,0,0);
  602. int rowskip = sizeof(b3SolverConstraint)/sizeof(float);//check this
  603. b3GpuConstraintInfo2 info2;
  604. info2.fps = 1.f/timeStep;
  605. info2.erp = globalErp;
  606. info2.m_J1linearAxisFloat4 = &currentConstraintRow->m_contactNormal;
  607. info2.m_J1angularAxisFloat4 = &currentConstraintRow->m_relpos1CrossNormal;
  608. info2.m_J2linearAxisFloat4 = 0;
  609. info2.m_J2angularAxisFloat4 = &currentConstraintRow->m_relpos2CrossNormal;
  610. info2.rowskip = sizeof(b3SolverConstraint)/sizeof(float);//check this
  611. ///the size of b3SolverConstraint needs be a multiple of float
  612. // b3Assert(info2.rowskip*sizeof(float)== sizeof(b3SolverConstraint));
  613. info2.m_constraintError = &currentConstraintRow->m_rhs;
  614. currentConstraintRow->m_cfm = globalCfm;
  615. info2.m_damping = globalDamping;
  616. info2.cfm = &currentConstraintRow->m_cfm;
  617. info2.m_lowerLimit = &currentConstraintRow->m_lowerLimit;
  618. info2.m_upperLimit = &currentConstraintRow->m_upperLimit;
  619. info2.m_numIterations = globalNumIterations;
  620. switch (constraint->m_constraintType)
  621. {
  622. case B3_GPU_POINT2POINT_CONSTRAINT_TYPE:
  623. {
  624. getInfo2Point2Point(constraint,&info2,bodies);
  625. break;
  626. }
  627. case B3_GPU_FIXED_CONSTRAINT_TYPE:
  628. {
  629. getInfo2Point2Point(constraint,&info2,bodies);
  630. getInfo2FixedOrientation(constraint,&info2,bodies,3);
  631. break;
  632. }
  633. default:
  634. {
  635. }
  636. }
  637. ///finalize the constraint setup
  638. for ( j=0;j<info1;j++)
  639. {
  640. __global b3SolverConstraint* solverConstraint = &currentConstraintRow[j];
  641. if (solverConstraint->m_upperLimit>=constraint->m_breakingImpulseThreshold)
  642. {
  643. solverConstraint->m_upperLimit = constraint->m_breakingImpulseThreshold;
  644. }
  645. if (solverConstraint->m_lowerLimit<=-constraint->m_breakingImpulseThreshold)
  646. {
  647. solverConstraint->m_lowerLimit = -constraint->m_breakingImpulseThreshold;
  648. }
  649. // solverConstraint->m_originalContactPoint = constraint;
  650. Matrix3x3 invInertiaWorldA= inertias[constraint->m_rbA].m_invInertiaWorld;
  651. {
  652. //float4 angularFactorA(1,1,1);
  653. float4 ftorqueAxis1 = solverConstraint->m_relpos1CrossNormal;
  654. solverConstraint->m_angularComponentA = mtMul1(invInertiaWorldA,ftorqueAxis1);//*angularFactorA;
  655. }
  656. Matrix3x3 invInertiaWorldB= inertias[constraint->m_rbB].m_invInertiaWorld;
  657. {
  658. float4 ftorqueAxis2 = solverConstraint->m_relpos2CrossNormal;
  659. solverConstraint->m_angularComponentB = mtMul1(invInertiaWorldB,ftorqueAxis2);//*constraint->m_rbB.getAngularFactor();
  660. }
  661. {
  662. //it is ok to use solverConstraint->m_contactNormal instead of -solverConstraint->m_contactNormal
  663. //because it gets multiplied iMJlB
  664. float4 iMJlA = solverConstraint->m_contactNormal*rbA->m_invMass;
  665. float4 iMJaA = mtMul3(solverConstraint->m_relpos1CrossNormal,invInertiaWorldA);
  666. float4 iMJlB = solverConstraint->m_contactNormal*rbB->m_invMass;//sign of normal?
  667. float4 iMJaB = mtMul3(solverConstraint->m_relpos2CrossNormal,invInertiaWorldB);
  668. float sum = dot3F4(iMJlA,solverConstraint->m_contactNormal);
  669. sum += dot3F4(iMJaA,solverConstraint->m_relpos1CrossNormal);
  670. sum += dot3F4(iMJlB,solverConstraint->m_contactNormal);
  671. sum += dot3F4(iMJaB,solverConstraint->m_relpos2CrossNormal);
  672. float fsum = fabs(sum);
  673. if (fsum>FLT_EPSILON)
  674. {
  675. solverConstraint->m_jacDiagABInv = 1.f/sum;
  676. } else
  677. {
  678. solverConstraint->m_jacDiagABInv = 0.f;
  679. }
  680. }
  681. ///fix rhs
  682. ///todo: add force/torque accelerators
  683. {
  684. float rel_vel;
  685. float vel1Dotn = dot3F4(solverConstraint->m_contactNormal,rbA->m_linVel) + dot3F4(solverConstraint->m_relpos1CrossNormal,rbA->m_angVel);
  686. float vel2Dotn = -dot3F4(solverConstraint->m_contactNormal,rbB->m_linVel) + dot3F4(solverConstraint->m_relpos2CrossNormal,rbB->m_angVel);
  687. rel_vel = vel1Dotn+vel2Dotn;
  688. float restitution = 0.f;
  689. float positionalError = solverConstraint->m_rhs;//already filled in by getConstraintInfo2
  690. float velocityError = restitution - rel_vel * info2.m_damping;
  691. float penetrationImpulse = positionalError*solverConstraint->m_jacDiagABInv;
  692. float velocityImpulse = velocityError *solverConstraint->m_jacDiagABInv;
  693. solverConstraint->m_rhs = penetrationImpulse+velocityImpulse;
  694. solverConstraint->m_appliedImpulse = 0.f;
  695. }
  696. }
  697. }
  698. }