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;
20
23{
24public:
26 explicit PhysicsUpdateContext(TempAllocator &inTempAllocator);
28
29 static constexpr int cMaxConcurrency = 32;
30 static constexpr int cMaxSubSteps = 4;
31
33
34 struct Step;
35
37 struct SubStep
38 {
40
41 bool mIsFirst;
42 bool mIsLast;
45
48
50 struct CCDBody
51 {
52 CCDBody(BodyID inBodyID1, Vec3Arg inDeltaPosition, float inLinearCastThresholdSq, float inMaxPenetration) : mDeltaPosition(inDeltaPosition), mBodyID1(inBodyID1), mLinearCastThresholdSq(inLinearCastThresholdSq), mMaxPenetration(inMaxPenetration) { }
53
59 float mFraction = 1.0f;
60 float mFractionPlusSlop = 1.0f;
64 };
65 atomic<uint32> mIntegrateVelocityReadIdx { 0 };
66 CCDBody * mCCDBodies = nullptr;
68 atomic<uint32> mNumCCDBodies = 0;
69 atomic<uint32> mNextCCDBody { 0 };
70 int * mActiveBodyToCCDBody = nullptr;
72
80 };
81
83
85 {
86 atomic<uint32> mWriteIdx { 0 };
87 uint8 mPadding1[JPH_CACHE_LINE_SIZE - sizeof(atomic<uint32>)];
88
89 atomic<uint32> mReadIdx { 0 };
90 uint8 mPadding2[JPH_CACHE_LINE_SIZE - sizeof(atomic<uint32>)];
91 };
92
94
95 using JobMask = uint32;
96 static_assert(sizeof(JobMask) * 8 >= cMaxConcurrency);
97
99 struct Step
100 {
101 Step() = default;
102 Step(const Step &) { JPH_ASSERT(false); } // vector needs a copy constructor, but we're never going to call it
103
105
107
109
110 atomic<uint32> mConstraintReadIdx { 0 };
111 uint8 mPadding1[JPH_CACHE_LINE_SIZE - sizeof(atomic<uint32>)];
112
113 atomic<uint32> mNumActiveConstraints { 0 };
114 uint8 mPadding2[JPH_CACHE_LINE_SIZE - sizeof(atomic<uint32>)];
115
116 atomic<uint32> mStepListenerReadIdx { 0 };
117 uint8 mPadding3[JPH_CACHE_LINE_SIZE - sizeof(atomic<uint32>)];
118
119 atomic<uint32> mApplyGravityReadIdx { 0 };
120 uint8 mPadding4[JPH_CACHE_LINE_SIZE - sizeof(atomic<uint32>)];
121
122 atomic<uint32> mActiveBodyReadIdx { 0 };
123 uint8 mPadding5[JPH_CACHE_LINE_SIZE - sizeof(atomic<uint32>)];
124
126
128
129 atomic<JobMask> mActiveFindCollisionJobs;
130
131 atomic<uint> mNumBodyPairs { 0 };
132 atomic<uint> mNumManifolds { 0 };
133
134 // Jobs in order of execution (some run in parallel)
148 };
149
150 using Steps = std::vector<Step, STLTempAllocator<Step>>;
151
153 int GetMaxConcurrency() const { const int max_concurrency = PhysicsUpdateContext::cMaxConcurrency; return min(max_concurrency, mJobSystem->GetMaxConcurrency()); }
154
159
164 atomic<uint32> mErrors { 0 };
165
167
168 BodyPair * mBodyPairs = nullptr;
169
171
173};
174
#define JPH_CACHE_LINE_SIZE
Definition: Core.h:334
uint32_t uint32
Definition: Core.h:312
#define JPH_NAMESPACE_END
Definition: Core.h:240
uint8_t uint8
Definition: Core.h:310
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
#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:99
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:28
Information used during the Update call.
Definition: PhysicsUpdateContext.h:23
~PhysicsUpdateContext()
Definition: PhysicsUpdateContext.cpp:17
Constraint ** mActiveConstraints
Constraints that were active at the start of the physics update step (activating bodies can activate ...
Definition: PhysicsUpdateContext.h:166
PhysicsSystem * mPhysicsSystem
The physics system we belong to.
Definition: PhysicsUpdateContext.h:155
static constexpr int cMaxSubSteps
Maximum supported amount of integration sub steps.
Definition: PhysicsUpdateContext.h:30
std::vector< Step, STLTempAllocator< Step > > Steps
Definition: PhysicsUpdateContext.h:150
JobSystem::Barrier * mBarrier
Barrier used to wait for all physics jobs to complete.
Definition: PhysicsUpdateContext.h:158
int GetMaxConcurrency() const
Maximum amount of concurrent jobs on this machine.
Definition: PhysicsUpdateContext.h:153
Steps mSteps
Definition: PhysicsUpdateContext.h:172
float mStepDeltaTime
Delta time for a simulation step (collision step)
Definition: PhysicsUpdateContext.h:160
float mSubStepDeltaTime
Delta time for a simulation sub step (integration step)
Definition: PhysicsUpdateContext.h:161
float mWarmStartImpulseRatio
Ratio of this step delta time vs last step.
Definition: PhysicsUpdateContext.h:162
uint32 JobMask
A mask that has as many bits as we can have concurrent jobs.
Definition: PhysicsUpdateContext.h:95
atomic< uint32 > mErrors
Errors that occurred during the update, actual type is EPhysicsUpdateError.
Definition: PhysicsUpdateContext.h:164
JobSystem * mJobSystem
Job system that processes jobs.
Definition: PhysicsUpdateContext.h:157
TempAllocator * mTempAllocator
Temporary allocator used during the update.
Definition: PhysicsUpdateContext.h:156
BodyPair * mBodyPairs
A list of body pairs found by the broadphase.
Definition: PhysicsUpdateContext.h:168
static constexpr int cMaxConcurrency
Maximum supported amount of concurrent jobs.
Definition: PhysicsUpdateContext.h:29
bool mUseLargeIslandSplitter
If true, use large island splitting.
Definition: PhysicsUpdateContext.h:163
IslandBuilder * mIslandBuilder
Keeps track of connected bodies and builds islands for multithreaded velocity/position update.
Definition: PhysicsUpdateContext.h:170
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:85
atomic< uint32 > mReadIdx
Next index to read in mBodyPair array (need to add thread index * mMaxBodyPairsPerQueue and modulo mM...
Definition: PhysicsUpdateContext.h:89
uint8 mPadding1[JPH_CACHE_LINE_SIZE - sizeof(atomic< uint32 >)]
Moved to own cache line to avoid conflicts with consumer jobs.
Definition: PhysicsUpdateContext.h:87
uint8 mPadding2[JPH_CACHE_LINE_SIZE - sizeof(atomic< uint32 >)]
Moved to own cache line to avoid conflicts with producer/consumer jobs.
Definition: PhysicsUpdateContext.h:90
atomic< uint32 > mWriteIdx
Next index to write in mBodyPair array (need to add thread index * mMaxBodyPairsPerQueue and modulo m...
Definition: PhysicsUpdateContext.h:86
Structure that contains data needed for each collision step.
Definition: PhysicsUpdateContext.h:100
JobHandleArray mFindCollisions
Find all collisions between active bodies an the world.
Definition: PhysicsUpdateContext.h:139
uint8 mPadding4[JPH_CACHE_LINE_SIZE - sizeof(atomic< uint32 >)]
Padding to avoid sharing cache line with the next atomic.
Definition: PhysicsUpdateContext.h:120
atomic< uint32 > mConstraintReadIdx
Next constraint for determine active constraints.
Definition: PhysicsUpdateContext.h:110
uint8 mPadding3[JPH_CACHE_LINE_SIZE - sizeof(atomic< uint32 >)]
Padding to avoid sharing cache line with the next atomic.
Definition: PhysicsUpdateContext.h:117
JobHandle mSetupVelocityConstraints
Calculate properties for all constraints in the constraint manager.
Definition: PhysicsUpdateContext.h:141
atomic< uint32 > mStepListenerReadIdx
Next step listener to call.
Definition: PhysicsUpdateContext.h:116
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:131
JobHandle mContactRemovedCallbacks
Calls the contact removed callbacks.
Definition: PhysicsUpdateContext.h:146
JobHandle mBuildIslandsFromConstraints
Go over all constraints and assign the bodies they're attached to to an island.
Definition: PhysicsUpdateContext.h:142
uint8 mPadding2[JPH_CACHE_LINE_SIZE - sizeof(atomic< uint32 >)]
Padding to avoid sharing cache line with the next atomic.
Definition: PhysicsUpdateContext.h:114
SubSteps mSubSteps
Integration sub steps.
Definition: PhysicsUpdateContext.h:145
atomic< uint32 > mActiveBodyReadIdx
Index of fist active body that has not yet been processed by the broadphase.
Definition: PhysicsUpdateContext.h:122
atomic< uint > mNumManifolds
The number of manifolds found in this step (used to size the contact cache in the next step)
Definition: PhysicsUpdateContext.h:132
uint32 mNumActiveBodiesAtStepStart
Number of bodies that were active at the start of the physics update step. Only these bodies will rec...
Definition: PhysicsUpdateContext.h:108
JobHandleArray mApplyGravity
Update velocities of bodies with gravity.
Definition: PhysicsUpdateContext.h:138
uint8 mPadding1[JPH_CACHE_LINE_SIZE - sizeof(atomic< uint32 >)]
Padding to avoid sharing cache line with the next atomic.
Definition: PhysicsUpdateContext.h:111
uint8 mPadding5[JPH_CACHE_LINE_SIZE - sizeof(atomic< uint32 >)]
Padding to avoid sharing cache line with the next atomic.
Definition: PhysicsUpdateContext.h:123
Step(const Step &)
Definition: PhysicsUpdateContext.h:102
JobHandle mStartNextStep
Job that kicks the next step (empty for the last step)
Definition: PhysicsUpdateContext.h:147
atomic< JobMask > mActiveFindCollisionJobs
A bitmask that indicates which jobs are still active.
Definition: PhysicsUpdateContext.h:129
JobHandle mBodySetIslandIndex
Set the current island index on each body (not used by the simulation, only for drawing purposes)
Definition: PhysicsUpdateContext.h:144
JobHandle mFinalizeIslands
Finalize calculation simulation islands.
Definition: PhysicsUpdateContext.h:143
JobHandleArray mStepListeners
Listeners to notify of the beginning of a physics step.
Definition: PhysicsUpdateContext.h:136
JobHandle mUpdateBroadphaseFinalize
Swap the newly built tree with the current tree.
Definition: PhysicsUpdateContext.h:140
PhysicsUpdateContext * mContext
The physics update context.
Definition: PhysicsUpdateContext.h:104
atomic< uint32 > mApplyGravityReadIdx
Next body to apply gravity to.
Definition: PhysicsUpdateContext.h:119
uint32 mMaxBodyPairsPerQueue
Amount of body pairs that we can queue per queue.
Definition: PhysicsUpdateContext.h:127
JobHandleArray mDetermineActiveConstraints
Determine which constraints will be active during this step.
Definition: PhysicsUpdateContext.h:137
atomic< uint32 > mNumActiveConstraints
Number of constraints in the mActiveConstraints array.
Definition: PhysicsUpdateContext.h:113
BroadPhase::UpdateState mBroadPhaseUpdateState
Handle returned by Broadphase::UpdatePrepare.
Definition: PhysicsUpdateContext.h:106
BodyPairQueues mBodyPairQueues
Queues in which to put body pairs that need to be tested by the narrowphase.
Definition: PhysicsUpdateContext.h:125
JobHandle mBroadPhasePrepare
Prepares the new tree in the background.
Definition: PhysicsUpdateContext.h:135
Contains the information needed to cast a body through the scene to do continuous collision detection...
Definition: PhysicsUpdateContext.h:51
float mLinearCastThresholdSq
Maximum allowed squared movement before doing a linear cast (determined by inner radius of shape)
Definition: PhysicsUpdateContext.h:61
BodyID mBodyID2
Body 2 (the body of the closest hit, only valid if mFractionPlusSlop < 1)
Definition: PhysicsUpdateContext.h:58
BodyID mBodyID1
Body 1 (the body that is performing collision detection)
Definition: PhysicsUpdateContext.h:57
float mMaxPenetration
Maximum allowed penetration (determined by inner radius of shape)
Definition: PhysicsUpdateContext.h:62
Vec3 mContactNormal
World space normal of closest hit (only valid if mFractionPlusSlop < 1)
Definition: PhysicsUpdateContext.h:55
ContactSettings mContactSettings
The contact settings for this contact.
Definition: PhysicsUpdateContext.h:63
float mFractionPlusSlop
Fraction at which the hit occurred + extra delta to allow body to penetrate by mMaxPenetration.
Definition: PhysicsUpdateContext.h:60
float mFraction
Fraction at which the hit occurred.
Definition: PhysicsUpdateContext.h:59
RVec3 mContactPointOn2
World space contact point on body 2 of closest hit (only valid if mFractionPlusSlop < 1)
Definition: PhysicsUpdateContext.h:56
Vec3 mDeltaPosition
Desired rotation step.
Definition: PhysicsUpdateContext.h:54
CCDBody(BodyID inBodyID1, Vec3Arg inDeltaPosition, float inLinearCastThresholdSq, float inMaxPenetration)
Definition: PhysicsUpdateContext.h:52
Structure that contains job handles for each integration sub step.
Definition: PhysicsUpdateContext.h:38
atomic< uint32 > mIntegrateVelocityReadIdx
Next active body index to take when integrating velocities.
Definition: PhysicsUpdateContext.h:65
Step * mStep
Step that this substeb belongs to.
Definition: PhysicsUpdateContext.h:39
bool mIsLastOfAll
If this is the last substep in the last step.
Definition: PhysicsUpdateContext.h:44
JobHandleArray mSolveVelocityConstraints
Solve the constraints in the velocity domain.
Definition: PhysicsUpdateContext.h:73
atomic< uint32 > mNextCCDBody
Next unprocessed body index in mCCDBodies.
Definition: PhysicsUpdateContext.h:69
bool mIsFirstOfAll
If this is the first substep of the first step.
Definition: PhysicsUpdateContext.h:43
int * mActiveBodyToCCDBody
A mapping between an index in BodyManager::mActiveBodies and the index in mCCDBodies.
Definition: PhysicsUpdateContext.h:70
JobHandleArray mSolvePositionConstraints
Solve all constraints in the position domain.
Definition: PhysicsUpdateContext.h:78
uint32 mCCDBodiesCapacity
Capacity of the mCCDBodies list.
Definition: PhysicsUpdateContext.h:67
atomic< uint32 > mSolvePositionConstraintsNextIsland
Next island that needs to be processed for the solve position constraints step (doesn't need own cach...
Definition: PhysicsUpdateContext.h:47
JobHandle mPostIntegrateVelocity
Finalize integration of all body positions.
Definition: PhysicsUpdateContext.h:76
JobHandle mResolveCCDContacts
Updates the positions and velocities for all bodies that need continuous collision detection.
Definition: PhysicsUpdateContext.h:77
uint32 mNumActiveBodyToCCDBody
Number of indices in mActiveBodyToCCDBody.
Definition: PhysicsUpdateContext.h:71
bool mIsLast
If this is the last substep in the step.
Definition: PhysicsUpdateContext.h:42
atomic< uint32 > mNumCCDBodies
Number of CCD bodies in mCCDBodies.
Definition: PhysicsUpdateContext.h:68
JobHandle mStartNextSubStep
Trampoline job that either kicks the next sub step or the next step.
Definition: PhysicsUpdateContext.h:79
CCDBody * mCCDBodies
List of bodies that need to do continuous collision detection.
Definition: PhysicsUpdateContext.h:66
JobHandle mPreIntegrateVelocity
Setup integration of all body positions.
Definition: PhysicsUpdateContext.h:74
JobHandleArray mIntegrateVelocity
Integrate all body positions.
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:46
bool mIsFirst
If this is the first substep in the step.
Definition: PhysicsUpdateContext.h:41