2 Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
3 Copyright (C) 2006, 2007 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.
16 #ifndef BT_CUDA_DEMO_DYNAMICS_WORLD3D_H
17 #define BT_CUDA_DEMO_DYNAMICS_WORLD3D_H
19 #include "BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h"
21 //#define BT_USE_CUDA 1
23 // 1. Uncomment //#define BT_USE_CUDA 1
24 // 2. Build and add libbulletcuda (Extras/CUDA) to project
25 // 3. Add $(CUDA_LIB_PATH) and cudart.lib to linker properties
29 #include "BulletMultiThreaded/btGpuDefines.h"
31 #define BT_GPU_PREF(func) btCuda_##func
32 #include "BulletMultiThreaded/btGpuUtilsSharedDefs.h"
34 #include "BulletMultiThreaded/btGpuDefines.h"
35 #include "../../src/BulletMultiThreaded/btGpuUtilsSharedDefs.h"
42 #include <vector_types.h>
43 #define BT_GPU_PREF(func) btCuda_##func
44 #include "../../src/BulletMultiThreaded/btGpuUtilsSharedDefs.h"
48 #include "btGpuDemo3dSharedTypes.h"
50 //#define CUDA_DEMO_DYNAMICS_WORLD3D_MAX_BATCHES 20
51 #define CUDA_DEMO_DYNAMICS_WORLD3D_MAX_BATCHES 15
53 class btCudaDemoDynamicsWorld3D : public btDiscreteDynamicsWorld
59 int m_maxPointsPerConstr;
64 bool m_useSeqImpSolver;
65 bool m_useCudaMotIntegr;
66 bool m_copyIntegrDataToGPU;
75 float* m_dLambdaDtBox;
76 float* m_dPositionConstraint;
79 float* m_dForceTorqueDamp;
80 float* m_dInvInertiaMass;
86 int* m_hConstraintBuffer;
87 int* m_hConstraintCounter;
97 // ------------- these are only needed for CPU version and for debugging
98 float* m_hLambdaDtBox;
99 float* m_hPositionConstraint;
105 btVector3 m_worldMin;
106 btVector3 m_worldMax;
108 //-------------------------------
109 int* m_hConstraintUsed;
111 //-------------------------------
113 float* m_hForceTorqueDamp;
114 float* m_hInvInertiaMass;
117 int m_numInBatches[CUDA_DEMO_DYNAMICS_WORLD3D_MAX_BATCHES];
120 btCudaDemoDynamicsWorld3D(btDispatcher* dispatcher,btBroadphaseInterface* pairCache,btConstraintSolver* constraintSolver,btCollisionConfiguration* collisionConfiguration, int maxPointsPerConstr = 4)
121 : btDiscreteDynamicsWorld(dispatcher, pairCache, constraintSolver, collisionConfiguration)
123 m_useCPUSolver = false;
124 m_useSeqImpSolver = false;
125 m_useCudaMotIntegr = true;
126 m_copyIntegrDataToGPU = true;
129 m_maxConstr = m_maxObj * m_maxNeihbors;
130 int sz = m_maxConstr;
131 m_hConstraintBuffer = new int[sz];
132 m_hConstraintCounter = new int[m_maxObj];
133 m_maxBatches = CUDA_DEMO_DYNAMICS_WORLD3D_MAX_BATCHES;
134 m_hIds = new int2[sz];
135 m_hBatchIds = new int[sz];
136 for(int i = 0; i < sz; i++)
140 m_hTrans = new float4[m_maxObj * 4];
141 m_hVel = new float4[m_maxObj];
142 m_hAngVel = new float4[m_maxObj];
144 m_maxPointsPerConstr = maxPointsPerConstr;
147 btCuda_allocateArray((void**)&m_dTrans, sizeof(float4) * m_maxObj * 4);
148 btCuda_allocateArray((void**)&m_dVel, sizeof(float4) * m_maxObj);
149 btCuda_allocateArray((void**)&m_dAngVel, sizeof(float4) * m_maxObj);
151 btCuda_allocateArray((void**)&m_dIds, sizeof(int2) * sz);
152 btCuda_allocateArray((void**)&m_dBatchIds, sizeof(int) * sz);
155 btCuda_allocateArray((void**)&m_dLambdaDtBox, sizeof(float) * sz * m_maxPointsPerConstr);
156 btCuda_allocateArray((void**)&m_dPositionConstraint, sizeof(float) * sz * m_maxPointsPerConstr);
157 btCuda_allocateArray((void**)&m_dNormal, sizeof(float3) * sz * m_maxPointsPerConstr);
158 btCuda_allocateArray((void**)&m_dContact, sizeof(float3) * sz * m_maxPointsPerConstr);
160 btCuda_allocateArray((void**)&m_dForceTorqueDamp, sizeof(float) * m_maxObj * 4 * 2);
161 btCuda_allocateArray((void**)&m_dInvInertiaMass, sizeof(float) * m_maxObj * 4 * 3);
164 m_hLambdaDtBox = new float[sz * m_maxPointsPerConstr];
165 m_hPositionConstraint = new float[sz * m_maxPointsPerConstr];
166 m_hNormal = new float3[sz * m_maxPointsPerConstr];
167 m_hContact = new float3[sz * m_maxPointsPerConstr];
173 m_hConstraintUsed = new int[sz];
175 m_hForceTorqueDamp = new float[m_maxObj * 4 * 2];
176 m_hInvInertiaMass = new float[4 * m_maxObj * 3];
179 virtual ~btCudaDemoDynamicsWorld3D()
181 delete [] m_hConstraintBuffer;
182 delete [] m_hConstraintCounter;
184 delete [] m_hBatchIds;
189 delete [] m_hLambdaDtBox;
190 delete [] m_hPositionConstraint;
192 delete [] m_hContact;
193 delete [] m_hConstraintUsed;
195 delete [] m_hForceTorqueDamp;
196 delete [] m_hInvInertiaMass;
200 btCuda_freeArray(m_dTrans);
201 btCuda_freeArray(m_dVel);
202 btCuda_freeArray(m_dAngVel);
204 btCuda_freeArray(m_dIds);
205 btCuda_freeArray(m_dBatchIds);
206 btCuda_freeArray(m_dLambdaDtBox);
207 btCuda_freeArray(m_dPositionConstraint);
208 btCuda_freeArray(m_dNormal);
209 btCuda_freeArray(m_dContact);
210 btCuda_freeArray(m_dForceTorqueDamp);
211 btCuda_freeArray(m_dInvInertiaMass);
215 virtual void calculateSimulationIslands()
217 if(m_useSeqImpSolver)
219 btDiscreteDynamicsWorld::calculateSimulationIslands();
222 virtual void solveConstraints(btContactSolverInfo& solverInfo);
224 virtual void predictUnconstraintMotion(btScalar timeStep);
225 virtual void integrateTransforms(btScalar timeStep);
230 void solveConstraintsCPU(btContactSolverInfo& solverInfo);
232 void debugDrawConstraints(int selectedBatch, const float* pColorTab);
234 void setObjRad(btScalar rad) { m_objRad = rad; }
235 void setWorldMin(const btVector3& worldMin) { m_worldMin = worldMin; }
236 void setWorldMax(const btVector3& worldMax) { m_worldMax = worldMax; }
240 void grabConstrData();
241 void createBatches();
242 void copyDataToGPU();
243 void copyDataFromGPU();
244 void writebackData();
245 void setUseCPUSolver(bool useCPU) { m_useCPUSolver = useCPU; }
246 void setUseSeqImpSolver(bool useSeqImpSolver) { m_useSeqImpSolver = useSeqImpSolver; }
247 void setUseCudaMotIntegr(bool useCudaMotIntegr) { m_useCudaMotIntegr = useCudaMotIntegr; }
248 void resetScene(void) { m_copyIntegrDataToGPU = true; }
252 #endif //BT_CUDA_DEMO_DYNAMICS_WORLD3D_H