PhysicsUpdateContext.h 11 KB

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