Initialize libbullet git in 2.0_beta.
[platform/upstream/libbullet.git] / Extras / RigidBodyGpuPipeline / opencl / 3dGridBroadphase / Shared / bt3dGridBroadphaseOCL.h
1 /*
2 Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
3 Copyright (C) 2006 - 2009 Sony Computer Entertainment Inc. 
4
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:
10
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.
14 */
15
16
17
18 #ifndef BT3DGRIDBROADPHASEOCL_H
19 #define BT3DGRIDBROADPHASEOCL_H
20
21 #ifdef __APPLE__
22 #ifdef USE_MINICL
23         #include <MiniCL/cl.h>
24 #else
25         #include <MiniCL/cl.h>
26 #endif
27 //CL_PLATFORM_MINI_CL could be defined in build system
28 #else
29 //#include <GL/glew.h>
30 // standard utility and system includes
31 #ifdef USE_MINICL
32         #include <MiniCL/cl.h>
33 #else
34         #include <CL/cl.h>
35 #endif
36 // Extra CL/GL include
37 //#include <CL/cl_gl.h>
38 #endif //__APPLE__
39
40 namespace adl
41 {
42         struct Device;
43         struct DeviceCL;
44 };
45
46 #include "BulletCollision/BroadphaseCollision/btSimpleBroadphase.h"
47 #include "btGpu3DGridBroadphaseSharedTypes.h"
48 #include "btGpu3DGridBroadphase.h"
49
50
51 #define GRID3DOCL_CHECKERROR(a, b) if((a)!=(b)) { printf("3D GRID OCL Error : %d\n", (a)); btAssert((a) == (b)); }
52
53 enum
54 {
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
63 };
64
65 struct bt3dGridOCLKernelInfo
66 {
67         int                     m_Id;
68         cl_kernel       m_kernel;
69         char*           m_name;
70         int                     m_workgroupSize;
71 };
72
73
74 ///The bt3dGridBroadphaseOCL uses OpenCL-capable GPU to compute overlapping pairs
75
76 class bt3dGridBroadphaseOCL : public btGpu3DGridBroadphase
77 {
78 protected:
79         int                                             m_hashSize;
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];
85         // data buffers
86         cl_mem                                  m_dBodiesHash;
87         cl_mem                                  m_dCellStart;
88         cl_mem                                  m_dPairBuff; 
89         cl_mem                                  m_dPairBuffStartCurr;
90 public:
91         cl_mem                                  m_dAABB;
92 protected:
93         cl_mem                                  m_dPairScanChanged;
94         cl_mem                                  m_dPairsChanged;
95         cl_mem                                  m_dPairsContiguous;
96         cl_mem                                  m_dBpParams;
97
98         adl::Device*                    m_deviceHost;
99         adl::DeviceCL*                  m_deviceCL;
100         bool                                    m_ownsDevice;
101
102
103 public:
104         unsigned int                    m_numPrefixSum;
105
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
116                                                         );
117         virtual ~bt3dGridBroadphaseOCL();
118
119 protected:
120         void initCL(cl_context context, cl_device_id device, cl_command_queue queue);
121         void initKernels();
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);
131
132 // overrides
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);
144 };
145
146 #endif //BT3DGRIDBROADPHASEOCL_H