Accelerate GPU raycaster with PLBVH.
This commit is contained in:
@@ -337,3 +337,103 @@ __kernel void rayCastKernel(
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
__kernel void findRayRigidPairIndexRanges(__global int2* rayRigidPairs,
|
||||
__global int* out_firstRayRigidPairIndexPerRay,
|
||||
__global int* out_numRayRigidPairsPerRay,
|
||||
int numRayRigidPairs)
|
||||
{
|
||||
int rayRigidPairIndex = get_global_id(0);
|
||||
if (rayRigidPairIndex >= numRayRigidPairs) return;
|
||||
|
||||
int rayIndex = rayRigidPairs[rayRigidPairIndex].x;
|
||||
|
||||
atomic_min(&out_firstRayRigidPairIndexPerRay[rayIndex], rayRigidPairIndex);
|
||||
atomic_inc(&out_numRayRigidPairsPerRay[rayIndex]);
|
||||
}
|
||||
|
||||
__kernel void rayCastPairsKernel(const __global b3RayInfo* rays,
|
||||
__global b3RayHit* hitResults,
|
||||
__global int* firstRayRigidPairIndexPerRay,
|
||||
__global int* numRayRigidPairsPerRay,
|
||||
|
||||
__global Body* bodies,
|
||||
__global Collidable* collidables,
|
||||
__global const b3GpuFace* faces,
|
||||
__global const ConvexPolyhedronCL* convexShapes,
|
||||
|
||||
__global int2* rayRigidPairs,
|
||||
int numRays)
|
||||
{
|
||||
int i = get_global_id(0);
|
||||
if (i >= numRays) return;
|
||||
|
||||
float4 rayFrom = rays[i].m_from;
|
||||
float4 rayTo = rays[i].m_to;
|
||||
|
||||
hitResults[i].m_hitFraction = 1.f;
|
||||
|
||||
float hitFraction = 1.f;
|
||||
float4 hitPoint;
|
||||
float4 hitNormal;
|
||||
int hitBodyIndex = -1;
|
||||
|
||||
//
|
||||
for(int pair = 0; pair < numRayRigidPairsPerRay[i]; ++pair)
|
||||
{
|
||||
int rayRigidPairIndex = pair + firstRayRigidPairIndexPerRay[i];
|
||||
int b = rayRigidPairs[rayRigidPairIndex].y;
|
||||
|
||||
if (hitResults[i].m_hitResult2 == b) continue;
|
||||
|
||||
Body body = bodies[b];
|
||||
Collidable rigidCollidable = collidables[body.m_collidableIdx];
|
||||
|
||||
float4 pos = body.m_pos;
|
||||
float4 orn = body.m_quat;
|
||||
|
||||
if (rigidCollidable.m_shapeType == SHAPE_CONVEX_HULL)
|
||||
{
|
||||
float4 invPos = (float4)(0,0,0,0);
|
||||
float4 invOrn = (float4)(0,0,0,0);
|
||||
float4 rayFromLocal = (float4)(0,0,0,0);
|
||||
float4 rayToLocal = (float4)(0,0,0,0);
|
||||
invOrn = qtInvert(orn);
|
||||
invPos = qtRotate(invOrn, -pos);
|
||||
rayFromLocal = qtRotate( invOrn, rayFrom ) + invPos;
|
||||
rayToLocal = qtRotate( invOrn, rayTo) + invPos;
|
||||
rayFromLocal.w = 0.f;
|
||||
rayToLocal.w = 0.f;
|
||||
int numFaces = convexShapes[rigidCollidable.m_shapeIndex].m_numFaces;
|
||||
int faceOffset = convexShapes[rigidCollidable.m_shapeIndex].m_faceOffset;
|
||||
|
||||
if (numFaces && rayConvex(rayFromLocal, rayToLocal, numFaces, faceOffset,faces, &hitFraction, &hitNormal))
|
||||
{
|
||||
hitBodyIndex = b;
|
||||
hitPoint = setInterpolate3(rayFrom, rayTo, hitFraction);
|
||||
}
|
||||
}
|
||||
|
||||
if (rigidCollidable.m_shapeType == SHAPE_SPHERE)
|
||||
{
|
||||
float radius = rigidCollidable.m_radius;
|
||||
|
||||
if (sphere_intersect(pos, radius, rayFrom, rayTo, &hitFraction))
|
||||
{
|
||||
hitBodyIndex = b;
|
||||
hitPoint = setInterpolate3(rayFrom, rayTo, hitFraction);
|
||||
hitNormal = (float4) (hitPoint - bodies[b].m_pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (hitBodyIndex >= 0)
|
||||
{
|
||||
hitResults[i].m_hitFraction = hitFraction;
|
||||
hitResults[i].m_hitPoint = hitPoint;
|
||||
hitResults[i].m_hitNormal = normalize(hitNormal);
|
||||
hitResults[i].m_hitResult0 = hitBodyIndex;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -281,4 +281,101 @@ static const char* rayCastKernelCL= \
|
||||
" hitResults[i].m_hitResult0 = hitBodyIndex;\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"__kernel void findRayRigidPairIndexRanges(__global int2* rayRigidPairs, \n"
|
||||
" __global int* out_firstRayRigidPairIndexPerRay,\n"
|
||||
" __global int* out_numRayRigidPairsPerRay,\n"
|
||||
" int numRayRigidPairs)\n"
|
||||
"{\n"
|
||||
" int rayRigidPairIndex = get_global_id(0);\n"
|
||||
" if (rayRigidPairIndex >= numRayRigidPairs) return;\n"
|
||||
" \n"
|
||||
" int rayIndex = rayRigidPairs[rayRigidPairIndex].x;\n"
|
||||
" \n"
|
||||
" atomic_min(&out_firstRayRigidPairIndexPerRay[rayIndex], rayRigidPairIndex);\n"
|
||||
" atomic_inc(&out_numRayRigidPairsPerRay[rayIndex]);\n"
|
||||
"}\n"
|
||||
"__kernel void rayCastPairsKernel(const __global b3RayInfo* rays, \n"
|
||||
" __global b3RayHit* hitResults, \n"
|
||||
" __global int* firstRayRigidPairIndexPerRay,\n"
|
||||
" __global int* numRayRigidPairsPerRay,\n"
|
||||
" \n"
|
||||
" __global Body* bodies,\n"
|
||||
" __global Collidable* collidables,\n"
|
||||
" __global const b3GpuFace* faces,\n"
|
||||
" __global const ConvexPolyhedronCL* convexShapes,\n"
|
||||
" \n"
|
||||
" __global int2* rayRigidPairs,\n"
|
||||
" int numRays)\n"
|
||||
"{\n"
|
||||
" int i = get_global_id(0);\n"
|
||||
" if (i >= numRays) return;\n"
|
||||
" \n"
|
||||
" float4 rayFrom = rays[i].m_from;\n"
|
||||
" float4 rayTo = rays[i].m_to;\n"
|
||||
" \n"
|
||||
" hitResults[i].m_hitFraction = 1.f;\n"
|
||||
" \n"
|
||||
" float hitFraction = 1.f;\n"
|
||||
" float4 hitPoint;\n"
|
||||
" float4 hitNormal;\n"
|
||||
" int hitBodyIndex = -1;\n"
|
||||
" \n"
|
||||
" //\n"
|
||||
" for(int pair = 0; pair < numRayRigidPairsPerRay[i]; ++pair)\n"
|
||||
" {\n"
|
||||
" int rayRigidPairIndex = pair + firstRayRigidPairIndexPerRay[i];\n"
|
||||
" int b = rayRigidPairs[rayRigidPairIndex].y;\n"
|
||||
" \n"
|
||||
" if (hitResults[i].m_hitResult2 == b) continue;\n"
|
||||
" \n"
|
||||
" Body body = bodies[b];\n"
|
||||
" Collidable rigidCollidable = collidables[body.m_collidableIdx];\n"
|
||||
" \n"
|
||||
" float4 pos = body.m_pos;\n"
|
||||
" float4 orn = body.m_quat;\n"
|
||||
" \n"
|
||||
" if (rigidCollidable.m_shapeType == SHAPE_CONVEX_HULL)\n"
|
||||
" {\n"
|
||||
" float4 invPos = (float4)(0,0,0,0);\n"
|
||||
" float4 invOrn = (float4)(0,0,0,0);\n"
|
||||
" float4 rayFromLocal = (float4)(0,0,0,0);\n"
|
||||
" float4 rayToLocal = (float4)(0,0,0,0);\n"
|
||||
" invOrn = qtInvert(orn);\n"
|
||||
" invPos = qtRotate(invOrn, -pos);\n"
|
||||
" rayFromLocal = qtRotate( invOrn, rayFrom ) + invPos;\n"
|
||||
" rayToLocal = qtRotate( invOrn, rayTo) + invPos;\n"
|
||||
" rayFromLocal.w = 0.f;\n"
|
||||
" rayToLocal.w = 0.f;\n"
|
||||
" int numFaces = convexShapes[rigidCollidable.m_shapeIndex].m_numFaces;\n"
|
||||
" int faceOffset = convexShapes[rigidCollidable.m_shapeIndex].m_faceOffset;\n"
|
||||
" \n"
|
||||
" if (numFaces && rayConvex(rayFromLocal, rayToLocal, numFaces, faceOffset,faces, &hitFraction, &hitNormal))\n"
|
||||
" {\n"
|
||||
" hitBodyIndex = b;\n"
|
||||
" hitPoint = setInterpolate3(rayFrom, rayTo, hitFraction);\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
" \n"
|
||||
" if (rigidCollidable.m_shapeType == SHAPE_SPHERE)\n"
|
||||
" {\n"
|
||||
" float radius = rigidCollidable.m_radius;\n"
|
||||
" \n"
|
||||
" if (sphere_intersect(pos, radius, rayFrom, rayTo, &hitFraction))\n"
|
||||
" {\n"
|
||||
" hitBodyIndex = b;\n"
|
||||
" hitPoint = setInterpolate3(rayFrom, rayTo, hitFraction);\n"
|
||||
" hitNormal = (float4) (hitPoint - bodies[b].m_pos);\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
" \n"
|
||||
" if (hitBodyIndex >= 0)\n"
|
||||
" {\n"
|
||||
" hitResults[i].m_hitFraction = hitFraction;\n"
|
||||
" hitResults[i].m_hitPoint = hitPoint;\n"
|
||||
" hitResults[i].m_hitNormal = normalize(hitNormal);\n"
|
||||
" hitResults[i].m_hitResult0 = hitBodyIndex;\n"
|
||||
" }\n"
|
||||
" \n"
|
||||
"}\n"
|
||||
;
|
||||
|
||||
Reference in New Issue
Block a user