[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / Bullet3OpenCL / RigidBody / kernels / batchingKernelsNew.h
1 //this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
2 static const char* batchingKernelsNewCL =
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 Erwin Coumans\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         "#pragma OPENCL EXTENSION cl_amd_printf : enable\n"
120         "#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable\n"
121         "#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable\n"
122         "#pragma OPENCL EXTENSION cl_khr_local_int32_extended_atomics : enable\n"
123         "#pragma OPENCL EXTENSION cl_khr_global_int32_extended_atomics : enable\n"
124         "#ifdef cl_ext_atomic_counters_32\n"
125         "#pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable\n"
126         "#else\n"
127         "#define counter32_t volatile __global int*\n"
128         "#endif\n"
129         "#define SIMD_WIDTH 64\n"
130         "typedef unsigned int u32;\n"
131         "typedef unsigned short u16;\n"
132         "typedef unsigned char u8;\n"
133         "#define GET_GROUP_IDX get_group_id(0)\n"
134         "#define GET_LOCAL_IDX get_local_id(0)\n"
135         "#define GET_GLOBAL_IDX get_global_id(0)\n"
136         "#define GET_GROUP_SIZE get_local_size(0)\n"
137         "#define GET_NUM_GROUPS get_num_groups(0)\n"
138         "#define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)\n"
139         "#define GROUP_MEM_FENCE mem_fence(CLK_LOCAL_MEM_FENCE)\n"
140         "#define AtomInc(x) atom_inc(&(x))\n"
141         "#define AtomInc1(x, out) out = atom_inc(&(x))\n"
142         "#define AppendInc(x, out) out = atomic_inc(x)\n"
143         "#define AtomAdd(x, value) atom_add(&(x), value)\n"
144         "#define AtomCmpxhg(x, cmp, value) atom_cmpxchg( &(x), cmp, value )\n"
145         "#define AtomXhg(x, value) atom_xchg ( &(x), value )\n"
146         "#define SELECT_UINT4( b, a, condition ) select( b,a,condition )\n"
147         "#define make_float4 (float4)\n"
148         "#define make_float2 (float2)\n"
149         "#define make_uint4 (uint4)\n"
150         "#define make_int4 (int4)\n"
151         "#define make_uint2 (uint2)\n"
152         "#define make_int2 (int2)\n"
153         "#define max2 max\n"
154         "#define min2 min\n"
155         "#define WG_SIZE 64\n"
156         "typedef struct \n"
157         "{\n"
158         "       int m_n;\n"
159         "       int m_start;\n"
160         "       int m_staticIdx;\n"
161         "       int m_paddings[1];\n"
162         "} ConstBuffer;\n"
163         "typedef struct \n"
164         "{\n"
165         "       int m_a;\n"
166         "       int m_b;\n"
167         "       u32 m_idx;\n"
168         "}Elem;\n"
169         "//     batching on the GPU\n"
170         "__kernel void CreateBatchesBruteForce( __global struct b3Contact4Data* gConstraints,   __global const u32* gN, __global const u32* gStart, int m_staticIdx )\n"
171         "{\n"
172         "       int wgIdx = GET_GROUP_IDX;\n"
173         "       int lIdx = GET_LOCAL_IDX;\n"
174         "       \n"
175         "       const int m_n = gN[wgIdx];\n"
176         "       const int m_start = gStart[wgIdx];\n"
177         "               \n"
178         "       if( lIdx == 0 )\n"
179         "       {\n"
180         "               for (int i=0;i<m_n;i++)\n"
181         "               {\n"
182         "                       int srcIdx = i+m_start;\n"
183         "                       int batchIndex = i;\n"
184         "                       gConstraints[ srcIdx ].m_batchIdx = batchIndex; \n"
185         "               }\n"
186         "       }\n"
187         "}\n"
188         "#define CHECK_SIZE (WG_SIZE)\n"
189         "u32 readBuf(__local u32* buff, int idx)\n"
190         "{\n"
191         "       idx = idx % (32*CHECK_SIZE);\n"
192         "       int bitIdx = idx%32;\n"
193         "       int bufIdx = idx/32;\n"
194         "       return buff[bufIdx] & (1<<bitIdx);\n"
195         "}\n"
196         "void writeBuf(__local u32* buff, int idx)\n"
197         "{\n"
198         "       idx = idx % (32*CHECK_SIZE);\n"
199         "       int bitIdx = idx%32;\n"
200         "       int bufIdx = idx/32;\n"
201         "       buff[bufIdx] |= (1<<bitIdx);\n"
202         "       //atom_or( &buff[bufIdx], (1<<bitIdx) );\n"
203         "}\n"
204         "u32 tryWrite(__local u32* buff, int idx)\n"
205         "{\n"
206         "       idx = idx % (32*CHECK_SIZE);\n"
207         "       int bitIdx = idx%32;\n"
208         "       int bufIdx = idx/32;\n"
209         "       u32 ans = (u32)atom_or( &buff[bufIdx], (1<<bitIdx) );\n"
210         "       return ((ans >> bitIdx)&1) == 0;\n"
211         "}\n"
212         "//     batching on the GPU\n"
213         "__kernel void CreateBatchesNew( __global struct b3Contact4Data* gConstraints, __global const u32* gN, __global const u32* gStart, __global int* batchSizes, int staticIdx )\n"
214         "{\n"
215         "       int wgIdx = GET_GROUP_IDX;\n"
216         "       int lIdx = GET_LOCAL_IDX;\n"
217         "       const int numConstraints = gN[wgIdx];\n"
218         "       const int m_start = gStart[wgIdx];\n"
219         "       b3Contact4Data_t tmp;\n"
220         "       \n"
221         "       __local u32 ldsFixedBuffer[CHECK_SIZE];\n"
222         "               \n"
223         "       \n"
224         "       \n"
225         "       \n"
226         "       \n"
227         "       if( lIdx == 0 )\n"
228         "       {\n"
229         "       \n"
230         "               \n"
231         "               __global struct b3Contact4Data* cs = &gConstraints[m_start];    \n"
232         "       \n"
233         "               \n"
234         "               int numValidConstraints = 0;\n"
235         "               int batchIdx = 0;\n"
236         "               while( numValidConstraints < numConstraints)\n"
237         "               {\n"
238         "                       int nCurrentBatch = 0;\n"
239         "                       //      clear flag\n"
240         "       \n"
241         "                       for(int i=0; i<CHECK_SIZE; i++) \n"
242         "                               ldsFixedBuffer[i] = 0;          \n"
243         "                       for(int i=numValidConstraints; i<numConstraints; i++)\n"
244         "                       {\n"
245         "                               int bodyAS = cs[i].m_bodyAPtrAndSignBit;\n"
246         "                               int bodyBS = cs[i].m_bodyBPtrAndSignBit;\n"
247         "                               int bodyA = abs(bodyAS);\n"
248         "                               int bodyB = abs(bodyBS);\n"
249         "                               bool aIsStatic = (bodyAS<0) || bodyAS==staticIdx;\n"
250         "                               bool bIsStatic = (bodyBS<0) || bodyBS==staticIdx;\n"
251         "                               int aUnavailable = aIsStatic ? 0 : readBuf( ldsFixedBuffer, bodyA);\n"
252         "                               int bUnavailable = bIsStatic ? 0 : readBuf( ldsFixedBuffer, bodyB);\n"
253         "                               \n"
254         "                               if( aUnavailable==0 && bUnavailable==0 ) // ok\n"
255         "                               {\n"
256         "                                       if (!aIsStatic)\n"
257         "                                       {\n"
258         "                                               writeBuf( ldsFixedBuffer, bodyA );\n"
259         "                                       }\n"
260         "                                       if (!bIsStatic)\n"
261         "                                       {\n"
262         "                                               writeBuf( ldsFixedBuffer, bodyB );\n"
263         "                                       }\n"
264         "                                       cs[i].m_batchIdx = batchIdx;\n"
265         "                                       if (i!=numValidConstraints)\n"
266         "                                       {\n"
267         "                                               tmp = cs[i];\n"
268         "                                               cs[i] = cs[numValidConstraints];\n"
269         "                                               cs[numValidConstraints]  = tmp;\n"
270         "                                       }\n"
271         "                                       numValidConstraints++;\n"
272         "                                       \n"
273         "                                       nCurrentBatch++;\n"
274         "                                       if( nCurrentBatch == SIMD_WIDTH)\n"
275         "                                       {\n"
276         "                                               nCurrentBatch = 0;\n"
277         "                                               for(int i=0; i<CHECK_SIZE; i++) \n"
278         "                                                       ldsFixedBuffer[i] = 0;\n"
279         "                                               \n"
280         "                                       }\n"
281         "                               }\n"
282         "                       }//for\n"
283         "                       batchIdx ++;\n"
284         "               }//while\n"
285         "               \n"
286         "               batchSizes[wgIdx] = batchIdx;\n"
287         "       }//if( lIdx == 0 )\n"
288         "       \n"
289         "       //return batchIdx;\n"
290         "}\n";