Merge branch 'navmeshtool_stats' into 'master'

Report more stats by navmeshtool

See merge request OpenMW/openmw!5260
This commit is contained in:
jvoisin
2026-04-05 20:04:05 +00:00
8 changed files with 171 additions and 45 deletions
+37 -5
View File
@@ -120,6 +120,9 @@ namespace NavMeshTool
addOption("write-binary-log", bpo::value<bool>()->implicit_value(true)->default_value(false),
"write progress in binary messages to be consumed by the launcher");
addOption("collect-stats", bpo::value<bool>()->implicit_value(true)->default_value(false),
"collect statistics for generated navmesh tiles including existing ones stored in database");
addOption("worldspace-filter", bpo::value<std::string>()->default_value(".*"),
"Regular expression to filter in specified worldspaces in modified ECMAScript grammar (see "
"https://en.cppreference.com/w/cpp/regex/ecmascript.html)");
@@ -184,6 +187,7 @@ namespace NavMeshTool
const bool processInteriorCells = variables["process-interior-cells"].as<bool>();
const bool removeUnusedTiles = variables["remove-unused-tiles"].as<bool>();
const bool writeBinaryLog = variables["write-binary-log"].as<bool>();
const bool collectStats = variables["collect-stats"].as<bool>();
const std::regex worldspaceFilter(variables["worldspace-filter"].as<std::string>());
@@ -240,8 +244,12 @@ namespace NavMeshTool
= collectWorldspaceCells(esmData, processInteriorCells, worldspaceFilter);
Status status = Status::Ok;
bool needVacuum = false;
std::size_t provided = 0;
std::size_t inserted = 0;
std::size_t updated = 0;
std::size_t deleted = 0;
std::size_t count = 0;
GenerateTilesStats stats;
{
SceneUtil::WorkQueue workQueue(threadsNumber);
@@ -253,8 +261,14 @@ namespace NavMeshTool
const WorldspaceData worldspaceData = gatherWorldspaceData(navigatorSettings, readers, vfs,
bulletShapeManager, esmData, writeBinaryLog, worldspace, cells);
const Result result = generateAllNavMeshTiles(agentBounds, navigatorSettings, removeUnusedTiles,
writeBinaryLog, worldspaceData, db, workQueue);
const GenerateAllNavMeshTilesOptions generateAllNavMeshTilesOptions{
.mRemoveUnusedTiles = removeUnusedTiles,
.mWriteBinaryLog = writeBinaryLog,
.mCollectStats = collectStats,
};
const GenerateTilesResult result = generateAllNavMeshTiles(
agentBounds, navigatorSettings, generateAllNavMeshTilesOptions, worldspaceData, db, workQueue);
++count;
@@ -262,14 +276,32 @@ namespace NavMeshTool
<< worldspace;
status = result.mStatus;
needVacuum = needVacuum || result.mNeedVacuum;
provided += result.mProvided;
inserted += result.mInserted;
updated += result.mUpdated;
deleted += result.mDeleted;
if (collectStats)
{
stats.mMaxPolyCountPerTile
= std::max(stats.mMaxPolyCountPerTile, result.mStats.mMaxPolyCountPerTile);
}
if (status != Status::Ok)
break;
}
}
if (status == Status::Ok && needVacuum)
Log(Debug::Info) << "Generated navmesh for " << provided << " tiles: " << inserted << " inserted, "
<< updated << " updated, " << deleted << " deleted";
if (collectStats)
{
Log(Debug::Info) << "Stats:";
Log(Debug::Info) << "max polygons per tile = " << stats.mMaxPolyCountPerTile;
}
if (status == Status::Ok && inserted + updated + deleted > 0)
{
Log(Debug::Info) << "Vacuuming the database...";
db.vacuum();
+57 -22
View File
@@ -36,6 +36,7 @@ namespace NavMeshTool
using DetourNavigator::GenerateNavMeshTile;
using DetourNavigator::MeshSource;
using DetourNavigator::NavMeshDb;
using DetourNavigator::NavMeshTileConsumerStats;
using DetourNavigator::NavMeshTileInfo;
using DetourNavigator::PreparedNavMeshData;
using DetourNavigator::RecastMesh;
@@ -77,10 +78,11 @@ namespace NavMeshTool
public:
std::atomic_size_t mExpected{ 0 };
explicit NavMeshTileConsumer(NavMeshDb& db, bool removeUnusedTiles, bool writeBinaryLog)
explicit NavMeshTileConsumer(NavMeshDb& db, const GenerateAllNavMeshTilesOptions& options)
: mDb(db)
, mRemoveUnusedTiles(removeUnusedTiles)
, mWriteBinaryLog(writeBinaryLog)
, mRemoveUnusedTiles(options.mRemoveUnusedTiles)
, mWriteBinaryLog(options.mWriteBinaryLog)
, mCollectStats(options.mCollectStats)
, mTransaction(mDb.startTransaction(Sqlite3::TransactionMode::Immediate))
, mNextTileId(mDb.getMaxTileId() + 1)
, mNextShapeId(mDb.getMaxShapeId() + 1)
@@ -99,6 +101,8 @@ namespace NavMeshTool
return mDeleted;
}
GenerateTilesStats getStats() const { return *mStats.lockConst(); }
std::int64_t resolveMeshSource(const MeshSource& source) override
{
const std::lock_guard lock(mMutex);
@@ -110,12 +114,27 @@ namespace NavMeshTool
{
std::optional<NavMeshTileInfo> result;
std::lock_guard lock(mMutex);
if (const auto tile = mDb.findTile(worldspace, tilePosition, input))
if (mCollectStats)
{
NavMeshTileInfo info;
info.mTileId = tile->mTileId;
info.mVersion = tile->mVersion;
result.emplace(info);
if (const auto tile = mDb.getTileData(worldspace, tilePosition, input))
{
NavMeshTileInfo info;
info.mTileId = tile->mTileId;
info.mVersion = tile->mVersion;
info.mData = std::make_unique<PreparedNavMeshData>();
deserialize(tile->mData, *info.mData);
result.emplace(std::move(info));
}
}
else
{
if (const auto tile = mDb.findTile(worldspace, tilePosition, input))
{
NavMeshTileInfo info;
info.mTileId = tile->mTileId;
info.mVersion = tile->mVersion;
result.emplace(std::move(info));
}
}
return result;
}
@@ -184,8 +203,16 @@ namespace NavMeshTool
mHasTile.notify_one();
}
void updateStats(const NavMeshTileConsumerStats& value) override
{
const Misc::Locked<GenerateTilesStats> stats = mStats.lock();
stats->mMaxPolyCountPerTile = std::max(stats->mMaxPolyCountPerTile, value.mPolyCount);
}
Status wait()
{
if (mExpected == 0)
return Status::Ok;
constexpr std::chrono::seconds transactionInterval(1);
std::unique_lock lock(mMutex);
auto start = std::chrono::steady_clock::now();
@@ -231,12 +258,14 @@ namespace NavMeshTool
NavMeshDb& mDb;
const bool mRemoveUnusedTiles;
const bool mWriteBinaryLog;
const bool mCollectStats;
Transaction mTransaction;
TileId mNextTileId;
std::condition_variable mHasTile;
Misc::ProgressReporter<LogGeneratedTiles> mReporter;
ShapeId mNextShapeId;
std::mutex mReportMutex;
Misc::ScopeGuarded<GenerateTilesStats> mStats;
void report()
{
@@ -266,18 +295,19 @@ namespace NavMeshTool
};
}
Result generateAllNavMeshTiles(const AgentBounds& agentBounds, const Settings& settings, bool removeUnusedTiles,
bool writeBinaryLog, const WorldspaceData& data, NavMeshDb& db, SceneUtil::WorkQueue& workQueue)
GenerateTilesResult generateAllNavMeshTiles(const AgentBounds& agentBounds, const Settings& settings,
const GenerateAllNavMeshTilesOptions& options, const WorldspaceData& data, NavMeshDb& db,
SceneUtil::WorkQueue& workQueue)
{
Log(Debug::Info) << "Generating navmesh tiles for " << data.mWorldspace << " worldspace...";
const std::shared_ptr<NavMeshTileConsumer> navMeshTileConsumer
= std::make_shared<NavMeshTileConsumer>(db, removeUnusedTiles, writeBinaryLog);
= std::make_shared<NavMeshTileConsumer>(db, options);
const TilesPositionsRange range = DetourNavigator::makeTilesPositionsRange(
Misc::Convert::toOsgXY(data.mAabb.m_min), Misc::Convert::toOsgXY(data.mAabb.m_max), settings.mRecast);
if (removeUnusedTiles)
if (options.mRemoveUnusedTiles)
navMeshTileConsumer->removeTilesOutsideRange(data.mWorldspace, range);
std::vector<TilePosition> worldspaceTiles = data.mTiles;
@@ -285,7 +315,7 @@ namespace NavMeshTool
{
const std::size_t tiles = worldspaceTiles.size();
if (writeBinaryLog)
if (options.mWriteBinaryLog)
serializeToStderr(ExpectedTiles{ static_cast<std::uint64_t>(tiles) });
navMeshTileConsumer->mExpected = tiles;
@@ -300,23 +330,28 @@ namespace NavMeshTool
= std::make_shared<RecastMeshProvider>(data.mTilesData);
for (const TilePosition& tilePosition : worldspaceTiles)
workQueue.addWorkItem(new GenerateNavMeshTile(
data.mWorldspace, tilePosition, recastMeshProvider, agentBounds, settings, navMeshTileConsumer));
workQueue.addWorkItem(new GenerateNavMeshTile(data.mWorldspace, tilePosition, recastMeshProvider,
agentBounds, settings, options.mCollectStats, navMeshTileConsumer));
const Status status = navMeshTileConsumer->wait();
if (status == Status::Ok)
navMeshTileConsumer->commit();
const auto inserted = navMeshTileConsumer->getInserted();
const auto updated = navMeshTileConsumer->getUpdated();
const auto deleted = navMeshTileConsumer->getDeleted();
const std::size_t provided = navMeshTileConsumer->getProvided();
const std::size_t inserted = navMeshTileConsumer->getInserted();
const std::size_t updated = navMeshTileConsumer->getUpdated();
const std::size_t deleted = navMeshTileConsumer->getDeleted();
Log(Debug::Info) << "Generated navmesh for " << navMeshTileConsumer->getProvided() << " tiles, " << inserted
<< " are inserted, " << updated << " updated and " << deleted << " deleted";
Log(Debug::Info) << "Generated navmesh for " << provided << " tiles: " << inserted << " inserted, " << updated
<< " updated, " << deleted << " deleted";
return Result{
return GenerateTilesResult{
.mStatus = status,
.mNeedVacuum = inserted + updated + deleted > 0,
.mProvided = provided,
.mInserted = inserted,
.mUpdated = updated,
.mDeleted = deleted,
.mStats = navMeshTileConsumer->getStats(),
};
}
}
+23 -5
View File
@@ -1,6 +1,8 @@
#ifndef OPENMW_NAVMESHTOOL_NAVMESH_H
#define OPENMW_NAVMESHTOOL_NAVMESH_H
#include <cstddef>
namespace DetourNavigator
{
class NavMeshDb;
@@ -17,6 +19,13 @@ namespace NavMeshTool
{
struct WorldspaceData;
struct GenerateAllNavMeshTilesOptions
{
bool mRemoveUnusedTiles;
bool mWriteBinaryLog;
bool mCollectStats;
};
enum class Status
{
Ok,
@@ -24,14 +33,23 @@ namespace NavMeshTool
NotEnoughSpace,
};
struct Result
struct GenerateTilesStats
{
Status mStatus;
bool mNeedVacuum;
int mMaxPolyCountPerTile = 0;
};
Result generateAllNavMeshTiles(const DetourNavigator::AgentBounds& agentBounds,
const DetourNavigator::Settings& settings, bool removeUnusedTiles, bool writeBinaryLog,
struct GenerateTilesResult
{
Status mStatus;
std::size_t mProvided;
std::size_t mInserted;
std::size_t mUpdated;
std::size_t mDeleted;
GenerateTilesStats mStats;
};
GenerateTilesResult generateAllNavMeshTiles(const DetourNavigator::AgentBounds& agentBounds,
const DetourNavigator::Settings& settings, const GenerateAllNavMeshTilesOptions& options,
const WorldspaceData& data, DetourNavigator::NavMeshDb& db, SceneUtil::WorkQueue& workQueue);
}
@@ -2,12 +2,15 @@
#include "dbrefgeometryobject.hpp"
#include "makenavmesh.hpp"
#include "navmeshdata.hpp"
#include "preparednavmeshdata.hpp"
#include "serialization.hpp"
#include "settings.hpp"
#include <components/debug/debuglog.hpp>
#include <DetourNavMesh.h>
#include <osg/io_utils>
#include <memory>
@@ -34,12 +37,13 @@ namespace DetourNavigator
GenerateNavMeshTile::GenerateNavMeshTile(ESM::RefId worldspace, const TilePosition& tilePosition,
std::weak_ptr<const RecastMeshProvider> recastMeshProvider, const AgentBounds& agentBounds,
const DetourNavigator::Settings& settings, std::weak_ptr<NavMeshTileConsumer> consumer)
const DetourNavigator::Settings& settings, bool collectStats, std::weak_ptr<NavMeshTileConsumer> consumer)
: mWorldspace(worldspace)
, mTilePosition(tilePosition)
, mRecastMeshProvider(std::move(recastMeshProvider))
, mAgentBounds(agentBounds)
, mSettings(settings)
, mCollectStats(collectStats)
, mConsumer(std::move(consumer))
{
}
@@ -78,6 +82,18 @@ namespace DetourNavigator
if (info.has_value() && info->mVersion == navMeshFormatVersion)
{
consumer->identity(mWorldspace, mTilePosition, info->mTileId);
if (mCollectStats)
{
const NavMeshData tileData
= makeNavMeshTileData(*info->mData, {}, mAgentBounds, mTilePosition, mSettings.mRecast);
const dtMeshHeader& header = *reinterpret_cast<dtMeshHeader*>(tileData.mValue.get());
consumer->updateStats(NavMeshTileConsumerStats{
.mPolyCount = header.polyCount,
});
}
ignore.mConsumer = nullptr;
return;
}
@@ -88,11 +104,23 @@ namespace DetourNavigator
if (data == nullptr)
return;
// Verify generated data before consuming.
const NavMeshData tileData = makeNavMeshTileData(*data, {}, mAgentBounds, mTilePosition, mSettings.mRecast);
if (info.has_value())
consumer->update(mWorldspace, mTilePosition, info->mTileId, navMeshFormatVersion, *data);
else
consumer->insert(mWorldspace, mTilePosition, navMeshFormatVersion, input, *data);
if (mCollectStats)
{
const dtMeshHeader& header = *reinterpret_cast<dtMeshHeader*>(tileData.mValue.get());
consumer->updateStats(NavMeshTileConsumerStats{
.mPolyCount = header.polyCount,
});
}
ignore.mConsumer = nullptr;
}
catch (const std::exception& e)
@@ -2,6 +2,7 @@
#define OPENMW_COMPONENTS_DETOURNAVIGATOR_GENERATENAVMESHTILE_H
#include "agentbounds.hpp"
#include "preparednavmeshdata.hpp"
#include "recastmeshprovider.hpp"
#include "tileposition.hpp"
@@ -19,7 +20,6 @@ namespace DetourNavigator
class RecastMesh;
struct NavMeshTileConsumer;
struct OffMeshConnection;
struct PreparedNavMeshData;
struct Settings;
struct MeshSource;
@@ -27,6 +27,12 @@ namespace DetourNavigator
{
std::int64_t mTileId;
std::int64_t mVersion;
std::unique_ptr<PreparedNavMeshData> mData;
};
struct NavMeshTileConsumerStats
{
int mPolyCount;
};
struct NavMeshTileConsumer
@@ -52,6 +58,8 @@ namespace DetourNavigator
= 0;
virtual void cancel(std::string_view reason) = 0;
virtual void updateStats(const NavMeshTileConsumerStats& value) = 0;
};
class GenerateNavMeshTile final : public SceneUtil::WorkItem
@@ -59,7 +67,7 @@ namespace DetourNavigator
public:
GenerateNavMeshTile(ESM::RefId worldspace, const TilePosition& tilePosition,
std::weak_ptr<const RecastMeshProvider> recastMeshProvider, const AgentBounds& agentBounds,
const Settings& settings, std::weak_ptr<NavMeshTileConsumer> consumer);
const Settings& settings, bool collectStats, std::weak_ptr<NavMeshTileConsumer> consumer);
void doWork() final;
@@ -69,6 +77,7 @@ namespace DetourNavigator
const std::weak_ptr<const RecastMeshProvider> mRecastMeshProvider;
const AgentBounds mAgentBounds;
const Settings& mSettings;
const bool mCollectStats;
const std::weak_ptr<NavMeshTileConsumer> mConsumer;
inline void impl() noexcept;
+6 -7
View File
@@ -33,7 +33,7 @@ namespace DetourNavigator
float mHeight;
};
std::vector<float> getOffMeshVerts(const std::vector<OffMeshConnection>& connections)
std::vector<float> getOffMeshVerts(std::span<const OffMeshConnection> connections)
{
std::vector<float> result;
@@ -72,7 +72,7 @@ namespace DetourNavigator
return Flag_none;
}
std::vector<unsigned char> getOffMeshConAreas(const std::vector<OffMeshConnection>& connections)
std::vector<unsigned char> getOffMeshConAreas(std::span<const OffMeshConnection> connections)
{
std::vector<unsigned char> result;
result.reserve(connections.size());
@@ -81,7 +81,7 @@ namespace DetourNavigator
return result;
}
std::vector<unsigned short> getOffMeshFlags(const std::vector<OffMeshConnection>& connections)
std::vector<unsigned short> getOffMeshFlags(std::span<const OffMeshConnection> connections)
{
std::vector<unsigned short> result;
result.reserve(connections.size());
@@ -550,8 +550,8 @@ namespace DetourNavigator
}
NavMeshData makeNavMeshTileData(const PreparedNavMeshData& data,
const std::vector<OffMeshConnection>& offMeshConnections, const AgentBounds& agentBounds,
const TilePosition& tile, const RecastSettings& settings)
std::span<const OffMeshConnection> offMeshConnections, const AgentBounds& agentBounds, const TilePosition& tile,
const RecastSettings& settings)
{
const auto offMeshConVerts = getOffMeshVerts(offMeshConnections);
const std::vector<float> offMeshConRad(offMeshConnections.size(), getRadius(settings, agentBounds));
@@ -594,9 +594,8 @@ namespace DetourNavigator
unsigned char* navMeshData;
int navMeshDataSize;
const auto navMeshDataCreated = dtCreateNavMeshData(&params, &navMeshData, &navMeshDataSize);
if (!navMeshDataCreated)
if (!dtCreateNavMeshData(&params, &navMeshData, &navMeshDataSize))
throw NavigatorException("Failed to create navmesh tile data");
return NavMeshData(navMeshData, navMeshDataSize);
+3 -3
View File
@@ -7,7 +7,7 @@
#include <components/esm/refid.hpp>
#include <memory>
#include <vector>
#include <span>
class dtNavMesh;
struct rcConfig;
@@ -47,8 +47,8 @@ namespace DetourNavigator
const TilePosition& tilePosition, const AgentBounds& agentBounds, const RecastSettings& settings);
NavMeshData makeNavMeshTileData(const PreparedNavMeshData& data,
const std::vector<OffMeshConnection>& offMeshConnections, const AgentBounds& agentBounds,
const TilePosition& tile, const RecastSettings& settings);
std::span<const OffMeshConnection> offMeshConnections, const AgentBounds& agentBounds, const TilePosition& tile,
const RecastSettings& settings);
void initEmptyNavMesh(const Settings& settings, dtNavMesh& navMesh);
+5
View File
@@ -107,6 +107,11 @@ namespace DetourNavigator
result.mWriteToNavMeshDb = ::Settings::navigator().mWriteToNavmeshdb;
result.mMaxDbFileSize = ::Settings::navigator().mMaxNavmeshdbFileSize;
if (result.mMaxTilesNumber < ::Settings::navigator().mMaxTilesNumber.get())
Log(Debug::Warning)
<< "Navigator max tiles number is adjusted due to limitation on number of bits for tile identifier: "
<< result.mMaxTilesNumber;
return result;
}
}