mirror of
https://github.com/OpenMW/openmw.git
synced 2025-01-19 20:23:54 +00:00
Use some C++17 where it makes the code more readable
Also replace boost::optional
This commit is contained in:
parent
4fc5b6f6f4
commit
1357bba0a0
4 changed files with 32 additions and 35 deletions
|
@ -185,7 +185,7 @@ namespace MWPhysics
|
||||||
mNewFrame = false;
|
mNewFrame = false;
|
||||||
if (mLOSCacheExpiry >= 0)
|
if (mLOSCacheExpiry >= 0)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::shared_timed_mutex> lock(mLOSCacheMutex);
|
std::unique_lock lock(mLOSCacheMutex);
|
||||||
mLOSCache.erase(
|
mLOSCache.erase(
|
||||||
std::remove_if(mLOSCache.begin(), mLOSCache.end(),
|
std::remove_if(mLOSCache.begin(), mLOSCache.end(),
|
||||||
[](const LOSRequest& req) { return req.mStale; }),
|
[](const LOSRequest& req) { return req.mStale; }),
|
||||||
|
@ -196,7 +196,7 @@ namespace MWPhysics
|
||||||
|
|
||||||
PhysicsTaskScheduler::~PhysicsTaskScheduler()
|
PhysicsTaskScheduler::~PhysicsTaskScheduler()
|
||||||
{
|
{
|
||||||
std::unique_lock<std::shared_timed_mutex> lock(mSimulationMutex);
|
std::unique_lock lock(mSimulationMutex);
|
||||||
mQuit = true;
|
mQuit = true;
|
||||||
mNumJobs = 0;
|
mNumJobs = 0;
|
||||||
mRemainingSteps = 0;
|
mRemainingSteps = 0;
|
||||||
|
@ -211,7 +211,7 @@ namespace MWPhysics
|
||||||
// This function run in the main thread.
|
// This function run in the main thread.
|
||||||
// While the mSimulationMutex is held, background physics threads can't run.
|
// While the mSimulationMutex is held, background physics threads can't run.
|
||||||
|
|
||||||
std::unique_lock<std::shared_timed_mutex> lock(mSimulationMutex);
|
std::unique_lock lock(mSimulationMutex);
|
||||||
|
|
||||||
// start by finishing previous background computation
|
// start by finishing previous background computation
|
||||||
if (mNumThreads != 0)
|
if (mNumThreads != 0)
|
||||||
|
@ -294,25 +294,25 @@ 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<std::shared_timed_mutex> lock(mCollisionWorldMutex, mThreadSafeBullet);
|
MaybeSharedLock lock(mCollisionWorldMutex, mThreadSafeBullet);
|
||||||
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<std::shared_timed_mutex> lock(mCollisionWorldMutex, mThreadSafeBullet);
|
MaybeSharedLock lock(mCollisionWorldMutex, mThreadSafeBullet);
|
||||||
mCollisionWorld->convexSweepTest(castShape, from, to, resultCallback);
|
mCollisionWorld->convexSweepTest(castShape, from, to, resultCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsTaskScheduler::contactTest(btCollisionObject* colObj, btCollisionWorld::ContactResultCallback& resultCallback)
|
void PhysicsTaskScheduler::contactTest(btCollisionObject* colObj, btCollisionWorld::ContactResultCallback& resultCallback)
|
||||||
{
|
{
|
||||||
std::shared_lock<std::shared_timed_mutex> lock(mCollisionWorldMutex);
|
std::shared_lock lock(mCollisionWorldMutex);
|
||||||
mCollisionWorld->contactTest(colObj, resultCallback);
|
mCollisionWorld->contactTest(colObj, resultCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
boost::optional<btVector3> PhysicsTaskScheduler::getHitPoint(const btTransform& from, btCollisionObject* target)
|
std::optional<btVector3> PhysicsTaskScheduler::getHitPoint(const btTransform& from, btCollisionObject* target)
|
||||||
{
|
{
|
||||||
MaybeSharedLock<std::shared_timed_mutex> lock(mCollisionWorldMutex, mThreadSafeBullet);
|
MaybeSharedLock lock(mCollisionWorldMutex, mThreadSafeBullet);
|
||||||
// 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();
|
||||||
|
@ -323,37 +323,37 @@ namespace MWPhysics
|
||||||
mCollisionWorld->rayTestSingle(from, rayTo, target, target->getCollisionShape(), target->getWorldTransform(), cb);
|
mCollisionWorld->rayTestSingle(from, rayTo, target, target->getCollisionShape(), target->getWorldTransform(), cb);
|
||||||
if (!cb.hasHit())
|
if (!cb.hasHit())
|
||||||
// didn't hit the target. this could happen if point is already inside the collision box
|
// didn't hit the target. this could happen if point is already inside the collision box
|
||||||
return boost::none;
|
return std::nullopt;
|
||||||
return {cb.m_hitPointWorld};
|
return {cb.m_hitPointWorld};
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsTaskScheduler::aabbTest(const btVector3& aabbMin, const btVector3& aabbMax, btBroadphaseAabbCallback& callback)
|
void PhysicsTaskScheduler::aabbTest(const btVector3& aabbMin, const btVector3& aabbMax, btBroadphaseAabbCallback& callback)
|
||||||
{
|
{
|
||||||
std::shared_lock<std::shared_timed_mutex> lock(mCollisionWorldMutex);
|
std::shared_lock lock(mCollisionWorldMutex);
|
||||||
mCollisionWorld->getBroadphase()->aabbTest(aabbMin, aabbMax, callback);
|
mCollisionWorld->getBroadphase()->aabbTest(aabbMin, aabbMax, callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsTaskScheduler::getAabb(const btCollisionObject* obj, btVector3& min, btVector3& max)
|
void PhysicsTaskScheduler::getAabb(const btCollisionObject* obj, btVector3& min, btVector3& max)
|
||||||
{
|
{
|
||||||
std::shared_lock<std::shared_timed_mutex> lock(mCollisionWorldMutex);
|
std::shared_lock lock(mCollisionWorldMutex);
|
||||||
obj->getCollisionShape()->getAabb(obj->getWorldTransform(), min, max);
|
obj->getCollisionShape()->getAabb(obj->getWorldTransform(), min, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsTaskScheduler::setCollisionFilterMask(btCollisionObject* collisionObject, int collisionFilterMask)
|
void PhysicsTaskScheduler::setCollisionFilterMask(btCollisionObject* collisionObject, int collisionFilterMask)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::shared_timed_mutex> lock(mCollisionWorldMutex);
|
std::unique_lock lock(mCollisionWorldMutex);
|
||||||
collisionObject->getBroadphaseHandle()->m_collisionFilterMask = collisionFilterMask;
|
collisionObject->getBroadphaseHandle()->m_collisionFilterMask = collisionFilterMask;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsTaskScheduler::addCollisionObject(btCollisionObject* collisionObject, int collisionFilterGroup, int collisionFilterMask)
|
void PhysicsTaskScheduler::addCollisionObject(btCollisionObject* collisionObject, int collisionFilterGroup, int collisionFilterMask)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::shared_timed_mutex> lock(mCollisionWorldMutex);
|
std::unique_lock lock(mCollisionWorldMutex);
|
||||||
mCollisionWorld->addCollisionObject(collisionObject, collisionFilterGroup, collisionFilterMask);
|
mCollisionWorld->addCollisionObject(collisionObject, collisionFilterGroup, collisionFilterMask);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PhysicsTaskScheduler::removeCollisionObject(btCollisionObject* collisionObject)
|
void PhysicsTaskScheduler::removeCollisionObject(btCollisionObject* collisionObject)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::shared_timed_mutex> lock(mCollisionWorldMutex);
|
std::unique_lock lock(mCollisionWorldMutex);
|
||||||
mCollisionWorld->removeCollisionObject(collisionObject);
|
mCollisionWorld->removeCollisionObject(collisionObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -361,19 +361,19 @@ namespace MWPhysics
|
||||||
{
|
{
|
||||||
if (mDeferAabbUpdate)
|
if (mDeferAabbUpdate)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(mUpdateAabbMutex);
|
std::unique_lock lock(mUpdateAabbMutex);
|
||||||
mUpdateAabb.insert(std::move(ptr));
|
mUpdateAabb.insert(std::move(ptr));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::unique_lock<std::shared_timed_mutex> lock(mCollisionWorldMutex);
|
std::unique_lock lock(mCollisionWorldMutex);
|
||||||
updatePtrAabb(ptr);
|
updatePtrAabb(ptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PhysicsTaskScheduler::getLineOfSight(const std::weak_ptr<Actor>& actor1, const std::weak_ptr<Actor>& actor2)
|
bool PhysicsTaskScheduler::getLineOfSight(const std::weak_ptr<Actor>& actor1, const std::weak_ptr<Actor>& actor2)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::shared_timed_mutex> lock(mLOSCacheMutex);
|
std::unique_lock lock(mLOSCacheMutex);
|
||||||
|
|
||||||
auto actorPtr1 = actor1.lock();
|
auto actorPtr1 = actor1.lock();
|
||||||
auto actorPtr2 = actor2.lock();
|
auto actorPtr2 = actor2.lock();
|
||||||
|
@ -395,7 +395,7 @@ namespace MWPhysics
|
||||||
|
|
||||||
void PhysicsTaskScheduler::refreshLOSCache()
|
void PhysicsTaskScheduler::refreshLOSCache()
|
||||||
{
|
{
|
||||||
std::shared_lock<std::shared_timed_mutex> lock(mLOSCacheMutex);
|
std::shared_lock lock(mLOSCacheMutex);
|
||||||
int job = 0;
|
int job = 0;
|
||||||
int numLOS = mLOSCache.size();
|
int numLOS = mLOSCache.size();
|
||||||
while ((job = mNextLOS.fetch_add(1, std::memory_order_relaxed)) < numLOS)
|
while ((job = mNextLOS.fetch_add(1, std::memory_order_relaxed)) < numLOS)
|
||||||
|
@ -414,9 +414,7 @@ namespace MWPhysics
|
||||||
|
|
||||||
void PhysicsTaskScheduler::updateAabbs()
|
void PhysicsTaskScheduler::updateAabbs()
|
||||||
{
|
{
|
||||||
std::unique_lock<std::shared_timed_mutex> lock1(mCollisionWorldMutex, std::defer_lock);
|
std::scoped_lock lock(mCollisionWorldMutex, mUpdateAabbMutex);
|
||||||
std::unique_lock<std::mutex> lock2(mUpdateAabbMutex, std::defer_lock);
|
|
||||||
std::lock(lock1, lock2);
|
|
||||||
std::for_each(mUpdateAabb.begin(), mUpdateAabb.end(),
|
std::for_each(mUpdateAabb.begin(), mUpdateAabb.end(),
|
||||||
[this](const std::weak_ptr<PtrHolder>& ptr) { updatePtrAabb(ptr); });
|
[this](const std::weak_ptr<PtrHolder>& ptr) { updatePtrAabb(ptr); });
|
||||||
mUpdateAabb.clear();
|
mUpdateAabb.clear();
|
||||||
|
@ -441,7 +439,7 @@ namespace MWPhysics
|
||||||
|
|
||||||
void PhysicsTaskScheduler::worker()
|
void PhysicsTaskScheduler::worker()
|
||||||
{
|
{
|
||||||
std::shared_lock<std::shared_timed_mutex> lock(mSimulationMutex);
|
std::shared_lock lock(mSimulationMutex);
|
||||||
while (!mQuit)
|
while (!mQuit)
|
||||||
{
|
{
|
||||||
if (!mNewFrame)
|
if (!mNewFrame)
|
||||||
|
@ -453,7 +451,7 @@ namespace MWPhysics
|
||||||
int job = 0;
|
int job = 0;
|
||||||
while (mRemainingSteps && (job = mNextJob.fetch_add(1, std::memory_order_relaxed)) < mNumJobs)
|
while (mRemainingSteps && (job = mNextJob.fetch_add(1, std::memory_order_relaxed)) < mNumJobs)
|
||||||
{
|
{
|
||||||
MaybeSharedLock<std::shared_timed_mutex> lockColWorld(mCollisionWorldMutex, mThreadSafeBullet);
|
MaybeSharedLock lockColWorld(mCollisionWorldMutex, mThreadSafeBullet);
|
||||||
if(const auto actor = mActorsFrameData[job].mActor.lock())
|
if(const auto actor = mActorsFrameData[job].mActor.lock())
|
||||||
MovementSolver::move(mActorsFrameData[job], mPhysicsDt, mCollisionWorld.get(), *mWorldFrameData);
|
MovementSolver::move(mActorsFrameData[job], mPhysicsDt, mCollisionWorld.get(), *mWorldFrameData);
|
||||||
}
|
}
|
||||||
|
@ -481,7 +479,7 @@ namespace MWPhysics
|
||||||
|
|
||||||
void PhysicsTaskScheduler::updateActorsPositions()
|
void PhysicsTaskScheduler::updateActorsPositions()
|
||||||
{
|
{
|
||||||
std::unique_lock<std::shared_timed_mutex> lock(mCollisionWorldMutex);
|
std::unique_lock lock(mCollisionWorldMutex);
|
||||||
for (auto& actorData : mActorsFrameData)
|
for (auto& actorData : mActorsFrameData)
|
||||||
{
|
{
|
||||||
if(const auto actor = actorData.mActor.lock())
|
if(const auto actor = actorData.mActor.lock())
|
||||||
|
@ -499,7 +497,7 @@ namespace MWPhysics
|
||||||
|
|
||||||
void PhysicsTaskScheduler::udpateActorsAabbs()
|
void PhysicsTaskScheduler::udpateActorsAabbs()
|
||||||
{
|
{
|
||||||
std::unique_lock<std::shared_timed_mutex> lock(mCollisionWorldMutex);
|
std::unique_lock lock(mCollisionWorldMutex);
|
||||||
for (const auto& actorData : mActorsFrameData)
|
for (const auto& actorData : mActorsFrameData)
|
||||||
if (actorData.mPositionChanged)
|
if (actorData.mPositionChanged)
|
||||||
{
|
{
|
||||||
|
@ -517,7 +515,7 @@ 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<std::shared_timed_mutex> lockColWorld(mCollisionWorldMutex, mThreadSafeBullet);
|
MaybeSharedLock lockColWorld(mCollisionWorldMutex, mThreadSafeBullet);
|
||||||
mCollisionWorld->rayTest(pos1, pos2, resultCallback);
|
mCollisionWorld->rayTest(pos1, pos2, resultCallback);
|
||||||
|
|
||||||
return !resultCallback.hasHit();
|
return !resultCallback.hasHit();
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
|
#include <optional>
|
||||||
#include <shared_mutex>
|
#include <shared_mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
|
@ -35,7 +36,7 @@ namespace MWPhysics
|
||||||
void rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, btCollisionWorld::RayResultCallback& resultCallback) const;
|
void rayTest(const btVector3& rayFromWorld, const btVector3& rayToWorld, btCollisionWorld::RayResultCallback& resultCallback) const;
|
||||||
void convexSweepTest(const btConvexShape* castShape, const btTransform& from, const btTransform& to, btCollisionWorld::ConvexResultCallback& resultCallback) const;
|
void convexSweepTest(const btConvexShape* castShape, const btTransform& from, const btTransform& to, btCollisionWorld::ConvexResultCallback& resultCallback) const;
|
||||||
void contactTest(btCollisionObject* colObj, btCollisionWorld::ContactResultCallback& resultCallback);
|
void contactTest(btCollisionObject* colObj, btCollisionWorld::ContactResultCallback& resultCallback);
|
||||||
boost::optional<btVector3> getHitPoint(const btTransform& from, btCollisionObject* target);
|
std::optional<btVector3> getHitPoint(const btTransform& from, btCollisionObject* target);
|
||||||
void aabbTest(const btVector3& aabbMin, const btVector3& aabbMax, btBroadphaseAabbCallback& callback);
|
void aabbTest(const btVector3& aabbMin, const btVector3& aabbMax, btBroadphaseAabbCallback& callback);
|
||||||
void getAabb(const btCollisionObject* obj, btVector3& min, btVector3& max);
|
void getAabb(const btCollisionObject* obj, btVector3& min, btVector3& max);
|
||||||
void setCollisionFilterMask(btCollisionObject* collisionObject, int collisionFilterMask);
|
void setCollisionFilterMask(btCollisionObject* collisionObject, int collisionFilterMask);
|
||||||
|
@ -82,9 +83,9 @@ namespace MWPhysics
|
||||||
std::atomic<int> mNextLOS;
|
std::atomic<int> mNextLOS;
|
||||||
std::vector<std::thread> mThreads;
|
std::vector<std::thread> mThreads;
|
||||||
|
|
||||||
mutable std::shared_timed_mutex mSimulationMutex;
|
mutable std::shared_mutex mSimulationMutex;
|
||||||
mutable std::shared_timed_mutex mCollisionWorldMutex;
|
mutable std::shared_mutex mCollisionWorldMutex;
|
||||||
mutable std::shared_timed_mutex mLOSCacheMutex;
|
mutable std::shared_mutex mLOSCacheMutex;
|
||||||
mutable std::mutex mUpdateAabbMutex;
|
mutable std::mutex mUpdateAabbMutex;
|
||||||
std::condition_variable_any mHasJob;
|
std::condition_variable_any mHasJob;
|
||||||
};
|
};
|
||||||
|
|
|
@ -239,7 +239,7 @@ namespace MWPhysics
|
||||||
|
|
||||||
auto hitpoint = mTaskScheduler->getHitPoint(rayFrom, targetCollisionObj);
|
auto hitpoint = mTaskScheduler->getHitPoint(rayFrom, targetCollisionObj);
|
||||||
if (hitpoint)
|
if (hitpoint)
|
||||||
return (point - Misc::Convert::toOsg(hitpoint.get())).length();
|
return (point - Misc::Convert::toOsg(*hitpoint)).length();
|
||||||
|
|
||||||
// didn't hit the target. this could happen if point is already inside the collision box
|
// didn't hit the target. this could happen if point is already inside the collision box
|
||||||
return 0.f;
|
return 0.f;
|
||||||
|
@ -686,10 +686,8 @@ namespace MWPhysics
|
||||||
std::vector<ActorFrameData> actorsFrameData;
|
std::vector<ActorFrameData> actorsFrameData;
|
||||||
actorsFrameData.reserve(mMovementQueue.size());
|
actorsFrameData.reserve(mMovementQueue.size());
|
||||||
const MWBase::World *world = MWBase::Environment::get().getWorld();
|
const MWBase::World *world = MWBase::Environment::get().getWorld();
|
||||||
for (const auto& m : mMovementQueue)
|
for (const auto& [character, movement] : mMovementQueue)
|
||||||
{
|
{
|
||||||
const auto& character = m.first;
|
|
||||||
const auto& movement = m.second;
|
|
||||||
const auto foundActor = mActors.find(character);
|
const auto foundActor = mActors.find(character);
|
||||||
if (foundActor == mActors.end()) // actor was already removed from the scene
|
if (foundActor == mActors.end()) // actor was already removed from the scene
|
||||||
{
|
{
|
||||||
|
|
|
@ -21,7 +21,7 @@ namespace Misc
|
||||||
/// @brief stop execution of threads until count distinct threads reach this point
|
/// @brief stop execution of threads until count distinct threads reach this point
|
||||||
void wait()
|
void wait()
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(mMutex);
|
std::unique_lock lock(mMutex);
|
||||||
|
|
||||||
++mRendezvousCount;
|
++mRendezvousCount;
|
||||||
const int currentGeneration = mGeneration;
|
const int currentGeneration = mGeneration;
|
||||||
|
|
Loading…
Reference in a new issue