Skip to content

Commit

Permalink
Added scope around BodyAccess::Grant to avoid assert when JobSoftBody…
Browse files Browse the repository at this point in the history
…Collide is directly executed from JobSoftBodyPrepare (e.g. when using JobSystemSingleThreaded)
  • Loading branch information
jrouwe committed Nov 18, 2023
1 parent 8ad7c87 commit 4fac195
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions Jolt/Physics/PhysicsSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2349,38 +2349,40 @@ void PhysicsSystem::JobSoftBodyPrepare(PhysicsUpdateContext *ioContext, PhysicsU
{
JPH_PROFILE_FUNCTION();

#ifdef JPH_ENABLE_ASSERTS
// Reading soft body positions
BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::Read);
#endif
{
#ifdef JPH_ENABLE_ASSERTS
// Reading soft body positions
BodyAccess::Grant grant(BodyAccess::EAccess::None, BodyAccess::EAccess::Read);
#endif

// Get the active soft bodies
BodyIDVector active_bodies;
mBodyManager.GetActiveBodies(EBodyType::SoftBody, active_bodies);
// Get the active soft bodies
BodyIDVector active_bodies;
mBodyManager.GetActiveBodies(EBodyType::SoftBody, active_bodies);

// Quit if there are no active soft bodies
if (active_bodies.empty())
{
// Kick the next step
if (ioStep->mStartNextStep.IsValid())
ioStep->mStartNextStep.RemoveDependency();
return;
}
// Quit if there are no active soft bodies
if (active_bodies.empty())
{
// Kick the next step
if (ioStep->mStartNextStep.IsValid())
ioStep->mStartNextStep.RemoveDependency();
return;
}

// Sort to get a deterministic update order
QuickSort(active_bodies.begin(), active_bodies.end());
// Sort to get a deterministic update order
QuickSort(active_bodies.begin(), active_bodies.end());

// Allocate soft body contexts
ioContext->mNumSoftBodies = (uint)active_bodies.size();
ioContext->mSoftBodyUpdateContexts = (SoftBodyUpdateContext *)ioContext->mTempAllocator->Allocate(ioContext->mNumSoftBodies * sizeof(SoftBodyUpdateContext));
// Allocate soft body contexts
ioContext->mNumSoftBodies = (uint)active_bodies.size();
ioContext->mSoftBodyUpdateContexts = (SoftBodyUpdateContext *)ioContext->mTempAllocator->Allocate(ioContext->mNumSoftBodies * sizeof(SoftBodyUpdateContext));

// Initialize soft body contexts
for (SoftBodyUpdateContext *sb_ctx = ioContext->mSoftBodyUpdateContexts, *sb_ctx_end = ioContext->mSoftBodyUpdateContexts + ioContext->mNumSoftBodies; sb_ctx < sb_ctx_end; ++sb_ctx)
{
new (sb_ctx) SoftBodyUpdateContext;
Body &body = mBodyManager.GetBody(active_bodies[sb_ctx - ioContext->mSoftBodyUpdateContexts]);
SoftBodyMotionProperties *mp = static_cast<SoftBodyMotionProperties *>(body.GetMotionProperties());
mp->InitializeUpdateContext(ioContext->mStepDeltaTime, body, *this, *sb_ctx);
// Initialize soft body contexts
for (SoftBodyUpdateContext *sb_ctx = ioContext->mSoftBodyUpdateContexts, *sb_ctx_end = ioContext->mSoftBodyUpdateContexts + ioContext->mNumSoftBodies; sb_ctx < sb_ctx_end; ++sb_ctx)
{
new (sb_ctx) SoftBodyUpdateContext;
Body &body = mBodyManager.GetBody(active_bodies[sb_ctx - ioContext->mSoftBodyUpdateContexts]);
SoftBodyMotionProperties *mp = static_cast<SoftBodyMotionProperties *>(body.GetMotionProperties());
mp->InitializeUpdateContext(ioContext->mStepDeltaTime, body, *this, *sb_ctx);
}
}

// We're ready to collide the first soft body
Expand Down

0 comments on commit 4fac195

Please sign in to comment.