1 //this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
2 static const char* updateAabbsKernelCL =
3 "#ifndef B3_UPDATE_AABBS_H\n"
4 "#define B3_UPDATE_AABBS_H\n"
7 "#ifndef B3_FLOAT4_H\n"
8 "#define B3_FLOAT4_H\n"
9 "#ifndef B3_PLATFORM_DEFINITIONS_H\n"
10 "#define B3_PLATFORM_DEFINITIONS_H\n"
15 "#ifdef __cplusplus\n"
17 "//keep B3_LARGE_FLOAT*B3_LARGE_FLOAT < FLT_MAX\n"
18 "#define B3_LARGE_FLOAT 1e18f\n"
19 "#define B3_INFINITY 1e18f\n"
20 "#define b3Assert(a)\n"
21 "#define b3ConstArray(a) __global const a*\n"
22 "#define b3AtomicInc atomic_inc\n"
23 "#define b3AtomicAdd atomic_add\n"
24 "#define b3Fabs fabs\n"
25 "#define b3Sqrt native_sqrt\n"
26 "#define b3Sin native_sin\n"
27 "#define b3Cos native_cos\n"
31 "#ifdef __cplusplus\n"
33 " typedef float4 b3Float4;\n"
34 " #define b3Float4ConstArg const b3Float4\n"
35 " #define b3MakeFloat4 (float4)\n"
36 " float b3Dot3F4(b3Float4ConstArg v0,b3Float4ConstArg v1)\n"
38 " float4 a1 = b3MakeFloat4(v0.xyz,0.f);\n"
39 " float4 b1 = b3MakeFloat4(v1.xyz,0.f);\n"
40 " return dot(a1, b1);\n"
42 " b3Float4 b3Cross3(b3Float4ConstArg v0,b3Float4ConstArg v1)\n"
44 " float4 a1 = b3MakeFloat4(v0.xyz,0.f);\n"
45 " float4 b1 = b3MakeFloat4(v1.xyz,0.f);\n"
46 " return cross(a1, b1);\n"
48 " #define b3MinFloat4 min\n"
49 " #define b3MaxFloat4 max\n"
50 " #define b3Normalized(a) normalize(a)\n"
53 "inline bool b3IsAlmostZero(b3Float4ConstArg v)\n"
55 " if(b3Fabs(v.x)>1e-6 || b3Fabs(v.y)>1e-6 || b3Fabs(v.z)>1e-6) \n"
59 "inline int b3MaxDot( b3Float4ConstArg vec, __global const b3Float4* vecArray, int vecLen, float* dotOut )\n"
61 " float maxDot = -B3_INFINITY;\n"
63 " int ptIndex = -1;\n"
64 " for( i = 0; i < vecLen; i++ )\n"
66 " float dot = b3Dot3F4(vecArray[i],vec);\n"
68 " if( dot > maxDot )\n"
74 " b3Assert(ptIndex>=0);\n"
79 " *dotOut = maxDot;\n"
82 "#endif //B3_FLOAT4_H\n"
83 "#ifndef B3_MAT3x3_H\n"
84 "#define B3_MAT3x3_H\n"
87 "#ifndef B3_PLATFORM_DEFINITIONS_H\n"
88 "#ifdef __cplusplus\n"
92 "#ifndef B3_FLOAT4_H\n"
93 "#ifdef __cplusplus\n"
96 "#endif //B3_FLOAT4_H\n"
97 "#ifdef __cplusplus\n"
99 " typedef float4 b3Quat;\n"
100 " #define b3QuatConstArg const b3Quat\n"
103 "inline float4 b3FastNormalize4(float4 v)\n"
105 " v = (float4)(v.xyz,0.f);\n"
106 " return fast_normalize(v);\n"
109 "inline b3Quat b3QuatMul(b3Quat a, b3Quat b);\n"
110 "inline b3Quat b3QuatNormalized(b3QuatConstArg in);\n"
111 "inline b3Quat b3QuatRotate(b3QuatConstArg q, b3QuatConstArg vec);\n"
112 "inline b3Quat b3QuatInvert(b3QuatConstArg q);\n"
113 "inline b3Quat b3QuatInverse(b3QuatConstArg q);\n"
114 "inline b3Quat b3QuatMul(b3QuatConstArg a, b3QuatConstArg b)\n"
117 " ans = b3Cross3( a, b );\n"
118 " ans += a.w*b+b.w*a;\n"
119 "// ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);\n"
120 " ans.w = a.w*b.w - b3Dot3F4(a, b);\n"
123 "inline b3Quat b3QuatNormalized(b3QuatConstArg in)\n"
127 " //return b3FastNormalize4(in);\n"
128 " float len = native_sqrt(dot(q, q));\n"
135 " q.x = q.y = q.z = 0.f;\n"
140 "inline float4 b3QuatRotate(b3QuatConstArg q, b3QuatConstArg vec)\n"
142 " b3Quat qInv = b3QuatInvert( q );\n"
143 " float4 vcpy = vec;\n"
145 " float4 out = b3QuatMul(b3QuatMul(q,vcpy),qInv);\n"
148 "inline b3Quat b3QuatInverse(b3QuatConstArg q)\n"
150 " return (b3Quat)(-q.xyz, q.w);\n"
152 "inline b3Quat b3QuatInvert(b3QuatConstArg q)\n"
154 " return (b3Quat)(-q.xyz, q.w);\n"
156 "inline float4 b3QuatInvRotate(b3QuatConstArg q, b3QuatConstArg vec)\n"
158 " return b3QuatRotate( b3QuatInvert( q ), vec );\n"
160 "inline b3Float4 b3TransformPoint(b3Float4ConstArg point, b3Float4ConstArg translation, b3QuatConstArg orientation)\n"
162 " return b3QuatRotate( orientation, point ) + (translation);\n"
166 "#endif //B3_QUAT_H\n"
167 "#ifdef __cplusplus\n"
171 " b3Float4 m_row[3];\n"
173 "#define b3Mat3x3ConstArg const b3Mat3x3\n"
174 "#define b3GetRow(m,row) (m.m_row[row])\n"
175 "inline b3Mat3x3 b3QuatGetRotationMatrix(b3Quat quat)\n"
177 " b3Float4 quat2 = (b3Float4)(quat.x*quat.x, quat.y*quat.y, quat.z*quat.z, 0.f);\n"
179 " out.m_row[0].x=1-2*quat2.y-2*quat2.z;\n"
180 " out.m_row[0].y=2*quat.x*quat.y-2*quat.w*quat.z;\n"
181 " out.m_row[0].z=2*quat.x*quat.z+2*quat.w*quat.y;\n"
182 " out.m_row[0].w = 0.f;\n"
183 " out.m_row[1].x=2*quat.x*quat.y+2*quat.w*quat.z;\n"
184 " out.m_row[1].y=1-2*quat2.x-2*quat2.z;\n"
185 " out.m_row[1].z=2*quat.y*quat.z-2*quat.w*quat.x;\n"
186 " out.m_row[1].w = 0.f;\n"
187 " out.m_row[2].x=2*quat.x*quat.z-2*quat.w*quat.y;\n"
188 " out.m_row[2].y=2*quat.y*quat.z+2*quat.w*quat.x;\n"
189 " out.m_row[2].z=1-2*quat2.x-2*quat2.y;\n"
190 " out.m_row[2].w = 0.f;\n"
193 "inline b3Mat3x3 b3AbsoluteMat3x3(b3Mat3x3ConstArg matIn)\n"
196 " out.m_row[0] = fabs(matIn.m_row[0]);\n"
197 " out.m_row[1] = fabs(matIn.m_row[1]);\n"
198 " out.m_row[2] = fabs(matIn.m_row[2]);\n"
202 "b3Mat3x3 mtZero();\n"
204 "b3Mat3x3 mtIdentity();\n"
206 "b3Mat3x3 mtTranspose(b3Mat3x3 m);\n"
208 "b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b);\n"
210 "b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b);\n"
212 "b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b);\n"
214 "b3Mat3x3 mtZero()\n"
217 " m.m_row[0] = (b3Float4)(0.f);\n"
218 " m.m_row[1] = (b3Float4)(0.f);\n"
219 " m.m_row[2] = (b3Float4)(0.f);\n"
223 "b3Mat3x3 mtIdentity()\n"
226 " m.m_row[0] = (b3Float4)(1,0,0,0);\n"
227 " m.m_row[1] = (b3Float4)(0,1,0,0);\n"
228 " m.m_row[2] = (b3Float4)(0,0,1,0);\n"
232 "b3Mat3x3 mtTranspose(b3Mat3x3 m)\n"
235 " out.m_row[0] = (b3Float4)(m.m_row[0].x, m.m_row[1].x, m.m_row[2].x, 0.f);\n"
236 " out.m_row[1] = (b3Float4)(m.m_row[0].y, m.m_row[1].y, m.m_row[2].y, 0.f);\n"
237 " out.m_row[2] = (b3Float4)(m.m_row[0].z, m.m_row[1].z, m.m_row[2].z, 0.f);\n"
241 "b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b)\n"
243 " b3Mat3x3 transB;\n"
244 " transB = mtTranspose( b );\n"
246 " // why this doesn't run when 0ing in the for{}\n"
247 " a.m_row[0].w = 0.f;\n"
248 " a.m_row[1].w = 0.f;\n"
249 " a.m_row[2].w = 0.f;\n"
250 " for(int i=0; i<3; i++)\n"
252 "// a.m_row[i].w = 0.f;\n"
253 " ans.m_row[i].x = b3Dot3F4(a.m_row[i],transB.m_row[0]);\n"
254 " ans.m_row[i].y = b3Dot3F4(a.m_row[i],transB.m_row[1]);\n"
255 " ans.m_row[i].z = b3Dot3F4(a.m_row[i],transB.m_row[2]);\n"
256 " ans.m_row[i].w = 0.f;\n"
261 "b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b)\n"
264 " ans.x = b3Dot3F4( a.m_row[0], b );\n"
265 " ans.y = b3Dot3F4( a.m_row[1], b );\n"
266 " ans.z = b3Dot3F4( a.m_row[2], b );\n"
271 "b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b)\n"
273 " b3Float4 colx = b3MakeFloat4(b.m_row[0].x, b.m_row[1].x, b.m_row[2].x, 0);\n"
274 " b3Float4 coly = b3MakeFloat4(b.m_row[0].y, b.m_row[1].y, b.m_row[2].y, 0);\n"
275 " b3Float4 colz = b3MakeFloat4(b.m_row[0].z, b.m_row[1].z, b.m_row[2].z, 0);\n"
277 " ans.x = b3Dot3F4( a, colx );\n"
278 " ans.y = b3Dot3F4( a, coly );\n"
279 " ans.z = b3Dot3F4( a, colz );\n"
283 "#endif //B3_MAT3x3_H\n"
284 "typedef struct b3Aabb b3Aabb_t;\n"
290 " b3Float4 m_minVec;\n"
291 " int m_minIndices[4];\n"
296 " b3Float4 m_maxVec;\n"
297 " int m_signedMaxIndices[4];\n"
300 "inline void b3TransformAabb2(b3Float4ConstArg localAabbMin,b3Float4ConstArg localAabbMax, float margin,\n"
301 " b3Float4ConstArg pos,\n"
302 " b3QuatConstArg orn,\n"
303 " b3Float4* aabbMinOut,b3Float4* aabbMaxOut)\n"
305 " b3Float4 localHalfExtents = 0.5f*(localAabbMax-localAabbMin);\n"
306 " localHalfExtents+=b3MakeFloat4(margin,margin,margin,0.f);\n"
307 " b3Float4 localCenter = 0.5f*(localAabbMax+localAabbMin);\n"
309 " m = b3QuatGetRotationMatrix(orn);\n"
310 " b3Mat3x3 abs_b = b3AbsoluteMat3x3(m);\n"
311 " b3Float4 center = b3TransformPoint(localCenter,pos,orn);\n"
313 " b3Float4 extent = b3MakeFloat4(b3Dot3F4(localHalfExtents,b3GetRow(abs_b,0)),\n"
314 " b3Dot3F4(localHalfExtents,b3GetRow(abs_b,1)),\n"
315 " b3Dot3F4(localHalfExtents,b3GetRow(abs_b,2)),\n"
317 " *aabbMinOut = center-extent;\n"
318 " *aabbMaxOut = center+extent;\n"
320 "/// conservative test for overlap between two aabbs\n"
321 "inline bool b3TestAabbAgainstAabb(b3Float4ConstArg aabbMin1,b3Float4ConstArg aabbMax1,\n"
322 " b3Float4ConstArg aabbMin2, b3Float4ConstArg aabbMax2)\n"
324 " bool overlap = true;\n"
325 " overlap = (aabbMin1.x > aabbMax2.x || aabbMax1.x < aabbMin2.x) ? false : overlap;\n"
326 " overlap = (aabbMin1.z > aabbMax2.z || aabbMax1.z < aabbMin2.z) ? false : overlap;\n"
327 " overlap = (aabbMin1.y > aabbMax2.y || aabbMax1.y < aabbMin2.y) ? false : overlap;\n"
330 "#endif //B3_AABB_H\n"
331 "#ifndef B3_COLLIDABLE_H\n"
332 "#define B3_COLLIDABLE_H\n"
333 "#ifndef B3_FLOAT4_H\n"
334 "#ifdef __cplusplus\n"
337 "#endif //B3_FLOAT4_H\n"
338 "#ifndef B3_QUAT_H\n"
339 "#ifdef __cplusplus\n"
342 "#endif //B3_QUAT_H\n"
343 "enum b3ShapeTypes\n"
345 " SHAPE_HEIGHT_FIELD=1,\n"
346 " SHAPE_CONVEX_HULL=3,\n"
348 " SHAPE_CONCAVE_TRIMESH=5,\n"
349 " SHAPE_COMPOUND_OF_CONVEX_HULLS=6,\n"
351 " MAX_NUM_SHAPE_TYPES,\n"
353 "typedef struct b3Collidable b3Collidable_t;\n"
354 "struct b3Collidable\n"
357 " int m_numChildShapes;\n"
363 " int m_compoundBvhIndex;\n"
365 " int m_shapeType;\n"
368 " int m_shapeIndex;\n"
372 "typedef struct b3GpuChildShape b3GpuChildShape_t;\n"
373 "struct b3GpuChildShape\n"
375 " b3Float4 m_childPosition;\n"
376 " b3Quat m_childOrientation;\n"
379 " int m_shapeIndex;//used for SHAPE_COMPOUND_OF_CONVEX_HULLS\n"
380 " int m_capsuleAxis;\n"
384 " float m_radius;//used for childshape of SHAPE_COMPOUND_OF_SPHERES or SHAPE_COMPOUND_OF_CAPSULES\n"
385 " int m_numChildShapes;//used for compound shape\n"
389 " float m_height;//used for childshape of SHAPE_COMPOUND_OF_CAPSULES\n"
390 " int m_collidableShapeIndex;\n"
392 " int m_shapeType;\n"
394 "struct b3CompoundOverlappingPair\n"
396 " int m_bodyIndexA;\n"
397 " int m_bodyIndexB;\n"
398 "// int m_pairType;\n"
399 " int m_childShapeIndexA;\n"
400 " int m_childShapeIndexB;\n"
402 "#endif //B3_COLLIDABLE_H\n"
403 "#ifndef B3_RIGIDBODY_DATA_H\n"
404 "#define B3_RIGIDBODY_DATA_H\n"
405 "#ifndef B3_FLOAT4_H\n"
406 "#ifdef __cplusplus\n"
409 "#endif //B3_FLOAT4_H\n"
410 "#ifndef B3_QUAT_H\n"
411 "#ifdef __cplusplus\n"
414 "#endif //B3_QUAT_H\n"
415 "#ifndef B3_MAT3x3_H\n"
416 "#ifdef __cplusplus\n"
419 "#endif //B3_MAT3x3_H\n"
420 "typedef struct b3RigidBodyData b3RigidBodyData_t;\n"
421 "struct b3RigidBodyData\n"
425 " b3Float4 m_linVel;\n"
426 " b3Float4 m_angVel;\n"
427 " int m_collidableIdx;\n"
428 " float m_invMass;\n"
429 " float m_restituitionCoeff;\n"
430 " float m_frictionCoeff;\n"
432 "typedef struct b3InertiaData b3InertiaData_t;\n"
433 "struct b3InertiaData\n"
435 " b3Mat3x3 m_invInertiaWorld;\n"
436 " b3Mat3x3 m_initInvInertia;\n"
438 "#endif //B3_RIGIDBODY_DATA_H\n"
440 "void b3ComputeWorldAabb( int bodyId, __global const b3RigidBodyData_t* bodies, __global const b3Collidable_t* collidables, __global const b3Aabb_t* localShapeAABB, __global b3Aabb_t* worldAabbs)\n"
442 " __global const b3RigidBodyData_t* body = &bodies[bodyId];\n"
443 " b3Float4 position = body->m_pos;\n"
444 " b3Quat orientation = body->m_quat;\n"
446 " int collidableIndex = body->m_collidableIdx;\n"
447 " int shapeIndex = collidables[collidableIndex].m_shapeIndex;\n"
449 " if (shapeIndex>=0)\n"
452 " b3Aabb_t localAabb = localShapeAABB[collidableIndex];\n"
453 " b3Aabb_t worldAabb;\n"
455 " b3Float4 aabbAMinOut,aabbAMaxOut; \n"
456 " float margin = 0.f;\n"
457 " b3TransformAabb2(localAabb.m_minVec,localAabb.m_maxVec,margin,position,orientation,&aabbAMinOut,&aabbAMaxOut);\n"
459 " worldAabb.m_minVec =aabbAMinOut;\n"
460 " worldAabb.m_minIndices[3] = bodyId;\n"
461 " worldAabb.m_maxVec = aabbAMaxOut;\n"
462 " worldAabb.m_signedMaxIndices[3] = body[bodyId].m_invMass==0.f? 0 : 1;\n"
463 " worldAabbs[bodyId] = worldAabb;\n"
466 "#endif //B3_UPDATE_AABBS_H\n"
467 "__kernel void initializeGpuAabbsFull( const int numNodes, __global b3RigidBodyData_t* gBodies,__global b3Collidable_t* collidables, __global b3Aabb_t* plocalShapeAABB, __global b3Aabb_t* pAABB)\n"
469 " int nodeID = get_global_id(0);\n"
470 " if( nodeID < numNodes )\n"
472 " b3ComputeWorldAabb(nodeID, gBodies, collidables, plocalShapeAABB,pAABB);\n"
475 "__kernel void clearOverlappingPairsKernel( __global int4* pairs, int numPairs)\n"
477 " int pairId = get_global_id(0);\n"
478 " if( pairId< numPairs )\n"
480 " pairs[pairId].z = 0xffffffff;\n"