From c54ef55ebb982bd453a8d51b8c1094c711ec295c Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Mon, 17 May 2021 12:40:55 +0400 Subject: [PATCH 1/2] Revert "Replace raw pointer by observer_ptr to avoid dangling pointer dereferencing" This reverts commit 5373cf1cd5e2899c7d9fd7976ec39fbf4e5efc95. --- components/sceneutil/lightmanager.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/components/sceneutil/lightmanager.cpp b/components/sceneutil/lightmanager.cpp index 16af3880f..e8cfad522 100644 --- a/components/sceneutil/lightmanager.cpp +++ b/components/sceneutil/lightmanager.cpp @@ -726,7 +726,7 @@ namespace SceneUtil META_StateAttribute(NifOsg, LightManagerStateAttribute, osg::StateAttribute::LIGHT) - void initSharedLayout(osg::GLExtensions* ext, int handle, LightManager& lightManager) const + void initSharedLayout(osg::GLExtensions* ext, int handle) const { constexpr std::array index = { static_cast(Shader::UBOBinding::LightBuffer) }; int totalBlockSize = -1; @@ -748,17 +748,13 @@ namespace SceneUtil for (int i = 0; i < 2; ++i) { - auto& buf = lightManager.getLightBuffer(i); + auto& buf = mLightManager->getLightBuffer(i); buf = new LightBuffer(*buf, offsets[0], offsets[1], offsets[2], totalBlockSize, stride); } } void apply(osg::State& state) const override { - osg::ref_ptr lightManager; - if (!mLightManager.lock(lightManager)) - return; - if (!mInitLayout) { mDummyProgram->apply(state); @@ -771,12 +767,12 @@ namespace SceneUtil // wait until the UBO binding is created if (activeUniformBlocks > 0) { - initSharedLayout(ext, handle, *lightManager); + initSharedLayout(ext, handle); mInitLayout = true; } } - lightManager->getLightBuffer(state.getFrameStamp()->getFrameNumber())->uploadCachedSunPos(state.getInitialViewMatrix()); - lightManager->getLightBuffer(state.getFrameStamp()->getFrameNumber())->dirty(); + mLightManager->getLightBuffer(state.getFrameStamp()->getFrameNumber())->uploadCachedSunPos(state.getInitialViewMatrix()); + mLightManager->getLightBuffer(state.getFrameStamp()->getFrameNumber())->dirty(); } private: @@ -806,7 +802,7 @@ namespace SceneUtil return shader; } - osg::observer_ptr mLightManager; + LightManager* mLightManager; osg::ref_ptr mDummyProgram; mutable bool mInitLayout; }; From 11c57978b69174eb87b6d8ec572a57696fd0bc73 Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Mon, 17 May 2021 13:01:15 +0400 Subject: [PATCH 2/2] Fix crashes on exit in the LightManager (bug #6044) --- components/sceneutil/lightmanager.cpp | 11 ++++++++++- components/sceneutil/lightmanager.hpp | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/components/sceneutil/lightmanager.cpp b/components/sceneutil/lightmanager.cpp index e8cfad522..a69a64ba3 100644 --- a/components/sceneutil/lightmanager.cpp +++ b/components/sceneutil/lightmanager.cpp @@ -439,7 +439,11 @@ namespace SceneUtil void apply(osg::State &state) const override { - auto* lightUniform = mLightManager->getStateSet()->getUniform("LightBuffer"); + osg::StateSet* stateSet = mLightManager->getStateSet(); + if (!stateSet) + return; + + auto* lightUniform = stateSet->getUniform("LightBuffer"); for (size_t i = 0; i < mLights.size(); ++i) { auto light = mLights[i]; @@ -832,6 +836,11 @@ namespace SceneUtil return ""; } + LightManager::~LightManager() + { + getOrCreateStateSet()->removeAttribute(osg::StateAttribute::LIGHT); + } + LightManager::LightManager(bool ffp) : mStartLight(0) , mLightingMask(~0u) diff --git a/components/sceneutil/lightmanager.hpp b/components/sceneutil/lightmanager.hpp index dc7f36e96..6dbe7a3f7 100644 --- a/components/sceneutil/lightmanager.hpp +++ b/components/sceneutil/lightmanager.hpp @@ -137,6 +137,8 @@ namespace SceneUtil LightManager(const LightManager& copy, const osg::CopyOp& copyop); + ~LightManager(); + /// @param mask This mask is compared with the current Camera's cull mask to determine if lighting is desired. /// By default, it's ~0u i.e. always on. /// If you have some views that do not require lighting, then set the Camera's cull mask to not include