Skip to content

Commit

Permalink
Add Scene::SetLegacyAutoGpuFlush
Browse files Browse the repository at this point in the history
Affects gazebosim#323

Signed-off-by: Matias N. Goldberg <dark_sylinc@yahoo.com.ar>
  • Loading branch information
darksylinc committed Jul 10, 2021
1 parent f7c630e commit c22002f
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 1 deletion.
25 changes: 25 additions & 0 deletions include/ignition/rendering/Scene.hh
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,31 @@ namespace ignition
/// so flushing once per probe may make better sense.
public: virtual void PostRenderGpuFlush() = 0;

/// \brief Old projects migrating to newer versions will
/// leak memory if they don't call PostRenderGpuFlush
///
/// Settings this value to true forces Gazebo to flush commands for
/// every camera.
///
/// This is much slower but will ease porting, specially
/// if it's not easy to adapt your code to call PostRenderGpuFlush
/// at regular intervals for some reason
///
/// New projects should set this value to false
///
/// \remarks Not all rendering engines care about this.
/// ogre2 plugin does.
///
/// \param[in] _autoFlush True for old projects who can't or don't know
/// when to call PostRenderGpuFlush and prefer to penalize rendering
/// performance
public: virtual void SetLegacyAutoGpuFlush( bool _autoFlush ) = 0;

/// \brief Gets the value of SetLegacyAutoGpuFlush
/// \return True if Gazebo is using the old method.
/// Returns always true for plugins that ignore SetLegacyAutoGpuFlush
public: virtual bool GetLegacyAutoGpuFlush() const = 0;

/// \brief Remove and destroy all objects from the scene graph. This does
/// not completely destroy scene resources, so new objects can be created
/// and added to the scene afterwards.
Expand Down
6 changes: 6 additions & 0 deletions include/ignition/rendering/base/BaseScene.hh
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,12 @@ namespace ignition
// Documentation inherited.
public: virtual void PostRenderGpuFlush() override;

// Documentation inherited.
public: virtual void SetLegacyAutoGpuFlush( bool _autoFlush ) override;

// Documentation inherited.
public: virtual bool GetLegacyAutoGpuFlush() const override;

protected: virtual unsigned int CreateObjectId();

protected: virtual std::string CreateObjectName(unsigned int _id,
Expand Down
6 changes: 6 additions & 0 deletions ogre2/include/ignition/rendering/ogre2/Ogre2Scene.hh
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ namespace ignition
// Documentation inherited
public: virtual void PostRenderGpuFlush() override;

// Documentation inherited.
public: virtual void SetLegacyAutoGpuFlush( bool _autoFlush ) override;

// Documentation inherited.
public: virtual bool GetLegacyAutoGpuFlush() const override;

/// \brief Get a pointer to the ogre scene manager
/// \return Pointer to the ogre scene manager
public: virtual Ogre::SceneManager *OgreSceneManager() const;
Expand Down
7 changes: 7 additions & 0 deletions ogre2/src/Ogre2DepthCamera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,10 @@ void Ogre2DepthCamera::CreateWorkspaceInstance()
//////////////////////////////////////////////////
void Ogre2DepthCamera::Render()
{
const bool legacyAutoGpuFlush = this->scene->GetLegacyAutoGpuFlush();
if (legacyAutoGpuFlush)
this->scene->OgreSceneManager()->updateSceneGraph();

// update the compositors
this->dataPtr->ogreCompositorWorkspace->_validateFinalTarget();
//engine->OgreRoot()->getRenderSystem()->_beginFrameOnce();
Expand All @@ -950,6 +954,9 @@ void Ogre2DepthCamera::Render()
Ogre::vector<Ogre::RenderTarget*>::type swappedTargets;
swappedTargets.reserve( 2u );
this->dataPtr->ogreCompositorWorkspace->_swapFinalTarget( swappedTargets );

if (legacyAutoGpuFlush)
this->scene->PostRenderGpuFlush();
}

//////////////////////////////////////////////////
Expand Down
7 changes: 7 additions & 0 deletions ogre2/src/Ogre2GpuRays.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,10 @@ void Ogre2GpuRays::UpdateRenderTarget1stPass()
/////////////////////////////////////////////////
void Ogre2GpuRays::UpdateRenderTarget2ndPass()
{
const bool legacyAutoGpuFlush = this->scene->GetLegacyAutoGpuFlush();
if (legacyAutoGpuFlush)
this->scene->OgreSceneManager()->updateSceneGraph();

this->dataPtr->ogreCompositorWorkspace2nd->_validateFinalTarget();
//engine->OgreRoot()->getRenderSystem()->_beginFrameOnce();
this->dataPtr->ogreCompositorWorkspace2nd->_beginUpdate(false);
Expand All @@ -1100,6 +1104,9 @@ void Ogre2GpuRays::UpdateRenderTarget2ndPass()
Ogre::vector<Ogre::RenderTarget*>::type swappedTargets;
swappedTargets.reserve( 2u );
this->dataPtr->ogreCompositorWorkspace2nd->_swapFinalTarget( swappedTargets );

if (legacyAutoGpuFlush)
this->scene->PostRenderGpuFlush();
}

//////////////////////////////////////////////////
Expand Down
7 changes: 7 additions & 0 deletions ogre2/src/Ogre2RenderTarget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,10 @@ void Ogre2RenderTarget::PostRender()
//////////////////////////////////////////////////
void Ogre2RenderTarget::Render()
{
const bool legacyAutoGpuFlush = this->scene->GetLegacyAutoGpuFlush();
if (legacyAutoGpuFlush)
this->scene->OgreSceneManager()->updateSceneGraph();

this->ogreCompositorWorkspace->_validateFinalTarget();
//engine->OgreRoot()->getRenderSystem()->_beginFrameOnce();
this->ogreCompositorWorkspace->_beginUpdate(false);
Expand All @@ -434,6 +438,9 @@ void Ogre2RenderTarget::Render()
Ogre::vector<Ogre::RenderTarget*>::type swappedTargets;
swappedTargets.reserve( 2u );
this->ogreCompositorWorkspace->_swapFinalTarget( swappedTargets );

if (legacyAutoGpuFlush)
this->scene->PostRenderGpuFlush();
}

//////////////////////////////////////////////////
Expand Down
15 changes: 15 additions & 0 deletions ogre2/src/Ogre2Scene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class ignition::rendering::Ogre2ScenePrivate
/// \brief Flag to indicate if sky is enabled or not
public: bool skyEnabled = false;

/// \brief Flag to indicate if we should flush GPU very often (per camera)
public: bool legacyAutoGpuFlush = false;

/// \brief Name of shadow compositor node
public: const std::string kShadowNodeName = "PbsMaterialsShadowNode";
};
Expand Down Expand Up @@ -203,6 +206,18 @@ void Ogre2Scene::PostRenderGpuFlush()
}
}

//////////////////////////////////////////////////
void Ogre2Scene::SetLegacyAutoGpuFlush(bool _autoFlush)
{
this->dataPtr->legacyAutoGpuFlush = _autoFlush;
}

//////////////////////////////////////////////////
bool Ogre2Scene::GetLegacyAutoGpuFlush() const
{
return this->dataPtr->legacyAutoGpuFlush;
}

//////////////////////////////////////////////////
void Ogre2Scene::Clear()
{
Expand Down
8 changes: 8 additions & 0 deletions ogre2/src/Ogre2SelectionBuffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ void Ogre2SelectionBuffer::Update()

this->dataPtr->materialSwitcher->Reset();

const bool legacyAutoGpuFlush =
this->dataPtr->scene->GetLegacyAutoGpuFlush();
if (legacyAutoGpuFlush)
this->dataPtr->scene->OgreSceneManager()->updateSceneGraph();

// manual update
this->dataPtr->scene->OgreSceneManager()->updateSceneGraph();
this->dataPtr->ogreCompositorWorkspace->_validateFinalTarget();
Expand All @@ -139,6 +144,9 @@ void Ogre2SelectionBuffer::Update()
swappedTargets.reserve( 2u );
this->dataPtr->ogreCompositorWorkspace->_swapFinalTarget( swappedTargets );

if (legacyAutoGpuFlush)
this->dataPtr->scene->PostRenderGpuFlush();

this->dataPtr->renderTexture->copyContentsToMemory(*this->dataPtr->pixelBox,
Ogre::RenderTarget::FB_FRONT);
}
Expand Down
8 changes: 7 additions & 1 deletion ogre2/src/Ogre2ThermalCamera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,10 @@ void Ogre2ThermalCamera::CreateThermalTexture()
void Ogre2ThermalCamera::Render()
{
// update the compositors
this->scene->OgreSceneManager()->updateSceneGraph();
const bool legacyAutoGpuFlush = this->scene->GetLegacyAutoGpuFlush();
if (legacyAutoGpuFlush)
this->scene->OgreSceneManager()->updateSceneGraph();

this->dataPtr->ogreCompositorWorkspace->_validateFinalTarget();
//engine->OgreRoot()->getRenderSystem()->_beginFrameOnce();
this->dataPtr->ogreCompositorWorkspace->_beginUpdate(false);
Expand All @@ -837,6 +840,9 @@ void Ogre2ThermalCamera::Render()
Ogre::vector<Ogre::RenderTarget*>::type swappedTargets;
swappedTargets.reserve( 2u );
this->dataPtr->ogreCompositorWorkspace->_swapFinalTarget( swappedTargets );

if (legacyAutoGpuFlush)
this->scene->PostRenderGpuFlush();
}

//////////////////////////////////////////////////
Expand Down
11 changes: 11 additions & 0 deletions src/base/BaseScene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,17 @@ void BaseScene::PostRenderGpuFlush()
{
}

//////////////////////////////////////////////////
void BaseScene::SetLegacyAutoGpuFlush(bool _autoFlush)
{
}

//////////////////////////////////////////////////
bool BaseScene::GetLegacyAutoGpuFlush() const
{
return true;
}

//////////////////////////////////////////////////
void BaseScene::Clear()
{
Expand Down

0 comments on commit c22002f

Please sign in to comment.