1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-03-26 22:40:25 +00:00

Merge branch 'fix_dangling_light_manager' into 'master'

Replace raw pointer by observer_ptr to avoid dangling pointer dereferencing (#6044)

Closes #6044

See merge request OpenMW/openmw!865
This commit is contained in:
psi29a 2021-05-15 15:19:55 +00:00
commit a1b96cb9cc

View file

@ -731,7 +731,7 @@ namespace SceneUtil
META_StateAttribute(NifOsg, LightManagerStateAttribute, osg::StateAttribute::LIGHT) META_StateAttribute(NifOsg, LightManagerStateAttribute, osg::StateAttribute::LIGHT)
void initSharedLayout(osg::GLExtensions* ext, int handle) const void initSharedLayout(osg::GLExtensions* ext, int handle, LightManager& lightManager) const
{ {
constexpr std::array<unsigned int, 1> index = { static_cast<unsigned int>(Shader::UBOBinding::LightBuffer) }; constexpr std::array<unsigned int, 1> index = { static_cast<unsigned int>(Shader::UBOBinding::LightBuffer) };
int totalBlockSize = -1; int totalBlockSize = -1;
@ -753,13 +753,17 @@ namespace SceneUtil
for (int i = 0; i < 2; ++i) for (int i = 0; i < 2; ++i)
{ {
auto& buf = mLightManager->getLightBuffer(i); auto& buf = lightManager.getLightBuffer(i);
buf->configureLayout(offsets[0], offsets[1], offsets[2], totalBlockSize, stride); buf->configureLayout(offsets[0], offsets[1], offsets[2], totalBlockSize, stride);
} }
} }
void apply(osg::State& state) const override void apply(osg::State& state) const override
{ {
osg::ref_ptr<LightManager> lightManager;
if (!mLightManager.lock(lightManager))
return;
if (!mInitLayout) if (!mInitLayout)
{ {
mDummyProgram->apply(state); mDummyProgram->apply(state);
@ -772,12 +776,12 @@ namespace SceneUtil
// wait until the UBO binding is created // wait until the UBO binding is created
if (activeUniformBlocks > 0) if (activeUniformBlocks > 0)
{ {
initSharedLayout(ext, handle); initSharedLayout(ext, handle, *lightManager);
mInitLayout = true; mInitLayout = true;
} }
} }
mLightManager->getLightBuffer(state.getFrameStamp()->getFrameNumber())->uploadCachedSunPos(state.getInitialViewMatrix()); lightManager->getLightBuffer(state.getFrameStamp()->getFrameNumber())->uploadCachedSunPos(state.getInitialViewMatrix());
mLightManager->getLightBuffer(state.getFrameStamp()->getFrameNumber())->dirty(); lightManager->getLightBuffer(state.getFrameStamp()->getFrameNumber())->dirty();
} }
private: private:
@ -807,7 +811,7 @@ namespace SceneUtil
return shader; return shader;
} }
LightManager* mLightManager; osg::observer_ptr<LightManager> mLightManager;
osg::ref_ptr<osg::Program> mDummyProgram; osg::ref_ptr<osg::Program> mDummyProgram;
mutable bool mInitLayout; mutable bool mInitLayout;
}; };