Use merged AABB to calculate grid cell size for PLBVH.

This commit is contained in:
Jackson Lee
2014-02-18 19:59:05 -08:00
parent b7b7356af8
commit 7f0e361fa0
4 changed files with 115 additions and 45 deletions

View File

@@ -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"