Initialize libbullet git in 2.0_beta.
[platform/upstream/libbullet.git] / Extras / RigidBodyGpuPipeline / dynamics / basic_demo / Stubs / batchingKernels.h
1 /*\r
2 Copyright (c) 2012 Advanced Micro Devices, Inc.  \r
3 \r
4 This software is provided 'as-is', without any express or implied warranty.\r
5 In no event will the authors be held liable for any damages arising from the use of this software.\r
6 Permission is granted to anyone to use this software for any purpose, \r
7 including commercial applications, and to alter it and redistribute it freely, \r
8 subject to the following restrictions:\r
9 \r
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.\r
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\r
12 3. This notice may not be removed or altered from any source distribution.\r
13 */\r
14 //Originally written by Takahiro Harada\r
15 \r
16 \r
17 static const char* batchingKernelsCL= \\r
18 "\n"\r
19 "#pragma OPENCL EXTENSION cl_amd_printf : enable\n"\r
20 "#pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable\n"\r
21 "#pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable\n"\r
22 "#pragma OPENCL EXTENSION cl_khr_local_int32_extended_atomics : enable\n"\r
23 "#pragma OPENCL EXTENSION cl_khr_global_int32_extended_atomics : enable\n"\r
24 "\n"\r
25 "#ifdef cl_ext_atomic_counters_32\n"\r
26 "#pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable\n"\r
27 "#else\n"\r
28 "#define counter32_t volatile __global int*\n"\r
29 "#endif\n"\r
30 "\n"\r
31 "\n"\r
32 "typedef unsigned int u32;\n"\r
33 "typedef unsigned short u16;\n"\r
34 "typedef unsigned char u8;\n"\r
35 "\n"\r
36 "#define GET_GROUP_IDX get_group_id(0)\n"\r
37 "#define GET_LOCAL_IDX get_local_id(0)\n"\r
38 "#define GET_GLOBAL_IDX get_global_id(0)\n"\r
39 "#define GET_GROUP_SIZE get_local_size(0)\n"\r
40 "#define GET_NUM_GROUPS get_num_groups(0)\n"\r
41 "#define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)\n"\r
42 "#define GROUP_MEM_FENCE mem_fence(CLK_LOCAL_MEM_FENCE)\n"\r
43 "#define AtomInc(x) atom_inc(&(x))\n"\r
44 "#define AtomInc1(x, out) out = atom_inc(&(x))\n"\r
45 "#define AppendInc(x, out) out = atomic_inc(x)\n"\r
46 "#define AtomAdd(x, value) atom_add(&(x), value)\n"\r
47 "#define AtomCmpxhg(x, cmp, value) atom_cmpxchg( &(x), cmp, value )\n"\r
48 "#define AtomXhg(x, value) atom_xchg ( &(x), value )\n"\r
49 "\n"\r
50 "\n"\r
51 "#define SELECT_UINT4( b, a, condition ) select( b,a,condition )\n"\r
52 "\n"\r
53 "#define make_float4 (float4)\n"\r
54 "#define make_float2 (float2)\n"\r
55 "#define make_uint4 (uint4)\n"\r
56 "#define make_int4 (int4)\n"\r
57 "#define make_uint2 (uint2)\n"\r
58 "#define make_int2 (int2)\n"\r
59 "\n"\r
60 "\n"\r
61 "#define max2 max\n"\r
62 "#define min2 min\n"\r
63 "\n"\r
64 "\n"\r
65 "#define WG_SIZE 64\n"\r
66 "\n"\r
67 "\n"\r
68 "\n"\r
69 "typedef struct \n"\r
70 "{\n"\r
71 "       float4 m_worldPos[4];\n"\r
72 "       float4 m_worldNormal;\n"\r
73 "       u32 m_coeffs;\n"\r
74 "       int m_batchIdx;\n"\r
75 "\n"\r
76 "       u32 m_bodyA;\n"\r
77 "       u32 m_bodyB;\n"\r
78 "}Contact4;\n"\r
79 "\n"\r
80 "typedef struct \n"\r
81 "{\n"\r
82 "       int m_n;\n"\r
83 "       int m_start;\n"\r
84 "       int m_staticIdx;\n"\r
85 "       int m_paddings[1];\n"\r
86 "} ConstBuffer;\n"\r
87 "\n"\r
88 "typedef struct \n"\r
89 "{\n"\r
90 "       u32 m_a;\n"\r
91 "       u32 m_b;\n"\r
92 "       u32 m_idx;\n"\r
93 "}Elem;\n"\r
94 "\n"\r
95 "#define STACK_SIZE (WG_SIZE*10)\n"\r
96 "//#define STACK_SIZE (WG_SIZE)\n"\r
97 "#define RING_SIZE 1024\n"\r
98 "#define RING_SIZE_MASK (RING_SIZE-1)\n"\r
99 "#define CHECK_SIZE (WG_SIZE)\n"\r
100 "\n"\r
101 "\n"\r
102 "#define GET_RING_CAPACITY (RING_SIZE - ldsRingEnd)\n"\r
103 "#define RING_END ldsTmp\n"\r
104 "\n"\r
105 "u32 readBuf(__local u32* buff, int idx)\n"\r
106 "{\n"\r
107 "       idx = idx % (32*CHECK_SIZE);\n"\r
108 "       int bitIdx = idx%32;\n"\r
109 "       int bufIdx = idx/32;\n"\r
110 "       return buff[bufIdx] & (1<<bitIdx);\n"\r
111 "}\n"\r
112 "\n"\r
113 "void writeBuf(__local u32* buff, int idx)\n"\r
114 "{\n"\r
115 "       idx = idx % (32*CHECK_SIZE);\n"\r
116 "       int bitIdx = idx%32;\n"\r
117 "       int bufIdx = idx/32;\n"\r
118 "//     buff[bufIdx] |= (1<<bitIdx);\n"\r
119 "       atom_or( &buff[bufIdx], (1<<bitIdx) );\n"\r
120 "}\n"\r
121 "\n"\r
122 "u32 tryWrite(__local u32* buff, int idx)\n"\r
123 "{\n"\r
124 "       idx = idx % (32*CHECK_SIZE);\n"\r
125 "       int bitIdx = idx%32;\n"\r
126 "       int bufIdx = idx/32;\n"\r
127 "       u32 ans = (u32)atom_or( &buff[bufIdx], (1<<bitIdx) );\n"\r
128 "       return ((ans >> bitIdx)&1) == 0;\n"\r
129 "}\n"\r
130 "\n"\r
131 "typedef struct \n"\r
132 "{\n"\r
133 "       int m_valInt0;\n"\r
134 "       int m_valInt1;\n"\r
135 "       int m_valInt2;\n"\r
136 "       int m_valInt3;\n"\r
137 "\n"\r
138 "       int m_valInt4;\n"\r
139 "       int m_valInt5;\n"\r
140 "       int m_valInt6;\n"\r
141 "       int m_valInt7;\n"\r
142 "\n"\r
143 "       int m_valInt8;\n"\r
144 "       int m_valInt9;\n"\r
145 "       int m_valInt10;\n"\r
146 "       int m_valInt11;\n"\r
147 "       \n"\r
148 "       int     m_valInt12;\n"\r
149 "       int     m_valInt13;\n"\r
150 "       int     m_valInt14;\n"\r
151 "       int     m_valInt15;\n"\r
152 "\n"\r
153 "\n"\r
154 "       float m_fval0;\n"\r
155 "       float m_fval1;\n"\r
156 "       float m_fval2;\n"\r
157 "       float m_fval3;\n"\r
158 "} SolverDebugInfo;\n"\r
159 "\n"\r
160 "//     batching on the GPU\n"\r
161 "__kernel void CreateBatches( __global Contact4* gConstraints, __global Contact4* gConstraintsOut, //__global u32* gRes, \n"\r
162 "               __global u32* gN, __global u32* gStart, \n"\r
163 "//             __global SolverDebugInfo* debugInfo, \n"\r
164 "               ConstBuffer cb )\n"\r
165 "{\n"\r
166 "       __local u32 ldsStackIdx[STACK_SIZE];\n"\r
167 "       __local u32 ldsStackEnd;\n"\r
168 "       __local Elem ldsRingElem[RING_SIZE];\n"\r
169 "       __local u32 ldsRingEnd;\n"\r
170 "       __local u32 ldsTmp;\n"\r
171 "       __local u32 ldsCheckBuffer[CHECK_SIZE];\n"\r
172 "       __local u32 ldsFixedBuffer[CHECK_SIZE];\n"\r
173 "       __local u32 ldsGEnd;\n"\r
174 "       __local u32 ldsDstEnd;\n"\r
175 "\n"\r
176 "       int wgIdx = GET_GROUP_IDX;\n"\r
177 "       int lIdx = GET_LOCAL_IDX;\n"\r
178 "       \n"\r
179 "       const int m_n = gN[wgIdx];\n"\r
180 "       const int m_start = gStart[wgIdx];\n"\r
181 "       const int m_staticIdx = cb.m_staticIdx;\n"\r
182 "               \n"\r
183 "       if( lIdx == 0 )\n"\r
184 "       {\n"\r
185 "               ldsRingEnd = 0;\n"\r
186 "               ldsGEnd = 0;\n"\r
187 "               ldsStackEnd = 0;\n"\r
188 "               ldsDstEnd = m_start;\n"\r
189 "       }\n"\r
190 "       \n"\r
191 "//     while(1)\n"\r
192 "       for(int ie=0; ie<250; ie++)\n"\r
193 "       {\n"\r
194 "               ldsFixedBuffer[lIdx] = 0;\n"\r
195 "\n"\r
196 "               for(int giter=0; giter<4; giter++)\n"\r
197 "               {\n"\r
198 "                       int ringCap = GET_RING_CAPACITY;\n"\r
199 "               \n"\r
200 "                       //      1. fill ring\n"\r
201 "                       if( ldsGEnd < m_n )\n"\r
202 "                       {\n"\r
203 "                               while( ringCap > WG_SIZE )\n"\r
204 "                               {\n"\r
205 "                                       if( ldsGEnd >= m_n ) break;\n"\r
206 "                                       if( lIdx < ringCap - WG_SIZE )\n"\r
207 "                                       {\n"\r
208 "                                               int srcIdx;\n"\r
209 "                                               AtomInc1( ldsGEnd, srcIdx );\n"\r
210 "                                               if( srcIdx < m_n )\n"\r
211 "                                               {\n"\r
212 "                                                       int dstIdx;\n"\r
213 "                                                       AtomInc1( ldsRingEnd, dstIdx );\n"\r
214 "                                                       \n"\r
215 "                                                       int a = gConstraints[m_start+srcIdx].m_bodyA;\n"\r
216 "                                                       int b = gConstraints[m_start+srcIdx].m_bodyB;\n"\r
217 "                                                       ldsRingElem[dstIdx].m_a = (a>b)? b:a;\n"\r
218 "                                                       ldsRingElem[dstIdx].m_b = (a>b)? a:b;\n"\r
219 "                                                       ldsRingElem[dstIdx].m_idx = srcIdx;\n"\r
220 "                                               }\n"\r
221 "                                       }\n"\r
222 "                                       ringCap = GET_RING_CAPACITY;\n"\r
223 "                               }\n"\r
224 "                       }\n"\r
225 "\n"\r
226 "                       GROUP_LDS_BARRIER;\n"\r
227 "       \n"\r
228 "                       //      2. fill stack\n"\r
229 "                       __local Elem* dst = ldsRingElem;\n"\r
230 "                       if( lIdx == 0 ) RING_END = 0;\n"\r
231 "\n"\r
232 "                       int srcIdx=lIdx;\n"\r
233 "                       int end = ldsRingEnd;\n"\r
234 "\n"\r
235 "                       {\n"\r
236 "                               for(int ii=0; ii<end; ii+=WG_SIZE, srcIdx+=WG_SIZE)\n"\r
237 "                               {\n"\r
238 "                                       Elem e;\n"\r
239 "                                       if(srcIdx<end) e = ldsRingElem[srcIdx];\n"\r
240 "                                       bool done = (srcIdx<end)?false:true;\n"\r
241 "\n"\r
242 "                                       for(int i=lIdx; i<CHECK_SIZE; i+=WG_SIZE) ldsCheckBuffer[lIdx] = 0;\n"\r
243 "                                       \n"\r
244 "                                       if( !done )\n"\r
245 "                                       {\n"\r
246 "                                               int aUsed = readBuf( ldsFixedBuffer, e.m_a);\n"\r
247 "                                               int bUsed = readBuf( ldsFixedBuffer, e.m_b);\n"\r
248 "\n"\r
249 "                                               if( aUsed==0 && bUsed==0 )\n"\r
250 "                                               {\n"\r
251 "                                                       int aAvailable;\n"\r
252 "                                                       int bAvailable;\n"\r
253 "\n"\r
254 "                                                       aAvailable = tryWrite( ldsCheckBuffer, e.m_a );\n"\r
255 "                                                       bAvailable = tryWrite( ldsCheckBuffer, e.m_b );\n"\r
256 "\n"\r
257 "                                                       //aAvailable = (m_staticIdx == e.m_a)? 1: aAvailable;\n"\r
258 "                                                       //bAvailable = (m_staticIdx == e.m_b)? 1: bAvailable;\n"\r
259 "\n"\r
260 "                                                       bool success = (aAvailable && bAvailable);\n"\r
261 "                                                       if(success)\n"\r
262 "                                                       {\n"\r
263 "                                                               writeBuf( ldsFixedBuffer, e.m_a );\n"\r
264 "                                                               writeBuf( ldsFixedBuffer, e.m_b );\n"\r
265 "                                                       }\n"\r
266 "                                                       done = success;\n"\r
267 "                                               }\n"\r
268 "                                       }\n"\r
269 "\n"\r
270 "                                       //      put it aside\n"\r
271 "                                       if(srcIdx<end)\n"\r
272 "                                       {\n"\r
273 "                                               if( done )\n"\r
274 "                                               {\n"\r
275 "                                                       int dstIdx; AtomInc1( ldsStackEnd, dstIdx );\n"\r
276 "                                                       if( dstIdx < STACK_SIZE )\n"\r
277 "                                                               ldsStackIdx[dstIdx] = e.m_idx;\n"\r
278 "                                                       else{\n"\r
279 "                                                               done = false;\n"\r
280 "                                                               AtomAdd( ldsStackEnd, -1 );\n"\r
281 "                                                       }\n"\r
282 "                                               }\n"\r
283 "                                               if( !done )\n"\r
284 "                                               {\n"\r
285 "                                                       int dstIdx; AtomInc1( RING_END, dstIdx );\n"\r
286 "                                                       dst[dstIdx] = e;\n"\r
287 "                                               }\n"\r
288 "                                       }\n"\r
289 "\n"\r
290 "                                       //      if filled, flush\n"\r
291 "                                       if( ldsStackEnd == STACK_SIZE )\n"\r
292 "                                       {\n"\r
293 "                                               for(int i=lIdx; i<STACK_SIZE; i+=WG_SIZE)\n"\r
294 "                                               {\n"\r
295 "                                                       int idx = m_start + ldsStackIdx[i];\n"\r
296 "                                                       int dstIdx; AtomInc1( ldsDstEnd, dstIdx );\n"\r
297 "                                                       gConstraintsOut[ dstIdx ] = gConstraints[ idx ];\n"\r
298 "                                                       gConstraintsOut[ dstIdx ].m_batchIdx = ie;\n"\r
299 "                                               }\n"\r
300 "                                               if( lIdx == 0 ) ldsStackEnd = 0;\n"\r
301 "\n"\r
302 "                                               //for(int i=lIdx; i<CHECK_SIZE; i+=WG_SIZE) \n"\r
303 "                                               ldsFixedBuffer[lIdx] = 0;\n"\r
304 "                                       }\n"\r
305 "                               }\n"\r
306 "                       }\n"\r
307 "\n"\r
308 "                       if( lIdx == 0 ) ldsRingEnd = RING_END;\n"\r
309 "               }\n"\r
310 "\n"\r
311 "               GROUP_LDS_BARRIER;\n"\r
312 "\n"\r
313 "               for(int i=lIdx; i<ldsStackEnd; i+=WG_SIZE)\n"\r
314 "               {\n"\r
315 "                       int idx = m_start + ldsStackIdx[i];\n"\r
316 "                       int dstIdx; AtomInc1( ldsDstEnd, dstIdx );\n"\r
317 "                       gConstraintsOut[ dstIdx ] = gConstraints[ idx ];\n"\r
318 "                       gConstraintsOut[ dstIdx ].m_batchIdx = ie;\n"\r
319 "               }\n"\r
320 "\n"\r
321 "               //      in case it couldn't consume any pair. Flush them\n"\r
322 "               //      todo. Serial batch worth while?\n"\r
323 "               if( ldsStackEnd == 0 )\n"\r
324 "               {\n"\r
325 "                       for(int i=lIdx; i<ldsRingEnd; i+=WG_SIZE)\n"\r
326 "                       {\n"\r
327 "                               int idx = m_start + ldsRingElem[i].m_idx;\n"\r
328 "                               int dstIdx; AtomInc1( ldsDstEnd, dstIdx );\n"\r
329 "                               gConstraintsOut[ dstIdx ] = gConstraints[ idx ];\n"\r
330 "                               gConstraintsOut[ dstIdx ].m_batchIdx = 100+i;\n"\r
331 "                       }\n"\r
332 "                       GROUP_LDS_BARRIER;\n"\r
333 "                       if( lIdx == 0 ) ldsRingEnd = 0;\n"\r
334 "               }\n"\r
335 "\n"\r
336 "               if( lIdx == 0 ) ldsStackEnd = 0;\n"\r
337 "\n"\r
338 "               GROUP_LDS_BARRIER;\n"\r
339 "\n"\r
340 "               //      termination\n"\r
341 "               if( ldsGEnd == m_n && ldsRingEnd == 0 )\n"\r
342 "                       break;\n"\r
343 "       }\n"\r
344 "\n"\r
345 "\n"\r
346 "}\n"\r
347 "\n"\r
348 "\n"\r
349 "\n"\r
350 "\n"\r
351 "\n"\r
352 "\n"\r
353 "\n"\r
354 "\n"\r
355 "\n"\r
356 "\n"\r
357 "\n"\r
358 "\n"\r
359 "\n"\r
360 "\n"\r
361 "\n"\r
362 "\n"\r
363 "\n"\r
364 "\n"\r
365 "\n"\r
366 "\n"\r
367 "\n"\r
368 "\n"\r
369 "\n"\r
370 "\n"\r
371 ;\r