From 442a0e84345bd30a332ba257d778064d213c4cc7 Mon Sep 17 00:00:00 2001 From: Bo Svensson <90132211+bosvensson1@users.noreply.github.com> Date: Tue, 5 Oct 2021 14:00:30 +0000 Subject: [PATCH] avoids two transforms in sky.cpp (#3150) As we discovered in #3148, `Transform` nodes and their low level equivalence `pushModelViewMatrix` are somewhat costly involving a `Matrix::invert` operation per frame. With this PR we avoid one `Transform` node for sun flashes and avoid another `pushModelViewMatrix` call in case the sun is fully visible. --- apps/openmw/mwrender/sky.cpp | 37 +++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/apps/openmw/mwrender/sky.cpp b/apps/openmw/mwrender/sky.cpp index c2ed62ef87..dd62d6678b 100644 --- a/apps/openmw/mwrender/sky.cpp +++ b/apps/openmw/mwrender/sky.cpp @@ -81,15 +81,15 @@ namespace return mat; } - osg::ref_ptr createTexturedQuad(int numUvSets=1) + osg::ref_ptr createTexturedQuad(int numUvSets=1, float scale=1.f) { osg::ref_ptr geom = new osg::Geometry; osg::ref_ptr verts = new osg::Vec3Array; - verts->push_back(osg::Vec3f(-0.5, -0.5, 0)); - verts->push_back(osg::Vec3f(-0.5, 0.5, 0)); - verts->push_back(osg::Vec3f(0.5, 0.5, 0)); - verts->push_back(osg::Vec3f(0.5, -0.5, 0)); + verts->push_back(osg::Vec3f(-0.5*scale, -0.5*scale, 0)); + verts->push_back(osg::Vec3f(-0.5*scale, 0.5*scale, 0)); + verts->push_back(osg::Vec3f(0.5*scale, 0.5*scale, 0)); + verts->push_back(osg::Vec3f(0.5*scale, -0.5*scale, 0)); geom->setVertexArray(verts); @@ -621,14 +621,13 @@ private: tex->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP_TO_EDGE); tex->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP_TO_EDGE); - osg::ref_ptr transform (new osg::PositionAttitudeTransform); + osg::ref_ptr group (new osg::Group); + + mTransform->addChild(group); + const float scale = 2.6f; - transform->setScale(osg::Vec3f(scale,scale,scale)); - - mTransform->addChild(transform); - - osg::ref_ptr geom = createTexturedQuad(); - transform->addChild(geom); + osg::ref_ptr geom = createTexturedQuad(1, scale); + group->addChild(geom); osg::StateSet* stateset = geom->getOrCreateStateSet(); @@ -637,7 +636,7 @@ private: stateset->setRenderBinDetails(RenderBin_SunGlare, "RenderBin"); stateset->setNestRenderBins(false); - mSunFlashNode = transform; + mSunFlashNode = group; mSunFlashCallback = new SunFlashCallback(mOcclusionQueryVisiblePixels, mOcclusionQueryTotalPixels); mSunFlashNode->addCullCallback(mSunFlashCallback); @@ -785,9 +784,11 @@ private: stateset = new osg::StateSet; stateset->setAttributeAndModes(mat, osg::StateAttribute::ON|osg::StateAttribute::OVERRIDE); } - - const float threshold = 0.6; - visibleRatio = visibleRatio * (1.f - threshold) + threshold; + else if (visibleRatio < 1.f) + { + const float threshold = 0.6; + visibleRatio = visibleRatio * (1.f - threshold) + threshold; + } } float scale = visibleRatio; @@ -797,11 +798,13 @@ private: // no traverse return; } + else if (scale == 1.f) + traverse(node, cv); else { osg::Matrix modelView = *cv->getModelViewMatrix(); - modelView.preMultScale(osg::Vec3f(visibleRatio, visibleRatio, visibleRatio)); + modelView.preMultScale(osg::Vec3f(scale, scale, scale)); if (stateset) cv->pushStateSet(stateset);