[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / Bullet3OpenCL / RigidBody / kernels / solveFriction.h
1 //this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
2 static const char* solveFrictionCL =
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         "//#pragma OPENCL EXTENSION cl_amd_printf : enable\n"
16         "#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable\n"
17         "#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable\n"
18         "#pragma OPENCL EXTENSION cl_khr_local_int32_extended_atomics : enable\n"
19         "#pragma OPENCL EXTENSION cl_khr_global_int32_extended_atomics : enable\n"
20         "#ifdef cl_ext_atomic_counters_32\n"
21         "#pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable\n"
22         "#else\n"
23         "#define counter32_t volatile global int*\n"
24         "#endif\n"
25         "typedef unsigned int u32;\n"
26         "typedef unsigned short u16;\n"
27         "typedef unsigned char u8;\n"
28         "#define GET_GROUP_IDX get_group_id(0)\n"
29         "#define GET_LOCAL_IDX get_local_id(0)\n"
30         "#define GET_GLOBAL_IDX get_global_id(0)\n"
31         "#define GET_GROUP_SIZE get_local_size(0)\n"
32         "#define GET_NUM_GROUPS get_num_groups(0)\n"
33         "#define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)\n"
34         "#define GROUP_MEM_FENCE mem_fence(CLK_LOCAL_MEM_FENCE)\n"
35         "#define AtomInc(x) atom_inc(&(x))\n"
36         "#define AtomInc1(x, out) out = atom_inc(&(x))\n"
37         "#define AppendInc(x, out) out = atomic_inc(x)\n"
38         "#define AtomAdd(x, value) atom_add(&(x), value)\n"
39         "#define AtomCmpxhg(x, cmp, value) atom_cmpxchg( &(x), cmp, value )\n"
40         "#define AtomXhg(x, value) atom_xchg ( &(x), value )\n"
41         "#define SELECT_UINT4( b, a, condition ) select( b,a,condition )\n"
42         "#define mymake_float4 (float4)\n"
43         "//#define make_float2 (float2)\n"
44         "//#define make_uint4 (uint4)\n"
45         "//#define make_int4 (int4)\n"
46         "//#define make_uint2 (uint2)\n"
47         "//#define make_int2 (int2)\n"
48         "#define max2 max\n"
49         "#define min2 min\n"
50         "///////////////////////////////////////\n"
51         "//     Vector\n"
52         "///////////////////////////////////////\n"
53         "__inline\n"
54         "float4 fastNormalize4(float4 v)\n"
55         "{\n"
56         "       return fast_normalize(v);\n"
57         "}\n"
58         "__inline\n"
59         "float4 cross3(float4 a, float4 b)\n"
60         "{\n"
61         "       return cross(a,b);\n"
62         "}\n"
63         "__inline\n"
64         "float dot3F4(float4 a, float4 b)\n"
65         "{\n"
66         "       float4 a1 = mymake_float4(a.xyz,0.f);\n"
67         "       float4 b1 = mymake_float4(b.xyz,0.f);\n"
68         "       return dot(a1, b1);\n"
69         "}\n"
70         "__inline\n"
71         "float4 normalize3(const float4 a)\n"
72         "{\n"
73         "       float4 n = mymake_float4(a.x, a.y, a.z, 0.f);\n"
74         "       return fastNormalize4( n );\n"
75         "//     float length = sqrtf(dot3F4(a, a));\n"
76         "//     return 1.f/length * a;\n"
77         "}\n"
78         "///////////////////////////////////////\n"
79         "//     Matrix3x3\n"
80         "///////////////////////////////////////\n"
81         "typedef struct\n"
82         "{\n"
83         "       float4 m_row[3];\n"
84         "}Matrix3x3;\n"
85         "__inline\n"
86         "float4 mtMul1(Matrix3x3 a, float4 b);\n"
87         "__inline\n"
88         "float4 mtMul3(float4 a, Matrix3x3 b);\n"
89         "__inline\n"
90         "float4 mtMul1(Matrix3x3 a, float4 b)\n"
91         "{\n"
92         "       float4 ans;\n"
93         "       ans.x = dot3F4( a.m_row[0], b );\n"
94         "       ans.y = dot3F4( a.m_row[1], b );\n"
95         "       ans.z = dot3F4( a.m_row[2], b );\n"
96         "       ans.w = 0.f;\n"
97         "       return ans;\n"
98         "}\n"
99         "__inline\n"
100         "float4 mtMul3(float4 a, Matrix3x3 b)\n"
101         "{\n"
102         "       float4 colx = mymake_float4(b.m_row[0].x, b.m_row[1].x, b.m_row[2].x, 0);\n"
103         "       float4 coly = mymake_float4(b.m_row[0].y, b.m_row[1].y, b.m_row[2].y, 0);\n"
104         "       float4 colz = mymake_float4(b.m_row[0].z, b.m_row[1].z, b.m_row[2].z, 0);\n"
105         "       float4 ans;\n"
106         "       ans.x = dot3F4( a, colx );\n"
107         "       ans.y = dot3F4( a, coly );\n"
108         "       ans.z = dot3F4( a, colz );\n"
109         "       return ans;\n"
110         "}\n"
111         "///////////////////////////////////////\n"
112         "//     Quaternion\n"
113         "///////////////////////////////////////\n"
114         "typedef float4 Quaternion;\n"
115         "#define WG_SIZE 64\n"
116         "typedef struct\n"
117         "{\n"
118         "       float4 m_pos;\n"
119         "       Quaternion m_quat;\n"
120         "       float4 m_linVel;\n"
121         "       float4 m_angVel;\n"
122         "       u32 m_shapeIdx;\n"
123         "       float m_invMass;\n"
124         "       float m_restituitionCoeff;\n"
125         "       float m_frictionCoeff;\n"
126         "} Body;\n"
127         "typedef struct\n"
128         "{\n"
129         "       Matrix3x3 m_invInertia;\n"
130         "       Matrix3x3 m_initInvInertia;\n"
131         "} Shape;\n"
132         "typedef struct\n"
133         "{\n"
134         "       float4 m_linear;\n"
135         "       float4 m_worldPos[4];\n"
136         "       float4 m_center;        \n"
137         "       float m_jacCoeffInv[4];\n"
138         "       float m_b[4];\n"
139         "       float m_appliedRambdaDt[4];\n"
140         "       float m_fJacCoeffInv[2];        \n"
141         "       float m_fAppliedRambdaDt[2];    \n"
142         "       u32 m_bodyA;\n"
143         "       u32 m_bodyB;\n"
144         "       int m_batchIdx;\n"
145         "       u32 m_paddings[1];\n"
146         "} Constraint4;\n"
147         "typedef struct\n"
148         "{\n"
149         "       int m_nConstraints;\n"
150         "       int m_start;\n"
151         "       int m_batchIdx;\n"
152         "       int m_nSplit;\n"
153         "//     int m_paddings[1];\n"
154         "} ConstBuffer;\n"
155         "typedef struct\n"
156         "{\n"
157         "       int m_solveFriction;\n"
158         "       int m_maxBatch; //      long batch really kills the performance\n"
159         "       int m_batchIdx;\n"
160         "       int m_nSplit;\n"
161         "//     int m_paddings[1];\n"
162         "} ConstBufferBatchSolve;\n"
163         "void setLinearAndAngular( float4 n, float4 r0, float4 r1, float4* linear, float4* angular0, float4* angular1);\n"
164         "void setLinearAndAngular( float4 n, float4 r0, float4 r1, float4* linear, float4* angular0, float4* angular1)\n"
165         "{\n"
166         "       *linear = mymake_float4(-n.xyz,0.f);\n"
167         "       *angular0 = -cross3(r0, n);\n"
168         "       *angular1 = cross3(r1, n);\n"
169         "}\n"
170         "float calcRelVel( float4 l0, float4 l1, float4 a0, float4 a1, float4 linVel0, float4 angVel0, float4 linVel1, float4 angVel1 );\n"
171         "float calcRelVel( float4 l0, float4 l1, float4 a0, float4 a1, float4 linVel0, float4 angVel0, float4 linVel1, float4 angVel1 )\n"
172         "{\n"
173         "       return dot3F4(l0, linVel0) + dot3F4(a0, angVel0) + dot3F4(l1, linVel1) + dot3F4(a1, angVel1);\n"
174         "}\n"
175         "float calcJacCoeff(const float4 linear0, const float4 linear1, const float4 angular0, const float4 angular1,\n"
176         "                                  float invMass0, const Matrix3x3* invInertia0, float invMass1, const Matrix3x3* invInertia1);\n"
177         "float calcJacCoeff(const float4 linear0, const float4 linear1, const float4 angular0, const float4 angular1,\n"
178         "                                       float invMass0, const Matrix3x3* invInertia0, float invMass1, const Matrix3x3* invInertia1)\n"
179         "{\n"
180         "       //      linear0,1 are normlized\n"
181         "       float jmj0 = invMass0;//dot3F4(linear0, linear0)*invMass0;\n"
182         "       float jmj1 = dot3F4(mtMul3(angular0,*invInertia0), angular0);\n"
183         "       float jmj2 = invMass1;//dot3F4(linear1, linear1)*invMass1;\n"
184         "       float jmj3 = dot3F4(mtMul3(angular1,*invInertia1), angular1);\n"
185         "       return -1.f/(jmj0+jmj1+jmj2+jmj3);\n"
186         "}\n"
187         "void btPlaneSpace1 (const float4* n, float4* p, float4* q);\n"
188         " void btPlaneSpace1 (const float4* n, float4* p, float4* q)\n"
189         "{\n"
190         "  if (fabs(n[0].z) > 0.70710678f) {\n"
191         "    // choose p in y-z plane\n"
192         "    float a = n[0].y*n[0].y + n[0].z*n[0].z;\n"
193         "    float k = 1.f/sqrt(a);\n"
194         "    p[0].x = 0;\n"
195         "       p[0].y = -n[0].z*k;\n"
196         "       p[0].z = n[0].y*k;\n"
197         "    // set q = n x p\n"
198         "    q[0].x = a*k;\n"
199         "       q[0].y = -n[0].x*p[0].z;\n"
200         "       q[0].z = n[0].x*p[0].y;\n"
201         "  }\n"
202         "  else {\n"
203         "    // choose p in x-y plane\n"
204         "    float a = n[0].x*n[0].x + n[0].y*n[0].y;\n"
205         "    float k = 1.f/sqrt(a);\n"
206         "    p[0].x = -n[0].y*k;\n"
207         "       p[0].y = n[0].x*k;\n"
208         "       p[0].z = 0;\n"
209         "    // set q = n x p\n"
210         "    q[0].x = -n[0].z*p[0].y;\n"
211         "       q[0].y = n[0].z*p[0].x;\n"
212         "       q[0].z = a*k;\n"
213         "  }\n"
214         "}\n"
215         "void solveFrictionConstraint(__global Body* gBodies, __global Shape* gShapes, __global Constraint4* ldsCs);\n"
216         "void solveFrictionConstraint(__global Body* gBodies, __global Shape* gShapes, __global Constraint4* ldsCs)\n"
217         "{\n"
218         "       float frictionCoeff = ldsCs[0].m_linear.w;\n"
219         "       int aIdx = ldsCs[0].m_bodyA;\n"
220         "       int bIdx = ldsCs[0].m_bodyB;\n"
221         "       float4 posA = gBodies[aIdx].m_pos;\n"
222         "       float4 linVelA = gBodies[aIdx].m_linVel;\n"
223         "       float4 angVelA = gBodies[aIdx].m_angVel;\n"
224         "       float invMassA = gBodies[aIdx].m_invMass;\n"
225         "       Matrix3x3 invInertiaA = gShapes[aIdx].m_invInertia;\n"
226         "       float4 posB = gBodies[bIdx].m_pos;\n"
227         "       float4 linVelB = gBodies[bIdx].m_linVel;\n"
228         "       float4 angVelB = gBodies[bIdx].m_angVel;\n"
229         "       float invMassB = gBodies[bIdx].m_invMass;\n"
230         "       Matrix3x3 invInertiaB = gShapes[bIdx].m_invInertia;\n"
231         "       \n"
232         "       {\n"
233         "               float maxRambdaDt[4] = {FLT_MAX,FLT_MAX,FLT_MAX,FLT_MAX};\n"
234         "               float minRambdaDt[4] = {0.f,0.f,0.f,0.f};\n"
235         "               float sum = 0;\n"
236         "               for(int j=0; j<4; j++)\n"
237         "               {\n"
238         "                       sum +=ldsCs[0].m_appliedRambdaDt[j];\n"
239         "               }\n"
240         "               frictionCoeff = 0.7f;\n"
241         "               for(int j=0; j<4; j++)\n"
242         "               {\n"
243         "                       maxRambdaDt[j] = frictionCoeff*sum;\n"
244         "                       minRambdaDt[j] = -maxRambdaDt[j];\n"
245         "               }\n"
246         "               \n"
247         "//             solveFriction( ldsCs, posA, &linVelA, &angVelA, invMassA, invInertiaA,\n"
248         "//                     posB, &linVelB, &angVelB, invMassB, invInertiaB, maxRambdaDt, minRambdaDt );\n"
249         "               \n"
250         "               \n"
251         "               {\n"
252         "                       \n"
253         "                       __global Constraint4* cs = ldsCs;\n"
254         "                       \n"
255         "                       if( cs->m_fJacCoeffInv[0] == 0 && cs->m_fJacCoeffInv[0] == 0 ) return;\n"
256         "                       const float4 center = cs->m_center;\n"
257         "                       \n"
258         "                       float4 n = -cs->m_linear;\n"
259         "                       \n"
260         "                       float4 tangent[2];\n"
261         "                       btPlaneSpace1(&n,&tangent[0],&tangent[1]);\n"
262         "                       float4 angular0, angular1, linear;\n"
263         "                       float4 r0 = center - posA;\n"
264         "                       float4 r1 = center - posB;\n"
265         "                       for(int i=0; i<2; i++)\n"
266         "                       {\n"
267         "                               setLinearAndAngular( tangent[i], r0, r1, &linear, &angular0, &angular1 );\n"
268         "                               float rambdaDt = calcRelVel(linear, -linear, angular0, angular1,\n"
269         "                                                                                       linVelA, angVelA, linVelB, angVelB );\n"
270         "                               rambdaDt *= cs->m_fJacCoeffInv[i];\n"
271         "                               \n"
272         "                               {\n"
273         "                                       float prevSum = cs->m_fAppliedRambdaDt[i];\n"
274         "                                       float updated = prevSum;\n"
275         "                                       updated += rambdaDt;\n"
276         "                                       updated = max2( updated, minRambdaDt[i] );\n"
277         "                                       updated = min2( updated, maxRambdaDt[i] );\n"
278         "                                       rambdaDt = updated - prevSum;\n"
279         "                                       cs->m_fAppliedRambdaDt[i] = updated;\n"
280         "                               }\n"
281         "                               \n"
282         "                               float4 linImp0 = invMassA*linear*rambdaDt;\n"
283         "                               float4 linImp1 = invMassB*(-linear)*rambdaDt;\n"
284         "                               float4 angImp0 = mtMul1(invInertiaA, angular0)*rambdaDt;\n"
285         "                               float4 angImp1 = mtMul1(invInertiaB, angular1)*rambdaDt;\n"
286         "                               \n"
287         "                               linVelA += linImp0;\n"
288         "                               angVelA += angImp0;\n"
289         "                               linVelB += linImp1;\n"
290         "                               angVelB += angImp1;\n"
291         "                       }\n"
292         "                       {       //      angular damping for point constraint\n"
293         "                               float4 ab = normalize3( posB - posA );\n"
294         "                               float4 ac = normalize3( center - posA );\n"
295         "                               if( dot3F4( ab, ac ) > 0.95f  || (invMassA == 0.f || invMassB == 0.f))\n"
296         "                               {\n"
297         "                                       float angNA = dot3F4( n, angVelA );\n"
298         "                                       float angNB = dot3F4( n, angVelB );\n"
299         "                                       \n"
300         "                                       angVelA -= (angNA*0.1f)*n;\n"
301         "                                       angVelB -= (angNB*0.1f)*n;\n"
302         "                               }\n"
303         "                       }\n"
304         "               }\n"
305         "               \n"
306         "               \n"
307         "       }\n"
308         "       if (gBodies[aIdx].m_invMass)\n"
309         "       {\n"
310         "               gBodies[aIdx].m_linVel = linVelA;\n"
311         "               gBodies[aIdx].m_angVel = angVelA;\n"
312         "       } else\n"
313         "       {\n"
314         "               gBodies[aIdx].m_linVel = mymake_float4(0,0,0,0);\n"
315         "               gBodies[aIdx].m_angVel = mymake_float4(0,0,0,0);\n"
316         "       }\n"
317         "       if (gBodies[bIdx].m_invMass)\n"
318         "       {\n"
319         "               gBodies[bIdx].m_linVel = linVelB;\n"
320         "               gBodies[bIdx].m_angVel = angVelB;\n"
321         "       } else\n"
322         "       {\n"
323         "               gBodies[bIdx].m_linVel = mymake_float4(0,0,0,0);\n"
324         "               gBodies[bIdx].m_angVel = mymake_float4(0,0,0,0);\n"
325         "       }\n"
326         " \n"
327         "}\n"
328         "typedef struct \n"
329         "{\n"
330         "       int m_valInt0;\n"
331         "       int m_valInt1;\n"
332         "       int m_valInt2;\n"
333         "       int m_valInt3;\n"
334         "       float m_val0;\n"
335         "       float m_val1;\n"
336         "       float m_val2;\n"
337         "       float m_val3;\n"
338         "} SolverDebugInfo;\n"
339         "__kernel\n"
340         "__attribute__((reqd_work_group_size(WG_SIZE,1,1)))\n"
341         "void BatchSolveKernelFriction(__global Body* gBodies,\n"
342         "                      __global Shape* gShapes,\n"
343         "                      __global Constraint4* gConstraints,\n"
344         "                      __global int* gN,\n"
345         "                      __global int* gOffsets,\n"
346         "                      __global int* batchSizes,\n"
347         "                       int maxBatch1,\n"
348         "                       int cellBatch,\n"
349         "                       int4 nSplit\n"
350         "                      )\n"
351         "{\n"
352         "       //__local int ldsBatchIdx[WG_SIZE+1];\n"
353         "       __local int ldsCurBatch;\n"
354         "       __local int ldsNextBatch;\n"
355         "       __local int ldsStart;\n"
356         "       int lIdx = GET_LOCAL_IDX;\n"
357         "       int wgIdx = GET_GROUP_IDX;\n"
358         "//     int gIdx = GET_GLOBAL_IDX;\n"
359         "//     debugInfo[gIdx].m_valInt0 = gIdx;\n"
360         "       //debugInfo[gIdx].m_valInt1 = GET_GROUP_SIZE;\n"
361         "       int zIdx = (wgIdx/((nSplit.x*nSplit.y)/4))*2+((cellBatch&4)>>2);\n"
362         "       int remain= (wgIdx%((nSplit.x*nSplit.y)/4));\n"
363         "       int yIdx = (remain/(nSplit.x/2))*2 + ((cellBatch&2)>>1);\n"
364         "       int xIdx = (remain%(nSplit.x/2))*2 + (cellBatch&1);\n"
365         "       int cellIdx = xIdx+yIdx*nSplit.x+zIdx*(nSplit.x*nSplit.y);\n"
366         "       \n"
367         "       if( gN[cellIdx] == 0 ) \n"
368         "               return;\n"
369         "       int maxBatch = batchSizes[cellIdx];\n"
370         "       const int start = gOffsets[cellIdx];\n"
371         "       const int end = start + gN[cellIdx];\n"
372         "       \n"
373         "       if( lIdx == 0 )\n"
374         "       {\n"
375         "               ldsCurBatch = 0;\n"
376         "               ldsNextBatch = 0;\n"
377         "               ldsStart = start;\n"
378         "       }\n"
379         "       GROUP_LDS_BARRIER;\n"
380         "       int idx=ldsStart+lIdx;\n"
381         "       while (ldsCurBatch < maxBatch)\n"
382         "       {\n"
383         "               for(; idx<end; )\n"
384         "               {\n"
385         "                       if (gConstraints[idx].m_batchIdx == ldsCurBatch)\n"
386         "                       {\n"
387         "                                       solveFrictionConstraint( gBodies, gShapes, &gConstraints[idx] );\n"
388         "                                idx+=64;\n"
389         "                       } else\n"
390         "                       {\n"
391         "                               break;\n"
392         "                       }\n"
393         "               }\n"
394         "               GROUP_LDS_BARRIER;\n"
395         "               if( lIdx == 0 )\n"
396         "               {\n"
397         "                       ldsCurBatch++;\n"
398         "               }\n"
399         "               GROUP_LDS_BARRIER;\n"
400         "       }\n"
401         "       \n"
402         "    \n"
403         "}\n"
404         "__kernel void solveSingleFrictionKernel(__global Body* gBodies,\n"
405         "                      __global Shape* gShapes,\n"
406         "                      __global Constraint4* gConstraints,\n"
407         "                       int cellIdx,\n"
408         "                       int batchOffset,\n"
409         "                       int numConstraintsInBatch\n"
410         "                      )\n"
411         "{\n"
412         "       int index = get_global_id(0);\n"
413         "       if (index < numConstraintsInBatch)\n"
414         "       {\n"
415         "               \n"
416         "               int idx=batchOffset+index;\n"
417         "       \n"
418         "               solveFrictionConstraint( gBodies, gShapes, &gConstraints[idx] );\n"
419         "       }    \n"
420         "}\n";