1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-06-19 19:11:32 +00:00

Use common function for sync and async case. Now both cases follow the

same flow, synchronous simulation is just a special case.
This commit is contained in:
fredzio 2021-09-29 10:17:31 +02:00
parent 499b161e7b
commit 21dbe314bf
3 changed files with 46 additions and 53 deletions

View file

@ -22,31 +22,31 @@
namespace namespace
{ {
/// @brief A scoped lock that is either shared or exclusive depending on configuration /// @brief A scoped lock that is either shared, exclusive or inexistent depending on configuration
template<class Mutex> template<class Mutex>
class MaybeSharedLock class MaybeSharedLock
{ {
public: public:
/// @param mutex a shared mutex /// @param mutex a shared mutex
/// @param canBeSharedLock decide wether the lock will be shared or exclusive /// @param threadCount decide wether the lock will be shared, exclusive or inexistent
MaybeSharedLock(Mutex& mutex, bool canBeSharedLock) : mMutex(mutex), mCanBeSharedLock(canBeSharedLock) MaybeSharedLock(Mutex& mutex, int threadCount) : mMutex(mutex), mThreadCount(threadCount)
{ {
if (mCanBeSharedLock) if (mThreadCount > 1)
mMutex.lock_shared(); mMutex.lock_shared();
else else if(mThreadCount == 1)
mMutex.lock(); mMutex.lock();
} }
~MaybeSharedLock() ~MaybeSharedLock()
{ {
if (mCanBeSharedLock) if (mThreadCount > 1)
mMutex.unlock_shared(); mMutex.unlock_shared();
else else if(mThreadCount == 1)
mMutex.unlock(); mMutex.unlock();
} }
private: private:
Mutex& mMutex; Mutex& mMutex;
bool mCanBeSharedLock; int mThreadCount;
}; };
bool isUnderWater(const MWPhysics::ActorFrameData& actorData) bool isUnderWater(const MWPhysics::ActorFrameData& actorData)
@ -62,14 +62,14 @@ namespace
namespace Config namespace Config
{ {
/// @return either the number of thread as configured by the user, or 1 if Bullet doesn't support multithreading /// @return either the number of thread as configured by the user, or 1 if Bullet doesn't support multithreading and user requested more than 1 background threads
int computeNumThreads(bool& threadSafeBullet) int computeNumThreads()
{ {
int wantedThread = Settings::Manager::getInt("async num threads", "Physics"); int wantedThread = Settings::Manager::getInt("async num threads", "Physics");
auto broad = std::make_unique<btDbvtBroadphase>(); auto broad = std::make_unique<btDbvtBroadphase>();
auto maxSupportedThreads = broad->m_rayTestStacks.size(); auto maxSupportedThreads = broad->m_rayTestStacks.size();
threadSafeBullet = (maxSupportedThreads > 1); auto threadSafeBullet = (maxSupportedThreads > 1);
if (!threadSafeBullet && wantedThread > 1) if (!threadSafeBullet && wantedThread > 1)
{ {
Log(Debug::Warning) << "Bullet was not compiled with multithreading support, 1 async thread will be used"; Log(Debug::Warning) << "Bullet was not compiled with multithreading support, 1 async thread will be used";
@ -88,6 +88,7 @@ namespace MWPhysics
, mTimeAccum(0.f) , mTimeAccum(0.f)
, mCollisionWorld(collisionWorld) , mCollisionWorld(collisionWorld)
, mDebugDrawer(debugDrawer) , mDebugDrawer(debugDrawer)
, mNumThreads(Config::computeNumThreads())
, mNumJobs(0) , mNumJobs(0)
, mRemainingSteps(0) , mRemainingSteps(0)
, mLOSCacheExpiry(Settings::Manager::getInt("lineofsight keep inactive cache", "Physics")) , mLOSCacheExpiry(Settings::Manager::getInt("lineofsight keep inactive cache", "Physics"))
@ -107,8 +108,6 @@ namespace MWPhysics
, mTimeEnd(0) , mTimeEnd(0)
, mFrameStart(0) , mFrameStart(0)
{ {
mNumThreads = Config::computeNumThreads(mThreadSafeBullet);
if (mNumThreads >= 1) if (mNumThreads >= 1)
{ {
for (int i = 0; i < mNumThreads; ++i) for (int i = 0; i < mNumThreads; ++i)
@ -197,8 +196,7 @@ namespace MWPhysics
// start by finishing previous background computation // start by finishing previous background computation
if (mNumThreads != 0) if (mNumThreads != 0)
{ {
for (size_t i = 0; i < mActors.size(); ++i) syncWithMainThread();
updateActor(*mActors[i], mActorsFrameData[i], mAdvanceSimulation, mTimeAccum, mPhysicsDt);
if(mAdvanceSimulation) if(mAdvanceSimulation)
mAsyncBudget.update(mTimer->delta_s(mAsyncStartTime, mTimeEnd), mPrevStepCount, mBudgetCursor); mAsyncBudget.update(mTimer->delta_s(mAsyncStartTime, mTimeEnd), mPrevStepCount, mBudgetCursor);
@ -233,7 +231,8 @@ namespace MWPhysics
if (mNumThreads == 0) if (mNumThreads == 0)
{ {
syncComputation(); doSimulation();
syncWithMainThread();
if(mAdvanceSimulation) if(mAdvanceSimulation)
mBudget.update(mTimer->delta_s(timeStart, mTimer->tick()), numSteps, mBudgetCursor); mBudget.update(mTimer->delta_s(timeStart, mTimer->tick()), numSteps, mBudgetCursor);
return; return;
@ -262,13 +261,13 @@ namespace MWPhysics
void PhysicsTaskScheduler::rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, btCollisionWorld::RayResultCallback& resultCallback) const void PhysicsTaskScheduler::rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, btCollisionWorld::RayResultCallback& resultCallback) const
{ {
MaybeSharedLock lock(mCollisionWorldMutex, mThreadSafeBullet); MaybeSharedLock lock(mCollisionWorldMutex, mNumThreads);
mCollisionWorld->rayTest(rayFromWorld, rayToWorld, resultCallback); mCollisionWorld->rayTest(rayFromWorld, rayToWorld, resultCallback);
} }
void PhysicsTaskScheduler::convexSweepTest(const btConvexShape* castShape, const btTransform& from, const btTransform& to, btCollisionWorld::ConvexResultCallback& resultCallback) const void PhysicsTaskScheduler::convexSweepTest(const btConvexShape* castShape, const btTransform& from, const btTransform& to, btCollisionWorld::ConvexResultCallback& resultCallback) const
{ {
MaybeSharedLock lock(mCollisionWorldMutex, mThreadSafeBullet); MaybeSharedLock lock(mCollisionWorldMutex, mNumThreads);
mCollisionWorld->convexSweepTest(castShape, from, to, resultCallback); mCollisionWorld->convexSweepTest(castShape, from, to, resultCallback);
} }
@ -280,7 +279,7 @@ namespace MWPhysics
std::optional<btVector3> PhysicsTaskScheduler::getHitPoint(const btTransform& from, btCollisionObject* target) std::optional<btVector3> PhysicsTaskScheduler::getHitPoint(const btTransform& from, btCollisionObject* target)
{ {
MaybeSharedLock lock(mCollisionWorldMutex, mThreadSafeBullet); MaybeSharedLock lock(mCollisionWorldMutex, mNumThreads);
// target the collision object's world origin, this should be the center of the collision object // target the collision object's world origin, this should be the center of the collision object
btTransform rayTo; btTransform rayTo;
rayTo.setIdentity(); rayTo.setIdentity();
@ -411,22 +410,7 @@ namespace MWPhysics
if (!mNewFrame) if (!mNewFrame)
mHasJob.wait(lock, [&]() { return mQuit || mNewFrame; }); mHasJob.wait(lock, [&]() { return mQuit || mNewFrame; });
mPreStepBarrier->wait([this] { afterPreStep(); }); doSimulation();
int job = 0;
while (mRemainingSteps && (job = mNextJob.fetch_add(1, std::memory_order_relaxed)) < mNumJobs)
{
MaybeSharedLock lockColWorld(mCollisionWorldMutex, mThreadSafeBullet);
MovementSolver::move(mActorsFrameData[job], mPhysicsDt, mCollisionWorld, *mWorldFrameData);
}
mPostStepBarrier->wait([this] { afterPostStep(); });
if (!mRemainingSteps)
{
refreshLOSCache();
mPostSimBarrier->wait([this] { afterPostSim(); });
}
} }
} }
@ -485,29 +469,29 @@ namespace MWPhysics
resultCallback.m_collisionFilterGroup = 0xFF; resultCallback.m_collisionFilterGroup = 0xFF;
resultCallback.m_collisionFilterMask = CollisionType_World|CollisionType_HeightMap|CollisionType_Door; resultCallback.m_collisionFilterMask = CollisionType_World|CollisionType_HeightMap|CollisionType_Door;
MaybeSharedLock lockColWorld(mCollisionWorldMutex, mThreadSafeBullet); MaybeSharedLock lockColWorld(mCollisionWorldMutex, mNumThreads);
mCollisionWorld->rayTest(pos1, pos2, resultCallback); mCollisionWorld->rayTest(pos1, pos2, resultCallback);
return !resultCallback.hasHit(); return !resultCallback.hasHit();
} }
void PhysicsTaskScheduler::syncComputation() void PhysicsTaskScheduler::doSimulation()
{ {
while (mRemainingSteps--) while (mRemainingSteps)
{ {
for (auto& actorData : mActorsFrameData) mPreStepBarrier->wait([this] { afterPreStep(); });
int job = 0;
while ((job = mNextJob.fetch_add(1, std::memory_order_relaxed)) < mNumJobs)
{ {
MovementSolver::unstuck(actorData, mCollisionWorld); MaybeSharedLock lockColWorld(mCollisionWorldMutex, mNumThreads);
MovementSolver::move(actorData, mPhysicsDt, mCollisionWorld, *mWorldFrameData); MovementSolver::move(mActorsFrameData[job], mPhysicsDt, mCollisionWorld, *mWorldFrameData);
} }
updateActorsPositions(); mPostStepBarrier->wait([this] { afterPostStep(); });
} }
for (size_t i = 0; i < mActors.size(); ++i)
updateActor(*mActors[i], mActorsFrameData[i], mAdvanceSimulation, mTimeAccum, mPhysicsDt);
refreshLOSCache(); refreshLOSCache();
mPostSimBarrier->wait([this] { afterPostSim(); });
} }
void PhysicsTaskScheduler::updateStats(osg::Timer_t frameStart, unsigned int frameNumber, osg::Stats& stats) void PhysicsTaskScheduler::updateStats(osg::Timer_t frameStart, unsigned int frameNumber, osg::Stats& stats)
@ -580,4 +564,10 @@ namespace MWPhysics
} }
mTimeEnd = mTimer->tick(); mTimeEnd = mTimer->tick();
} }
void PhysicsTaskScheduler::syncWithMainThread()
{
for (size_t i = 0; i < mActors.size(); ++i)
updateActor(*mActors[i], mActorsFrameData[i], mAdvanceSimulation, mTimeAccum, mPhysicsDt);
}
} }

View file

@ -61,7 +61,7 @@ namespace MWPhysics
void releaseSharedStates(); // destroy all objects whose destructor can't be safely called from ~PhysicsTaskScheduler() void releaseSharedStates(); // destroy all objects whose destructor can't be safely called from ~PhysicsTaskScheduler()
private: private:
void syncComputation(); void doSimulation();
void worker(); void worker();
void updateActorsPositions(); void updateActorsPositions();
void updateActor(Actor& actor, ActorFrameData& actorData, bool simulationPerformed, float timeAccum, float dt) const; void updateActor(Actor& actor, ActorFrameData& actorData, bool simulationPerformed, float timeAccum, float dt) const;
@ -74,6 +74,7 @@ namespace MWPhysics
void afterPreStep(); void afterPreStep();
void afterPostStep(); void afterPostStep();
void afterPostSim(); void afterPostSim();
void syncWithMainThread();
std::unique_ptr<WorldFrameData> mWorldFrameData; std::unique_ptr<WorldFrameData> mWorldFrameData;
std::vector<std::shared_ptr<Actor>> mActors; std::vector<std::shared_ptr<Actor>> mActors;
@ -98,7 +99,6 @@ namespace MWPhysics
int mLOSCacheExpiry; int mLOSCacheExpiry;
bool mNewFrame; bool mNewFrame;
bool mAdvanceSimulation; bool mAdvanceSimulation;
bool mThreadSafeBullet;
bool mQuit; bool mQuit;
std::atomic<int> mNextJob; std::atomic<int> mNextJob;
std::atomic<int> mNextLOS; std::atomic<int> mNextLOS;

View file

@ -1,6 +1,7 @@
#ifndef OPENMW_BARRIER_H #ifndef OPENMW_BARRIER_H
#define OPENMW_BARRIER_H #define OPENMW_BARRIER_H
#include <cassert>
#include <condition_variable> #include <condition_variable>
#include <mutex> #include <mutex>
@ -12,7 +13,9 @@ namespace Misc
public: public:
/// @param count number of threads to wait on /// @param count number of threads to wait on
explicit Barrier(int count) : mThreadCount(count), mRendezvousCount(0), mGeneration(0) explicit Barrier(int count) : mThreadCount(count), mRendezvousCount(0), mGeneration(0)
{} {
assert(count >= 0);
}
/// @brief stop execution of threads until count distinct threads reach this point /// @brief stop execution of threads until count distinct threads reach this point
/// @param func callable to be executed once after all threads have met /// @param func callable to be executed once after all threads have met
@ -22,8 +25,8 @@ namespace Misc
std::unique_lock lock(mMutex); std::unique_lock lock(mMutex);
++mRendezvousCount; ++mRendezvousCount;
const int currentGeneration = mGeneration; const unsigned int currentGeneration = mGeneration;
if (mRendezvousCount == mThreadCount) if (mRendezvousCount == mThreadCount || mThreadCount == 0)
{ {
++mGeneration; ++mGeneration;
mRendezvousCount = 0; mRendezvousCount = 0;
@ -37,9 +40,9 @@ namespace Misc
} }
private: private:
int mThreadCount; unsigned int mThreadCount;
int mRendezvousCount; unsigned int mRendezvousCount;
int mGeneration; unsigned int mGeneration;
mutable std::mutex mMutex; mutable std::mutex mMutex;
std::condition_variable mRendezvous; std::condition_variable mRendezvous;
}; };