Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / Gpu3dDemo / btGpuDemoDynamicsWorld3D.h
1 /*
2 Bullet Continuous Collision Detection and Physics Library, http://bulletphysics.org
3 Copyright (C) 2006, 2007 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 #ifndef BT_CUDA_DEMO_DYNAMICS_WORLD3D_H
17 #define BT_CUDA_DEMO_DYNAMICS_WORLD3D_H
18
19 #include "BulletDynamics/Dynamics/btDiscreteDynamicsWorld.h"
20
21 //#define BT_USE_CUDA 1
22 // To enable CUDA : 
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
26
27
28 #ifdef BT_USE_CUDA
29         #include "BulletMultiThreaded/btGpuDefines.h"
30         #undef BT_GPU_PREF
31         #define BT_GPU_PREF(func) btCuda_##func
32         #include "BulletMultiThreaded/btGpuUtilsSharedDefs.h"
33 #else
34         #include "BulletMultiThreaded/btGpuDefines.h"
35         #include "../../src/BulletMultiThreaded/btGpuUtilsSharedDefs.h"
36 #endif
37
38 #undef BT_GPU_PREF
39
40
41 #if 0 // ###
42 #include <vector_types.h>
43 #define BT_GPU_PREF(func) btCuda_##func
44 #include "../../src/BulletMultiThreaded/btGpuUtilsSharedDefs.h"
45 #undef BT_GPU_PREF
46 #endif
47
48 #include "btGpuDemo3dSharedTypes.h"
49
50 //#define CUDA_DEMO_DYNAMICS_WORLD3D_MAX_BATCHES 20
51 #define CUDA_DEMO_DYNAMICS_WORLD3D_MAX_BATCHES 15
52
53 class btCudaDemoDynamicsWorld3D : public btDiscreteDynamicsWorld
54 {
55 protected:
56         int                                             m_maxObj;
57         int                                             m_maxNeihbors;
58         int                                             m_maxConstr;
59         int                                             m_maxPointsPerConstr;
60
61         int                                             m_numObj;
62         int                                             m_numSimStep;
63         bool                                    m_useCPUSolver;
64         bool                                    m_useSeqImpSolver;
65         bool                                    m_useCudaMotIntegr;
66         bool                                    m_copyIntegrDataToGPU;
67
68
69 #ifdef BT_USE_CUDA
70         float4*                                 m_dTrans;
71         float4*                                 m_dVel;
72         float4*                                 m_dAngVel;
73         int2*                                   m_dIds;
74         int*                                    m_dBatchIds;
75         float*                                  m_dLambdaDtBox;
76         float*                                  m_dPositionConstraint;
77         float3*                                 m_dNormal;
78         float3*                                 m_dContact;
79         float*                                  m_dForceTorqueDamp;
80         float*                                  m_dInvInertiaMass;
81 #endif
82
83         float4*                                 m_hTrans;
84         float4*                                 m_hVel;
85         float4*                                 m_hAngVel;
86         int*                                    m_hConstraintBuffer;
87         int*                                    m_hConstraintCounter;
88         int                                             m_maxBatches;
89         int                                             m_numBatches;
90         int                                             m_numConstraints;
91         int2*                                   m_hIds;
92         int*                                    m_hBatchIds;
93         
94         int                                             m_maxVtxPerObj;
95
96
97         // ------------- these are only needed for CPU version and for debugging
98         float*                                  m_hLambdaDtBox;
99         float*                                  m_hPositionConstraint;
100         float3*                                 m_hNormal;
101         float3*                                 m_hContact;
102         // ------------- 
103
104         btScalar                                m_objRad;
105         btVector3                               m_worldMin;
106         btVector3                               m_worldMax;
107         
108         //-------------------------------
109         int*                                    m_hConstraintUsed;
110
111         //-------------------------------
112
113         float*                                  m_hForceTorqueDamp;
114         float*                                  m_hInvInertiaMass;
115
116 public:
117         int                                             m_numInBatches[CUDA_DEMO_DYNAMICS_WORLD3D_MAX_BATCHES];
118
119
120         btCudaDemoDynamicsWorld3D(btDispatcher* dispatcher,btBroadphaseInterface* pairCache,btConstraintSolver* constraintSolver,btCollisionConfiguration* collisionConfiguration, int maxPointsPerConstr = 4)
121                 : btDiscreteDynamicsWorld(dispatcher, pairCache, constraintSolver, collisionConfiguration)
122         { 
123                 m_useCPUSolver = false;
124                 m_useSeqImpSolver = false;
125                 m_useCudaMotIntegr = true;
126                 m_copyIntegrDataToGPU = true;
127                 m_maxObj = 32768;
128                 m_maxNeihbors = 26;
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++)
137                 {
138                         m_hBatchIds[i] = -1;
139                 }
140                 m_hTrans = new float4[m_maxObj * 4];
141                 m_hVel = new float4[m_maxObj];
142                 m_hAngVel = new float4[m_maxObj];
143
144                 m_maxPointsPerConstr = maxPointsPerConstr;
145
146 #ifdef BT_USE_CUDA
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);
150
151                 btCuda_allocateArray((void**)&m_dIds, sizeof(int2) * sz);
152                 btCuda_allocateArray((void**)&m_dBatchIds, sizeof(int) * sz);
153
154
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);
159
160                 btCuda_allocateArray((void**)&m_dForceTorqueDamp, sizeof(float) * m_maxObj * 4 * 2);
161                 btCuda_allocateArray((void**)&m_dInvInertiaMass, sizeof(float) * m_maxObj * 4 * 3);
162 #endif
163
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];
168
169                 m_numSimStep = 0;
170
171                 m_objRad = 1.0f;
172
173                 m_hConstraintUsed = new int[sz];
174
175                 m_hForceTorqueDamp = new float[m_maxObj * 4 * 2];
176                 m_hInvInertiaMass = new float[4 * m_maxObj * 3];
177
178         }
179         virtual ~btCudaDemoDynamicsWorld3D()
180         {
181                 delete [] m_hConstraintBuffer;
182                 delete [] m_hConstraintCounter;
183                 delete [] m_hIds;
184                 delete [] m_hBatchIds;
185                 delete [] m_hTrans;
186                 delete [] m_hVel;
187                 delete [] m_hAngVel;
188
189                 delete [] m_hLambdaDtBox;
190                 delete [] m_hPositionConstraint;
191                 delete [] m_hNormal;
192                 delete [] m_hContact;
193                 delete [] m_hConstraintUsed;
194
195                 delete [] m_hForceTorqueDamp;
196                 delete [] m_hInvInertiaMass;
197
198
199 #ifdef BT_USE_CUDA
200                 btCuda_freeArray(m_dTrans);
201                 btCuda_freeArray(m_dVel);
202                 btCuda_freeArray(m_dAngVel);
203
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);
212 #endif
213
214         }
215         virtual void    calculateSimulationIslands()
216         {
217                 if(m_useSeqImpSolver)
218                 {
219                         btDiscreteDynamicsWorld::calculateSimulationIslands();
220                 }
221         }
222         virtual void    solveConstraints(btContactSolverInfo& solverInfo);
223
224         virtual void    predictUnconstraintMotion(btScalar timeStep);
225         virtual void    integrateTransforms(btScalar timeStep);
226
227
228
229
230         void    solveConstraintsCPU(btContactSolverInfo& solverInfo);
231
232         void debugDrawConstraints(int selectedBatch, const float* pColorTab);
233
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; }
237
238         void grabData();
239         void grabObjData();
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; }
249 };
250
251
252 #endif //BT_CUDA_DEMO_DYNAMICS_WORLD3D_H