| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888 |
- #define TRIANGLE_NUM_CONVEX_FACES 5
- #pragma OPENCL EXTENSION cl_amd_printf : enable
- #pragma OPENCL EXTENSION cl_khr_local_int32_base_atomics : enable
- #pragma OPENCL EXTENSION cl_khr_global_int32_base_atomics : enable
- #pragma OPENCL EXTENSION cl_khr_local_int32_extended_atomics : enable
- #pragma OPENCL EXTENSION cl_khr_global_int32_extended_atomics : enable
- #ifdef cl_ext_atomic_counters_32
- #pragma OPENCL EXTENSION cl_ext_atomic_counters_32 : enable
- #else
- #define counter32_t volatile __global int*
- #endif
- #define GET_GROUP_IDX get_group_id(0)
- #define GET_LOCAL_IDX get_local_id(0)
- #define GET_GLOBAL_IDX get_global_id(0)
- #define GET_GROUP_SIZE get_local_size(0)
- #define GET_NUM_GROUPS get_num_groups(0)
- #define GROUP_LDS_BARRIER barrier(CLK_LOCAL_MEM_FENCE)
- #define GROUP_MEM_FENCE mem_fence(CLK_LOCAL_MEM_FENCE)
- #define AtomInc(x) atom_inc(&(x))
- #define AtomInc1(x, out) out = atom_inc(&(x))
- #define AppendInc(x, out) out = atomic_inc(x)
- #define AtomAdd(x, value) atom_add(&(x), value)
- #define AtomCmpxhg(x, cmp, value) atom_cmpxchg( &(x), cmp, value )
- #define AtomXhg(x, value) atom_xchg ( &(x), value )
- #define max2 max
- #define min2 min
- typedef unsigned int u32;
- #include "Bullet3Collision/NarrowPhaseCollision/shared/b3Contact4Data.h"
- #include "Bullet3Collision/NarrowPhaseCollision/shared/b3ConvexPolyhedronData.h"
- #include "Bullet3Collision/NarrowPhaseCollision/shared/b3Collidable.h"
- #include "Bullet3Collision/NarrowPhaseCollision/shared/b3RigidBodyData.h"
- #define GET_NPOINTS(x) (x).m_worldNormalOnB.w
- #define SELECT_UINT4( b, a, condition ) select( b,a,condition )
- #define make_float4 (float4)
- #define make_float2 (float2)
- #define make_uint4 (uint4)
- #define make_int4 (int4)
- #define make_uint2 (uint2)
- #define make_int2 (int2)
- __inline
- float fastDiv(float numerator, float denominator)
- {
- return native_divide(numerator, denominator);
- // return numerator/denominator;
- }
- __inline
- float4 fastDiv4(float4 numerator, float4 denominator)
- {
- return native_divide(numerator, denominator);
- }
- __inline
- float4 cross3(float4 a, float4 b)
- {
- return cross(a,b);
- }
- //#define dot3F4 dot
- __inline
- float dot3F4(float4 a, float4 b)
- {
- float4 a1 = make_float4(a.xyz,0.f);
- float4 b1 = make_float4(b.xyz,0.f);
- return dot(a1, b1);
- }
- __inline
- float4 fastNormalize4(float4 v)
- {
- return fast_normalize(v);
- }
- ///////////////////////////////////////
- // Quaternion
- ///////////////////////////////////////
- typedef float4 Quaternion;
- __inline
- Quaternion qtMul(Quaternion a, Quaternion b);
- __inline
- Quaternion qtNormalize(Quaternion in);
- __inline
- float4 qtRotate(Quaternion q, float4 vec);
- __inline
- Quaternion qtInvert(Quaternion q);
- __inline
- Quaternion qtMul(Quaternion a, Quaternion b)
- {
- Quaternion ans;
- ans = cross3( a, b );
- ans += a.w*b+b.w*a;
- // ans.w = a.w*b.w - (a.x*b.x+a.y*b.y+a.z*b.z);
- ans.w = a.w*b.w - dot3F4(a, b);
- return ans;
- }
- __inline
- Quaternion qtNormalize(Quaternion in)
- {
- return fastNormalize4(in);
- // in /= length( in );
- // return in;
- }
- __inline
- float4 qtRotate(Quaternion q, float4 vec)
- {
- Quaternion qInv = qtInvert( q );
- float4 vcpy = vec;
- vcpy.w = 0.f;
- float4 out = qtMul(qtMul(q,vcpy),qInv);
- return out;
- }
- __inline
- Quaternion qtInvert(Quaternion q)
- {
- return (Quaternion)(-q.xyz, q.w);
- }
- __inline
- float4 qtInvRotate(const Quaternion q, float4 vec)
- {
- return qtRotate( qtInvert( q ), vec );
- }
- __inline
- float4 transform(const float4* p, const float4* translation, const Quaternion* orientation)
- {
- return qtRotate( *orientation, *p ) + (*translation);
- }
- __inline
- float4 normalize3(const float4 a)
- {
- float4 n = make_float4(a.x, a.y, a.z, 0.f);
- return fastNormalize4( n );
- }
- __inline float4 lerp3(const float4 a,const float4 b, float t)
- {
- return make_float4( a.x + (b.x - a.x) * t,
- a.y + (b.y - a.y) * t,
- a.z + (b.z - a.z) * t,
- 0.f);
- }
- // Clips a face to the back of a plane, return the number of vertices out, stored in ppVtxOut
- int clipFaceGlobal(__global const float4* pVtxIn, int numVertsIn, float4 planeNormalWS,float planeEqWS, __global float4* ppVtxOut)
- {
-
- int ve;
- float ds, de;
- int numVertsOut = 0;
- //double-check next test
- if (numVertsIn < 2)
- return 0;
-
- float4 firstVertex=pVtxIn[numVertsIn-1];
- float4 endVertex = pVtxIn[0];
-
- ds = dot3F4(planeNormalWS,firstVertex)+planeEqWS;
-
- for (ve = 0; ve < numVertsIn; ve++)
- {
- endVertex=pVtxIn[ve];
- de = dot3F4(planeNormalWS,endVertex)+planeEqWS;
- if (ds<0)
- {
- if (de<0)
- {
- // Start < 0, end < 0, so output endVertex
- ppVtxOut[numVertsOut++] = endVertex;
- }
- else
- {
- // Start < 0, end >= 0, so output intersection
- ppVtxOut[numVertsOut++] = lerp3(firstVertex, endVertex,(ds * 1.f/(ds - de)) );
- }
- }
- else
- {
- if (de<0)
- {
- // Start >= 0, end < 0 so output intersection and end
- ppVtxOut[numVertsOut++] = lerp3(firstVertex, endVertex,(ds * 1.f/(ds - de)) );
- ppVtxOut[numVertsOut++] = endVertex;
- }
- }
- firstVertex = endVertex;
- ds = de;
- }
- return numVertsOut;
- }
- // Clips a face to the back of a plane, return the number of vertices out, stored in ppVtxOut
- int clipFace(const float4* pVtxIn, int numVertsIn, float4 planeNormalWS,float planeEqWS, float4* ppVtxOut)
- {
-
- int ve;
- float ds, de;
- int numVertsOut = 0;
- //double-check next test
- if (numVertsIn < 2)
- return 0;
- float4 firstVertex=pVtxIn[numVertsIn-1];
- float4 endVertex = pVtxIn[0];
-
- ds = dot3F4(planeNormalWS,firstVertex)+planeEqWS;
- for (ve = 0; ve < numVertsIn; ve++)
- {
- endVertex=pVtxIn[ve];
- de = dot3F4(planeNormalWS,endVertex)+planeEqWS;
- if (ds<0)
- {
- if (de<0)
- {
- // Start < 0, end < 0, so output endVertex
- ppVtxOut[numVertsOut++] = endVertex;
- }
- else
- {
- // Start < 0, end >= 0, so output intersection
- ppVtxOut[numVertsOut++] = lerp3(firstVertex, endVertex,(ds * 1.f/(ds - de)) );
- }
- }
- else
- {
- if (de<0)
- {
- // Start >= 0, end < 0 so output intersection and end
- ppVtxOut[numVertsOut++] = lerp3(firstVertex, endVertex,(ds * 1.f/(ds - de)) );
- ppVtxOut[numVertsOut++] = endVertex;
- }
- }
- firstVertex = endVertex;
- ds = de;
- }
- return numVertsOut;
- }
- int clipFaceAgainstHull(const float4 separatingNormal, __global const b3ConvexPolyhedronData_t* hullA,
- const float4 posA, const Quaternion ornA, float4* worldVertsB1, int numWorldVertsB1,
- float4* worldVertsB2, int capacityWorldVertsB2,
- const float minDist, float maxDist,
- __global const float4* vertices,
- __global const b3GpuFace_t* faces,
- __global const int* indices,
- float4* contactsOut,
- int contactCapacity)
- {
- int numContactsOut = 0;
- float4* pVtxIn = worldVertsB1;
- float4* pVtxOut = worldVertsB2;
-
- int numVertsIn = numWorldVertsB1;
- int numVertsOut = 0;
- int closestFaceA=-1;
- {
- float dmin = FLT_MAX;
- for(int face=0;face<hullA->m_numFaces;face++)
- {
- const float4 Normal = make_float4(
- faces[hullA->m_faceOffset+face].m_plane.x,
- faces[hullA->m_faceOffset+face].m_plane.y,
- faces[hullA->m_faceOffset+face].m_plane.z,0.f);
- const float4 faceANormalWS = qtRotate(ornA,Normal);
-
- float d = dot3F4(faceANormalWS,separatingNormal);
- if (d < dmin)
- {
- dmin = d;
- closestFaceA = face;
- }
- }
- }
- if (closestFaceA<0)
- return numContactsOut;
- b3GpuFace_t polyA = faces[hullA->m_faceOffset+closestFaceA];
- // clip polygon to back of planes of all faces of hull A that are adjacent to witness face
- int numVerticesA = polyA.m_numIndices;
- for(int e0=0;e0<numVerticesA;e0++)
- {
- const float4 a = vertices[hullA->m_vertexOffset+indices[polyA.m_indexOffset+e0]];
- const float4 b = vertices[hullA->m_vertexOffset+indices[polyA.m_indexOffset+((e0+1)%numVerticesA)]];
- const float4 edge0 = a - b;
- const float4 WorldEdge0 = qtRotate(ornA,edge0);
- float4 planeNormalA = make_float4(polyA.m_plane.x,polyA.m_plane.y,polyA.m_plane.z,0.f);
- float4 worldPlaneAnormal1 = qtRotate(ornA,planeNormalA);
- float4 planeNormalWS1 = -cross3(WorldEdge0,worldPlaneAnormal1);
- float4 worldA1 = transform(&a,&posA,&ornA);
- float planeEqWS1 = -dot3F4(worldA1,planeNormalWS1);
-
- float4 planeNormalWS = planeNormalWS1;
- float planeEqWS=planeEqWS1;
-
- //clip face
- //clipFace(*pVtxIn, *pVtxOut,planeNormalWS,planeEqWS);
- numVertsOut = clipFace(pVtxIn, numVertsIn, planeNormalWS,planeEqWS, pVtxOut);
- //btSwap(pVtxIn,pVtxOut);
- float4* tmp = pVtxOut;
- pVtxOut = pVtxIn;
- pVtxIn = tmp;
- numVertsIn = numVertsOut;
- numVertsOut = 0;
- }
-
- // only keep points that are behind the witness face
- {
- float4 localPlaneNormal = make_float4(polyA.m_plane.x,polyA.m_plane.y,polyA.m_plane.z,0.f);
- float localPlaneEq = polyA.m_plane.w;
- float4 planeNormalWS = qtRotate(ornA,localPlaneNormal);
- float planeEqWS=localPlaneEq-dot3F4(planeNormalWS,posA);
- for (int i=0;i<numVertsIn;i++)
- {
- float depth = dot3F4(planeNormalWS,pVtxIn[i])+planeEqWS;
- if (depth <=minDist)
- {
- depth = minDist;
- }
- if (depth <=maxDist)
- {
- float4 pointInWorld = pVtxIn[i];
- //resultOut.addContactPoint(separatingNormal,point,depth);
- contactsOut[numContactsOut++] = make_float4(pointInWorld.x,pointInWorld.y,pointInWorld.z,depth);
- }
- }
- }
- return numContactsOut;
- }
- int clipFaceAgainstHullLocalA(const float4 separatingNormal, const b3ConvexPolyhedronData_t* hullA,
- const float4 posA, const Quaternion ornA, float4* worldVertsB1, int numWorldVertsB1,
- float4* worldVertsB2, int capacityWorldVertsB2,
- const float minDist, float maxDist,
- const float4* verticesA,
- const b3GpuFace_t* facesA,
- const int* indicesA,
- __global const float4* verticesB,
- __global const b3GpuFace_t* facesB,
- __global const int* indicesB,
- float4* contactsOut,
- int contactCapacity)
- {
- int numContactsOut = 0;
- float4* pVtxIn = worldVertsB1;
- float4* pVtxOut = worldVertsB2;
-
- int numVertsIn = numWorldVertsB1;
- int numVertsOut = 0;
- int closestFaceA=-1;
- {
- float dmin = FLT_MAX;
- for(int face=0;face<hullA->m_numFaces;face++)
- {
- const float4 Normal = make_float4(
- facesA[hullA->m_faceOffset+face].m_plane.x,
- facesA[hullA->m_faceOffset+face].m_plane.y,
- facesA[hullA->m_faceOffset+face].m_plane.z,0.f);
- const float4 faceANormalWS = qtRotate(ornA,Normal);
-
- float d = dot3F4(faceANormalWS,separatingNormal);
- if (d < dmin)
- {
- dmin = d;
- closestFaceA = face;
- }
- }
- }
- if (closestFaceA<0)
- return numContactsOut;
- b3GpuFace_t polyA = facesA[hullA->m_faceOffset+closestFaceA];
- // clip polygon to back of planes of all faces of hull A that are adjacent to witness face
- int numVerticesA = polyA.m_numIndices;
- for(int e0=0;e0<numVerticesA;e0++)
- {
- const float4 a = verticesA[hullA->m_vertexOffset+indicesA[polyA.m_indexOffset+e0]];
- const float4 b = verticesA[hullA->m_vertexOffset+indicesA[polyA.m_indexOffset+((e0+1)%numVerticesA)]];
- const float4 edge0 = a - b;
- const float4 WorldEdge0 = qtRotate(ornA,edge0);
- float4 planeNormalA = make_float4(polyA.m_plane.x,polyA.m_plane.y,polyA.m_plane.z,0.f);
- float4 worldPlaneAnormal1 = qtRotate(ornA,planeNormalA);
- float4 planeNormalWS1 = -cross3(WorldEdge0,worldPlaneAnormal1);
- float4 worldA1 = transform(&a,&posA,&ornA);
- float planeEqWS1 = -dot3F4(worldA1,planeNormalWS1);
-
- float4 planeNormalWS = planeNormalWS1;
- float planeEqWS=planeEqWS1;
-
- //clip face
- //clipFace(*pVtxIn, *pVtxOut,planeNormalWS,planeEqWS);
- numVertsOut = clipFace(pVtxIn, numVertsIn, planeNormalWS,planeEqWS, pVtxOut);
- //btSwap(pVtxIn,pVtxOut);
- float4* tmp = pVtxOut;
- pVtxOut = pVtxIn;
- pVtxIn = tmp;
- numVertsIn = numVertsOut;
- numVertsOut = 0;
- }
-
- // only keep points that are behind the witness face
- {
- float4 localPlaneNormal = make_float4(polyA.m_plane.x,polyA.m_plane.y,polyA.m_plane.z,0.f);
- float localPlaneEq = polyA.m_plane.w;
- float4 planeNormalWS = qtRotate(ornA,localPlaneNormal);
- float planeEqWS=localPlaneEq-dot3F4(planeNormalWS,posA);
- for (int i=0;i<numVertsIn;i++)
- {
- float depth = dot3F4(planeNormalWS,pVtxIn[i])+planeEqWS;
- if (depth <=minDist)
- {
- depth = minDist;
- }
- if (depth <=maxDist)
- {
- float4 pointInWorld = pVtxIn[i];
- //resultOut.addContactPoint(separatingNormal,point,depth);
- contactsOut[numContactsOut++] = make_float4(pointInWorld.x,pointInWorld.y,pointInWorld.z,depth);
- }
- }
- }
- return numContactsOut;
- }
- int clipHullAgainstHull(const float4 separatingNormal,
- __global const b3ConvexPolyhedronData_t* hullA, __global const b3ConvexPolyhedronData_t* hullB,
- const float4 posA, const Quaternion ornA,const float4 posB, const Quaternion ornB,
- float4* worldVertsB1, float4* worldVertsB2, int capacityWorldVerts,
- const float minDist, float maxDist,
- __global const float4* vertices,
- __global const b3GpuFace_t* faces,
- __global const int* indices,
- float4* localContactsOut,
- int localContactCapacity)
- {
- int numContactsOut = 0;
- int numWorldVertsB1= 0;
- int closestFaceB=-1;
- float dmax = -FLT_MAX;
- {
- for(int face=0;face<hullB->m_numFaces;face++)
- {
- const float4 Normal = make_float4(faces[hullB->m_faceOffset+face].m_plane.x,
- faces[hullB->m_faceOffset+face].m_plane.y, faces[hullB->m_faceOffset+face].m_plane.z,0.f);
- const float4 WorldNormal = qtRotate(ornB, Normal);
- float d = dot3F4(WorldNormal,separatingNormal);
- if (d > dmax)
- {
- dmax = d;
- closestFaceB = face;
- }
- }
- }
- {
- const b3GpuFace_t polyB = faces[hullB->m_faceOffset+closestFaceB];
- const int numVertices = polyB.m_numIndices;
- for(int e0=0;e0<numVertices;e0++)
- {
- const float4 b = vertices[hullB->m_vertexOffset+indices[polyB.m_indexOffset+e0]];
- worldVertsB1[numWorldVertsB1++] = transform(&b,&posB,&ornB);
- }
- }
- if (closestFaceB>=0)
- {
- numContactsOut = clipFaceAgainstHull(separatingNormal, hullA,
- posA,ornA,
- worldVertsB1,numWorldVertsB1,worldVertsB2,capacityWorldVerts, minDist, maxDist,vertices,
- faces,
- indices,localContactsOut,localContactCapacity);
- }
- return numContactsOut;
- }
- int clipHullAgainstHullLocalA(const float4 separatingNormal,
- const b3ConvexPolyhedronData_t* hullA, __global const b3ConvexPolyhedronData_t* hullB,
- const float4 posA, const Quaternion ornA,const float4 posB, const Quaternion ornB,
- float4* worldVertsB1, float4* worldVertsB2, int capacityWorldVerts,
- const float minDist, float maxDist,
- const float4* verticesA,
- const b3GpuFace_t* facesA,
- const int* indicesA,
- __global const float4* verticesB,
- __global const b3GpuFace_t* facesB,
- __global const int* indicesB,
- float4* localContactsOut,
- int localContactCapacity)
- {
- int numContactsOut = 0;
- int numWorldVertsB1= 0;
- int closestFaceB=-1;
- float dmax = -FLT_MAX;
- {
- for(int face=0;face<hullB->m_numFaces;face++)
- {
- const float4 Normal = make_float4(facesB[hullB->m_faceOffset+face].m_plane.x,
- facesB[hullB->m_faceOffset+face].m_plane.y, facesB[hullB->m_faceOffset+face].m_plane.z,0.f);
- const float4 WorldNormal = qtRotate(ornB, Normal);
- float d = dot3F4(WorldNormal,separatingNormal);
- if (d > dmax)
- {
- dmax = d;
- closestFaceB = face;
- }
- }
- }
- {
- const b3GpuFace_t polyB = facesB[hullB->m_faceOffset+closestFaceB];
- const int numVertices = polyB.m_numIndices;
- for(int e0=0;e0<numVertices;e0++)
- {
- const float4 b = verticesB[hullB->m_vertexOffset+indicesB[polyB.m_indexOffset+e0]];
- worldVertsB1[numWorldVertsB1++] = transform(&b,&posB,&ornB);
- }
- }
- if (closestFaceB>=0)
- {
- numContactsOut = clipFaceAgainstHullLocalA(separatingNormal, hullA,
- posA,ornA,
- worldVertsB1,numWorldVertsB1,worldVertsB2,capacityWorldVerts, minDist, maxDist,
- verticesA,facesA,indicesA,
- verticesB,facesB,indicesB,
- localContactsOut,localContactCapacity);
- }
- return numContactsOut;
- }
- #define PARALLEL_SUM(v, n) for(int j=1; j<n; j++) v[0] += v[j];
- #define PARALLEL_DO(execution, n) for(int ie=0; ie<n; ie++){execution;}
- #define REDUCE_MAX(v, n) {int i=0;\
- for(int offset=0; offset<n; offset++) v[i] = (v[i].y > v[i+offset].y)? v[i]: v[i+offset]; }
- #define REDUCE_MIN(v, n) {int i=0;\
- for(int offset=0; offset<n; offset++) v[i] = (v[i].y < v[i+offset].y)? v[i]: v[i+offset]; }
- int extractManifoldSequentialGlobal(__global const float4* p, int nPoints, float4 nearNormal, int4* contactIdx)
- {
- if( nPoints == 0 )
- return 0;
-
- if (nPoints <=4)
- return nPoints;
-
-
- if (nPoints >64)
- nPoints = 64;
-
- float4 center = make_float4(0.f);
- {
-
- for (int i=0;i<nPoints;i++)
- center += p[i];
- center /= (float)nPoints;
- }
-
-
-
- // sample 4 directions
-
- float4 aVector = p[0] - center;
- float4 u = cross3( nearNormal, aVector );
- float4 v = cross3( nearNormal, u );
- u = normalize3( u );
- v = normalize3( v );
-
-
- //keep point with deepest penetration
- float minW= FLT_MAX;
-
- int minIndex=-1;
-
- float4 maxDots;
- maxDots.x = FLT_MIN;
- maxDots.y = FLT_MIN;
- maxDots.z = FLT_MIN;
- maxDots.w = FLT_MIN;
-
- // idx, distance
- for(int ie = 0; ie<nPoints; ie++ )
- {
- if (p[ie].w<minW)
- {
- minW = p[ie].w;
- minIndex=ie;
- }
- float f;
- float4 r = p[ie]-center;
- f = dot3F4( u, r );
- if (f<maxDots.x)
- {
- maxDots.x = f;
- contactIdx[0].x = ie;
- }
-
- f = dot3F4( -u, r );
- if (f<maxDots.y)
- {
- maxDots.y = f;
- contactIdx[0].y = ie;
- }
-
-
- f = dot3F4( v, r );
- if (f<maxDots.z)
- {
- maxDots.z = f;
- contactIdx[0].z = ie;
- }
-
- f = dot3F4( -v, r );
- if (f<maxDots.w)
- {
- maxDots.w = f;
- contactIdx[0].w = ie;
- }
-
- }
-
- if (contactIdx[0].x != minIndex && contactIdx[0].y != minIndex && contactIdx[0].z != minIndex && contactIdx[0].w != minIndex)
- {
- //replace the first contact with minimum (todo: replace contact with least penetration)
- contactIdx[0].x = minIndex;
- }
-
- return 4;
-
- }
- int extractManifoldSequentialGlobalFake(__global const float4* p, int nPoints, float4 nearNormal, int* contactIdx)
- {
- contactIdx[0] = 0;
- contactIdx[1] = 1;
- contactIdx[2] = 2;
- contactIdx[3] = 3;
-
- if( nPoints == 0 ) return 0;
-
- nPoints = min2( nPoints, 4 );
- return nPoints;
-
- }
- int extractManifoldSequential(const float4* p, int nPoints, float4 nearNormal, int* contactIdx)
- {
- if( nPoints == 0 ) return 0;
- nPoints = min2( nPoints, 64 );
- float4 center = make_float4(0.f);
- {
- float4 v[64];
- for (int i=0;i<nPoints;i++)
- v[i] = p[i];
- //memcpy( v, p, nPoints*sizeof(float4) );
- PARALLEL_SUM( v, nPoints );
- center = v[0]/(float)nPoints;
- }
-
- { // sample 4 directions
- if( nPoints < 4 )
- {
- for(int i=0; i<nPoints; i++)
- contactIdx[i] = i;
- return nPoints;
- }
- float4 aVector = p[0] - center;
- float4 u = cross3( nearNormal, aVector );
- float4 v = cross3( nearNormal, u );
- u = normalize3( u );
- v = normalize3( v );
- int idx[4];
- float2 max00 = make_float2(0,FLT_MAX);
- {
- // idx, distance
- {
- {
- int4 a[64];
- for(int ie = 0; ie<nPoints; ie++ )
- {
-
-
- float f;
- float4 r = p[ie]-center;
- f = dot3F4( u, r );
- a[ie].x = ((*(u32*)&f) & 0xffffff00) | (0xff & ie);
- f = dot3F4( -u, r );
- a[ie].y = ((*(u32*)&f) & 0xffffff00) | (0xff & ie);
- f = dot3F4( v, r );
- a[ie].z = ((*(u32*)&f) & 0xffffff00) | (0xff & ie);
- f = dot3F4( -v, r );
- a[ie].w = ((*(u32*)&f) & 0xffffff00) | (0xff & ie);
- }
- for(int ie=0; ie<nPoints; ie++)
- {
- a[0].x = (a[0].x > a[ie].x )? a[0].x: a[ie].x;
- a[0].y = (a[0].y > a[ie].y )? a[0].y: a[ie].y;
- a[0].z = (a[0].z > a[ie].z )? a[0].z: a[ie].z;
- a[0].w = (a[0].w > a[ie].w )? a[0].w: a[ie].w;
- }
- idx[0] = (int)a[0].x & 0xff;
- idx[1] = (int)a[0].y & 0xff;
- idx[2] = (int)a[0].z & 0xff;
- idx[3] = (int)a[0].w & 0xff;
- }
- }
- {
- float2 h[64];
- PARALLEL_DO( h[ie] = make_float2((float)ie, p[ie].w), nPoints );
- REDUCE_MIN( h, nPoints );
- max00 = h[0];
- }
- }
- contactIdx[0] = idx[0];
- contactIdx[1] = idx[1];
- contactIdx[2] = idx[2];
- contactIdx[3] = idx[3];
- return 4;
- }
- }
- __kernel void extractManifoldAndAddContactKernel(__global const int4* pairs,
- __global const b3RigidBodyData_t* rigidBodies,
- __global const float4* closestPointsWorld,
- __global const float4* separatingNormalsWorld,
- __global const int* contactCounts,
- __global const int* contactOffsets,
- __global struct b3Contact4Data* restrict contactsOut,
- counter32_t nContactsOut,
- int contactCapacity,
- int numPairs,
- int pairIndex
- )
- {
- int idx = get_global_id(0);
-
- if (idx<numPairs)
- {
- float4 normal = separatingNormalsWorld[idx];
- int nPoints = contactCounts[idx];
- __global const float4* pointsIn = &closestPointsWorld[contactOffsets[idx]];
- float4 localPoints[64];
- for (int i=0;i<nPoints;i++)
- {
- localPoints[i] = pointsIn[i];
- }
- int contactIdx[4];// = {-1,-1,-1,-1};
- contactIdx[0] = -1;
- contactIdx[1] = -1;
- contactIdx[2] = -1;
- contactIdx[3] = -1;
- int nContacts = extractManifoldSequential(localPoints, nPoints, normal, contactIdx);
- int dstIdx;
- AppendInc( nContactsOut, dstIdx );
- if (dstIdx<contactCapacity)
- {
- __global struct b3Contact4Data* c = contactsOut + dstIdx;
- c->m_worldNormalOnB = -normal;
- c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);
- c->m_batchIdx = idx;
- int bodyA = pairs[pairIndex].x;
- int bodyB = pairs[pairIndex].y;
- c->m_bodyAPtrAndSignBit = rigidBodies[bodyA].m_invMass==0 ? -bodyA:bodyA;
- c->m_bodyBPtrAndSignBit = rigidBodies[bodyB].m_invMass==0 ? -bodyB:bodyB;
- c->m_childIndexA = -1;
- c->m_childIndexB = -1;
- for (int i=0;i<nContacts;i++)
- {
- c->m_worldPosB[i] = localPoints[contactIdx[i]];
- }
- GET_NPOINTS(*c) = nContacts;
- }
- }
- }
- void trInverse(float4 translationIn, Quaternion orientationIn,
- float4* translationOut, Quaternion* orientationOut)
- {
- *orientationOut = qtInvert(orientationIn);
- *translationOut = qtRotate(*orientationOut, -translationIn);
- }
- void trMul(float4 translationA, Quaternion orientationA,
- float4 translationB, Quaternion orientationB,
- float4* translationOut, Quaternion* orientationOut)
- {
- *orientationOut = qtMul(orientationA,orientationB);
- *translationOut = transform(&translationB,&translationA,&orientationA);
- }
- __kernel void clipHullHullKernel( __global int4* pairs,
- __global const b3RigidBodyData_t* rigidBodies,
- __global const b3Collidable_t* collidables,
- __global const b3ConvexPolyhedronData_t* convexShapes,
- __global const float4* vertices,
- __global const float4* uniqueEdges,
- __global const b3GpuFace_t* faces,
- __global const int* indices,
- __global const float4* separatingNormals,
- __global const int* hasSeparatingAxis,
- __global struct b3Contact4Data* restrict globalContactsOut,
- counter32_t nGlobalContactsOut,
- int numPairs,
- int contactCapacity)
- {
- int i = get_global_id(0);
- int pairIndex = i;
-
- float4 worldVertsB1[64];
- float4 worldVertsB2[64];
- int capacityWorldVerts = 64;
- float4 localContactsOut[64];
- int localContactCapacity=64;
-
- float minDist = -1e30f;
- float maxDist = 0.02f;
- if (i<numPairs)
- {
- int bodyIndexA = pairs[i].x;
- int bodyIndexB = pairs[i].y;
-
- int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
- int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
- if (hasSeparatingAxis[i])
- {
-
- int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
- int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
-
-
- int numLocalContactsOut = clipHullAgainstHull(separatingNormals[i],
- &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],
- rigidBodies[bodyIndexA].m_pos,rigidBodies[bodyIndexA].m_quat,
- rigidBodies[bodyIndexB].m_pos,rigidBodies[bodyIndexB].m_quat,
- worldVertsB1,worldVertsB2,capacityWorldVerts,
- minDist, maxDist,
- vertices,faces,indices,
- localContactsOut,localContactCapacity);
-
- if (numLocalContactsOut>0)
- {
- float4 normal = -separatingNormals[i];
- int nPoints = numLocalContactsOut;
- float4* pointsIn = localContactsOut;
- int contactIdx[4];// = {-1,-1,-1,-1};
- contactIdx[0] = -1;
- contactIdx[1] = -1;
- contactIdx[2] = -1;
- contactIdx[3] = -1;
-
- int nReducedContacts = extractManifoldSequential(pointsIn, nPoints, normal, contactIdx);
-
-
- int mprContactIndex = pairs[pairIndex].z;
- int dstIdx = mprContactIndex;
- if (dstIdx<0)
- {
- AppendInc( nGlobalContactsOut, dstIdx );
- }
- if (dstIdx<contactCapacity)
- {
- pairs[pairIndex].z = dstIdx;
- __global struct b3Contact4Data* c = globalContactsOut+ dstIdx;
- c->m_worldNormalOnB = -normal;
- c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);
- c->m_batchIdx = pairIndex;
- int bodyA = pairs[pairIndex].x;
- int bodyB = pairs[pairIndex].y;
- c->m_bodyAPtrAndSignBit = rigidBodies[bodyA].m_invMass==0?-bodyA:bodyA;
- c->m_bodyBPtrAndSignBit = rigidBodies[bodyB].m_invMass==0?-bodyB:bodyB;
- c->m_childIndexA = -1;
- c->m_childIndexB = -1;
- for (int i=0;i<nReducedContacts;i++)
- {
- //this condition means: overwrite contact point, unless at index i==0 we have a valid 'mpr' contact
- if (i>0||(mprContactIndex<0))
- {
- c->m_worldPosB[i] = pointsIn[contactIdx[i]];
- }
- }
- GET_NPOINTS(*c) = nReducedContacts;
- }
-
- }// if (numContactsOut>0)
- }// if (hasSeparatingAxis[i])
- }// if (i<numPairs)
- }
- __kernel void clipCompoundsHullHullKernel( __global const int4* gpuCompoundPairs,
- __global const b3RigidBodyData_t* rigidBodies,
- __global const b3Collidable_t* collidables,
- __global const b3ConvexPolyhedronData_t* convexShapes,
- __global const float4* vertices,
- __global const float4* uniqueEdges,
- __global const b3GpuFace_t* faces,
- __global const int* indices,
- __global const b3GpuChildShape_t* gpuChildShapes,
- __global const float4* gpuCompoundSepNormalsOut,
- __global const int* gpuHasCompoundSepNormalsOut,
- __global struct b3Contact4Data* restrict globalContactsOut,
- counter32_t nGlobalContactsOut,
- int numCompoundPairs, int maxContactCapacity)
- {
- int i = get_global_id(0);
- int pairIndex = i;
-
- float4 worldVertsB1[64];
- float4 worldVertsB2[64];
- int capacityWorldVerts = 64;
- float4 localContactsOut[64];
- int localContactCapacity=64;
-
- float minDist = -1e30f;
- float maxDist = 0.02f;
- if (i<numCompoundPairs)
- {
- if (gpuHasCompoundSepNormalsOut[i])
- {
- int bodyIndexA = gpuCompoundPairs[i].x;
- int bodyIndexB = gpuCompoundPairs[i].y;
-
- int childShapeIndexA = gpuCompoundPairs[i].z;
- int childShapeIndexB = gpuCompoundPairs[i].w;
-
- int collidableIndexA = -1;
- int collidableIndexB = -1;
-
- float4 ornA = rigidBodies[bodyIndexA].m_quat;
- float4 posA = rigidBodies[bodyIndexA].m_pos;
-
- float4 ornB = rigidBodies[bodyIndexB].m_quat;
- float4 posB = rigidBodies[bodyIndexB].m_pos;
-
- if (childShapeIndexA >= 0)
- {
- collidableIndexA = gpuChildShapes[childShapeIndexA].m_shapeIndex;
- float4 childPosA = gpuChildShapes[childShapeIndexA].m_childPosition;
- float4 childOrnA = gpuChildShapes[childShapeIndexA].m_childOrientation;
- float4 newPosA = qtRotate(ornA,childPosA)+posA;
- float4 newOrnA = qtMul(ornA,childOrnA);
- posA = newPosA;
- ornA = newOrnA;
- } else
- {
- collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
- }
-
- if (childShapeIndexB>=0)
- {
- collidableIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;
- float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;
- float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;
- float4 newPosB = transform(&childPosB,&posB,&ornB);
- float4 newOrnB = qtMul(ornB,childOrnB);
- posB = newPosB;
- ornB = newOrnB;
- } else
- {
- collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
- }
-
- int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
- int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
-
- int numLocalContactsOut = clipHullAgainstHull(gpuCompoundSepNormalsOut[i],
- &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],
- posA,ornA,
- posB,ornB,
- worldVertsB1,worldVertsB2,capacityWorldVerts,
- minDist, maxDist,
- vertices,faces,indices,
- localContactsOut,localContactCapacity);
-
- if (numLocalContactsOut>0)
- {
- float4 normal = -gpuCompoundSepNormalsOut[i];
- int nPoints = numLocalContactsOut;
- float4* pointsIn = localContactsOut;
- int contactIdx[4];// = {-1,-1,-1,-1};
- contactIdx[0] = -1;
- contactIdx[1] = -1;
- contactIdx[2] = -1;
- contactIdx[3] = -1;
-
- int nReducedContacts = extractManifoldSequential(pointsIn, nPoints, normal, contactIdx);
-
- int dstIdx;
- AppendInc( nGlobalContactsOut, dstIdx );
- if ((dstIdx+nReducedContacts) < maxContactCapacity)
- {
- __global struct b3Contact4Data* c = globalContactsOut+ dstIdx;
- c->m_worldNormalOnB = -normal;
- c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);
- c->m_batchIdx = pairIndex;
- int bodyA = gpuCompoundPairs[pairIndex].x;
- int bodyB = gpuCompoundPairs[pairIndex].y;
- c->m_bodyAPtrAndSignBit = rigidBodies[bodyA].m_invMass==0?-bodyA:bodyA;
- c->m_bodyBPtrAndSignBit = rigidBodies[bodyB].m_invMass==0?-bodyB:bodyB;
- c->m_childIndexA = childShapeIndexA;
- c->m_childIndexB = childShapeIndexB;
- for (int i=0;i<nReducedContacts;i++)
- {
- c->m_worldPosB[i] = pointsIn[contactIdx[i]];
- }
- GET_NPOINTS(*c) = nReducedContacts;
- }
-
- }// if (numContactsOut>0)
- }// if (gpuHasCompoundSepNormalsOut[i])
- }// if (i<numCompoundPairs)
- }
- __kernel void sphereSphereCollisionKernel( __global const int4* pairs,
- __global const b3RigidBodyData_t* rigidBodies,
- __global const b3Collidable_t* collidables,
- __global const float4* separatingNormals,
- __global const int* hasSeparatingAxis,
- __global struct b3Contact4Data* restrict globalContactsOut,
- counter32_t nGlobalContactsOut,
- int contactCapacity,
- int numPairs)
- {
- int i = get_global_id(0);
- int pairIndex = i;
-
- if (i<numPairs)
- {
- int bodyIndexA = pairs[i].x;
- int bodyIndexB = pairs[i].y;
-
- int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
- int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
- if (collidables[collidableIndexA].m_shapeType == SHAPE_SPHERE &&
- collidables[collidableIndexB].m_shapeType == SHAPE_SPHERE)
- {
- //sphere-sphere
- float radiusA = collidables[collidableIndexA].m_radius;
- float radiusB = collidables[collidableIndexB].m_radius;
- float4 posA = rigidBodies[bodyIndexA].m_pos;
- float4 posB = rigidBodies[bodyIndexB].m_pos;
- float4 diff = posA-posB;
- float len = length(diff);
-
- ///iff distance positive, don't generate a new contact
- if ( len <= (radiusA+radiusB))
- {
- ///distance (negative means penetration)
- float dist = len - (radiusA+radiusB);
- float4 normalOnSurfaceB = make_float4(1.f,0.f,0.f,0.f);
- if (len > 0.00001)
- {
- normalOnSurfaceB = diff / len;
- }
- float4 contactPosB = posB + normalOnSurfaceB*radiusB;
- contactPosB.w = dist;
-
- int dstIdx;
- AppendInc( nGlobalContactsOut, dstIdx );
- if (dstIdx < contactCapacity)
- {
- __global struct b3Contact4Data* c = &globalContactsOut[dstIdx];
- c->m_worldNormalOnB = -normalOnSurfaceB;
- c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);
- c->m_batchIdx = pairIndex;
- int bodyA = pairs[pairIndex].x;
- int bodyB = pairs[pairIndex].y;
- c->m_bodyAPtrAndSignBit = rigidBodies[bodyA].m_invMass==0?-bodyA:bodyA;
- c->m_bodyBPtrAndSignBit = rigidBodies[bodyB].m_invMass==0?-bodyB:bodyB;
- c->m_worldPosB[0] = contactPosB;
- c->m_childIndexA = -1;
- c->m_childIndexB = -1;
- GET_NPOINTS(*c) = 1;
- }//if (dstIdx < numPairs)
- }//if ( len <= (radiusA+radiusB))
- }//SHAPE_SPHERE SHAPE_SPHERE
- }//if (i<numPairs)
- }
- __kernel void clipHullHullConcaveConvexKernel( __global int4* concavePairsIn,
- __global const b3RigidBodyData_t* rigidBodies,
- __global const b3Collidable_t* collidables,
- __global const b3ConvexPolyhedronData_t* convexShapes,
- __global const float4* vertices,
- __global const float4* uniqueEdges,
- __global const b3GpuFace_t* faces,
- __global const int* indices,
- __global const b3GpuChildShape_t* gpuChildShapes,
- __global const float4* separatingNormals,
- __global struct b3Contact4Data* restrict globalContactsOut,
- counter32_t nGlobalContactsOut,
- int contactCapacity,
- int numConcavePairs)
- {
- int i = get_global_id(0);
- int pairIndex = i;
-
- float4 worldVertsB1[64];
- float4 worldVertsB2[64];
- int capacityWorldVerts = 64;
- float4 localContactsOut[64];
- int localContactCapacity=64;
-
- float minDist = -1e30f;
- float maxDist = 0.02f;
- if (i<numConcavePairs)
- {
- //negative value means that the pair is invalid
- if (concavePairsIn[i].w<0)
- return;
- int bodyIndexA = concavePairsIn[i].x;
- int bodyIndexB = concavePairsIn[i].y;
- int f = concavePairsIn[i].z;
- int childShapeIndexA = f;
-
- int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
- int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
-
- int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
- int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
-
- ///////////////////////////////////////////////////////////////
-
-
- bool overlap = false;
-
- b3ConvexPolyhedronData_t convexPolyhedronA;
- //add 3 vertices of the triangle
- convexPolyhedronA.m_numVertices = 3;
- convexPolyhedronA.m_vertexOffset = 0;
- float4 localCenter = make_float4(0.f,0.f,0.f,0.f);
- b3GpuFace_t face = faces[convexShapes[shapeIndexA].m_faceOffset+f];
-
- float4 verticesA[3];
- for (int i=0;i<3;i++)
- {
- int index = indices[face.m_indexOffset+i];
- float4 vert = vertices[convexShapes[shapeIndexA].m_vertexOffset+index];
- verticesA[i] = vert;
- localCenter += vert;
- }
- float dmin = FLT_MAX;
- int localCC=0;
- //a triangle has 3 unique edges
- convexPolyhedronA.m_numUniqueEdges = 3;
- convexPolyhedronA.m_uniqueEdgesOffset = 0;
- float4 uniqueEdgesA[3];
-
- uniqueEdgesA[0] = (verticesA[1]-verticesA[0]);
- uniqueEdgesA[1] = (verticesA[2]-verticesA[1]);
- uniqueEdgesA[2] = (verticesA[0]-verticesA[2]);
- convexPolyhedronA.m_faceOffset = 0;
-
- float4 normal = make_float4(face.m_plane.x,face.m_plane.y,face.m_plane.z,0.f);
-
- b3GpuFace_t facesA[TRIANGLE_NUM_CONVEX_FACES];
- int indicesA[3+3+2+2+2];
- int curUsedIndices=0;
- int fidx=0;
- //front size of triangle
- {
- facesA[fidx].m_indexOffset=curUsedIndices;
- indicesA[0] = 0;
- indicesA[1] = 1;
- indicesA[2] = 2;
- curUsedIndices+=3;
- float c = face.m_plane.w;
- facesA[fidx].m_plane.x = normal.x;
- facesA[fidx].m_plane.y = normal.y;
- facesA[fidx].m_plane.z = normal.z;
- facesA[fidx].m_plane.w = c;
- facesA[fidx].m_numIndices=3;
- }
- fidx++;
- //back size of triangle
- {
- facesA[fidx].m_indexOffset=curUsedIndices;
- indicesA[3]=2;
- indicesA[4]=1;
- indicesA[5]=0;
- curUsedIndices+=3;
- float c = dot3F4(normal,verticesA[0]);
- float c1 = -face.m_plane.w;
- facesA[fidx].m_plane.x = -normal.x;
- facesA[fidx].m_plane.y = -normal.y;
- facesA[fidx].m_plane.z = -normal.z;
- facesA[fidx].m_plane.w = c;
- facesA[fidx].m_numIndices=3;
- }
- fidx++;
- bool addEdgePlanes = true;
- if (addEdgePlanes)
- {
- int numVertices=3;
- int prevVertex = numVertices-1;
- for (int i=0;i<numVertices;i++)
- {
- float4 v0 = verticesA[i];
- float4 v1 = verticesA[prevVertex];
-
- float4 edgeNormal = normalize(cross(normal,v1-v0));
- float c = -dot3F4(edgeNormal,v0);
- facesA[fidx].m_numIndices = 2;
- facesA[fidx].m_indexOffset=curUsedIndices;
- indicesA[curUsedIndices++]=i;
- indicesA[curUsedIndices++]=prevVertex;
-
- facesA[fidx].m_plane.x = edgeNormal.x;
- facesA[fidx].m_plane.y = edgeNormal.y;
- facesA[fidx].m_plane.z = edgeNormal.z;
- facesA[fidx].m_plane.w = c;
- fidx++;
- prevVertex = i;
- }
- }
- convexPolyhedronA.m_numFaces = TRIANGLE_NUM_CONVEX_FACES;
- convexPolyhedronA.m_localCenter = localCenter*(1.f/3.f);
- float4 posA = rigidBodies[bodyIndexA].m_pos;
- posA.w = 0.f;
- float4 posB = rigidBodies[bodyIndexB].m_pos;
- posB.w = 0.f;
- float4 ornA = rigidBodies[bodyIndexA].m_quat;
- float4 ornB =rigidBodies[bodyIndexB].m_quat;
- float4 sepAxis = separatingNormals[i];
-
- int shapeTypeB = collidables[collidableIndexB].m_shapeType;
- int childShapeIndexB =-1;
- if (shapeTypeB==SHAPE_COMPOUND_OF_CONVEX_HULLS)
- {
- ///////////////////
- ///compound shape support
-
- childShapeIndexB = concavePairsIn[pairIndex].w;
- int childColIndexB = gpuChildShapes[childShapeIndexB].m_shapeIndex;
- shapeIndexB = collidables[childColIndexB].m_shapeIndex;
- float4 childPosB = gpuChildShapes[childShapeIndexB].m_childPosition;
- float4 childOrnB = gpuChildShapes[childShapeIndexB].m_childOrientation;
- float4 newPosB = transform(&childPosB,&posB,&ornB);
- float4 newOrnB = qtMul(ornB,childOrnB);
- posB = newPosB;
- ornB = newOrnB;
-
- }
-
- ////////////////////////////////////////
-
-
-
- int numLocalContactsOut = clipHullAgainstHullLocalA(sepAxis,
- &convexPolyhedronA, &convexShapes[shapeIndexB],
- posA,ornA,
- posB,ornB,
- worldVertsB1,worldVertsB2,capacityWorldVerts,
- minDist, maxDist,
- &verticesA,&facesA,&indicesA,
- vertices,faces,indices,
- localContactsOut,localContactCapacity);
-
- if (numLocalContactsOut>0)
- {
- float4 normal = -separatingNormals[i];
- int nPoints = numLocalContactsOut;
- float4* pointsIn = localContactsOut;
- int contactIdx[4];// = {-1,-1,-1,-1};
- contactIdx[0] = -1;
- contactIdx[1] = -1;
- contactIdx[2] = -1;
- contactIdx[3] = -1;
-
- int nReducedContacts = extractManifoldSequential(pointsIn, nPoints, normal, contactIdx);
-
- int dstIdx;
- AppendInc( nGlobalContactsOut, dstIdx );
- if (dstIdx<contactCapacity)
- {
- __global struct b3Contact4Data* c = globalContactsOut+ dstIdx;
- c->m_worldNormalOnB = -normal;
- c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);
- c->m_batchIdx = pairIndex;
- int bodyA = concavePairsIn[pairIndex].x;
- int bodyB = concavePairsIn[pairIndex].y;
- c->m_bodyAPtrAndSignBit = rigidBodies[bodyA].m_invMass==0?-bodyA:bodyA;
- c->m_bodyBPtrAndSignBit = rigidBodies[bodyB].m_invMass==0?-bodyB:bodyB;
- c->m_childIndexA = childShapeIndexA;
- c->m_childIndexB = childShapeIndexB;
- for (int i=0;i<nReducedContacts;i++)
- {
- c->m_worldPosB[i] = pointsIn[contactIdx[i]];
- }
- GET_NPOINTS(*c) = nReducedContacts;
- }
-
- }// if (numContactsOut>0)
- }// if (i<numPairs)
- }
- int findClippingFaces(const float4 separatingNormal,
- __global const b3ConvexPolyhedronData_t* hullA, __global const b3ConvexPolyhedronData_t* hullB,
- const float4 posA, const Quaternion ornA,const float4 posB, const Quaternion ornB,
- __global float4* worldVertsA1,
- __global float4* worldNormalsA1,
- __global float4* worldVertsB1,
- int capacityWorldVerts,
- const float minDist, float maxDist,
- __global const float4* vertices,
- __global const b3GpuFace_t* faces,
- __global const int* indices,
- __global int4* clippingFaces, int pairIndex)
- {
- int numContactsOut = 0;
- int numWorldVertsB1= 0;
-
-
- int closestFaceB=-1;
- float dmax = -FLT_MAX;
-
- {
- for(int face=0;face<hullB->m_numFaces;face++)
- {
- const float4 Normal = make_float4(faces[hullB->m_faceOffset+face].m_plane.x,
- faces[hullB->m_faceOffset+face].m_plane.y, faces[hullB->m_faceOffset+face].m_plane.z,0.f);
- const float4 WorldNormal = qtRotate(ornB, Normal);
- float d = dot3F4(WorldNormal,separatingNormal);
- if (d > dmax)
- {
- dmax = d;
- closestFaceB = face;
- }
- }
- }
-
- {
- const b3GpuFace_t polyB = faces[hullB->m_faceOffset+closestFaceB];
- const int numVertices = polyB.m_numIndices;
- for(int e0=0;e0<numVertices;e0++)
- {
- const float4 b = vertices[hullB->m_vertexOffset+indices[polyB.m_indexOffset+e0]];
- worldVertsB1[pairIndex*capacityWorldVerts+numWorldVertsB1++] = transform(&b,&posB,&ornB);
- }
- }
-
- int closestFaceA=-1;
- {
- float dmin = FLT_MAX;
- for(int face=0;face<hullA->m_numFaces;face++)
- {
- const float4 Normal = make_float4(
- faces[hullA->m_faceOffset+face].m_plane.x,
- faces[hullA->m_faceOffset+face].m_plane.y,
- faces[hullA->m_faceOffset+face].m_plane.z,
- 0.f);
- const float4 faceANormalWS = qtRotate(ornA,Normal);
-
- float d = dot3F4(faceANormalWS,separatingNormal);
- if (d < dmin)
- {
- dmin = d;
- closestFaceA = face;
- worldNormalsA1[pairIndex] = faceANormalWS;
- }
- }
- }
-
- int numVerticesA = faces[hullA->m_faceOffset+closestFaceA].m_numIndices;
- for(int e0=0;e0<numVerticesA;e0++)
- {
- const float4 a = vertices[hullA->m_vertexOffset+indices[faces[hullA->m_faceOffset+closestFaceA].m_indexOffset+e0]];
- worldVertsA1[pairIndex*capacityWorldVerts+e0] = transform(&a, &posA,&ornA);
- }
-
- clippingFaces[pairIndex].x = closestFaceA;
- clippingFaces[pairIndex].y = closestFaceB;
- clippingFaces[pairIndex].z = numVerticesA;
- clippingFaces[pairIndex].w = numWorldVertsB1;
-
-
- return numContactsOut;
- }
- int clipFaces(__global float4* worldVertsA1,
- __global float4* worldNormalsA1,
- __global float4* worldVertsB1,
- __global float4* worldVertsB2,
- int capacityWorldVertsB2,
- const float minDist, float maxDist,
- __global int4* clippingFaces,
- int pairIndex)
- {
- int numContactsOut = 0;
-
- int closestFaceA = clippingFaces[pairIndex].x;
- int closestFaceB = clippingFaces[pairIndex].y;
- int numVertsInA = clippingFaces[pairIndex].z;
- int numVertsInB = clippingFaces[pairIndex].w;
-
- int numVertsOut = 0;
-
- if (closestFaceA<0)
- return numContactsOut;
-
- __global float4* pVtxIn = &worldVertsB1[pairIndex*capacityWorldVertsB2];
- __global float4* pVtxOut = &worldVertsB2[pairIndex*capacityWorldVertsB2];
-
-
-
- // clip polygon to back of planes of all faces of hull A that are adjacent to witness face
-
- for(int e0=0;e0<numVertsInA;e0++)
- {
- const float4 aw = worldVertsA1[pairIndex*capacityWorldVertsB2+e0];
- const float4 bw = worldVertsA1[pairIndex*capacityWorldVertsB2+((e0+1)%numVertsInA)];
- const float4 WorldEdge0 = aw - bw;
- float4 worldPlaneAnormal1 = worldNormalsA1[pairIndex];
- float4 planeNormalWS1 = -cross3(WorldEdge0,worldPlaneAnormal1);
- float4 worldA1 = aw;
- float planeEqWS1 = -dot3F4(worldA1,planeNormalWS1);
- float4 planeNormalWS = planeNormalWS1;
- float planeEqWS=planeEqWS1;
- numVertsOut = clipFaceGlobal(pVtxIn, numVertsInB, planeNormalWS,planeEqWS, pVtxOut);
- __global float4* tmp = pVtxOut;
- pVtxOut = pVtxIn;
- pVtxIn = tmp;
- numVertsInB = numVertsOut;
- numVertsOut = 0;
- }
-
- //float4 planeNormalWS = worldNormalsA1[pairIndex];
- //float planeEqWS=-dot3F4(planeNormalWS,worldVertsA1[pairIndex*capacityWorldVertsB2]);
-
- /*for (int i=0;i<numVertsInB;i++)
- {
- pVtxOut[i] = pVtxIn[i];
- }*/
-
-
-
-
- //numVertsInB=0;
-
- float4 planeNormalWS = worldNormalsA1[pairIndex];
- float planeEqWS=-dot3F4(planeNormalWS,worldVertsA1[pairIndex*capacityWorldVertsB2]);
- for (int i=0;i<numVertsInB;i++)
- {
- float depth = dot3F4(planeNormalWS,pVtxIn[i])+planeEqWS;
- if (depth <=minDist)
- {
- depth = minDist;
- }
-
- if (depth <=maxDist)
- {
- float4 pointInWorld = pVtxIn[i];
- pVtxOut[numContactsOut++] = make_float4(pointInWorld.x,pointInWorld.y,pointInWorld.z,depth);
- }
- }
-
- clippingFaces[pairIndex].w =numContactsOut;
-
-
- return numContactsOut;
- }
- __kernel void findClippingFacesKernel( __global const int4* pairs,
- __global const b3RigidBodyData_t* rigidBodies,
- __global const b3Collidable_t* collidables,
- __global const b3ConvexPolyhedronData_t* convexShapes,
- __global const float4* vertices,
- __global const float4* uniqueEdges,
- __global const b3GpuFace_t* faces,
- __global const int* indices,
- __global const float4* separatingNormals,
- __global const int* hasSeparatingAxis,
- __global int4* clippingFacesOut,
- __global float4* worldVertsA1,
- __global float4* worldNormalsA1,
- __global float4* worldVertsB1,
- int capacityWorldVerts,
- int numPairs
- )
- {
-
- int i = get_global_id(0);
- int pairIndex = i;
-
-
- float minDist = -1e30f;
- float maxDist = 0.02f;
-
- if (i<numPairs)
- {
-
- if (hasSeparatingAxis[i])
- {
-
- int bodyIndexA = pairs[i].x;
- int bodyIndexB = pairs[i].y;
-
- int collidableIndexA = rigidBodies[bodyIndexA].m_collidableIdx;
- int collidableIndexB = rigidBodies[bodyIndexB].m_collidableIdx;
-
- int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
- int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
-
-
-
- int numLocalContactsOut = findClippingFaces(separatingNormals[i],
- &convexShapes[shapeIndexA], &convexShapes[shapeIndexB],
- rigidBodies[bodyIndexA].m_pos,rigidBodies[bodyIndexA].m_quat,
- rigidBodies[bodyIndexB].m_pos,rigidBodies[bodyIndexB].m_quat,
- worldVertsA1,
- worldNormalsA1,
- worldVertsB1,capacityWorldVerts,
- minDist, maxDist,
- vertices,faces,indices,
- clippingFacesOut,i);
-
-
- }// if (hasSeparatingAxis[i])
- }// if (i<numPairs)
-
- }
- __kernel void clipFacesAndFindContactsKernel( __global const float4* separatingNormals,
- __global const int* hasSeparatingAxis,
- __global int4* clippingFacesOut,
- __global float4* worldVertsA1,
- __global float4* worldNormalsA1,
- __global float4* worldVertsB1,
- __global float4* worldVertsB2,
- int vertexFaceCapacity,
- int numPairs,
- int debugMode
- )
- {
- int i = get_global_id(0);
- int pairIndex = i;
-
-
- float minDist = -1e30f;
- float maxDist = 0.02f;
-
- if (i<numPairs)
- {
-
- if (hasSeparatingAxis[i])
- {
-
- // int bodyIndexA = pairs[i].x;
- // int bodyIndexB = pairs[i].y;
-
- int numLocalContactsOut = 0;
- int capacityWorldVertsB2 = vertexFaceCapacity;
-
- __global float4* pVtxIn = &worldVertsB1[pairIndex*capacityWorldVertsB2];
- __global float4* pVtxOut = &worldVertsB2[pairIndex*capacityWorldVertsB2];
-
- {
- __global int4* clippingFaces = clippingFacesOut;
-
-
- int closestFaceA = clippingFaces[pairIndex].x;
- int closestFaceB = clippingFaces[pairIndex].y;
- int numVertsInA = clippingFaces[pairIndex].z;
- int numVertsInB = clippingFaces[pairIndex].w;
-
- int numVertsOut = 0;
-
- if (closestFaceA>=0)
- {
-
-
-
- // clip polygon to back of planes of all faces of hull A that are adjacent to witness face
-
- for(int e0=0;e0<numVertsInA;e0++)
- {
- const float4 aw = worldVertsA1[pairIndex*capacityWorldVertsB2+e0];
- const float4 bw = worldVertsA1[pairIndex*capacityWorldVertsB2+((e0+1)%numVertsInA)];
- const float4 WorldEdge0 = aw - bw;
- float4 worldPlaneAnormal1 = worldNormalsA1[pairIndex];
- float4 planeNormalWS1 = -cross3(WorldEdge0,worldPlaneAnormal1);
- float4 worldA1 = aw;
- float planeEqWS1 = -dot3F4(worldA1,planeNormalWS1);
- float4 planeNormalWS = planeNormalWS1;
- float planeEqWS=planeEqWS1;
- numVertsOut = clipFaceGlobal(pVtxIn, numVertsInB, planeNormalWS,planeEqWS, pVtxOut);
- __global float4* tmp = pVtxOut;
- pVtxOut = pVtxIn;
- pVtxIn = tmp;
- numVertsInB = numVertsOut;
- numVertsOut = 0;
- }
-
- float4 planeNormalWS = worldNormalsA1[pairIndex];
- float planeEqWS=-dot3F4(planeNormalWS,worldVertsA1[pairIndex*capacityWorldVertsB2]);
-
- for (int i=0;i<numVertsInB;i++)
- {
- float depth = dot3F4(planeNormalWS,pVtxIn[i])+planeEqWS;
- if (depth <=minDist)
- {
- depth = minDist;
- }
-
- if (depth <=maxDist)
- {
- float4 pointInWorld = pVtxIn[i];
- pVtxOut[numLocalContactsOut++] = make_float4(pointInWorld.x,pointInWorld.y,pointInWorld.z,depth);
- }
- }
-
- }
- clippingFaces[pairIndex].w =numLocalContactsOut;
-
- }
-
- for (int i=0;i<numLocalContactsOut;i++)
- pVtxIn[i] = pVtxOut[i];
-
- }// if (hasSeparatingAxis[i])
- }// if (i<numPairs)
-
- }
- __kernel void newContactReductionKernel( __global int4* pairs,
- __global const b3RigidBodyData_t* rigidBodies,
- __global const float4* separatingNormals,
- __global const int* hasSeparatingAxis,
- __global struct b3Contact4Data* globalContactsOut,
- __global int4* clippingFaces,
- __global float4* worldVertsB2,
- volatile __global int* nGlobalContactsOut,
- int vertexFaceCapacity,
- int contactCapacity,
- int numPairs
- )
- {
- int i = get_global_id(0);
- int pairIndex = i;
-
- int4 contactIdx;
- contactIdx=make_int4(0,1,2,3);
-
- if (i<numPairs)
- {
-
- if (hasSeparatingAxis[i])
- {
-
-
-
-
- int nPoints = clippingFaces[pairIndex].w;
-
- if (nPoints>0)
- {
- __global float4* pointsIn = &worldVertsB2[pairIndex*vertexFaceCapacity];
- float4 normal = -separatingNormals[i];
-
- int nReducedContacts = extractManifoldSequentialGlobal(pointsIn, nPoints, normal, &contactIdx);
-
- int mprContactIndex = pairs[pairIndex].z;
- int dstIdx = mprContactIndex;
- if (dstIdx<0)
- {
- AppendInc( nGlobalContactsOut, dstIdx );
- }
- //#if 0
-
- if (dstIdx < contactCapacity)
- {
- __global struct b3Contact4Data* c = &globalContactsOut[dstIdx];
- c->m_worldNormalOnB = -normal;
- c->m_restituitionCoeffCmp = (0.f*0xffff);c->m_frictionCoeffCmp = (0.7f*0xffff);
- c->m_batchIdx = pairIndex;
- int bodyA = pairs[pairIndex].x;
- int bodyB = pairs[pairIndex].y;
- pairs[pairIndex].w = dstIdx;
- c->m_bodyAPtrAndSignBit = rigidBodies[bodyA].m_invMass==0?-bodyA:bodyA;
- c->m_bodyBPtrAndSignBit = rigidBodies[bodyB].m_invMass==0?-bodyB:bodyB;
- c->m_childIndexA =-1;
- c->m_childIndexB =-1;
- switch (nReducedContacts)
- {
- case 4:
- c->m_worldPosB[3] = pointsIn[contactIdx.w];
- case 3:
- c->m_worldPosB[2] = pointsIn[contactIdx.z];
- case 2:
- c->m_worldPosB[1] = pointsIn[contactIdx.y];
- case 1:
- if (mprContactIndex<0)//test
- c->m_worldPosB[0] = pointsIn[contactIdx.x];
- default:
- {
- }
- };
-
- GET_NPOINTS(*c) = nReducedContacts;
-
- }
-
-
- //#endif
-
- }// if (numContactsOut>0)
- }// if (hasSeparatingAxis[i])
- }// if (i<numPairs)
-
-
- }
|