My Project: dynamics.mod/NewtonDynamics/source/ampPhysics/dgAmpInstance.h Source File

My Project

dgAmpInstance.h
Go to the documentation of this file.
1 /* Copyright (c) <2003-2011> <Julio Jerez, Newton Game Dynamics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 *
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 * misrepresented as being the original software.
18 *
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21
22 #ifndef _DG_AMP_INSTANCE_H_
23 #define _DG_AMP_INSTANCE_H_
24
25 #include "dgAMP.h"
26 #include "dgAmpAllocator.h"
27
28 using namespace concurrency;
29 using namespace concurrency::graphics;
30
31 class dgAmpJacobian
32 {
33 public:
34 float_4 m_linear;
35 float_4 m_angular;
36 };
37
38 class dgAmpJacobianPair
39 {
40 public:
41 dgAmpJacobian m_jacobianM0;
42 dgAmpJacobian m_jacobianM1;
43 };
44
45 class dgAmpMatrixRightSide
46 {
47 public:
48 union
49 {
50 struct
51 {
52 // float m_force;
53 // float m_accel;
54 // float m_deltaAccel;
55 // float m_deltaForce;
56 // float m_invDJMinvJt;
57 // float m_maxImpact;
58 float m_diagDamp;
59 float m_restitution;
60 float m_penetration;
61 float m_penetrationStiffness;
62
63 float m_coordenateAccel;
64 float m_lowerBoundFrictionCoefficent;
65 float m_upperBoundFrictionCoefficent;
66
67 //dgForceImpactPair* m_jointFeebackForce;
68 int m_jointFeebackForce[2];
69 int m_normalForceIndex;
70 int m_accelIsMotor;
71 };
72 float m_data[3][4];
73 };
74 };
75
76
77 class dgAmpMatrix4x4
78 {
79 public:
80 dgAmpMatrix4x4()
81 {
82 }
83
84 dgAmpMatrix4x4 (const float_4& v0, const float_4& v1, const float_4& v2, const float_4& v3) restrict(amp,cpu)
85 {
86 m_row[0] = v0;
87 m_row[1] = v1;
88 m_row[2] = v2;
89 m_row[3] = v3;
90 }
91 float_4 m_row[4];
92 };
93
94
95 class dgAmpBodyData
96 {
97 public:
98 dgAmpBodyData (dgMemoryAllocator* const allocator);
99 void Alloc (dgInt32 count);
100 void CLean ();
101
102 dgInt32 m_currentSize;
103 array<float_4, 1> m_bodyInvMass;
104 array<dgAmpJacobian, 1> m_bodyDamp;
105 array<dgAmpJacobian, 1> m_bodyVelocity;
106 array<dgAmpJacobian, 1> m_bodyNetForce;
107 array<dgAmpJacobian, 1> m_bodyInternalForce;
108 array<dgAmpMatrix4x4 , 1> m_bodyMatrix;
109 array<dgAmpMatrix4x4 , 1> m_bodyInvInertiaMatrix;
110
111 //std::vector<float_4, dgAmpAllocator<float_4>> m_bodyInvMassCpu;
112 //std::vector<dgAmpJacobian, dgAmpAllocator<dgAmpJacobian>> m_bodyDampCpu;
113 //std::vector<dgAmpMatrix4x4, dgAmpAllocator<dgAmpJacobian>> m_bodyMatrixCpu;
114 //std::vector<dgAmpJacobian, dgAmpAllocator<dgAmpJacobian>> m_bodyVelocityCpu;
115 array_view<float_4, 1> m_bodyInvMassCpu;
116 array_view<dgAmpJacobian, 1> m_bodyDampCpu;
117 array_view<dgAmpMatrix4x4, 1> m_bodyMatrixCpu;
118 array_view<dgAmpJacobian, 1> m_bodyVelocityCpu;
119 };
120
121 class dgAmpConstraintData
122 {
123 public:
124 class dgAmpJacobianMatrixElement
125 {
126 public:
127 dgAmpJacobianPair m_Jt;
128 dgAmpMatrixRightSide m_data;
129 };
130
131 dgAmpConstraintData (dgMemoryAllocator* const allocator);
132 void Alloc (dgInt32 size);
133 void CLean ();
134
135 dgInt32 m_currentSize;
136 array<dgAmpJacobianMatrixElement, 1> m_matrixData;
137
138 //std::vector<dgAmpJacobianMatrixElement, dgAmpAllocator<dgAmpJacobianMatrixElement>> m_matrixDataCpu;
139 array_view<dgAmpJacobianMatrixElement, 1> m_matrixDataCpu;
140 };
141
142
143 class dgAmpInstance: public dgAmpBodyData, public dgAmpConstraintData
144 {
145 public:
146 class dgAcceleratorDescription
147 {
148 public:
149 char m_path[128];
150 char m_description[128];
151 };
152
153 DG_CLASS_ALLOCATOR(allocator);
154
155 dgAmpInstance(dgWorld* const world);
156 ~dgAmpInstance(void);
157
158 void CleanUp();
159 dgInt32 GetPlatformsCount() const;
160 void SelectPlaform(dgInt32 deviceIndex);
161 void GetVendorString(dgInt32 deviceIndex, char* const name, dgInt32 maxlength) const;
162
163 void ConstraintSolver (dgInt32 islandCount, const dgIsland* const islandArray, dgFloat32 timestep);
164
165 private:
166 static float_4 ToFloat4 (const dgVector& v);
167
168 static dgAmpMatrix4x4 Transpose (const dgAmpMatrix4x4& matrix) restrict(amp,cpu);
169 static dgAmpMatrix4x4 Multiply (const dgAmpMatrix4x4& matrixA, const dgAmpMatrix4x4& matrixB) restrict(amp,cpu);
170
171 static float Dot (const float_4& vectorA, const float_4& vectorB) restrict(amp,cpu);
172 static float_4 Scale (const float_4& vector, float scale) restrict(amp,cpu);
173 static float_4 RotateVector (const dgAmpMatrix4x4& matrix, const float_4& vector) restrict(amp,cpu);
174 static float_4 UnrotateVector (const dgAmpMatrix4x4& matrix, const float_4& vector) restrict(amp,cpu);
175 static float_4 TransformVector (const dgAmpMatrix4x4& matrix, const float_4& vector) restrict(amp,cpu);
176 static float_4 UntransformVector (const dgAmpMatrix4x4& matrix, const float_4& vector) restrict(amp,cpu);
177
178
179 static void InitializeBodyArrayParallelKernel (void* const context, void* const worldContext, dgInt32 threadID);
180 static void BuildJacobianMatrixParallelKernel (void* const context, void* const worldContext, dgInt32 threadID);
181 void GetJacobianDerivativesParallel (dgJointInfo* const jointInfo, dgInt32 threadIndex, dgInt32 rowBase, dgFloat32 timestep);
182
183 void InitilizeBodyArrayParallel (dgParallelSolverSyncData* const syncData);
184 void BuildJacobianMatrixParallel (dgParallelSolverSyncData* const syncData);
185 void CreateParallelArrayBatchArrays (dgParallelSolverSyncData* const syncData, const dgIsland* const islandArray, dgInt32 islandCount);
186
187 static void AddDamingAccelKernel (const dgAmpMatrix4x4& bodyMatrix, const dgAmpJacobian& damping, dgAmpJacobian& veloc) restrict(amp,cpu);
188 static void CalcuateInvInertiaMatrixKernel (const dgAmpMatrix4x4& bodyMatrix, dgAmpMatrix4x4& invInertiaMatrix, const float_4& invInertia) restrict(amp,cpu);
189
190 dgWorld* m_world;
191 accelerator m_accelerator;
192 dgList<dgAcceleratorDescription> m_acceleratorList;
193 };
194
195 inline float_4 dgAmpInstance::ToFloat4 (const dgVector& v)
196 {
197 return float_4 (v[0], v[1], v[2], v[3]);
198 }
199
200 inline float dgAmpInstance::Dot (const float_4& vectorA, const float_4& vectorB) restrict(amp,cpu)
201 {
202 return vectorA.get_x() * vectorB.get_x() + vectorA.get_y() * vectorB.get_y() + vectorA.get_z() * vectorB.get_z() + vectorA.get_w() * vectorB.get_w();
203 }
204
205 inline dgAmpMatrix4x4 dgAmpInstance::Transpose (const dgAmpMatrix4x4& matrix) restrict(amp,cpu)
206 {
207 return dgAmpMatrix4x4 (float_4 (matrix.m_row[0].get_x(), matrix.m_row[1].get_x(), matrix.m_row[2].get_x(), matrix.m_row[3].get_x()),
208 float_4 (matrix.m_row[0].get_y(), matrix.m_row[1].get_y(), matrix.m_row[2].get_y(), matrix.m_row[3].get_y()),
209 float_4 (matrix.m_row[0].get_z(), matrix.m_row[1].get_z(), matrix.m_row[2].get_z(), matrix.m_row[3].get_z()),
210 float_4 (matrix.m_row[0].get_w(), matrix.m_row[1].get_w(), matrix.m_row[2].get_w(), matrix.m_row[3].get_w()));
211 }
212
213
214 inline float_4 dgAmpInstance::Scale (const float_4& vector, float scale) restrict(amp,cpu)
215 {
216 return float_4 (vector * float_4 (scale, scale, scale, scale));
217 }
218
219 inline float_4 dgAmpInstance::RotateVector (const dgAmpMatrix4x4& matrix, const float_4& vector) restrict(amp,cpu)
220 {
221 return float_4 (Scale (matrix.m_row[0], vector.get_x()) + Scale (matrix.m_row[1], vector.get_y()) + Scale (matrix.m_row[2], vector.get_z()));
222 }
223
224 inline float_4 dgAmpInstance::UnrotateVector (const dgAmpMatrix4x4& matrix, const float_4& vector) restrict(amp,cpu)
225 {
226 return float_4 (Dot (matrix.m_row[0], vector), Dot (matrix.m_row[1], vector), Dot (matrix.m_row[2], vector), vector.get_w());
227 }
228
229 inline float_4 dgAmpInstance::TransformVector (const dgAmpMatrix4x4& matrix, const float_4& vector) restrict(amp,cpu)
230 {
231 return float_4 (Scale (matrix.m_row[0], vector.get_x()) + Scale (matrix.m_row[1], vector.get_y()) + Scale (matrix.m_row[2], vector.get_z()) + Scale (matrix.m_row[3], vector.get_w()));
232 }
233
234 inline float_4 dgAmpInstance::UntransformVector (const dgAmpMatrix4x4& matrix, const float_4& vector) restrict(amp,cpu)
235 {
236 return UnrotateVector (matrix, vector - matrix.m_row[3]);
237 }
238
239 inline dgAmpMatrix4x4 dgAmpInstance::Multiply (const dgAmpMatrix4x4& matrixA, const dgAmpMatrix4x4& matrixB) restrict(amp,cpu)
240 {
241 return dgAmpMatrix4x4 (TransformVector(matrixB, matrixA.m_row[0]), TransformVector(matrixB, matrixA.m_row[1]), TransformVector(matrixB, matrixA.m_row[2]), TransformVector(matrixB, matrixA.m_row[3]));
242 }
243
244 #endif
dgAmpConstraintData::m_matrixData
array< dgAmpJacobianMatrixElement, 1 > m_matrixData
Definition: dgAmpInstance.h:136
dgAmpJacobian
Definition: dgAmpInstance.h:31
dgAmpJacobian::m_linear
float_4 m_linear
Definition: dgAmpInstance.h:34
dgAmpJacobianPair
Definition: dgAmpInstance.h:38
dgAmpConstraintData::dgAmpJacobianMatrixElement
Definition: dgAmpInstance.h:124
dgAmpJacobian::m_angular
float_4 m_angular
Definition: dgAmpInstance.h:35
dgAmpInstance
Definition: dgAmpInstance.h:143
dgAmpBodyData::m_bodyDamp
array< dgAmpJacobian, 1 > m_bodyDamp
Definition: dgAmpInstance.h:104
dgAmpJacobianPair::m_jacobianM1
dgAmpJacobian m_jacobianM1
Definition: dgAmpInstance.h:42
dgAmpMatrixRightSide::m_accelIsMotor
int m_accelIsMotor
Definition: dgAmpInstance.h:70
concurrency
dgAmpBodyData
Definition: dgAmpInstance.h:95
dgAmpConstraintData::dgAmpJacobianMatrixElement::m_Jt
dgAmpJacobianPair m_Jt
Definition: dgAmpInstance.h:127
dgFloat32
float dgFloat32
Definition: dgTypes.h:233
dgInt32
int dgInt32
Definition: dgTypes.h:218
dgVector
Definition: dgVector.h:639
dgAmpConstraintData::dgAmpJacobianMatrixElement::m_data
dgAmpMatrixRightSide m_data
Definition: dgAmpInstance.h:128
dgJointInfo
Definition: dgWorldDynamicUpdate.h:98
dgAmpBodyData::m_bodyVelocity
array< dgAmpJacobian, 1 > m_bodyVelocity
Definition: dgAmpInstance.h:105
dgAmpMatrixRightSide::m_upperBoundFrictionCoefficent
float m_upperBoundFrictionCoefficent
Definition: dgAmpInstance.h:65
cpu
float cpu
Definition: dgAmpInstance.h:201
DG_CLASS_ALLOCATOR
#define DG_CLASS_ALLOCATOR(allocator)
Definition: dgMemory.h:60
dgParallelSolverSyncData
Definition: dgWorldDynamicUpdate.h:119
dgList
Definition: dgList.h:33
dgAmpMatrixRightSide::m_coordenateAccel
float m_coordenateAccel
Definition: dgAmpInstance.h:63
dgAmpBodyData::m_bodyMatrix
array< dgAmpMatrix4x4, 1 > m_bodyMatrix
Definition: dgAmpInstance.h:108
dgAmpBodyData::m_bodyInternalForce
array< dgAmpJacobian, 1 > m_bodyInternalForce
Definition: dgAmpInstance.h:107
dgAmpMatrixRightSide::m_lowerBoundFrictionCoefficent
float m_lowerBoundFrictionCoefficent
Definition: dgAmpInstance.h:64
dgAmpBodyData::m_bodyMatrixCpu
array_view< dgAmpMatrix4x4, 1 > m_bodyMatrixCpu
Definition: dgAmpInstance.h:117
dgAmpBodyData::m_bodyNetForce
array< dgAmpJacobian, 1 > m_bodyNetForce
Definition: dgAmpInstance.h:106
m_row
tmp m_row *[0] tmp m_row *[1] tmp m_row *[2] invInertiaMatrix m_row[0]
Definition: dgAmpSolver.cpp:771
dgAmpBodyData::m_bodyInvInertiaMatrix
array< dgAmpMatrix4x4, 1 > m_bodyInvInertiaMatrix
Definition: dgAmpInstance.h:109
dgAmpJacobianPair::m_jacobianM0
dgAmpJacobian m_jacobianM0
Definition: dgAmpInstance.h:41
dgAmpMatrix4x4
Definition: dgAmpInstance.h:77
dgAMP.h
dgAmpMatrixRightSide::m_penetration
float m_penetration
Definition: dgAmpInstance.h:60
dgAmpConstraintData::m_currentSize
dgInt32 m_currentSize
Definition: dgAmpInstance.h:135
dgAmpMatrixRightSide
Definition: dgAmpInstance.h:45
threadIndex
dgFloat32 dgInt32 threadIndex
Definition: dgConstraint.h:44
dgAmpAllocator.h
timestep
dgFloat32 timestep
Definition: dgBallConstraint.h:34
dgAmpBodyData::m_bodyVelocityCpu
array_view< dgAmpJacobian, 1 > m_bodyVelocityCpu
Definition: dgAmpInstance.h:118
dgAmpMatrixRightSide::m_diagDamp
float m_diagDamp
Definition: dgAmpInstance.h:58
dgMemoryAllocator
Definition: dgMemory.h:73
dgAmpInstance::dgAcceleratorDescription
Definition: dgAmpInstance.h:146
dgWorld
Definition: dgWorld.h:129
dgAmpConstraintData::m_matrixDataCpu
array_view< dgAmpJacobianMatrixElement, 1 > m_matrixDataCpu
Definition: dgAmpInstance.h:139
dgAmpMatrixRightSide::m_restitution
float m_restitution
Definition: dgAmpInstance.h:59
dgAmpBodyData::m_bodyDampCpu
array_view< dgAmpJacobian, 1 > m_bodyDampCpu
Definition: dgAmpInstance.h:116
dgAmpMatrix4x4::dgAmpMatrix4x4
dgAmpMatrix4x4()
Definition: dgAmpInstance.h:80
dgAmpConstraintData
Definition: dgAmpInstance.h:121
dgAmpBodyData::m_currentSize
dgInt32 m_currentSize
Definition: dgAmpInstance.h:102
dgAmpMatrixRightSide::m_normalForceIndex
int m_normalForceIndex
Definition: dgAmpInstance.h:69
dgAmpMatrixRightSide::m_penetrationStiffness
float m_penetrationStiffness
Definition: dgAmpInstance.h:61
dgIsland
Definition: dgWorldDynamicUpdate.h:84
dgAmpBodyData::m_bodyInvMass
array< float_4, 1 > m_bodyInvMass
Definition: dgAmpInstance.h:103
graphics
dgAmpMatrix4x4::m_row
m_row[1]
Definition: dgAmpInstance.h:87
dgAmpBodyData::m_bodyInvMassCpu
array_view< float_4, 1 > m_bodyInvMassCpu
Definition: dgAmpInstance.h:115