diff --git a/src/ruis/render/context.cpp b/src/ruis/render/context.cpp index d6c926ac5..e6aad5502 100644 --- a/src/ruis/render/context.cpp +++ b/src/ruis/render/context.cpp @@ -61,6 +61,11 @@ context::~context() if (!cur_context_stack.empty()) { cur_context_stack.back()->native_window.get().bind_rendering_context(); + } else { + // no render contexts are left, so make the factory's shared rendering + // context current: the shared render objects created by the factory + // must be destroyed with a valid current context + this->factory.get().bind(); } } diff --git a/src/ruis/render/factory.hpp b/src/ruis/render/factory.hpp index 2dd7d2c38..bf850a4dd 100644 --- a/src/ruis/render/factory.hpp +++ b/src/ruis/render/factory.hpp @@ -45,6 +45,7 @@ along with this program. If not, see . #include "vertex_buffer.hpp" namespace ruis::render { +class context; /** * @brief Render object factory. @@ -62,6 +63,10 @@ namespace ruis::render { */ class factory : public std::enable_shared_from_this { + // ruis::render::context calls the protected bind() when the last render + // context is destroyed, to make the shared rendering context current + friend class context; + public: virtual ~factory() = default; @@ -80,11 +85,23 @@ class factory : public std::enable_shared_from_this * therefore call its private bind_rendering_context(). Concrete renderer * factories use this to make their headless (shared) GL context current. */ - void bind(ruis::render::native_window& win) const + static void bind_window_context(ruis::render::native_window& win) { win.bind_rendering_context(); } + /** + * @brief Make this factory's shared rendering context current. + * + * The default (empty) implementation is for renderers that have no notion + * of a current context (e.g. Vulkan). GL-based factories override it to + * make their shared (headless) GL context current. Called by + * ruis::render::context when the last render context is destroyed, so + * that the shared render objects created by the factory can be destroyed + * with a valid current context. + */ + virtual void bind() const {} + public: factory(const factory&) = delete; factory& operator=(const factory&) = delete; diff --git a/tests/harness/modules/ruis-render-opengl b/tests/harness/modules/ruis-render-opengl index 7b2a7ecea..332ce39f2 160000 --- a/tests/harness/modules/ruis-render-opengl +++ b/tests/harness/modules/ruis-render-opengl @@ -1 +1 @@ -Subproject commit 7b2a7ecea5c337befdf6fec2d2bf2689a84d381d +Subproject commit 332ce39f29feaa0ea78daa995b2c4d05fd793c6f diff --git a/tests/harness/modules/ruis-render-opengles b/tests/harness/modules/ruis-render-opengles index d742586c1..7f8c040ce 160000 --- a/tests/harness/modules/ruis-render-opengles +++ b/tests/harness/modules/ruis-render-opengles @@ -1 +1 @@ -Subproject commit d742586c1cc6e7dce55adcadb9842a0c3cd0704a +Subproject commit 7f8c040cedeceba45b1865b020d7979f1b744869