From 78fd5b78da41417b9293b197ff059d5a7a0187df Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Tue, 22 Feb 2022 23:24:37 +0100 Subject: [PATCH] Wait for initialization when adding assets at runtime. Correctly destroy the RenderableGalaxy shader when deinitializing (closes #1601) --- include/openspace/util/threadpool.h | 2 ++ modules/galaxy/rendering/renderablegalaxy.cpp | 5 +++++ src/scene/sceneinitializer.cpp | 7 +++++++ src/util/threadpool.cpp | 4 ++++ 4 files changed, 18 insertions(+) diff --git a/include/openspace/util/threadpool.h b/include/openspace/util/threadpool.h index 73a65e5302..6c2287f258 100644 --- a/include/openspace/util/threadpool.h +++ b/include/openspace/util/threadpool.h @@ -56,6 +56,8 @@ public: void enqueue(std::function f); void clearTasks(); + bool hasOutstandingTasks() const; + private: friend class Worker; diff --git a/modules/galaxy/rendering/renderablegalaxy.cpp b/modules/galaxy/rendering/renderablegalaxy.cpp index ead0306bcb..891270155b 100644 --- a/modules/galaxy/rendering/renderablegalaxy.cpp +++ b/modules/galaxy/rendering/renderablegalaxy.cpp @@ -494,6 +494,11 @@ void RenderableGalaxy::deinitializeGL() { _raycaster = nullptr; } + global::renderEngine->removeRenderProgram(_pointsProgram.get()); + _pointsProgram = nullptr; + global::renderEngine->removeRenderProgram(_billboardsProgram.get()); + _billboardsProgram = nullptr; + glDeleteVertexArrays(1, &_pointsVao); glDeleteBuffers(1, &_positionVbo); glDeleteBuffers(1, &_colorVbo); diff --git a/src/scene/sceneinitializer.cpp b/src/scene/sceneinitializer.cpp index 2f121cf403..b1068e27a9 100644 --- a/src/scene/sceneinitializer.cpp +++ b/src/scene/sceneinitializer.cpp @@ -100,6 +100,13 @@ void MultiThreadedSceneInitializer::initializeNode(SceneGraphNode* node) { } std::vector MultiThreadedSceneInitializer::takeInitializedNodes() { + // Some of the scene graph nodes might still be in the initialization queue and we + // should wait for those to finish or we end up in some half-initialized state since + // other parts of the application already know about their existence + while (_threadPool.hasOutstandingTasks()) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + std::lock_guard g(_mutex); std::vector nodes = std::move(_initializedNodes); return nodes; diff --git a/src/util/threadpool.cpp b/src/util/threadpool.cpp index b07c2b44a2..ab77e0939c 100644 --- a/src/util/threadpool.cpp +++ b/src/util/threadpool.cpp @@ -99,4 +99,8 @@ void ThreadPool::clearTasks() { } // release lock } +bool ThreadPool::hasOutstandingTasks() const { + return !tasks.empty(); +} + } // namespace openspace