solverSetup.cl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 "Bullet3Dynamics/shared/b3ConvertConstraint4.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. #define WG_SIZE 64
  143. typedef struct
  144. {
  145. int m_nConstraints;
  146. int m_start;
  147. int m_batchIdx;
  148. int m_nSplit;
  149. // int m_paddings[1];
  150. } ConstBuffer;
  151. typedef struct
  152. {
  153. int m_solveFriction;
  154. int m_maxBatch; // long batch really kills the performance
  155. int m_batchIdx;
  156. int m_nSplit;
  157. // int m_paddings[1];
  158. } ConstBufferBatchSolve;
  159. typedef struct
  160. {
  161. int m_valInt0;
  162. int m_valInt1;
  163. int m_valInt2;
  164. int m_valInt3;
  165. float m_val0;
  166. float m_val1;
  167. float m_val2;
  168. float m_val3;
  169. } SolverDebugInfo;
  170. typedef struct
  171. {
  172. int m_nContacts;
  173. float m_dt;
  174. float m_positionDrift;
  175. float m_positionConstraintCoeff;
  176. } ConstBufferCTC;
  177. __kernel
  178. __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
  179. void ContactToConstraintKernel(__global struct b3Contact4Data* gContact, __global b3RigidBodyData_t* gBodies, __global b3InertiaData_t* gShapes, __global b3ContactConstraint4_t* gConstraintOut,
  180. int nContacts,
  181. float dt,
  182. float positionDrift,
  183. float positionConstraintCoeff
  184. )
  185. {
  186. int gIdx = GET_GLOBAL_IDX;
  187. if( gIdx < nContacts )
  188. {
  189. int aIdx = abs(gContact[gIdx].m_bodyAPtrAndSignBit);
  190. int bIdx = abs(gContact[gIdx].m_bodyBPtrAndSignBit);
  191. float4 posA = gBodies[aIdx].m_pos;
  192. float4 linVelA = gBodies[aIdx].m_linVel;
  193. float4 angVelA = gBodies[aIdx].m_angVel;
  194. float invMassA = gBodies[aIdx].m_invMass;
  195. b3Mat3x3 invInertiaA = gShapes[aIdx].m_initInvInertia;
  196. float4 posB = gBodies[bIdx].m_pos;
  197. float4 linVelB = gBodies[bIdx].m_linVel;
  198. float4 angVelB = gBodies[bIdx].m_angVel;
  199. float invMassB = gBodies[bIdx].m_invMass;
  200. b3Mat3x3 invInertiaB = gShapes[bIdx].m_initInvInertia;
  201. b3ContactConstraint4_t cs;
  202. setConstraint4( posA, linVelA, angVelA, invMassA, invInertiaA, posB, linVelB, angVelB, invMassB, invInertiaB,
  203. &gContact[gIdx], dt, positionDrift, positionConstraintCoeff,
  204. &cs );
  205. cs.m_batchIdx = gContact[gIdx].m_batchIdx;
  206. gConstraintOut[gIdx] = cs;
  207. }
  208. }