Skip to content

Commit

Permalink
Properly recreate Rectangle2D(v2) on device lost
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenegff committed Nov 20, 2024
1 parent 77084da commit 759d2f8
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 21 deletions.
7 changes: 6 additions & 1 deletion Components/Atmosphere/include/OgreAtmosphereNpr.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ THE SOFTWARE.

#include "OgreAtmosphereComponent.h"
#include "OgreColourValue.h"
#include "OgreRenderSystem.h"
#include "OgreSharedPtr.h"
#include "OgreVector3.h"

Expand Down Expand Up @@ -59,7 +60,8 @@ namespace Ogre
A PBR solution is iterative and requires more resources.
*/
class _OgreAtmosphereExport AtmosphereNpr final : public AtmosphereComponent
class _OgreAtmosphereExport AtmosphereNpr final : public AtmosphereComponent,
public RenderSystem::Listener
{
public:
struct Preset
Expand Down Expand Up @@ -202,6 +204,9 @@ namespace Ogre
AtmosphereNpr( VaoManager *vaoManager );
~AtmosphereNpr() override;

/// @see RenderSystem::Listener
void eventOccurred( const String &eventName, const NameValuePairList *parameters ) override;

void setSky( Ogre::SceneManager *sceneManager, bool bEnabled );
void destroySky( Ogre::SceneManager *sceneManager );

Expand Down
26 changes: 21 additions & 5 deletions Components/Atmosphere/src/OgreAtmosphereNpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,14 @@ namespace Ogre
{
mHlmsBuffer = vaoManager->createConstBuffer( sizeof( AtmoSettingsGpu ), BT_DEFAULT, 0, false );
createMaterial();

RenderSystem::addSharedListener( this );
}
//-------------------------------------------------------------------------
AtmosphereNpr::~AtmosphereNpr()
{
RenderSystem::removeSharedListener( this );

std::map<Ogre::SceneManager *, Rectangle2D *>::const_iterator itor = mSkies.begin();
std::map<Ogre::SceneManager *, Rectangle2D *>::const_iterator endt = mSkies.end();

Expand All @@ -90,7 +94,7 @@ namespace Ogre
itor->first->_setAtmosphere( nullptr );

itor->second->detachFromParent();
OGRE_DELETE itor->second;
itor->first->destroyRectangle2D( itor->second );

++itor;
}
Expand All @@ -109,6 +113,20 @@ namespace Ogre
mHlmsBuffer = 0;
}
//-------------------------------------------------------------------------
void AtmosphereNpr::eventOccurred( const String &eventName, const NameValuePairList *parameters )
{
if( eventName == "DeviceLost" )
{
mVaoManager->destroyConstBuffer( mHlmsBuffer );
mHlmsBuffer = 0;
}
else if( eventName == "DeviceRestored" )
{
mHlmsBuffer =
mVaoManager->createConstBuffer( sizeof( AtmoSettingsGpu ), BT_DEFAULT, 0, false );
}
}
//-------------------------------------------------------------------------
void AtmosphereNpr::createMaterial()
{
OGRE_ASSERT_LOW( !mMaterial );
Expand Down Expand Up @@ -226,9 +244,7 @@ namespace Ogre
std::map<Ogre::SceneManager *, Rectangle2D *>::iterator itor = mSkies.find( sceneManager );
if( itor == mSkies.end() )
{
sky = OGRE_NEW Rectangle2D( Id::generateNewId<MovableObject>(),
&sceneManager->_getEntityMemoryManager( SCENE_STATIC ),
sceneManager );
sky = sceneManager->createRectangle2D( SCENE_STATIC );
// We can't use BT_DYNAMIC_* because the scene may be rendered from multiple cameras
// in the same frame, and dynamic supports only one set of values per frame
sky->initialize( BT_DEFAULT,
Expand Down Expand Up @@ -259,7 +275,7 @@ namespace Ogre
std::map<Ogre::SceneManager *, Rectangle2D *>::iterator itor = mSkies.find( sceneManager );
if( itor != mSkies.end() )
{
OGRE_DELETE itor->second;
sceneManager->destroyRectangle2D( itor->second );
mSkies.erase( itor );
}
}
Expand Down
5 changes: 5 additions & 0 deletions OgreMain/include/OgreRectangle2D2.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ namespace Ogre
Rectangle2D( IdType id, ObjectMemoryManager *objectMemoryManager, SceneManager *manager );
~Rectangle2D() override;

/** @copydoc MovableObject::_releaseManualHardwareResources */
void _releaseManualHardwareResources() override;
/** @copydoc MovableObject::_restoreManualHardwareResources */
void _restoreManualHardwareResources() override;

bool isQuad() const;
bool isStereo() const;
bool hasNormals() const;
Expand Down
14 changes: 6 additions & 8 deletions OgreMain/src/OgreRadialDensityMask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ namespace Ogre
{
memcpy( mRadius, radius, sizeof( mRadius ) );

mRectangle =
OGRE_NEW Rectangle2D( Id::generateNewId<MovableObject>(),
&sceneManager->_getEntityMemoryManager( SCENE_STATIC ), sceneManager );
mRectangle = sceneManager->createRectangle2D( SCENE_STATIC );

mRectangle->setHollowRectRadius( mRadius[0] );
mRectangle->setGeometry( mLeftEyeCenter, mRightEyeCenter );
Expand Down Expand Up @@ -142,7 +140,8 @@ namespace Ogre
MaterialPtr material = mRectangle->getMaterial();
MaterialManager::getSingleton().remove( material );

OGRE_DELETE mRectangle;
SceneManager *sceneManager = mRectangle->_getManager();
sceneManager->destroyRectangle2D( mRectangle );
mRectangle = 0;
}
//-------------------------------------------------------------------------
Expand Down Expand Up @@ -224,10 +223,9 @@ namespace Ogre
MaterialPtr material = mRectangle->getMaterial();
SceneManager *sceneManager = mRectangle->_getManager();

OGRE_DELETE mRectangle;
mRectangle = OGRE_NEW Rectangle2D( Id::generateNewId<MovableObject>(),
&sceneManager->_getEntityMemoryManager( SCENE_STATIC ),
sceneManager );
sceneManager->destroyRectangle2D( mRectangle );
mRectangle = 0;
mRectangle = sceneManager->createRectangle2D( SCENE_STATIC );

mRectangle->setHollowRectRadius( mRadius[0] );
mRectangle->initialize(
Expand Down
7 changes: 6 additions & 1 deletion OgreMain/src/OgreRectangle2D2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ namespace Ogre
mRenderables.push_back( this );
}
//-----------------------------------------------------------------------------------
Rectangle2D::~Rectangle2D()
Rectangle2D::~Rectangle2D() { _releaseManualHardwareResources(); }
//-----------------------------------------------------------------------------------
void Rectangle2D::_releaseManualHardwareResources()
{
VaoManager *vaoManager = mManager->getDestinationRenderSystem()->getVaoManager();

Expand Down Expand Up @@ -88,8 +90,11 @@ namespace Ogre

++itor;
}
mVaoPerLod->clear();
}
//-----------------------------------------------------------------------------------
void Rectangle2D::_restoreManualHardwareResources() { createBuffers(); }
//-----------------------------------------------------------------------------------
bool Rectangle2D::isQuad() const { return ( mGeometryFlags & GeometryFlagQuad ) != 0u; }
//-----------------------------------------------------------------------------------
bool Rectangle2D::isStereo() const { return ( mGeometryFlags & GeometryFlagStereo ) != 0u; }
Expand Down
16 changes: 10 additions & 6 deletions OgreMain/src/OgreSceneManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,11 @@ namespace Ogre
mForwardPlusSystem = 0;
mForwardPlusImpl = 0;

OGRE_DELETE mSky;
mSky = 0;
if( mSky )
{
destroyRectangle2D( mSky );
mSky = 0;
}

OGRE_DELETE mRadialDensityMask;
mRadialDensityMask = 0;
Expand Down Expand Up @@ -1080,8 +1083,7 @@ namespace Ogre
{
if( !mSky )
{
mSky = OGRE_NEW Rectangle2D( Id::generateNewId<MovableObject>(),
&mEntityMemoryManager[SCENE_STATIC], this );
mSky = createRectangle2D( SCENE_STATIC );
// We can't use BT_DYNAMIC_* because the scene may be rendered from multiple cameras
// in the same frame, and dynamic supports only one set of values per frame
mSky->initialize( BT_DEFAULT,
Expand Down Expand Up @@ -1146,9 +1148,11 @@ namespace Ogre
else
{
if( mSky )
{
mSky->detachFromParent();
OGRE_DELETE mSky;
mSky = 0;
destroyRectangle2D( mSky );
mSky = 0;
}
if( mSkyMaterial )
{
materialManager.remove( mSkyMaterial );
Expand Down

0 comments on commit 759d2f8

Please sign in to comment.