Skip to content

Commit

Permalink
AtmosphereNpr: recreate const buffer on device lost
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenegff committed Nov 20, 2024
1 parent c40528e commit 8fac6ae
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 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

0 comments on commit 8fac6ae

Please sign in to comment.