/
broadphaseKernel.h
405 lines (405 loc) · 11.7 KB
/
broadphaseKernel.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
static const char* broadphaseKernelCL= \
"\n"
"//keep this enum in sync with the CPU version (in AdlCollisionShape.h)\n"
"#define SHAPE_CONVEX_HULL 3\n"
"\n"
"typedef float4 Quaternion;\n"
"\n"
"__inline\n"
"float4 cross3(float4 a, float4 b)\n"
"{\n"
" return cross(a,b);\n"
"}\n"
"\n"
"__inline\n"
"float dot3F4(float4 a, float4 b)\n"
"{\n"
" float4 a1 = (float4)(a.xyz,0.f);\n"
" float4 b1 = (float4)(b.xyz,0.f);\n"
" return dot(a1, b1);\n"
"}\n"
"\n"
"\n"
"__inline\n"
"Quaternion qtMul(Quaternion a, Quaternion b)\n"
"{\n"
" Quaternion ans;\n"
" ans = cross3( a, b );\n"
" ans += a.w*b+b.w*a;\n"
" ans.w = a.w*b.w - dot3F4(a, b);\n"
" return ans;\n"
"}\n"
"\n"
"__inline\n"
"Quaternion qtInvert(Quaternion q)\n"
"{\n"
" return (Quaternion)(-q.xyz, q.w);\n"
"}\n"
"\n"
"__inline\n"
"float4 qtRotate(Quaternion q, float4 vec)\n"
"{\n"
" Quaternion qInv = qtInvert( q );\n"
" float4 vcpy = vec;\n"
" vcpy.w = 0.f;\n"
" float4 out = qtMul(qtMul(q,vcpy),qInv);\n"
" return out;\n"
"}\n"
"\n"
"__inline\n"
"float4 transform(const float4* p, const float4* translation, const Quaternion* orientation)\n"
"{\n"
" return qtRotate( *orientation, *p ) + (*translation);\n"
"}\n"
"\n"
"typedef struct\n"
"{\n"
" float4 m_row[3];\n"
"} Matrix3x3;\n"
"\n"
"typedef unsigned int u32;\n"
"\n"
"\n"
"typedef struct\n"
"{\n"
" float4 m_pos;\n"
" float4 m_quat;\n"
" float4 m_linVel;\n"
" float4 m_angVel;\n"
"\n"
" u32 m_collidableIdx;\n"
" float m_invMass;\n"
" float m_restituitionCoeff;\n"
" float m_frictionCoeff;\n"
"} Body;\n"
"\n"
"typedef struct Collidable\n"
"{\n"
" int m_shapeType;\n"
" int m_shapeIndex;\n"
"} Collidable;\n"
"\n"
"\n"
"typedef struct\n"
"{\n"
" Matrix3x3 m_invInertia;\n"
" Matrix3x3 m_initInvInertia;\n"
"} Shape;\n"
"\n"
"\n"
"__inline\n"
"Matrix3x3 qtGetRotationMatrix(float4 quat)\n"
"{\n"
" float4 quat2 = (float4)(quat.x*quat.x, quat.y*quat.y, quat.z*quat.z, 0.f);\n"
" Matrix3x3 out;\n"
"\n"
" out.m_row[0].x=fabs(1-2*quat2.y-2*quat2.z);\n"
" out.m_row[0].y=fabs(2*quat.x*quat.y-2*quat.w*quat.z);\n"
" out.m_row[0].z=fabs(2*quat.x*quat.z+2*quat.w*quat.y);\n"
" out.m_row[0].w = 0.f;\n"
"\n"
" out.m_row[1].x=fabs(2*quat.x*quat.y+2*quat.w*quat.z);\n"
" out.m_row[1].y=fabs(1-2*quat2.x-2*quat2.z);\n"
" out.m_row[1].z=fabs(2*quat.y*quat.z-2*quat.w*quat.x);\n"
" out.m_row[1].w = 0.f;\n"
"\n"
" out.m_row[2].x=fabs(2*quat.x*quat.z-2*quat.w*quat.y);\n"
" out.m_row[2].y=fabs(2*quat.y*quat.z+2*quat.w*quat.x);\n"
" out.m_row[2].z=fabs(1-2*quat2.x-2*quat2.y);\n"
" out.m_row[2].w = 0.f;\n"
"\n"
" return out;\n"
"}\n"
"\n"
"\n"
"typedef struct \n"
"{\n"
" float fx;\n"
" float fy;\n"
" float fz;\n"
" int uw;\n"
"} btAABBCL;\n"
"\n"
"__inline\n"
"Matrix3x3 mtTranspose(Matrix3x3 m)\n"
"{\n"
" Matrix3x3 out;\n"
" out.m_row[0] = (float4)(m.m_row[0].x, m.m_row[1].x, m.m_row[2].x, 0.f);\n"
" out.m_row[1] = (float4)(m.m_row[0].y, m.m_row[1].y, m.m_row[2].y, 0.f);\n"
" out.m_row[2] = (float4)(m.m_row[0].z, m.m_row[1].z, m.m_row[2].z, 0.f);\n"
" return out;\n"
"}\n"
"\n"
"\n"
"\n"
"__inline\n"
"Matrix3x3 mtMul(Matrix3x3 a, Matrix3x3 b)\n"
"{\n"
" Matrix3x3 transB;\n"
" transB = mtTranspose( b );\n"
" Matrix3x3 ans;\n"
" // why this doesn't run when 0ing in the for{}\n"
" a.m_row[0].w = 0.f;\n"
" a.m_row[1].w = 0.f;\n"
" a.m_row[2].w = 0.f;\n"
" for(int i=0; i<3; i++)\n"
" {\n"
"// a.m_row[i].w = 0.f;\n"
" ans.m_row[i].x = dot3F4(a.m_row[i],transB.m_row[0]);\n"
" ans.m_row[i].y = dot3F4(a.m_row[i],transB.m_row[1]);\n"
" ans.m_row[i].z = dot3F4(a.m_row[i],transB.m_row[2]);\n"
" ans.m_row[i].w = 0.f;\n"
" }\n"
" return ans;\n"
"}\n"
"\n"
"\n"
"//apply gravity\n"
"//update world inverse inertia tensor\n"
"//copy velocity from arrays to bodies\n"
"//copy transforms from buffer to bodies\n"
"\n"
"__kernel void \n"
" setupBodiesKernel( const int startOffset, const int numNodes, __global float4 *g_vertexBuffer,\n"
" __global float4 *linVel,\n"
" __global float4 *pAngVel,\n"
" __global Body* gBodies, __global Shape* bodyInertias\n"
" )\n"
"{\n"
" int nodeID = get_global_id(0);\n"
" \n"
" float timeStep = 0.0166666f;\n"
" float BT_GPU_ANGULAR_MOTION_THRESHOLD = (0.25f * 3.14159254);\n"
"\n"
" if( nodeID < numNodes )\n"
" {\n"
" float inverseMass = gBodies[nodeID].m_invMass;\n"
" if (inverseMass != 0.f)\n"
" {\n"
" float4 position = g_vertexBuffer[nodeID + startOffset/4];\n"
" float4 orientation = g_vertexBuffer[nodeID + startOffset/4+numNodes];\n"
"\n"
" float4 gravityAcceleration = (float4)(0.f,-9.8f,0.f,0.f);\n"
" linVel[nodeID] += gravityAcceleration * timeStep;\n"
" \n"
" gBodies[nodeID].m_pos = position;\n"
" gBodies[nodeID].m_quat = orientation;\n"
"\n"
" gBodies[nodeID].m_linVel = (float4)(linVel[nodeID].xyz,0.f);\n"
" gBodies[nodeID].m_angVel = (float4)(pAngVel[nodeID].xyz,0.f);\n"
"\n"
" Matrix3x3 m = qtGetRotationMatrix( orientation);\n"
" Matrix3x3 mT = mtTranspose( m );\n"
" Matrix3x3 localInvInertia = bodyInertias[nodeID].m_initInvInertia;\n"
"\n"
" Matrix3x3 tmp = mtMul(m, localInvInertia);\n"
"\n"
" Matrix3x3 tmp2 = mtMul(tmp, mT);\n"
" bodyInertias[nodeID].m_invInertia = tmp2;\n"
"\n"
" //shapeInfo.m_invInertia = mtMul( mtMul( m, shapeInfo.m_initInvInertia ), mT );\n"
"\n"
" } else\n"
" {\n"
" gBodies[nodeID].m_linVel = (float4)(0.f,0.f,0.f,0.f);\n"
" gBodies[nodeID].m_angVel = (float4)(0.f,0.f,0.f,0.f);\n"
" }\n"
"\n"
"\n"
" }\n"
"}\n"
"\n"
"\n"
"__kernel void \n"
" copyVelocitiesKernel( const int startOffset, const int numNodes, __global float4 *g_vertexBuffer,\n"
" __global float4 *linVel,\n"
" __global float4 *pAngVel,\n"
" __global Body* gBodies, __global Shape* bodyInertias\n"
" )\n"
"{\n"
" int nodeID = get_global_id(0);\n"
" \n"
" if( nodeID < numNodes )\n"
" {\n"
" float inverseMass = gBodies[nodeID].m_invMass;\n"
" if (inverseMass != 0.f)\n"
" {\n"
" linVel[nodeID] = (float4)(gBodies[nodeID].m_linVel.xyz,0.f);\n"
" pAngVel[nodeID] = (float4)(gBodies[nodeID].m_angVel.xyz,0.f);\n"
" } else\n"
" {\n"
" linVel[nodeID] = (float4)(0,0,0,0);\n"
" pAngVel[nodeID] = (float4)(0,0,0,0);\n"
" }\n"
" }\n"
"}\n"
"\n"
"\n"
"\n"
"__kernel void \n"
" initializeGpuAabbsSimple( const int startOffset, const int numNodes, __global float4 *g_vertexBuffer, __global btAABBCL* pAABB)\n"
"{\n"
" int nodeID = get_global_id(0);\n"
" \n"
" if( nodeID < numNodes )\n"
" {\n"
" float4 position = g_vertexBuffer[nodeID + startOffset/4];\n"
" float4 orientation = g_vertexBuffer[nodeID + startOffset/4+numNodes];\n"
" float4 color = g_vertexBuffer[nodeID + startOffset/4+numNodes+numNodes];\n"
" \n"
" float4 green = (float4)(.4f,1.f,.4f,1.f);\n"
" g_vertexBuffer[nodeID + startOffset/4+numNodes+numNodes] = green;\n"
" \n"
"\n"
" float4 halfExtents = (float4)(1.01f,1.01f,1.01f,0.f);\n"
" //float4 extent=(float4)(1.f,1.f,1.f,0.f);\n"
"\n"
" Matrix3x3 abs_b = qtGetRotationMatrix(orientation);\n"
"\n"
" float4 extent = (float4) (\n"
" dot(abs_b.m_row[0],halfExtents),\n"
" dot(abs_b.m_row[1],halfExtents),\n"
" dot(abs_b.m_row[2],halfExtents),\n"
" 0.f);\n"
" \n"
"\n"
" pAABB[nodeID*2].fx = position.x-extent.x;\n"
" pAABB[nodeID*2].fy = position.y-extent.y;\n"
" pAABB[nodeID*2].fz = position.z-extent.z;\n"
" pAABB[nodeID*2].uw = nodeID;\n"
"\n"
" pAABB[nodeID*2+1].fx = position.x+extent.x;\n"
" pAABB[nodeID*2+1].fy = position.y+extent.y;\n"
" pAABB[nodeID*2+1].fz = position.z+extent.z;\n"
" pAABB[nodeID*2+1].uw = nodeID; \n"
" }\n"
"}\n"
"\n"
"\n"
"\n"
"__kernel void \n"
" initializeGpuAabbsFull( const int startOffset, const int numNodes, __global float4 *g_vertexBuffer, __global Body* gBodies,__global Collidable* collidables, __global btAABBCL* plocalShapeAABB, __global btAABBCL* pAABB)\n"
"{\n"
" int nodeID = get_global_id(0);\n"
" \n"
" if( nodeID < numNodes )\n"
" {\n"
" float4 position = g_vertexBuffer[nodeID + startOffset/4];\n"
" float4 orientation = g_vertexBuffer[nodeID + startOffset/4+numNodes];\n"
" float4 color = g_vertexBuffer[nodeID + startOffset/4+numNodes+numNodes];\n"
" \n"
" float4 green = (float4)(.4f,1.f,.4f,1.f);\n"
" g_vertexBuffer[nodeID + startOffset/4+numNodes+numNodes] = green;\n"
" \n"
" \n"
" \n"
" // int shapeType = gBodies[nodeID].m_shapeType;\n"
" //if (shapeType==SHAPE_CONVEX_HULL)\n"
" {\n"
" \n"
" \n"
" int collidableIndex = gBodies[nodeID].m_collidableIdx;\n"
" int shapeIndex = collidables[collidableIndex].m_shapeIndex;\n"
" \n"
" if (shapeIndex>=0)\n"
" {\n"
" btAABBCL minAabb = plocalShapeAABB[shapeIndex*2];\n"
" btAABBCL maxAabb = plocalShapeAABB[shapeIndex*2+1];\n"
" \n"
" float4 halfExtents = ((float4)(maxAabb.fx - minAabb.fx,maxAabb.fy - minAabb.fy,maxAabb.fz - minAabb.fz,0.f))*0.5f;\n"
" float4 localCenter = ((float4)(maxAabb.fx + minAabb.fx,maxAabb.fy + minAabb.fy,maxAabb.fz + minAabb.fz,0.f))*0.5f;\n"
" \n"
" float4 worldCenter = transform(&localCenter,&position,&orientation);\n"
" \n"
" Matrix3x3 abs_b = qtGetRotationMatrix(orientation);\n"
" float4 extent = (float4) ( dot(abs_b.m_row[0],halfExtents),dot(abs_b.m_row[1],halfExtents),dot(abs_b.m_row[2],halfExtents),0.f);\n"
" \n"
" \n"
" pAABB[nodeID*2].fx = worldCenter.x-extent.x;\n"
" pAABB[nodeID*2].fy = worldCenter.y-extent.y;\n"
" pAABB[nodeID*2].fz = worldCenter.z-extent.z;\n"
" pAABB[nodeID*2].uw = nodeID;\n"
" \n"
" pAABB[nodeID*2+1].fx = worldCenter.x+extent.x;\n"
" pAABB[nodeID*2+1].fy = worldCenter.y+extent.y;\n"
" pAABB[nodeID*2+1].fz = worldCenter.z+extent.z;\n"
" pAABB[nodeID*2+1].uw = gBodies[nodeID].m_invMass==0.f? 0 : 1;\n"
" }\n"
" } \n"
" }\n"
"}\n"
"\n"
"\n"
"__kernel void \n"
" broadphaseColorKernel( const int startOffset, const int numNodes, __global float4 *g_vertexBuffer, __global int2* pOverlappingPairs, const int numOverlap)\n"
"{\n"
" int nodeID = get_global_id(0);\n"
" if( nodeID < numOverlap )\n"
" {\n"
" int2 pair = pOverlappingPairs[nodeID];\n"
" float4 red = (float4)(1.f,0.4f,0.4f,1.f);\n"
" \n"
" g_vertexBuffer[pair.x + startOffset/4+numNodes+numNodes] = red;\n"
" g_vertexBuffer[pair.y + startOffset/4+numNodes+numNodes] = red;\n"
" }\n"
"}\n"
"\n"
"\n"
"\n"
"__kernel void \n"
" broadphaseKernel( const int startOffset, const int numNodes, __global float4 *g_vertexBuffer)\n"
"{\n"
" int nodeID = get_global_id(0);\n"
" \n"
"// float BT_GPU_ANGULAR_MOTION_THRESHOLD = (0.25f * 3.14159254);\n"
" \n"
" if( nodeID < numNodes )\n"
" {\n"
" float4 position = g_vertexBuffer[nodeID + startOffset/4];\n"
" //float4 orientation = g_vertexBuffer[nodeID + startOffset/4+numNodes];\n"
" float4 color = g_vertexBuffer[nodeID + startOffset/4+numNodes+numNodes];\n"
" \n"
" float4 red = (float4)(1.f,0.f,0.f,0.f);\n"
" float4 green = (float4)(0.f,1.f,0.f,0.f);\n"
" float4 blue = (float4)(0.f,0.f,1.f,0.f);\n"
" float overlap=0;\n"
" int equal = 0;\n"
" \n"
" g_vertexBuffer[nodeID + startOffset/4+numNodes+numNodes] = green;\n"
" \n"
" for (int i=0;i<numNodes;i++)\n"
" {\n"
" if (i!=nodeID)\n"
" {\n"
" float4 otherPosition = g_vertexBuffer[i + startOffset/4];\n"
" if ((otherPosition.x == position.x)&&\n"
" (otherPosition.y == position.y)&&\n"
" (otherPosition.z == position.z))\n"
" equal=1;\n"
" \n"
" \n"
" float distsqr = \n"
" ((otherPosition.x - position.x)* (otherPosition.x - position.x))+\n"
" ((otherPosition.y - position.y)* (otherPosition.y - position.y))+\n"
" ((otherPosition.z - position.z)* (otherPosition.z - position.z));\n"
" \n"
" if (distsqr<7.f)\n"
" overlap+=0.25f;\n"
" }\n"
" }\n"
" \n"
" \n"
" if (equal)\n"
" {\n"
" g_vertexBuffer[nodeID + startOffset/4+numNodes+numNodes]=blue;\n"
" } else\n"
" {\n"
" if (overlap>0.f)\n"
" g_vertexBuffer[nodeID + startOffset/4+numNodes+numNodes]=red*overlap;\n"
" else\n"
" g_vertexBuffer[nodeID + startOffset/4+numNodes+numNodes]=green;\n"
" }\n"
" }\n"
"}\n"
;