diff --git a/src/ruisapp/glue/windows/application.cxx b/src/ruisapp/glue/windows/application.cxx index 4d7f372..3de7ed4 100644 --- a/src/ruisapp/glue/windows/application.cxx +++ b/src/ruisapp/glue/windows/application.cxx @@ -143,6 +143,9 @@ 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) ) diff --git a/src/ruisapp/glue/windows/application.hxx b/src/ruisapp/glue/windows/application.hxx index 9f55ca1..bed78e3 100644 --- a/src/ruisapp/glue/windows/application.hxx +++ b/src/ruisapp/glue/windows/application.hxx @@ -73,6 +73,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 { @@ -118,6 +147,13 @@ class application_glue : public utki::destructable const utki::shared_ref common_render_objects; const utki::shared_ref ruis_resource_loader; +#ifndef RUISAPP_RENDER_VULKAN + // 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 + public: const utki::shared_ref ruis_style_provider;