From 33ef7fc8ca7e97c9a124260fbdf72db981e26d8d Mon Sep 17 00:00:00 2001 From: elsid Date: Sat, 20 Apr 2024 01:51:39 +0200 Subject: [PATCH 1/4] Make ClearCacheFunctor a class and rename to clarify the purpose --- apps/openmw/mwrender/objectpaging.cpp | 92 +++++++++++++++------------ 1 file changed, 52 insertions(+), 40 deletions(-) diff --git a/apps/openmw/mwrender/objectpaging.cpp b/apps/openmw/mwrender/objectpaging.cpp index a720164e03..6d79c13593 100644 --- a/apps/openmw/mwrender/objectpaging.cpp +++ b/apps/openmw/mwrender/objectpaging.cpp @@ -875,35 +875,54 @@ namespace MWRender return Mask_Static; } - struct ClearCacheFunctor + namespace { - void operator()(MWRender::ChunkId id, osg::Object* obj) + class CollectIntersecting { - if (intersects(id, mPosition)) - mToClear.insert(id); - } - bool intersects(ChunkId id, osg::Vec3f pos) - { - if (mActiveGridOnly && !std::get<2>(id)) - return false; - pos /= getCellSize(mWorldspace); - clampToCell(pos); - osg::Vec2f center = std::get<0>(id); - float halfSize = std::get<1>(id) / 2; - return pos.x() >= center.x() - halfSize && pos.y() >= center.y() - halfSize - && pos.x() <= center.x() + halfSize && pos.y() <= center.y() + halfSize; - } - void clampToCell(osg::Vec3f& cellPos) - { - cellPos.x() = std::clamp(cellPos.x(), mCell.x(), mCell.x() + 1); - cellPos.y() = std::clamp(cellPos.y(), mCell.y(), mCell.y() + 1); - } - osg::Vec3f mPosition; - osg::Vec2i mCell; - ESM::RefId mWorldspace; - std::set mToClear; - bool mActiveGridOnly = false; - }; + public: + explicit CollectIntersecting( + bool activeGridOnly, const osg::Vec3f& position, const osg::Vec2i& cell, ESM::RefId worldspace) + : mActiveGridOnly(activeGridOnly) + , mPosition(position) + , mCell(cell) + , mWorldspace(worldspace) + { + } + + void operator()(const ChunkId& id, osg::Object* /*obj*/) + { + if (mActiveGridOnly && !std::get<2>(id)) + return; + if (intersects(id, mPosition)) + mCollected.insert(id); + } + + const std::set& getCollected() const { return mCollected; } + + private: + bool intersects(ChunkId id, osg::Vec3f pos) const + { + pos /= getCellSize(mWorldspace); + clampToCell(pos); + osg::Vec2f center = std::get<0>(id); + float halfSize = std::get<1>(id) / 2; + return pos.x() >= center.x() - halfSize && pos.y() >= center.y() - halfSize + && pos.x() <= center.x() + halfSize && pos.y() <= center.y() + halfSize; + } + + void clampToCell(osg::Vec3f& cellPos) const + { + cellPos.x() = std::clamp(cellPos.x(), mCell.x(), mCell.x() + 1); + cellPos.y() = std::clamp(cellPos.y(), mCell.y(), mCell.y() + 1); + } + + bool mActiveGridOnly; + osg::Vec3f mPosition; + osg::Vec2i mCell; + ESM::RefId mWorldspace; + std::set mCollected; + }; + } bool ObjectPaging::enableObject( int type, ESM::RefNum refnum, const osg::Vec3f& pos, const osg::Vec2i& cell, bool enabled) @@ -921,14 +940,11 @@ namespace MWRender return false; } - ClearCacheFunctor ccf; - ccf.mPosition = pos; - ccf.mCell = cell; - ccf.mWorldspace = mWorldspace; + CollectIntersecting ccf(false, pos, cell, mWorldspace); mCache->call(ccf); - if (ccf.mToClear.empty()) + if (ccf.getCollected().empty()) return false; - for (const auto& chunk : ccf.mToClear) + for (const ChunkId& chunk : ccf.getCollected()) mCache->removeFromObjectCache(chunk); return true; } @@ -946,15 +962,11 @@ namespace MWRender return false; } - ClearCacheFunctor ccf; - ccf.mPosition = pos; - ccf.mCell = cell; - ccf.mActiveGridOnly = true; - ccf.mWorldspace = mWorldspace; + CollectIntersecting ccf(true, pos, cell, mWorldspace); mCache->call(ccf); - if (ccf.mToClear.empty()) + if (ccf.getCollected().empty()) return false; - for (const auto& chunk : ccf.mToClear) + for (const ChunkId& chunk : ccf.getCollected()) mCache->removeFromObjectCache(chunk); return true; } From b64069156df49e2bcbbb4ed37fbda7c84b5063d7 Mon Sep 17 00:00:00 2001 From: elsid Date: Sun, 26 May 2024 19:57:39 +0200 Subject: [PATCH 2/4] Convert position on construction --- apps/openmw/mwrender/objectpaging.cpp | 34 +++++++++++---------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/apps/openmw/mwrender/objectpaging.cpp b/apps/openmw/mwrender/objectpaging.cpp index 6d79c13593..839cedc181 100644 --- a/apps/openmw/mwrender/objectpaging.cpp +++ b/apps/openmw/mwrender/objectpaging.cpp @@ -877,15 +877,19 @@ namespace MWRender namespace { + osg::Vec2f clampToCell(const osg::Vec3f& cellPos, const osg::Vec2i& cell) + { + return osg::Vec2f(std::clamp(cellPos.x(), cell.x(), cell.x() + 1), + std::clamp(cellPos.y(), cell.y(), cell.y() + 1)); + } + class CollectIntersecting { public: explicit CollectIntersecting( bool activeGridOnly, const osg::Vec3f& position, const osg::Vec2i& cell, ESM::RefId worldspace) : mActiveGridOnly(activeGridOnly) - , mPosition(position) - , mCell(cell) - , mWorldspace(worldspace) + , mPosition(clampToCell(position / getCellSize(worldspace), cell)) { } @@ -893,33 +897,23 @@ namespace MWRender { if (mActiveGridOnly && !std::get<2>(id)) return; - if (intersects(id, mPosition)) + if (intersects(id)) mCollected.insert(id); } const std::set& getCollected() const { return mCollected; } private: - bool intersects(ChunkId id, osg::Vec3f pos) const + bool intersects(ChunkId id) const { - pos /= getCellSize(mWorldspace); - clampToCell(pos); - osg::Vec2f center = std::get<0>(id); - float halfSize = std::get<1>(id) / 2; - return pos.x() >= center.x() - halfSize && pos.y() >= center.y() - halfSize - && pos.x() <= center.x() + halfSize && pos.y() <= center.y() + halfSize; - } - - void clampToCell(osg::Vec3f& cellPos) const - { - cellPos.x() = std::clamp(cellPos.x(), mCell.x(), mCell.x() + 1); - cellPos.y() = std::clamp(cellPos.y(), mCell.y(), mCell.y() + 1); + const osg::Vec2f center = std::get<0>(id); + const float halfSize = std::get<1>(id) / 2; + return mPosition.x() >= center.x() - halfSize && mPosition.y() >= center.y() - halfSize + && mPosition.x() <= center.x() + halfSize && mPosition.y() <= center.y() + halfSize; } bool mActiveGridOnly; - osg::Vec3f mPosition; - osg::Vec2i mCell; - ESM::RefId mWorldspace; + osg::Vec2f mPosition; std::set mCollected; }; } From b5a75ba96ccada00bcf05500d0fb805b3fe77abb Mon Sep 17 00:00:00 2001 From: elsid Date: Sun, 26 May 2024 20:06:03 +0200 Subject: [PATCH 3/4] Use std::vector to collect ChunkIds to be removed Cache does not have duplicated entries. --- apps/openmw/mwrender/objectpaging.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/openmw/mwrender/objectpaging.cpp b/apps/openmw/mwrender/objectpaging.cpp index 839cedc181..392d444e65 100644 --- a/apps/openmw/mwrender/objectpaging.cpp +++ b/apps/openmw/mwrender/objectpaging.cpp @@ -898,10 +898,10 @@ namespace MWRender if (mActiveGridOnly && !std::get<2>(id)) return; if (intersects(id)) - mCollected.insert(id); + mCollected.push_back(id); } - const std::set& getCollected() const { return mCollected; } + const std::vector& getCollected() const { return mCollected; } private: bool intersects(ChunkId id) const @@ -914,7 +914,7 @@ namespace MWRender bool mActiveGridOnly; osg::Vec2f mPosition; - std::set mCollected; + std::vector mCollected; }; } From 6218afa484d0bfbcf7b0b64ea6878b2a04a55058 Mon Sep 17 00:00:00 2001 From: elsid Date: Sun, 5 May 2024 12:55:22 +0200 Subject: [PATCH 4/4] Cleanup object paging includes --- apps/openmw/mwrender/objectpaging.cpp | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/apps/openmw/mwrender/objectpaging.cpp b/apps/openmw/mwrender/objectpaging.cpp index 392d444e65..ee52148801 100644 --- a/apps/openmw/mwrender/objectpaging.cpp +++ b/apps/openmw/mwrender/objectpaging.cpp @@ -9,6 +9,8 @@ #include #include #include +#include +#include #include #include @@ -16,24 +18,19 @@ #include #include #include - #include #include -#include -#include -#include -#include -#include - -#include -#include - #include +#include #include #include +#include +#include #include #include +#include #include +#include #include "apps/openmw/mwbase/environment.hpp" #include "apps/openmw/mwbase/world.hpp" @@ -41,8 +38,6 @@ #include "vismask.hpp" -#include - namespace MWRender {