Skip to content

Commit ce950dc

Browse files
committed
initial caching of repeated data
1 parent b161190 commit ce950dc

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

apps/openmw/mwrender/objectpaging.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,26 @@ namespace MWRender
111111
return {};
112112
}
113113
}
114+
115+
// Retrieve pre-computed template radius from node user data, or compute fallback
116+
// to ensure robustness in case radius wasn't pre-computed
117+
float getTemplateRadius(const osg::Node* node)
118+
{
119+
if (!node)
120+
return 0.0f;
121+
122+
const auto* userDataContainer = node->getUserDataContainer();
123+
if (!userDataContainer || userDataContainer->getNumUserObjects() == 0)
124+
return node->getBound().radius(); // Fallback: compute if not cached
125+
126+
// First user object should be the template radius FloatValueObject
127+
if (const auto* floatValue = dynamic_cast<const osg::FloatValueObject*>(
128+
userDataContainer->getUserObject(0)))
129+
return floatValue->getValue();
130+
131+
// Fallback: compute radius if not cached as expected
132+
return node->getBound().radius();
133+
}
114134
}
115135

116136
osg::ref_ptr<osg::Node> ObjectPaging::getChunk(float size, const osg::Vec2f& center, unsigned char /*lod*/,
@@ -794,7 +814,9 @@ namespace MWRender
794814
continue;
795815
}
796816

797-
const float radius2 = cnode->getBound().radius2() * ref.mScale * ref.mScale;
817+
// Use pre-computed template radius to avoid expensive getBound() traversal
818+
const float templateRadius = getTemplateRadius(cnode);
819+
const float radius2 = templateRadius * templateRadius * ref.mScale * ref.mScale;
798820
if (radius2 < dSqr * minSize * minSize && !activeGrid)
799821
{
800822
std::lock_guard<std::mutex> lock(mSizeCacheMutex);
@@ -844,8 +866,10 @@ namespace MWRender
844866
{
845867
const PagedCellRef& ref = *refPtr;
846868

869+
// Use pre-computed template radius for merge decision culling
870+
const float templateRadius = getTemplateRadius(cnode);
847871
if (!activeGrid && minSizeMerged != minSize
848-
&& cnode->getBound().radius2() * ref.mScale * ref.mScale
872+
&& templateRadius * templateRadius * ref.mScale * ref.mScale
849873
< (viewPoint - ref.mPosition).length2() * minSizeMerged * minSizeMerged)
850874
continue;
851875

components/resource/scenemanager.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <osg/Group>
1010
#include <osg/Node>
1111
#include <osg/UserDataContainer>
12+
#include <osg/ValueObject>
1213

1314
#include <osgAnimation/BasicAnimationManager>
1415
#include <osgAnimation/Bone>
@@ -1017,6 +1018,13 @@ namespace Resource
10171018
else
10181019
loaded->getBound();
10191020

1021+
// Pre-compute and cache the bounding radius as user data to avoid redundant
1022+
// getBound() calls during object paging visibility checks
1023+
const float radius = loaded->getBound().radius();
1024+
// Store as first user object for quick retrieval
1025+
loaded->getOrCreateUserDataContainer()->addUserObject(
1026+
new osg::FloatValueObject("template_radius", radius));
1027+
10201028
mCache->addEntryToObjectCache(path.value(), loaded);
10211029
return loaded;
10221030
}

0 commit comments

Comments
 (0)