1 #include "b3PrefixScanFloat4CL.h"
3 #define B3_PREFIXSCAN_FLOAT4_PROG_PATH "src/Bullet3OpenCL/ParallelPrimitives/kernels/PrefixScanFloat4Kernels.cl"
5 #include "b3LauncherCL.h"
6 #include "Bullet3OpenCL/Initialize/b3OpenCLUtils.h"
7 #include "kernels/PrefixScanKernelsFloat4CL.h"
9 b3PrefixScanFloat4CL::b3PrefixScanFloat4CL(cl_context ctx, cl_device_id device, cl_command_queue queue, int size)
10 : m_commandQueue(queue)
12 const char* scanKernelSource = prefixScanKernelsFloat4CL;
14 char* additionalMacros = 0;
16 m_workBuffer = new b3OpenCLArray<b3Vector3>(ctx, queue, size);
17 cl_program scanProg = b3OpenCLUtils::compileCLProgramFromString(ctx, device, scanKernelSource, &pErrNum, additionalMacros, B3_PREFIXSCAN_FLOAT4_PROG_PATH);
20 m_localScanKernel = b3OpenCLUtils::compileCLKernelFromString(ctx, device, scanKernelSource, "LocalScanKernel", &pErrNum, scanProg, additionalMacros);
21 b3Assert(m_localScanKernel);
22 m_blockSumKernel = b3OpenCLUtils::compileCLKernelFromString(ctx, device, scanKernelSource, "TopLevelScanKernel", &pErrNum, scanProg, additionalMacros);
23 b3Assert(m_blockSumKernel);
24 m_propagationKernel = b3OpenCLUtils::compileCLKernelFromString(ctx, device, scanKernelSource, "AddOffsetKernel", &pErrNum, scanProg, additionalMacros);
25 b3Assert(m_propagationKernel);
28 b3PrefixScanFloat4CL::~b3PrefixScanFloat4CL()
31 clReleaseKernel(m_localScanKernel);
32 clReleaseKernel(m_blockSumKernel);
33 clReleaseKernel(m_propagationKernel);
40 for (int i = 0; i < sizeof(T) * 8; i++)
45 void b3PrefixScanFloat4CL::execute(b3OpenCLArray<b3Vector3>& src, b3OpenCLArray<b3Vector3>& dst, int n, b3Vector3* sum)
47 // b3Assert( data->m_option == EXCLUSIVE );
48 const unsigned int numBlocks = (const unsigned int)((n + BLOCK_SIZE * 2 - 1) / (BLOCK_SIZE * 2));
50 dst.resize(src.size());
51 m_workBuffer->resize(src.size());
55 constBuffer.y = numBlocks;
56 constBuffer.z = (int)b3NextPowerOf2(numBlocks);
58 b3OpenCLArray<b3Vector3>* srcNative = &src;
59 b3OpenCLArray<b3Vector3>* dstNative = &dst;
62 b3BufferInfoCL bInfo[] = {b3BufferInfoCL(dstNative->getBufferCL()), b3BufferInfoCL(srcNative->getBufferCL()), b3BufferInfoCL(m_workBuffer->getBufferCL())};
64 b3LauncherCL launcher(m_commandQueue, m_localScanKernel, "m_localScanKernel");
65 launcher.setBuffers(bInfo, sizeof(bInfo) / sizeof(b3BufferInfoCL));
66 launcher.setConst(constBuffer);
67 launcher.launch1D(numBlocks * BLOCK_SIZE, BLOCK_SIZE);
71 b3BufferInfoCL bInfo[] = {b3BufferInfoCL(m_workBuffer->getBufferCL())};
73 b3LauncherCL launcher(m_commandQueue, m_blockSumKernel, "m_blockSumKernel");
74 launcher.setBuffers(bInfo, sizeof(bInfo) / sizeof(b3BufferInfoCL));
75 launcher.setConst(constBuffer);
76 launcher.launch1D(BLOCK_SIZE, BLOCK_SIZE);
81 b3BufferInfoCL bInfo[] = {b3BufferInfoCL(dstNative->getBufferCL()), b3BufferInfoCL(m_workBuffer->getBufferCL())};
82 b3LauncherCL launcher(m_commandQueue, m_propagationKernel, "m_propagationKernel");
83 launcher.setBuffers(bInfo, sizeof(bInfo) / sizeof(b3BufferInfoCL));
84 launcher.setConst(constBuffer);
85 launcher.launch1D((numBlocks - 1) * BLOCK_SIZE, BLOCK_SIZE);
90 clFinish(m_commandQueue);
91 dstNative->copyToHostPointer(sum, 1, n - 1, true);
95 void b3PrefixScanFloat4CL::executeHost(b3AlignedObjectArray<b3Vector3>& src, b3AlignedObjectArray<b3Vector3>& dst, int n, b3Vector3* sum)
97 b3Vector3 s = b3MakeVector3(0, 0, 0);
98 //if( data->m_option == EXCLUSIVE )
100 for (int i = 0; i < n; i++)
108 for(int i=0; i<n; i++)