avoid out-of-bounds issue for some OpenCL kernel, hanging Mac OSX (should not happen, need to check why)

split kernel for debugging
This commit is contained in:
Erwin Coumans
2013-12-17 10:44:41 -08:00
parent 7b55ffd237
commit 7e86932edf
5 changed files with 1548 additions and 233 deletions

View File

@@ -1381,27 +1381,21 @@ __kernel void findSeparatingAxisVertexFaceKernel( __global const int4* pairs,
int shapeIndexA = collidables[collidableIndexA].m_shapeIndex;
int shapeIndexB = collidables[collidableIndexB].m_shapeIndex;
hasSeparatingAxis[i] = 0;
//once the broadphase avoids static-static pairs, we can remove this test
if ((rigidBodies[bodyIndexA].m_invMass==0) &&(rigidBodies[bodyIndexB].m_invMass==0))
{
hasSeparatingAxis[i] = 0;
return;
}
if ((collidables[collidableIndexA].m_shapeType!=SHAPE_CONVEX_HULL) ||(collidables[collidableIndexB].m_shapeType!=SHAPE_CONVEX_HULL))
{
hasSeparatingAxis[i] = 0;
return;
}
if ((collidables[collidableIndexA].m_shapeType==SHAPE_CONCAVE_TRIMESH))
{
hasSeparatingAxis[i] = 0;
return;
}
int numFacesA = convexShapes[shapeIndexA].m_numFaces;
@@ -1524,7 +1518,7 @@ __kernel void findSeparatingAxisEdgeEdgeKernel( __global const int4* pairs,
int findClippingFaces(const float4 separatingNormal,
inline int findClippingFaces(const float4 separatingNormal,
const ConvexPolyhedronCL* hullA,
__global const ConvexPolyhedronCL* hullB,
const float4 posA, const Quaternion ornA,const float4 posB, const Quaternion ornB,
@@ -1565,11 +1559,17 @@ int findClippingFaces(const float4 separatingNormal,
{
const btGpuFace polyB = facesB[hullB->m_faceOffset+closestFaceB];
const int numVertices = polyB.m_numIndices;
int numVertices = polyB.m_numIndices;
if (numVertices>capacityWorldVerts)
numVertices = capacityWorldVerts;
for(int e0=0;e0<numVertices;e0++)
{
const float4 b = verticesB[hullB->m_vertexOffset+indicesB[polyB.m_indexOffset+e0]];
worldVertsB1[pairIndex*capacityWorldVerts+numWorldVertsB1++] = transform(&b,&posB,&ornB);
if (e0<capacityWorldVerts)
{
const float4 b = verticesB[hullB->m_vertexOffset+indicesB[polyB.m_indexOffset+e0]];
worldVertsB1[pairIndex*capacityWorldVerts+numWorldVertsB1++] = transform(&b,&posB,&ornB);
}
}
}
@@ -1596,10 +1596,16 @@ int findClippingFaces(const float4 separatingNormal,
}
int numVerticesA = facesA[hullA->m_faceOffset+closestFaceA].m_numIndices;
if (numVerticesA>capacityWorldVerts)
numVerticesA = capacityWorldVerts;
for(int e0=0;e0<numVerticesA;e0++)
{
const float4 a = verticesA[hullA->m_vertexOffset+indicesA[facesA[hullA->m_faceOffset+closestFaceA].m_indexOffset+e0]];
worldVertsA1[pairIndex*capacityWorldVerts+e0] = transform(&a, &posA,&ornA);
if (e0<capacityWorldVerts)
{
const float4 a = verticesA[hullA->m_vertexOffset+indicesA[facesA[hullA->m_faceOffset+closestFaceA].m_indexOffset+e0]];
worldVertsA1[pairIndex*capacityWorldVerts+e0] = transform(&a, &posA,&ornA);
}
}
clippingFaces[pairIndex].x = closestFaceA;
@@ -1913,3 +1919,543 @@ __kernel void findConcaveSeparatingAxisKernel( __global int4* concavePairs,
concavePairs[pairIdx].w = -1;
}
}
// work-in-progress
__kernel void findConcaveSeparatingAxisVertexFaceKernel( __global int4* concavePairs,
__global const BodyData* rigidBodies,
__global const btCollidableGpu* collidables,
__global const ConvexPolyhedronCL* convexShapes,
__global const float4* vertices,
__global const float4* uniqueEdges,
__global const btGpuFace* faces,
__global const int* indices,
__global const btGpuChildShape* gpuChildShapes,
__global btAabbCL* aabbs,
__global float4* concaveSeparatingNormalsOut,
__global int* concaveHasSeparatingNormals,
__global int4* clippingFacesOut,
__global float4* worldVertsA1GPU,
__global float4* worldNormalsAGPU,
__global float4* worldVertsB1GPU,
__global float* dmins,
int vertexFaceCapacity,
int numConcavePairs
)
{
int i = get_global_id(0);
if (i>=numConcavePairs)
return;
concaveHasSeparatingNormals[i] = 0;
int pairIdx = i;
int bodyIndexA = concavePairs[i].x;
int bodyIndexB = concavePairs[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;
if (collidables[collidableIndexB].m_shapeType!=SHAPE_CONVEX_HULL&&
collidables[collidableIndexB].m_shapeType!=SHAPE_COMPOUND_OF_CONVEX_HULLS)
{
concavePairs[pairIdx].w = -1;
return;
}
int numFacesA = convexShapes[shapeIndexA].m_numFaces;
int numActualConcaveConvexTests = 0;
int f = concavePairs[i].z;
bool overlap = false;
ConvexPolyhedronCL 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);
btGpuFace face = faces[convexShapes[shapeIndexA].m_faceOffset+f];
float4 triMinAabb, triMaxAabb;
btAabbCL triAabb;
triAabb.m_min = make_float4(1e30f,1e30f,1e30f,0.f);
triAabb.m_max = make_float4(-1e30f,-1e30f,-1e30f,0.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;
triAabb.m_min = min(triAabb.m_min,vert);
triAabb.m_max = max(triAabb.m_max,vert);
}
overlap = true;
overlap = (triAabb.m_min.x > aabbs[bodyIndexB].m_max.x || triAabb.m_max.x < aabbs[bodyIndexB].m_min.x) ? false : overlap;
overlap = (triAabb.m_min.z > aabbs[bodyIndexB].m_max.z || triAabb.m_max.z < aabbs[bodyIndexB].m_min.z) ? false : overlap;
overlap = (triAabb.m_min.y > aabbs[bodyIndexB].m_max.y || triAabb.m_max.y < aabbs[bodyIndexB].m_min.y) ? false : overlap;
if (overlap)
{
float dmin = FLT_MAX;
int hasSeparatingAxis=5;
float4 sepAxis=make_float4(1,2,3,4);
int localCC=0;
numActualConcaveConvexTests++;
//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);
btGpuFace 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 = dot(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 = -dot(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;
///////////////////
///compound shape support
if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)
{
int compoundChild = concavePairs[pairIdx].w;
int childShapeIndexB = compoundChild;//collidables[collidableIndexB].m_shapeIndex+compoundChild;
int childColIndexB = 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;
shapeIndexB = collidables[childColIndexB].m_shapeIndex;
}
//////////////////
float4 c0local = convexPolyhedronA.m_localCenter;
float4 c0 = transform(&c0local, &posA, &ornA);
float4 c1local = convexShapes[shapeIndexB].m_localCenter;
float4 c1 = transform(&c1local,&posB,&ornB);
const float4 DeltaC2 = c0 - c1;
bool sepA = findSeparatingAxisLocalA( &convexPolyhedronA, &convexShapes[shapeIndexB],
posA,ornA,
posB,ornB,
DeltaC2,
verticesA,uniqueEdgesA,facesA,indicesA,
vertices,uniqueEdges,faces,indices,
&sepAxis,&dmin);
hasSeparatingAxis = 4;
if (!sepA)
{
hasSeparatingAxis = 0;
} else
{
bool sepB = findSeparatingAxisLocalB( &convexShapes[shapeIndexB],&convexPolyhedronA,
posB,ornB,
posA,ornA,
DeltaC2,
vertices,uniqueEdges,faces,indices,
verticesA,uniqueEdgesA,facesA,indicesA,
&sepAxis,&dmin);
if (!sepB)
{
hasSeparatingAxis = 0;
} else
{
hasSeparatingAxis = 1;
}
}
if (hasSeparatingAxis)
{
dmins[i] = dmin;
concaveSeparatingNormalsOut[pairIdx]=sepAxis;
concaveHasSeparatingNormals[i]=1;
} else
{
//mark this pair as in-active
concavePairs[pairIdx].w = -1;
}
}
else
{
//mark this pair as in-active
concavePairs[pairIdx].w = -1;
}
}
// work-in-progress
__kernel void findConcaveSeparatingAxisEdgeEdgeKernel( __global int4* concavePairs,
__global const BodyData* rigidBodies,
__global const btCollidableGpu* collidables,
__global const ConvexPolyhedronCL* convexShapes,
__global const float4* vertices,
__global const float4* uniqueEdges,
__global const btGpuFace* faces,
__global const int* indices,
__global const btGpuChildShape* gpuChildShapes,
__global btAabbCL* aabbs,
__global float4* concaveSeparatingNormalsOut,
__global int* concaveHasSeparatingNormals,
__global int4* clippingFacesOut,
__global float4* worldVertsA1GPU,
__global float4* worldNormalsAGPU,
__global float4* worldVertsB1GPU,
__global float* dmins,
int vertexFaceCapacity,
int numConcavePairs
)
{
int i = get_global_id(0);
if (i>=numConcavePairs)
return;
if (!concaveHasSeparatingNormals[i])
return;
int pairIdx = i;
int bodyIndexA = concavePairs[i].x;
int bodyIndexB = concavePairs[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 numFacesA = convexShapes[shapeIndexA].m_numFaces;
int numActualConcaveConvexTests = 0;
int f = concavePairs[i].z;
bool overlap = false;
ConvexPolyhedronCL 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);
btGpuFace face = faces[convexShapes[shapeIndexA].m_faceOffset+f];
float4 triMinAabb, triMaxAabb;
btAabbCL triAabb;
triAabb.m_min = make_float4(1e30f,1e30f,1e30f,0.f);
triAabb.m_max = make_float4(-1e30f,-1e30f,-1e30f,0.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;
triAabb.m_min = min(triAabb.m_min,vert);
triAabb.m_max = max(triAabb.m_max,vert);
}
overlap = true;
overlap = (triAabb.m_min.x > aabbs[bodyIndexB].m_max.x || triAabb.m_max.x < aabbs[bodyIndexB].m_min.x) ? false : overlap;
overlap = (triAabb.m_min.z > aabbs[bodyIndexB].m_max.z || triAabb.m_max.z < aabbs[bodyIndexB].m_min.z) ? false : overlap;
overlap = (triAabb.m_min.y > aabbs[bodyIndexB].m_max.y || triAabb.m_max.y < aabbs[bodyIndexB].m_min.y) ? false : overlap;
if (overlap)
{
float dmin = dmins[i];
int hasSeparatingAxis=5;
float4 sepAxis=make_float4(1,2,3,4);
sepAxis = concaveSeparatingNormalsOut[pairIdx];
int localCC=0;
numActualConcaveConvexTests++;
//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);
btGpuFace 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 = dot(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 = -dot(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;
///////////////////
///compound shape support
if (collidables[collidableIndexB].m_shapeType==SHAPE_COMPOUND_OF_CONVEX_HULLS)
{
int compoundChild = concavePairs[pairIdx].w;
int childShapeIndexB = compoundChild;//collidables[collidableIndexB].m_shapeIndex+compoundChild;
int childColIndexB = 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;
shapeIndexB = collidables[childColIndexB].m_shapeIndex;
}
//////////////////
float4 c0local = convexPolyhedronA.m_localCenter;
float4 c0 = transform(&c0local, &posA, &ornA);
float4 c1local = convexShapes[shapeIndexB].m_localCenter;
float4 c1 = transform(&c1local,&posB,&ornB);
const float4 DeltaC2 = c0 - c1;
{
bool sepEE = findSeparatingAxisEdgeEdgeLocalA( &convexPolyhedronA, &convexShapes[shapeIndexB],
posA,ornA,
posB,ornB,
DeltaC2,
verticesA,uniqueEdgesA,facesA,indicesA,
vertices,uniqueEdges,faces,indices,
&sepAxis,&dmin);
if (!sepEE)
{
hasSeparatingAxis = 0;
} else
{
hasSeparatingAxis = 1;
}
}
if (hasSeparatingAxis)
{
sepAxis.w = dmin;
dmins[i] = dmin;
concaveSeparatingNormalsOut[pairIdx]=sepAxis;
concaveHasSeparatingNormals[i]=1;
float minDist = -1e30f;
float maxDist = 0.02f;
findClippingFaces(sepAxis,
&convexPolyhedronA,
&convexShapes[shapeIndexB],
posA,ornA,
posB,ornB,
worldVertsA1GPU,
worldNormalsAGPU,
worldVertsB1GPU,
vertexFaceCapacity,
minDist, maxDist,
verticesA,
facesA,
indicesA,
vertices,
faces,
indices,
clippingFacesOut, pairIdx);
} else
{
//mark this pair as in-active
concavePairs[pairIdx].w = -1;
}
}
else
{
//mark this pair as in-active
concavePairs[pairIdx].w = -1;
}
}