1//
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions
4// are met:
5// * Redistributions of source code must retain the above copyright
6// notice, this list of conditions and the following disclaimer.
7// * Redistributions in binary form must reproduce the above copyright
8// notice, this list of conditions and the following disclaimer in the
9// documentation and/or other materials provided with the distribution.
10// * Neither the name of NVIDIA CORPORATION nor the names of its
11// contributors may be used to endorse or promote products derived
12// from this software without specific prior written permission.
13//
14// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
15// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25//
26// Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved.
27// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
28// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
29
30#ifndef PXV_DYNAMICS_CONTEXT_H
31#define PXV_DYNAMICS_CONTEXT_H
32
33#include "CmPhysXCommon.h"
34#include "PxSceneDesc.h"
35#include "DyThresholdTable.h"
36#include "PxcNpThreadContext.h"
37#include "PxsSimulationController.h"
38#include "DyConstraintWriteBack.h"
39#include "PsAllocator.h"
40
41#define DY_MAX_VELOCITY_COUNT 4
42
43namespace physx
44{
45
46class PxsIslandManager;
47class PxcNpMemBlockPool;
48
49namespace Cm
50{
51 class EventProfiler;
52 class FlushPool;
53}
54
55namespace IG
56{
57 class SimpleIslandManager;
58 class IslandSim;
59}
60
61template<typename T, typename P> class PxcThreadCoherentCache;
62class PxcScratchAllocator;
63struct PxvSimStats;
64class PxTaskManager;
65class PxcNpMemBlockPool;
66struct PxgDynamicsMemoryConfig;
67class PxsContactManagerOutputIterator;
68struct PxsContactManagerOutput;
69class PxsKernelWranglerManager;
70class PxsHeapMemoryAllocator;
71class PxsMemoryManager;
72class PxsContactManager;
73
74
75namespace Dy
76{
77
78
79class Context
80{
81 PX_NOCOPY(Context)
82public:
83 /**
84 \brief Returns the bounce threshold
85 \return The bounce threshold.
86 */
87 PX_FORCE_INLINE PxReal getBounceThreshold() const { return mBounceThreshold; }
88 /**
89 \brief Returns the friction offset threshold
90 \return The friction offset threshold.
91 */
92 PX_FORCE_INLINE PxReal getFrictionOffsetThreshold() const { return mFrictionOffsetThreshold; }
93 /**
94 \brief Returns the friction offset threshold
95 \return The friction offset threshold.
96 */
97 PX_FORCE_INLINE PxReal getSolverOffsetSlop() const { return mSolverOffsetSlop; }
98 /**
99 \brief Returns the correlation distance
100 \return The correlation distance.
101 */
102 PX_FORCE_INLINE PxReal getCorrelationDistance() const { return mCorrelationDistance; }
103
104 /**
105 \brief Returns the CCD separation threshold
106 \return The CCD separation threshold.
107 */
108 PX_FORCE_INLINE PxReal getCCDSeparationThreshold() const { return mCCDSeparationThreshold; }
109
110 /**
111 \brief Sets the bounce threshold
112 \param[in] f The bounce threshold
113 */
114 PX_FORCE_INLINE void setBounceThreshold(PxReal f) { mBounceThreshold = f; }
115 /**
116 \brief Sets the correlation distance
117 \param[in] f The correlation distance
118 */
119 PX_FORCE_INLINE void setCorrelationDistance(PxReal f) { mCorrelationDistance = f; }
120 /**
121 \brief Sets the friction offset threshold
122 \param[in] offset The friction offset threshold
123 */
124 PX_FORCE_INLINE void setFrictionOffsetThreshold(PxReal offset) { mFrictionOffsetThreshold = offset; }
125 /**
126 \brief Sets the solver offset slop
127 \param[in] offset The solver offset slop
128 */
129 PX_FORCE_INLINE void setSolverOffsetSlop(PxReal offset) { mSolverOffsetSlop = offset; }
130 /**
131 \brief Sets the friction offset threshold
132 \param[in] offset The friction offset threshold
133 */
134 PX_FORCE_INLINE void setCCDSeparationThreshold(PxReal offset) { mCCDSeparationThreshold = offset; }
135
136
137 /**
138 \brief Returns the solver batch size
139 \return The solver batch size.
140 */
141 PX_FORCE_INLINE PxU32 getSolverBatchSize() const { return mSolverBatchSize; }
142 /**
143 \brief Sets the solver batch size
144 \param[in] f The solver batch size
145 */
146 PX_FORCE_INLINE void setSolverBatchSize(PxU32 f) { mSolverBatchSize = f; }
147
148 /**
149 \brief Returns the solver batch size
150 \return The solver batch size.
151 */
152 PX_FORCE_INLINE PxU32 getSolverArticBatchSize() const { return mSolverArticBatchSize; }
153 /**
154 \brief Sets the solver batch size
155 \param[in] f The solver batch size
156 */
157 PX_FORCE_INLINE void setSolverArticBatchSize(PxU32 f) { mSolverArticBatchSize = f; }
158
159
160
161 /**
162 \brief Returns the maximum solver constraint size
163 \return The maximum solver constraint size in this island in bytes.
164 */
165 PX_FORCE_INLINE PxU32 getMaxSolverConstraintSize() const { return mMaxSolverConstraintSize; }
166
167 /**
168 \brief Returns the friction model being used.
169 \return The friction model being used.
170 */
171 PX_FORCE_INLINE PxFrictionType::Enum getFrictionType() const { return mFrictionType; }
172
173 /**
174 \brief Returns the threshold stream
175 \return The threshold stream
176 */
177 PX_FORCE_INLINE ThresholdStream& getThresholdStream() { return *mThresholdStream; }
178
179 PX_FORCE_INLINE ThresholdStream& getForceChangedThresholdStream() { return *mForceChangedThresholdStream; }
180
181 /**
182 \brief Returns the threshold table
183 \return The threshold table
184 */
185 PX_FORCE_INLINE ThresholdTable& getThresholdTable() { return mThresholdTable; }
186
187 /**
188 \brief Sets the friction model to be used.
189 \param[in] f The friction model to be used.
190 */
191 PX_FORCE_INLINE void setFrictionType(PxFrictionType::Enum f) { mFrictionType = f; }
192
193 /**
194 \brief Destroys this dynamics context
195 */
196 virtual void destroy() = 0;
197
198
199
200 PX_FORCE_INLINE PxcDataStreamPool& getContactStreamPool() { return mContactStreamPool; }
201
202 PX_FORCE_INLINE PxcDataStreamPool& getPatchStreamPool() { return mPatchStreamPool; }
203
204 PX_FORCE_INLINE PxcDataStreamPool& getForceStreamPool() { return mForceStreamPool; }
205
206 PX_FORCE_INLINE Ps::Array<Dy::ConstraintWriteback, Ps::VirtualAllocator>& getConstraintWriteBackPool() { return mConstraintWriteBackPool; }
207
208
209 /**
210 \brief Returns the current frame's timestep
211 \return The current frame's timestep.
212 */
213 PX_FORCE_INLINE PxReal getDt() const { return mDt; }
214 /**
215 \brief Returns 1/(current frame's timestep)
216 \return 1/(current frame's timestep).
217 */
218 PX_FORCE_INLINE PxReal getInvDt() const { return mInvDt; }
219
220 PX_FORCE_INLINE PxReal getMaxBiasCoefficient() const { return mMaxBiasCoefficient; }
221
222 PX_FORCE_INLINE PxVec3 getGravity() const { return mGravity; }
223
224
225
226 /**
227 \brief The entry point for the constraint solver.
228 \param[in] dt The simulation time-step
229 \param[in] continuation The continuation task for the solver
230 \param[in] processLostTouchTask The task that processes lost touches
231
232 This method is called after the island generation has completed. Its main responsibilities are:
233 (1) Reserving the solver body pools
234 (2) Initializing the static and kinematic solver bodies, which are shared resources between islands.
235 (3) Construct the solver task chains for each island
236
237 Each island is solved as an independent solver task chain. In addition, large islands may be solved using multiple parallel tasks.
238 Island solving is asynchronous. Once all islands have been solved, the continuation task will be called.
239
240 */
241 virtual void update(IG::SimpleIslandManager& simpleIslandManager, PxBaseTask* continuation, PxBaseTask* processLostTouchTask,
242 PxsContactManager** foundPatchManagers, PxU32 nbFoundPatchManagers, PxsContactManager** lostPatchManagers, PxU32 nbLostPatchManagers, PxU32 maxPatchesPerCM,
243 PxsContactManagerOutputIterator& iterator, PxsContactManagerOutput* gpuOutputs, const PxReal dt, const PxVec3& gravity, const PxU32 bitMapWordCounts) = 0;
244
245 virtual void processLostPatches(IG::SimpleIslandManager& simpleIslandManager, PxsContactManager** lostPatchManagers, PxU32 nbLostPatchManagers, PxsContactManagerOutputIterator& iterator) = 0;
246
247
248 /**
249 \brief This method copy gpu solver body data to cpu body core
250 */
251 virtual void updateBodyCore(PxBaseTask* continuation) = 0;
252
253 /**
254 \brief Called after update's task chain has completed. This collects the results of the solver together
255 */
256 virtual void mergeResults() = 0;
257
258 virtual void setSimulationController(PxsSimulationController* simulationController) = 0;
259
260 virtual void getDataStreamBase(void*& contactStreamBase, void*& patchStreamBase, void*& forceAndIndiceStreamBase) = 0;
261
262 void createThresholdStream(Ps::VirtualAllocatorCallback& callback) { PX_ASSERT(mThresholdStream == NULL); mThresholdStream = PX_PLACEMENT_NEW(PX_ALLOC(sizeof(ThresholdStream), PX_DEBUG_EXP("ThresholdStream")), ThresholdStream(callback));}
263
264 void createForceChangeThresholdStream(Ps::VirtualAllocatorCallback& callback) { PX_ASSERT(mForceChangedThresholdStream == NULL); mForceChangedThresholdStream = PX_PLACEMENT_NEW(PX_ALLOC(sizeof(ThresholdStream), PX_DEBUG_EXP("ThresholdStream")), ThresholdStream(callback));}
265
266
267
268protected:
269
270 Context(IG::IslandSim* accurateIslandSim, Ps::VirtualAllocatorCallback* allocatorCallback,
271 PxvSimStats& simStats, bool enableStabilization, bool useEnhancedDeterminism, bool useAdaptiveForce,
272 const PxReal maxBiasCoefficient) :
273 mThresholdStream(NULL),
274 mForceChangedThresholdStream(NULL),
275 mAccurateIslandSim(accurateIslandSim),
276 mDt (1.0f),
277 mInvDt (1.0f),
278 mMaxBiasCoefficient (maxBiasCoefficient),
279 mEnableStabilization (enableStabilization),
280 mUseEnhancedDeterminism (useEnhancedDeterminism),
281 mUseAdaptiveForce (useAdaptiveForce),
282 mBounceThreshold(-2.0f),
283 mSolverBatchSize(32),
284 mConstraintWriteBackPool(Ps::VirtualAllocator(allocatorCallback)),
285 mSimStats(simStats)
286 {
287 }
288
289 virtual ~Context()
290 {
291 if(mThresholdStream)
292 {
293 mThresholdStream->~ThresholdStream();
294 PX_FREE(mThresholdStream);
295 }
296 mThresholdStream = NULL;
297 if(mForceChangedThresholdStream)
298 {
299 mForceChangedThresholdStream->~ThresholdStream();
300 PX_FREE(mForceChangedThresholdStream);
301 }
302 mForceChangedThresholdStream = NULL;
303 }
304
305 ThresholdStream* mThresholdStream;
306 ThresholdStream* mForceChangedThresholdStream;
307 ThresholdTable mThresholdTable;
308
309 IG::IslandSim* mAccurateIslandSim;
310 PxsSimulationController* mSimulationController;
311 /**
312 \brief Time-step.
313 */
314 PxReal mDt;
315 /**
316 \brief 1/time-step.
317 */
318 PxReal mInvDt;
319
320 PxReal mMaxBiasCoefficient;
321
322 const bool mEnableStabilization;
323
324 const bool mUseEnhancedDeterminism;
325
326 const bool mUseAdaptiveForce;
327
328 PxVec3 mGravity;
329 /**
330 \brief max solver constraint size
331 */
332 PxU32 mMaxSolverConstraintSize;
333
334 /**
335 \brief Threshold controlling the relative velocity at which the solver transitions between restitution and bias for solving normal contact constraint.
336 */
337 PxReal mBounceThreshold;
338 /**
339 \brief Threshold controlling whether friction anchors are constructed or not. If the separation is above mFrictionOffsetThreshold, the contact will not be considered to become a friction anchor
340 */
341 PxReal mFrictionOffsetThreshold;
342
343 /**
344 \brief Tolerance used to zero offsets along an axis if it is below this threshold. Used to compensate for small numerical divergence inside contact gen.
345 */
346 PxReal mSolverOffsetSlop;
347
348 /**
349 \brief Threshold controlling whether distant contacts are processed using bias, restitution or a combination of the two. This only has effect on pairs involving bodies that have enabled speculative CCD simulation mode.
350 */
351 PxReal mCCDSeparationThreshold;
352
353 /**
354 \brief Threshold for controlling friction correlation
355 */
356 PxReal mCorrelationDistance;
357 /**
358 \brief The minimum size of an island to generate a solver task chain.
359 */
360 PxU32 mSolverBatchSize;
361
362 /**
363 \brief The minimum number of articulations required to generate a solver task chain.
364 */
365 PxU32 mSolverArticBatchSize;
366
367 /**
368 \brief The current friction model being used
369 */
370 PxFrictionType::Enum mFrictionType;
371
372 /**
373 \brief Structure to encapsulate contact stream allocations. Used by GPU solver to reference pre-allocated pinned host memory
374 */
375 PxcDataStreamPool mContactStreamPool;
376
377 /**
378 \brief Struct to encapsulate the contact patch stream allocations. Used by GPU solver to reference pre-allocated pinned host memory
379 */
380
381 PxcDataStreamPool mPatchStreamPool;
382
383 /**
384 \brief Structure to encapsulate force stream allocations. Used by GPU solver to reference pre-allocated pinned host memory for force reports.
385 */
386 PxcDataStreamPool mForceStreamPool;
387
388 /**
389 \brief Structure to encapsulate constraint write back allocations. Used by GPU/CPU solver to reference pre-allocated pinned host memory for breakable joint reports.
390 */
391 Ps::Array<Dy::ConstraintWriteback, Ps::VirtualAllocator> mConstraintWriteBackPool;
392
393 PxvSimStats& mSimStats;
394
395
396};
397
398Context* createDynamicsContext( PxcNpMemBlockPool* memBlockPool,
399 PxcScratchAllocator& scratchAllocator, Cm::FlushPool& taskPool,
400 PxvSimStats& simStats, PxTaskManager* taskManager, Ps::VirtualAllocatorCallback* allocatorCallback, PxsMaterialManager* materialManager,
401 IG::IslandSim* accurateIslandSim, PxU64 contextID,
402 const bool enableStabilization, const bool useEnhancedDeterminism, const bool useAdaptiveForce, const PxReal maxBiasCoefficient,
403 const bool frictionEveryIteration
404 );
405
406Context* createTGSDynamicsContext(PxcNpMemBlockPool* memBlockPool,
407 PxcScratchAllocator& scratchAllocator, Cm::FlushPool& taskPool,
408 PxvSimStats& simStats, PxTaskManager* taskManager, Ps::VirtualAllocatorCallback* allocatorCallback, PxsMaterialManager* materialManager,
409 IG::IslandSim* accurateIslandSim, PxU64 contextID,
410 const bool enableStabilization, const bool useEnhancedDeterminism, const bool useAdaptiveForce, const PxReal lengthScale
411);
412
413
414}
415
416}
417
418#endif
419
420

source code of qtquick3dphysics/src/3rdparty/PhysX/source/lowleveldynamics/include/DyContext.h