2017-08-01 14:30:58 +02:00
|
|
|
/*
|
|
|
|
This software is provided 'as-is', without any express or implied warranty.
|
|
|
|
In no event will the authors be held liable for any damages arising from the use of this software.
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it freely,
|
|
|
|
subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
//Initial Author Jackson Lee, 2014
|
|
|
|
|
|
|
|
#include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
|
|
|
|
#include "Bullet3OpenCL/ParallelPrimitives/b3LauncherCL.h"
|
|
|
|
|
|
|
|
#include "b3GpuParallelLinearBvh.h"
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
b3GpuParallelLinearBvh::b3GpuParallelLinearBvh(cl_context context, cl_device_id device, cl_command_queue queue) : m_queue(queue),
|
|
|
|
m_radixSorter(context, device, queue),
|
|
|
|
|
|
|
|
m_rootNodeIndex(context, queue),
|
|
|
|
m_maxDistanceFromRoot(context, queue),
|
|
|
|
m_temp(context, queue),
|
|
|
|
|
|
|
|
m_internalNodeAabbs(context, queue),
|
|
|
|
m_internalNodeLeafIndexRanges(context, queue),
|
|
|
|
m_internalNodeChildNodes(context, queue),
|
|
|
|
m_internalNodeParentNodes(context, queue),
|
|
|
|
|
|
|
|
m_commonPrefixes(context, queue),
|
|
|
|
m_commonPrefixLengths(context, queue),
|
|
|
|
m_distanceFromRoot(context, queue),
|
|
|
|
|
|
|
|
m_leafNodeParentNodes(context, queue),
|
|
|
|
m_mortonCodesAndAabbIndicies(context, queue),
|
|
|
|
m_mergedAabb(context, queue),
|
|
|
|
m_leafNodeAabbs(context, queue),
|
|
|
|
|
|
|
|
m_largeAabbs(context, queue)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
m_rootNodeIndex.resize(1);
|
|
|
|
m_maxDistanceFromRoot.resize(1);
|
|
|
|
m_temp.resize(1);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//
|
|
|
|
const char CL_PROGRAM_PATH[] = "src/Bullet3OpenCL/BroadphaseCollision/kernels/parallelLinearBvh.cl";
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
const char* kernelSource = parallelLinearBvhCL; //parallelLinearBvhCL.h
|
2017-08-01 14:30:58 +02:00
|
|
|
cl_int error;
|
|
|
|
char* additionalMacros = 0;
|
|
|
|
m_parallelLinearBvhProgram = b3OpenCLUtils::compileCLProgramFromString(context, device, kernelSource, &error, additionalMacros, CL_PROGRAM_PATH);
|
|
|
|
b3Assert(m_parallelLinearBvhProgram);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
m_separateAabbsKernel = b3OpenCLUtils::compileCLKernelFromString(context, device, kernelSource, "separateAabbs", &error, m_parallelLinearBvhProgram, additionalMacros);
|
2017-08-01 14:30:58 +02:00
|
|
|
b3Assert(m_separateAabbsKernel);
|
2019-01-03 14:26:51 +01:00
|
|
|
m_findAllNodesMergedAabbKernel = b3OpenCLUtils::compileCLKernelFromString(context, device, kernelSource, "findAllNodesMergedAabb", &error, m_parallelLinearBvhProgram, additionalMacros);
|
2017-08-01 14:30:58 +02:00
|
|
|
b3Assert(m_findAllNodesMergedAabbKernel);
|
2019-01-03 14:26:51 +01:00
|
|
|
m_assignMortonCodesAndAabbIndiciesKernel = b3OpenCLUtils::compileCLKernelFromString(context, device, kernelSource, "assignMortonCodesAndAabbIndicies", &error, m_parallelLinearBvhProgram, additionalMacros);
|
2017-08-01 14:30:58 +02:00
|
|
|
b3Assert(m_assignMortonCodesAndAabbIndiciesKernel);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
m_computeAdjacentPairCommonPrefixKernel = b3OpenCLUtils::compileCLKernelFromString(context, device, kernelSource, "computeAdjacentPairCommonPrefix", &error, m_parallelLinearBvhProgram, additionalMacros);
|
2017-08-01 14:30:58 +02:00
|
|
|
b3Assert(m_computeAdjacentPairCommonPrefixKernel);
|
2019-01-03 14:26:51 +01:00
|
|
|
m_buildBinaryRadixTreeLeafNodesKernel = b3OpenCLUtils::compileCLKernelFromString(context, device, kernelSource, "buildBinaryRadixTreeLeafNodes", &error, m_parallelLinearBvhProgram, additionalMacros);
|
2017-08-01 14:30:58 +02:00
|
|
|
b3Assert(m_buildBinaryRadixTreeLeafNodesKernel);
|
2019-01-03 14:26:51 +01:00
|
|
|
m_buildBinaryRadixTreeInternalNodesKernel = b3OpenCLUtils::compileCLKernelFromString(context, device, kernelSource, "buildBinaryRadixTreeInternalNodes", &error, m_parallelLinearBvhProgram, additionalMacros);
|
2017-08-01 14:30:58 +02:00
|
|
|
b3Assert(m_buildBinaryRadixTreeInternalNodesKernel);
|
2019-01-03 14:26:51 +01:00
|
|
|
m_findDistanceFromRootKernel = b3OpenCLUtils::compileCLKernelFromString(context, device, kernelSource, "findDistanceFromRoot", &error, m_parallelLinearBvhProgram, additionalMacros);
|
2017-08-01 14:30:58 +02:00
|
|
|
b3Assert(m_findDistanceFromRootKernel);
|
2019-01-03 14:26:51 +01:00
|
|
|
m_buildBinaryRadixTreeAabbsRecursiveKernel = b3OpenCLUtils::compileCLKernelFromString(context, device, kernelSource, "buildBinaryRadixTreeAabbsRecursive", &error, m_parallelLinearBvhProgram, additionalMacros);
|
2017-08-01 14:30:58 +02:00
|
|
|
b3Assert(m_buildBinaryRadixTreeAabbsRecursiveKernel);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
m_findLeafIndexRangesKernel = b3OpenCLUtils::compileCLKernelFromString(context, device, kernelSource, "findLeafIndexRanges", &error, m_parallelLinearBvhProgram, additionalMacros);
|
2017-08-01 14:30:58 +02:00
|
|
|
b3Assert(m_findLeafIndexRangesKernel);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
m_plbvhCalculateOverlappingPairsKernel = b3OpenCLUtils::compileCLKernelFromString(context, device, kernelSource, "plbvhCalculateOverlappingPairs", &error, m_parallelLinearBvhProgram, additionalMacros);
|
2017-08-01 14:30:58 +02:00
|
|
|
b3Assert(m_plbvhCalculateOverlappingPairsKernel);
|
2019-01-03 14:26:51 +01:00
|
|
|
m_plbvhRayTraverseKernel = b3OpenCLUtils::compileCLKernelFromString(context, device, kernelSource, "plbvhRayTraverse", &error, m_parallelLinearBvhProgram, additionalMacros);
|
2017-08-01 14:30:58 +02:00
|
|
|
b3Assert(m_plbvhRayTraverseKernel);
|
2019-01-03 14:26:51 +01:00
|
|
|
m_plbvhLargeAabbAabbTestKernel = b3OpenCLUtils::compileCLKernelFromString(context, device, kernelSource, "plbvhLargeAabbAabbTest", &error, m_parallelLinearBvhProgram, additionalMacros);
|
2017-08-01 14:30:58 +02:00
|
|
|
b3Assert(m_plbvhLargeAabbAabbTestKernel);
|
2019-01-03 14:26:51 +01:00
|
|
|
m_plbvhLargeAabbRayTestKernel = b3OpenCLUtils::compileCLKernelFromString(context, device, kernelSource, "plbvhLargeAabbRayTest", &error, m_parallelLinearBvhProgram, additionalMacros);
|
2017-08-01 14:30:58 +02:00
|
|
|
b3Assert(m_plbvhLargeAabbRayTestKernel);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
b3GpuParallelLinearBvh::~b3GpuParallelLinearBvh()
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
clReleaseKernel(m_separateAabbsKernel);
|
|
|
|
clReleaseKernel(m_findAllNodesMergedAabbKernel);
|
|
|
|
clReleaseKernel(m_assignMortonCodesAndAabbIndiciesKernel);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
clReleaseKernel(m_computeAdjacentPairCommonPrefixKernel);
|
|
|
|
clReleaseKernel(m_buildBinaryRadixTreeLeafNodesKernel);
|
|
|
|
clReleaseKernel(m_buildBinaryRadixTreeInternalNodesKernel);
|
|
|
|
clReleaseKernel(m_findDistanceFromRootKernel);
|
|
|
|
clReleaseKernel(m_buildBinaryRadixTreeAabbsRecursiveKernel);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
clReleaseKernel(m_findLeafIndexRangesKernel);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
clReleaseKernel(m_plbvhCalculateOverlappingPairsKernel);
|
|
|
|
clReleaseKernel(m_plbvhRayTraverseKernel);
|
|
|
|
clReleaseKernel(m_plbvhLargeAabbAabbTestKernel);
|
|
|
|
clReleaseKernel(m_plbvhLargeAabbRayTestKernel);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
clReleaseProgram(m_parallelLinearBvhProgram);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void b3GpuParallelLinearBvh::build(const b3OpenCLArray<b3SapAabb>& worldSpaceAabbs, const b3OpenCLArray<int>& smallAabbIndices,
|
|
|
|
const b3OpenCLArray<int>& largeAabbIndices)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
B3_PROFILE("b3ParallelLinearBvh::build()");
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
int numLargeAabbs = largeAabbIndices.size();
|
|
|
|
int numSmallAabbs = smallAabbIndices.size();
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
//Since all AABBs(both large and small) are input as a contiguous array,
|
2017-08-01 14:30:58 +02:00
|
|
|
//with 2 additional arrays used to indicate the indices of large and small AABBs,
|
|
|
|
//it is necessary to separate the AABBs so that the large AABBs will not degrade the quality of the BVH.
|
|
|
|
{
|
|
|
|
B3_PROFILE("Separate large and small AABBs");
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
m_largeAabbs.resize(numLargeAabbs);
|
|
|
|
m_leafNodeAabbs.resize(numSmallAabbs);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//Write large AABBs into m_largeAabbs
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
b3BufferInfoCL bufferInfo[] =
|
|
|
|
{
|
|
|
|
b3BufferInfoCL(worldSpaceAabbs.getBufferCL()),
|
|
|
|
b3BufferInfoCL(largeAabbIndices.getBufferCL()),
|
|
|
|
|
|
|
|
b3BufferInfoCL(m_largeAabbs.getBufferCL())};
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
b3LauncherCL launcher(m_queue, m_separateAabbsKernel, "m_separateAabbsKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
launcher.setBuffers(bufferInfo, sizeof(bufferInfo) / sizeof(b3BufferInfoCL));
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.setConst(numLargeAabbs);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.launch1D(numLargeAabbs);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//Write small AABBs into m_leafNodeAabbs
|
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
b3BufferInfoCL bufferInfo[] =
|
|
|
|
{
|
|
|
|
b3BufferInfoCL(worldSpaceAabbs.getBufferCL()),
|
|
|
|
b3BufferInfoCL(smallAabbIndices.getBufferCL()),
|
|
|
|
|
|
|
|
b3BufferInfoCL(m_leafNodeAabbs.getBufferCL())};
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
b3LauncherCL launcher(m_queue, m_separateAabbsKernel, "m_separateAabbsKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
launcher.setBuffers(bufferInfo, sizeof(bufferInfo) / sizeof(b3BufferInfoCL));
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.setConst(numSmallAabbs);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.launch1D(numSmallAabbs);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
clFinish(m_queue);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//
|
2019-01-03 14:26:51 +01:00
|
|
|
int numLeaves = numSmallAabbs; //Number of leaves in the BVH == Number of rigid bodies with small AABBs
|
2017-08-01 14:30:58 +02:00
|
|
|
int numInternalNodes = numLeaves - 1;
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
if (numLeaves < 2)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
//Number of leaf nodes is checked in calculateOverlappingPairs() and testRaysAgainstBvhAabbs(),
|
|
|
|
//so it does not matter if numLeaves == 0 and rootNodeIndex == -1
|
|
|
|
int rootNodeIndex = numLeaves - 1;
|
|
|
|
m_rootNodeIndex.copyFromHostPointer(&rootNodeIndex, 1);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//Since the AABBs need to be rearranged(sorted) for the BVH construction algorithm,
|
|
|
|
//m_mortonCodesAndAabbIndicies.m_value is used to map a sorted AABB index to the unsorted AABB index
|
|
|
|
//instead of directly moving the AABBs. It needs to be set for the ray cast traversal kernel to work.
|
|
|
|
//( m_mortonCodesAndAabbIndicies[].m_value == unsorted index == index of m_leafNodeAabbs )
|
2019-01-03 14:26:51 +01:00
|
|
|
if (numLeaves == 1)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
b3SortData leaf;
|
2019-01-03 14:26:51 +01:00
|
|
|
leaf.m_value = 0; //1 leaf so index is always 0; leaf.m_key does not need to be set
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
m_mortonCodesAndAabbIndicies.resize(1);
|
|
|
|
m_mortonCodesAndAabbIndicies.copyFromHostPointer(&leaf, 1);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//
|
|
|
|
{
|
|
|
|
m_internalNodeAabbs.resize(numInternalNodes);
|
|
|
|
m_internalNodeLeafIndexRanges.resize(numInternalNodes);
|
|
|
|
m_internalNodeChildNodes.resize(numInternalNodes);
|
|
|
|
m_internalNodeParentNodes.resize(numInternalNodes);
|
|
|
|
|
|
|
|
m_commonPrefixes.resize(numInternalNodes);
|
|
|
|
m_commonPrefixLengths.resize(numInternalNodes);
|
|
|
|
m_distanceFromRoot.resize(numInternalNodes);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
m_leafNodeParentNodes.resize(numLeaves);
|
|
|
|
m_mortonCodesAndAabbIndicies.resize(numLeaves);
|
|
|
|
m_mergedAabb.resize(numLeaves);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
//Find the merged AABB of all small AABBs; this is used to define the size of
|
2017-08-01 14:30:58 +02:00
|
|
|
//each cell in the virtual grid for the next kernel(2^10 cells in each dimension).
|
|
|
|
{
|
|
|
|
B3_PROFILE("Find AABB of merged nodes");
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
m_mergedAabb.copyFromOpenCLArray(m_leafNodeAabbs); //Need to make a copy since the kernel modifies the array
|
|
|
|
|
|
|
|
for (int numAabbsNeedingMerge = numLeaves; numAabbsNeedingMerge >= 2;
|
|
|
|
numAabbsNeedingMerge = numAabbsNeedingMerge / 2 + numAabbsNeedingMerge % 2)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
b3BufferInfoCL bufferInfo[] =
|
|
|
|
{
|
|
|
|
b3BufferInfoCL(m_mergedAabb.getBufferCL()) //Resulting AABB is stored in m_mergedAabb[0]
|
|
|
|
};
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
b3LauncherCL launcher(m_queue, m_findAllNodesMergedAabbKernel, "m_findAllNodesMergedAabbKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
launcher.setBuffers(bufferInfo, sizeof(bufferInfo) / sizeof(b3BufferInfoCL));
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.setConst(numAabbsNeedingMerge);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.launch1D(numAabbsNeedingMerge);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
clFinish(m_queue);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//Insert the center of the AABBs into a virtual grid,
|
|
|
|
//then convert the discrete grid coordinates into a morton code
|
|
|
|
//For each element in m_mortonCodesAndAabbIndicies, set
|
|
|
|
// m_key == morton code (value to sort by)
|
|
|
|
// m_value == small AABB index
|
|
|
|
{
|
|
|
|
B3_PROFILE("Assign morton codes");
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
b3BufferInfoCL bufferInfo[] =
|
|
|
|
{
|
|
|
|
b3BufferInfoCL(m_leafNodeAabbs.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_mergedAabb.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_mortonCodesAndAabbIndicies.getBufferCL())};
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
b3LauncherCL launcher(m_queue, m_assignMortonCodesAndAabbIndiciesKernel, "m_assignMortonCodesAndAabbIndiciesKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
launcher.setBuffers(bufferInfo, sizeof(bufferInfo) / sizeof(b3BufferInfoCL));
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.setConst(numLeaves);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.launch1D(numLeaves);
|
|
|
|
clFinish(m_queue);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//
|
|
|
|
{
|
|
|
|
B3_PROFILE("Sort leaves by morton codes");
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
m_radixSorter.execute(m_mortonCodesAndAabbIndicies);
|
|
|
|
clFinish(m_queue);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//
|
|
|
|
constructBinaryRadixTree();
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//Since it is a sorted binary radix tree, each internal node contains a contiguous subset of leaf node indices.
|
|
|
|
//The root node contains leaf node indices in the range [0, numLeafNodes - 1].
|
|
|
|
//The child nodes of each node split their parent's index range into 2 contiguous halves.
|
|
|
|
//
|
|
|
|
//For example, if the root has indices [0, 31], its children might partition that range into [0, 11] and [12, 31].
|
|
|
|
//The next level in the tree could then split those ranges into [0, 2], [3, 11], [12, 22], and [23, 31].
|
|
|
|
//
|
|
|
|
//This property can be used for optimizing calculateOverlappingPairs(), to avoid testing each AABB pair twice
|
|
|
|
{
|
|
|
|
B3_PROFILE("m_findLeafIndexRangesKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
b3BufferInfoCL bufferInfo[] =
|
|
|
|
{
|
|
|
|
b3BufferInfoCL(m_internalNodeChildNodes.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_internalNodeLeafIndexRanges.getBufferCL())};
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
b3LauncherCL launcher(m_queue, m_findLeafIndexRangesKernel, "m_findLeafIndexRangesKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
launcher.setBuffers(bufferInfo, sizeof(bufferInfo) / sizeof(b3BufferInfoCL));
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.setConst(numInternalNodes);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.launch1D(numInternalNodes);
|
|
|
|
clFinish(m_queue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void b3GpuParallelLinearBvh::calculateOverlappingPairs(b3OpenCLArray<b3Int4>& out_overlappingPairs)
|
|
|
|
{
|
|
|
|
int maxPairs = out_overlappingPairs.size();
|
|
|
|
b3OpenCLArray<int>& numPairsGpu = m_temp;
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
int reset = 0;
|
|
|
|
numPairsGpu.copyFromHostPointer(&reset, 1);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//
|
2019-01-03 14:26:51 +01:00
|
|
|
if (m_leafNodeAabbs.size() > 1)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
B3_PROFILE("PLBVH small-small AABB test");
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
int numQueryAabbs = m_leafNodeAabbs.size();
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
b3BufferInfoCL bufferInfo[] =
|
|
|
|
{
|
|
|
|
b3BufferInfoCL(m_leafNodeAabbs.getBufferCL()),
|
|
|
|
|
|
|
|
b3BufferInfoCL(m_rootNodeIndex.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_internalNodeChildNodes.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_internalNodeAabbs.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_internalNodeLeafIndexRanges.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_mortonCodesAndAabbIndicies.getBufferCL()),
|
|
|
|
|
|
|
|
b3BufferInfoCL(numPairsGpu.getBufferCL()),
|
|
|
|
b3BufferInfoCL(out_overlappingPairs.getBufferCL())};
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
b3LauncherCL launcher(m_queue, m_plbvhCalculateOverlappingPairsKernel, "m_plbvhCalculateOverlappingPairsKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
launcher.setBuffers(bufferInfo, sizeof(bufferInfo) / sizeof(b3BufferInfoCL));
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.setConst(maxPairs);
|
|
|
|
launcher.setConst(numQueryAabbs);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.launch1D(numQueryAabbs);
|
|
|
|
clFinish(m_queue);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
int numLargeAabbRigids = m_largeAabbs.size();
|
2019-01-03 14:26:51 +01:00
|
|
|
if (numLargeAabbRigids > 0 && m_leafNodeAabbs.size() > 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
B3_PROFILE("PLBVH large-small AABB test");
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
int numQueryAabbs = m_leafNodeAabbs.size();
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
b3BufferInfoCL bufferInfo[] =
|
|
|
|
{
|
|
|
|
b3BufferInfoCL(m_leafNodeAabbs.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_largeAabbs.getBufferCL()),
|
|
|
|
|
|
|
|
b3BufferInfoCL(numPairsGpu.getBufferCL()),
|
|
|
|
b3BufferInfoCL(out_overlappingPairs.getBufferCL())};
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
b3LauncherCL launcher(m_queue, m_plbvhLargeAabbAabbTestKernel, "m_plbvhLargeAabbAabbTestKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
launcher.setBuffers(bufferInfo, sizeof(bufferInfo) / sizeof(b3BufferInfoCL));
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.setConst(maxPairs);
|
|
|
|
launcher.setConst(numLargeAabbRigids);
|
|
|
|
launcher.setConst(numQueryAabbs);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.launch1D(numQueryAabbs);
|
|
|
|
clFinish(m_queue);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//
|
|
|
|
int numPairs = -1;
|
|
|
|
numPairsGpu.copyToHostPointer(&numPairs, 1);
|
2019-01-03 14:26:51 +01:00
|
|
|
if (numPairs > maxPairs)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
b3Error("Error running out of pairs: numPairs = %d, maxPairs = %d.\n", numPairs, maxPairs);
|
|
|
|
numPairs = maxPairs;
|
|
|
|
numPairsGpu.copyFromHostPointer(&maxPairs, 1);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
out_overlappingPairs.resize(numPairs);
|
|
|
|
}
|
|
|
|
|
2019-01-03 14:26:51 +01:00
|
|
|
void b3GpuParallelLinearBvh::testRaysAgainstBvhAabbs(const b3OpenCLArray<b3RayInfo>& rays,
|
|
|
|
b3OpenCLArray<int>& out_numRayRigidPairs, b3OpenCLArray<b3Int2>& out_rayRigidPairs)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
B3_PROFILE("PLBVH testRaysAgainstBvhAabbs()");
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
int numRays = rays.size();
|
|
|
|
int maxRayRigidPairs = out_rayRigidPairs.size();
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
int reset = 0;
|
|
|
|
out_numRayRigidPairs.copyFromHostPointer(&reset, 1);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//
|
2019-01-03 14:26:51 +01:00
|
|
|
if (m_leafNodeAabbs.size() > 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
B3_PROFILE("PLBVH ray test small AABB");
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
b3BufferInfoCL bufferInfo[] =
|
|
|
|
{
|
|
|
|
b3BufferInfoCL(m_leafNodeAabbs.getBufferCL()),
|
|
|
|
|
|
|
|
b3BufferInfoCL(m_rootNodeIndex.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_internalNodeChildNodes.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_internalNodeAabbs.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_internalNodeLeafIndexRanges.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_mortonCodesAndAabbIndicies.getBufferCL()),
|
|
|
|
|
|
|
|
b3BufferInfoCL(rays.getBufferCL()),
|
|
|
|
|
|
|
|
b3BufferInfoCL(out_numRayRigidPairs.getBufferCL()),
|
|
|
|
b3BufferInfoCL(out_rayRigidPairs.getBufferCL())};
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
b3LauncherCL launcher(m_queue, m_plbvhRayTraverseKernel, "m_plbvhRayTraverseKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
launcher.setBuffers(bufferInfo, sizeof(bufferInfo) / sizeof(b3BufferInfoCL));
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.setConst(maxRayRigidPairs);
|
|
|
|
launcher.setConst(numRays);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.launch1D(numRays);
|
|
|
|
clFinish(m_queue);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
int numLargeAabbRigids = m_largeAabbs.size();
|
2019-01-03 14:26:51 +01:00
|
|
|
if (numLargeAabbRigids > 0)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
|
|
|
B3_PROFILE("PLBVH ray test large AABB");
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
b3BufferInfoCL bufferInfo[] =
|
|
|
|
{
|
|
|
|
b3BufferInfoCL(m_largeAabbs.getBufferCL()),
|
|
|
|
b3BufferInfoCL(rays.getBufferCL()),
|
|
|
|
|
|
|
|
b3BufferInfoCL(out_numRayRigidPairs.getBufferCL()),
|
|
|
|
b3BufferInfoCL(out_rayRigidPairs.getBufferCL())};
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
b3LauncherCL launcher(m_queue, m_plbvhLargeAabbRayTestKernel, "m_plbvhLargeAabbRayTestKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
launcher.setBuffers(bufferInfo, sizeof(bufferInfo) / sizeof(b3BufferInfoCL));
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.setConst(numLargeAabbRigids);
|
|
|
|
launcher.setConst(maxRayRigidPairs);
|
|
|
|
launcher.setConst(numRays);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.launch1D(numRays);
|
|
|
|
clFinish(m_queue);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//
|
|
|
|
int numRayRigidPairs = -1;
|
|
|
|
out_numRayRigidPairs.copyToHostPointer(&numRayRigidPairs, 1);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
if (numRayRigidPairs > maxRayRigidPairs)
|
2017-08-01 14:30:58 +02:00
|
|
|
b3Error("Error running out of rayRigid pairs: numRayRigidPairs = %d, maxRayRigidPairs = %d.\n", numRayRigidPairs, maxRayRigidPairs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void b3GpuParallelLinearBvh::constructBinaryRadixTree()
|
|
|
|
{
|
|
|
|
B3_PROFILE("b3GpuParallelLinearBvh::constructBinaryRadixTree()");
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
int numLeaves = m_leafNodeAabbs.size();
|
|
|
|
int numInternalNodes = numLeaves - 1;
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//Each internal node is placed in between 2 leaf nodes.
|
|
|
|
//By using this arrangement and computing the common prefix between
|
|
|
|
//these 2 adjacent leaf nodes, it is possible to quickly construct a binary radix tree.
|
|
|
|
{
|
|
|
|
B3_PROFILE("m_computeAdjacentPairCommonPrefixKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
b3BufferInfoCL bufferInfo[] =
|
|
|
|
{
|
|
|
|
b3BufferInfoCL(m_mortonCodesAndAabbIndicies.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_commonPrefixes.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_commonPrefixLengths.getBufferCL())};
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
b3LauncherCL launcher(m_queue, m_computeAdjacentPairCommonPrefixKernel, "m_computeAdjacentPairCommonPrefixKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
launcher.setBuffers(bufferInfo, sizeof(bufferInfo) / sizeof(b3BufferInfoCL));
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.setConst(numInternalNodes);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.launch1D(numInternalNodes);
|
|
|
|
clFinish(m_queue);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
//For each leaf node, select its parent node by
|
2017-08-01 14:30:58 +02:00
|
|
|
//comparing the 2 nearest internal nodes and assign child node indices
|
|
|
|
{
|
|
|
|
B3_PROFILE("m_buildBinaryRadixTreeLeafNodesKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
b3BufferInfoCL bufferInfo[] =
|
|
|
|
{
|
|
|
|
b3BufferInfoCL(m_commonPrefixLengths.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_leafNodeParentNodes.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_internalNodeChildNodes.getBufferCL())};
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
b3LauncherCL launcher(m_queue, m_buildBinaryRadixTreeLeafNodesKernel, "m_buildBinaryRadixTreeLeafNodesKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
launcher.setBuffers(bufferInfo, sizeof(bufferInfo) / sizeof(b3BufferInfoCL));
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.setConst(numLeaves);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.launch1D(numLeaves);
|
|
|
|
clFinish(m_queue);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//For each internal node, perform 2 binary searches among the other internal nodes
|
|
|
|
//to its left and right to find its potential parent nodes and assign child node indices
|
|
|
|
{
|
|
|
|
B3_PROFILE("m_buildBinaryRadixTreeInternalNodesKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
b3BufferInfoCL bufferInfo[] =
|
|
|
|
{
|
|
|
|
b3BufferInfoCL(m_commonPrefixes.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_commonPrefixLengths.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_internalNodeChildNodes.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_internalNodeParentNodes.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_rootNodeIndex.getBufferCL())};
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
b3LauncherCL launcher(m_queue, m_buildBinaryRadixTreeInternalNodesKernel, "m_buildBinaryRadixTreeInternalNodesKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
launcher.setBuffers(bufferInfo, sizeof(bufferInfo) / sizeof(b3BufferInfoCL));
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.setConst(numInternalNodes);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.launch1D(numInternalNodes);
|
|
|
|
clFinish(m_queue);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//Find the number of nodes seperating each internal node and the root node
|
|
|
|
//so that the AABBs can be set using the next kernel.
|
|
|
|
//Also determine the maximum number of nodes separating an internal node and the root node.
|
|
|
|
{
|
|
|
|
B3_PROFILE("m_findDistanceFromRootKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
b3BufferInfoCL bufferInfo[] =
|
|
|
|
{
|
|
|
|
b3BufferInfoCL(m_rootNodeIndex.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_internalNodeParentNodes.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_maxDistanceFromRoot.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_distanceFromRoot.getBufferCL())};
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
b3LauncherCL launcher(m_queue, m_findDistanceFromRootKernel, "m_findDistanceFromRootKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
launcher.setBuffers(bufferInfo, sizeof(bufferInfo) / sizeof(b3BufferInfoCL));
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.setConst(numInternalNodes);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.launch1D(numInternalNodes);
|
|
|
|
clFinish(m_queue);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//Starting from the internal nodes nearest to the leaf nodes, recursively move up
|
|
|
|
//the tree towards the root to set the AABBs of each internal node; each internal node
|
|
|
|
//checks its children and merges their AABBs
|
|
|
|
{
|
|
|
|
B3_PROFILE("m_buildBinaryRadixTreeAabbsRecursiveKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
int maxDistanceFromRoot = -1;
|
|
|
|
{
|
|
|
|
B3_PROFILE("copy maxDistanceFromRoot to CPU");
|
|
|
|
m_maxDistanceFromRoot.copyToHostPointer(&maxDistanceFromRoot, 1);
|
|
|
|
clFinish(m_queue);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
|
|
|
for (int distanceFromRoot = maxDistanceFromRoot; distanceFromRoot >= 0; --distanceFromRoot)
|
2017-08-01 14:30:58 +02:00
|
|
|
{
|
2019-01-03 14:26:51 +01:00
|
|
|
b3BufferInfoCL bufferInfo[] =
|
|
|
|
{
|
|
|
|
b3BufferInfoCL(m_distanceFromRoot.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_mortonCodesAndAabbIndicies.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_internalNodeChildNodes.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_leafNodeAabbs.getBufferCL()),
|
|
|
|
b3BufferInfoCL(m_internalNodeAabbs.getBufferCL())};
|
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
b3LauncherCL launcher(m_queue, m_buildBinaryRadixTreeAabbsRecursiveKernel, "m_buildBinaryRadixTreeAabbsRecursiveKernel");
|
2019-01-03 14:26:51 +01:00
|
|
|
launcher.setBuffers(bufferInfo, sizeof(bufferInfo) / sizeof(b3BufferInfoCL));
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.setConst(maxDistanceFromRoot);
|
|
|
|
launcher.setConst(distanceFromRoot);
|
|
|
|
launcher.setConst(numInternalNodes);
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
//It may seem inefficent to launch a thread for each internal node when a
|
|
|
|
//much smaller number of nodes is actually processed, but this is actually
|
2019-01-03 14:26:51 +01:00
|
|
|
//faster than determining the exact nodes that are ready to merge their child AABBs.
|
2017-08-01 14:30:58 +02:00
|
|
|
launcher.launch1D(numInternalNodes);
|
|
|
|
}
|
2019-01-03 14:26:51 +01:00
|
|
|
|
2017-08-01 14:30:58 +02:00
|
|
|
clFinish(m_queue);
|
|
|
|
}
|
|
|
|
}
|