share more data structures and code between OpenCL and C/C++ on CPU:
move the setConstraint4/b3ConvertConstraint4 to shared code.
This commit is contained in:
@@ -12,17 +12,88 @@ static const char* integrateKernelCL= \
|
||||
"3. This notice may not be removed or altered from any source distribution.\n"
|
||||
"*/\n"
|
||||
"//Originally written by Erwin Coumans\n"
|
||||
"float4 quatMult(float4 q1, float4 q2)\n"
|
||||
"#ifndef B3_RIGIDBODY_DATA_H\n"
|
||||
"#define B3_RIGIDBODY_DATA_H\n"
|
||||
"#ifndef B3_FLOAT4_H\n"
|
||||
"#define B3_FLOAT4_H\n"
|
||||
"#ifndef B3_PLATFORM_DEFINITIONS_H\n"
|
||||
"#define B3_PLATFORM_DEFINITIONS_H\n"
|
||||
"struct MyTest\n"
|
||||
"{\n"
|
||||
" float4 q;\n"
|
||||
" q.x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y;\n"
|
||||
" q.y = q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z;\n"
|
||||
" q.z = q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x;\n"
|
||||
" q.w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z; \n"
|
||||
" return q;\n"
|
||||
" int bla;\n"
|
||||
"};\n"
|
||||
"#ifdef __cplusplus\n"
|
||||
"#else\n"
|
||||
"#define b3AtomicInc atomic_inc\n"
|
||||
"#define b3Fabs fabs\n"
|
||||
"#endif\n"
|
||||
"#endif\n"
|
||||
"#ifdef __cplusplus\n"
|
||||
"#else\n"
|
||||
" typedef float4 b3Float4;\n"
|
||||
" #define b3Float4ConstArg const b3Float4\n"
|
||||
" #define b3MakeFloat4 (float4)\n"
|
||||
" float b3Dot3F4(b3Float4ConstArg v0,b3Float4ConstArg v1)\n"
|
||||
" {\n"
|
||||
" float4 a1 = b3MakeFloat4(v0.xyz,0.f);\n"
|
||||
" float4 b1 = b3MakeFloat4(v1.xyz,0.f);\n"
|
||||
" return dot(a1, b1);\n"
|
||||
" }\n"
|
||||
" b3Float4 b3Cross3(b3Float4ConstArg v0,b3Float4ConstArg v1)\n"
|
||||
" {\n"
|
||||
" float4 a1 = b3MakeFloat4(v0.xyz,0.f);\n"
|
||||
" float4 b1 = b3MakeFloat4(v1.xyz,0.f);\n"
|
||||
" return cross(a1, b1);\n"
|
||||
" }\n"
|
||||
"#endif \n"
|
||||
" \n"
|
||||
"inline bool b3IsAlmostZero(b3Float4ConstArg v)\n"
|
||||
"{\n"
|
||||
" if(b3Fabs(v.x)>1e-6 || b3Fabs(v.y)>1e-6 || b3Fabs(v.z)>1e-6) \n"
|
||||
" return false;\n"
|
||||
" return true;\n"
|
||||
"}\n"
|
||||
"float4 quatNorm(float4 q)\n"
|
||||
"#endif //B3_FLOAT4_H\n"
|
||||
"#ifndef B3_QUAT_H\n"
|
||||
"#define B3_QUAT_H\n"
|
||||
"#ifndef B3_PLATFORM_DEFINITIONS_H\n"
|
||||
"#ifdef __cplusplus\n"
|
||||
"#else\n"
|
||||
"#endif\n"
|
||||
"#endif\n"
|
||||
"#ifndef B3_FLOAT4_H\n"
|
||||
"#ifdef __cplusplus\n"
|
||||
"#else\n"
|
||||
"#endif \n"
|
||||
"#endif //B3_FLOAT4_H\n"
|
||||
"#ifdef __cplusplus\n"
|
||||
"#else\n"
|
||||
" typedef float4 b3Quat;\n"
|
||||
" #define b3QuatConstArg const b3Quat\n"
|
||||
" \n"
|
||||
" \n"
|
||||
"inline float4 b3FastNormalize4(float4 v)\n"
|
||||
"{\n"
|
||||
" v = (float4)(v.xyz,0.f);\n"
|
||||
" return fast_normalize(v);\n"
|
||||
"}\n"
|
||||
" \n"
|
||||
"inline b3Quat b3QuatMul(b3Quat a, b3Quat b);\n"
|
||||
"inline b3Quat b3QuatNormalize(b3QuatConstArg in);\n"
|
||||
"inline b3Quat b3QuatRotate(b3QuatConstArg q, b3QuatConstArg vec);\n"
|
||||
"inline b3Quat b3QuatInvert(b3QuatConstArg q);\n"
|
||||
"inline b3Quat b3QuatMul(b3QuatConstArg a, b3QuatConstArg b)\n"
|
||||
"{\n"
|
||||
" b3Quat ans;\n"
|
||||
" ans = b3Cross3( a, b );\n"
|
||||
" ans += a.w*b+b.w*a;\n"
|
||||
"// ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);\n"
|
||||
" ans.w = a.w*b.w - b3Dot3F4(a, b);\n"
|
||||
" return ans;\n"
|
||||
"}\n"
|
||||
"inline b3Quat b3QuatNormalize(b3QuatConstArg in)\n"
|
||||
"{\n"
|
||||
" //return b3FastNormalize4(in);\n"
|
||||
" float len = native_sqrt(dot(q, q));\n"
|
||||
" if(len > 0.f)\n"
|
||||
" {\n"
|
||||
@@ -35,19 +106,184 @@ static const char* integrateKernelCL= \
|
||||
" }\n"
|
||||
" return q;\n"
|
||||
"}\n"
|
||||
"inline float4 b3QuatRotate(b3QuatConstArg q, b3QuatConstArg vec)\n"
|
||||
"{\n"
|
||||
" b3Quat qInv = b3QuatInvert( q );\n"
|
||||
" float4 vcpy = vec;\n"
|
||||
" vcpy.w = 0.f;\n"
|
||||
" float4 out = b3QuatMul(b3QuatMul(q,vcpy),qInv);\n"
|
||||
" return out;\n"
|
||||
"}\n"
|
||||
"inline b3Quat b3QuatInvert(b3QuatConstArg q)\n"
|
||||
"{\n"
|
||||
" return (b3Quat)(-q.xyz, q.w);\n"
|
||||
"}\n"
|
||||
"inline float4 b3QuatInvRotate(b3QuatConstArg q, b3QuatConstArg vec)\n"
|
||||
"{\n"
|
||||
" return b3QuatRotate( b3QuatInvert( q ), vec );\n"
|
||||
"}\n"
|
||||
"inline b3Float4 b3TransformPoint(b3Float4ConstArg point, b3Float4ConstArg translation, b3QuatConstArg orientation)\n"
|
||||
"{\n"
|
||||
" return b3QuatRotate( orientation, point ) + (translation);\n"
|
||||
"}\n"
|
||||
" \n"
|
||||
"#endif \n"
|
||||
"#endif //B3_QUAT_H\n"
|
||||
"#ifndef B3_MAT3x3_H\n"
|
||||
"#define B3_MAT3x3_H\n"
|
||||
"#ifndef B3_QUAT_H\n"
|
||||
"#ifdef __cplusplus\n"
|
||||
"#else\n"
|
||||
"#endif \n"
|
||||
"#endif //B3_QUAT_H\n"
|
||||
"#ifdef __cplusplus\n"
|
||||
"#else\n"
|
||||
"typedef struct\n"
|
||||
"{\n"
|
||||
" float4 m_pos;\n"
|
||||
" float4 m_quat;\n"
|
||||
" float4 m_linVel;\n"
|
||||
" float4 m_angVel;\n"
|
||||
" unsigned int m_collidableIdx;\n"
|
||||
" float m_invMass;\n"
|
||||
" float m_restituitionCoeff;\n"
|
||||
" float m_frictionCoeff;\n"
|
||||
"} Body;\n"
|
||||
" b3Float4 m_row[3];\n"
|
||||
"}b3Mat3x3;\n"
|
||||
"#define b3Mat3x3ConstArg const b3Mat3x3\n"
|
||||
"#define b3GetRow(m,row) (m.m_row[row])\n"
|
||||
"inline b3Mat3x3 b3QuatGetRotationMatrix(b3Quat quat)\n"
|
||||
"{\n"
|
||||
" b3Float4 quat2 = (b3Float4)(quat.x*quat.x, quat.y*quat.y, quat.z*quat.z, 0.f);\n"
|
||||
" b3Mat3x3 out;\n"
|
||||
" out.m_row[0].x=1-2*quat2.y-2*quat2.z;\n"
|
||||
" out.m_row[0].y=2*quat.x*quat.y-2*quat.w*quat.z;\n"
|
||||
" out.m_row[0].z=2*quat.x*quat.z+2*quat.w*quat.y;\n"
|
||||
" out.m_row[0].w = 0.f;\n"
|
||||
" out.m_row[1].x=2*quat.x*quat.y+2*quat.w*quat.z;\n"
|
||||
" out.m_row[1].y=1-2*quat2.x-2*quat2.z;\n"
|
||||
" out.m_row[1].z=2*quat.y*quat.z-2*quat.w*quat.x;\n"
|
||||
" out.m_row[1].w = 0.f;\n"
|
||||
" out.m_row[2].x=2*quat.x*quat.z-2*quat.w*quat.y;\n"
|
||||
" out.m_row[2].y=2*quat.y*quat.z+2*quat.w*quat.x;\n"
|
||||
" out.m_row[2].z=1-2*quat2.x-2*quat2.y;\n"
|
||||
" out.m_row[2].w = 0.f;\n"
|
||||
" return out;\n"
|
||||
"}\n"
|
||||
"inline b3Mat3x3 b3AbsoluteMat3x3(b3Mat3x3ConstArg matIn)\n"
|
||||
"{\n"
|
||||
" b3Mat3x3 out;\n"
|
||||
" out.m_row[0] = fabs(matIn.m_row[0]);\n"
|
||||
" out.m_row[1] = fabs(matIn.m_row[1]);\n"
|
||||
" out.m_row[2] = fabs(matIn.m_row[2]);\n"
|
||||
" return out;\n"
|
||||
"}\n"
|
||||
"__inline\n"
|
||||
"b3Mat3x3 mtZero();\n"
|
||||
"__inline\n"
|
||||
"b3Mat3x3 mtIdentity();\n"
|
||||
"__inline\n"
|
||||
"b3Mat3x3 mtTranspose(b3Mat3x3 m);\n"
|
||||
"__inline\n"
|
||||
"b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b);\n"
|
||||
"__inline\n"
|
||||
"b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b);\n"
|
||||
"__inline\n"
|
||||
"b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b);\n"
|
||||
"__inline\n"
|
||||
"b3Mat3x3 mtZero()\n"
|
||||
"{\n"
|
||||
" b3Mat3x3 m;\n"
|
||||
" m.m_row[0] = (b3Float4)(0.f);\n"
|
||||
" m.m_row[1] = (b3Float4)(0.f);\n"
|
||||
" m.m_row[2] = (b3Float4)(0.f);\n"
|
||||
" return m;\n"
|
||||
"}\n"
|
||||
"__inline\n"
|
||||
"b3Mat3x3 mtIdentity()\n"
|
||||
"{\n"
|
||||
" b3Mat3x3 m;\n"
|
||||
" m.m_row[0] = (b3Float4)(1,0,0,0);\n"
|
||||
" m.m_row[1] = (b3Float4)(0,1,0,0);\n"
|
||||
" m.m_row[2] = (b3Float4)(0,0,1,0);\n"
|
||||
" return m;\n"
|
||||
"}\n"
|
||||
"__inline\n"
|
||||
"b3Mat3x3 mtTranspose(b3Mat3x3 m)\n"
|
||||
"{\n"
|
||||
" b3Mat3x3 out;\n"
|
||||
" out.m_row[0] = (b3Float4)(m.m_row[0].x, m.m_row[1].x, m.m_row[2].x, 0.f);\n"
|
||||
" out.m_row[1] = (b3Float4)(m.m_row[0].y, m.m_row[1].y, m.m_row[2].y, 0.f);\n"
|
||||
" out.m_row[2] = (b3Float4)(m.m_row[0].z, m.m_row[1].z, m.m_row[2].z, 0.f);\n"
|
||||
" return out;\n"
|
||||
"}\n"
|
||||
"__inline\n"
|
||||
"b3Mat3x3 mtMul(b3Mat3x3 a, b3Mat3x3 b)\n"
|
||||
"{\n"
|
||||
" b3Mat3x3 transB;\n"
|
||||
" transB = mtTranspose( b );\n"
|
||||
" b3Mat3x3 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 = b3Dot3F4(a.m_row[i],transB.m_row[0]);\n"
|
||||
" ans.m_row[i].y = b3Dot3F4(a.m_row[i],transB.m_row[1]);\n"
|
||||
" ans.m_row[i].z = b3Dot3F4(a.m_row[i],transB.m_row[2]);\n"
|
||||
" ans.m_row[i].w = 0.f;\n"
|
||||
" }\n"
|
||||
" return ans;\n"
|
||||
"}\n"
|
||||
"__inline\n"
|
||||
"b3Float4 mtMul1(b3Mat3x3 a, b3Float4 b)\n"
|
||||
"{\n"
|
||||
" b3Float4 ans;\n"
|
||||
" ans.x = b3Dot3F4( a.m_row[0], b );\n"
|
||||
" ans.y = b3Dot3F4( a.m_row[1], b );\n"
|
||||
" ans.z = b3Dot3F4( a.m_row[2], b );\n"
|
||||
" ans.w = 0.f;\n"
|
||||
" return ans;\n"
|
||||
"}\n"
|
||||
"__inline\n"
|
||||
"b3Float4 mtMul3(b3Float4 a, b3Mat3x3 b)\n"
|
||||
"{\n"
|
||||
" b3Float4 colx = b3MakeFloat4(b.m_row[0].x, b.m_row[1].x, b.m_row[2].x, 0);\n"
|
||||
" b3Float4 coly = b3MakeFloat4(b.m_row[0].y, b.m_row[1].y, b.m_row[2].y, 0);\n"
|
||||
" b3Float4 colz = b3MakeFloat4(b.m_row[0].z, b.m_row[1].z, b.m_row[2].z, 0);\n"
|
||||
" b3Float4 ans;\n"
|
||||
" ans.x = b3Dot3F4( a, colx );\n"
|
||||
" ans.y = b3Dot3F4( a, coly );\n"
|
||||
" ans.z = b3Dot3F4( a, colz );\n"
|
||||
" return ans;\n"
|
||||
"}\n"
|
||||
"#endif\n"
|
||||
"#endif //B3_MAT3x3_H\n"
|
||||
"typedef struct b3RigidBodyData b3RigidBodyData_t;\n"
|
||||
"struct b3RigidBodyData\n"
|
||||
"{\n"
|
||||
" b3Float4 m_pos;\n"
|
||||
" b3Quat m_quat;\n"
|
||||
" b3Float4 m_linVel;\n"
|
||||
" b3Float4 m_angVel;\n"
|
||||
" int m_collidableIdx;\n"
|
||||
" float m_invMass;\n"
|
||||
" float m_restituitionCoeff;\n"
|
||||
" float m_frictionCoeff;\n"
|
||||
"};\n"
|
||||
"typedef struct b3InertiaData b3InertiaData_t;\n"
|
||||
"struct b3InertiaData\n"
|
||||
"{\n"
|
||||
" b3Mat3x3 m_invInertiaWorld;\n"
|
||||
" b3Mat3x3 m_initInvInertia;\n"
|
||||
"};\n"
|
||||
"#endif //B3_RIGIDBODY_DATA_H\n"
|
||||
" \n"
|
||||
"float4 quatMult(float4 q1, float4 q2)\n"
|
||||
"{\n"
|
||||
" float4 q;\n"
|
||||
" q.x = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y;\n"
|
||||
" q.y = q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z;\n"
|
||||
" q.z = q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x;\n"
|
||||
" q.w = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z; \n"
|
||||
" return q;\n"
|
||||
"}\n"
|
||||
"__kernel void \n"
|
||||
" integrateTransformsKernel( __global Body* bodies,const int numNodes, float timeStep, float angularDamping, float4 gravityAcceleration)\n"
|
||||
" integrateTransformsKernel( __global b3RigidBodyData_t* bodies,const int numNodes, float timeStep, float angularDamping, float4 gravityAcceleration)\n"
|
||||
"{\n"
|
||||
" int nodeID = get_global_id(0);\n"
|
||||
" float BT_GPU_ANGULAR_MOTION_THRESHOLD = (0.25f * 3.14159254f);\n"
|
||||
@@ -82,7 +318,7 @@ static const char* integrateKernelCL= \
|
||||
" dorn.w = native_cos(fAngle * timeStep * 0.5f);\n"
|
||||
" float4 orn0 = bodies[nodeID].m_quat;\n"
|
||||
" float4 predictedOrn = quatMult(dorn, orn0);\n"
|
||||
" predictedOrn = quatNorm(predictedOrn);\n"
|
||||
" predictedOrn = b3QuatNormalize(predictedOrn);\n"
|
||||
" bodies[nodeID].m_quat=predictedOrn;\n"
|
||||
" }\n"
|
||||
" //linear velocity \n"
|
||||
|
||||
Reference in New Issue
Block a user