2 Copyright (c) 2012 Advanced Micro Devices, Inc.
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose,
7 including commercial applications, and to alter it and redistribute it freely,
8 subject to the following restrictions:
10 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
14 //Originally written by Takahiro Harada
17 #include "Bullet3Collision/NarrowPhaseCollision/shared/b3Contact4Data.h"
19 #pragma OPENCL EXTENSION cl_amd_printf : enable
20 #pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable
21 #pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
22 #pragma OPENCL EXTENSION cl_khr_local_int32_extended_atomics : enable
23 #pragma OPENCL EXTENSION cl_khr_global_int32_extended_atomics : enable
26 #ifdef cl_ext_atomic_counters_32
27 #pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable
29 #define counter32_t volatile global int*
32 typedef unsigned int u32;
33 typedef unsigned short u16;
34 typedef unsigned char u8;
36 #define GET_GROUP_IDX get_group_id(0)
37 #define GET_LOCAL_IDX get_local_id(0)
38 #define GET_GLOBAL_IDX get_global_id(0)
39 #define GET_GROUP_SIZE get_local_size(0)
40 #define GET_NUM_GROUPS get_num_groups(0)
41 #define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)
42 #define GROUP_MEM_FENCE mem_fence(CLK_LOCAL_MEM_FENCE)
43 #define AtomInc(x) atom_inc(&(x))
44 #define AtomInc1(x, out) out = atom_inc(&(x))
45 #define AppendInc(x, out) out = atomic_inc(x)
46 #define AtomAdd(x, value) atom_add(&(x), value)
47 #define AtomCmpxhg(x, cmp, value) atom_cmpxchg( &(x), cmp, value )
48 #define AtomXhg(x, value) atom_xchg ( &(x), value )
51 #define SELECT_UINT4( b, a, condition ) select( b,a,condition )
53 #define make_float4 (float4)
54 #define make_float2 (float2)
55 #define make_uint4 (uint4)
56 #define make_int4 (int4)
57 #define make_uint2 (uint2)
58 #define make_int2 (int2)
65 ///////////////////////////////////////
67 ///////////////////////////////////////
69 float fastDiv(float numerator, float denominator)
71 return native_divide(numerator, denominator);
72 // return numerator/denominator;
76 float4 fastDiv4(float4 numerator, float4 denominator)
78 return native_divide(numerator, denominator);
82 float fastSqrtf(float f2)
84 return native_sqrt(f2);
89 float fastRSqrt(float f2)
91 return native_rsqrt(f2);
95 float fastLength4(float4 v)
97 return fast_length(v);
101 float4 fastNormalize4(float4 v)
103 return fast_normalize(v);
111 return native_sqrt(a);
115 float4 cross3(float4 a, float4 b)
121 float dot3F4(float4 a, float4 b)
123 float4 a1 = make_float4(a.xyz,0.f);
124 float4 b1 = make_float4(b.xyz,0.f);
129 float length3(const float4 a)
131 return sqrtf(dot3F4(a,a));
135 float dot4(const float4 a, const float4 b)
142 float dot3w1(const float4 point, const float4 eqn)
144 return dot3F4(point,eqn) + eqn.w;
148 float4 normalize3(const float4 a)
150 float4 n = make_float4(a.x, a.y, a.z, 0.f);
151 return fastNormalize4( n );
152 // float length = sqrtf(dot3F4(a, a));
153 // return 1.f/length * a;
157 float4 normalize4(const float4 a)
159 float length = sqrtf(dot4(a, a));
160 return 1.f/length * a;
164 float4 createEquation(const float4 a, const float4 b, const float4 c)
169 eqn = normalize3( cross3(ab, ac) );
170 eqn.w = -dot3F4(eqn,a);
174 ///////////////////////////////////////
176 ///////////////////////////////////////
187 Matrix3x3 mtIdentity();
190 Matrix3x3 mtTranspose(Matrix3x3 m);
193 Matrix3x3 mtMul(Matrix3x3 a, Matrix3x3 b);
196 float4 mtMul1(Matrix3x3 a, float4 b);
199 float4 mtMul3(float4 a, Matrix3x3 b);
205 m.m_row[0] = (float4)(0.f);
206 m.m_row[1] = (float4)(0.f);
207 m.m_row[2] = (float4)(0.f);
212 Matrix3x3 mtIdentity()
215 m.m_row[0] = (float4)(1,0,0,0);
216 m.m_row[1] = (float4)(0,1,0,0);
217 m.m_row[2] = (float4)(0,0,1,0);
222 Matrix3x3 mtTranspose(Matrix3x3 m)
225 out.m_row[0] = (float4)(m.m_row[0].x, m.m_row[1].x, m.m_row[2].x, 0.f);
226 out.m_row[1] = (float4)(m.m_row[0].y, m.m_row[1].y, m.m_row[2].y, 0.f);
227 out.m_row[2] = (float4)(m.m_row[0].z, m.m_row[1].z, m.m_row[2].z, 0.f);
232 Matrix3x3 mtMul(Matrix3x3 a, Matrix3x3 b)
235 transB = mtTranspose( b );
237 // why this doesn't run when 0ing in the for{}
241 for(int i=0; i<3; i++)
243 // a.m_row[i].w = 0.f;
244 ans.m_row[i].x = dot3F4(a.m_row[i],transB.m_row[0]);
245 ans.m_row[i].y = dot3F4(a.m_row[i],transB.m_row[1]);
246 ans.m_row[i].z = dot3F4(a.m_row[i],transB.m_row[2]);
247 ans.m_row[i].w = 0.f;
253 float4 mtMul1(Matrix3x3 a, float4 b)
256 ans.x = dot3F4( a.m_row[0], b );
257 ans.y = dot3F4( a.m_row[1], b );
258 ans.z = dot3F4( a.m_row[2], b );
264 float4 mtMul3(float4 a, Matrix3x3 b)
266 float4 colx = make_float4(b.m_row[0].x, b.m_row[1].x, b.m_row[2].x, 0);
267 float4 coly = make_float4(b.m_row[0].y, b.m_row[1].y, b.m_row[2].y, 0);
268 float4 colz = make_float4(b.m_row[0].z, b.m_row[1].z, b.m_row[2].z, 0);
271 ans.x = dot3F4( a, colx );
272 ans.y = dot3F4( a, coly );
273 ans.z = dot3F4( a, colz );
277 ///////////////////////////////////////
279 ///////////////////////////////////////
281 typedef float4 Quaternion;
284 Quaternion qtMul(Quaternion a, Quaternion b);
287 Quaternion qtNormalize(Quaternion in);
290 float4 qtRotate(Quaternion q, float4 vec);
293 Quaternion qtInvert(Quaternion q);
300 Quaternion qtMul(Quaternion a, Quaternion b)
303 ans = cross3( a, b );
305 // ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);
306 ans.w = a.w*b.w - dot3F4(a, b);
311 Quaternion qtNormalize(Quaternion in)
313 return fastNormalize4(in);
314 // in /= length( in );
318 float4 qtRotate(Quaternion q, float4 vec)
320 Quaternion qInv = qtInvert( q );
323 float4 out = qtMul(qtMul(q,vcpy),qInv);
328 Quaternion qtInvert(Quaternion q)
330 return (Quaternion)(-q.xyz, q.w);
334 float4 qtInvRotate(const Quaternion q, float4 vec)
336 return qtRotate( qtInvert( q ), vec );
353 float m_restituitionCoeff;
354 float m_frictionCoeff;
359 Matrix3x3 m_invInertia;
360 Matrix3x3 m_initInvInertia;
366 float4 m_worldPos[4];
368 float m_jacCoeffInv[4];
370 float m_appliedRambdaDt[4];
372 float m_fJacCoeffInv[2];
373 float m_fAppliedRambdaDt[2];
390 // int m_paddings[1];
396 int m_maxBatch; // long batch really kills the performance
399 // int m_paddings[1];
400 } ConstBufferBatchSolve;
424 __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
425 void ReorderContactKernel(__global struct b3Contact4Data* in, __global struct b3Contact4Data* out, __global int2* sortData, int4 cb )
427 int nContacts = cb.x;
428 int gIdx = GET_GLOBAL_IDX;
430 if( gIdx < nContacts )
432 int srcIdx = sortData[gIdx].y;
433 out[gIdx] = in[srcIdx];
437 __kernel __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
438 void SetDeterminismSortDataChildShapeB(__global struct b3Contact4Data* contactsIn, __global int2* sortDataOut, int nContacts)
440 int gIdx = GET_GLOBAL_IDX;
442 if( gIdx < nContacts )
445 sd.x = contactsIn[gIdx].m_childIndexB;
447 sortDataOut[gIdx] = sd;
451 __kernel __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
452 void SetDeterminismSortDataChildShapeA(__global struct b3Contact4Data* contactsIn, __global int2* sortDataInOut, int nContacts)
454 int gIdx = GET_GLOBAL_IDX;
456 if( gIdx < nContacts )
459 sdIn = sortDataInOut[gIdx];
461 sdOut.x = contactsIn[sdIn.y].m_childIndexA;
463 sortDataInOut[gIdx] = sdOut;
467 __kernel __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
468 void SetDeterminismSortDataBodyA(__global struct b3Contact4Data* contactsIn, __global int2* sortDataInOut, int nContacts)
470 int gIdx = GET_GLOBAL_IDX;
472 if( gIdx < nContacts )
475 sdIn = sortDataInOut[gIdx];
477 sdOut.x = contactsIn[sdIn.y].m_bodyAPtrAndSignBit;
479 sortDataInOut[gIdx] = sdOut;
485 __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
486 void SetDeterminismSortDataBodyB(__global struct b3Contact4Data* contactsIn, __global int2* sortDataInOut, int nContacts)
488 int gIdx = GET_GLOBAL_IDX;
490 if( gIdx < nContacts )
493 sdIn = sortDataInOut[gIdx];
495 sdOut.x = contactsIn[sdIn.y].m_bodyBPtrAndSignBit;
497 sortDataInOut[gIdx] = sdOut;
513 __constant const int gridTable4x4[] =
521 __constant const int gridTable8x8[] =
523 0, 2, 3, 16, 17, 18, 19, 1,
524 66, 64, 80, 67, 82, 81, 65, 83,
525 131,144,128,130,147,129,145,146,
526 208,195,194,192,193,211,210,209,
527 21, 22, 23, 5, 4, 6, 7, 20,
528 86, 85, 69, 87, 70, 68, 84, 71,
529 151,133,149,150,135,148,132,134,
530 197,27,214,213,212,199,198,196
537 #define USE_SPATIAL_BATCHING 1
538 #define USE_4x4_GRID 1
541 __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
542 void SetSortDataKernel(__global struct b3Contact4Data* gContact, __global Body* gBodies, __global int2* gSortDataOut,
543 int nContacts,float scale,int4 nSplit,int staticIdx)
546 int gIdx = GET_GLOBAL_IDX;
548 if( gIdx < nContacts )
550 int aPtrAndSignBit = gContact[gIdx].m_bodyAPtrAndSignBit;
551 int bPtrAndSignBit = gContact[gIdx].m_bodyBPtrAndSignBit;
553 int aIdx = abs(aPtrAndSignBit );
554 int bIdx = abs(bPtrAndSignBit);
556 bool aStatic = (aPtrAndSignBit<0) ||(aPtrAndSignBit==staticIdx);
557 bool bStatic = (bPtrAndSignBit<0) ||(bPtrAndSignBit==staticIdx);
559 #if USE_SPATIAL_BATCHING
560 int idx = (aStatic)? bIdx: aIdx;
561 float4 p = gBodies[idx].m_pos;
562 int xIdx = (int)((p.x-((p.x<0.f)?1.f:0.f))*scale) & (nSplit.x-1);
563 int yIdx = (int)((p.y-((p.y<0.f)?1.f:0.f))*scale) & (nSplit.y-1);
564 int zIdx = (int)((p.z-((p.z<0.f)?1.f:0.f))*scale) & (nSplit.z-1);
565 int newIndex = (xIdx+yIdx*nSplit.x+zIdx*nSplit.x*nSplit.y);
567 #else//USE_SPATIAL_BATCHING
576 int gridIndex = aa + bb*4;
577 int newIndex = gridTable4x4[gridIndex];
586 int gridIndex = aa + bb*8;
587 int newIndex = gridTable8x8[gridIndex];
589 #endif//USE_SPATIAL_BATCHING
592 gSortDataOut[gIdx].x = newIndex;
593 gSortDataOut[gIdx].y = gIdx;
597 gSortDataOut[gIdx].x = 0xffffffff;
602 __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
603 void CopyConstraintKernel(__global struct b3Contact4Data* gIn, __global struct b3Contact4Data* gOut, int4 cb )
605 int gIdx = GET_GLOBAL_IDX;
608 gOut[gIdx] = gIn[gIdx];