RavEngine
Loading...
Searching...
No Matches
DySolverCore.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef DY_SOLVER_CORE_H
30#define DY_SOLVER_CORE_H
31
32#include "PxvConfig.h"
33#include "foundation/PxArray.h"
34#include "foundation/PxThread.h"
35
36
37namespace physx
38{
39
40struct PxSolverBody;
41struct PxSolverBodyData;
42struct PxSolverConstraintDesc;
43struct PxConstraintBatchHeader;
44
45namespace Dy
46{
47struct ThresholdStreamElement;
48
49
50struct ArticulationSolverDesc;
51class Articulation;
52struct SolverContext;
53
54typedef void (*WriteBackMethod)(const PxSolverConstraintDesc& desc, SolverContext& cache, PxSolverBodyData& sbd0, PxSolverBodyData& sbd1);
55typedef void (*SolveMethod)(const PxSolverConstraintDesc& desc, SolverContext& cache);
56typedef void (*SolveBlockMethod)(const PxSolverConstraintDesc* desc, const PxU32 constraintCount, SolverContext& cache);
57typedef void (*SolveWriteBackBlockMethod)(const PxSolverConstraintDesc* desc, const PxU32 constraintCount, SolverContext& cache);
58typedef void (*WriteBackBlockMethod)(const PxSolverConstraintDesc* desc, const PxU32 constraintCount, SolverContext& cache);
59
60#define PX_PROFILE_SOLVE_STALLS 0
61#if PX_PROFILE_SOLVE_STALLS
62#if PX_WINDOWS
63#include <windows.h>
64
65
66PX_FORCE_INLINE PxU64 readTimer()
67{
68 //return __rdtsc();
69
70 LARGE_INTEGER i;
71 QueryPerformanceCounter(&i);
72 return i.QuadPart;
73}
74
75#endif
76#endif
77
78
79#define YIELD_THREADS 1
80
81#if YIELD_THREADS
82
83#define ATTEMPTS_BEFORE_BACKOFF 30000
84#define ATTEMPTS_BEFORE_RETEST 10000
85
86#endif
87
88PX_INLINE void WaitForProgressCount(volatile PxI32* pGlobalIndex, const PxI32 targetIndex)
89{
90#if YIELD_THREADS
91 if(*pGlobalIndex < targetIndex)
92 {
93 bool satisfied = false;
94 PxU32 count = ATTEMPTS_BEFORE_BACKOFF;
95 do
96 {
97 satisfied = true;
98 while(*pGlobalIndex < targetIndex)
99 {
100 if(--count == 0)
101 {
102 satisfied = false;
103 break;
104 }
105 }
106 if(!satisfied)
108 count = ATTEMPTS_BEFORE_RETEST;
109 }
110 while(!satisfied);
111 }
112#else
113 while(*pGlobalIndex < targetIndex);
114#endif
115}
116
117
118#if PX_PROFILE_SOLVE_STALLS
119PX_INLINE void WaitForProgressCount(volatile PxI32* pGlobalIndex, const PxI32 targetIndex, PxU64& stallTime)
120{
121 if(*pGlobalIndex < targetIndex)
122 {
123 bool satisfied = false;
124 PxU32 count = ATTEMPTS_BEFORE_BACKOFF;
125 do
126 {
127 satisfied = true;
128 PxU64 startTime = readTimer();
129 while(*pGlobalIndex < targetIndex)
130 {
131 if(--count == 0)
132 {
133 satisfied = false;
134 break;
135 }
136 }
137 PxU64 endTime = readTimer();
138 stallTime += (endTime - startTime);
139 if(!satisfied)
141 count = ATTEMPTS_BEFORE_BACKOFF;
142 }
143 while(!satisfied);
144 }
145}
146
147#define WAIT_FOR_PROGRESS(pGlobalIndex, targetIndex) if(*pGlobalIndex < targetIndex) WaitForProgressCount(pGlobalIndex, targetIndex, stallCount)
148#else
149#define WAIT_FOR_PROGRESS(pGlobalIndex, targetIndex) if(*pGlobalIndex < targetIndex) WaitForProgressCount(pGlobalIndex, targetIndex)
150#endif
151#define WAIT_FOR_PROGRESS_NO_TIMER(pGlobalIndex, targetIndex) if(*pGlobalIndex < targetIndex) WaitForProgressCount(pGlobalIndex, targetIndex)
152
153
155{
156 //Default friction model params
157 PxU32 positionIterations;
158 PxU32 velocityIterations;
159 PxSolverBody* PX_RESTRICT bodyListStart;
160 PxSolverBodyData* PX_RESTRICT bodyDataList;
161 PxU32 bodyListSize;
162 PxU32 solverBodyOffset;
163 ArticulationSolverDesc* PX_RESTRICT articulationListStart;
164 PxU32 articulationListSize;
165 PxSolverConstraintDesc* PX_RESTRICT constraintList;
166 PxConstraintBatchHeader* constraintBatchHeaders;
167 PxU32 numConstraintHeaders;
168 PxU32* headersPerPartition;
169 PxU32 nbPartitions;
170 Cm::SpatialVector* PX_RESTRICT motionVelocityArray;
171 PxU32 batchSize;
172 PxsBodyCore*const* bodyArray;
173 PxsRigidBody** PX_RESTRICT rigidBodies;
174
175 //Shared state progress counters
176 PxI32 constraintIndex;
177 PxI32 constraintIndex2;
178 PxI32 bodyListIndex;
179 PxI32 bodyListIndex2;
180 PxI32 articSolveIndex;
181 PxI32 articSolveIndex2;
182 PxI32 bodyIntegrationListIndex;
183 PxI32 numObjectsIntegrated;
184
185 PxReal dt;
186 PxReal invDt;
187
188
189 //Additional 1d/2d friction model params
190 PxSolverConstraintDesc* PX_RESTRICT frictionConstraintList;
191
192 PxConstraintBatchHeader* frictionConstraintBatches;
193 PxU32 numFrictionConstraintHeaders;
194 PxU32* frictionHeadersPerPartition;
195 PxU32 nbFrictionPartitions;
196
197 //Additional Shared state progress counters
198 PxI32 frictionConstraintIndex;
199
200 //Write-back threshold information
201 ThresholdStreamElement* PX_RESTRICT thresholdStream;
202 PxU32 thresholdStreamLength;
203
204 PxI32* outThresholdPairs;
205
206 PxU32 mMaxArticulationLinks;
208 Cm::SpatialVectorF* deltaV;
209};
210
211
217{
218public:
219 virtual void destroyV() = 0;
220 virtual ~SolverCore() {}
221 /*
222 solves dual problem exactly by GS-iterating until convergence stops
223 only uses regular velocity vector for storing results, and backs up initial state, which is restored.
224 the solution forces are saved in a vector.
225
226 state should not be stored, this function is safe to call from multiple threads.
227
228 Returns the total number of constraints that should be solved across all threads. Used for synchronization outside of this method
229 */
230
231 virtual PxI32 solveVParallelAndWriteBack
232 (SolverIslandParams& params, Cm::SpatialVectorF* Z, Cm::SpatialVectorF* deltaV) const = 0;
233
234
235 virtual void solveV_Blocks
236 (SolverIslandParams& params) const = 0;
237
238
239 virtual void writeBackV
240 (const PxSolverConstraintDesc* PX_RESTRICT constraintList, const PxU32 constraintListSize, PxConstraintBatchHeader* contactConstraintBatches, const PxU32 numConstraintBatches,
241 ThresholdStreamElement* PX_RESTRICT thresholdStream, const PxU32 thresholdStreamLength, PxU32& outThresholdPairs,
242 PxSolverBodyData* atomListData, WriteBackBlockMethod writeBackTable[]) const = 0;
243};
244
245}
246
247}
248
249#endif
Definition CmSpatialVector.h:46
Definition DySolverCore.h:217
static void yield()
Definition PxThread.h:323
Definition PxsRigidBody.h:43
#define PX_RESTRICT
Definition PxPreprocessor.h:355
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
#define PX_INLINE
Definition PxPreprocessor.h:320
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Definition CmSpatialVector.h:134
Definition DyVArticulation.h:102
Definition DySolverCore.h:155
Definition DyThresholdTable.h:52
A header that defines the size of a specific batch of constraints (of same type and without dependenc...
Definition PxSolverDefs.h:98
Struct that the solver uses to store the state and other properties of a body.
Definition PxSolverDefs.h:74
Struct that the solver uses to store velocity updates for a body.
Definition PxSolverDefs.h:55
Constraint descriptor used inside the solver.
Definition PxSolverDefs.h:108
Definition PxvDynamics.h:74