From 5f243f93bad4daaf2dc353758db4bc5ef210a824 Mon Sep 17 00:00:00 2001 From: HanifKoh <76276251+HanifKoh@users.noreply.github.com> Date: Wed, 11 Nov 2026 14:19:30 +0800 Subject: [PATCH] Don't Crash or Stay Silent When the OpenGL Context Can't Be Activated (#17441) * Don't Crash or Stay Silent When the OpenGL Context Can't Be Activated When the driver refuses to make the OpenGL context current, no ImGui frame ever runs and OpenGL is never initialized, so ImGui has no current font and the GL functions are never loaded. A notification that measures its text from an event handler (a validation warning, a job's progress, the slicing progress bar) then reads the null font and crashes, and once a slice completes, loading its G-code preview calls the unloaded GL functions and crashes too. Notifications now skip measuring until a frame has set up the font; they stay Unknown, and render() already measures them when it draws. The G-code preview is not loaded while OpenGL cannot be initialized; with OpenGL up, init_opengl() returns at once and nothing changes. On Windows, a shown canvas whose context cannot be made current is reported once: wx drops the wglMakeCurrent error in release builds, so the call is repeated to log GetLastError() with the canvas's pixel format and the display it is on. If OpenGL never came up in the session, the user gets one error dialog instead of an empty 3D view with no explanation. A GTK canvas can fail until its surface is mapped, so other platforms only get the crash fixes. * Name the Display Adapter Instead of Only Suggesting a Driver Upgrade When OpenGL Can't Be Activated A context that can't be made current has several causes: no OpenGL driver behind the display the window is on (a disabled adapter, or one running on the Microsoft Basic Display Adapter), a broken driver, or an overlay. The old advice to upgrade the driver covered only one of them. The dialog now names the adapter Windows reports for the window's display and lists the general checks, and the log line keeps the display device and adapter name. (cherry picked from commit 3c72223aaf7976bdece51d125e5364d219c3d91f) --- src/slic3r/GUI/GLCanvas3D.cpp | 12 +++- src/slic3r/GUI/HintNotification.cpp | 3 + src/slic3r/GUI/NotificationManager.cpp | 4 ++ src/slic3r/GUI/OpenGLManager.cpp | 97 ++++++++++++++++++++++++++ src/slic3r/GUI/OpenGLManager.hpp | 5 ++ 5 files changed, 120 insertions(+), 1 deletion(-) diff --git a/src/slic3r/GUI/GLCanvas3D.cpp b/src/slic3r/GUI/GLCanvas3D.cpp index 41b0071203..1ae06db52e 100644 --- a/src/slic3r/GUI/GLCanvas3D.cpp +++ b/src/slic3r/GUI/GLCanvas3D.cpp @@ -2563,7 +2563,13 @@ void GLCanvas3D::_render_frame(bool scene_dirty, bool only_init) return; // ensures this canvas is current and initialized - if (!_is_shown_on_screen() || !_set_current() || !wxGetApp().init_opengl()) + if (!_is_shown_on_screen()) + return; + if (!_set_current()) { + wxGetApp().get_opengl_manager().report_make_current_failure(*m_canvas, m_context); + return; + } + if (!wxGetApp().init_opengl()) return; if (!is_initialized() && !init()) @@ -3907,6 +3913,10 @@ void GLCanvas3D::load_gcode_preview(const GCodeProcessorResult& gcode_result, co const std::vector& exclude_bounding_box = plate->get_exclude_areas(); _set_shown_canvas_current(); + // The viewer uploads to the GPU from here on. Without a context the driver lets us make + // current, the OpenGL functions were never loaded and there is nothing to draw it with. + if (!wxGetApp().init_opengl()) + return; //BBS: init is called in GLCanvas3D.render() //when load gcode directly, it is too late diff --git a/src/slic3r/GUI/HintNotification.cpp b/src/slic3r/GUI/HintNotification.cpp index 1ad5903dea..2a7c9fb380 100644 --- a/src/slic3r/GUI/HintNotification.cpp +++ b/src/slic3r/GUI/HintNotification.cpp @@ -850,6 +850,9 @@ void NotificationManager::HintNotification::init() // Do not init closing notification if (is_finished()) return; + // No font before the first ImGui frame, see PopNotification::init(). + if (ImGui::GetFont() == nullptr) + return; count_spaces(); count_lines(); diff --git a/src/slic3r/GUI/NotificationManager.cpp b/src/slic3r/GUI/NotificationManager.cpp index 91efc430df..ef4fded534 100644 --- a/src/slic3r/GUI/NotificationManager.cpp +++ b/src/slic3r/GUI/NotificationManager.cpp @@ -627,6 +627,10 @@ void NotificationManager::PopNotification::init() // Do not init closing notification if (is_finished()) return; + // Text is measured with the ImGui font, which the first ImGui frame sets up. Without a + // working OpenGL context that frame never comes, so stay Unknown and let render() init. + if (ImGui::GetFont() == nullptr) + return; count_spaces(); count_lines(); diff --git a/src/slic3r/GUI/OpenGLManager.cpp b/src/slic3r/GUI/OpenGLManager.cpp index fccfb57a9a..6f455b8b99 100644 --- a/src/slic3r/GUI/OpenGLManager.cpp +++ b/src/slic3r/GUI/OpenGLManager.cpp @@ -3,6 +3,7 @@ #include "GUI.hpp" #include "I18N.hpp" +#include "format.hpp" #include "3DScene.hpp" #include "libslic3r/AppConfig.hpp" @@ -38,6 +39,12 @@ #include "../Utils/MacDarkMode.hpp" #endif // __APPLE__ +#ifdef _WIN32 +#include +#include +#include +#endif // _WIN32 + #ifdef __WXGTK__ #include "LinuxDisplayBackend.hpp" #ifdef wxHAS_EGL @@ -427,6 +434,96 @@ wxGLContext* OpenGLManager::init_glcontext(wxGLCanvas& canvas, const std::pairGetGLRC() == nullptr) + out << "no OpenGL context was created"; + else if (::wglMakeCurrent(hdc, context->GetGLRC()) != FALSE) { + retry_succeeded = true; + out << "wglMakeCurrent succeeded on retry"; + } else { + const DWORD error = ::GetLastError(); + out << "wglMakeCurrent failed with error " << error << " (" << std::system_category().message(static_cast(error)) << ")"; + } + + PIXELFORMATDESCRIPTOR pfd{}; + if (const int pixel_format = ::GetPixelFormat(hdc); pixel_format > 0 && ::DescribePixelFormat(hdc, pixel_format, sizeof(pfd), &pfd) > 0) + out << ", pixel format " << pixel_format << " (flags 0x" << std::hex << pfd.dwFlags << std::dec << ", color " + << static_cast(pfd.cColorBits) << ", depth " << static_cast(pfd.cDepthBits) << ", stencil " + << static_cast(pfd.cStencilBits) << ")"; + else + out << ", no pixel format on the canvas"; + + if (!display.device.empty()) + out << ", " << into_u8(display.device) << " on " << (display.adapter.empty() ? "an unknown adapter" : into_u8(display.adapter)); + return out.str(); +} +#endif // _WIN32 + +void OpenGLManager::report_make_current_failure([[maybe_unused]] const wxGLCanvas& canvas, [[maybe_unused]] const wxGLContext* context) +{ + if (m_make_current_failure_reported) + return; + m_make_current_failure_reported = true; +#ifdef _WIN32 + const CanvasDisplay display = canvas_display(canvas); + bool retry_succeeded = false; + BOOST_LOG_TRIVIAL(error) << "Cannot make the OpenGL context current on a shown canvas: " + << describe_make_current_failure(canvas, context, display, retry_succeeded); + // A failure after OpenGL came up, or one a retry clears, does not leave the 3D view empty. + if (m_gl_initialized || retry_succeeded) + return; + // The cause varies (no OpenGL driver behind the display, a disabled or broken driver, an overlay), + // so name the adapter Windows reports for the window's display instead of guessing one. + // TRN The OpenGL context could not be made current, so nothing can be drawn in the 3D view. + wxString message = _L("OpenGL could not be activated for the 3D view, so it cannot be shown."); + if (!display.adapter.empty()) { + message += "\n\n"; + // TRN %1% is the name of a graphics card or display adapter as Windows reports it, such as "Microsoft Basic Display Adapter". + message += format_wxstr(_L("The window is on a display driven by \"%1%\"."), display.adapter); + } + message += "\n\n"; + message += _L("Make sure the display adapter driving this window's display is enabled and its driver is installed and up to date."); + message += "\n"; + message += _L("If the computer has more than one graphics card, connecting the monitor to another one may help."); + message += "\n"; + message += _L("If the problem remains, close any overlay or screen capture software and try again."); + // show_error() defers the dialog, so it does not open inside the paint handler drawing the canvas. + show_error(nullptr, message); +#endif // _WIN32 +} + static const char* const SCENE_CANVAS_NAME = "OrcaSceneCanvas"; wxGLCanvas* OpenGLManager::create_wxglcanvas(wxWindow& parent) diff --git a/src/slic3r/GUI/OpenGLManager.hpp b/src/slic3r/GUI/OpenGLManager.hpp index e9de7fc197..a9e2e354d2 100644 --- a/src/slic3r/GUI/OpenGLManager.hpp +++ b/src/slic3r/GUI/OpenGLManager.hpp @@ -77,6 +77,7 @@ private: }; bool m_gl_initialized{ false }; + bool m_make_current_failure_reported{ false }; wxGLContext* m_context{ nullptr }; GLShadersManager m_shaders_manager; static GLInfo s_gl_info; @@ -92,6 +93,10 @@ public: bool init_gl(bool popup_error = true); wxGLContext* init_glcontext(wxGLCanvas& canvas, const std::pair& required_opengl_version, bool enable_compatibility_profile, bool enable_debug); + // Called when the context cannot be made current on a shown canvas, which then stays empty. + // On Windows, logs why once per session and, if OpenGL never came up, tells the user. Elsewhere + // this does nothing: a GTK canvas can fail transiently until its surface is mapped. + void report_make_current_failure(const wxGLCanvas& canvas, const wxGLContext* context); GLShaderProgram* get_shader(const std::string& shader_name) { return m_shaders_manager.get_shader(shader_name); } GLShaderProgram* get_current_shader() { return m_shaders_manager.get_current_shader(); }