3 Copyright (c) 2012 Advanced Micro Devices, Inc.
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
15 //Originally written by Takahiro Harada
17 #include "Bullet3Dynamics/shared/b3ConvertConstraint4.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);
190 // int m_paddings[1];
196 int m_maxBatch; // long batch really kills the performance
199 // int m_paddings[1];
200 } ConstBufferBatchSolve;
230 float m_positionDrift;
231 float m_positionConstraintCoeff;
235 __attribute__((reqd_work_group_size(WG_SIZE,1,1)))
236 void ContactToConstraintKernel(__global struct b3Contact4Data* gContact, __global b3RigidBodyData_t* gBodies, __global b3InertiaData_t* gShapes, __global b3ContactConstraint4_t* gConstraintOut,
240 float positionConstraintCoeff
243 int gIdx = GET_GLOBAL_IDX;
245 if( gIdx < nContacts )
247 int aIdx = abs(gContact[gIdx].m_bodyAPtrAndSignBit);
248 int bIdx = abs(gContact[gIdx].m_bodyBPtrAndSignBit);
250 float4 posA = gBodies[aIdx].m_pos;
251 float4 linVelA = gBodies[aIdx].m_linVel;
252 float4 angVelA = gBodies[aIdx].m_angVel;
253 float invMassA = gBodies[aIdx].m_invMass;
254 b3Mat3x3 invInertiaA = gShapes[aIdx].m_initInvInertia;
256 float4 posB = gBodies[bIdx].m_pos;
257 float4 linVelB = gBodies[bIdx].m_linVel;
258 float4 angVelB = gBodies[bIdx].m_angVel;
259 float invMassB = gBodies[bIdx].m_invMass;
260 b3Mat3x3 invInertiaB = gShapes[bIdx].m_initInvInertia;
262 b3ContactConstraint4_t cs;
264 setConstraint4( posA, linVelA, angVelA, invMassA, invInertiaA, posB, linVelB, angVelB, invMassB, invInertiaB,
265 &gContact[gIdx], dt, positionDrift, positionConstraintCoeff,
268 cs.m_batchIdx = gContact[gIdx].m_batchIdx;
270 gConstraintOut[gIdx] = cs;