Render only cached recast mesh

To avoid waiting while recast mesh is generating.

Remove redundant continue.
pull/3169/head
elsid 3 years ago
parent daff7aba01
commit 0c8a811ad5
No known key found for this signature in database
GPG Key ID: B845CB9FEE18AB40

@ -60,7 +60,6 @@ namespace MWRender
it->second.mValue = group; it->second.mValue = group;
it->second.mGeneration = tile->second->getGeneration(); it->second.mGeneration = tile->second->getGeneration();
it->second.mRevision = tile->second->getRevision(); it->second.mRevision = tile->second->getRevision();
continue;
} }
++it; ++it;

@ -13,7 +13,7 @@ namespace DetourNavigator
{ {
if (!mImpl.addObject(id, shape, transform, areaType)) if (!mImpl.addObject(id, shape, transform, areaType))
return false; return false;
mCached.lock()->reset(); mOutdatedCache = true;
return true; return true;
} }
@ -21,7 +21,7 @@ namespace DetourNavigator
{ {
if (!mImpl.updateObject(id, transform, areaType)) if (!mImpl.updateObject(id, transform, areaType))
return false; return false;
mCached.lock()->reset(); mOutdatedCache = true;
return true; return true;
} }
@ -29,7 +29,7 @@ namespace DetourNavigator
{ {
auto object = mImpl.removeObject(id); auto object = mImpl.removeObject(id);
if (object) if (object)
mCached.lock()->reset(); mOutdatedCache = true;
return object; return object;
} }
@ -38,7 +38,7 @@ namespace DetourNavigator
{ {
if (!mImpl.addWater(cellPosition, cellSize, shift)) if (!mImpl.addWater(cellPosition, cellSize, shift))
return false; return false;
mCached.lock()->reset(); mOutdatedCache = true;
return true; return true;
} }
@ -46,7 +46,7 @@ namespace DetourNavigator
{ {
const auto water = mImpl.removeWater(cellPosition); const auto water = mImpl.removeWater(cellPosition);
if (water) if (water)
mCached.lock()->reset(); mOutdatedCache = true;
return water; return water;
} }
@ -55,7 +55,7 @@ namespace DetourNavigator
{ {
if (!mImpl.addHeightfield(cellPosition, cellSize, shift, shape)) if (!mImpl.addHeightfield(cellPosition, cellSize, shift, shape))
return false; return false;
mCached.lock()->reset(); mOutdatedCache = true;
return true; return true;
} }
@ -63,18 +63,27 @@ namespace DetourNavigator
{ {
const auto cell = mImpl.removeHeightfield(cellPosition); const auto cell = mImpl.removeHeightfield(cellPosition);
if (cell) if (cell)
mCached.lock()->reset(); mOutdatedCache = true;
return cell; return cell;
} }
std::shared_ptr<RecastMesh> CachedRecastMeshManager::getMesh() std::shared_ptr<RecastMesh> CachedRecastMeshManager::getMesh()
{ {
std::shared_ptr<RecastMesh> cached = *mCached.lock(); bool outdated = true;
if (!mOutdatedCache.compare_exchange_strong(outdated, false))
{
std::shared_ptr<RecastMesh> cached = getCachedMesh();
if (cached != nullptr) if (cached != nullptr)
return cached; return cached;
cached = mImpl.getMesh(); }
*mCached.lock() = cached; std::shared_ptr<RecastMesh> mesh = mImpl.getMesh();
return cached; *mCached.lock() = mesh;
return mesh;
}
std::shared_ptr<RecastMesh> CachedRecastMeshManager::getCachedMesh() const
{
return *mCached.lockConst();
} }
bool CachedRecastMeshManager::isEmpty() const bool CachedRecastMeshManager::isEmpty() const

@ -7,6 +7,8 @@
#include <components/misc/guarded.hpp> #include <components/misc/guarded.hpp>
#include <atomic>
namespace DetourNavigator namespace DetourNavigator
{ {
class CachedRecastMeshManager class CachedRecastMeshManager
@ -32,6 +34,8 @@ namespace DetourNavigator
std::shared_ptr<RecastMesh> getMesh(); std::shared_ptr<RecastMesh> getMesh();
std::shared_ptr<RecastMesh> getCachedMesh() const;
bool isEmpty() const; bool isEmpty() const;
void reportNavMeshChange(const Version& recastMeshVersion, const Version& navMeshVersion); void reportNavMeshChange(const Version& recastMeshVersion, const Version& navMeshVersion);
@ -41,6 +45,7 @@ namespace DetourNavigator
private: private:
RecastMeshManager mImpl; RecastMeshManager mImpl;
Misc::ScopeGuarded<std::shared_ptr<RecastMesh>> mCached; Misc::ScopeGuarded<std::shared_ptr<RecastMesh>> mCached;
std::atomic_bool mOutdatedCache {true};
}; };
} }

@ -236,7 +236,7 @@ namespace DetourNavigator
std::optional<osg::Vec3f> raycast(const osg::Vec3f& agentHalfExtents, const osg::Vec3f& start, std::optional<osg::Vec3f> raycast(const osg::Vec3f& agentHalfExtents, const osg::Vec3f& start,
const osg::Vec3f& end, const Flags includeFlags) const; const osg::Vec3f& end, const Flags includeFlags) const;
virtual RecastMeshTiles getRecastMeshTiles() = 0; virtual RecastMeshTiles getRecastMeshTiles() const = 0;
virtual float getMaxNavmeshAreaRealRadius() const = 0; virtual float getMaxNavmeshAreaRealRadius() const = 0;
}; };

@ -187,7 +187,7 @@ namespace DetourNavigator
mNavMeshManager.reportStats(frameNumber, stats); mNavMeshManager.reportStats(frameNumber, stats);
} }
RecastMeshTiles NavigatorImpl::getRecastMeshTiles() RecastMeshTiles NavigatorImpl::getRecastMeshTiles() const
{ {
return mNavMeshManager.getRecastMeshTiles(); return mNavMeshManager.getRecastMeshTiles();
} }

@ -60,7 +60,7 @@ namespace DetourNavigator
void reportStats(unsigned int frameNumber, osg::Stats& stats) const override; void reportStats(unsigned int frameNumber, osg::Stats& stats) const override;
RecastMeshTiles getRecastMeshTiles() override; RecastMeshTiles getRecastMeshTiles() const override;
float getMaxNavmeshAreaRealRadius() const override; float getMaxNavmeshAreaRealRadius() const override;

@ -94,7 +94,7 @@ namespace DetourNavigator
void reportStats(unsigned int /*frameNumber*/, osg::Stats& /*stats*/) const override {} void reportStats(unsigned int /*frameNumber*/, osg::Stats& /*stats*/) const override {}
RecastMeshTiles getRecastMeshTiles() override RecastMeshTiles getRecastMeshTiles() const override
{ {
return {}; return {};
} }

@ -232,14 +232,15 @@ namespace DetourNavigator
mAsyncNavMeshUpdater.reportStats(frameNumber, stats); mAsyncNavMeshUpdater.reportStats(frameNumber, stats);
} }
RecastMeshTiles NavMeshManager::getRecastMeshTiles() RecastMeshTiles NavMeshManager::getRecastMeshTiles() const
{ {
std::vector<TilePosition> tiles; std::vector<TilePosition> tiles;
mRecastMeshManager.forEachTile( mRecastMeshManager.forEachTile(
[&tiles] (const TilePosition& tile, const CachedRecastMeshManager&) { tiles.push_back(tile); }); [&tiles] (const TilePosition& tile, const CachedRecastMeshManager&) { tiles.push_back(tile); });
RecastMeshTiles result; RecastMeshTiles result;
std::transform(tiles.begin(), tiles.end(), std::inserter(result, result.end()), for (const TilePosition& tile : tiles)
[this] (const TilePosition& tile) { return std::make_pair(tile, mRecastMeshManager.getMesh(tile)); }); if (auto mesh = mRecastMeshManager.getCachedMesh(tile))
result.emplace(tile, std::move(mesh));
return result; return result;
} }

@ -59,7 +59,7 @@ namespace DetourNavigator
void reportStats(unsigned int frameNumber, osg::Stats& stats) const; void reportStats(unsigned int frameNumber, osg::Stats& stats) const;
RecastMeshTiles getRecastMeshTiles(); RecastMeshTiles getRecastMeshTiles() const;
private: private:
const Settings& mSettings; const Settings& mSettings;

@ -192,17 +192,16 @@ namespace DetourNavigator
std::shared_ptr<RecastMesh> TileCachedRecastMeshManager::getMesh(const TilePosition& tilePosition) const std::shared_ptr<RecastMesh> TileCachedRecastMeshManager::getMesh(const TilePosition& tilePosition) const
{ {
const auto manager = [&] () -> std::shared_ptr<CachedRecastMeshManager> if (const auto manager = getManager(tilePosition))
{ return manager->getMesh();
const auto tiles = mTiles.lockConst();
const auto it = tiles->find(tilePosition);
if (it == tiles->end())
return nullptr; return nullptr;
return it->second; }
} ();
if (manager == nullptr) std::shared_ptr<RecastMesh> TileCachedRecastMeshManager::getCachedMesh(const TilePosition& tilePosition) const
{
if (const auto manager = getManager(tilePosition))
return manager->getCachedMesh();
return nullptr; return nullptr;
return manager->getMesh();
} }
std::size_t TileCachedRecastMeshManager::getRevision() const std::size_t TileCachedRecastMeshManager::getRevision() const
@ -256,4 +255,13 @@ namespace DetourNavigator
} }
return tileResult; return tileResult;
} }
std::shared_ptr<CachedRecastMeshManager> TileCachedRecastMeshManager::getManager(const TilePosition& tilePosition) const
{
const auto tiles = mTiles.lockConst();
const auto it = tiles->find(tilePosition);
if (it == tiles->end())
return nullptr;
return it->second;
}
} }

@ -88,6 +88,8 @@ namespace DetourNavigator
std::shared_ptr<RecastMesh> getMesh(const TilePosition& tilePosition) const; std::shared_ptr<RecastMesh> getMesh(const TilePosition& tilePosition) const;
std::shared_ptr<RecastMesh> getCachedMesh(const TilePosition& tilePosition) const;
template <class Function> template <class Function>
void forEachTile(Function&& function) const void forEachTile(Function&& function) const
{ {
@ -118,6 +120,8 @@ namespace DetourNavigator
std::optional<RemovedRecastMeshObject> removeTile(const ObjectId id, const TilePosition& tilePosition, std::optional<RemovedRecastMeshObject> removeTile(const ObjectId id, const TilePosition& tilePosition,
TilesMap& tiles); TilesMap& tiles);
inline std::shared_ptr<CachedRecastMeshManager> getManager(const TilePosition& tilePosition) const;
}; };
} }

Loading…
Cancel
Save