integrateKernel.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. //this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
  2. static const char* integrateKernelCL =
  3. "/*\n"
  4. "Copyright (c) 2013 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 Erwin Coumans\n"
  15. "#ifndef B3_RIGIDBODY_DATA_H\n"
  16. "#define B3_RIGIDBODY_DATA_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. "#ifndef B3_QUAT_H\n"
  94. "#define B3_QUAT_H\n"
  95. "#ifndef B3_PLATFORM_DEFINITIONS_H\n"
  96. "#ifdef __cplusplus\n"
  97. "#else\n"
  98. "#endif\n"
  99. "#endif\n"
  100. "#ifndef B3_FLOAT4_H\n"
  101. "#ifdef __cplusplus\n"
  102. "#else\n"
  103. "#endif \n"
  104. "#endif //B3_FLOAT4_H\n"
  105. "#ifdef __cplusplus\n"
  106. "#else\n"
  107. " typedef float4 b3Quat;\n"
  108. " #define b3QuatConstArg const b3Quat\n"
  109. " \n"
  110. " \n"
  111. "inline float4 b3FastNormalize4(float4 v)\n"
  112. "{\n"
  113. " v = (float4)(v.xyz,0.f);\n"
  114. " return fast_normalize(v);\n"
  115. "}\n"
  116. " \n"
  117. "inline b3Quat b3QuatMul(b3Quat a, b3Quat b);\n"
  118. "inline b3Quat b3QuatNormalized(b3QuatConstArg in);\n"
  119. "inline b3Quat b3QuatRotate(b3QuatConstArg q, b3QuatConstArg vec);\n"
  120. "inline b3Quat b3QuatInvert(b3QuatConstArg q);\n"
  121. "inline b3Quat b3QuatInverse(b3QuatConstArg q);\n"
  122. "inline b3Quat b3QuatMul(b3QuatConstArg a, b3QuatConstArg b)\n"
  123. "{\n"
  124. " b3Quat ans;\n"
  125. " ans = b3Cross3( a, b );\n"
  126. " ans += a.w*b+b.w*a;\n"
  127. "// ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);\n"
  128. " ans.w = a.w*b.w - b3Dot3F4(a, b);\n"
  129. " return ans;\n"
  130. "}\n"
  131. "inline b3Quat b3QuatNormalized(b3QuatConstArg in)\n"
  132. "{\n"
  133. " b3Quat q;\n"
  134. " q=in;\n"
  135. " //return b3FastNormalize4(in);\n"
  136. " float len = native_sqrt(dot(q, q));\n"
  137. " if(len > 0.f)\n"
  138. " {\n"
  139. " q *= 1.f / len;\n"
  140. " }\n"
  141. " else\n"
  142. " {\n"
  143. " q.x = q.y = q.z = 0.f;\n"
  144. " q.w = 1.f;\n"
  145. " }\n"
  146. " return q;\n"
  147. "}\n"
  148. "inline float4 b3QuatRotate(b3QuatConstArg q, b3QuatConstArg vec)\n"
  149. "{\n"
  150. " b3Quat qInv = b3QuatInvert( q );\n"
  151. " float4 vcpy = vec;\n"
  152. " vcpy.w = 0.f;\n"
  153. " float4 out = b3QuatMul(b3QuatMul(q,vcpy),qInv);\n"
  154. " return out;\n"
  155. "}\n"
  156. "inline b3Quat b3QuatInverse(b3QuatConstArg q)\n"
  157. "{\n"
  158. " return (b3Quat)(-q.xyz, q.w);\n"
  159. "}\n"
  160. "inline b3Quat b3QuatInvert(b3QuatConstArg q)\n"
  161. "{\n"
  162. " return (b3Quat)(-q.xyz, q.w);\n"
  163. "}\n"
  164. "inline float4 b3QuatInvRotate(b3QuatConstArg q, b3QuatConstArg vec)\n"
  165. "{\n"
  166. " return b3QuatRotate( b3QuatInvert( q ), vec );\n"
  167. "}\n"
  168. "inline b3Float4 b3TransformPoint(b3Float4ConstArg point, b3Float4ConstArg translation, b3QuatConstArg orientation)\n"
  169. "{\n"
  170. " return b3QuatRotate( orientation, point ) + (translation);\n"
  171. "}\n"
  172. " \n"
  173. "#endif \n"
  174. "#endif //B3_QUAT_H\n"
  175. "#ifndef B3_MAT3x3_H\n"
  176. "#define B3_MAT3x3_H\n"
  177. "#ifndef B3_QUAT_H\n"
  178. "#ifdef __cplusplus\n"
  179. "#else\n"
  180. "#endif \n"
  181. "#endif //B3_QUAT_H\n"
  182. "#ifdef __cplusplus\n"
  183. "#else\n"
  184. "typedef struct\n"
  185. "{\n"
  186. " b3Float4 m_row[3];\n"
  187. "}b3Mat3x3;\n"
  188. "#define b3Mat3x3ConstArg const b3Mat3x3\n"
  189. "#define b3GetRow(m,row) (m.m_row[row])\n"
  190. "inline b3Mat3x3 b3QuatGetRotationMatrix(b3Quat quat)\n"
  191. "{\n"
  192. " b3Float4 quat2 = (b3Float4)(quat.x*quat.x, quat.y*quat.y, quat.z*quat.z, 0.f);\n"
  193. " b3Mat3x3 out;\n"
  194. " out.m_row[0].x=1-2*quat2.y-2*quat2.z;\n"
  195. " out.m_row[0].y=2*quat.x*quat.y-2*quat.w*quat.z;\n"
  196. " out.m_row[0].z=2*quat.x*quat.z+2*quat.w*quat.y;\n"
  197. " out.m_row[0].w = 0.f;\n"
  198. " out.m_row[1].x=2*quat.x*quat.y+2*quat.w*quat.z;\n"
  199. " out.m_row[1].y=1-2*quat2.x-2*quat2.z;\n"
  200. " out.m_row[1].z=2*quat.y*quat.z-2*quat.w*quat.x;\n"
  201. " out.m_row[1].w = 0.f;\n"
  202. " out.m_row[2].x=2*quat.x*quat.z-2*quat.w*quat.y;\n"
  203. " out.m_row[2].y=2*quat.y*quat.z+2*quat.w*quat.x;\n"
  204. " out.m_row[2].z=1-2*quat2.x-2*quat2.y;\n"
  205. " out.m_row[2].w = 0.f;\n"
  206. " return out;\n"
  207. "}\n"
  208. "inline b3Mat3x3 b3AbsoluteMat3x3(b3Mat3x3ConstArg matIn)\n"
  209. "{\n"
  210. " b3Mat3x3 out;\n"
  211. " out.m_row[0] = fabs(matIn.m_row[0]);\n"
  212. " out.m_row[1] = fabs(matIn.m_row[1]);\n"
  213. " out.m_row[2] = fabs(matIn.m_row[2]);\n"
  214. " return out;\n"
  215. "}\n"
  216. "__inline\n"
  217. "b3Mat3x3 mtZero();\n"
  218. "__inline\n"
  219. "b3Mat3x3 mtIdentity();\n"
  220. "__inline\n"
  221. "b3Mat3x3 mtTranspose(b3Mat3x3 m);\n"
  222. "__inline\n"
  223. "b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b);\n"
  224. "__inline\n"
  225. "b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b);\n"
  226. "__inline\n"
  227. "b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b);\n"
  228. "__inline\n"
  229. "b3Mat3x3 mtZero()\n"
  230. "{\n"
  231. " b3Mat3x3 m;\n"
  232. " m.m_row[0] = (b3Float4)(0.f);\n"
  233. " m.m_row[1] = (b3Float4)(0.f);\n"
  234. " m.m_row[2] = (b3Float4)(0.f);\n"
  235. " return m;\n"
  236. "}\n"
  237. "__inline\n"
  238. "b3Mat3x3 mtIdentity()\n"
  239. "{\n"
  240. " b3Mat3x3 m;\n"
  241. " m.m_row[0] = (b3Float4)(1,0,0,0);\n"
  242. " m.m_row[1] = (b3Float4)(0,1,0,0);\n"
  243. " m.m_row[2] = (b3Float4)(0,0,1,0);\n"
  244. " return m;\n"
  245. "}\n"
  246. "__inline\n"
  247. "b3Mat3x3 mtTranspose(b3Mat3x3 m)\n"
  248. "{\n"
  249. " b3Mat3x3 out;\n"
  250. " out.m_row[0] = (b3Float4)(m.m_row[0].x, m.m_row[1].x, m.m_row[2].x, 0.f);\n"
  251. " out.m_row[1] = (b3Float4)(m.m_row[0].y, m.m_row[1].y, m.m_row[2].y, 0.f);\n"
  252. " out.m_row[2] = (b3Float4)(m.m_row[0].z, m.m_row[1].z, m.m_row[2].z, 0.f);\n"
  253. " return out;\n"
  254. "}\n"
  255. "__inline\n"
  256. "b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b)\n"
  257. "{\n"
  258. " b3Mat3x3 transB;\n"
  259. " transB = mtTranspose( b );\n"
  260. " b3Mat3x3 ans;\n"
  261. " // why this doesn't run when 0ing in the for{}\n"
  262. " a.m_row[0].w = 0.f;\n"
  263. " a.m_row[1].w = 0.f;\n"
  264. " a.m_row[2].w = 0.f;\n"
  265. " for(int i=0; i<3; i++)\n"
  266. " {\n"
  267. "// a.m_row[i].w = 0.f;\n"
  268. " ans.m_row[i].x = b3Dot3F4(a.m_row[i],transB.m_row[0]);\n"
  269. " ans.m_row[i].y = b3Dot3F4(a.m_row[i],transB.m_row[1]);\n"
  270. " ans.m_row[i].z = b3Dot3F4(a.m_row[i],transB.m_row[2]);\n"
  271. " ans.m_row[i].w = 0.f;\n"
  272. " }\n"
  273. " return ans;\n"
  274. "}\n"
  275. "__inline\n"
  276. "b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b)\n"
  277. "{\n"
  278. " b3Float4 ans;\n"
  279. " ans.x = b3Dot3F4( a.m_row[0], b );\n"
  280. " ans.y = b3Dot3F4( a.m_row[1], b );\n"
  281. " ans.z = b3Dot3F4( a.m_row[2], b );\n"
  282. " ans.w = 0.f;\n"
  283. " return ans;\n"
  284. "}\n"
  285. "__inline\n"
  286. "b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b)\n"
  287. "{\n"
  288. " b3Float4 colx = b3MakeFloat4(b.m_row[0].x, b.m_row[1].x, b.m_row[2].x, 0);\n"
  289. " b3Float4 coly = b3MakeFloat4(b.m_row[0].y, b.m_row[1].y, b.m_row[2].y, 0);\n"
  290. " b3Float4 colz = b3MakeFloat4(b.m_row[0].z, b.m_row[1].z, b.m_row[2].z, 0);\n"
  291. " b3Float4 ans;\n"
  292. " ans.x = b3Dot3F4( a, colx );\n"
  293. " ans.y = b3Dot3F4( a, coly );\n"
  294. " ans.z = b3Dot3F4( a, colz );\n"
  295. " return ans;\n"
  296. "}\n"
  297. "#endif\n"
  298. "#endif //B3_MAT3x3_H\n"
  299. "typedef struct b3RigidBodyData b3RigidBodyData_t;\n"
  300. "struct b3RigidBodyData\n"
  301. "{\n"
  302. " b3Float4 m_pos;\n"
  303. " b3Quat m_quat;\n"
  304. " b3Float4 m_linVel;\n"
  305. " b3Float4 m_angVel;\n"
  306. " int m_collidableIdx;\n"
  307. " float m_invMass;\n"
  308. " float m_restituitionCoeff;\n"
  309. " float m_frictionCoeff;\n"
  310. "};\n"
  311. "typedef struct b3InertiaData b3InertiaData_t;\n"
  312. "struct b3InertiaData\n"
  313. "{\n"
  314. " b3Mat3x3 m_invInertiaWorld;\n"
  315. " b3Mat3x3 m_initInvInertia;\n"
  316. "};\n"
  317. "#endif //B3_RIGIDBODY_DATA_H\n"
  318. " \n"
  319. "#ifndef B3_RIGIDBODY_DATA_H\n"
  320. "#endif //B3_RIGIDBODY_DATA_H\n"
  321. " \n"
  322. "inline void integrateSingleTransform( __global b3RigidBodyData_t* bodies,int nodeID, float timeStep, float angularDamping, b3Float4ConstArg gravityAcceleration)\n"
  323. "{\n"
  324. " \n"
  325. " if (bodies[nodeID].m_invMass != 0.f)\n"
  326. " {\n"
  327. " float BT_GPU_ANGULAR_MOTION_THRESHOLD = (0.25f * 3.14159254f);\n"
  328. " //angular velocity\n"
  329. " {\n"
  330. " b3Float4 axis;\n"
  331. " //add some hardcoded angular damping\n"
  332. " bodies[nodeID].m_angVel.x *= angularDamping;\n"
  333. " bodies[nodeID].m_angVel.y *= angularDamping;\n"
  334. " bodies[nodeID].m_angVel.z *= angularDamping;\n"
  335. " \n"
  336. " b3Float4 angvel = bodies[nodeID].m_angVel;\n"
  337. " float fAngle = b3Sqrt(b3Dot3F4(angvel, angvel));\n"
  338. " \n"
  339. " //limit the angular motion\n"
  340. " if(fAngle*timeStep > BT_GPU_ANGULAR_MOTION_THRESHOLD)\n"
  341. " {\n"
  342. " fAngle = BT_GPU_ANGULAR_MOTION_THRESHOLD / timeStep;\n"
  343. " }\n"
  344. " if(fAngle < 0.001f)\n"
  345. " {\n"
  346. " // use Taylor's expansions of sync function\n"
  347. " axis = angvel * (0.5f*timeStep-(timeStep*timeStep*timeStep)*0.020833333333f * fAngle * fAngle);\n"
  348. " }\n"
  349. " else\n"
  350. " {\n"
  351. " // sync(fAngle) = sin(c*fAngle)/t\n"
  352. " axis = angvel * ( b3Sin(0.5f * fAngle * timeStep) / fAngle);\n"
  353. " }\n"
  354. " \n"
  355. " b3Quat dorn;\n"
  356. " dorn.x = axis.x;\n"
  357. " dorn.y = axis.y;\n"
  358. " dorn.z = axis.z;\n"
  359. " dorn.w = b3Cos(fAngle * timeStep * 0.5f);\n"
  360. " b3Quat orn0 = bodies[nodeID].m_quat;\n"
  361. " b3Quat predictedOrn = b3QuatMul(dorn, orn0);\n"
  362. " predictedOrn = b3QuatNormalized(predictedOrn);\n"
  363. " bodies[nodeID].m_quat=predictedOrn;\n"
  364. " }\n"
  365. " //linear velocity \n"
  366. " bodies[nodeID].m_pos += bodies[nodeID].m_linVel * timeStep;\n"
  367. " \n"
  368. " //apply gravity\n"
  369. " bodies[nodeID].m_linVel += gravityAcceleration * timeStep;\n"
  370. " \n"
  371. " }\n"
  372. " \n"
  373. "}\n"
  374. "inline void b3IntegrateTransform( __global b3RigidBodyData_t* body, float timeStep, float angularDamping, b3Float4ConstArg gravityAcceleration)\n"
  375. "{\n"
  376. " float BT_GPU_ANGULAR_MOTION_THRESHOLD = (0.25f * 3.14159254f);\n"
  377. " \n"
  378. " if( (body->m_invMass != 0.f))\n"
  379. " {\n"
  380. " //angular velocity\n"
  381. " {\n"
  382. " b3Float4 axis;\n"
  383. " //add some hardcoded angular damping\n"
  384. " body->m_angVel.x *= angularDamping;\n"
  385. " body->m_angVel.y *= angularDamping;\n"
  386. " body->m_angVel.z *= angularDamping;\n"
  387. " \n"
  388. " b3Float4 angvel = body->m_angVel;\n"
  389. " float fAngle = b3Sqrt(b3Dot3F4(angvel, angvel));\n"
  390. " //limit the angular motion\n"
  391. " if(fAngle*timeStep > BT_GPU_ANGULAR_MOTION_THRESHOLD)\n"
  392. " {\n"
  393. " fAngle = BT_GPU_ANGULAR_MOTION_THRESHOLD / timeStep;\n"
  394. " }\n"
  395. " if(fAngle < 0.001f)\n"
  396. " {\n"
  397. " // use Taylor's expansions of sync function\n"
  398. " axis = angvel * (0.5f*timeStep-(timeStep*timeStep*timeStep)*0.020833333333f * fAngle * fAngle);\n"
  399. " }\n"
  400. " else\n"
  401. " {\n"
  402. " // sync(fAngle) = sin(c*fAngle)/t\n"
  403. " axis = angvel * ( b3Sin(0.5f * fAngle * timeStep) / fAngle);\n"
  404. " }\n"
  405. " b3Quat dorn;\n"
  406. " dorn.x = axis.x;\n"
  407. " dorn.y = axis.y;\n"
  408. " dorn.z = axis.z;\n"
  409. " dorn.w = b3Cos(fAngle * timeStep * 0.5f);\n"
  410. " b3Quat orn0 = body->m_quat;\n"
  411. " b3Quat predictedOrn = b3QuatMul(dorn, orn0);\n"
  412. " predictedOrn = b3QuatNormalized(predictedOrn);\n"
  413. " body->m_quat=predictedOrn;\n"
  414. " }\n"
  415. " //apply gravity\n"
  416. " body->m_linVel += gravityAcceleration * timeStep;\n"
  417. " //linear velocity \n"
  418. " body->m_pos += body->m_linVel * timeStep;\n"
  419. " \n"
  420. " }\n"
  421. " \n"
  422. "}\n"
  423. "__kernel void \n"
  424. " integrateTransformsKernel( __global b3RigidBodyData_t* bodies,const int numNodes, float timeStep, float angularDamping, float4 gravityAcceleration)\n"
  425. "{\n"
  426. " int nodeID = get_global_id(0);\n"
  427. " \n"
  428. " if( nodeID < numNodes)\n"
  429. " {\n"
  430. " integrateSingleTransform(bodies,nodeID, timeStep, angularDamping,gravityAcceleration);\n"
  431. " }\n"
  432. "}\n";