From f0c1bd4ea4fc6c6cd3850e9b686ee5e3b5b380d8 Mon Sep 17 00:00:00 2001 From: igagis-bot Date: Thu, 26 Nov 2026 10:24:32 +0200 Subject: [PATCH 1/3] glue: re-bind shared GL context before destroying shared GL objects (windows) On exit, the windows (and their GL contexts) are destroyed before the shared GL objects (common shaders, common render objects, shared resource textures) still owned by the glue, so those GL objects were deleted with no current GL context on the thread, triggering GL_INVALID_OPERATION in the debug GL error assertions. Add a guard member to application_glue that is destroyed right before the shared GL objects and re-binds the shared GL context (which outlives them) on this thread. --- src/ruisapp/glue/windows/application.cxx | 3 ++- src/ruisapp/glue/windows/application.hxx | 34 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/ruisapp/glue/windows/application.cxx b/src/ruisapp/glue/windows/application.cxx index 6c20deba..a6c193f7 100644 --- a/src/ruisapp/glue/windows/application.cxx +++ b/src/ruisapp/glue/windows/application.cxx @@ -133,7 +133,8 @@ application_glue::application_glue(const utki::version_duplet& gl_version) : ), ruis_style_provider( // utki::make_shared(this->ruis_resource_loader) - ) + ), + shared_gl_context_restorer(this->shared_gl_context_native_window.get()) {} void application_glue::render() diff --git a/src/ruisapp/glue/windows/application.hxx b/src/ruisapp/glue/windows/application.hxx index 2f82ff9f..5e86ed48 100644 --- a/src/ruisapp/glue/windows/application.hxx +++ b/src/ruisapp/glue/windows/application.hxx @@ -65,6 +65,35 @@ public: */ void update_units_from_system(); }; + +/** + * @brief Re-binds the shared GL context on destruction. + * + * Rendering makes each window's GL context current, and the window contexts + * are destroyed together with the windows. After that, no GL context is + * current on this thread. The shared GL objects (common shaders, common + * render objects, shared resource textures) are destroyed after the windows + * and still need a current context of the shared group to be released, so + * this guard restores the shared context (which outlives the shared objects) + * right before they are destroyed. + */ +class restore_shared_gl_context_on_destruction +{ + native_window& shared_gl_context_window; +public: + explicit restore_shared_gl_context_on_destruction( + native_window& shared_gl_context_window + ) : + shared_gl_context_window(shared_gl_context_window) + {} + restore_shared_gl_context_on_destruction(const restore_shared_gl_context_on_destruction&) = delete; + restore_shared_gl_context_on_destruction& operator=(const restore_shared_gl_context_on_destruction&) = delete; + + ~restore_shared_gl_context_on_destruction() + { + this->shared_gl_context_window.bind_rendering_context(); + } +}; } // namespace namespace { @@ -84,6 +113,11 @@ public: const utki::shared_ref ruis_style_provider; private: + // Declared after all owners of shared GL objects (and thus destroyed + // before them), so that the shared GL context is re-bound before the + // shared GL objects are destroyed. + const restore_shared_gl_context_on_destruction shared_gl_context_restorer; + std::map> windows; public: From a76144cc755d3b0cbc2e0ec5b1629392a413449d Mon Sep 17 00:00:00 2001 From: igagis-bot Date: Thu, 26 Nov 2026 13:04:27 +0200 Subject: [PATCH 2/3] windows: support the Vulkan variant without a shared gl context window Main now removes the shared gl context window in the Vulkan variant, so guard the shared gl context restorer the same way. Vulkan has no 'current context' to re-bind. Also, declare the restorer before ruis_style_provider to satisfy -Wreorder, and format the code with clang-format. --- src/ruisapp/glue/windows/application.cxx | 6 ++++-- src/ruisapp/glue/windows/application.hxx | 16 +++++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/ruisapp/glue/windows/application.cxx b/src/ruisapp/glue/windows/application.cxx index c24ff247..3de7ed4e 100644 --- a/src/ruisapp/glue/windows/application.cxx +++ b/src/ruisapp/glue/windows/application.cxx @@ -143,10 +143,12 @@ application_glue::application_glue(const utki::version_duplet& gl_version) : this->common_render_objects ) ), +#ifndef RUISAPP_RENDER_VULKAN + shared_gl_context_restorer(this->shared_gl_context_native_window.get()), +#endif ruis_style_provider( // utki::make_shared(this->ruis_resource_loader) - ), - shared_gl_context_restorer(this->shared_gl_context_native_window.get()) + ) {} void application_glue::render() diff --git a/src/ruisapp/glue/windows/application.hxx b/src/ruisapp/glue/windows/application.hxx index bb9dc534..3a2b78b2 100644 --- a/src/ruisapp/glue/windows/application.hxx +++ b/src/ruisapp/glue/windows/application.hxx @@ -88,12 +88,12 @@ public: class restore_shared_gl_context_on_destruction { native_window& shared_gl_context_window; + public: - explicit restore_shared_gl_context_on_destruction( - native_window& shared_gl_context_window - ) : + explicit restore_shared_gl_context_on_destruction(native_window& shared_gl_context_window) : shared_gl_context_window(shared_gl_context_window) {} + restore_shared_gl_context_on_destruction(const restore_shared_gl_context_on_destruction&) = delete; restore_shared_gl_context_on_destruction& operator=(const restore_shared_gl_context_on_destruction&) = delete; @@ -147,15 +147,17 @@ class application_glue : public utki::destructable const utki::shared_ref common_render_objects; const utki::shared_ref ruis_resource_loader; -public: - const utki::shared_ref ruis_style_provider; - -private: +#ifndef RUISAPP_RENDER_VULKAN // Declared after all owners of shared GL objects (and thus destroyed // before them), so that the shared GL context is re-bound before the // shared GL objects are destroyed. const restore_shared_gl_context_on_destruction shared_gl_context_restorer; +#endif + +public: + const utki::shared_ref ruis_style_provider; +private: // std::vector is used instead of std::map to preserve the order in which // the windows were created, so that application::get_windows() returns the // windows in a stable order. The number of windows is small (2-3), so From 935229de97d97a329582ea4b50eb75c6e61100a0 Mon Sep 17 00:00:00 2001 From: igagis-bot Date: Thu, 26 Nov 2026 15:07:38 +0200 Subject: [PATCH 3/3] windows: mention common_render_objects in the restorer declaration comment --- src/ruisapp/glue/windows/application.hxx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ruisapp/glue/windows/application.hxx b/src/ruisapp/glue/windows/application.hxx index 3a2b78b2..bed78e38 100644 --- a/src/ruisapp/glue/windows/application.hxx +++ b/src/ruisapp/glue/windows/application.hxx @@ -148,8 +148,8 @@ class application_glue : public utki::destructable const utki::shared_ref ruis_resource_loader; #ifndef RUISAPP_RENDER_VULKAN - // Declared after all owners of shared GL objects (and thus destroyed - // before them), so that the shared GL context is re-bound before the + // Must be declared after common_render_objects (and thus destroyed + // before it), so that the shared GL context is re-bound before the // shared GL objects are destroyed. const restore_shared_gl_context_on_destruction shared_gl_context_restorer; #endif