[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / Bullet3OpenCL / NarrowphaseCollision / kernels / satKernels.h
1 //this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
2 static const char* satKernelsCL =
3         "//keep this enum in sync with the CPU version (in btCollidable.h)\n"
4         "//written by Erwin Coumans\n"
5         "#define SHAPE_CONVEX_HULL 3\n"
6         "#define SHAPE_CONCAVE_TRIMESH 5\n"
7         "#define TRIANGLE_NUM_CONVEX_FACES 5\n"
8         "#define SHAPE_COMPOUND_OF_CONVEX_HULLS 6\n"
9         "#define B3_MAX_STACK_DEPTH 256\n"
10         "typedef unsigned int u32;\n"
11         "///keep this in sync with btCollidable.h\n"
12         "typedef struct\n"
13         "{\n"
14         "       union {\n"
15         "               int m_numChildShapes;\n"
16         "               int m_bvhIndex;\n"
17         "       };\n"
18         "       union\n"
19         "       {\n"
20         "               float m_radius;\n"
21         "               int     m_compoundBvhIndex;\n"
22         "       };\n"
23         "       \n"
24         "       int m_shapeType;\n"
25         "       int m_shapeIndex;\n"
26         "       \n"
27         "} btCollidableGpu;\n"
28         "#define MAX_NUM_PARTS_IN_BITS 10\n"
29         "///b3QuantizedBvhNode is a compressed aabb node, 16 bytes.\n"
30         "///Node can be used for leafnode or internal node. Leafnodes can point to 32-bit triangle index (non-negative range).\n"
31         "typedef struct\n"
32         "{\n"
33         "       //12 bytes\n"
34         "       unsigned short int      m_quantizedAabbMin[3];\n"
35         "       unsigned short int      m_quantizedAabbMax[3];\n"
36         "       //4 bytes\n"
37         "       int     m_escapeIndexOrTriangleIndex;\n"
38         "} b3QuantizedBvhNode;\n"
39         "typedef struct\n"
40         "{\n"
41         "       float4          m_aabbMin;\n"
42         "       float4          m_aabbMax;\n"
43         "       float4          m_quantization;\n"
44         "       int                     m_numNodes;\n"
45         "       int                     m_numSubTrees;\n"
46         "       int                     m_nodeOffset;\n"
47         "       int                     m_subTreeOffset;\n"
48         "} b3BvhInfo;\n"
49         "int    getTriangleIndex(const b3QuantizedBvhNode* rootNode)\n"
50         "{\n"
51         "       unsigned int x=0;\n"
52         "       unsigned int y = (~(x&0))<<(31-MAX_NUM_PARTS_IN_BITS);\n"
53         "       // Get only the lower bits where the triangle index is stored\n"
54         "       return (rootNode->m_escapeIndexOrTriangleIndex&~(y));\n"
55         "}\n"
56         "int    getTriangleIndexGlobal(__global const b3QuantizedBvhNode* rootNode)\n"
57         "{\n"
58         "       unsigned int x=0;\n"
59         "       unsigned int y = (~(x&0))<<(31-MAX_NUM_PARTS_IN_BITS);\n"
60         "       // Get only the lower bits where the triangle index is stored\n"
61         "       return (rootNode->m_escapeIndexOrTriangleIndex&~(y));\n"
62         "}\n"
63         "int isLeafNode(const b3QuantizedBvhNode* rootNode)\n"
64         "{\n"
65         "       //skipindex is negative (internal node), triangleindex >=0 (leafnode)\n"
66         "       return (rootNode->m_escapeIndexOrTriangleIndex >= 0)? 1 : 0;\n"
67         "}\n"
68         "int isLeafNodeGlobal(__global const b3QuantizedBvhNode* rootNode)\n"
69         "{\n"
70         "       //skipindex is negative (internal node), triangleindex >=0 (leafnode)\n"
71         "       return (rootNode->m_escapeIndexOrTriangleIndex >= 0)? 1 : 0;\n"
72         "}\n"
73         "       \n"
74         "int getEscapeIndex(const b3QuantizedBvhNode* rootNode)\n"
75         "{\n"
76         "       return -rootNode->m_escapeIndexOrTriangleIndex;\n"
77         "}\n"
78         "int getEscapeIndexGlobal(__global const b3QuantizedBvhNode* rootNode)\n"
79         "{\n"
80         "       return -rootNode->m_escapeIndexOrTriangleIndex;\n"
81         "}\n"
82         "typedef struct\n"
83         "{\n"
84         "       //12 bytes\n"
85         "       unsigned short int      m_quantizedAabbMin[3];\n"
86         "       unsigned short int      m_quantizedAabbMax[3];\n"
87         "       //4 bytes, points to the root of the subtree\n"
88         "       int                     m_rootNodeIndex;\n"
89         "       //4 bytes\n"
90         "       int                     m_subtreeSize;\n"
91         "       int                     m_padding[3];\n"
92         "} b3BvhSubtreeInfo;\n"
93         "typedef struct\n"
94         "{\n"
95         "       float4  m_childPosition;\n"
96         "       float4  m_childOrientation;\n"
97         "       int m_shapeIndex;\n"
98         "       int m_unused0;\n"
99         "       int m_unused1;\n"
100         "       int m_unused2;\n"
101         "} btGpuChildShape;\n"
102         "typedef struct\n"
103         "{\n"
104         "       float4 m_pos;\n"
105         "       float4 m_quat;\n"
106         "       float4 m_linVel;\n"
107         "       float4 m_angVel;\n"
108         "       u32 m_collidableIdx;\n"
109         "       float m_invMass;\n"
110         "       float m_restituitionCoeff;\n"
111         "       float m_frictionCoeff;\n"
112         "} BodyData;\n"
113         "typedef struct  \n"
114         "{\n"
115         "       float4          m_localCenter;\n"
116         "       float4          m_extents;\n"
117         "       float4          mC;\n"
118         "       float4          mE;\n"
119         "       \n"
120         "       float                   m_radius;\n"
121         "       int     m_faceOffset;\n"
122         "       int m_numFaces;\n"
123         "       int     m_numVertices;\n"
124         "       int m_vertexOffset;\n"
125         "       int     m_uniqueEdgesOffset;\n"
126         "       int     m_numUniqueEdges;\n"
127         "       int m_unused;\n"
128         "} ConvexPolyhedronCL;\n"
129         "typedef struct \n"
130         "{\n"
131         "       union\n"
132         "       {\n"
133         "               float4  m_min;\n"
134         "               float   m_minElems[4];\n"
135         "               int                     m_minIndices[4];\n"
136         "       };\n"
137         "       union\n"
138         "       {\n"
139         "               float4  m_max;\n"
140         "               float   m_maxElems[4];\n"
141         "               int                     m_maxIndices[4];\n"
142         "       };\n"
143         "} btAabbCL;\n"
144         "#ifndef B3_AABB_H\n"
145         "#define B3_AABB_H\n"
146         "#ifndef B3_FLOAT4_H\n"
147         "#define B3_FLOAT4_H\n"
148         "#ifndef B3_PLATFORM_DEFINITIONS_H\n"
149         "#define B3_PLATFORM_DEFINITIONS_H\n"
150         "struct MyTest\n"
151         "{\n"
152         "       int bla;\n"
153         "};\n"
154         "#ifdef __cplusplus\n"
155         "#else\n"
156         "//keep B3_LARGE_FLOAT*B3_LARGE_FLOAT < FLT_MAX\n"
157         "#define B3_LARGE_FLOAT 1e18f\n"
158         "#define B3_INFINITY 1e18f\n"
159         "#define b3Assert(a)\n"
160         "#define b3ConstArray(a) __global const a*\n"
161         "#define b3AtomicInc atomic_inc\n"
162         "#define b3AtomicAdd atomic_add\n"
163         "#define b3Fabs fabs\n"
164         "#define b3Sqrt native_sqrt\n"
165         "#define b3Sin native_sin\n"
166         "#define b3Cos native_cos\n"
167         "#define B3_STATIC\n"
168         "#endif\n"
169         "#endif\n"
170         "#ifdef __cplusplus\n"
171         "#else\n"
172         "       typedef float4  b3Float4;\n"
173         "       #define b3Float4ConstArg const b3Float4\n"
174         "       #define b3MakeFloat4 (float4)\n"
175         "       float b3Dot3F4(b3Float4ConstArg v0,b3Float4ConstArg v1)\n"
176         "       {\n"
177         "               float4 a1 = b3MakeFloat4(v0.xyz,0.f);\n"
178         "               float4 b1 = b3MakeFloat4(v1.xyz,0.f);\n"
179         "               return dot(a1, b1);\n"
180         "       }\n"
181         "       b3Float4 b3Cross3(b3Float4ConstArg v0,b3Float4ConstArg v1)\n"
182         "       {\n"
183         "               float4 a1 = b3MakeFloat4(v0.xyz,0.f);\n"
184         "               float4 b1 = b3MakeFloat4(v1.xyz,0.f);\n"
185         "               return cross(a1, b1);\n"
186         "       }\n"
187         "       #define b3MinFloat4 min\n"
188         "       #define b3MaxFloat4 max\n"
189         "       #define b3Normalized(a) normalize(a)\n"
190         "#endif \n"
191         "               \n"
192         "inline bool b3IsAlmostZero(b3Float4ConstArg v)\n"
193         "{\n"
194         "       if(b3Fabs(v.x)>1e-6 || b3Fabs(v.y)>1e-6 || b3Fabs(v.z)>1e-6)    \n"
195         "               return false;\n"
196         "       return true;\n"
197         "}\n"
198         "inline int    b3MaxDot( b3Float4ConstArg vec, __global const b3Float4* vecArray, int vecLen, float* dotOut )\n"
199         "{\n"
200         "    float maxDot = -B3_INFINITY;\n"
201         "    int i = 0;\n"
202         "    int ptIndex = -1;\n"
203         "    for( i = 0; i < vecLen; i++ )\n"
204         "    {\n"
205         "        float dot = b3Dot3F4(vecArray[i],vec);\n"
206         "            \n"
207         "        if( dot > maxDot )\n"
208         "        {\n"
209         "            maxDot = dot;\n"
210         "            ptIndex = i;\n"
211         "        }\n"
212         "    }\n"
213         "       b3Assert(ptIndex>=0);\n"
214         "    if (ptIndex<0)\n"
215         "       {\n"
216         "               ptIndex = 0;\n"
217         "       }\n"
218         "    *dotOut = maxDot;\n"
219         "    return ptIndex;\n"
220         "}\n"
221         "#endif //B3_FLOAT4_H\n"
222         "#ifndef B3_MAT3x3_H\n"
223         "#define B3_MAT3x3_H\n"
224         "#ifndef B3_QUAT_H\n"
225         "#define B3_QUAT_H\n"
226         "#ifndef B3_PLATFORM_DEFINITIONS_H\n"
227         "#ifdef __cplusplus\n"
228         "#else\n"
229         "#endif\n"
230         "#endif\n"
231         "#ifndef B3_FLOAT4_H\n"
232         "#ifdef __cplusplus\n"
233         "#else\n"
234         "#endif \n"
235         "#endif //B3_FLOAT4_H\n"
236         "#ifdef __cplusplus\n"
237         "#else\n"
238         "       typedef float4  b3Quat;\n"
239         "       #define b3QuatConstArg const b3Quat\n"
240         "       \n"
241         "       \n"
242         "inline float4 b3FastNormalize4(float4 v)\n"
243         "{\n"
244         "       v = (float4)(v.xyz,0.f);\n"
245         "       return fast_normalize(v);\n"
246         "}\n"
247         "       \n"
248         "inline b3Quat b3QuatMul(b3Quat a, b3Quat b);\n"
249         "inline b3Quat b3QuatNormalized(b3QuatConstArg in);\n"
250         "inline b3Quat b3QuatRotate(b3QuatConstArg q, b3QuatConstArg vec);\n"
251         "inline b3Quat b3QuatInvert(b3QuatConstArg q);\n"
252         "inline b3Quat b3QuatInverse(b3QuatConstArg q);\n"
253         "inline b3Quat b3QuatMul(b3QuatConstArg a, b3QuatConstArg b)\n"
254         "{\n"
255         "       b3Quat ans;\n"
256         "       ans = b3Cross3( a, b );\n"
257         "       ans += a.w*b+b.w*a;\n"
258         "//     ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);\n"
259         "       ans.w = a.w*b.w - b3Dot3F4(a, b);\n"
260         "       return ans;\n"
261         "}\n"
262         "inline b3Quat b3QuatNormalized(b3QuatConstArg in)\n"
263         "{\n"
264         "       b3Quat q;\n"
265         "       q=in;\n"
266         "       //return b3FastNormalize4(in);\n"
267         "       float len = native_sqrt(dot(q, q));\n"
268         "       if(len > 0.f)\n"
269         "       {\n"
270         "               q *= 1.f / len;\n"
271         "       }\n"
272         "       else\n"
273         "       {\n"
274         "               q.x = q.y = q.z = 0.f;\n"
275         "               q.w = 1.f;\n"
276         "       }\n"
277         "       return q;\n"
278         "}\n"
279         "inline float4 b3QuatRotate(b3QuatConstArg q, b3QuatConstArg vec)\n"
280         "{\n"
281         "       b3Quat qInv = b3QuatInvert( q );\n"
282         "       float4 vcpy = vec;\n"
283         "       vcpy.w = 0.f;\n"
284         "       float4 out = b3QuatMul(b3QuatMul(q,vcpy),qInv);\n"
285         "       return out;\n"
286         "}\n"
287         "inline b3Quat b3QuatInverse(b3QuatConstArg q)\n"
288         "{\n"
289         "       return (b3Quat)(-q.xyz, q.w);\n"
290         "}\n"
291         "inline b3Quat b3QuatInvert(b3QuatConstArg q)\n"
292         "{\n"
293         "       return (b3Quat)(-q.xyz, q.w);\n"
294         "}\n"
295         "inline float4 b3QuatInvRotate(b3QuatConstArg q, b3QuatConstArg vec)\n"
296         "{\n"
297         "       return b3QuatRotate( b3QuatInvert( q ), vec );\n"
298         "}\n"
299         "inline b3Float4 b3TransformPoint(b3Float4ConstArg point, b3Float4ConstArg translation, b3QuatConstArg  orientation)\n"
300         "{\n"
301         "       return b3QuatRotate( orientation, point ) + (translation);\n"
302         "}\n"
303         "       \n"
304         "#endif \n"
305         "#endif //B3_QUAT_H\n"
306         "#ifdef __cplusplus\n"
307         "#else\n"
308         "typedef struct\n"
309         "{\n"
310         "       b3Float4 m_row[3];\n"
311         "}b3Mat3x3;\n"
312         "#define b3Mat3x3ConstArg const b3Mat3x3\n"
313         "#define b3GetRow(m,row) (m.m_row[row])\n"
314         "inline b3Mat3x3 b3QuatGetRotationMatrix(b3Quat quat)\n"
315         "{\n"
316         "       b3Float4 quat2 = (b3Float4)(quat.x*quat.x, quat.y*quat.y, quat.z*quat.z, 0.f);\n"
317         "       b3Mat3x3 out;\n"
318         "       out.m_row[0].x=1-2*quat2.y-2*quat2.z;\n"
319         "       out.m_row[0].y=2*quat.x*quat.y-2*quat.w*quat.z;\n"
320         "       out.m_row[0].z=2*quat.x*quat.z+2*quat.w*quat.y;\n"
321         "       out.m_row[0].w = 0.f;\n"
322         "       out.m_row[1].x=2*quat.x*quat.y+2*quat.w*quat.z;\n"
323         "       out.m_row[1].y=1-2*quat2.x-2*quat2.z;\n"
324         "       out.m_row[1].z=2*quat.y*quat.z-2*quat.w*quat.x;\n"
325         "       out.m_row[1].w = 0.f;\n"
326         "       out.m_row[2].x=2*quat.x*quat.z-2*quat.w*quat.y;\n"
327         "       out.m_row[2].y=2*quat.y*quat.z+2*quat.w*quat.x;\n"
328         "       out.m_row[2].z=1-2*quat2.x-2*quat2.y;\n"
329         "       out.m_row[2].w = 0.f;\n"
330         "       return out;\n"
331         "}\n"
332         "inline b3Mat3x3 b3AbsoluteMat3x3(b3Mat3x3ConstArg matIn)\n"
333         "{\n"
334         "       b3Mat3x3 out;\n"
335         "       out.m_row[0] = fabs(matIn.m_row[0]);\n"
336         "       out.m_row[1] = fabs(matIn.m_row[1]);\n"
337         "       out.m_row[2] = fabs(matIn.m_row[2]);\n"
338         "       return out;\n"
339         "}\n"
340         "__inline\n"
341         "b3Mat3x3 mtZero();\n"
342         "__inline\n"
343         "b3Mat3x3 mtIdentity();\n"
344         "__inline\n"
345         "b3Mat3x3 mtTranspose(b3Mat3x3 m);\n"
346         "__inline\n"
347         "b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b);\n"
348         "__inline\n"
349         "b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b);\n"
350         "__inline\n"
351         "b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b);\n"
352         "__inline\n"
353         "b3Mat3x3 mtZero()\n"
354         "{\n"
355         "       b3Mat3x3 m;\n"
356         "       m.m_row[0] = (b3Float4)(0.f);\n"
357         "       m.m_row[1] = (b3Float4)(0.f);\n"
358         "       m.m_row[2] = (b3Float4)(0.f);\n"
359         "       return m;\n"
360         "}\n"
361         "__inline\n"
362         "b3Mat3x3 mtIdentity()\n"
363         "{\n"
364         "       b3Mat3x3 m;\n"
365         "       m.m_row[0] = (b3Float4)(1,0,0,0);\n"
366         "       m.m_row[1] = (b3Float4)(0,1,0,0);\n"
367         "       m.m_row[2] = (b3Float4)(0,0,1,0);\n"
368         "       return m;\n"
369         "}\n"
370         "__inline\n"
371         "b3Mat3x3 mtTranspose(b3Mat3x3 m)\n"
372         "{\n"
373         "       b3Mat3x3 out;\n"
374         "       out.m_row[0] = (b3Float4)(m.m_row[0].x, m.m_row[1].x, m.m_row[2].x, 0.f);\n"
375         "       out.m_row[1] = (b3Float4)(m.m_row[0].y, m.m_row[1].y, m.m_row[2].y, 0.f);\n"
376         "       out.m_row[2] = (b3Float4)(m.m_row[0].z, m.m_row[1].z, m.m_row[2].z, 0.f);\n"
377         "       return out;\n"
378         "}\n"
379         "__inline\n"
380         "b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b)\n"
381         "{\n"
382         "       b3Mat3x3 transB;\n"
383         "       transB = mtTranspose( b );\n"
384         "       b3Mat3x3 ans;\n"
385         "       //      why this doesn't run when 0ing in the for{}\n"
386         "       a.m_row[0].w = 0.f;\n"
387         "       a.m_row[1].w = 0.f;\n"
388         "       a.m_row[2].w = 0.f;\n"
389         "       for(int i=0; i<3; i++)\n"
390         "       {\n"
391         "//     a.m_row[i].w = 0.f;\n"
392         "               ans.m_row[i].x = b3Dot3F4(a.m_row[i],transB.m_row[0]);\n"
393         "               ans.m_row[i].y = b3Dot3F4(a.m_row[i],transB.m_row[1]);\n"
394         "               ans.m_row[i].z = b3Dot3F4(a.m_row[i],transB.m_row[2]);\n"
395         "               ans.m_row[i].w = 0.f;\n"
396         "       }\n"
397         "       return ans;\n"
398         "}\n"
399         "__inline\n"
400         "b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b)\n"
401         "{\n"
402         "       b3Float4 ans;\n"
403         "       ans.x = b3Dot3F4( a.m_row[0], b );\n"
404         "       ans.y = b3Dot3F4( a.m_row[1], b );\n"
405         "       ans.z = b3Dot3F4( a.m_row[2], b );\n"
406         "       ans.w = 0.f;\n"
407         "       return ans;\n"
408         "}\n"
409         "__inline\n"
410         "b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b)\n"
411         "{\n"
412         "       b3Float4 colx = b3MakeFloat4(b.m_row[0].x, b.m_row[1].x, b.m_row[2].x, 0);\n"
413         "       b3Float4 coly = b3MakeFloat4(b.m_row[0].y, b.m_row[1].y, b.m_row[2].y, 0);\n"
414         "       b3Float4 colz = b3MakeFloat4(b.m_row[0].z, b.m_row[1].z, b.m_row[2].z, 0);\n"
415         "       b3Float4 ans;\n"
416         "       ans.x = b3Dot3F4( a, colx );\n"
417         "       ans.y = b3Dot3F4( a, coly );\n"
418         "       ans.z = b3Dot3F4( a, colz );\n"
419         "       return ans;\n"
420         "}\n"
421         "#endif\n"
422         "#endif //B3_MAT3x3_H\n"
423         "typedef struct b3Aabb b3Aabb_t;\n"
424         "struct b3Aabb\n"
425         "{\n"
426         "       union\n"
427         "       {\n"
428         "               float m_min[4];\n"
429         "               b3Float4 m_minVec;\n"
430         "               int m_minIndices[4];\n"
431         "       };\n"
432         "       union\n"
433         "       {\n"
434         "               float   m_max[4];\n"
435         "               b3Float4 m_maxVec;\n"
436         "               int m_signedMaxIndices[4];\n"
437         "       };\n"
438         "};\n"
439         "inline void b3TransformAabb2(b3Float4ConstArg localAabbMin,b3Float4ConstArg localAabbMax, float margin,\n"
440         "                                               b3Float4ConstArg pos,\n"
441         "                                               b3QuatConstArg orn,\n"
442         "                                               b3Float4* aabbMinOut,b3Float4* aabbMaxOut)\n"
443         "{\n"
444         "               b3Float4 localHalfExtents = 0.5f*(localAabbMax-localAabbMin);\n"
445         "               localHalfExtents+=b3MakeFloat4(margin,margin,margin,0.f);\n"
446         "               b3Float4 localCenter = 0.5f*(localAabbMax+localAabbMin);\n"
447         "               b3Mat3x3 m;\n"
448         "               m = b3QuatGetRotationMatrix(orn);\n"
449         "               b3Mat3x3 abs_b = b3AbsoluteMat3x3(m);\n"
450         "               b3Float4 center = b3TransformPoint(localCenter,pos,orn);\n"
451         "               \n"
452         "               b3Float4 extent = b3MakeFloat4(b3Dot3F4(localHalfExtents,b3GetRow(abs_b,0)),\n"
453         "                                                                                b3Dot3F4(localHalfExtents,b3GetRow(abs_b,1)),\n"
454         "                                                                                b3Dot3F4(localHalfExtents,b3GetRow(abs_b,2)),\n"
455         "                                                                                0.f);\n"
456         "               *aabbMinOut = center-extent;\n"
457         "               *aabbMaxOut = center+extent;\n"
458         "}\n"
459         "/// conservative test for overlap between two aabbs\n"
460         "inline bool b3TestAabbAgainstAabb(b3Float4ConstArg aabbMin1,b3Float4ConstArg aabbMax1,\n"
461         "                                                               b3Float4ConstArg aabbMin2, b3Float4ConstArg aabbMax2)\n"
462         "{\n"
463         "       bool overlap = true;\n"
464         "       overlap = (aabbMin1.x > aabbMax2.x || aabbMax1.x < aabbMin2.x) ? false : overlap;\n"
465         "       overlap = (aabbMin1.z > aabbMax2.z || aabbMax1.z < aabbMin2.z) ? false : overlap;\n"
466         "       overlap = (aabbMin1.y > aabbMax2.y || aabbMax1.y < aabbMin2.y) ? false : overlap;\n"
467         "       return overlap;\n"
468         "}\n"
469         "#endif //B3_AABB_H\n"
470         "/*\n"
471         "Bullet Continuous Collision Detection and Physics Library\n"
472         "Copyright (c) 2003-2013 Erwin Coumans  http://bulletphysics.org\n"
473         "This software is provided 'as-is', without any express or implied warranty.\n"
474         "In no event will the authors be held liable for any damages arising from the use of this software.\n"
475         "Permission is granted to anyone to use this software for any purpose,\n"
476         "including commercial applications, and to alter it and redistribute it freely,\n"
477         "subject to the following restrictions:\n"
478         "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"
479         "2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.\n"
480         "3. This notice may not be removed or altered from any source distribution.\n"
481         "*/\n"
482         "#ifndef B3_INT2_H\n"
483         "#define B3_INT2_H\n"
484         "#ifdef __cplusplus\n"
485         "#else\n"
486         "#define b3UnsignedInt2 uint2\n"
487         "#define b3Int2 int2\n"
488         "#define b3MakeInt2 (int2)\n"
489         "#endif //__cplusplus\n"
490         "#endif\n"
491         "typedef struct\n"
492         "{\n"
493         "       float4 m_plane;\n"
494         "       int m_indexOffset;\n"
495         "       int m_numIndices;\n"
496         "} btGpuFace;\n"
497         "#define make_float4 (float4)\n"
498         "__inline\n"
499         "float4 cross3(float4 a, float4 b)\n"
500         "{\n"
501         "       return cross(a,b);\n"
502         "       \n"
503         "//     float4 a1 = make_float4(a.xyz,0.f);\n"
504         "//     float4 b1 = make_float4(b.xyz,0.f);\n"
505         "//     return cross(a1,b1);\n"
506         "//float4 c = make_float4(a.y*b.z - a.z*b.y,a.z*b.x - a.x*b.z,a.x*b.y - a.y*b.x,0.f);\n"
507         "       \n"
508         "       //      float4 c = make_float4(a.y*b.z - a.z*b.y,1.f,a.x*b.y - a.y*b.x,0.f);\n"
509         "       \n"
510         "       //return c;\n"
511         "}\n"
512         "__inline\n"
513         "float dot3F4(float4 a, float4 b)\n"
514         "{\n"
515         "       float4 a1 = make_float4(a.xyz,0.f);\n"
516         "       float4 b1 = make_float4(b.xyz,0.f);\n"
517         "       return dot(a1, b1);\n"
518         "}\n"
519         "__inline\n"
520         "float4 fastNormalize4(float4 v)\n"
521         "{\n"
522         "       v = make_float4(v.xyz,0.f);\n"
523         "       return fast_normalize(v);\n"
524         "}\n"
525         "///////////////////////////////////////\n"
526         "//     Quaternion\n"
527         "///////////////////////////////////////\n"
528         "typedef float4 Quaternion;\n"
529         "__inline\n"
530         "Quaternion qtMul(Quaternion a, Quaternion b);\n"
531         "__inline\n"
532         "Quaternion qtNormalize(Quaternion in);\n"
533         "__inline\n"
534         "float4 qtRotate(Quaternion q, float4 vec);\n"
535         "__inline\n"
536         "Quaternion qtInvert(Quaternion q);\n"
537         "__inline\n"
538         "Quaternion qtMul(Quaternion a, Quaternion b)\n"
539         "{\n"
540         "       Quaternion ans;\n"
541         "       ans = cross3( a, b );\n"
542         "       ans += a.w*b+b.w*a;\n"
543         "//     ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);\n"
544         "       ans.w = a.w*b.w - dot3F4(a, b);\n"
545         "       return ans;\n"
546         "}\n"
547         "__inline\n"
548         "Quaternion qtNormalize(Quaternion in)\n"
549         "{\n"
550         "       return fastNormalize4(in);\n"
551         "//     in /= length( in );\n"
552         "//     return in;\n"
553         "}\n"
554         "__inline\n"
555         "float4 qtRotate(Quaternion q, float4 vec)\n"
556         "{\n"
557         "       Quaternion qInv = qtInvert( q );\n"
558         "       float4 vcpy = vec;\n"
559         "       vcpy.w = 0.f;\n"
560         "       float4 out = qtMul(qtMul(q,vcpy),qInv);\n"
561         "       return out;\n"
562         "}\n"
563         "__inline\n"
564         "Quaternion qtInvert(Quaternion q)\n"
565         "{\n"
566         "       return (Quaternion)(-q.xyz, q.w);\n"
567         "}\n"
568         "__inline\n"
569         "float4 qtInvRotate(const Quaternion q, float4 vec)\n"
570         "{\n"
571         "       return qtRotate( qtInvert( q ), vec );\n"
572         "}\n"
573         "__inline\n"
574         "float4 transform(const float4* p, const float4* translation, const Quaternion* orientation)\n"
575         "{\n"
576         "       return qtRotate( *orientation, *p ) + (*translation);\n"
577         "}\n"
578         "__inline\n"
579         "float4 normalize3(const float4 a)\n"
580         "{\n"
581         "       float4 n = make_float4(a.x, a.y, a.z, 0.f);\n"
582         "       return fastNormalize4( n );\n"
583         "}\n"
584         "inline void projectLocal(const ConvexPolyhedronCL* hull,  const float4 pos, const float4 orn, \n"
585         "const float4* dir, const float4* vertices, float* min, float* max)\n"
586         "{\n"
587         "       min[0] = FLT_MAX;\n"
588         "       max[0] = -FLT_MAX;\n"
589         "       int numVerts = hull->m_numVertices;\n"
590         "       const float4 localDir = qtInvRotate(orn,*dir);\n"
591         "       float offset = dot(pos,*dir);\n"
592         "       for(int i=0;i<numVerts;i++)\n"
593         "       {\n"
594         "               float dp = dot(vertices[hull->m_vertexOffset+i],localDir);\n"
595         "               if(dp < min[0]) \n"
596         "                       min[0] = dp;\n"
597         "               if(dp > max[0]) \n"
598         "                       max[0] = dp;\n"
599         "       }\n"
600         "       if(min[0]>max[0])\n"
601         "       {\n"
602         "               float tmp = min[0];\n"
603         "               min[0] = max[0];\n"
604         "               max[0] = tmp;\n"
605         "       }\n"
606         "       min[0] += offset;\n"
607         "       max[0] += offset;\n"
608         "}\n"
609         "inline void project(__global const ConvexPolyhedronCL* hull,  const float4 pos, const float4 orn, \n"
610         "const float4* dir, __global const float4* vertices, float* min, float* max)\n"
611         "{\n"
612         "       min[0] = FLT_MAX;\n"
613         "       max[0] = -FLT_MAX;\n"
614         "       int numVerts = hull->m_numVertices;\n"
615         "       const float4 localDir = qtInvRotate(orn,*dir);\n"
616         "       float offset = dot(pos,*dir);\n"
617         "       for(int i=0;i<numVerts;i++)\n"
618         "       {\n"
619         "               float dp = dot(vertices[hull->m_vertexOffset+i],localDir);\n"
620         "               if(dp < min[0]) \n"
621         "                       min[0] = dp;\n"
622         "               if(dp > max[0]) \n"
623         "                       max[0] = dp;\n"
624         "       }\n"
625         "       if(min[0]>max[0])\n"
626         "       {\n"
627         "               float tmp = min[0];\n"
628         "               min[0] = max[0];\n"
629         "               max[0] = tmp;\n"
630         "       }\n"
631         "       min[0] += offset;\n"
632         "       max[0] += offset;\n"
633         "}\n"
634         "inline bool TestSepAxisLocalA(const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB, \n"
635         "       const float4 posA,const float4 ornA,\n"
636         "       const float4 posB,const float4 ornB,\n"
637         "       float4* sep_axis, const float4* verticesA, __global const float4* verticesB,float* depth)\n"
638         "{\n"
639         "       float Min0,Max0;\n"
640         "       float Min1,Max1;\n"
641         "       projectLocal(hullA,posA,ornA,sep_axis,verticesA, &Min0, &Max0);\n"
642         "       project(hullB,posB,ornB, sep_axis,verticesB, &Min1, &Max1);\n"
643         "       if(Max0<Min1 || Max1<Min0)\n"
644         "               return false;\n"
645         "       float d0 = Max0 - Min1;\n"
646         "       float d1 = Max1 - Min0;\n"
647         "       *depth = d0<d1 ? d0:d1;\n"
648         "       return true;\n"
649         "}\n"
650         "inline bool IsAlmostZero(const float4 v)\n"
651         "{\n"
652         "       if(fabs(v.x)>1e-6f || fabs(v.y)>1e-6f || fabs(v.z)>1e-6f)\n"
653         "               return false;\n"
654         "       return true;\n"
655         "}\n"
656         "bool findSeparatingAxisLocalA( const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB, \n"
657         "       const float4 posA1,\n"
658         "       const float4 ornA,\n"
659         "       const float4 posB1,\n"
660         "       const float4 ornB,\n"
661         "       const float4 DeltaC2,\n"
662         "       \n"
663         "       const float4* verticesA, \n"
664         "       const float4* uniqueEdgesA, \n"
665         "       const btGpuFace* facesA,\n"
666         "       const int*  indicesA,\n"
667         "       __global const float4* verticesB, \n"
668         "       __global const float4* uniqueEdgesB, \n"
669         "       __global const btGpuFace* facesB,\n"
670         "       __global const int*  indicesB,\n"
671         "       float4* sep,\n"
672         "       float* dmin)\n"
673         "{\n"
674         "       \n"
675         "       float4 posA = posA1;\n"
676         "       posA.w = 0.f;\n"
677         "       float4 posB = posB1;\n"
678         "       posB.w = 0.f;\n"
679         "       int curPlaneTests=0;\n"
680         "       {\n"
681         "               int numFacesA = hullA->m_numFaces;\n"
682         "               // Test normals from hullA\n"
683         "               for(int i=0;i<numFacesA;i++)\n"
684         "               {\n"
685         "                       const float4 normal = facesA[hullA->m_faceOffset+i].m_plane;\n"
686         "                       float4 faceANormalWS = qtRotate(ornA,normal);\n"
687         "                       if (dot3F4(DeltaC2,faceANormalWS)<0)\n"
688         "                               faceANormalWS*=-1.f;\n"
689         "                       curPlaneTests++;\n"
690         "                       float d;\n"
691         "                       if(!TestSepAxisLocalA( hullA, hullB, posA,ornA,posB,ornB,&faceANormalWS, verticesA, verticesB,&d))\n"
692         "                               return false;\n"
693         "                       if(d<*dmin)\n"
694         "                       {\n"
695         "                               *dmin = d;\n"
696         "                               *sep = faceANormalWS;\n"
697         "                       }\n"
698         "               }\n"
699         "       }\n"
700         "       if((dot3F4(-DeltaC2,*sep))>0.0f)\n"
701         "       {\n"
702         "               *sep = -(*sep);\n"
703         "       }\n"
704         "       return true;\n"
705         "}\n"
706         "bool findSeparatingAxisLocalB( __global const ConvexPolyhedronCL* hullA,  const ConvexPolyhedronCL* hullB, \n"
707         "       const float4 posA1,\n"
708         "       const float4 ornA,\n"
709         "       const float4 posB1,\n"
710         "       const float4 ornB,\n"
711         "       const float4 DeltaC2,\n"
712         "       __global const float4* verticesA, \n"
713         "       __global const float4* uniqueEdgesA, \n"
714         "       __global const btGpuFace* facesA,\n"
715         "       __global const int*  indicesA,\n"
716         "       const float4* verticesB,\n"
717         "       const float4* uniqueEdgesB, \n"
718         "       const btGpuFace* facesB,\n"
719         "       const int*  indicesB,\n"
720         "       float4* sep,\n"
721         "       float* dmin)\n"
722         "{\n"
723         "       float4 posA = posA1;\n"
724         "       posA.w = 0.f;\n"
725         "       float4 posB = posB1;\n"
726         "       posB.w = 0.f;\n"
727         "       int curPlaneTests=0;\n"
728         "       {\n"
729         "               int numFacesA = hullA->m_numFaces;\n"
730         "               // Test normals from hullA\n"
731         "               for(int i=0;i<numFacesA;i++)\n"
732         "               {\n"
733         "                       const float4 normal = facesA[hullA->m_faceOffset+i].m_plane;\n"
734         "                       float4 faceANormalWS = qtRotate(ornA,normal);\n"
735         "                       if (dot3F4(DeltaC2,faceANormalWS)<0)\n"
736         "                               faceANormalWS *= -1.f;\n"
737         "                       curPlaneTests++;\n"
738         "                       float d;\n"
739         "                       if(!TestSepAxisLocalA( hullB, hullA, posB,ornB,posA,ornA, &faceANormalWS, verticesB,verticesA, &d))\n"
740         "                               return false;\n"
741         "                       if(d<*dmin)\n"
742         "                       {\n"
743         "                               *dmin = d;\n"
744         "                               *sep = faceANormalWS;\n"
745         "                       }\n"
746         "               }\n"
747         "       }\n"
748         "       if((dot3F4(-DeltaC2,*sep))>0.0f)\n"
749         "       {\n"
750         "               *sep = -(*sep);\n"
751         "       }\n"
752         "       return true;\n"
753         "}\n"
754         "bool findSeparatingAxisEdgeEdgeLocalA( const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB, \n"
755         "       const float4 posA1,\n"
756         "       const float4 ornA,\n"
757         "       const float4 posB1,\n"
758         "       const float4 ornB,\n"
759         "       const float4 DeltaC2,\n"
760         "       const float4* verticesA, \n"
761         "       const float4* uniqueEdgesA, \n"
762         "       const btGpuFace* facesA,\n"
763         "       const int*  indicesA,\n"
764         "       __global const float4* verticesB, \n"
765         "       __global const float4* uniqueEdgesB, \n"
766         "       __global const btGpuFace* facesB,\n"
767         "       __global const int*  indicesB,\n"
768         "               float4* sep,\n"
769         "       float* dmin)\n"
770         "{\n"
771         "       float4 posA = posA1;\n"
772         "       posA.w = 0.f;\n"
773         "       float4 posB = posB1;\n"
774         "       posB.w = 0.f;\n"
775         "       int curPlaneTests=0;\n"
776         "       int curEdgeEdge = 0;\n"
777         "       // Test edges\n"
778         "       for(int e0=0;e0<hullA->m_numUniqueEdges;e0++)\n"
779         "       {\n"
780         "               const float4 edge0 = uniqueEdgesA[hullA->m_uniqueEdgesOffset+e0];\n"
781         "               float4 edge0World = qtRotate(ornA,edge0);\n"
782         "               for(int e1=0;e1<hullB->m_numUniqueEdges;e1++)\n"
783         "               {\n"
784         "                       const float4 edge1 = uniqueEdgesB[hullB->m_uniqueEdgesOffset+e1];\n"
785         "                       float4 edge1World = qtRotate(ornB,edge1);\n"
786         "                       float4 crossje = cross3(edge0World,edge1World);\n"
787         "                       curEdgeEdge++;\n"
788         "                       if(!IsAlmostZero(crossje))\n"
789         "                       {\n"
790         "                               crossje = normalize3(crossje);\n"
791         "                               if (dot3F4(DeltaC2,crossje)<0)\n"
792         "                                       crossje *= -1.f;\n"
793         "                               float dist;\n"
794         "                               bool result = true;\n"
795         "                               {\n"
796         "                                       float Min0,Max0;\n"
797         "                                       float Min1,Max1;\n"
798         "                                       projectLocal(hullA,posA,ornA,&crossje,verticesA, &Min0, &Max0);\n"
799         "                                       project(hullB,posB,ornB,&crossje,verticesB, &Min1, &Max1);\n"
800         "                               \n"
801         "                                       if(Max0<Min1 || Max1<Min0)\n"
802         "                                               result = false;\n"
803         "                               \n"
804         "                                       float d0 = Max0 - Min1;\n"
805         "                                       float d1 = Max1 - Min0;\n"
806         "                                       dist = d0<d1 ? d0:d1;\n"
807         "                                       result = true;\n"
808         "                               }\n"
809         "                               \n"
810         "                               if(dist<*dmin)\n"
811         "                               {\n"
812         "                                       *dmin = dist;\n"
813         "                                       *sep = crossje;\n"
814         "                               }\n"
815         "                       }\n"
816         "               }\n"
817         "       }\n"
818         "       \n"
819         "       if((dot3F4(-DeltaC2,*sep))>0.0f)\n"
820         "       {\n"
821         "               *sep = -(*sep);\n"
822         "       }\n"
823         "       return true;\n"
824         "}\n"
825         "inline bool TestSepAxis(__global const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB, \n"
826         "       const float4 posA,const float4 ornA,\n"
827         "       const float4 posB,const float4 ornB,\n"
828         "       float4* sep_axis, __global const float4* vertices,float* depth)\n"
829         "{\n"
830         "       float Min0,Max0;\n"
831         "       float Min1,Max1;\n"
832         "       project(hullA,posA,ornA,sep_axis,vertices, &Min0, &Max0);\n"
833         "       project(hullB,posB,ornB, sep_axis,vertices, &Min1, &Max1);\n"
834         "       if(Max0<Min1 || Max1<Min0)\n"
835         "               return false;\n"
836         "       float d0 = Max0 - Min1;\n"
837         "       float d1 = Max1 - Min0;\n"
838         "       *depth = d0<d1 ? d0:d1;\n"
839         "       return true;\n"
840         "}\n"
841         "bool findSeparatingAxis(       __global const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB, \n"
842         "       const float4 posA1,\n"
843         "       const float4 ornA,\n"
844         "       const float4 posB1,\n"
845         "       const float4 ornB,\n"
846         "       const float4 DeltaC2,\n"
847         "       __global const float4* vertices, \n"
848         "       __global const float4* uniqueEdges, \n"
849         "       __global const btGpuFace* faces,\n"
850         "       __global const int*  indices,\n"
851         "       float4* sep,\n"
852         "       float* dmin)\n"
853         "{\n"
854         "       \n"
855         "       float4 posA = posA1;\n"
856         "       posA.w = 0.f;\n"
857         "       float4 posB = posB1;\n"
858         "       posB.w = 0.f;\n"
859         "       \n"
860         "       int curPlaneTests=0;\n"
861         "       {\n"
862         "               int numFacesA = hullA->m_numFaces;\n"
863         "               // Test normals from hullA\n"
864         "               for(int i=0;i<numFacesA;i++)\n"
865         "               {\n"
866         "                       const float4 normal = faces[hullA->m_faceOffset+i].m_plane;\n"
867         "                       float4 faceANormalWS = qtRotate(ornA,normal);\n"
868         "       \n"
869         "                       if (dot3F4(DeltaC2,faceANormalWS)<0)\n"
870         "                               faceANormalWS*=-1.f;\n"
871         "                               \n"
872         "                       curPlaneTests++;\n"
873         "       \n"
874         "                       float d;\n"
875         "                       if(!TestSepAxis( hullA, hullB, posA,ornA,posB,ornB,&faceANormalWS, vertices,&d))\n"
876         "                               return false;\n"
877         "       \n"
878         "                       if(d<*dmin)\n"
879         "                       {\n"
880         "                               *dmin = d;\n"
881         "                               *sep = faceANormalWS;\n"
882         "                       }\n"
883         "               }\n"
884         "       }\n"
885         "               if((dot3F4(-DeltaC2,*sep))>0.0f)\n"
886         "               {\n"
887         "                       *sep = -(*sep);\n"
888         "               }\n"
889         "       \n"
890         "       return true;\n"
891         "}\n"
892         "bool findSeparatingAxisUnitSphere(     __global const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB, \n"
893         "       const float4 posA1,\n"
894         "       const float4 ornA,\n"
895         "       const float4 posB1,\n"
896         "       const float4 ornB,\n"
897         "       const float4 DeltaC2,\n"
898         "       __global const float4* vertices,\n"
899         "       __global const float4* unitSphereDirections,\n"
900         "       int numUnitSphereDirections,\n"
901         "       float4* sep,\n"
902         "       float* dmin)\n"
903         "{\n"
904         "       \n"
905         "       float4 posA = posA1;\n"
906         "       posA.w = 0.f;\n"
907         "       float4 posB = posB1;\n"
908         "       posB.w = 0.f;\n"
909         "       int curPlaneTests=0;\n"
910         "       int curEdgeEdge = 0;\n"
911         "       // Test unit sphere directions\n"
912         "       for (int i=0;i<numUnitSphereDirections;i++)\n"
913         "       {\n"
914         "               float4 crossje;\n"
915         "               crossje = unitSphereDirections[i];      \n"
916         "               if (dot3F4(DeltaC2,crossje)>0)\n"
917         "                       crossje *= -1.f;\n"
918         "               {\n"
919         "                       float dist;\n"
920         "                       bool result = true;\n"
921         "                       float Min0,Max0;\n"
922         "                       float Min1,Max1;\n"
923         "                       project(hullA,posA,ornA,&crossje,vertices, &Min0, &Max0);\n"
924         "                       project(hullB,posB,ornB,&crossje,vertices, &Min1, &Max1);\n"
925         "               \n"
926         "                       if(Max0<Min1 || Max1<Min0)\n"
927         "                               return false;\n"
928         "               \n"
929         "                       float d0 = Max0 - Min1;\n"
930         "                       float d1 = Max1 - Min0;\n"
931         "                       dist = d0<d1 ? d0:d1;\n"
932         "                       result = true;\n"
933         "       \n"
934         "                       if(dist<*dmin)\n"
935         "                       {\n"
936         "                               *dmin = dist;\n"
937         "                               *sep = crossje;\n"
938         "                       }\n"
939         "               }\n"
940         "       }\n"
941         "       \n"
942         "       if((dot3F4(-DeltaC2,*sep))>0.0f)\n"
943         "       {\n"
944         "               *sep = -(*sep);\n"
945         "       }\n"
946         "       return true;\n"
947         "}\n"
948         "bool findSeparatingAxisEdgeEdge(       __global const ConvexPolyhedronCL* hullA, __global const ConvexPolyhedronCL* hullB, \n"
949         "       const float4 posA1,\n"
950         "       const float4 ornA,\n"
951         "       const float4 posB1,\n"
952         "       const float4 ornB,\n"
953         "       const float4 DeltaC2,\n"
954         "       __global const float4* vertices, \n"
955         "       __global const float4* uniqueEdges, \n"
956         "       __global const btGpuFace* faces,\n"
957         "       __global const int*  indices,\n"
958         "       float4* sep,\n"
959         "       float* dmin)\n"
960         "{\n"
961         "       \n"
962         "       float4 posA = posA1;\n"
963         "       posA.w = 0.f;\n"
964         "       float4 posB = posB1;\n"
965         "       posB.w = 0.f;\n"
966         "       int curPlaneTests=0;\n"
967         "       int curEdgeEdge = 0;\n"
968         "       // Test edges\n"
969         "       for(int e0=0;e0<hullA->m_numUniqueEdges;e0++)\n"
970         "       {\n"
971         "               const float4 edge0 = uniqueEdges[hullA->m_uniqueEdgesOffset+e0];\n"
972         "               float4 edge0World = qtRotate(ornA,edge0);\n"
973         "               for(int e1=0;e1<hullB->m_numUniqueEdges;e1++)\n"
974         "               {\n"
975         "                       const float4 edge1 = uniqueEdges[hullB->m_uniqueEdgesOffset+e1];\n"
976         "                       float4 edge1World = qtRotate(ornB,edge1);\n"
977         "                       float4 crossje = cross3(edge0World,edge1World);\n"
978         "                       curEdgeEdge++;\n"
979         "                       if(!IsAlmostZero(crossje))\n"
980         "                       {\n"
981         "                               crossje = normalize3(crossje);\n"
982         "                               if (dot3F4(DeltaC2,crossje)<0)\n"
983         "                                       crossje*=-1.f;\n"
984         "                                       \n"
985         "                               float dist;\n"
986         "                               bool result = true;\n"
987         "                               {\n"
988         "                                       float Min0,Max0;\n"
989         "                                       float Min1,Max1;\n"
990         "                                       project(hullA,posA,ornA,&crossje,vertices, &Min0, &Max0);\n"
991         "                                       project(hullB,posB,ornB,&crossje,vertices, &Min1, &Max1);\n"
992         "                               \n"
993         "                                       if(Max0<Min1 || Max1<Min0)\n"
994         "                                               return false;\n"
995         "                               \n"
996         "                                       float d0 = Max0 - Min1;\n"
997         "                                       float d1 = Max1 - Min0;\n"
998         "                                       dist = d0<d1 ? d0:d1;\n"
999         "                                       result = true;\n"
1000         "                               }\n"
1001         "                               \n"
1002         "                               if(dist<*dmin)\n"
1003         "                               {\n"
1004         "                                       *dmin = dist;\n"
1005         "                                       *sep = crossje;\n"
1006         "                               }\n"
1007         "                       }\n"
1008         "               }\n"
1009         "       }\n"
1010         "       \n"
1011         "       if((dot3F4(-DeltaC2,*sep))>0.0f)\n"
1012         "       {\n"
1013         "               *sep = -(*sep);\n"
1014         "       }\n"
1015         "       return true;\n"
1016         "}\n"
1017         "// work-in-progress\n"
1018         "__kernel void   processCompoundPairsKernel( __global const int4* gpuCompoundPairs,\n"
1019         "                                                                                                                                                                       __global const BodyData* rigidBodies, \n"
1020         "                                                                                                                                                                       __global const btCollidableGpu* collidables,\n"
1021         "                                                                                                                                                                       __global const ConvexPolyhedronCL* convexShapes, \n"
1022         "                                                                                                                                                                       __global const float4* vertices,\n"
1023         "                                                                                                                                                                       __global const float4* uniqueEdges,\n"
1024         "                                                                                                                                                                       __global const btGpuFace* faces,\n"
1025         "                                                                                                                                                                       __global const int* indices,\n"
1026         "                                                                                                                                                                       __global btAabbCL* aabbs,\n"
1027         "                                                                                                                                                                       __global const btGpuChildShape* gpuChildShapes,\n"
1028         "                                                                                                                                                                       __global volatile float4* gpuCompoundSepNormalsOut,\n"
1029         "                                                                                                                                                                       __global volatile int* gpuHasCompoundSepNormalsOut,\n"
1030         "                                                                                                                                                                       int numCompoundPairs\n"
1031         "                                                                                                                                                                       )\n"
1032         "{\n"
1033         "       int i = get_global_id(0);\n"
1034         "       if (i<numCompoundPairs)\n"
1035         "       {\n"
1036         "               int bodyIndexA = gpuCompoundPairs[i].x;\n"
1037         "               int bodyIndexB = gpuCompoundPairs[i].y;\n"
1038         "               int childShapeIndexA = gpuCompoundPairs[i].z;\n"
1039         "               int childShapeIndexB = gpuCompoundPairs[i].w;\n"
1040         "               \n"
1041         "               int collidableIndexA = -1;\n"
1042         "               int collidableIndexB = -1;\n"
1043         "               \n"
1044         "               float4 ornA = rigidBodies[bodyIndexA].m_quat;\n"
1045         "               float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
1046         "               \n"
1047         "               float4 ornB = rigidBodies[bodyIndexB].m_quat;\n"
1048         "               float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
1049         "                                                       \n"
1050         "               if (childShapeIndexA >= 0)\n"
1051         "               {\n"
1052         "                       collidableIndexA = gpuChildShapes[childShapeIndexA].m_shapeIndex;\n"
1053         "                       float4 childPosA = gpuChildShapes[childShapeIndexA].m_childPosition;\n"
1054         "                       float4 childOrnA = gpuChildShapes[childShapeIndexA].m_childOrientation;\n"
1055         "                       float4 newPosA = qtRotate(ornA,childPosA)+posA;\n"
1056         "                       float4 newOrnA = qtMul(ornA,childOrnA);\n"
1057         "                       posA = newPosA;\n"
1058         "                       ornA = newOrnA;\n"
1059         "               } else\n"
1060         "               {\n"
1061         "                       collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
1062         "               }\n"
1063         "               \n"
1064         "               if (childShapeIndexB>=0)\n"
1065         "               {\n"
1066         "                       collidableIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;\n"
1067         "                       float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;\n"
1068         "                       float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;\n"
1069         "                       float4 newPosB = transform(&childPosB,&posB,&ornB);\n"
1070         "                       float4 newOrnB = qtMul(ornB,childOrnB);\n"
1071         "                       posB = newPosB;\n"
1072         "                       ornB = newOrnB;\n"
1073         "               } else\n"
1074         "               {\n"
1075         "                       collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;     \n"
1076         "               }\n"
1077         "       \n"
1078         "               gpuHasCompoundSepNormalsOut[i] = 0;\n"
1079         "       \n"
1080         "               int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
1081         "               int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
1082         "       \n"
1083         "               int shapeTypeA = collidables[collidableIndexA].m_shapeType;\n"
1084         "               int shapeTypeB = collidables[collidableIndexB].m_shapeType;\n"
1085         "       \n"
1086         "               if ((shapeTypeA != SHAPE_CONVEX_HULL) || (shapeTypeB != SHAPE_CONVEX_HULL))\n"
1087         "               {\n"
1088         "                       return;\n"
1089         "               }\n"
1090         "               int hasSeparatingAxis = 5;\n"
1091         "                                                       \n"
1092         "               int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
1093         "               float dmin = FLT_MAX;\n"
1094         "               posA.w = 0.f;\n"
1095         "               posB.w = 0.f;\n"
1096         "               float4 c0local = convexShapes[shapeIndexA].m_localCenter;\n"
1097         "               float4 c0 = transform(&c0local, &posA, &ornA);\n"
1098         "               float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
1099         "               float4 c1 = transform(&c1local,&posB,&ornB);\n"
1100         "               const float4 DeltaC2 = c0 - c1;\n"
1101         "               float4 sepNormal = make_float4(1,0,0,0);\n"
1102         "               bool sepA = findSeparatingAxis( &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,posB,ornB,DeltaC2,vertices,uniqueEdges,faces,indices,&sepNormal,&dmin);\n"
1103         "               hasSeparatingAxis = 4;\n"
1104         "               if (!sepA)\n"
1105         "               {\n"
1106         "                       hasSeparatingAxis = 0;\n"
1107         "               } else\n"
1108         "               {\n"
1109         "                       bool sepB = findSeparatingAxis( &convexShapes[shapeIndexB],&convexShapes[shapeIndexA],posB,ornB,posA,ornA,DeltaC2,vertices,uniqueEdges,faces,indices,&sepNormal,&dmin);\n"
1110         "                       if (!sepB)\n"
1111         "                       {\n"
1112         "                               hasSeparatingAxis = 0;\n"
1113         "                       } else//(!sepB)\n"
1114         "                       {\n"
1115         "                               bool sepEE = findSeparatingAxisEdgeEdge(        &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,posB,ornB,DeltaC2,vertices,uniqueEdges,faces,indices,&sepNormal,&dmin);\n"
1116         "                               if (sepEE)\n"
1117         "                               {\n"
1118         "                                               gpuCompoundSepNormalsOut[i] = sepNormal;//fastNormalize4(sepNormal);\n"
1119         "                                               gpuHasCompoundSepNormalsOut[i] = 1;\n"
1120         "                               }//sepEE\n"
1121         "                       }//(!sepB)\n"
1122         "               }//(!sepA)\n"
1123         "               \n"
1124         "               \n"
1125         "       }\n"
1126         "               \n"
1127         "}\n"
1128         "inline b3Float4 MyUnQuantize(const unsigned short* vecIn, b3Float4 quantization, b3Float4 bvhAabbMin)\n"
1129         "{\n"
1130         "               b3Float4 vecOut;\n"
1131         "               vecOut = b3MakeFloat4(\n"
1132         "                       (float)(vecIn[0]) / (quantization.x),\n"
1133         "                       (float)(vecIn[1]) / (quantization.y),\n"
1134         "                       (float)(vecIn[2]) / (quantization.z),\n"
1135         "                       0.f);\n"
1136         "               vecOut += bvhAabbMin;\n"
1137         "               return vecOut;\n"
1138         "}\n"
1139         "inline b3Float4 MyUnQuantizeGlobal(__global const unsigned short* vecIn, b3Float4 quantization, b3Float4 bvhAabbMin)\n"
1140         "{\n"
1141         "               b3Float4 vecOut;\n"
1142         "               vecOut = b3MakeFloat4(\n"
1143         "                       (float)(vecIn[0]) / (quantization.x),\n"
1144         "                       (float)(vecIn[1]) / (quantization.y),\n"
1145         "                       (float)(vecIn[2]) / (quantization.z),\n"
1146         "                       0.f);\n"
1147         "               vecOut += bvhAabbMin;\n"
1148         "               return vecOut;\n"
1149         "}\n"
1150         "// work-in-progress\n"
1151         "__kernel void   findCompoundPairsKernel( __global const int4* pairs, \n"
1152         "       __global const BodyData* rigidBodies, \n"
1153         "       __global const btCollidableGpu* collidables,\n"
1154         "       __global const ConvexPolyhedronCL* convexShapes, \n"
1155         "       __global const float4* vertices,\n"
1156         "       __global const float4* uniqueEdges,\n"
1157         "       __global const btGpuFace* faces,\n"
1158         "       __global const int* indices,\n"
1159         "       __global b3Aabb_t* aabbLocalSpace,\n"
1160         "       __global const btGpuChildShape* gpuChildShapes,\n"
1161         "       __global volatile int4* gpuCompoundPairsOut,\n"
1162         "       __global volatile int* numCompoundPairsOut,\n"
1163         "       __global const b3BvhSubtreeInfo* subtrees,\n"
1164         "       __global const b3QuantizedBvhNode* quantizedNodes,\n"
1165         "       __global const b3BvhInfo* bvhInfos,\n"
1166         "       int numPairs,\n"
1167         "       int maxNumCompoundPairsCapacity\n"
1168         "       )\n"
1169         "{\n"
1170         "       int i = get_global_id(0);\n"
1171         "       if (i<numPairs)\n"
1172         "       {\n"
1173         "               int bodyIndexA = pairs[i].x;\n"
1174         "               int bodyIndexB = pairs[i].y;\n"
1175         "               int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
1176         "               int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n"
1177         "               int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
1178         "               int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
1179         "               //once the broadphase avoids static-static pairs, we can remove this test\n"
1180         "               if ((rigidBodies[bodyIndexA].m_invMass==0) &&(rigidBodies[bodyIndexB].m_invMass==0))\n"
1181         "               {\n"
1182         "                       return;\n"
1183         "               }\n"
1184         "               if ((collidables[collidableIndexA].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS) &&(collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS))\n"
1185         "               {\n"
1186         "                       int bvhA = collidables[collidableIndexA].m_compoundBvhIndex;\n"
1187         "                       int bvhB = collidables[collidableIndexB].m_compoundBvhIndex;\n"
1188         "                       int numSubTreesA = bvhInfos[bvhA].m_numSubTrees;\n"
1189         "                       int subTreesOffsetA = bvhInfos[bvhA].m_subTreeOffset;\n"
1190         "                       int subTreesOffsetB = bvhInfos[bvhB].m_subTreeOffset;\n"
1191         "                       int numSubTreesB = bvhInfos[bvhB].m_numSubTrees;\n"
1192         "                       \n"
1193         "                       float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
1194         "                       b3Quat ornA = rigidBodies[bodyIndexA].m_quat;\n"
1195         "                       b3Quat ornB = rigidBodies[bodyIndexB].m_quat;\n"
1196         "                       float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
1197         "                       \n"
1198         "                       for (int p=0;p<numSubTreesA;p++)\n"
1199         "                       {\n"
1200         "                               b3BvhSubtreeInfo subtreeA = subtrees[subTreesOffsetA+p];\n"
1201         "                               //bvhInfos[bvhA].m_quantization\n"
1202         "                               b3Float4 treeAminLocal = MyUnQuantize(subtreeA.m_quantizedAabbMin,bvhInfos[bvhA].m_quantization,bvhInfos[bvhA].m_aabbMin);\n"
1203         "                               b3Float4 treeAmaxLocal = MyUnQuantize(subtreeA.m_quantizedAabbMax,bvhInfos[bvhA].m_quantization,bvhInfos[bvhA].m_aabbMin);\n"
1204         "                               b3Float4 aabbAMinOut,aabbAMaxOut;\n"
1205         "                               float margin=0.f;\n"
1206         "                               b3TransformAabb2(treeAminLocal,treeAmaxLocal, margin,posA,ornA,&aabbAMinOut,&aabbAMaxOut);\n"
1207         "                               \n"
1208         "                               for (int q=0;q<numSubTreesB;q++)\n"
1209         "                               {\n"
1210         "                                       b3BvhSubtreeInfo subtreeB = subtrees[subTreesOffsetB+q];\n"
1211         "                                       b3Float4 treeBminLocal = MyUnQuantize(subtreeB.m_quantizedAabbMin,bvhInfos[bvhB].m_quantization,bvhInfos[bvhB].m_aabbMin);\n"
1212         "                                       b3Float4 treeBmaxLocal = MyUnQuantize(subtreeB.m_quantizedAabbMax,bvhInfos[bvhB].m_quantization,bvhInfos[bvhB].m_aabbMin);\n"
1213         "                                       b3Float4 aabbBMinOut,aabbBMaxOut;\n"
1214         "                                       float margin=0.f;\n"
1215         "                                       b3TransformAabb2(treeBminLocal,treeBmaxLocal, margin,posB,ornB,&aabbBMinOut,&aabbBMaxOut);\n"
1216         "                                       \n"
1217         "                                       \n"
1218         "                                       bool aabbOverlap = b3TestAabbAgainstAabb(aabbAMinOut,aabbAMaxOut,aabbBMinOut,aabbBMaxOut);\n"
1219         "                                       if (aabbOverlap)\n"
1220         "                                       {\n"
1221         "                                               \n"
1222         "                                               int startNodeIndexA = subtreeA.m_rootNodeIndex+bvhInfos[bvhA].m_nodeOffset;\n"
1223         "                                               int endNodeIndexA = startNodeIndexA+subtreeA.m_subtreeSize;\n"
1224         "                                               int startNodeIndexB = subtreeB.m_rootNodeIndex+bvhInfos[bvhB].m_nodeOffset;\n"
1225         "                                               int endNodeIndexB = startNodeIndexB+subtreeB.m_subtreeSize;\n"
1226         "                                               b3Int2 nodeStack[B3_MAX_STACK_DEPTH];\n"
1227         "                                               b3Int2 node0;\n"
1228         "                                               node0.x = startNodeIndexA;\n"
1229         "                                               node0.y = startNodeIndexB;\n"
1230         "                                               int maxStackDepth = B3_MAX_STACK_DEPTH;\n"
1231         "                                               int depth=0;\n"
1232         "                                               nodeStack[depth++]=node0;\n"
1233         "                                               do\n"
1234         "                                               {\n"
1235         "                                                       b3Int2 node = nodeStack[--depth];\n"
1236         "                                                       b3Float4 aMinLocal = MyUnQuantizeGlobal(quantizedNodes[node.x].m_quantizedAabbMin,bvhInfos[bvhA].m_quantization,bvhInfos[bvhA].m_aabbMin);\n"
1237         "                                                       b3Float4 aMaxLocal = MyUnQuantizeGlobal(quantizedNodes[node.x].m_quantizedAabbMax,bvhInfos[bvhA].m_quantization,bvhInfos[bvhA].m_aabbMin);\n"
1238         "                                                       b3Float4 bMinLocal = MyUnQuantizeGlobal(quantizedNodes[node.y].m_quantizedAabbMin,bvhInfos[bvhB].m_quantization,bvhInfos[bvhB].m_aabbMin);\n"
1239         "                                                       b3Float4 bMaxLocal = MyUnQuantizeGlobal(quantizedNodes[node.y].m_quantizedAabbMax,bvhInfos[bvhB].m_quantization,bvhInfos[bvhB].m_aabbMin);\n"
1240         "                                                       float margin=0.f;\n"
1241         "                                                       b3Float4 aabbAMinOut,aabbAMaxOut;\n"
1242         "                                                       b3TransformAabb2(aMinLocal,aMaxLocal, margin,posA,ornA,&aabbAMinOut,&aabbAMaxOut);\n"
1243         "                                                       b3Float4 aabbBMinOut,aabbBMaxOut;\n"
1244         "                                                       b3TransformAabb2(bMinLocal,bMaxLocal, margin,posB,ornB,&aabbBMinOut,&aabbBMaxOut);\n"
1245         "                                                       \n"
1246         "                                                       bool nodeOverlap = b3TestAabbAgainstAabb(aabbAMinOut,aabbAMaxOut,aabbBMinOut,aabbBMaxOut);\n"
1247         "                                                       if (nodeOverlap)\n"
1248         "                                                       {\n"
1249         "                                                               bool isLeafA = isLeafNodeGlobal(&quantizedNodes[node.x]);\n"
1250         "                                                               bool isLeafB = isLeafNodeGlobal(&quantizedNodes[node.y]);\n"
1251         "                                                               bool isInternalA = !isLeafA;\n"
1252         "                                                               bool isInternalB = !isLeafB;\n"
1253         "                                                               //fail, even though it might hit two leaf nodes\n"
1254         "                                                               if (depth+4>maxStackDepth && !(isLeafA && isLeafB))\n"
1255         "                                                               {\n"
1256         "                                                                       //printf(\"Error: traversal exceeded maxStackDepth\");\n"
1257         "                                                                       continue;\n"
1258         "                                                               }\n"
1259         "                                                               if(isInternalA)\n"
1260         "                                                               {\n"
1261         "                                                                       int nodeAleftChild = node.x+1;\n"
1262         "                                                                       bool isNodeALeftChildLeaf = isLeafNodeGlobal(&quantizedNodes[node.x+1]);\n"
1263         "                                                                       int nodeArightChild = isNodeALeftChildLeaf? node.x+2 : node.x+1 + getEscapeIndexGlobal(&quantizedNodes[node.x+1]);\n"
1264         "                                                                       if(isInternalB)\n"
1265         "                                                                       {                                       \n"
1266         "                                                                               int nodeBleftChild = node.y+1;\n"
1267         "                                                                               bool isNodeBLeftChildLeaf = isLeafNodeGlobal(&quantizedNodes[node.y+1]);\n"
1268         "                                                                               int nodeBrightChild = isNodeBLeftChildLeaf? node.y+2 : node.y+1 + getEscapeIndexGlobal(&quantizedNodes[node.y+1]);\n"
1269         "                                                                               nodeStack[depth++] = b3MakeInt2(nodeAleftChild, nodeBleftChild);\n"
1270         "                                                                               nodeStack[depth++] = b3MakeInt2(nodeArightChild, nodeBleftChild);\n"
1271         "                                                                               nodeStack[depth++] = b3MakeInt2(nodeAleftChild, nodeBrightChild);\n"
1272         "                                                                               nodeStack[depth++] = b3MakeInt2(nodeArightChild, nodeBrightChild);\n"
1273         "                                                                       }\n"
1274         "                                                                       else\n"
1275         "                                                                       {\n"
1276         "                                                                               nodeStack[depth++] = b3MakeInt2(nodeAleftChild,node.y);\n"
1277         "                                                                               nodeStack[depth++] = b3MakeInt2(nodeArightChild,node.y);\n"
1278         "                                                                       }\n"
1279         "                                                               }\n"
1280         "                                                               else\n"
1281         "                                                               {\n"
1282         "                                                                       if(isInternalB)\n"
1283         "                                                                       {\n"
1284         "                                                                               int nodeBleftChild = node.y+1;\n"
1285         "                                                                               bool isNodeBLeftChildLeaf = isLeafNodeGlobal(&quantizedNodes[node.y+1]);\n"
1286         "                                                                               int nodeBrightChild = isNodeBLeftChildLeaf? node.y+2 : node.y+1 + getEscapeIndexGlobal(&quantizedNodes[node.y+1]);\n"
1287         "                                                                               nodeStack[depth++] = b3MakeInt2(node.x,nodeBleftChild);\n"
1288         "                                                                               nodeStack[depth++] = b3MakeInt2(node.x,nodeBrightChild);\n"
1289         "                                                                       }\n"
1290         "                                                                       else\n"
1291         "                                                                       {\n"
1292         "                                                                               int compoundPairIdx = atomic_inc(numCompoundPairsOut);\n"
1293         "                                                                               if (compoundPairIdx<maxNumCompoundPairsCapacity)\n"
1294         "                                                                               {\n"
1295         "                                                                                       int childShapeIndexA = getTriangleIndexGlobal(&quantizedNodes[node.x]);\n"
1296         "                                                                                       int childShapeIndexB = getTriangleIndexGlobal(&quantizedNodes[node.y]);\n"
1297         "                                                                                       gpuCompoundPairsOut[compoundPairIdx]  = (int4)(bodyIndexA,bodyIndexB,childShapeIndexA,childShapeIndexB);\n"
1298         "                                                                               }\n"
1299         "                                                                       }\n"
1300         "                                                               }\n"
1301         "                                                       }\n"
1302         "                                               } while (depth);\n"
1303         "                                       }\n"
1304         "                               }\n"
1305         "                       }\n"
1306         "                       \n"
1307         "                       return;\n"
1308         "               }\n"
1309         "               if ((collidables[collidableIndexA].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS) ||(collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS))\n"
1310         "               {\n"
1311         "                       if (collidables[collidableIndexA].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS) \n"
1312         "                       {\n"
1313         "                               int numChildrenA = collidables[collidableIndexA].m_numChildShapes;\n"
1314         "                               for (int c=0;c<numChildrenA;c++)\n"
1315         "                               {\n"
1316         "                                       int childShapeIndexA = collidables[collidableIndexA].m_shapeIndex+c;\n"
1317         "                                       int childColIndexA = gpuChildShapes[childShapeIndexA].m_shapeIndex;\n"
1318         "                                       float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
1319         "                                       float4 ornA = rigidBodies[bodyIndexA].m_quat;\n"
1320         "                                       float4 childPosA = gpuChildShapes[childShapeIndexA].m_childPosition;\n"
1321         "                                       float4 childOrnA = gpuChildShapes[childShapeIndexA].m_childOrientation;\n"
1322         "                                       float4 newPosA = qtRotate(ornA,childPosA)+posA;\n"
1323         "                                       float4 newOrnA = qtMul(ornA,childOrnA);\n"
1324         "                                       int shapeIndexA = collidables[childColIndexA].m_shapeIndex;\n"
1325         "                                       b3Aabb_t aabbAlocal = aabbLocalSpace[shapeIndexA];\n"
1326         "                                       float margin = 0.f;\n"
1327         "                                       \n"
1328         "                                       b3Float4 aabbAMinWS;\n"
1329         "                                       b3Float4 aabbAMaxWS;\n"
1330         "                                       \n"
1331         "                                       b3TransformAabb2(aabbAlocal.m_minVec,aabbAlocal.m_maxVec,margin,\n"
1332         "                                               newPosA,\n"
1333         "                                               newOrnA,\n"
1334         "                                               &aabbAMinWS,&aabbAMaxWS);\n"
1335         "                                               \n"
1336         "                                       \n"
1337         "                                       if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)\n"
1338         "                                       {\n"
1339         "                                               int numChildrenB = collidables[collidableIndexB].m_numChildShapes;\n"
1340         "                                               for (int b=0;b<numChildrenB;b++)\n"
1341         "                                               {\n"
1342         "                                                       int childShapeIndexB = collidables[collidableIndexB].m_shapeIndex+b;\n"
1343         "                                                       int childColIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;\n"
1344         "                                                       float4 ornB = rigidBodies[bodyIndexB].m_quat;\n"
1345         "                                                       float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
1346         "                                                       float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;\n"
1347         "                                                       float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;\n"
1348         "                                                       float4 newPosB = transform(&childPosB,&posB,&ornB);\n"
1349         "                                                       float4 newOrnB = qtMul(ornB,childOrnB);\n"
1350         "                                                       int shapeIndexB = collidables[childColIndexB].m_shapeIndex;\n"
1351         "                                                       b3Aabb_t aabbBlocal = aabbLocalSpace[shapeIndexB];\n"
1352         "                                                       \n"
1353         "                                                       b3Float4 aabbBMinWS;\n"
1354         "                                                       b3Float4 aabbBMaxWS;\n"
1355         "                                                       \n"
1356         "                                                       b3TransformAabb2(aabbBlocal.m_minVec,aabbBlocal.m_maxVec,margin,\n"
1357         "                                                               newPosB,\n"
1358         "                                                               newOrnB,\n"
1359         "                                                               &aabbBMinWS,&aabbBMaxWS);\n"
1360         "                                                               \n"
1361         "                                                               \n"
1362         "                                                       \n"
1363         "                                                       bool aabbOverlap = b3TestAabbAgainstAabb(aabbAMinWS,aabbAMaxWS,aabbBMinWS,aabbBMaxWS);\n"
1364         "                                                       if (aabbOverlap)\n"
1365         "                                                       {\n"
1366         "                                                               int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
1367         "                                                               float dmin = FLT_MAX;\n"
1368         "                                                               float4 posA = newPosA;\n"
1369         "                                                               posA.w = 0.f;\n"
1370         "                                                               float4 posB = newPosB;\n"
1371         "                                                               posB.w = 0.f;\n"
1372         "                                                               float4 c0local = convexShapes[shapeIndexA].m_localCenter;\n"
1373         "                                                               float4 ornA = newOrnA;\n"
1374         "                                                               float4 c0 = transform(&c0local, &posA, &ornA);\n"
1375         "                                                               float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
1376         "                                                               float4 ornB =newOrnB;\n"
1377         "                                                               float4 c1 = transform(&c1local,&posB,&ornB);\n"
1378         "                                                               const float4 DeltaC2 = c0 - c1;\n"
1379         "                                                               {//\n"
1380         "                                                                       int compoundPairIdx = atomic_inc(numCompoundPairsOut);\n"
1381         "                                                                       if (compoundPairIdx<maxNumCompoundPairsCapacity)\n"
1382         "                                                                       {\n"
1383         "                                                                               gpuCompoundPairsOut[compoundPairIdx]  = (int4)(bodyIndexA,bodyIndexB,childShapeIndexA,childShapeIndexB);\n"
1384         "                                                                       }\n"
1385         "                                                               }//\n"
1386         "                                                       }//fi(1)\n"
1387         "                                               } //for (int b=0\n"
1388         "                                       }//if (collidables[collidableIndexB].\n"
1389         "                                       else//if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)\n"
1390         "                                       {\n"
1391         "                                               if (1)\n"
1392         "                                               {\n"
1393         "                                                       int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
1394         "                                                       float dmin = FLT_MAX;\n"
1395         "                                                       float4 posA = newPosA;\n"
1396         "                                                       posA.w = 0.f;\n"
1397         "                                                       float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
1398         "                                                       posB.w = 0.f;\n"
1399         "                                                       float4 c0local = convexShapes[shapeIndexA].m_localCenter;\n"
1400         "                                                       float4 ornA = newOrnA;\n"
1401         "                                                       float4 c0 = transform(&c0local, &posA, &ornA);\n"
1402         "                                                       float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
1403         "                                                       float4 ornB = rigidBodies[bodyIndexB].m_quat;\n"
1404         "                                                       float4 c1 = transform(&c1local,&posB,&ornB);\n"
1405         "                                                       const float4 DeltaC2 = c0 - c1;\n"
1406         "                                                       {\n"
1407         "                                                               int compoundPairIdx = atomic_inc(numCompoundPairsOut);\n"
1408         "                                                               if (compoundPairIdx<maxNumCompoundPairsCapacity)\n"
1409         "                                                               {\n"
1410         "                                                                       gpuCompoundPairsOut[compoundPairIdx] = (int4)(bodyIndexA,bodyIndexB,childShapeIndexA,-1);\n"
1411         "                                                               }//if (compoundPairIdx<maxNumCompoundPairsCapacity)\n"
1412         "                                                       }//\n"
1413         "                                               }//fi (1)\n"
1414         "                                       }//if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)\n"
1415         "                               }//for (int b=0;b<numChildrenB;b++)     \n"
1416         "                               return;\n"
1417         "                       }//if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)\n"
1418         "                       if ((collidables[collidableIndexA].m_shapeType!=SHAPE_CONCAVE_TRIMESH) \n"
1419         "                               && (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS))\n"
1420         "                       {\n"
1421         "                               int numChildrenB = collidables[collidableIndexB].m_numChildShapes;\n"
1422         "                               for (int b=0;b<numChildrenB;b++)\n"
1423         "                               {\n"
1424         "                                       int childShapeIndexB = collidables[collidableIndexB].m_shapeIndex+b;\n"
1425         "                                       int childColIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;\n"
1426         "                                       float4 ornB = rigidBodies[bodyIndexB].m_quat;\n"
1427         "                                       float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
1428         "                                       float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;\n"
1429         "                                       float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;\n"
1430         "                                       float4 newPosB = qtRotate(ornB,childPosB)+posB;\n"
1431         "                                       float4 newOrnB = qtMul(ornB,childOrnB);\n"
1432         "                                       int shapeIndexB = collidables[childColIndexB].m_shapeIndex;\n"
1433         "                                       //////////////////////////////////////\n"
1434         "                                       if (1)\n"
1435         "                                       {\n"
1436         "                                               int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
1437         "                                               float dmin = FLT_MAX;\n"
1438         "                                               float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
1439         "                                               posA.w = 0.f;\n"
1440         "                                               float4 posB = newPosB;\n"
1441         "                                               posB.w = 0.f;\n"
1442         "                                               float4 c0local = convexShapes[shapeIndexA].m_localCenter;\n"
1443         "                                               float4 ornA = rigidBodies[bodyIndexA].m_quat;\n"
1444         "                                               float4 c0 = transform(&c0local, &posA, &ornA);\n"
1445         "                                               float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
1446         "                                               float4 ornB =newOrnB;\n"
1447         "                                               float4 c1 = transform(&c1local,&posB,&ornB);\n"
1448         "                                               const float4 DeltaC2 = c0 - c1;\n"
1449         "                                               {//\n"
1450         "                                                       int compoundPairIdx = atomic_inc(numCompoundPairsOut);\n"
1451         "                                                       if (compoundPairIdx<maxNumCompoundPairsCapacity)\n"
1452         "                                                       {\n"
1453         "                                                               gpuCompoundPairsOut[compoundPairIdx] = (int4)(bodyIndexA,bodyIndexB,-1,childShapeIndexB);\n"
1454         "                                                       }//fi (compoundPairIdx<maxNumCompoundPairsCapacity)\n"
1455         "                                               }//\n"
1456         "                                       }//fi (1)       \n"
1457         "                               }//for (int b=0;b<numChildrenB;b++)\n"
1458         "                               return;\n"
1459         "                       }//if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)\n"
1460         "                       return;\n"
1461         "               }//fi ((collidables[collidableIndexA].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS) ||(collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS))\n"
1462         "       }//i<numPairs\n"
1463         "}\n"
1464         "// work-in-progress\n"
1465         "__kernel void   findSeparatingAxisKernel( __global const int4* pairs, \n"
1466         "                                                                                                                                                                       __global const BodyData* rigidBodies, \n"
1467         "                                                                                                                                                                       __global const btCollidableGpu* collidables,\n"
1468         "                                                                                                                                                                       __global const ConvexPolyhedronCL* convexShapes, \n"
1469         "                                                                                                                                                                       __global const float4* vertices,\n"
1470         "                                                                                                                                                                       __global const float4* uniqueEdges,\n"
1471         "                                                                                                                                                                       __global const btGpuFace* faces,\n"
1472         "                                                                                                                                                                       __global const int* indices,\n"
1473         "                                                                                                                                                                       __global btAabbCL* aabbs,\n"
1474         "                                                                                                                                                                       __global volatile float4* separatingNormals,\n"
1475         "                                                                                                                                                                       __global volatile int* hasSeparatingAxis,\n"
1476         "                                                                                                                                                                       int numPairs\n"
1477         "                                                                                                                                                                       )\n"
1478         "{\n"
1479         "       int i = get_global_id(0);\n"
1480         "       \n"
1481         "       if (i<numPairs)\n"
1482         "       {\n"
1483         "       \n"
1484         "               int bodyIndexA = pairs[i].x;\n"
1485         "               int bodyIndexB = pairs[i].y;\n"
1486         "               int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
1487         "               int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n"
1488         "       \n"
1489         "               int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
1490         "               int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
1491         "               \n"
1492         "               \n"
1493         "               //once the broadphase avoids static-static pairs, we can remove this test\n"
1494         "               if ((rigidBodies[bodyIndexA].m_invMass==0) &&(rigidBodies[bodyIndexB].m_invMass==0))\n"
1495         "               {\n"
1496         "                       hasSeparatingAxis[i] = 0;\n"
1497         "                       return;\n"
1498         "               }\n"
1499         "               \n"
1500         "               if ((collidables[collidableIndexA].m_shapeType!=SHAPE_CONVEX_HULL) ||(collidables[collidableIndexB].m_shapeType!=SHAPE_CONVEX_HULL))\n"
1501         "               {\n"
1502         "                       hasSeparatingAxis[i] = 0;\n"
1503         "                       return;\n"
1504         "               }\n"
1505         "                       \n"
1506         "               if ((collidables[collidableIndexA].m_shapeType==SHAPE_CONCAVE_TRIMESH))\n"
1507         "               {\n"
1508         "                       hasSeparatingAxis[i] = 0;\n"
1509         "                       return;\n"
1510         "               }\n"
1511         "               int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
1512         "               float dmin = FLT_MAX;\n"
1513         "               float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
1514         "               posA.w = 0.f;\n"
1515         "               float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
1516         "               posB.w = 0.f;\n"
1517         "               float4 c0local = convexShapes[shapeIndexA].m_localCenter;\n"
1518         "               float4 ornA = rigidBodies[bodyIndexA].m_quat;\n"
1519         "               float4 c0 = transform(&c0local, &posA, &ornA);\n"
1520         "               float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
1521         "               float4 ornB =rigidBodies[bodyIndexB].m_quat;\n"
1522         "               float4 c1 = transform(&c1local,&posB,&ornB);\n"
1523         "               const float4 DeltaC2 = c0 - c1;\n"
1524         "               float4 sepNormal;\n"
1525         "               \n"
1526         "               bool sepA = findSeparatingAxis( &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,\n"
1527         "                                                                                                                                                                                               posB,ornB,\n"
1528         "                                                                                                                                                                                               DeltaC2,\n"
1529         "                                                                                                                                                                                               vertices,uniqueEdges,faces,\n"
1530         "                                                                                                                                                                                               indices,&sepNormal,&dmin);\n"
1531         "               hasSeparatingAxis[i] = 4;\n"
1532         "               if (!sepA)\n"
1533         "               {\n"
1534         "                       hasSeparatingAxis[i] = 0;\n"
1535         "               } else\n"
1536         "               {\n"
1537         "                       bool sepB = findSeparatingAxis( &convexShapes[shapeIndexB],&convexShapes[shapeIndexA],posB,ornB,\n"
1538         "                                                                                                                                                                                                       posA,ornA,\n"
1539         "                                                                                                                                                                                                       DeltaC2,\n"
1540         "                                                                                                                                                                                                       vertices,uniqueEdges,faces,\n"
1541         "                                                                                                                                                                                                       indices,&sepNormal,&dmin);\n"
1542         "                       if (!sepB)\n"
1543         "                       {\n"
1544         "                               hasSeparatingAxis[i] = 0;\n"
1545         "                       } else\n"
1546         "                       {\n"
1547         "                               bool sepEE = findSeparatingAxisEdgeEdge(        &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,\n"
1548         "                                                                                                                                                                                                       posB,ornB,\n"
1549         "                                                                                                                                                                                                       DeltaC2,\n"
1550         "                                                                                                                                                                                                       vertices,uniqueEdges,faces,\n"
1551         "                                                                                                                                                                                                       indices,&sepNormal,&dmin);\n"
1552         "                               if (!sepEE)\n"
1553         "                               {\n"
1554         "                                       hasSeparatingAxis[i] = 0;\n"
1555         "                               } else\n"
1556         "                               {\n"
1557         "                                       hasSeparatingAxis[i] = 1;\n"
1558         "                                       separatingNormals[i] = sepNormal;\n"
1559         "                               }\n"
1560         "                       }\n"
1561         "               }\n"
1562         "               \n"
1563         "       }\n"
1564         "}\n"
1565         "__kernel void   findSeparatingAxisVertexFaceKernel( __global const int4* pairs, \n"
1566         "                                                                                                                                                                       __global const BodyData* rigidBodies, \n"
1567         "                                                                                                                                                                       __global const btCollidableGpu* collidables,\n"
1568         "                                                                                                                                                                       __global const ConvexPolyhedronCL* convexShapes, \n"
1569         "                                                                                                                                                                       __global const float4* vertices,\n"
1570         "                                                                                                                                                                       __global const float4* uniqueEdges,\n"
1571         "                                                                                                                                                                       __global const btGpuFace* faces,\n"
1572         "                                                                                                                                                                       __global const int* indices,\n"
1573         "                                                                                                                                                                       __global btAabbCL* aabbs,\n"
1574         "                                                                                                                                                                       __global volatile float4* separatingNormals,\n"
1575         "                                                                                                                                                                       __global volatile int* hasSeparatingAxis,\n"
1576         "                                                                                                                                                                       __global  float* dmins,\n"
1577         "                                                                                                                                                                       int numPairs\n"
1578         "                                                                                                                                                                       )\n"
1579         "{\n"
1580         "       int i = get_global_id(0);\n"
1581         "       \n"
1582         "       if (i<numPairs)\n"
1583         "       {\n"
1584         "       \n"
1585         "               int bodyIndexA = pairs[i].x;\n"
1586         "               int bodyIndexB = pairs[i].y;\n"
1587         "               int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
1588         "               int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n"
1589         "       \n"
1590         "               int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
1591         "               int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
1592         "       \n"
1593         "               hasSeparatingAxis[i] = 0;       \n"
1594         "               \n"
1595         "               //once the broadphase avoids static-static pairs, we can remove this test\n"
1596         "               if ((rigidBodies[bodyIndexA].m_invMass==0) &&(rigidBodies[bodyIndexB].m_invMass==0))\n"
1597         "               {\n"
1598         "                       return;\n"
1599         "               }\n"
1600         "               \n"
1601         "               if ((collidables[collidableIndexA].m_shapeType!=SHAPE_CONVEX_HULL) ||(collidables[collidableIndexB].m_shapeType!=SHAPE_CONVEX_HULL))\n"
1602         "               {\n"
1603         "                       return;\n"
1604         "               }\n"
1605         "                       \n"
1606         "               int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
1607         "               float dmin = FLT_MAX;\n"
1608         "               dmins[i] = dmin;\n"
1609         "               \n"
1610         "               float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
1611         "               posA.w = 0.f;\n"
1612         "               float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
1613         "               posB.w = 0.f;\n"
1614         "               float4 c0local = convexShapes[shapeIndexA].m_localCenter;\n"
1615         "               float4 ornA = rigidBodies[bodyIndexA].m_quat;\n"
1616         "               float4 c0 = transform(&c0local, &posA, &ornA);\n"
1617         "               float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
1618         "               float4 ornB =rigidBodies[bodyIndexB].m_quat;\n"
1619         "               float4 c1 = transform(&c1local,&posB,&ornB);\n"
1620         "               const float4 DeltaC2 = c0 - c1;\n"
1621         "               float4 sepNormal;\n"
1622         "               \n"
1623         "               bool sepA = findSeparatingAxis( &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,\n"
1624         "                                                                                                                                                                                               posB,ornB,\n"
1625         "                                                                                                                                                                                               DeltaC2,\n"
1626         "                                                                                                                                                                                               vertices,uniqueEdges,faces,\n"
1627         "                                                                                                                                                                                               indices,&sepNormal,&dmin);\n"
1628         "               hasSeparatingAxis[i] = 4;\n"
1629         "               if (!sepA)\n"
1630         "               {\n"
1631         "                       hasSeparatingAxis[i] = 0;\n"
1632         "               } else\n"
1633         "               {\n"
1634         "                       bool sepB = findSeparatingAxis( &convexShapes[shapeIndexB],&convexShapes[shapeIndexA],posB,ornB,\n"
1635         "                                                                                                                                                                                                       posA,ornA,\n"
1636         "                                                                                                                                                                                                       DeltaC2,\n"
1637         "                                                                                                                                                                                                       vertices,uniqueEdges,faces,\n"
1638         "                                                                                                                                                                                                       indices,&sepNormal,&dmin);\n"
1639         "                       if (sepB)\n"
1640         "                       {\n"
1641         "                               dmins[i] = dmin;\n"
1642         "                               hasSeparatingAxis[i] = 1;\n"
1643         "                               separatingNormals[i] = sepNormal;\n"
1644         "                       }\n"
1645         "               }\n"
1646         "               \n"
1647         "       }\n"
1648         "}\n"
1649         "__kernel void   findSeparatingAxisEdgeEdgeKernel( __global const int4* pairs, \n"
1650         "                                                                                                                                                                       __global const BodyData* rigidBodies, \n"
1651         "                                                                                                                                                                       __global const btCollidableGpu* collidables,\n"
1652         "                                                                                                                                                                       __global const ConvexPolyhedronCL* convexShapes, \n"
1653         "                                                                                                                                                                       __global const float4* vertices,\n"
1654         "                                                                                                                                                                       __global const float4* uniqueEdges,\n"
1655         "                                                                                                                                                                       __global const btGpuFace* faces,\n"
1656         "                                                                                                                                                                       __global const int* indices,\n"
1657         "                                                                                                                                                                       __global btAabbCL* aabbs,\n"
1658         "                                                                                                                                                                       __global  float4* separatingNormals,\n"
1659         "                                                                                                                                                                       __global  int* hasSeparatingAxis,\n"
1660         "                                                                                                                                                                       __global  float* dmins,\n"
1661         "                                                                                                                                                                       __global const float4* unitSphereDirections,\n"
1662         "                                                                                                                                                                       int numUnitSphereDirections,\n"
1663         "                                                                                                                                                                       int numPairs\n"
1664         "                                                                                                                                                                       )\n"
1665         "{\n"
1666         "       int i = get_global_id(0);\n"
1667         "       \n"
1668         "       if (i<numPairs)\n"
1669         "       {\n"
1670         "               if (hasSeparatingAxis[i])\n"
1671         "               {\n"
1672         "       \n"
1673         "                       int bodyIndexA = pairs[i].x;\n"
1674         "                       int bodyIndexB = pairs[i].y;\n"
1675         "       \n"
1676         "                       int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
1677         "                       int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n"
1678         "               \n"
1679         "                       int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
1680         "                       int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
1681         "                       \n"
1682         "                       \n"
1683         "                       int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
1684         "       \n"
1685         "                       float dmin = dmins[i];\n"
1686         "       \n"
1687         "                       float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
1688         "                       posA.w = 0.f;\n"
1689         "                       float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
1690         "                       posB.w = 0.f;\n"
1691         "                       float4 c0local = convexShapes[shapeIndexA].m_localCenter;\n"
1692         "                       float4 ornA = rigidBodies[bodyIndexA].m_quat;\n"
1693         "                       float4 c0 = transform(&c0local, &posA, &ornA);\n"
1694         "                       float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
1695         "                       float4 ornB =rigidBodies[bodyIndexB].m_quat;\n"
1696         "                       float4 c1 = transform(&c1local,&posB,&ornB);\n"
1697         "                       const float4 DeltaC2 = c0 - c1;\n"
1698         "                       float4 sepNormal = separatingNormals[i];\n"
1699         "                       \n"
1700         "                       \n"
1701         "                       \n"
1702         "                       bool sepEE = false;\n"
1703         "                       int numEdgeEdgeDirections = convexShapes[shapeIndexA].m_numUniqueEdges*convexShapes[shapeIndexB].m_numUniqueEdges;\n"
1704         "                       if (numEdgeEdgeDirections<=numUnitSphereDirections)\n"
1705         "                       {\n"
1706         "                               sepEE = findSeparatingAxisEdgeEdge(     &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,\n"
1707         "                                                                                                                                                                                                       posB,ornB,\n"
1708         "                                                                                                                                                                                                       DeltaC2,\n"
1709         "                                                                                                                                                                                                       vertices,uniqueEdges,faces,\n"
1710         "                                                                                                                                                                                                       indices,&sepNormal,&dmin);\n"
1711         "                                                                                                                                                                                                       \n"
1712         "                                       if (!sepEE)\n"
1713         "                                       {\n"
1714         "                                               hasSeparatingAxis[i] = 0;\n"
1715         "                                       } else\n"
1716         "                                       {\n"
1717         "                                               hasSeparatingAxis[i] = 1;\n"
1718         "                                               separatingNormals[i] = sepNormal;\n"
1719         "                                       }\n"
1720         "                       }\n"
1721         "                       /*\n"
1722         "                       ///else case is a separate kernel, to make Mac OSX OpenCL compiler happy\n"
1723         "                       else\n"
1724         "                       {\n"
1725         "                               sepEE = findSeparatingAxisUnitSphere(&convexShapes[shapeIndexA], &convexShapes[shapeIndexB],posA,ornA,\n"
1726         "                                                                                                                                                                                                       posB,ornB,\n"
1727         "                                                                                                                                                                                                       DeltaC2,\n"
1728         "                                                                                                                                                                                                       vertices,unitSphereDirections,numUnitSphereDirections,\n"
1729         "                                                                                                                                                                                                       &sepNormal,&dmin);\n"
1730         "                                       if (!sepEE)\n"
1731         "                                       {\n"
1732         "                                               hasSeparatingAxis[i] = 0;\n"
1733         "                                       } else\n"
1734         "                                       {\n"
1735         "                                               hasSeparatingAxis[i] = 1;\n"
1736         "                                               separatingNormals[i] = sepNormal;\n"
1737         "                                       }\n"
1738         "                       }\n"
1739         "                       */\n"
1740         "               }               //if (hasSeparatingAxis[i])\n"
1741         "       }//(i<numPairs)\n"
1742         "}\n"
1743         "inline int     findClippingFaces(const float4 separatingNormal,\n"
1744         "                      const ConvexPolyhedronCL* hullA, \n"
1745         "                                         __global const ConvexPolyhedronCL* hullB,\n"
1746         "                      const float4 posA, const Quaternion ornA,const float4 posB, const Quaternion ornB,\n"
1747         "                       __global float4* worldVertsA1,\n"
1748         "                      __global float4* worldNormalsA1,\n"
1749         "                      __global float4* worldVertsB1,\n"
1750         "                      int capacityWorldVerts,\n"
1751         "                      const float minDist, float maxDist,\n"
1752         "                                         const float4* verticesA,\n"
1753         "                      const btGpuFace* facesA,\n"
1754         "                      const int* indicesA,\n"
1755         "                                         __global const float4* verticesB,\n"
1756         "                      __global const btGpuFace* facesB,\n"
1757         "                      __global const int* indicesB,\n"
1758         "                      __global int4* clippingFaces, int pairIndex)\n"
1759         "{\n"
1760         "       int numContactsOut = 0;\n"
1761         "       int numWorldVertsB1= 0;\n"
1762         "    \n"
1763         "    \n"
1764         "       int closestFaceB=0;\n"
1765         "       float dmax = -FLT_MAX;\n"
1766         "    \n"
1767         "       {\n"
1768         "               for(int face=0;face<hullB->m_numFaces;face++)\n"
1769         "               {\n"
1770         "                       const float4 Normal = make_float4(facesB[hullB->m_faceOffset+face].m_plane.x,\n"
1771         "                                              facesB[hullB->m_faceOffset+face].m_plane.y, facesB[hullB->m_faceOffset+face].m_plane.z,0.f);\n"
1772         "                       const float4 WorldNormal = qtRotate(ornB, Normal);\n"
1773         "                       float d = dot3F4(WorldNormal,separatingNormal);\n"
1774         "                       if (d > dmax)\n"
1775         "                       {\n"
1776         "                               dmax = d;\n"
1777         "                               closestFaceB = face;\n"
1778         "                       }\n"
1779         "               }\n"
1780         "       }\n"
1781         "    \n"
1782         "       {\n"
1783         "               const btGpuFace polyB = facesB[hullB->m_faceOffset+closestFaceB];\n"
1784         "               int numVertices = polyB.m_numIndices;\n"
1785         "        if (numVertices>capacityWorldVerts)\n"
1786         "            numVertices = capacityWorldVerts;\n"
1787         "        \n"
1788         "               for(int e0=0;e0<numVertices;e0++)\n"
1789         "               {\n"
1790         "            if (e0<capacityWorldVerts)\n"
1791         "            {\n"
1792         "                const float4 b = verticesB[hullB->m_vertexOffset+indicesB[polyB.m_indexOffset+e0]];\n"
1793         "                worldVertsB1[pairIndex*capacityWorldVerts+numWorldVertsB1++] = transform(&b,&posB,&ornB);\n"
1794         "            }\n"
1795         "               }\n"
1796         "       }\n"
1797         "    \n"
1798         "    int closestFaceA=0;\n"
1799         "       {\n"
1800         "               float dmin = FLT_MAX;\n"
1801         "               for(int face=0;face<hullA->m_numFaces;face++)\n"
1802         "               {\n"
1803         "                       const float4 Normal = make_float4(\n"
1804         "                                              facesA[hullA->m_faceOffset+face].m_plane.x,\n"
1805         "                                              facesA[hullA->m_faceOffset+face].m_plane.y,\n"
1806         "                                              facesA[hullA->m_faceOffset+face].m_plane.z,\n"
1807         "                                              0.f);\n"
1808         "                       const float4 faceANormalWS = qtRotate(ornA,Normal);\n"
1809         "            \n"
1810         "                       float d = dot3F4(faceANormalWS,separatingNormal);\n"
1811         "                       if (d < dmin)\n"
1812         "                       {\n"
1813         "                               dmin = d;\n"
1814         "                               closestFaceA = face;\n"
1815         "                worldNormalsA1[pairIndex] = faceANormalWS;\n"
1816         "                       }\n"
1817         "               }\n"
1818         "       }\n"
1819         "    \n"
1820         "    int numVerticesA = facesA[hullA->m_faceOffset+closestFaceA].m_numIndices;\n"
1821         "    if (numVerticesA>capacityWorldVerts)\n"
1822         "       numVerticesA = capacityWorldVerts;\n"
1823         "    \n"
1824         "       for(int e0=0;e0<numVerticesA;e0++)\n"
1825         "       {\n"
1826         "        if (e0<capacityWorldVerts)\n"
1827         "        {\n"
1828         "            const float4 a = verticesA[hullA->m_vertexOffset+indicesA[facesA[hullA->m_faceOffset+closestFaceA].m_indexOffset+e0]];\n"
1829         "            worldVertsA1[pairIndex*capacityWorldVerts+e0] = transform(&a, &posA,&ornA);\n"
1830         "        }\n"
1831         "    }\n"
1832         "    \n"
1833         "    clippingFaces[pairIndex].x = closestFaceA;\n"
1834         "    clippingFaces[pairIndex].y = closestFaceB;\n"
1835         "    clippingFaces[pairIndex].z = numVerticesA;\n"
1836         "    clippingFaces[pairIndex].w = numWorldVertsB1;\n"
1837         "    \n"
1838         "    \n"
1839         "       return numContactsOut;\n"
1840         "}\n"
1841         "// work-in-progress\n"
1842         "__kernel void   findConcaveSeparatingAxisKernel( __global int4* concavePairs,\n"
1843         "                                                                                                                                                                       __global const BodyData* rigidBodies,\n"
1844         "                                                                                                                                                                       __global const btCollidableGpu* collidables,\n"
1845         "                                                                                                                                                                       __global const ConvexPolyhedronCL* convexShapes, \n"
1846         "                                                                                                                                                                       __global const float4* vertices,\n"
1847         "                                                                                                                                                                       __global const float4* uniqueEdges,\n"
1848         "                                                                                                                                                                       __global const btGpuFace* faces,\n"
1849         "                                                                                                                                                                       __global const int* indices,\n"
1850         "                                                                                                                                                                       __global const btGpuChildShape* gpuChildShapes,\n"
1851         "                                                                                                                                                                       __global btAabbCL* aabbs,\n"
1852         "                                                                                                                                                                       __global float4* concaveSeparatingNormalsOut,\n"
1853         "                                                                                                                                                                       __global int* concaveHasSeparatingNormals,\n"
1854         "                                                                                                                                                                       __global int4* clippingFacesOut,\n"
1855         "                                                                                                                                                                       __global float4* worldVertsA1GPU,\n"
1856         "                                                                                                                                                                       __global float4*  worldNormalsAGPU,\n"
1857         "                                                                                                                                                                       __global float4* worldVertsB1GPU,\n"
1858         "                                                                                                                                                                       int vertexFaceCapacity,\n"
1859         "                                                                                                                                                                       int numConcavePairs\n"
1860         "                                                                                                                                                                       )\n"
1861         "{\n"
1862         "       int i = get_global_id(0);\n"
1863         "       if (i>=numConcavePairs)\n"
1864         "               return;\n"
1865         "       concaveHasSeparatingNormals[i] = 0;\n"
1866         "       int pairIdx = i;\n"
1867         "       int bodyIndexA = concavePairs[i].x;\n"
1868         "       int bodyIndexB = concavePairs[i].y;\n"
1869         "       int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;\n"
1870         "       int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;\n"
1871         "       int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;\n"
1872         "       int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;\n"
1873         "       if (collidables[collidableIndexB].m_shapeType!=SHAPE_CONVEX_HULL&&\n"
1874         "               collidables[collidableIndexB].m_shapeType!=SHAPE_COMPOUND_OF_CONVEX_HULLS)\n"
1875         "       {\n"
1876         "               concavePairs[pairIdx].w = -1;\n"
1877         "               return;\n"
1878         "       }\n"
1879         "       int numFacesA = convexShapes[shapeIndexA].m_numFaces;\n"
1880         "       int numActualConcaveConvexTests = 0;\n"
1881         "       \n"
1882         "       int f = concavePairs[i].z;\n"
1883         "       \n"
1884         "       bool overlap = false;\n"
1885         "       \n"
1886         "       ConvexPolyhedronCL convexPolyhedronA;\n"
1887         "       //add 3 vertices of the triangle\n"
1888         "       convexPolyhedronA.m_numVertices = 3;\n"
1889         "       convexPolyhedronA.m_vertexOffset = 0;\n"
1890         "       float4  localCenter = make_float4(0.f,0.f,0.f,0.f);\n"
1891         "       btGpuFace face = faces[convexShapes[shapeIndexA].m_faceOffset+f];\n"
1892         "       float4 triMinAabb, triMaxAabb;\n"
1893         "       btAabbCL triAabb;\n"
1894         "       triAabb.m_min = make_float4(1e30f,1e30f,1e30f,0.f);\n"
1895         "       triAabb.m_max = make_float4(-1e30f,-1e30f,-1e30f,0.f);\n"
1896         "       \n"
1897         "       float4 verticesA[3];\n"
1898         "       for (int i=0;i<3;i++)\n"
1899         "       {\n"
1900         "               int index = indices[face.m_indexOffset+i];\n"
1901         "               float4 vert = vertices[convexShapes[shapeIndexA].m_vertexOffset+index];\n"
1902         "               verticesA[i] = vert;\n"
1903         "               localCenter += vert;\n"
1904         "                       \n"
1905         "               triAabb.m_min = min(triAabb.m_min,vert);                \n"
1906         "               triAabb.m_max = max(triAabb.m_max,vert);                \n"
1907         "       }\n"
1908         "       overlap = true;\n"
1909         "       overlap = (triAabb.m_min.x > aabbs[bodyIndexB].m_max.x || triAabb.m_max.x < aabbs[bodyIndexB].m_min.x) ? false : overlap;\n"
1910         "       overlap = (triAabb.m_min.z > aabbs[bodyIndexB].m_max.z || triAabb.m_max.z < aabbs[bodyIndexB].m_min.z) ? false : overlap;\n"
1911         "       overlap = (triAabb.m_min.y > aabbs[bodyIndexB].m_max.y || triAabb.m_max.y < aabbs[bodyIndexB].m_min.y) ? false : overlap;\n"
1912         "               \n"
1913         "       if (overlap)\n"
1914         "       {\n"
1915         "               float dmin = FLT_MAX;\n"
1916         "               int hasSeparatingAxis=5;\n"
1917         "               float4 sepAxis=make_float4(1,2,3,4);\n"
1918         "               int localCC=0;\n"
1919         "               numActualConcaveConvexTests++;\n"
1920         "               //a triangle has 3 unique edges\n"
1921         "               convexPolyhedronA.m_numUniqueEdges = 3;\n"
1922         "               convexPolyhedronA.m_uniqueEdgesOffset = 0;\n"
1923         "               float4 uniqueEdgesA[3];\n"
1924         "               \n"
1925         "               uniqueEdgesA[0] = (verticesA[1]-verticesA[0]);\n"
1926         "               uniqueEdgesA[1] = (verticesA[2]-verticesA[1]);\n"
1927         "               uniqueEdgesA[2] = (verticesA[0]-verticesA[2]);\n"
1928         "               convexPolyhedronA.m_faceOffset = 0;\n"
1929         "                                  \n"
1930         "               float4 normal = make_float4(face.m_plane.x,face.m_plane.y,face.m_plane.z,0.f);\n"
1931         "                             \n"
1932         "               btGpuFace facesA[TRIANGLE_NUM_CONVEX_FACES];\n"
1933         "               int indicesA[3+3+2+2+2];\n"
1934         "               int curUsedIndices=0;\n"
1935         "               int fidx=0;\n"
1936         "               //front size of triangle\n"
1937         "               {\n"
1938         "                       facesA[fidx].m_indexOffset=curUsedIndices;\n"
1939         "                       indicesA[0] = 0;\n"
1940         "                       indicesA[1] = 1;\n"
1941         "                       indicesA[2] = 2;\n"
1942         "                       curUsedIndices+=3;\n"
1943         "                       float c = face.m_plane.w;\n"
1944         "                       facesA[fidx].m_plane.x = normal.x;\n"
1945         "                       facesA[fidx].m_plane.y = normal.y;\n"
1946         "                       facesA[fidx].m_plane.z = normal.z;\n"
1947         "                       facesA[fidx].m_plane.w = c;\n"
1948         "                       facesA[fidx].m_numIndices=3;\n"
1949         "               }\n"
1950         "               fidx++;\n"
1951         "               //back size of triangle\n"
1952         "               {\n"
1953         "                       facesA[fidx].m_indexOffset=curUsedIndices;\n"
1954         "                       indicesA[3]=2;\n"
1955         "                       indicesA[4]=1;\n"
1956         "                       indicesA[5]=0;\n"
1957         "                       curUsedIndices+=3;\n"
1958         "                       float c = dot(normal,verticesA[0]);\n"
1959         "                       float c1 = -face.m_plane.w;\n"
1960         "                       facesA[fidx].m_plane.x = -normal.x;\n"
1961         "                       facesA[fidx].m_plane.y = -normal.y;\n"
1962         "                       facesA[fidx].m_plane.z = -normal.z;\n"
1963         "                       facesA[fidx].m_plane.w = c;\n"
1964         "                       facesA[fidx].m_numIndices=3;\n"
1965         "               }\n"
1966         "               fidx++;\n"
1967         "               bool addEdgePlanes = true;\n"
1968         "               if (addEdgePlanes)\n"
1969         "               {\n"
1970         "                       int numVertices=3;\n"
1971         "                       int prevVertex = numVertices-1;\n"
1972         "                       for (int i=0;i<numVertices;i++)\n"
1973         "                       {\n"
1974         "                               float4 v0 = verticesA[i];\n"
1975         "                               float4 v1 = verticesA[prevVertex];\n"
1976         "                                            \n"
1977         "                               float4 edgeNormal = normalize(cross(normal,v1-v0));\n"
1978         "                               float c = -dot(edgeNormal,v0);\n"
1979         "                               facesA[fidx].m_numIndices = 2;\n"
1980         "                               facesA[fidx].m_indexOffset=curUsedIndices;\n"
1981         "                               indicesA[curUsedIndices++]=i;\n"
1982         "                               indicesA[curUsedIndices++]=prevVertex;\n"
1983         "                                            \n"
1984         "                               facesA[fidx].m_plane.x = edgeNormal.x;\n"
1985         "                               facesA[fidx].m_plane.y = edgeNormal.y;\n"
1986         "                               facesA[fidx].m_plane.z = edgeNormal.z;\n"
1987         "                               facesA[fidx].m_plane.w = c;\n"
1988         "                               fidx++;\n"
1989         "                               prevVertex = i;\n"
1990         "                       }\n"
1991         "               }\n"
1992         "               convexPolyhedronA.m_numFaces = TRIANGLE_NUM_CONVEX_FACES;\n"
1993         "               convexPolyhedronA.m_localCenter = localCenter*(1.f/3.f);\n"
1994         "               float4 posA = rigidBodies[bodyIndexA].m_pos;\n"
1995         "               posA.w = 0.f;\n"
1996         "               float4 posB = rigidBodies[bodyIndexB].m_pos;\n"
1997         "               posB.w = 0.f;\n"
1998         "               float4 ornA = rigidBodies[bodyIndexA].m_quat;\n"
1999         "               float4 ornB =rigidBodies[bodyIndexB].m_quat;\n"
2000         "               \n"
2001         "               ///////////////////\n"
2002         "               ///compound shape support\n"
2003         "               if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)\n"
2004         "               {\n"
2005         "                       int compoundChild = concavePairs[pairIdx].w;\n"
2006         "                       int childShapeIndexB = compoundChild;//collidables[collidableIndexB].m_shapeIndex+compoundChild;\n"
2007         "                       int childColIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;\n"
2008         "                       float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;\n"
2009         "                       float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;\n"
2010         "                       float4 newPosB = transform(&childPosB,&posB,&ornB);\n"
2011         "                       float4 newOrnB = qtMul(ornB,childOrnB);\n"
2012         "                       posB = newPosB;\n"
2013         "                       ornB = newOrnB;\n"
2014         "                       shapeIndexB = collidables[childColIndexB].m_shapeIndex;\n"
2015         "               }\n"
2016         "               //////////////////\n"
2017         "               float4 c0local = convexPolyhedronA.m_localCenter;\n"
2018         "               float4 c0 = transform(&c0local, &posA, &ornA);\n"
2019         "               float4 c1local = convexShapes[shapeIndexB].m_localCenter;\n"
2020         "               float4 c1 = transform(&c1local,&posB,&ornB);\n"
2021         "               const float4 DeltaC2 = c0 - c1;\n"
2022         "               bool sepA = findSeparatingAxisLocalA(   &convexPolyhedronA, &convexShapes[shapeIndexB],\n"
2023         "                                                                                               posA,ornA,\n"
2024         "                                                                                               posB,ornB,\n"
2025         "                                                                                               DeltaC2,\n"
2026         "                                                                                               verticesA,uniqueEdgesA,facesA,indicesA,\n"
2027         "                                                                                               vertices,uniqueEdges,faces,indices,\n"
2028         "                                                                                               &sepAxis,&dmin);\n"
2029         "               hasSeparatingAxis = 4;\n"
2030         "               if (!sepA)\n"
2031         "               {\n"
2032         "                       hasSeparatingAxis = 0;\n"
2033         "               } else\n"
2034         "               {\n"
2035         "                       bool sepB = findSeparatingAxisLocalB(   &convexShapes[shapeIndexB],&convexPolyhedronA,\n"
2036         "                                                                                               posB,ornB,\n"
2037         "                                                                                               posA,ornA,\n"
2038         "                                                                                               DeltaC2,\n"
2039         "                                                                                               vertices,uniqueEdges,faces,indices,\n"
2040         "                                                                                               verticesA,uniqueEdgesA,facesA,indicesA,\n"
2041         "                                                                                               &sepAxis,&dmin);\n"
2042         "                       if (!sepB)\n"
2043         "                       {\n"
2044         "                               hasSeparatingAxis = 0;\n"
2045         "                       } else\n"
2046         "                       {\n"
2047         "                               bool sepEE = findSeparatingAxisEdgeEdgeLocalA(  &convexPolyhedronA, &convexShapes[shapeIndexB],\n"
2048         "                                                                                                                       posA,ornA,\n"
2049         "                                                                                                                       posB,ornB,\n"
2050         "                                                                                                                       DeltaC2,\n"
2051         "                                                                                                                       verticesA,uniqueEdgesA,facesA,indicesA,\n"
2052         "                                                                                                                       vertices,uniqueEdges,faces,indices,\n"
2053         "                                                                                                                       &sepAxis,&dmin);\n"
2054         "       \n"
2055         "                               if (!sepEE)\n"
2056         "                               {\n"
2057         "                                       hasSeparatingAxis = 0;\n"
2058         "                               } else\n"
2059         "                               {\n"
2060         "                                       hasSeparatingAxis = 1;\n"
2061         "                               }\n"
2062         "                       }\n"
2063         "               }       \n"
2064         "               \n"
2065         "               if (hasSeparatingAxis)\n"
2066         "               {\n"
2067         "                       sepAxis.w = dmin;\n"
2068         "                       concaveSeparatingNormalsOut[pairIdx]=sepAxis;\n"
2069         "                       concaveHasSeparatingNormals[i]=1;\n"
2070         "                       float minDist = -1e30f;\n"
2071         "                       float maxDist = 0.02f;\n"
2072         "               \n"
2073         "                       findClippingFaces(sepAxis,\n"
2074         "                     &convexPolyhedronA,\n"
2075         "                                        &convexShapes[shapeIndexB],\n"
2076         "                                        posA,ornA,\n"
2077         "                                        posB,ornB,\n"
2078         "                      worldVertsA1GPU,\n"
2079         "                      worldNormalsAGPU,\n"
2080         "                      worldVertsB1GPU,\n"
2081         "                                         vertexFaceCapacity,\n"
2082         "                      minDist, maxDist,\n"
2083         "                      verticesA,\n"
2084         "                      facesA,\n"
2085         "                      indicesA,\n"
2086         "                                         vertices,\n"
2087         "                      faces,\n"
2088         "                      indices,\n"
2089         "                      clippingFacesOut, pairIdx);\n"
2090         "               } else\n"
2091         "               {       \n"
2092         "                       //mark this pair as in-active\n"
2093         "                       concavePairs[pairIdx].w = -1;\n"
2094         "               }\n"
2095         "       }\n"
2096         "       else\n"
2097         "       {       \n"
2098         "               //mark this pair as in-active\n"
2099         "               concavePairs[pairIdx].w = -1;\n"
2100         "       }\n"
2101         "       \n"
2102         "       concavePairs[pairIdx].z = -1;//now z is used for existing/persistent contacts\n"
2103         "}\n";