solverSetup.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. //this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
  2. static const char* solverSetupCL =
  3. "/*\n"
  4. "Copyright (c) 2012 Advanced Micro Devices, Inc. \n"
  5. "This software is provided 'as-is', without any express or implied warranty.\n"
  6. "In no event will the authors be held liable for any damages arising from the use of this software.\n"
  7. "Permission is granted to anyone to use this software for any purpose, \n"
  8. "including commercial applications, and to alter it and redistribute it freely, \n"
  9. "subject to the following restrictions:\n"
  10. "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.\n"
  11. "2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\n"
  12. "3. This notice may not be removed or altered from any source distribution.\n"
  13. "*/\n"
  14. "//Originally written by Takahiro Harada\n"
  15. "#ifndef B3_CONTACT4DATA_H\n"
  16. "#define B3_CONTACT4DATA_H\n"
  17. "#ifndef B3_FLOAT4_H\n"
  18. "#define B3_FLOAT4_H\n"
  19. "#ifndef B3_PLATFORM_DEFINITIONS_H\n"
  20. "#define B3_PLATFORM_DEFINITIONS_H\n"
  21. "struct MyTest\n"
  22. "{\n"
  23. " int bla;\n"
  24. "};\n"
  25. "#ifdef __cplusplus\n"
  26. "#else\n"
  27. "//keep B3_LARGE_FLOAT*B3_LARGE_FLOAT < FLT_MAX\n"
  28. "#define B3_LARGE_FLOAT 1e18f\n"
  29. "#define B3_INFINITY 1e18f\n"
  30. "#define b3Assert(a)\n"
  31. "#define b3ConstArray(a) __global const a*\n"
  32. "#define b3AtomicInc atomic_inc\n"
  33. "#define b3AtomicAdd atomic_add\n"
  34. "#define b3Fabs fabs\n"
  35. "#define b3Sqrt native_sqrt\n"
  36. "#define b3Sin native_sin\n"
  37. "#define b3Cos native_cos\n"
  38. "#define B3_STATIC\n"
  39. "#endif\n"
  40. "#endif\n"
  41. "#ifdef __cplusplus\n"
  42. "#else\n"
  43. " typedef float4 b3Float4;\n"
  44. " #define b3Float4ConstArg const b3Float4\n"
  45. " #define b3MakeFloat4 (float4)\n"
  46. " float b3Dot3F4(b3Float4ConstArg v0,b3Float4ConstArg v1)\n"
  47. " {\n"
  48. " float4 a1 = b3MakeFloat4(v0.xyz,0.f);\n"
  49. " float4 b1 = b3MakeFloat4(v1.xyz,0.f);\n"
  50. " return dot(a1, b1);\n"
  51. " }\n"
  52. " b3Float4 b3Cross3(b3Float4ConstArg v0,b3Float4ConstArg v1)\n"
  53. " {\n"
  54. " float4 a1 = b3MakeFloat4(v0.xyz,0.f);\n"
  55. " float4 b1 = b3MakeFloat4(v1.xyz,0.f);\n"
  56. " return cross(a1, b1);\n"
  57. " }\n"
  58. " #define b3MinFloat4 min\n"
  59. " #define b3MaxFloat4 max\n"
  60. " #define b3Normalized(a) normalize(a)\n"
  61. "#endif \n"
  62. " \n"
  63. "inline bool b3IsAlmostZero(b3Float4ConstArg v)\n"
  64. "{\n"
  65. " if(b3Fabs(v.x)>1e-6 || b3Fabs(v.y)>1e-6 || b3Fabs(v.z)>1e-6) \n"
  66. " return false;\n"
  67. " return true;\n"
  68. "}\n"
  69. "inline int b3MaxDot( b3Float4ConstArg vec, __global const b3Float4* vecArray, int vecLen, float* dotOut )\n"
  70. "{\n"
  71. " float maxDot = -B3_INFINITY;\n"
  72. " int i = 0;\n"
  73. " int ptIndex = -1;\n"
  74. " for( i = 0; i < vecLen; i++ )\n"
  75. " {\n"
  76. " float dot = b3Dot3F4(vecArray[i],vec);\n"
  77. " \n"
  78. " if( dot > maxDot )\n"
  79. " {\n"
  80. " maxDot = dot;\n"
  81. " ptIndex = i;\n"
  82. " }\n"
  83. " }\n"
  84. " b3Assert(ptIndex>=0);\n"
  85. " if (ptIndex<0)\n"
  86. " {\n"
  87. " ptIndex = 0;\n"
  88. " }\n"
  89. " *dotOut = maxDot;\n"
  90. " return ptIndex;\n"
  91. "}\n"
  92. "#endif //B3_FLOAT4_H\n"
  93. "typedef struct b3Contact4Data b3Contact4Data_t;\n"
  94. "struct b3Contact4Data\n"
  95. "{\n"
  96. " b3Float4 m_worldPosB[4];\n"
  97. "// b3Float4 m_localPosA[4];\n"
  98. "// b3Float4 m_localPosB[4];\n"
  99. " b3Float4 m_worldNormalOnB; // w: m_nPoints\n"
  100. " unsigned short m_restituitionCoeffCmp;\n"
  101. " unsigned short m_frictionCoeffCmp;\n"
  102. " int m_batchIdx;\n"
  103. " int m_bodyAPtrAndSignBit;//x:m_bodyAPtr, y:m_bodyBPtr\n"
  104. " int m_bodyBPtrAndSignBit;\n"
  105. " int m_childIndexA;\n"
  106. " int m_childIndexB;\n"
  107. " int m_unused1;\n"
  108. " int m_unused2;\n"
  109. "};\n"
  110. "inline int b3Contact4Data_getNumPoints(const struct b3Contact4Data* contact)\n"
  111. "{\n"
  112. " return (int)contact->m_worldNormalOnB.w;\n"
  113. "};\n"
  114. "inline void b3Contact4Data_setNumPoints(struct b3Contact4Data* contact, int numPoints)\n"
  115. "{\n"
  116. " contact->m_worldNormalOnB.w = (float)numPoints;\n"
  117. "};\n"
  118. "#endif //B3_CONTACT4DATA_H\n"
  119. "#ifndef B3_CONTACT_CONSTRAINT5_H\n"
  120. "#define B3_CONTACT_CONSTRAINT5_H\n"
  121. "#ifndef B3_FLOAT4_H\n"
  122. "#ifdef __cplusplus\n"
  123. "#else\n"
  124. "#endif \n"
  125. "#endif //B3_FLOAT4_H\n"
  126. "typedef struct b3ContactConstraint4 b3ContactConstraint4_t;\n"
  127. "struct b3ContactConstraint4\n"
  128. "{\n"
  129. " b3Float4 m_linear;//normal?\n"
  130. " b3Float4 m_worldPos[4];\n"
  131. " b3Float4 m_center; // friction\n"
  132. " float m_jacCoeffInv[4];\n"
  133. " float m_b[4];\n"
  134. " float m_appliedRambdaDt[4];\n"
  135. " float m_fJacCoeffInv[2]; // friction\n"
  136. " float m_fAppliedRambdaDt[2]; // friction\n"
  137. " unsigned int m_bodyA;\n"
  138. " unsigned int m_bodyB;\n"
  139. " int m_batchIdx;\n"
  140. " unsigned int m_paddings;\n"
  141. "};\n"
  142. "//inline void setFrictionCoeff(float value) { m_linear[3] = value; }\n"
  143. "inline float b3GetFrictionCoeff(b3ContactConstraint4_t* constraint) \n"
  144. "{\n"
  145. " return constraint->m_linear.w; \n"
  146. "}\n"
  147. "#endif //B3_CONTACT_CONSTRAINT5_H\n"
  148. "#ifndef B3_RIGIDBODY_DATA_H\n"
  149. "#define B3_RIGIDBODY_DATA_H\n"
  150. "#ifndef B3_FLOAT4_H\n"
  151. "#ifdef __cplusplus\n"
  152. "#else\n"
  153. "#endif \n"
  154. "#endif //B3_FLOAT4_H\n"
  155. "#ifndef B3_QUAT_H\n"
  156. "#define B3_QUAT_H\n"
  157. "#ifndef B3_PLATFORM_DEFINITIONS_H\n"
  158. "#ifdef __cplusplus\n"
  159. "#else\n"
  160. "#endif\n"
  161. "#endif\n"
  162. "#ifndef B3_FLOAT4_H\n"
  163. "#ifdef __cplusplus\n"
  164. "#else\n"
  165. "#endif \n"
  166. "#endif //B3_FLOAT4_H\n"
  167. "#ifdef __cplusplus\n"
  168. "#else\n"
  169. " typedef float4 b3Quat;\n"
  170. " #define b3QuatConstArg const b3Quat\n"
  171. " \n"
  172. " \n"
  173. "inline float4 b3FastNormalize4(float4 v)\n"
  174. "{\n"
  175. " v = (float4)(v.xyz,0.f);\n"
  176. " return fast_normalize(v);\n"
  177. "}\n"
  178. " \n"
  179. "inline b3Quat b3QuatMul(b3Quat a, b3Quat b);\n"
  180. "inline b3Quat b3QuatNormalized(b3QuatConstArg in);\n"
  181. "inline b3Quat b3QuatRotate(b3QuatConstArg q, b3QuatConstArg vec);\n"
  182. "inline b3Quat b3QuatInvert(b3QuatConstArg q);\n"
  183. "inline b3Quat b3QuatInverse(b3QuatConstArg q);\n"
  184. "inline b3Quat b3QuatMul(b3QuatConstArg a, b3QuatConstArg b)\n"
  185. "{\n"
  186. " b3Quat ans;\n"
  187. " ans = b3Cross3( a, b );\n"
  188. " ans += a.w*b+b.w*a;\n"
  189. "// ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);\n"
  190. " ans.w = a.w*b.w - b3Dot3F4(a, b);\n"
  191. " return ans;\n"
  192. "}\n"
  193. "inline b3Quat b3QuatNormalized(b3QuatConstArg in)\n"
  194. "{\n"
  195. " b3Quat q;\n"
  196. " q=in;\n"
  197. " //return b3FastNormalize4(in);\n"
  198. " float len = native_sqrt(dot(q, q));\n"
  199. " if(len > 0.f)\n"
  200. " {\n"
  201. " q *= 1.f / len;\n"
  202. " }\n"
  203. " else\n"
  204. " {\n"
  205. " q.x = q.y = q.z = 0.f;\n"
  206. " q.w = 1.f;\n"
  207. " }\n"
  208. " return q;\n"
  209. "}\n"
  210. "inline float4 b3QuatRotate(b3QuatConstArg q, b3QuatConstArg vec)\n"
  211. "{\n"
  212. " b3Quat qInv = b3QuatInvert( q );\n"
  213. " float4 vcpy = vec;\n"
  214. " vcpy.w = 0.f;\n"
  215. " float4 out = b3QuatMul(b3QuatMul(q,vcpy),qInv);\n"
  216. " return out;\n"
  217. "}\n"
  218. "inline b3Quat b3QuatInverse(b3QuatConstArg q)\n"
  219. "{\n"
  220. " return (b3Quat)(-q.xyz, q.w);\n"
  221. "}\n"
  222. "inline b3Quat b3QuatInvert(b3QuatConstArg q)\n"
  223. "{\n"
  224. " return (b3Quat)(-q.xyz, q.w);\n"
  225. "}\n"
  226. "inline float4 b3QuatInvRotate(b3QuatConstArg q, b3QuatConstArg vec)\n"
  227. "{\n"
  228. " return b3QuatRotate( b3QuatInvert( q ), vec );\n"
  229. "}\n"
  230. "inline b3Float4 b3TransformPoint(b3Float4ConstArg point, b3Float4ConstArg translation, b3QuatConstArg orientation)\n"
  231. "{\n"
  232. " return b3QuatRotate( orientation, point ) + (translation);\n"
  233. "}\n"
  234. " \n"
  235. "#endif \n"
  236. "#endif //B3_QUAT_H\n"
  237. "#ifndef B3_MAT3x3_H\n"
  238. "#define B3_MAT3x3_H\n"
  239. "#ifndef B3_QUAT_H\n"
  240. "#ifdef __cplusplus\n"
  241. "#else\n"
  242. "#endif \n"
  243. "#endif //B3_QUAT_H\n"
  244. "#ifdef __cplusplus\n"
  245. "#else\n"
  246. "typedef struct\n"
  247. "{\n"
  248. " b3Float4 m_row[3];\n"
  249. "}b3Mat3x3;\n"
  250. "#define b3Mat3x3ConstArg const b3Mat3x3\n"
  251. "#define b3GetRow(m,row) (m.m_row[row])\n"
  252. "inline b3Mat3x3 b3QuatGetRotationMatrix(b3Quat quat)\n"
  253. "{\n"
  254. " b3Float4 quat2 = (b3Float4)(quat.x*quat.x, quat.y*quat.y, quat.z*quat.z, 0.f);\n"
  255. " b3Mat3x3 out;\n"
  256. " out.m_row[0].x=1-2*quat2.y-2*quat2.z;\n"
  257. " out.m_row[0].y=2*quat.x*quat.y-2*quat.w*quat.z;\n"
  258. " out.m_row[0].z=2*quat.x*quat.z+2*quat.w*quat.y;\n"
  259. " out.m_row[0].w = 0.f;\n"
  260. " out.m_row[1].x=2*quat.x*quat.y+2*quat.w*quat.z;\n"
  261. " out.m_row[1].y=1-2*quat2.x-2*quat2.z;\n"
  262. " out.m_row[1].z=2*quat.y*quat.z-2*quat.w*quat.x;\n"
  263. " out.m_row[1].w = 0.f;\n"
  264. " out.m_row[2].x=2*quat.x*quat.z-2*quat.w*quat.y;\n"
  265. " out.m_row[2].y=2*quat.y*quat.z+2*quat.w*quat.x;\n"
  266. " out.m_row[2].z=1-2*quat2.x-2*quat2.y;\n"
  267. " out.m_row[2].w = 0.f;\n"
  268. " return out;\n"
  269. "}\n"
  270. "inline b3Mat3x3 b3AbsoluteMat3x3(b3Mat3x3ConstArg matIn)\n"
  271. "{\n"
  272. " b3Mat3x3 out;\n"
  273. " out.m_row[0] = fabs(matIn.m_row[0]);\n"
  274. " out.m_row[1] = fabs(matIn.m_row[1]);\n"
  275. " out.m_row[2] = fabs(matIn.m_row[2]);\n"
  276. " return out;\n"
  277. "}\n"
  278. "__inline\n"
  279. "b3Mat3x3 mtZero();\n"
  280. "__inline\n"
  281. "b3Mat3x3 mtIdentity();\n"
  282. "__inline\n"
  283. "b3Mat3x3 mtTranspose(b3Mat3x3 m);\n"
  284. "__inline\n"
  285. "b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b);\n"
  286. "__inline\n"
  287. "b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b);\n"
  288. "__inline\n"
  289. "b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b);\n"
  290. "__inline\n"
  291. "b3Mat3x3 mtZero()\n"
  292. "{\n"
  293. " b3Mat3x3 m;\n"
  294. " m.m_row[0] = (b3Float4)(0.f);\n"
  295. " m.m_row[1] = (b3Float4)(0.f);\n"
  296. " m.m_row[2] = (b3Float4)(0.f);\n"
  297. " return m;\n"
  298. "}\n"
  299. "__inline\n"
  300. "b3Mat3x3 mtIdentity()\n"
  301. "{\n"
  302. " b3Mat3x3 m;\n"
  303. " m.m_row[0] = (b3Float4)(1,0,0,0);\n"
  304. " m.m_row[1] = (b3Float4)(0,1,0,0);\n"
  305. " m.m_row[2] = (b3Float4)(0,0,1,0);\n"
  306. " return m;\n"
  307. "}\n"
  308. "__inline\n"
  309. "b3Mat3x3 mtTranspose(b3Mat3x3 m)\n"
  310. "{\n"
  311. " b3Mat3x3 out;\n"
  312. " out.m_row[0] = (b3Float4)(m.m_row[0].x, m.m_row[1].x, m.m_row[2].x, 0.f);\n"
  313. " out.m_row[1] = (b3Float4)(m.m_row[0].y, m.m_row[1].y, m.m_row[2].y, 0.f);\n"
  314. " out.m_row[2] = (b3Float4)(m.m_row[0].z, m.m_row[1].z, m.m_row[2].z, 0.f);\n"
  315. " return out;\n"
  316. "}\n"
  317. "__inline\n"
  318. "b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b)\n"
  319. "{\n"
  320. " b3Mat3x3 transB;\n"
  321. " transB = mtTranspose( b );\n"
  322. " b3Mat3x3 ans;\n"
  323. " // why this doesn't run when 0ing in the for{}\n"
  324. " a.m_row[0].w = 0.f;\n"
  325. " a.m_row[1].w = 0.f;\n"
  326. " a.m_row[2].w = 0.f;\n"
  327. " for(int i=0; i<3; i++)\n"
  328. " {\n"
  329. "// a.m_row[i].w = 0.f;\n"
  330. " ans.m_row[i].x = b3Dot3F4(a.m_row[i],transB.m_row[0]);\n"
  331. " ans.m_row[i].y = b3Dot3F4(a.m_row[i],transB.m_row[1]);\n"
  332. " ans.m_row[i].z = b3Dot3F4(a.m_row[i],transB.m_row[2]);\n"
  333. " ans.m_row[i].w = 0.f;\n"
  334. " }\n"
  335. " return ans;\n"
  336. "}\n"
  337. "__inline\n"
  338. "b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b)\n"
  339. "{\n"
  340. " b3Float4 ans;\n"
  341. " ans.x = b3Dot3F4( a.m_row[0], b );\n"
  342. " ans.y = b3Dot3F4( a.m_row[1], b );\n"
  343. " ans.z = b3Dot3F4( a.m_row[2], b );\n"
  344. " ans.w = 0.f;\n"
  345. " return ans;\n"
  346. "}\n"
  347. "__inline\n"
  348. "b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b)\n"
  349. "{\n"
  350. " b3Float4 colx = b3MakeFloat4(b.m_row[0].x, b.m_row[1].x, b.m_row[2].x, 0);\n"
  351. " b3Float4 coly = b3MakeFloat4(b.m_row[0].y, b.m_row[1].y, b.m_row[2].y, 0);\n"
  352. " b3Float4 colz = b3MakeFloat4(b.m_row[0].z, b.m_row[1].z, b.m_row[2].z, 0);\n"
  353. " b3Float4 ans;\n"
  354. " ans.x = b3Dot3F4( a, colx );\n"
  355. " ans.y = b3Dot3F4( a, coly );\n"
  356. " ans.z = b3Dot3F4( a, colz );\n"
  357. " return ans;\n"
  358. "}\n"
  359. "#endif\n"
  360. "#endif //B3_MAT3x3_H\n"
  361. "typedef struct b3RigidBodyData b3RigidBodyData_t;\n"
  362. "struct b3RigidBodyData\n"
  363. "{\n"
  364. " b3Float4 m_pos;\n"
  365. " b3Quat m_quat;\n"
  366. " b3Float4 m_linVel;\n"
  367. " b3Float4 m_angVel;\n"
  368. " int m_collidableIdx;\n"
  369. " float m_invMass;\n"
  370. " float m_restituitionCoeff;\n"
  371. " float m_frictionCoeff;\n"
  372. "};\n"
  373. "typedef struct b3InertiaData b3InertiaData_t;\n"
  374. "struct b3InertiaData\n"
  375. "{\n"
  376. " b3Mat3x3 m_invInertiaWorld;\n"
  377. " b3Mat3x3 m_initInvInertia;\n"
  378. "};\n"
  379. "#endif //B3_RIGIDBODY_DATA_H\n"
  380. " \n"
  381. "void b3PlaneSpace1 (b3Float4ConstArg n, b3Float4* p, b3Float4* q);\n"
  382. " void b3PlaneSpace1 (b3Float4ConstArg n, b3Float4* p, b3Float4* q)\n"
  383. "{\n"
  384. " if (b3Fabs(n.z) > 0.70710678f) {\n"
  385. " // choose p in y-z plane\n"
  386. " float a = n.y*n.y + n.z*n.z;\n"
  387. " float k = 1.f/sqrt(a);\n"
  388. " p[0].x = 0;\n"
  389. " p[0].y = -n.z*k;\n"
  390. " p[0].z = n.y*k;\n"
  391. " // set q = n x p\n"
  392. " q[0].x = a*k;\n"
  393. " q[0].y = -n.x*p[0].z;\n"
  394. " q[0].z = n.x*p[0].y;\n"
  395. " }\n"
  396. " else {\n"
  397. " // choose p in x-y plane\n"
  398. " float a = n.x*n.x + n.y*n.y;\n"
  399. " float k = 1.f/sqrt(a);\n"
  400. " p[0].x = -n.y*k;\n"
  401. " p[0].y = n.x*k;\n"
  402. " p[0].z = 0;\n"
  403. " // set q = n x p\n"
  404. " q[0].x = -n.z*p[0].y;\n"
  405. " q[0].y = n.z*p[0].x;\n"
  406. " q[0].z = a*k;\n"
  407. " }\n"
  408. "}\n"
  409. " \n"
  410. "void setLinearAndAngular( b3Float4ConstArg n, b3Float4ConstArg r0, b3Float4ConstArg r1, b3Float4* linear, b3Float4* angular0, b3Float4* angular1)\n"
  411. "{\n"
  412. " *linear = b3MakeFloat4(n.x,n.y,n.z,0.f);\n"
  413. " *angular0 = b3Cross3(r0, n);\n"
  414. " *angular1 = -b3Cross3(r1, n);\n"
  415. "}\n"
  416. "float calcRelVel( b3Float4ConstArg l0, b3Float4ConstArg l1, b3Float4ConstArg a0, b3Float4ConstArg a1, b3Float4ConstArg linVel0,\n"
  417. " b3Float4ConstArg angVel0, b3Float4ConstArg linVel1, b3Float4ConstArg angVel1 )\n"
  418. "{\n"
  419. " return b3Dot3F4(l0, linVel0) + b3Dot3F4(a0, angVel0) + b3Dot3F4(l1, linVel1) + b3Dot3F4(a1, angVel1);\n"
  420. "}\n"
  421. "float calcJacCoeff(b3Float4ConstArg linear0, b3Float4ConstArg linear1, b3Float4ConstArg angular0, b3Float4ConstArg angular1,\n"
  422. " float invMass0, const b3Mat3x3* invInertia0, float invMass1, const b3Mat3x3* invInertia1)\n"
  423. "{\n"
  424. " // linear0,1 are normlized\n"
  425. " float jmj0 = invMass0;//b3Dot3F4(linear0, linear0)*invMass0;\n"
  426. " float jmj1 = b3Dot3F4(mtMul3(angular0,*invInertia0), angular0);\n"
  427. " float jmj2 = invMass1;//b3Dot3F4(linear1, linear1)*invMass1;\n"
  428. " float jmj3 = b3Dot3F4(mtMul3(angular1,*invInertia1), angular1);\n"
  429. " return -1.f/(jmj0+jmj1+jmj2+jmj3);\n"
  430. "}\n"
  431. "void setConstraint4( b3Float4ConstArg posA, b3Float4ConstArg linVelA, b3Float4ConstArg angVelA, float invMassA, b3Mat3x3ConstArg invInertiaA,\n"
  432. " b3Float4ConstArg posB, b3Float4ConstArg linVelB, b3Float4ConstArg angVelB, float invMassB, b3Mat3x3ConstArg invInertiaB, \n"
  433. " __global struct b3Contact4Data* src, float dt, float positionDrift, float positionConstraintCoeff,\n"
  434. " b3ContactConstraint4_t* dstC )\n"
  435. "{\n"
  436. " dstC->m_bodyA = abs(src->m_bodyAPtrAndSignBit);\n"
  437. " dstC->m_bodyB = abs(src->m_bodyBPtrAndSignBit);\n"
  438. " float dtInv = 1.f/dt;\n"
  439. " for(int ic=0; ic<4; ic++)\n"
  440. " {\n"
  441. " dstC->m_appliedRambdaDt[ic] = 0.f;\n"
  442. " }\n"
  443. " dstC->m_fJacCoeffInv[0] = dstC->m_fJacCoeffInv[1] = 0.f;\n"
  444. " dstC->m_linear = src->m_worldNormalOnB;\n"
  445. " dstC->m_linear.w = 0.7f ;//src->getFrictionCoeff() );\n"
  446. " for(int ic=0; ic<4; ic++)\n"
  447. " {\n"
  448. " b3Float4 r0 = src->m_worldPosB[ic] - posA;\n"
  449. " b3Float4 r1 = src->m_worldPosB[ic] - posB;\n"
  450. " if( ic >= src->m_worldNormalOnB.w )//npoints\n"
  451. " {\n"
  452. " dstC->m_jacCoeffInv[ic] = 0.f;\n"
  453. " continue;\n"
  454. " }\n"
  455. " float relVelN;\n"
  456. " {\n"
  457. " b3Float4 linear, angular0, angular1;\n"
  458. " setLinearAndAngular(src->m_worldNormalOnB, r0, r1, &linear, &angular0, &angular1);\n"
  459. " dstC->m_jacCoeffInv[ic] = calcJacCoeff(linear, -linear, angular0, angular1,\n"
  460. " invMassA, &invInertiaA, invMassB, &invInertiaB );\n"
  461. " relVelN = calcRelVel(linear, -linear, angular0, angular1,\n"
  462. " linVelA, angVelA, linVelB, angVelB);\n"
  463. " float e = 0.f;//src->getRestituitionCoeff();\n"
  464. " if( relVelN*relVelN < 0.004f ) e = 0.f;\n"
  465. " dstC->m_b[ic] = e*relVelN;\n"
  466. " //float penetration = src->m_worldPosB[ic].w;\n"
  467. " dstC->m_b[ic] += (src->m_worldPosB[ic].w + positionDrift)*positionConstraintCoeff*dtInv;\n"
  468. " dstC->m_appliedRambdaDt[ic] = 0.f;\n"
  469. " }\n"
  470. " }\n"
  471. " if( src->m_worldNormalOnB.w > 0 )//npoints\n"
  472. " { // prepare friction\n"
  473. " b3Float4 center = b3MakeFloat4(0.f,0.f,0.f,0.f);\n"
  474. " for(int i=0; i<src->m_worldNormalOnB.w; i++) \n"
  475. " center += src->m_worldPosB[i];\n"
  476. " center /= (float)src->m_worldNormalOnB.w;\n"
  477. " b3Float4 tangent[2];\n"
  478. " b3PlaneSpace1(src->m_worldNormalOnB,&tangent[0],&tangent[1]);\n"
  479. " \n"
  480. " b3Float4 r[2];\n"
  481. " r[0] = center - posA;\n"
  482. " r[1] = center - posB;\n"
  483. " for(int i=0; i<2; i++)\n"
  484. " {\n"
  485. " b3Float4 linear, angular0, angular1;\n"
  486. " setLinearAndAngular(tangent[i], r[0], r[1], &linear, &angular0, &angular1);\n"
  487. " dstC->m_fJacCoeffInv[i] = calcJacCoeff(linear, -linear, angular0, angular1,\n"
  488. " invMassA, &invInertiaA, invMassB, &invInertiaB );\n"
  489. " dstC->m_fAppliedRambdaDt[i] = 0.f;\n"
  490. " }\n"
  491. " dstC->m_center = center;\n"
  492. " }\n"
  493. " for(int i=0; i<4; i++)\n"
  494. " {\n"
  495. " if( i<src->m_worldNormalOnB.w )\n"
  496. " {\n"
  497. " dstC->m_worldPos[i] = src->m_worldPosB[i];\n"
  498. " }\n"
  499. " else\n"
  500. " {\n"
  501. " dstC->m_worldPos[i] = b3MakeFloat4(0.f,0.f,0.f,0.f);\n"
  502. " }\n"
  503. " }\n"
  504. "}\n"
  505. "#pragma OPENCL EXTENSION cl_amd_printf : enable\n"
  506. "#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable\n"
  507. "#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable\n"
  508. "#pragma OPENCL EXTENSION cl_khr_local_int32_extended_atomics : enable\n"
  509. "#pragma OPENCL EXTENSION cl_khr_global_int32_extended_atomics : enable\n"
  510. "#ifdef cl_ext_atomic_counters_32\n"
  511. "#pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable\n"
  512. "#else\n"
  513. "#define counter32_t volatile global int*\n"
  514. "#endif\n"
  515. "typedef unsigned int u32;\n"
  516. "typedef unsigned short u16;\n"
  517. "typedef unsigned char u8;\n"
  518. "#define GET_GROUP_IDX get_group_id(0)\n"
  519. "#define GET_LOCAL_IDX get_local_id(0)\n"
  520. "#define GET_GLOBAL_IDX get_global_id(0)\n"
  521. "#define GET_GROUP_SIZE get_local_size(0)\n"
  522. "#define GET_NUM_GROUPS get_num_groups(0)\n"
  523. "#define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)\n"
  524. "#define GROUP_MEM_FENCE mem_fence(CLK_LOCAL_MEM_FENCE)\n"
  525. "#define AtomInc(x) atom_inc(&(x))\n"
  526. "#define AtomInc1(x, out) out = atom_inc(&(x))\n"
  527. "#define AppendInc(x, out) out = atomic_inc(x)\n"
  528. "#define AtomAdd(x, value) atom_add(&(x), value)\n"
  529. "#define AtomCmpxhg(x, cmp, value) atom_cmpxchg( &(x), cmp, value )\n"
  530. "#define AtomXhg(x, value) atom_xchg ( &(x), value )\n"
  531. "#define SELECT_UINT4( b, a, condition ) select( b,a,condition )\n"
  532. "#define make_float4 (float4)\n"
  533. "#define make_float2 (float2)\n"
  534. "#define make_uint4 (uint4)\n"
  535. "#define make_int4 (int4)\n"
  536. "#define make_uint2 (uint2)\n"
  537. "#define make_int2 (int2)\n"
  538. "#define max2 max\n"
  539. "#define min2 min\n"
  540. "///////////////////////////////////////\n"
  541. "// Vector\n"
  542. "///////////////////////////////////////\n"
  543. "__inline\n"
  544. "float fastDiv(float numerator, float denominator)\n"
  545. "{\n"
  546. " return native_divide(numerator, denominator); \n"
  547. "// return numerator/denominator; \n"
  548. "}\n"
  549. "__inline\n"
  550. "float4 fastDiv4(float4 numerator, float4 denominator)\n"
  551. "{\n"
  552. " return native_divide(numerator, denominator); \n"
  553. "}\n"
  554. "__inline\n"
  555. "float fastSqrtf(float f2)\n"
  556. "{\n"
  557. " return native_sqrt(f2);\n"
  558. "// return sqrt(f2);\n"
  559. "}\n"
  560. "__inline\n"
  561. "float fastRSqrt(float f2)\n"
  562. "{\n"
  563. " return native_rsqrt(f2);\n"
  564. "}\n"
  565. "__inline\n"
  566. "float fastLength4(float4 v)\n"
  567. "{\n"
  568. " return fast_length(v);\n"
  569. "}\n"
  570. "__inline\n"
  571. "float4 fastNormalize4(float4 v)\n"
  572. "{\n"
  573. " return fast_normalize(v);\n"
  574. "}\n"
  575. "__inline\n"
  576. "float sqrtf(float a)\n"
  577. "{\n"
  578. "// return sqrt(a);\n"
  579. " return native_sqrt(a);\n"
  580. "}\n"
  581. "__inline\n"
  582. "float4 cross3(float4 a, float4 b)\n"
  583. "{\n"
  584. " return cross(a,b);\n"
  585. "}\n"
  586. "__inline\n"
  587. "float dot3F4(float4 a, float4 b)\n"
  588. "{\n"
  589. " float4 a1 = make_float4(a.xyz,0.f);\n"
  590. " float4 b1 = make_float4(b.xyz,0.f);\n"
  591. " return dot(a1, b1);\n"
  592. "}\n"
  593. "__inline\n"
  594. "float length3(const float4 a)\n"
  595. "{\n"
  596. " return sqrtf(dot3F4(a,a));\n"
  597. "}\n"
  598. "__inline\n"
  599. "float dot4(const float4 a, const float4 b)\n"
  600. "{\n"
  601. " return dot( a, b );\n"
  602. "}\n"
  603. "// for height\n"
  604. "__inline\n"
  605. "float dot3w1(const float4 point, const float4 eqn)\n"
  606. "{\n"
  607. " return dot3F4(point,eqn) + eqn.w;\n"
  608. "}\n"
  609. "__inline\n"
  610. "float4 normalize3(const float4 a)\n"
  611. "{\n"
  612. " float4 n = make_float4(a.x, a.y, a.z, 0.f);\n"
  613. " return fastNormalize4( n );\n"
  614. "// float length = sqrtf(dot3F4(a, a));\n"
  615. "// return 1.f/length * a;\n"
  616. "}\n"
  617. "__inline\n"
  618. "float4 normalize4(const float4 a)\n"
  619. "{\n"
  620. " float length = sqrtf(dot4(a, a));\n"
  621. " return 1.f/length * a;\n"
  622. "}\n"
  623. "__inline\n"
  624. "float4 createEquation(const float4 a, const float4 b, const float4 c)\n"
  625. "{\n"
  626. " float4 eqn;\n"
  627. " float4 ab = b-a;\n"
  628. " float4 ac = c-a;\n"
  629. " eqn = normalize3( cross3(ab, ac) );\n"
  630. " eqn.w = -dot3F4(eqn,a);\n"
  631. " return eqn;\n"
  632. "}\n"
  633. "#define WG_SIZE 64\n"
  634. "typedef struct\n"
  635. "{\n"
  636. " int m_nConstraints;\n"
  637. " int m_start;\n"
  638. " int m_batchIdx;\n"
  639. " int m_nSplit;\n"
  640. "// int m_paddings[1];\n"
  641. "} ConstBuffer;\n"
  642. "typedef struct\n"
  643. "{\n"
  644. " int m_solveFriction;\n"
  645. " int m_maxBatch; // long batch really kills the performance\n"
  646. " int m_batchIdx;\n"
  647. " int m_nSplit;\n"
  648. "// int m_paddings[1];\n"
  649. "} ConstBufferBatchSolve;\n"
  650. " \n"
  651. "typedef struct \n"
  652. "{\n"
  653. " int m_valInt0;\n"
  654. " int m_valInt1;\n"
  655. " int m_valInt2;\n"
  656. " int m_valInt3;\n"
  657. " float m_val0;\n"
  658. " float m_val1;\n"
  659. " float m_val2;\n"
  660. " float m_val3;\n"
  661. "} SolverDebugInfo;\n"
  662. "typedef struct\n"
  663. "{\n"
  664. " int m_nContacts;\n"
  665. " float m_dt;\n"
  666. " float m_positionDrift;\n"
  667. " float m_positionConstraintCoeff;\n"
  668. "} ConstBufferCTC;\n"
  669. "__kernel\n"
  670. "__attribute__((reqd_work_group_size(WG_SIZE,1,1)))\n"
  671. "void ContactToConstraintKernel(__global struct b3Contact4Data* gContact, __global b3RigidBodyData_t* gBodies, __global b3InertiaData_t* gShapes, __global b3ContactConstraint4_t* gConstraintOut, \n"
  672. "int nContacts,\n"
  673. "float dt,\n"
  674. "float positionDrift,\n"
  675. "float positionConstraintCoeff\n"
  676. ")\n"
  677. "{\n"
  678. " int gIdx = GET_GLOBAL_IDX;\n"
  679. " \n"
  680. " if( gIdx < nContacts )\n"
  681. " {\n"
  682. " int aIdx = abs(gContact[gIdx].m_bodyAPtrAndSignBit);\n"
  683. " int bIdx = abs(gContact[gIdx].m_bodyBPtrAndSignBit);\n"
  684. " float4 posA = gBodies[aIdx].m_pos;\n"
  685. " float4 linVelA = gBodies[aIdx].m_linVel;\n"
  686. " float4 angVelA = gBodies[aIdx].m_angVel;\n"
  687. " float invMassA = gBodies[aIdx].m_invMass;\n"
  688. " b3Mat3x3 invInertiaA = gShapes[aIdx].m_initInvInertia;\n"
  689. " float4 posB = gBodies[bIdx].m_pos;\n"
  690. " float4 linVelB = gBodies[bIdx].m_linVel;\n"
  691. " float4 angVelB = gBodies[bIdx].m_angVel;\n"
  692. " float invMassB = gBodies[bIdx].m_invMass;\n"
  693. " b3Mat3x3 invInertiaB = gShapes[bIdx].m_initInvInertia;\n"
  694. " b3ContactConstraint4_t cs;\n"
  695. " setConstraint4( posA, linVelA, angVelA, invMassA, invInertiaA, posB, linVelB, angVelB, invMassB, invInertiaB,\n"
  696. " &gContact[gIdx], dt, positionDrift, positionConstraintCoeff,\n"
  697. " &cs );\n"
  698. " \n"
  699. " cs.m_batchIdx = gContact[gIdx].m_batchIdx;\n"
  700. " gConstraintOut[gIdx] = cs;\n"
  701. " }\n"
  702. "}\n";