2 Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
\r
3 Copyright (C) 2006 - 2009 Sony Computer Entertainment Inc.
\r
5 This software is provided 'as-is', without any express or implied warranty.
\r
6 In no event will the authors be held liable for any damages arising from the use of this software.
\r
7 Permission is granted to anyone to use this software for any purpose,
\r
8 including commercial applications, and to alter it and redistribute it freely,
\r
9 subject to the following restrictions:
\r
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.
\r
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
\r
13 3. This notice may not be removed or altered from any source distribution.
\r
17 #ifndef BT_PARTICLES_DYNAMICS_WORLD_H
\r
18 #define BT_PARTICLES_DYNAMICS_WORLD_H
\r
22 #include <MiniCL/cl.h>
\r
23 #include <MiniCL/cl_gl.h>
\r
26 #include <OpenCL/cl.h>
\r
29 #include <CL/cl_gl.h>
\r
38 #include "BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h"
\r
39 #include "BulletDynamics/ConstraintSolver/btTypedConstraint.h"
\r
40 #include "BulletDynamics/ConstraintSolver/btPoint2PointConstraint.h"
\r
42 #include "btParticlesSharedDefs.h"
\r
43 #include "btParticlesSharedTypes.h"
\r
46 #define PARTICLES_MAX_NEIGHBORS (32)
\r
47 #define DEF_PARTICLE_RADIUS (0.023f)
\r
48 //#define WORLD_SIZE 1.9f
\r
49 #define WORLD_SIZE 1.f
\r
53 PARTICLES_KERNEL_INTEGRATE_MOTION = 0,
\r
54 PARTICLES_KERNEL_COMPUTE_CELL_ID,
\r
55 PARTICLES_KERNEL_CLEAR_CELL_START,
\r
56 PARTICLES_KERNEL_FIND_CELL_START,
\r
57 PARTICLES_KERNEL_COLLIDE_PARTICLES,
\r
58 PARTICLES_KERNEL_BITONIC_SORT_CELL_ID_LOCAL,
\r
59 PARTICLES_KERNEL_BITONIC_SORT_CELL_ID_LOCAL_1,
\r
60 PARTICLES_KERNEL_BITONIC_SORT_CELL_ID_MERGE_GLOBAL,
\r
61 PARTICLES_KERNEL_BITONIC_SORT_CELL_ID_MERGE_LOCAL,
\r
62 PARTICLES_KERNEL_TOTAL
\r
69 SIMSTAGE_INTEGRATE_MOTION,
\r
70 SIMSTAGE_COMPUTE_CELL_ID,
\r
71 SIMSTAGE_SORT_CELL_ID,
\r
72 SIMSTAGE_FIND_CELL_START,
\r
73 SIMSTAGE_COLLIDE_PARTICLES,
\r
82 int m_workgroupSize;
\r
85 class btParticlesDynamicsWorld : public btDiscreteDynamicsWorld
\r
90 btScalar m_particleRad;
\r
91 struct GL_ToggleControl* m_useCpuControls[SIMSTAGE_TOTAL];
\r
94 int m_hashSize; // power of 2 >= m_numSpheres;
\r
95 int m_numGridCells;
\r
97 int m_numSolverIterations;
\r
100 btAlignedObjectArray<btVector3> m_hPos;
\r
101 btAlignedObjectArray<btVector3> m_hVel;
\r
102 btAlignedObjectArray<btVector3> m_hSortedPos;
\r
103 btAlignedObjectArray<btVector3> m_hSortedVel;
\r
105 btAlignedObjectArray<btInt2> m_hPosHash;
\r
106 btAlignedObjectArray<int> m_hCellStart;
\r
111 cl_mem m_dCellStart;
\r
112 cl_mem m_dSimParams; // copy of m_simParams : global simulation paramerers such as gravity, etc.
\r
113 cl_mem m_dSortedPos;
\r
114 cl_mem m_dSortedVel;
\r
117 cl_context m_cxMainContext;
\r
118 cl_device_id m_cdDevice;
\r
119 cl_command_queue m_cqCommandQue;
\r
120 cl_program m_cpProgram;
\r
122 btKernelInfo m_kernels[PARTICLES_KERNEL_TOTAL];
\r
124 btVector3 m_cellSize;
\r
127 btVector3 m_worldMin;
\r
128 btVector3 m_worldMax;
\r
131 unsigned int m_posVbo;
\r
132 unsigned int m_colVbo;
\r
133 btSimParams m_simParams;
\r
136 int getNumParticles() { return m_numParticles; }
\r
137 float* getPosBuffer() { return (float*)&(m_hPos[0]); }
\r
140 btParticlesDynamicsWorld(btDispatcher* dispatcher,btBroadphaseInterface* pairCache,btConstraintSolver* constraintSolver,btCollisionConfiguration* collisionConfiguration,
\r
141 int maxObjs , int maxNeighbors = PARTICLES_MAX_NEIGHBORS)
\r
142 : btDiscreteDynamicsWorld(dispatcher, pairCache, constraintSolver, collisionConfiguration)
\r
144 m_cxMainContext = 0;
\r
146 // m_particleRad = btScalar(0.5f);
\r
147 m_particleRad = DEF_PARTICLE_RADIUS;
\r
148 m_simParams.m_gravity[0] = 0.f;
\r
149 m_simParams.m_gravity[1] = -10.f;
\r
150 m_simParams.m_gravity[2] = 0.f;
\r
151 m_simParams.m_gravity[3] = 0.f;
\r
152 m_numSolverIterations = 4;
\r
154 virtual ~btParticlesDynamicsWorld();
\r
155 virtual int stepSimulation( btScalar timeStep,int maxSubSteps=1, btScalar fixedTimeStep=btScalar(1.)/btScalar(60.));
\r
157 void initDeviceData();
\r
158 void initCLKernels(int argc, char** argv);
\r
160 void postInitDeviceData();
\r
161 void getShapeData();
\r
162 void allocateBuffers();
\r
163 void grabSimulationData();
\r
165 void runIntegrateMotionKernel();
\r
166 void runComputeCellIdKernel();
\r
167 void runSortHashKernel();
\r
168 void runFindCellStartKernel();
\r
169 void runCollideParticlesKernel();
\r
171 void initKernel(int kernelId, const char* pName);
\r
172 void runKernelWithWorkgroupSize(int kernelId, int globalSize);
\r
173 void bitonicSortNv(cl_mem pKey, unsigned int batch, unsigned int arrayLength, unsigned int dir);
\r
175 void scanExclusiveLocal1(cl_mem d_Dst, cl_mem d_Src, unsigned int n, unsigned int size);
\r
176 void scanExclusiveLocal2(cl_mem d_Buffer, cl_mem d_Dst, cl_mem d_Src, unsigned int n, unsigned int size);
\r
177 void uniformUpdate(cl_mem d_Dst, cl_mem d_Buffer, unsigned int n);
\r
178 void scanExclusive(cl_mem d_Dst, cl_mem d_Src, unsigned int arrayLength);
\r
183 #endif //BT_PARTICLES_DYNAMICS_WORLD_H
\r