Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
PhysicsUpdateContext.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
11#include <Jolt/Core/JobSystem.h>
13
15
16class PhysicsSystem;
17class IslandBuilder;
18class Constraint;
19class TempAllocator;
21
24{
25public:
27 explicit PhysicsUpdateContext(TempAllocator &inTempAllocator);
29
30 static constexpr int cMaxConcurrency = 32;
31
33
34 struct Step;
35
37 {
38 atomic<uint32> mWriteIdx { 0 };
39 uint8 mPadding1[JPH_CACHE_LINE_SIZE - sizeof(atomic<uint32>)];
40
41 atomic<uint32> mReadIdx { 0 };
42 uint8 mPadding2[JPH_CACHE_LINE_SIZE - sizeof(atomic<uint32>)];
43 };
44
46
47 using JobMask = uint32;
48 static_assert(sizeof(JobMask) * 8 >= cMaxConcurrency);
49
51 struct Step
52 {
53 Step() = default;
54 Step(const Step &) { JPH_ASSERT(false); } // vector needs a copy constructor, but we're never going to call it
55
57
58 bool mIsFirst;
59 bool mIsLast;
60
62
64
65 atomic<uint32> mDetermineActiveConstraintReadIdx { 0 };
66 uint8 mPadding1[JPH_CACHE_LINE_SIZE - sizeof(atomic<uint32>)];
67
68 atomic<uint32> mNumActiveConstraints { 0 };
69 uint8 mPadding2[JPH_CACHE_LINE_SIZE - sizeof(atomic<uint32>)];
70
71 atomic<uint32> mSetupVelocityConstraintsReadIdx { 0 };
72 uint8 mPadding3[JPH_CACHE_LINE_SIZE - sizeof(atomic<uint32>)];
73
74 atomic<uint32> mStepListenerReadIdx { 0 };
75 uint8 mPadding4[JPH_CACHE_LINE_SIZE - sizeof(atomic<uint32>)];
76
77 atomic<uint32> mApplyGravityReadIdx { 0 };
78 uint8 mPadding5[JPH_CACHE_LINE_SIZE - sizeof(atomic<uint32>)];
79
80 atomic<uint32> mActiveBodyReadIdx { 0 };
81 uint8 mPadding6[JPH_CACHE_LINE_SIZE - sizeof(atomic<uint32>)];
82
84
86
87 atomic<JobMask> mActiveFindCollisionJobs;
88
89 atomic<uint> mNumBodyPairs { 0 };
90 atomic<uint> mNumManifolds { 0 };
91
94
96 struct CCDBody
97 {
98 CCDBody(BodyID inBodyID1, Vec3Arg inDeltaPosition, float inLinearCastThresholdSq, float inMaxPenetration) : mDeltaPosition(inDeltaPosition), mBodyID1(inBodyID1), mLinearCastThresholdSq(inLinearCastThresholdSq), mMaxPenetration(inMaxPenetration) { }
99
106 float mFraction = 1.0f;
107 float mFractionPlusSlop = 1.0f;
111 };
112 atomic<uint32> mIntegrateVelocityReadIdx { 0 };
113 CCDBody * mCCDBodies = nullptr;
115 atomic<uint32> mNumCCDBodies = 0;
116 atomic<uint32> mNextCCDBody { 0 };
117 int * mActiveBodyToCCDBody = nullptr;
119
120 // Jobs in order of execution (some run in parallel)
143 };
144
145 using Steps = std::vector<Step, STLTempAllocator<Step>>;
146
148 int GetMaxConcurrency() const { const int max_concurrency = PhysicsUpdateContext::cMaxConcurrency; return min(max_concurrency, mJobSystem->GetMaxConcurrency()); }
149
154
157 atomic<uint32> mErrors { 0 };
158
160
161 BodyPair * mBodyPairs = nullptr;
162
164
166
169 atomic<uint> mSoftBodyToCollide { 0 };
170};
171
std::uint8_t uint8
Definition: Core.h:440
#define JPH_CACHE_LINE_SIZE
Definition: Core.h:466
unsigned int uint
Definition: Core.h:439
#define JPH_NAMESPACE_END
Definition: Core.h:367
std::uint32_t uint32
Definition: Core.h:442
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding ra...
Definition: BodyID.h:13
Base class for all physics constraints. A constraint removes one or more degrees of freedom for a rig...
Definition: Constraint.h:103
Definition: ContactListener.h:41
Keeps track of connected bodies and builds islands for multithreaded velocity/position update.
Definition: IslandBuilder.h:19
A job barrier keeps track of a number of jobs and allows waiting until they are all completed.
Definition: JobSystem.h:123
Definition: JobSystem.h:80
Definition: JobSystem.h:70
virtual int GetMaxConcurrency() const =0
Get maximum number of concurrently executing jobs.
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition: NonCopyable.h:11
Definition: PhysicsSystem.h:29
Information used during the Update call.
Definition: PhysicsUpdateContext.h:24
~PhysicsUpdateContext()
Definition: PhysicsUpdateContext.cpp:17
SoftBodyUpdateContext * mSoftBodyUpdateContexts
Contexts for updating soft bodies.
Definition: PhysicsUpdateContext.h:168
Constraint ** mActiveConstraints
Constraints that were active at the start of the physics update step (activating bodies can activate ...
Definition: PhysicsUpdateContext.h:159
PhysicsSystem * mPhysicsSystem
The physics system we belong to.
Definition: PhysicsUpdateContext.h:150
std::vector< Step, STLTempAllocator< Step > > Steps
Definition: PhysicsUpdateContext.h:145
JobSystem::Barrier * mBarrier
Barrier used to wait for all physics jobs to complete.
Definition: PhysicsUpdateContext.h:153
int GetMaxConcurrency() const
Maximum amount of concurrent jobs on this machine.
Definition: PhysicsUpdateContext.h:148
Steps mSteps
Definition: PhysicsUpdateContext.h:165
float mStepDeltaTime
Delta time for a simulation step (collision step)
Definition: PhysicsUpdateContext.h:155
float mWarmStartImpulseRatio
Ratio of this step delta time vs last step.
Definition: PhysicsUpdateContext.h:156
uint32 JobMask
A mask that has as many bits as we can have concurrent jobs.
Definition: PhysicsUpdateContext.h:47
atomic< uint32 > mErrors
Errors that occurred during the update, actual type is EPhysicsUpdateError.
Definition: PhysicsUpdateContext.h:157
JobSystem * mJobSystem
Job system that processes jobs.
Definition: PhysicsUpdateContext.h:152
TempAllocator * mTempAllocator
Temporary allocator used during the update.
Definition: PhysicsUpdateContext.h:151
BodyPair * mBodyPairs
A list of body pairs found by the broadphase.
Definition: PhysicsUpdateContext.h:161
static constexpr int cMaxConcurrency
Maximum supported amount of concurrent jobs.
Definition: PhysicsUpdateContext.h:30
atomic< uint > mSoftBodyToCollide
Next soft body to take when running SoftBodyCollide jobs.
Definition: PhysicsUpdateContext.h:169
uint mNumSoftBodies
Number of active soft bodies in the simulation.
Definition: PhysicsUpdateContext.h:167
IslandBuilder * mIslandBuilder
Keeps track of connected bodies and builds islands for multithreaded velocity/position update.
Definition: PhysicsUpdateContext.h:163
Temporary data used by the update of a soft body.
Definition: SoftBodyUpdateContext.h:18
A sub shape id contains a path to an element (usually a triangle or other primitive type) of a compou...
Definition: SubShapeID.h:23
Definition: TempAllocator.h:16
Definition: Vec3.h:16
Structure that holds a body pair.
Definition: BodyPair.h:14
Context used during broadphase update.
Definition: BroadPhase.h:44
Definition: PhysicsUpdateContext.h:37
atomic< uint32 > mReadIdx
Next index to read in mBodyPair array (need to add thread index * mMaxBodyPairsPerQueue and modulo mM...
Definition: PhysicsUpdateContext.h:41
uint8 mPadding1[JPH_CACHE_LINE_SIZE - sizeof(atomic< uint32 >)]
Moved to own cache line to avoid conflicts with consumer jobs.
Definition: PhysicsUpdateContext.h:39
uint8 mPadding2[JPH_CACHE_LINE_SIZE - sizeof(atomic< uint32 >)]
Moved to own cache line to avoid conflicts with producer/consumer jobs.
Definition: PhysicsUpdateContext.h:42
atomic< uint32 > mWriteIdx
Next index to write in mBodyPair array (need to add thread index * mMaxBodyPairsPerQueue and modulo m...
Definition: PhysicsUpdateContext.h:38
Contains the information needed to cast a body through the scene to do continuous collision detection...
Definition: PhysicsUpdateContext.h:97
float mMaxPenetration
Maximum allowed penetration (determined by inner radius of shape)
Definition: PhysicsUpdateContext.h:109
RVec3 mContactPointOn2
World space contact point on body 2 of closest hit (only valid if mFractionPlusSlop < 1)
Definition: PhysicsUpdateContext.h:102
float mLinearCastThresholdSq
Maximum allowed squared movement before doing a linear cast (determined by inner radius of shape)
Definition: PhysicsUpdateContext.h:108
Vec3 mDeltaPosition
Desired rotation step.
Definition: PhysicsUpdateContext.h:100
SubShapeID mSubShapeID2
Sub shape of body 2 that was hit (only valid if mFractionPlusSlop < 1)
Definition: PhysicsUpdateContext.h:105
BodyID mBodyID2
Body 2 (the body of the closest hit, only valid if mFractionPlusSlop < 1)
Definition: PhysicsUpdateContext.h:104
Vec3 mContactNormal
World space normal of closest hit (only valid if mFractionPlusSlop < 1)
Definition: PhysicsUpdateContext.h:101
ContactSettings mContactSettings
The contact settings for this contact.
Definition: PhysicsUpdateContext.h:110
CCDBody(BodyID inBodyID1, Vec3Arg inDeltaPosition, float inLinearCastThresholdSq, float inMaxPenetration)
Definition: PhysicsUpdateContext.h:98
float mFraction
Fraction at which the hit occurred.
Definition: PhysicsUpdateContext.h:106
float mFractionPlusSlop
Fraction at which the hit occurred + extra delta to allow body to penetrate by mMaxPenetration.
Definition: PhysicsUpdateContext.h:107
BodyID mBodyID1
Body 1 (the body that is performing collision detection)
Definition: PhysicsUpdateContext.h:103
Structure that contains data needed for each collision step.
Definition: PhysicsUpdateContext.h:52
JobHandleArray mFindCollisions
Find all collisions between active bodies an the world.
Definition: PhysicsUpdateContext.h:125
uint8 mPadding4[JPH_CACHE_LINE_SIZE - sizeof(atomic< uint32 >)]
Padding to avoid sharing cache line with the next atomic.
Definition: PhysicsUpdateContext.h:75
atomic< uint32 > mSolveVelocityConstraintsNextIsland
Next island that needs to be processed for the solve velocity constraints step (doesn't need own cach...
Definition: PhysicsUpdateContext.h:92
uint32 mNumActiveBodyToCCDBody
Number of indices in mActiveBodyToCCDBody.
Definition: PhysicsUpdateContext.h:118
bool mIsLast
If this is the last step.
Definition: PhysicsUpdateContext.h:59
JobHandleArray mSoftBodyCollide
Finds all colliding shapes for soft bodies.
Definition: PhysicsUpdateContext.h:139
uint8 mPadding6[JPH_CACHE_LINE_SIZE - sizeof(atomic< uint32 >)]
Padding to avoid sharing cache line with the next atomic.
Definition: PhysicsUpdateContext.h:81
uint8 mPadding3[JPH_CACHE_LINE_SIZE - sizeof(atomic< uint32 >)]
Padding to avoid sharing cache line with the next atomic.
Definition: PhysicsUpdateContext.h:72
JobHandleArray mSolveVelocityConstraints
Solve the constraints in the velocity domain.
Definition: PhysicsUpdateContext.h:131
JobHandle mPostIntegrateVelocity
Finalize integration of all body positions.
Definition: PhysicsUpdateContext.h:134
JobHandle mPreIntegrateVelocity
Setup integration of all body positions.
Definition: PhysicsUpdateContext.h:132
atomic< uint32 > mNumCCDBodies
Number of CCD bodies in mCCDBodies.
Definition: PhysicsUpdateContext.h:115
atomic< uint32 > mStepListenerReadIdx
Next step listener to call.
Definition: PhysicsUpdateContext.h:74
uint32 mCCDBodiesCapacity
Capacity of the mCCDBodies list.
Definition: PhysicsUpdateContext.h:114
atomic< uint > mNumBodyPairs
The number of body pairs found in this step (used to size the contact cache in the next step)
Definition: PhysicsUpdateContext.h:89
JobHandle mContactRemovedCallbacks
Calls the contact removed callbacks.
Definition: PhysicsUpdateContext.h:137
JobHandle mBuildIslandsFromConstraints
Go over all constraints and assign the bodies they're attached to to an island.
Definition: PhysicsUpdateContext.h:128
JobHandleArray mSolvePositionConstraints
Solve all constraints in the position domain.
Definition: PhysicsUpdateContext.h:136
uint8 mPadding2[JPH_CACHE_LINE_SIZE - sizeof(atomic< uint32 >)]
Padding to avoid sharing cache line with the next atomic.
Definition: PhysicsUpdateContext.h:69
JobHandle mSoftBodyFinalize
Finalizes the soft body update.
Definition: PhysicsUpdateContext.h:141
atomic< uint32 > mActiveBodyReadIdx
Index of fist active body that has not yet been processed by the broadphase.
Definition: PhysicsUpdateContext.h:80
atomic< uint32 > mSolvePositionConstraintsNextIsland
Next island that needs to be processed for the solve position constraints step (doesn't need own cach...
Definition: PhysicsUpdateContext.h:93
atomic< uint > mNumManifolds
The number of manifolds found in this step (used to size the contact cache in the next step)
Definition: PhysicsUpdateContext.h:90
uint32 mNumActiveBodiesAtStepStart
Number of bodies that were active at the start of the physics update step. Only these bodies will rec...
Definition: PhysicsUpdateContext.h:63
JobHandleArray mIntegrateVelocity
Integrate all body positions.
Definition: PhysicsUpdateContext.h:133
JobHandle mResolveCCDContacts
Updates the positions and velocities for all bodies that need continuous collision detection.
Definition: PhysicsUpdateContext.h:135
atomic< uint32 > mSetupVelocityConstraintsReadIdx
Next constraint for setting up velocity constraints.
Definition: PhysicsUpdateContext.h:71
JobHandleArray mApplyGravity
Update velocities of bodies with gravity.
Definition: PhysicsUpdateContext.h:124
uint8 mPadding1[JPH_CACHE_LINE_SIZE - sizeof(atomic< uint32 >)]
Padding to avoid sharing cache line with the next atomic.
Definition: PhysicsUpdateContext.h:66
uint8 mPadding5[JPH_CACHE_LINE_SIZE - sizeof(atomic< uint32 >)]
Padding to avoid sharing cache line with the next atomic.
Definition: PhysicsUpdateContext.h:78
Step(const Step &)
Definition: PhysicsUpdateContext.h:54
JobHandle mStartNextStep
Job that kicks the next step (empty for the last step)
Definition: PhysicsUpdateContext.h:142
bool mIsFirst
If this is the first step.
Definition: PhysicsUpdateContext.h:58
CCDBody * mCCDBodies
List of bodies that need to do continuous collision detection.
Definition: PhysicsUpdateContext.h:113
JobHandleArray mSetupVelocityConstraints
Calculate properties for all constraints in the constraint manager.
Definition: PhysicsUpdateContext.h:127
atomic< JobMask > mActiveFindCollisionJobs
A bitmask that indicates which jobs are still active.
Definition: PhysicsUpdateContext.h:87
JobHandle mBodySetIslandIndex
Set the current island index on each body (not used by the simulation, only for drawing purposes)
Definition: PhysicsUpdateContext.h:130
JobHandle mFinalizeIslands
Finalize calculation simulation islands.
Definition: PhysicsUpdateContext.h:129
JobHandleArray mStepListeners
Listeners to notify of the beginning of a physics step.
Definition: PhysicsUpdateContext.h:122
JobHandle mUpdateBroadphaseFinalize
Swap the newly built tree with the current tree.
Definition: PhysicsUpdateContext.h:126
JobHandleArray mSoftBodySimulate
Simulates all particles.
Definition: PhysicsUpdateContext.h:140
PhysicsUpdateContext * mContext
The physics update context.
Definition: PhysicsUpdateContext.h:56
atomic< uint32 > mNextCCDBody
Next unprocessed body index in mCCDBodies.
Definition: PhysicsUpdateContext.h:116
atomic< uint32 > mApplyGravityReadIdx
Next body to apply gravity to.
Definition: PhysicsUpdateContext.h:77
uint32 mMaxBodyPairsPerQueue
Amount of body pairs that we can queue per queue.
Definition: PhysicsUpdateContext.h:85
atomic< uint32 > mIntegrateVelocityReadIdx
Next active body index to take when integrating velocities.
Definition: PhysicsUpdateContext.h:112
JobHandleArray mDetermineActiveConstraints
Determine which constraints will be active during this step.
Definition: PhysicsUpdateContext.h:123
int * mActiveBodyToCCDBody
A mapping between an index in BodyManager::mActiveBodies and the index in mCCDBodies.
Definition: PhysicsUpdateContext.h:117
atomic< uint32 > mNumActiveConstraints
Number of constraints in the mActiveConstraints array.
Definition: PhysicsUpdateContext.h:68
atomic< uint32 > mDetermineActiveConstraintReadIdx
Next constraint for determine active constraints.
Definition: PhysicsUpdateContext.h:65
BroadPhase::UpdateState mBroadPhaseUpdateState
Handle returned by Broadphase::UpdatePrepare.
Definition: PhysicsUpdateContext.h:61
BodyPairQueues mBodyPairQueues
Queues in which to put body pairs that need to be tested by the narrowphase.
Definition: PhysicsUpdateContext.h:83
JobHandle mSoftBodyPrepare
Prepares updating the soft bodies.
Definition: PhysicsUpdateContext.h:138
JobHandle mBroadPhasePrepare
Prepares the new tree in the background.
Definition: PhysicsUpdateContext.h:121