1 //this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
2 static const char* rayCastKernelCL =
3 "#define SHAPE_CONVEX_HULL 3\n"
4 "#define SHAPE_PLANE 4\n"
5 "#define SHAPE_CONCAVE_TRIMESH 5\n"
6 "#define SHAPE_COMPOUND_OF_CONVEX_HULLS 6\n"
7 "#define SHAPE_SPHERE 7\n"
15 " float m_hitFraction;\n"
16 " int m_hitResult0;\n"
17 " int m_hitResult1;\n"
18 " int m_hitResult2;\n"
19 " float4 m_hitPoint;\n"
20 " float4 m_hitNormal;\n"
28 " unsigned int m_collidableIdx;\n"
30 " float m_restituitionCoeff;\n"
31 " float m_frictionCoeff;\n"
33 "typedef struct Collidable\n"
36 " int m_numChildShapes;\n"
41 " int m_shapeIndex;\n"
45 " float4 m_localCenter;\n"
46 " float4 m_extents;\n"
50 " int m_faceOffset;\n"
52 " int m_numVertices;\n"
53 " int m_vertexOffset;\n"
54 " int m_uniqueEdgesOffset;\n"
55 " int m_numUniqueEdges;\n"
57 "} ConvexPolyhedronCL;\n"
61 " int m_indexOffset;\n"
62 " int m_numIndices;\n"
64 "///////////////////////////////////////\n"
66 "///////////////////////////////////////\n"
67 "typedef float4 Quaternion;\n"
69 " Quaternion qtMul(Quaternion a, Quaternion b);\n"
71 " Quaternion qtNormalize(Quaternion in);\n"
73 " Quaternion qtInvert(Quaternion q);\n"
75 " float dot3F4(float4 a, float4 b)\n"
77 " float4 a1 = (float4)(a.xyz,0.f);\n"
78 " float4 b1 = (float4)(b.xyz,0.f);\n"
79 " return dot(a1, b1);\n"
82 " Quaternion qtMul(Quaternion a, Quaternion b)\n"
85 " ans = cross( a, b );\n"
86 " ans += a.w*b+b.w*a;\n"
87 " // ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);\n"
88 " ans.w = a.w*b.w - dot3F4(a, b);\n"
92 " Quaternion qtNormalize(Quaternion in)\n"
94 " return fast_normalize(in);\n"
95 " // in /= length( in );\n"
99 " float4 qtRotate(Quaternion q, float4 vec)\n"
101 " Quaternion qInv = qtInvert( q );\n"
102 " float4 vcpy = vec;\n"
104 " float4 out = qtMul(q,vcpy);\n"
105 " out = qtMul(out,qInv);\n"
109 " Quaternion qtInvert(Quaternion q)\n"
111 " return (Quaternion)(-q.xyz, q.w);\n"
114 " float4 qtInvRotate(const Quaternion q, float4 vec)\n"
116 " return qtRotate( qtInvert( q ), vec );\n"
118 "void trInverse(float4 translationIn, Quaternion orientationIn,\n"
119 " float4* translationOut, Quaternion* orientationOut)\n"
121 " *orientationOut = qtInvert(orientationIn);\n"
122 " *translationOut = qtRotate(*orientationOut, -translationIn);\n"
124 "bool rayConvex(float4 rayFromLocal, float4 rayToLocal, int numFaces, int faceOffset,\n"
125 " __global const b3GpuFace* faces, float* hitFraction, float4* hitNormal)\n"
127 " rayFromLocal.w = 0.f;\n"
128 " rayToLocal.w = 0.f;\n"
129 " bool result = true;\n"
130 " float exitFraction = hitFraction[0];\n"
131 " float enterFraction = -0.3f;\n"
132 " float4 curHitNormal = (float4)(0,0,0,0);\n"
133 " for (int i=0;i<numFaces && result;i++)\n"
135 " b3GpuFace face = faces[faceOffset+i];\n"
136 " float fromPlaneDist = dot(rayFromLocal,face.m_plane)+face.m_plane.w;\n"
137 " float toPlaneDist = dot(rayToLocal,face.m_plane)+face.m_plane.w;\n"
138 " if (fromPlaneDist<0.f)\n"
140 " if (toPlaneDist >= 0.f)\n"
142 " float fraction = fromPlaneDist / (fromPlaneDist-toPlaneDist);\n"
143 " if (exitFraction>fraction)\n"
145 " exitFraction = fraction;\n"
150 " if (toPlaneDist<0.f)\n"
152 " float fraction = fromPlaneDist / (fromPlaneDist-toPlaneDist);\n"
153 " if (enterFraction <= fraction)\n"
155 " enterFraction = fraction;\n"
156 " curHitNormal = face.m_plane;\n"
157 " curHitNormal.w = 0.f;\n"
164 " if (exitFraction <= enterFraction)\n"
167 " if (enterFraction < 0.f)\n"
173 " hitFraction[0] = enterFraction;\n"
174 " hitNormal[0] = curHitNormal;\n"
178 "bool sphere_intersect(float4 spherePos, float radius, float4 rayFrom, float4 rayTo, float* hitFraction)\n"
180 " float4 rs = rayFrom - spherePos;\n"
182 " float4 rayDir = rayTo-rayFrom;\n"
184 " float A = dot(rayDir,rayDir);\n"
185 " float B = dot(rs, rayDir);\n"
186 " float C = dot(rs, rs) - (radius * radius);\n"
187 " float D = B * B - A*C;\n"
190 " float t = (-B - sqrt(D))/A;\n"
191 " if ( (t >= 0.0f) && (t < (*hitFraction)) )\n"
193 " *hitFraction = t;\n"
199 "float4 setInterpolate3(float4 from, float4 to, float t)\n"
201 " float s = 1.0f - t;\n"
203 " result = s * from + t * to;\n"
204 " result.w = 0.f; \n"
207 "__kernel void rayCastKernel( \n"
209 " const __global b3RayInfo* rays, \n"
210 " __global b3RayHit* hitResults, \n"
211 " const int numBodies, \n"
212 " __global Body* bodies,\n"
213 " __global Collidable* collidables,\n"
214 " __global const b3GpuFace* faces,\n"
215 " __global const ConvexPolyhedronCL* convexShapes )\n"
217 " int i = get_global_id(0);\n"
220 " hitResults[i].m_hitFraction = 1.f;\n"
221 " float4 rayFrom = rays[i].m_from;\n"
222 " float4 rayTo = rays[i].m_to;\n"
223 " float hitFraction = 1.f;\n"
224 " float4 hitPoint;\n"
225 " float4 hitNormal;\n"
226 " int hitBodyIndex= -1;\n"
227 " int cachedCollidableIndex = -1;\n"
228 " Collidable cachedCollidable;\n"
229 " for (int b=0;b<numBodies;b++)\n"
231 " if (hitResults[i].m_hitResult2==b)\n"
233 " Body body = bodies[b];\n"
234 " float4 pos = body.m_pos;\n"
235 " float4 orn = body.m_quat;\n"
236 " if (cachedCollidableIndex != body.m_collidableIdx)\n"
238 " cachedCollidableIndex = body.m_collidableIdx;\n"
239 " cachedCollidable = collidables[cachedCollidableIndex];\n"
241 " if (cachedCollidable.m_shapeType == SHAPE_CONVEX_HULL)\n"
243 " float4 invPos = (float4)(0,0,0,0);\n"
244 " float4 invOrn = (float4)(0,0,0,0);\n"
245 " float4 rayFromLocal = (float4)(0,0,0,0);\n"
246 " float4 rayToLocal = (float4)(0,0,0,0);\n"
247 " invOrn = qtInvert(orn);\n"
248 " invPos = qtRotate(invOrn, -pos);\n"
249 " rayFromLocal = qtRotate( invOrn, rayFrom ) + invPos;\n"
250 " rayToLocal = qtRotate( invOrn, rayTo) + invPos;\n"
251 " rayFromLocal.w = 0.f;\n"
252 " rayToLocal.w = 0.f;\n"
253 " int numFaces = convexShapes[cachedCollidable.m_shapeIndex].m_numFaces;\n"
254 " int faceOffset = convexShapes[cachedCollidable.m_shapeIndex].m_faceOffset;\n"
257 " if (rayConvex(rayFromLocal, rayToLocal, numFaces, faceOffset,faces, &hitFraction, &hitNormal))\n"
259 " hitBodyIndex = b;\n"
264 " if (cachedCollidable.m_shapeType == SHAPE_SPHERE)\n"
266 " float radius = cachedCollidable.m_radius;\n"
268 " if (sphere_intersect(pos, radius, rayFrom, rayTo, &hitFraction))\n"
270 " hitBodyIndex = b;\n"
271 " hitNormal = (float4) (hitPoint-bodies[b].m_pos);\n"
275 " if (hitBodyIndex>=0)\n"
277 " hitPoint = setInterpolate3(rayFrom, rayTo,hitFraction);\n"
278 " hitResults[i].m_hitFraction = hitFraction;\n"
279 " hitResults[i].m_hitPoint = hitPoint;\n"
280 " hitResults[i].m_hitNormal = normalize(hitNormal);\n"
281 " hitResults[i].m_hitResult0 = hitBodyIndex;\n"
284 "__kernel void findRayRigidPairIndexRanges(__global int2* rayRigidPairs, \n"
285 " __global int* out_firstRayRigidPairIndexPerRay,\n"
286 " __global int* out_numRayRigidPairsPerRay,\n"
287 " int numRayRigidPairs)\n"
289 " int rayRigidPairIndex = get_global_id(0);\n"
290 " if (rayRigidPairIndex >= numRayRigidPairs) return;\n"
292 " int rayIndex = rayRigidPairs[rayRigidPairIndex].x;\n"
294 " atomic_min(&out_firstRayRigidPairIndexPerRay[rayIndex], rayRigidPairIndex);\n"
295 " atomic_inc(&out_numRayRigidPairsPerRay[rayIndex]);\n"
297 "__kernel void rayCastPairsKernel(const __global b3RayInfo* rays, \n"
298 " __global b3RayHit* hitResults, \n"
299 " __global int* firstRayRigidPairIndexPerRay,\n"
300 " __global int* numRayRigidPairsPerRay,\n"
302 " __global Body* bodies,\n"
303 " __global Collidable* collidables,\n"
304 " __global const b3GpuFace* faces,\n"
305 " __global const ConvexPolyhedronCL* convexShapes,\n"
307 " __global int2* rayRigidPairs,\n"
310 " int i = get_global_id(0);\n"
311 " if (i >= numRays) return;\n"
313 " float4 rayFrom = rays[i].m_from;\n"
314 " float4 rayTo = rays[i].m_to;\n"
316 " hitResults[i].m_hitFraction = 1.f;\n"
318 " float hitFraction = 1.f;\n"
319 " float4 hitPoint;\n"
320 " float4 hitNormal;\n"
321 " int hitBodyIndex = -1;\n"
324 " for(int pair = 0; pair < numRayRigidPairsPerRay[i]; ++pair)\n"
326 " int rayRigidPairIndex = pair + firstRayRigidPairIndexPerRay[i];\n"
327 " int b = rayRigidPairs[rayRigidPairIndex].y;\n"
329 " if (hitResults[i].m_hitResult2 == b) continue;\n"
331 " Body body = bodies[b];\n"
332 " Collidable rigidCollidable = collidables[body.m_collidableIdx];\n"
334 " float4 pos = body.m_pos;\n"
335 " float4 orn = body.m_quat;\n"
337 " if (rigidCollidable.m_shapeType == SHAPE_CONVEX_HULL)\n"
339 " float4 invPos = (float4)(0,0,0,0);\n"
340 " float4 invOrn = (float4)(0,0,0,0);\n"
341 " float4 rayFromLocal = (float4)(0,0,0,0);\n"
342 " float4 rayToLocal = (float4)(0,0,0,0);\n"
343 " invOrn = qtInvert(orn);\n"
344 " invPos = qtRotate(invOrn, -pos);\n"
345 " rayFromLocal = qtRotate( invOrn, rayFrom ) + invPos;\n"
346 " rayToLocal = qtRotate( invOrn, rayTo) + invPos;\n"
347 " rayFromLocal.w = 0.f;\n"
348 " rayToLocal.w = 0.f;\n"
349 " int numFaces = convexShapes[rigidCollidable.m_shapeIndex].m_numFaces;\n"
350 " int faceOffset = convexShapes[rigidCollidable.m_shapeIndex].m_faceOffset;\n"
352 " if (numFaces && rayConvex(rayFromLocal, rayToLocal, numFaces, faceOffset,faces, &hitFraction, &hitNormal))\n"
354 " hitBodyIndex = b;\n"
355 " hitPoint = setInterpolate3(rayFrom, rayTo, hitFraction);\n"
359 " if (rigidCollidable.m_shapeType == SHAPE_SPHERE)\n"
361 " float radius = rigidCollidable.m_radius;\n"
363 " if (sphere_intersect(pos, radius, rayFrom, rayTo, &hitFraction))\n"
365 " hitBodyIndex = b;\n"
366 " hitPoint = setInterpolate3(rayFrom, rayTo, hitFraction);\n"
367 " hitNormal = (float4) (hitPoint - bodies[b].m_pos);\n"
372 " if (hitBodyIndex >= 0)\n"
374 " hitResults[i].m_hitFraction = hitFraction;\n"
375 " hitResults[i].m_hitPoint = hitPoint;\n"
376 " hitResults[i].m_hitNormal = normalize(hitNormal);\n"
377 " hitResults[i].m_hitResult0 = hitBodyIndex;\n"