[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / Bullet3OpenCL / BroadphaseCollision / kernels / sapKernels.h
1 //this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
2 static const char* sapCL =
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         "#define NEW_PAIR_MARKER -1\n"
16         "typedef struct \n"
17         "{\n"
18         "       union\n"
19         "       {\n"
20         "               float4  m_min;\n"
21         "               float   m_minElems[4];\n"
22         "               int                     m_minIndices[4];\n"
23         "       };\n"
24         "       union\n"
25         "       {\n"
26         "               float4  m_max;\n"
27         "               float   m_maxElems[4];\n"
28         "               int                     m_maxIndices[4];\n"
29         "       };\n"
30         "} btAabbCL;\n"
31         "/// conservative test for overlap between two aabbs\n"
32         "bool TestAabbAgainstAabb2(const btAabbCL* aabb1, __local const btAabbCL* aabb2);\n"
33         "bool TestAabbAgainstAabb2(const btAabbCL* aabb1, __local const btAabbCL* aabb2)\n"
34         "{\n"
35         "       bool overlap = true;\n"
36         "       overlap = (aabb1->m_min.x > aabb2->m_max.x || aabb1->m_max.x < aabb2->m_min.x) ? false : overlap;\n"
37         "       overlap = (aabb1->m_min.z > aabb2->m_max.z || aabb1->m_max.z < aabb2->m_min.z) ? false : overlap;\n"
38         "       overlap = (aabb1->m_min.y > aabb2->m_max.y || aabb1->m_max.y < aabb2->m_min.y) ? false : overlap;\n"
39         "       return overlap;\n"
40         "}\n"
41         "bool TestAabbAgainstAabb2GlobalGlobal(__global const btAabbCL* aabb1, __global const btAabbCL* aabb2);\n"
42         "bool TestAabbAgainstAabb2GlobalGlobal(__global const btAabbCL* aabb1, __global const btAabbCL* aabb2)\n"
43         "{\n"
44         "       bool overlap = true;\n"
45         "       overlap = (aabb1->m_min.x > aabb2->m_max.x || aabb1->m_max.x < aabb2->m_min.x) ? false : overlap;\n"
46         "       overlap = (aabb1->m_min.z > aabb2->m_max.z || aabb1->m_max.z < aabb2->m_min.z) ? false : overlap;\n"
47         "       overlap = (aabb1->m_min.y > aabb2->m_max.y || aabb1->m_max.y < aabb2->m_min.y) ? false : overlap;\n"
48         "       return overlap;\n"
49         "}\n"
50         "bool TestAabbAgainstAabb2Global(const btAabbCL* aabb1, __global const btAabbCL* aabb2);\n"
51         "bool TestAabbAgainstAabb2Global(const btAabbCL* aabb1, __global const btAabbCL* aabb2)\n"
52         "{\n"
53         "       bool overlap = true;\n"
54         "       overlap = (aabb1->m_min.x > aabb2->m_max.x || aabb1->m_max.x < aabb2->m_min.x) ? false : overlap;\n"
55         "       overlap = (aabb1->m_min.z > aabb2->m_max.z || aabb1->m_max.z < aabb2->m_min.z) ? false : overlap;\n"
56         "       overlap = (aabb1->m_min.y > aabb2->m_max.y || aabb1->m_max.y < aabb2->m_min.y) ? false : overlap;\n"
57         "       return overlap;\n"
58         "}\n"
59         "__kernel void   computePairsKernelTwoArrays( __global const btAabbCL* unsortedAabbs, __global const int* unsortedAabbMapping,  __global const int* unsortedAabbMapping2, volatile __global int4* pairsOut,volatile  __global int* pairCount, int numUnsortedAabbs, int numUnSortedAabbs2, int axis, int maxPairs)\n"
60         "{\n"
61         "       int i = get_global_id(0);\n"
62         "       if (i>=numUnsortedAabbs)\n"
63         "               return;\n"
64         "       int j = get_global_id(1);\n"
65         "       if (j>=numUnSortedAabbs2)\n"
66         "               return;\n"
67         "       __global const btAabbCL* unsortedAabbPtr = &unsortedAabbs[unsortedAabbMapping[i]];\n"
68         "       __global const btAabbCL* unsortedAabbPtr2 = &unsortedAabbs[unsortedAabbMapping2[j]];\n"
69         "       if (TestAabbAgainstAabb2GlobalGlobal(unsortedAabbPtr,unsortedAabbPtr2))\n"
70         "       {\n"
71         "               int4 myPair;\n"
72         "               \n"
73         "               int xIndex = unsortedAabbPtr[0].m_minIndices[3];\n"
74         "               int yIndex = unsortedAabbPtr2[0].m_minIndices[3];\n"
75         "               if (xIndex>yIndex)\n"
76         "               {\n"
77         "                       int tmp = xIndex;\n"
78         "                       xIndex=yIndex;\n"
79         "                       yIndex=tmp;\n"
80         "               }\n"
81         "               \n"
82         "               myPair.x = xIndex;\n"
83         "               myPair.y = yIndex;\n"
84         "               myPair.z = NEW_PAIR_MARKER;\n"
85         "               myPair.w = NEW_PAIR_MARKER;\n"
86         "               int curPair = atomic_inc (pairCount);\n"
87         "               if (curPair<maxPairs)\n"
88         "               {\n"
89         "                               pairsOut[curPair] = myPair; //flush to main memory\n"
90         "               }\n"
91         "       }\n"
92         "}\n"
93         "__kernel void   computePairsKernelBruteForce( __global const btAabbCL* aabbs, volatile __global int4* pairsOut,volatile  __global int* pairCount, int numObjects, int axis, int maxPairs)\n"
94         "{\n"
95         "       int i = get_global_id(0);\n"
96         "       if (i>=numObjects)\n"
97         "               return;\n"
98         "       for (int j=i+1;j<numObjects;j++)\n"
99         "       {\n"
100         "               if (TestAabbAgainstAabb2GlobalGlobal(&aabbs[i],&aabbs[j]))\n"
101         "               {\n"
102         "                       int4 myPair;\n"
103         "                       myPair.x = aabbs[i].m_minIndices[3];\n"
104         "                       myPair.y = aabbs[j].m_minIndices[3];\n"
105         "                       myPair.z = NEW_PAIR_MARKER;\n"
106         "                       myPair.w = NEW_PAIR_MARKER;\n"
107         "                       int curPair = atomic_inc (pairCount);\n"
108         "                       if (curPair<maxPairs)\n"
109         "                       {\n"
110         "                                       pairsOut[curPair] = myPair; //flush to main memory\n"
111         "                       }\n"
112         "               }\n"
113         "       }\n"
114         "}\n"
115         "__kernel void   computePairsKernelOriginal( __global const btAabbCL* aabbs, volatile __global int4* pairsOut,volatile  __global int* pairCount, int numObjects, int axis, int maxPairs)\n"
116         "{\n"
117         "       int i = get_global_id(0);\n"
118         "       if (i>=numObjects)\n"
119         "               return;\n"
120         "       for (int j=i+1;j<numObjects;j++)\n"
121         "       {\n"
122         "       if(aabbs[i].m_maxElems[axis] < (aabbs[j].m_minElems[axis])) \n"
123         "               {\n"
124         "                       break;\n"
125         "               }\n"
126         "               if (TestAabbAgainstAabb2GlobalGlobal(&aabbs[i],&aabbs[j]))\n"
127         "               {\n"
128         "                       int4 myPair;\n"
129         "                       myPair.x = aabbs[i].m_minIndices[3];\n"
130         "                       myPair.y = aabbs[j].m_minIndices[3];\n"
131         "                       myPair.z = NEW_PAIR_MARKER;\n"
132         "                       myPair.w = NEW_PAIR_MARKER;\n"
133         "                       int curPair = atomic_inc (pairCount);\n"
134         "                       if (curPair<maxPairs)\n"
135         "                       {\n"
136         "                                       pairsOut[curPair] = myPair; //flush to main memory\n"
137         "                       }\n"
138         "               }\n"
139         "       }\n"
140         "}\n"
141         "__kernel void   computePairsKernelBarrier( __global const btAabbCL* aabbs, volatile __global int4* pairsOut,volatile  __global int* pairCount, int numObjects, int axis, int maxPairs)\n"
142         "{\n"
143         "       int i = get_global_id(0);\n"
144         "       int localId = get_local_id(0);\n"
145         "       __local int numActiveWgItems[1];\n"
146         "       __local int breakRequest[1];\n"
147         "       if (localId==0)\n"
148         "       {\n"
149         "               numActiveWgItems[0] = 0;\n"
150         "               breakRequest[0] = 0;\n"
151         "       }\n"
152         "       barrier(CLK_LOCAL_MEM_FENCE);\n"
153         "       atomic_inc(numActiveWgItems);\n"
154         "       barrier(CLK_LOCAL_MEM_FENCE);\n"
155         "       int localBreak = 0;\n"
156         "       int j=i+1;\n"
157         "       do\n"
158         "       {\n"
159         "               barrier(CLK_LOCAL_MEM_FENCE);\n"
160         "       \n"
161         "               if (j<numObjects)\n"
162         "               {\n"
163         "               if(aabbs[i].m_maxElems[axis] < (aabbs[j].m_minElems[axis])) \n"
164         "                       {\n"
165         "                               if (!localBreak)\n"
166         "                               {\n"
167         "                                       atomic_inc(breakRequest);\n"
168         "                                       localBreak = 1;\n"
169         "                               }\n"
170         "                       }\n"
171         "               }\n"
172         "               \n"
173         "               barrier(CLK_LOCAL_MEM_FENCE);\n"
174         "               \n"
175         "               if (j>=numObjects && !localBreak)\n"
176         "               {\n"
177         "                       atomic_inc(breakRequest);\n"
178         "                       localBreak = 1;\n"
179         "               }\n"
180         "               barrier(CLK_LOCAL_MEM_FENCE);\n"
181         "               \n"
182         "               if (!localBreak)\n"
183         "               {\n"
184         "                       if (TestAabbAgainstAabb2GlobalGlobal(&aabbs[i],&aabbs[j]))\n"
185         "                       {\n"
186         "                               int4 myPair;\n"
187         "                               myPair.x = aabbs[i].m_minIndices[3];\n"
188         "                               myPair.y = aabbs[j].m_minIndices[3];\n"
189         "                               myPair.z = NEW_PAIR_MARKER;\n"
190         "                               myPair.w = NEW_PAIR_MARKER;\n"
191         "                               int curPair = atomic_inc (pairCount);\n"
192         "                               if (curPair<maxPairs)\n"
193         "                               {\n"
194         "                                               pairsOut[curPair] = myPair; //flush to main memory\n"
195         "                               }\n"
196         "                       }\n"
197         "               }\n"
198         "               j++;\n"
199         "       } while (breakRequest[0]<numActiveWgItems[0]);\n"
200         "}\n"
201         "__kernel void   computePairsKernelLocalSharedMemory( __global const btAabbCL* aabbs, volatile __global int4* pairsOut,volatile  __global int* pairCount, int numObjects, int axis, int maxPairs)\n"
202         "{\n"
203         "       int i = get_global_id(0);\n"
204         "       int localId = get_local_id(0);\n"
205         "       __local int numActiveWgItems[1];\n"
206         "       __local int breakRequest[1];\n"
207         "       __local btAabbCL localAabbs[128];// = aabbs[i];\n"
208         "       \n"
209         "       btAabbCL myAabb;\n"
210         "       \n"
211         "       myAabb = (i<numObjects)? aabbs[i]:aabbs[0];\n"
212         "       float testValue =       myAabb.m_maxElems[axis];\n"
213         "       \n"
214         "       if (localId==0)\n"
215         "       {\n"
216         "               numActiveWgItems[0] = 0;\n"
217         "               breakRequest[0] = 0;\n"
218         "       }\n"
219         "       int localCount=0;\n"
220         "       int block=0;\n"
221         "       localAabbs[localId] = (i+block)<numObjects? aabbs[i+block] : aabbs[0];\n"
222         "       localAabbs[localId+64] = (i+block+64)<numObjects? aabbs[i+block+64]: aabbs[0];\n"
223         "       \n"
224         "       barrier(CLK_LOCAL_MEM_FENCE);\n"
225         "       atomic_inc(numActiveWgItems);\n"
226         "       barrier(CLK_LOCAL_MEM_FENCE);\n"
227         "       int localBreak = 0;\n"
228         "       \n"
229         "       int j=i+1;\n"
230         "       do\n"
231         "       {\n"
232         "               barrier(CLK_LOCAL_MEM_FENCE);\n"
233         "       \n"
234         "               if (j<numObjects)\n"
235         "               {\n"
236         "               if(testValue < (localAabbs[localCount+localId+1].m_minElems[axis])) \n"
237         "                       {\n"
238         "                               if (!localBreak)\n"
239         "                               {\n"
240         "                                       atomic_inc(breakRequest);\n"
241         "                                       localBreak = 1;\n"
242         "                               }\n"
243         "                       }\n"
244         "               }\n"
245         "               \n"
246         "               barrier(CLK_LOCAL_MEM_FENCE);\n"
247         "               \n"
248         "               if (j>=numObjects && !localBreak)\n"
249         "               {\n"
250         "                       atomic_inc(breakRequest);\n"
251         "                       localBreak = 1;\n"
252         "               }\n"
253         "               barrier(CLK_LOCAL_MEM_FENCE);\n"
254         "               \n"
255         "               if (!localBreak)\n"
256         "               {\n"
257         "                       if (TestAabbAgainstAabb2(&myAabb,&localAabbs[localCount+localId+1]))\n"
258         "                       {\n"
259         "                               int4 myPair;\n"
260         "                               myPair.x = myAabb.m_minIndices[3];\n"
261         "                               myPair.y = localAabbs[localCount+localId+1].m_minIndices[3];\n"
262         "                               myPair.z = NEW_PAIR_MARKER;\n"
263         "                               myPair.w = NEW_PAIR_MARKER;\n"
264         "                               int curPair = atomic_inc (pairCount);\n"
265         "                               if (curPair<maxPairs)\n"
266         "                               {\n"
267         "                                               pairsOut[curPair] = myPair; //flush to main memory\n"
268         "                               }\n"
269         "                       }\n"
270         "               }\n"
271         "               \n"
272         "               barrier(CLK_LOCAL_MEM_FENCE);\n"
273         "               localCount++;\n"
274         "               if (localCount==64)\n"
275         "               {\n"
276         "                       localCount = 0;\n"
277         "                       block+=64;                      \n"
278         "                       localAabbs[localId] = ((i+block)<numObjects) ? aabbs[i+block] : aabbs[0];\n"
279         "                       localAabbs[localId+64] = ((i+64+block)<numObjects) ? aabbs[i+block+64] : aabbs[0];\n"
280         "               }\n"
281         "               j++;\n"
282         "               \n"
283         "       } while (breakRequest[0]<numActiveWgItems[0]);\n"
284         "       \n"
285         "}\n"
286         "//http://stereopsis.com/radix.html\n"
287         "unsigned int FloatFlip(float fl);\n"
288         "unsigned int FloatFlip(float fl)\n"
289         "{\n"
290         "       unsigned int f = *(unsigned int*)&fl;\n"
291         "       unsigned int mask = -(int)(f >> 31) | 0x80000000;\n"
292         "       return f ^ mask;\n"
293         "}\n"
294         "float IFloatFlip(unsigned int f);\n"
295         "float IFloatFlip(unsigned int f)\n"
296         "{\n"
297         "       unsigned int mask = ((f >> 31) - 1) | 0x80000000;\n"
298         "       unsigned int fl = f ^ mask;\n"
299         "       return *(float*)&fl;\n"
300         "}\n"
301         "__kernel void   copyAabbsKernel( __global const btAabbCL* allAabbs, __global btAabbCL* destAabbs, int numObjects)\n"
302         "{\n"
303         "       int i = get_global_id(0);\n"
304         "       if (i>=numObjects)\n"
305         "               return;\n"
306         "       int src = destAabbs[i].m_maxIndices[3];\n"
307         "       destAabbs[i] = allAabbs[src];\n"
308         "       destAabbs[i].m_maxIndices[3] = src;\n"
309         "}\n"
310         "__kernel void   flipFloatKernel( __global const btAabbCL* allAabbs, __global const int* smallAabbMapping, __global int2* sortData, int numObjects, int axis)\n"
311         "{\n"
312         "       int i = get_global_id(0);\n"
313         "       if (i>=numObjects)\n"
314         "               return;\n"
315         "       \n"
316         "       \n"
317         "       sortData[i].x = FloatFlip(allAabbs[smallAabbMapping[i]].m_minElems[axis]);\n"
318         "       sortData[i].y = i;\n"
319         "               \n"
320         "}\n"
321         "__kernel void   scatterKernel( __global const btAabbCL* allAabbs, __global const int* smallAabbMapping, volatile __global const int2* sortData, __global btAabbCL* sortedAabbs, int numObjects)\n"
322         "{\n"
323         "       int i = get_global_id(0);\n"
324         "       if (i>=numObjects)\n"
325         "               return;\n"
326         "       \n"
327         "       sortedAabbs[i] = allAabbs[smallAabbMapping[sortData[i].y]];\n"
328         "}\n"
329         "__kernel void   prepareSumVarianceKernel( __global const btAabbCL* allAabbs, __global const int* smallAabbMapping, __global float4* sum, __global float4* sum2,int numAabbs)\n"
330         "{\n"
331         "       int i = get_global_id(0);\n"
332         "       if (i>=numAabbs)\n"
333         "               return;\n"
334         "       \n"
335         "       btAabbCL smallAabb = allAabbs[smallAabbMapping[i]];\n"
336         "       \n"
337         "       float4 s;\n"
338         "       s = (smallAabb.m_max+smallAabb.m_min)*0.5f;\n"
339         "       sum[i]=s;\n"
340         "       sum2[i]=s*s;    \n"
341         "}\n";