File: dgWorldDynamicUpdate.h

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (409 lines) | stat: -rw-r--r-- 11,312 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* Copyright (c) <2003-2011> <Julio Jerez, Newton Game Dynamics>
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 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.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/

#if !defined(AFX_DYNAMIC_UPDATE_H__EC18C699_D48D_448F_A510_A865B2CC0789__INCLUDED_)
#define AFX_DYNAMIC_UPDATE_H__EC18C699_D48D_448F_A510_A865B2CC0789__INCLUDED_


#include "dgPhysicsStdafx.h"


//#define DG_PSD_DAMP_TOL           dgFloat32 (1.0e-2f)
#define DG_PSD_DAMP_TOL             dgFloat32 (1.0e-3f)
//#define DG_PSD_DAMP_TOL           dgFloat32 (1.0e-4f)


#define LINEAR_SOLVER_SUB_STEPS         4


class dgIsland;
class dgJointInfo;
class dgBodyInfo;
class dgJacobianMemory;
class dgWorldDynamicUpdate;


class dgIslandCallbackStruct {
public:
	dgWorld *m_world;
	dgInt32 m_count;
	dgInt32 m_strideInByte;
	void *m_bodyArray;
};



template<class T>
class dgQueue {
public:
	dgQueue(T *pool, dgInt32 size)
		: m_pool(pool) {

		m_mod = size;
		m_lastIndex = 0;
		m_firstIndex = 0;
	}

	void Insert(T info) {
		m_pool[m_firstIndex] = info;
		m_firstIndex ++;
		if (m_firstIndex >= m_mod) {
			m_firstIndex = 0;
		}
		NEWTON_ASSERT(m_firstIndex != m_lastIndex);
	}

	T Remove() {
		NEWTON_ASSERT(m_firstIndex != m_lastIndex);

		T element = m_pool[m_lastIndex];
		m_lastIndex ++;
		if (m_lastIndex >= m_mod) {
			m_lastIndex = 0;
		}

		return element;
	}

	void Reset() {
		m_lastIndex = m_firstIndex;
	}

	bool IsEmpty() const {
		return (m_firstIndex == m_lastIndex);
	}

	dgInt32 m_mod;
	dgInt32 m_firstIndex;
	dgInt32 m_lastIndex;
	T *m_pool;
};


class dgSolverWorlkerThreads: public dgWorkerThread {
public:
	virtual void ThreadExecute();

	dgInt32 m_count;
	dgInt32 m_threads;
	dgInt32 m_useSimd;
	dgInt32 m_solverMode;
	dgFloat32 m_timestep;
	dgWorld *m_world;
	dgJacobianMemory *m_system;
	dgWorldDynamicUpdate *m_dynamics;
};



class dgParallelSolverClear: public dgWorkerThread {
public:
	virtual void ThreadExecute();

	dgInt32 m_count;
	dgInt32 m_useSimd;
	dgWorld *m_world;
	const dgBodyInfo *m_bodyArray;
	dgJacobian *m_internalVeloc;
	dgJacobian *m_internalForces;
};

class dgParallelSolverInitInternalForces: public dgWorkerThread {
public:
	virtual void ThreadExecute();

	dgInt32 m_count;
	dgInt32 m_useSimd;
	dgInt32 *m_locks;
	dgWorld *m_world;
	dgFloat32 *m_force;
	const dgJacobianPair *m_Jt;
	dgJacobian *m_internalForces;
	const dgJointInfo *m_constraintArray;
};

class dgParallelSolverInitFeedbackUpdate: public dgWorkerThread {
public:
	virtual void ThreadExecute();

	dgInt32 m_count;
	dgInt32 m_hasJointFeeback;
	dgInt32 *m_locks;
	dgWorld *m_world;
	dgFloat32 m_timestep;
	dgFloat32 *m_force;
	dgFloat32 **m_jointForceFeeback;
	const dgJointInfo *m_constraintArray;
};


class dgParallelSolverBodyInertia: public dgWorkerThread {
public:
	virtual void ThreadExecute();

	dgInt32 m_count;
	dgInt32 m_useSimd;
	dgWorld *m_world;
	dgBodyInfo *m_bodyArray;
};

class dgParallelSolverBuildJacobianRows: public dgWorkerThread {
public:
	virtual void ThreadExecute();

	dgInt32 m_count;
	dgInt32 m_useSimd;
	dgInt32 m_bodyCount;
	dgWorld *m_world;
	dgFloat32 *m_force;
	dgFloat32 *m_diagDamp;
	dgFloat32 *m_deltaAccel;
	dgFloat32 *m_invDJMinvJt;
	dgFloat32 *m_coordenateAccel;
	dgFloat32 **m_jointFeebackForce;
	dgJacobianPair *m_Jt;
	dgJacobianPair *m_JMinv;
	dgBodyInfo *m_bodyArray;
	dgJointInfo *m_constraintArray;
};

class dgParallelSolverBuildJacobianMatrix: public dgWorkerThread {
public:
	virtual void ThreadExecute();

	bool m_bitMode;
	dgInt32 m_count;
	dgInt32 m_jointSolved;
	dgInt32 *m_rowsCount;
	dgFloat32 m_timestep;
	dgFloat32 m_invTimestep;
	dgWorld *m_world;
	dgJacobianMemory *m_solverMemory;
	dgJointInfo *m_constraintArray;
};

class dgParallelSolverJointAcceleration: public dgWorkerThread {
public:
	virtual void ThreadExecute();

	dgInt32 m_count;
	dgInt32 m_useSimd;
	dgInt32 m_jointStart;
	dgWorld *m_world;
	dgFloat32 m_timeStep;
	dgFloat32 m_invTimeStep;
	dgFloat32 m_firstPassCoef;
	const dgJacobianPair *m_Jt;
	dgFloat32 *m_penetration;
	const dgFloat32 *m_restitution;
	const dgFloat32 *m_externAccel;
	dgFloat32 *m_coordenateAccel;
	const dgInt32 *m_accelIsMortor;
	const dgInt32 *m_normalForceIndex;
	const dgFloat32 *m_penetrationStiffness;
	const dgJointInfo *m_constraintArray;
};


class dgParallelSolverUpdateVeloc: public dgWorkerThread {
public:
	virtual void ThreadExecute();

	dgInt32 m_count;
	dgInt32 m_useSimd;
	dgWorld *m_world;
	dgFloat32 m_timeStep;
	dgJacobian *m_internalVeloc;
	const dgJacobian *m_internalForces;
	const dgBodyInfo *m_bodyArray;
};

class dgParallelSolverUpdateForce: public dgWorkerThread {
public:
	virtual void ThreadExecute();

	dgInt32 m_count;
	dgInt32 m_useSimd;
	dgWorld *m_world;

	dgFloat32 m_invStep;
	dgFloat32 m_invTimeStep;
	dgFloat32 m_maxAccNorm2;
	const dgJacobian *m_internalVeloc;
	const dgBodyInfo *m_bodyArray;
};




class dgParallelSolverCalculateForces: public dgWorkerThread {
public:
	virtual void ThreadExecute();

	dgInt32 m_count;
	dgInt32 m_useSimd;
	dgInt32 m_threads;
	dgWorld *m_world;
	dgInt32 *m_locks;
	dgJacobian *m_internalForces;
	dgFloat32 m_accNorm;
	dgFloat32 *m_force;
	const dgFloat32 *m_diagDamp;
	const dgInt32 *m_normalForceIndex;
	const dgFloat32 *m_invDJMinvJt;
	const dgFloat32 *m_coordenateAccel;
	const dgFloat32 *m_lowerFrictionCoef;
	const dgFloat32 *m_upperFrictionCoef;
	const dgJacobianPair *m_Jt;
	const dgJacobianPair *m_JMinv;
	const dgJointInfo *m_constraintArray;
};



class dgJacobianMemory {
public:
	dgInt32 m_rowCount;
	dgInt32 m_bodyCount;
	dgInt32 m_jointCount;
	dgInt32 m_maxBodiesCount;
	dgInt32 m_maxJacobiansCount;
	dgInt32 m_threadIndex;
	dgFloat32 m_timeStep;
	dgFloat32 m_invTimeStep;
	dgWorld *m_world;
	dgInt32 *m_treadLocks;
	dgJacobian *m_internalVeloc;
	dgJacobian *m_internalForces;
	dgJointInfo *m_constraintArray;
	dgJacobianPair *m_Jt;
	dgJacobianPair *m_JMinv;
	dgFloat32 *m_force;
	dgFloat32 *m_accel;
	dgFloat32 *m_deltaAccel;
	dgFloat32 *m_deltaForce;
	dgFloat32 *m_diagDamp;
	dgFloat32 *m_invDJMinvJt;
	dgFloat32 *m_restitution;
	dgFloat32 *m_penetration;
	dgFloat32 *m_coordenateAccel;
	dgFloat32 *m_penetrationStiffness;
	dgFloat32 *m_lowerBoundFrictionCoefficent;
	dgFloat32 *m_upperBoundFrictionCoefficent;
	dgFloat32 **m_jointFeebackForce;
	dgBodyInfo *m_bodyArray;
	dgInt32 *m_normalForceIndex;
	dgInt32 *m_accelIsMotor;

	void SwapRows(dgInt32 i, dgInt32 j) const;
	void SwapRowsSimd(dgInt32 i, dgInt32 j) const;

	void CalculateReactionsForces(dgInt32 solverMode, dgFloat32 maxAccNorm) const;
	void CalculateReactionsForcesSimd(dgInt32 solverMode, dgFloat32 maxAccNorm) const;

	void ApplyExternalForcesAndAcceleration(dgFloat32 maxAccNorm) const;
	void ApplyExternalForcesAndAccelerationSimd(dgFloat32 maxAccNorm) const;

	void CalculateSimpleBodyReactionsForces(dgFloat32 maxAccNorm) const;
	void CalculateSimpleBodyReactionsForcesSimd(dgFloat32 maxAccNorm) const;

	dgFloat32 CalculateJointForces(dgInt32 joint, dgFloat32 *forceStep, dgFloat32 maxAccNorm) const;
	dgFloat32 CalculateJointForcesSimd(dgInt32 joint, dgFloat32 *forceStep, dgFloat32 maxAccNorm) const;

	void CalculateForcesSimulationMode(dgFloat32 maxAccNorm) const;
	void CalculateForcesSimulationModeSimd(dgFloat32 maxAccNorm) const;

	void CalculateForcesGameMode(dgInt32 itertions, dgFloat32 maxAccNorm) const;
	void CalculateForcesGameModeSimd(dgInt32 itertions, dgFloat32 maxAccNorm) const;

	void CalculateForcesGameModeParallel(dgInt32 itertions, dgFloat32 maxAccNorm, dgInt32 archModel) const;
	void CalculateReactionsForcesParallel(dgInt32 solverMode, dgFloat32 maxAccNorm, dgInt32 archModel) const;
};


class dgWorldDynamicUpdate {

	dgWorldDynamicUpdate();
	void UpdateDynamics(dgWorld *const world, dgInt32 archMode, dgFloat32 timestep);

private:
	// single core functions
	void BuildIsland(dgQueue<dgBody *> &queue, dgInt32 jountCount, dgInt32 hasUnilateralJoints, dgInt32 isContinueCollisionIsland);
//	dgBody* SpanningTree (dgBody* body, dgBody** const queuePool, dgInt32 queueSize, dgInt32 solveMode);
	void SpanningTree(dgBody *const body);
	void ReallocBodyMemory(dgInt32 count);
	void ReallocJointsMemory(dgInt32 count);
	void ReallocIslandMemory(dgInt32 count);


	// multi-cores functions
	void BuildJacobianMatrix(const dgIsland &island, dgInt32 threadIndex, dgFloat32 timestep);
	void BuildJacobianMatrixSimd(const dgIsland &island, dgInt32 threadIndex, dgFloat32 timestep);
	dgInt32 GetJacobialDerivatives(const dgIsland &island, dgInt32 threadIndex, bool bitMode, dgInt32 rowCount, dgFloat32 timestep);


	void BuildJacobianMatrixParallel(const dgIsland &island, dgFloat32 timestep, dgInt32 archModel);
	dgInt32 GetJacobialDerivativesParallel(const dgIsland &island, bool bitMode, dgInt32 rowCount, dgFloat32 timestep);

	void ReallocIntenalForcesMemory(dgInt32 count, dgInt32 threadIndex);
	void ReallocJacobiansMemory(dgInt32 count, dgInt32 threadIndex);

//	void SortIslands ();
//	dgInt32 CompareIslands (const dgIsland& A, const dgIsland& B) const;
	void IntegrateArray(const dgBodyInfo *body, dgInt32 count, dgFloat32 accelTolerance, dgFloat32 timestep, dgInt32 threadIndex, bool update) const;

	dgInt32 m_bodies;
	dgInt32 m_joints;
	dgInt32 m_islands;
	dgInt32 m_markLru;
	dgInt32 m_maxJointCount;
	dgInt32 m_maxBodiesCount;
	dgInt32 m_maxIslandCount;

	dgIsland *m_islandArray;
	dgBodyInfo *m_bodyArray;
	dgJointInfo *m_constraintArray;


	dgJacobianMemory m_solverMemory [DG_MAXIMUN_THREADS];
	dgSolverWorlkerThreads m_workerThreads[DG_MAXIMUN_THREADS];
	dgParallelSolverClear m_clearAccumulators[DG_MAXIMUN_THREADS];
	dgParallelSolverBodyInertia m_parallelBodyInertiaMatrix[DG_MAXIMUN_THREADS];
	dgParallelSolverUpdateVeloc m_parallelSolverUpdateVeloc[DG_MAXIMUN_THREADS];
	dgParallelSolverUpdateForce m_parallelSolverUpdateForce[DG_MAXIMUN_THREADS];
	dgParallelSolverCalculateForces m_parallelSolverCalculateForces[DG_MAXIMUN_THREADS];
	dgParallelSolverInitInternalForces m_parallelInitIntenalForces[DG_MAXIMUN_THREADS];
	dgParallelSolverBuildJacobianRows m_parallelSolverBuildJacobianRows[DG_MAXIMUN_THREADS];
	dgParallelSolverJointAcceleration m_parallelSolverJointAcceleration[DG_MAXIMUN_THREADS];
	dgParallelSolverInitFeedbackUpdate m_parallelSolverInitFeedbackUpdate[DG_MAXIMUN_THREADS];
	dgParallelSolverBuildJacobianMatrix m_parallelSolverBuildJacobianMatrix[DG_MAXIMUN_THREADS];


	dgBody *m_sentinelBody;
	dgWorld *m_world;
	friend class dgWorld;
	friend class dgJacobianMemory;
	friend class dgSolverWorlkerThreads;
};


#define DG_CHECK_ACTIVE(body) NEWTON_ASSERT (((body->m_invMass.m_w == dgFloat32 (0.0f)) && !body->m_active) || (body->m_invMass.m_w > dgFloat32 (0.0f)))
#endif