Use merged AABB to calculate grid cell size for PLBVH.
This commit is contained in:
@@ -80,8 +80,8 @@ unsigned int getMortonCode(unsigned int x, unsigned int y, unsigned int z)
|
||||
return interleaveBits(x) << 0 | interleaveBits(y) << 1 | interleaveBits(z) << 2;
|
||||
}
|
||||
|
||||
/*
|
||||
__kernel void findAllNodesMergedAabb(__global b3AabbCL* worldSpaceAabbs, __global b3AabbCL* out_mergedAabb, int numAabbs)
|
||||
|
||||
__kernel void findAllNodesMergedAabb(__global b3AabbCL* out_mergedAabb, int numAabbs)
|
||||
{
|
||||
int aabbIndex = get_global_id(0);
|
||||
if(aabbIndex >= numAabbs) return;
|
||||
@@ -89,11 +89,11 @@ __kernel void findAllNodesMergedAabb(__global b3AabbCL* worldSpaceAabbs, __globa
|
||||
//Find the most significant bit(msb)
|
||||
int mostSignificantBit = 0;
|
||||
{
|
||||
int temp = numLeaves;
|
||||
int temp = numAabbs;
|
||||
while(temp >>= 1) mostSignificantBit++; //Start counting from 0 (0 and 1 have msb 0, 2 has msb 1)
|
||||
}
|
||||
|
||||
int numberOfAabbsAboveMsbSplit = numAabbs & ~( ~(0) << mostSignificantBit ); // verify
|
||||
int numberOfAabbsAboveMsbSplit = numAabbs & ~( ~(0) << mostSignificantBit );
|
||||
int numRemainingAabbs = (1 << mostSignificantBit);
|
||||
|
||||
//Merge AABBs above most significant bit so that the number of remaining AABBs is a power of 2
|
||||
@@ -102,8 +102,8 @@ __kernel void findAllNodesMergedAabb(__global b3AabbCL* worldSpaceAabbs, __globa
|
||||
{
|
||||
int otherAabbIndex = numRemainingAabbs + aabbIndex;
|
||||
|
||||
b3AabbCL aabb = worldSpaceAabbs[aabbIndex];
|
||||
b3AabbCL otherAabb = worldSpaceAabbs[otherAabbIndex];
|
||||
b3AabbCL aabb = out_mergedAabb[aabbIndex];
|
||||
b3AabbCL otherAabb = out_mergedAabb[otherAabbIndex];
|
||||
|
||||
b3AabbCL mergedAabb;
|
||||
mergedAabb.m_min = b3Min(aabb.m_min, otherAabb.m_min);
|
||||
@@ -121,8 +121,8 @@ __kernel void findAllNodesMergedAabb(__global b3AabbCL* worldSpaceAabbs, __globa
|
||||
{
|
||||
int otherAabbIndex = aabbIndex + offset;
|
||||
|
||||
b3AabbCL aabb = worldSpaceAabbs[aabbIndex];
|
||||
b3AabbCL otherAabb = worldSpaceAabbs[otherAabbIndex];
|
||||
b3AabbCL aabb = out_mergedAabb[aabbIndex];
|
||||
b3AabbCL otherAabb = out_mergedAabb[otherAabbIndex];
|
||||
|
||||
b3AabbCL mergedAabb;
|
||||
mergedAabb.m_min = b3Min(aabb.m_min, otherAabb.m_min);
|
||||
@@ -130,27 +130,29 @@ __kernel void findAllNodesMergedAabb(__global b3AabbCL* worldSpaceAabbs, __globa
|
||||
out_mergedAabb[aabbIndex] = mergedAabb;
|
||||
}
|
||||
|
||||
offset = offset / 2;
|
||||
offset /= 2;
|
||||
|
||||
barrier(CLK_GLOBAL_MEM_FENCE);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
__kernel void assignMortonCodesAndAabbIndicies(__global b3AabbCL* worldSpaceAabbs,
|
||||
__global SortDataCL* out_mortonCodesAndAabbIndices,
|
||||
b3Scalar cellSize, int numAabbs)
|
||||
__kernel void assignMortonCodesAndAabbIndicies(__global b3AabbCL* worldSpaceAabbs, __global b3AabbCL* mergedAabbOfAllNodes,
|
||||
__global SortDataCL* out_mortonCodesAndAabbIndices, int numAabbs)
|
||||
{
|
||||
int leafNodeIndex = get_global_id(0); //Leaf node index == AABB index
|
||||
if(leafNodeIndex >= numAabbs) return;
|
||||
|
||||
b3AabbCL aabb = worldSpaceAabbs[leafNodeIndex];
|
||||
b3AabbCL mergedAabb = mergedAabbOfAllNodes[0];
|
||||
b3Vector3 gridCenter = (mergedAabb.m_min + mergedAabb.m_max) * 0.5f;
|
||||
b3Vector3 gridCellSize = (mergedAabb.m_max - mergedAabb.m_min) / (float)1024;
|
||||
|
||||
b3Vector3 center = (aabb.m_min + aabb.m_max) * 0.5f;
|
||||
b3AabbCL aabb = worldSpaceAabbs[leafNodeIndex];
|
||||
b3Vector3 aabbCenter = (aabb.m_min + aabb.m_max) * 0.5f;
|
||||
b3Vector3 aabbCenterRelativeToGrid = aabbCenter - gridCenter;
|
||||
|
||||
//Quantize into integer coordinates
|
||||
//floor() is needed to prevent the center cell, at (0,0,0) from being twice the size
|
||||
b3Vector3 gridPosition = center / cellSize;
|
||||
b3Vector3 gridPosition = aabbCenterRelativeToGrid / gridCellSize;
|
||||
|
||||
int4 discretePosition;
|
||||
discretePosition.x = (int)( (gridPosition.x >= 0.0f) ? gridPosition.x : floor(gridPosition.x) );
|
||||
|
||||
@@ -75,20 +75,77 @@ static const char* parallelLinearBvhCL= \
|
||||
"{\n"
|
||||
" return interleaveBits(x) << 0 | interleaveBits(y) << 1 | interleaveBits(z) << 2;\n"
|
||||
"}\n"
|
||||
"__kernel void assignMortonCodesAndAabbIndicies(__global b3AabbCL* worldSpaceAabbs, \n"
|
||||
" __global SortDataCL* out_mortonCodesAndAabbIndices, \n"
|
||||
" b3Scalar cellSize, int numAabbs)\n"
|
||||
"__kernel void findAllNodesMergedAabb(__global b3AabbCL* out_mergedAabb, int numAabbs)\n"
|
||||
"{\n"
|
||||
" int aabbIndex = get_global_id(0);\n"
|
||||
" if(aabbIndex >= numAabbs) return;\n"
|
||||
" \n"
|
||||
" //Find the most significant bit(msb)\n"
|
||||
" int mostSignificantBit = 0;\n"
|
||||
" {\n"
|
||||
" int temp = numAabbs;\n"
|
||||
" while(temp >>= 1) mostSignificantBit++; //Start counting from 0 (0 and 1 have msb 0, 2 has msb 1)\n"
|
||||
" }\n"
|
||||
" \n"
|
||||
" int numberOfAabbsAboveMsbSplit = numAabbs & ~( ~(0) << mostSignificantBit );\n"
|
||||
" int numRemainingAabbs = (1 << mostSignificantBit);\n"
|
||||
" \n"
|
||||
" //Merge AABBs above most significant bit so that the number of remaining AABBs is a power of 2\n"
|
||||
" //For example, if there are 159 AABBs = 128 + 31, then merge indices [0, 30] and 128 + [0, 30]\n"
|
||||
" if(aabbIndex < numberOfAabbsAboveMsbSplit)\n"
|
||||
" {\n"
|
||||
" int otherAabbIndex = numRemainingAabbs + aabbIndex;\n"
|
||||
" \n"
|
||||
" b3AabbCL aabb = out_mergedAabb[aabbIndex];\n"
|
||||
" b3AabbCL otherAabb = out_mergedAabb[otherAabbIndex];\n"
|
||||
" \n"
|
||||
" b3AabbCL mergedAabb;\n"
|
||||
" mergedAabb.m_min = b3Min(aabb.m_min, otherAabb.m_min);\n"
|
||||
" mergedAabb.m_max = b3Max(aabb.m_max, otherAabb.m_max);\n"
|
||||
" out_mergedAabb[aabbIndex] = mergedAabb;\n"
|
||||
" }\n"
|
||||
" \n"
|
||||
" barrier(CLK_GLOBAL_MEM_FENCE);\n"
|
||||
" \n"
|
||||
" //\n"
|
||||
" int offset = numRemainingAabbs / 2;\n"
|
||||
" while(offset >= 1)\n"
|
||||
" {\n"
|
||||
" if(aabbIndex < offset)\n"
|
||||
" {\n"
|
||||
" int otherAabbIndex = aabbIndex + offset;\n"
|
||||
" \n"
|
||||
" b3AabbCL aabb = out_mergedAabb[aabbIndex];\n"
|
||||
" b3AabbCL otherAabb = out_mergedAabb[otherAabbIndex];\n"
|
||||
" \n"
|
||||
" b3AabbCL mergedAabb;\n"
|
||||
" mergedAabb.m_min = b3Min(aabb.m_min, otherAabb.m_min);\n"
|
||||
" mergedAabb.m_max = b3Max(aabb.m_max, otherAabb.m_max);\n"
|
||||
" out_mergedAabb[aabbIndex] = mergedAabb;\n"
|
||||
" }\n"
|
||||
" \n"
|
||||
" offset /= 2;\n"
|
||||
" \n"
|
||||
" barrier(CLK_GLOBAL_MEM_FENCE);\n"
|
||||
" }\n"
|
||||
"}\n"
|
||||
"__kernel void assignMortonCodesAndAabbIndicies(__global b3AabbCL* worldSpaceAabbs, __global b3AabbCL* mergedAabbOfAllNodes, \n"
|
||||
" __global SortDataCL* out_mortonCodesAndAabbIndices, int numAabbs)\n"
|
||||
"{\n"
|
||||
" int leafNodeIndex = get_global_id(0); //Leaf node index == AABB index\n"
|
||||
" if(leafNodeIndex >= numAabbs) return;\n"
|
||||
" \n"
|
||||
" b3AabbCL aabb = worldSpaceAabbs[leafNodeIndex];\n"
|
||||
" b3AabbCL mergedAabb = mergedAabbOfAllNodes[0];\n"
|
||||
" b3Vector3 gridCenter = (mergedAabb.m_min + mergedAabb.m_max) * 0.5f;\n"
|
||||
" b3Vector3 gridCellSize = (mergedAabb.m_max - mergedAabb.m_min) / (float)1024;\n"
|
||||
" \n"
|
||||
" b3Vector3 center = (aabb.m_min + aabb.m_max) * 0.5f;\n"
|
||||
" b3AabbCL aabb = worldSpaceAabbs[leafNodeIndex];\n"
|
||||
" b3Vector3 aabbCenter = (aabb.m_min + aabb.m_max) * 0.5f;\n"
|
||||
" b3Vector3 aabbCenterRelativeToGrid = aabbCenter - gridCenter;\n"
|
||||
" \n"
|
||||
" //Quantize into integer coordinates\n"
|
||||
" //floor() is needed to prevent the center cell, at (0,0,0) from being twice the size\n"
|
||||
" b3Vector3 gridPosition = center / cellSize;\n"
|
||||
" b3Vector3 gridPosition = aabbCenterRelativeToGrid / gridCellSize;\n"
|
||||
" \n"
|
||||
" int4 discretePosition;\n"
|
||||
" discretePosition.x = (int)( (gridPosition.x >= 0.0f) ? gridPosition.x : floor(gridPosition.x) );\n"
|
||||
@@ -110,7 +167,7 @@ static const char* parallelLinearBvhCL= \
|
||||
" out_mortonCodesAndAabbIndices[leafNodeIndex] = mortonCodeIndexPair;\n"
|
||||
"}\n"
|
||||
"#define B3_PLVBH_TRAVERSE_MAX_STACK_SIZE 128\n"
|
||||
"#define B3_PLBVH_ROOT_NODE_MARKER -1 //Used to indicate that the node has no parent \n"
|
||||
"#define B3_PLBVH_ROOT_NODE_MARKER -1 //Used to indicate that the (root) node has no parent \n"
|
||||
"#define B3_PLBVH_ROOT_NODE_INDEX 0\n"
|
||||
"//For elements of internalNodeChildIndices(int2), the negative bit determines whether it is a leaf or internal node.\n"
|
||||
"//Positive index == leaf node, while negative index == internal node (remove negative sign to get index).\n"
|
||||
@@ -264,13 +321,16 @@ static const char* parallelLinearBvhCL= \
|
||||
" __global int* out_numPairs, __global int4* out_overlappingPairs, \n"
|
||||
" int maxPairs, int numQueryAabbs)\n"
|
||||
"{\n"
|
||||
"#define USE_SPATIALLY_COHERENT_INDICIES //mortonCodesAndAabbIndices[] contains rigid body indices sorted along the z-curve\n"
|
||||
"#ifdef USE_SPATIALLY_COHERENT_INDICIES\n"
|
||||
" int queryRigidIndex = get_group_id(0) * get_local_size(0) + get_local_id(0);\n"
|
||||
" if(queryRigidIndex >= numQueryAabbs) return;\n"
|
||||
" \n"
|
||||
" queryRigidIndex = mortonCodesAndAabbIndices[queryRigidIndex].m_value;\n"
|
||||
" //int queryRigidIndex = get_global_id(0);\n"
|
||||
" //if(queryRigidIndex >= numQueryAabbs) return;\n"
|
||||
" \n"
|
||||
"#else\n"
|
||||
" int queryRigidIndex = get_global_id(0);\n"
|
||||
" if(queryRigidIndex >= numQueryAabbs) return;\n"
|
||||
"#endif\n"
|
||||
" b3AabbCL queryAabb = rigidAabbs[queryRigidIndex];\n"
|
||||
" \n"
|
||||
" int stack[B3_PLVBH_TRAVERSE_MAX_STACK_SIZE];\n"
|
||||
|
||||
Reference in New Issue
Block a user