Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Ogre2DepthCamera performance by skipping color target passes when there are no connections #965

Merged
merged 5 commits into from
Jan 26, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 74 additions & 7 deletions ogre2/src/Ogre2DepthCamera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include "gz/rendering/ogre2/Ogre2Conversions.hh"
#include "gz/rendering/ogre2/Ogre2DepthCamera.hh"
#include "gz/rendering/ogre2/Ogre2GaussianNoisePass.hh"
#include "gz/rendering/ogre2/Ogre2Includes.hh"
#include "gz/rendering/ogre2/Ogre2ParticleEmitter.hh"
#include "gz/rendering/ogre2/Ogre2RenderEngine.hh"
#include "gz/rendering/ogre2/Ogre2RenderTarget.hh"
Expand All @@ -40,6 +39,21 @@

#include "Ogre2ParticleNoiseListener.hh"

#ifdef _MSC_VER
#pragma warning(push, 0)
#endif
#include <Compositor/OgreCompositorManager2.h>
#include <Compositor/OgreCompositorWorkspace.h>
#include <Compositor/Pass/PassClear/OgreCompositorPassClearDef.h>
#include <Compositor/Pass/PassQuad/OgreCompositorPassQuadDef.h>
#include <Compositor/Pass/PassScene/OgreCompositorPassSceneDef.h>
#include <OgreRoot.h>
#include <OgreSceneManager.h>
#include <OgreTechnique.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif

namespace gz
{
namespace rendering
Expand Down Expand Up @@ -147,6 +161,17 @@

/// \brief Name of shadow compositor node
public: const std::string kShadowNodeName = "PbsMaterialsShadowNode";

/// \brief Execution mask for this workspace
/// If RGB point color data are requested, the execution mask of the color
/// target will be updated to match the workspace's execution mask so that
/// these passes are executed, otherwise they will be skipped for performance
/// improvement.
public: const uint8_t kDepthExecutionMask = 0xEF;
iche033 marked this conversation as resolved.
Show resolved Hide resolved

/// \brief Pointer to the color target in the workspace
public: Ogre::CompositorTargetDef *colorTarget{nullptr};

};

using namespace gz;
Expand Down Expand Up @@ -314,6 +339,7 @@
{
ogreCompMgr->removeWorkspace(
this->dataPtr->ogreCompositorWorkspace);
this->dataPtr->colorTarget = nullptr;
}

if (this->dataPtr->depthMaterial)
Expand Down Expand Up @@ -703,12 +729,17 @@
baseNodeDef->setNumTargetPass(4);
Ogre::CompositorTargetDef *colorTargetDef =
baseNodeDef->addTargetPass("colorTexture");

if (validBackground)
colorTargetDef->setNumPasses(3);
colorTargetDef->setNumPasses(4);

Check warning on line 733 in ogre2/src/Ogre2DepthCamera.cc

View check run for this annotation

Codecov / codecov/patch

ogre2/src/Ogre2DepthCamera.cc#L733

Added line #L733 was not covered by tests
else
colorTargetDef->setNumPasses(2);
colorTargetDef->setNumPasses(3);
{
// clear pass
Ogre::CompositorPassSceneDef *passClear =
static_cast<Ogre::CompositorPassSceneDef *>(
colorTargetDef->addPass(Ogre::PASS_CLEAR));
passClear->mExecutionMask = this->dataPtr->kDepthExecutionMask;

// scene pass - opaque
{
Ogre::CompositorPassSceneDef *passScene =
Expand All @@ -732,6 +763,7 @@
Ogre2Conversions::Convert(this->Scene()->BackgroundColor()));

}
passScene->mExecutionMask = ~this->dataPtr->kDepthExecutionMask;
}

// render background, e.g. sky, after opaque stuff
Expand All @@ -745,6 +777,7 @@
+ this->Name();
passQuad->mFrustumCorners =
Ogre::CompositorPassQuadDef::CAMERA_DIRECTION;
passQuad->mExecutionMask = ~this->dataPtr->kDepthExecutionMask;

Check warning on line 780 in ogre2/src/Ogre2DepthCamera.cc

View check run for this annotation

Codecov / codecov/patch

ogre2/src/Ogre2DepthCamera.cc#L780

Added line #L780 was not covered by tests
}

// scene pass - transparent stuff
Expand All @@ -757,6 +790,7 @@
// Although this may be just fine
passScene->mShadowNode = this->dataPtr->kShadowNodeName;
passScene->mFirstRQ = 2u;
passScene->mExecutionMask = ~this->dataPtr->kDepthExecutionMask;
}
}

Expand All @@ -773,9 +807,11 @@
this->FarClipPlane(),
this->FarClipPlane(),
this->FarClipPlane()));
// depth texute does not contain particles
// depth texture does not contain particles
passScene->setVisibilityMask(
GZ_VISIBILITY_ALL & ~Ogre2ParticleEmitter::kParticleVisibilityFlags);
passScene->mEnableForwardPlus = false;
passScene->setLightVisibilityMask(0x0);
}

Ogre::CompositorTargetDef *particleTargetDef =
Expand All @@ -790,6 +826,8 @@
passScene->setAllClearColours(Ogre::ColourValue::Black);
passScene->setVisibilityMask(
Ogre2ParticleEmitter::kParticleVisibilityFlags);
passScene->mEnableForwardPlus = false;
passScene->setLightVisibilityMask(0x0);
}

// rt0 target - converts depth to xyz
Expand Down Expand Up @@ -919,7 +957,7 @@
Ogre::GpuResidency::Resident);
}

CreateWorkspaceInstance();
this->CreateWorkspaceInstance();
}

//////////////////////////////////////////////////
Expand All @@ -941,7 +979,8 @@
externalTargets,
this->ogreCamera,
this->dataPtr->ogreCompositorWorkspaceDef,
false);
false, -1, 0, 0, Ogre::Vector4::ZERO, 0x00,
this->dataPtr->kDepthExecutionMask);

this->dataPtr->ogreCompositorWorkspace->addListener(
engine->TerraWorkspaceListener());
Expand Down Expand Up @@ -1004,6 +1043,34 @@
if (!this->dataPtr->ogreCompositorWorkspace)
this->CreateWorkspaceInstance();

if (!this->dataPtr->colorTarget)
{
auto engine = Ogre2RenderEngine::Instance();
auto ogreRoot = engine->OgreRoot();
Ogre::CompositorManager2 *ogreCompMgr = ogreRoot->getCompositorManager2();
Ogre::CompositorNodeDef *nodeDef =
ogreCompMgr->getNodeDefinitionNonConst(
this->dataPtr->ogreCompositorBaseNodeDef);
this->dataPtr->colorTarget = nodeDef->getTargetPass(0);
}

Ogre::CompositorPassDefVec &colorPasses =
this->dataPtr->colorTarget->getCompositorPassesNonConst();
GZ_ASSERT(colorPasses.size() > 2u,
"Ogre2DepthCamera color target should contain more than 2 passes");
GZ_ASSERT(colorPasses[0]->getType() == Ogre::PASS_CLEAR,
"Ogre2DepthCamera color target should start with a clear pass");
colorPasses[0]->mExecutionMask =
(this->dataPtr->newRgbPointCloud.ConnectionCount() > 0u) ?
~this->dataPtr->kDepthExecutionMask :this->dataPtr->kDepthExecutionMask;
for (unsigned int i = 1; i < colorPasses.size(); ++i)
{
colorPasses[i]->mExecutionMask =
(this->dataPtr->newRgbPointCloud.ConnectionCount() > 0u) ?
this->dataPtr->kDepthExecutionMask :
~this->dataPtr->kDepthExecutionMask;
}

// update depth camera render passes
Ogre2RenderTarget::UpdateRenderPassChain(
this->dataPtr->ogreCompositorWorkspace,
Expand Down