2 Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
3 Copyright (C) 2006 - 2009 Sony Computer Entertainment Inc.
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
18 #ifndef BT3DGRIDBROADPHASEOCL_H
19 #define BT3DGRIDBROADPHASEOCL_H
23 #include <MiniCL/cl.h>
25 #include <MiniCL/cl.h>
27 //CL_PLATFORM_MINI_CL could be defined in build system
29 //#include <GL/glew.h>
30 // standard utility and system includes
32 #include <MiniCL/cl.h>
36 // Extra CL/GL include
37 //#include <CL/cl_gl.h>
46 #include "BulletCollision/BroadphaseCollision/btSimpleBroadphase.h"
47 #include "btGpu3DGridBroadphaseSharedTypes.h"
48 #include "btGpu3DGridBroadphase.h"
51 #define GRID3DOCL_CHECKERROR(a, b) if((a)!=(b)) { printf("3D GRID OCL Error : %d\n", (a)); btAssert((a) == (b)); }
55 GRID3DOCL_KERNEL_CALC_HASH_AABB = 0,
56 GRID3DOCL_KERNEL_CLEAR_CELL_START,
57 GRID3DOCL_KERNEL_FIND_CELL_START,
58 GRID3DOCL_KERNEL_FIND_OVERLAPPING_PAIRS,
59 GRID3DOCL_KERNEL_FIND_PAIRS_LARGE,
60 GRID3DOCL_KERNEL_COMPUTE_CACHE_CHANGES,
61 GRID3DOCL_KERNEL_SQUEEZE_PAIR_BUFF,
62 GRID3DOCL_KERNEL_TOTAL
65 struct bt3dGridOCLKernelInfo
74 ///The bt3dGridBroadphaseOCL uses OpenCL-capable GPU to compute overlapping pairs
76 class bt3dGridBroadphaseOCL : public btGpu3DGridBroadphase
80 cl_context m_cxMainContext;
81 cl_device_id m_cdDevice;
82 cl_command_queue m_cqCommandQue;
83 cl_program m_cpProgram;
84 bt3dGridOCLKernelInfo m_kernels[GRID3DOCL_KERNEL_TOTAL];
89 cl_mem m_dPairBuffStartCurr;
93 cl_mem m_dPairScanChanged;
94 cl_mem m_dPairsChanged;
95 cl_mem m_dPairsContiguous;
98 adl::Device* m_deviceHost;
99 adl::DeviceCL* m_deviceCL;
104 unsigned int m_numPrefixSum;
106 bt3dGridBroadphaseOCL( btOverlappingPairCache* overlappingPairCache,
107 const btVector3& cellSize,
108 int gridSizeX, int gridSizeY, int gridSizeZ,
109 int maxSmallProxies, int maxLargeProxies, int maxPairsPerSmallProxy,
110 btScalar maxSmallProxySize,
111 int maxSmallProxiesPerCell = 8,
112 cl_context context = NULL,
113 cl_device_id device = NULL,
114 cl_command_queue queue = NULL,
115 adl::DeviceCL* deviceCL = 0
117 virtual ~bt3dGridBroadphaseOCL();
120 void initCL(cl_context context, cl_device_id device, cl_command_queue queue);
122 void allocateBuffers();
123 void prefillBuffers();
124 void initKernel(int kernelId, char* pName);
125 void allocateArray(void** devPtr, unsigned int size);
126 void freeArray(void* devPtr);
127 void runKernelWithWorkgroupSize(int kernelId, int globalSize);
128 void setKernelArg(int kernelId, int argNum, int argSize, void* argPtr);
129 void copyArrayToDevice(cl_mem device, const void* host, unsigned int size, int devOffs = 0, int hostOffs = 0);
130 void copyArrayFromDevice(void* host, const cl_mem device, unsigned int size, int hostOffs = 0, int devOffs = 0);
133 virtual void setParameters(bt3DGridBroadphaseParams* hostParams);
134 virtual void prepareAABB();
135 virtual void calcHashAABB();
136 virtual void sortHash();
137 virtual void findCellStart();
138 virtual void findOverlappingPairs();
139 virtual void findPairsLarge();
140 virtual void computePairCacheChanges();
141 virtual void scanOverlappingPairBuff(bool copyToCpu=true);
142 virtual void squeezeOverlappingPairBuff();
143 virtual void resetPool(btDispatcher* dispatcher);
146 #endif //BT3DGRIDBROADPHASEOCL_H