Compare the GL context, not the ImGui context, when detecting a context change - #463
Merged
Merged
Conversation
…xt change CheckAndHandleContextChange compared ImGui.GetCurrentContext().Handle, but that context is created once in ImGuiController.Init and never replaced, so the comparison could never differ and ReloadAllTextures was unreachable. A replaced GL context therefore left every tracked texture id naming an object the new context had never created. Track the GL the tracked textures were uploaded against and compare it by reference instead. The first observation records without reloading, so a window move or resize does not re-upload textures that are still valid. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XD56Kg65nsraeL7vTL9MgU
|
This was referenced Sep 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Fixes #462.
The bug
CheckAndHandleContextChange()is called fromRemapCanvasand the windowMovehandler, and is documented as reloading textures when the GL context changes. It comparedImGui.GetCurrentContext().Handleagainst a cachedcurrentGLContextHandle— but that is the ImGui context, created once inImGuiController.Initand never replaced. The comparison could never differ, soReloadAllTextures()was unreachable and a replaced GL context left every id inTexturesnaming an object the new context had never created: images render blank for the rest of the process, silently.The fix
Per the issue's suggested approach, track the thing that actually identifies the context — the
GLthe tracked textures were uploaded against — and compare it by reference:internal static IntPtr currentGLContextHandlebecomesinternal static GL? currentGLContext.ImGui.GetCurrentContext()capture inonConfigureIOis removed; the field is owned entirely by the detector, so no new assignment site can forget to update it.ReloadAllTexturesdeliberately does not delete the old ids ("the context is already gone"), which is only true when the context really has changed.Reset()clears the field.CheckAndHandleContextChangeno longer needsunsafe, so that block and itsS6640suppression are gone.What this does and does not detect
It detects a GL the application replaced. A driver-side reset that invalidates the context underneath an unchanged
GLinstance — the hybrid-graphics and resume-from-sleep cases the issue also mentions — is not observable this way; catching those needs aGL_KHR_robustnessreset query, which requires a robustness-enabled context and hardware to verify against. That is recorded in a<remarks>on the method and is deliberately not attempted here. What this change does settle is that the comparison is now against the right object, instead of one that provably never changes.Tests
Two tests in
ImGuiAppTests, using the existingFakeRendererBackend— the reload uploads throughIRendererBackend, not throughGLdirectly, so no GL entry point is ever called and the test needs no live context. TheGLinstances are constructed over aMock<INativeContext>purely to be distinct references.CheckAndHandleContextChange_WhenTheGLIsReplaced_ReloadsTrackedTextures— the regression the issue asks for. Asserts the first observation leaves the texture alone, then that replacing theGLreloads it and the tracked entry picks up the id the new context handed back. Fails against the previous implementation (verified by restoringImGuiApp.csand re-running: 1 failed, 472 passed).CheckAndHandleContextChange_WithTheSameGL_DoesNotReload— the guard against the opposite failure.RemapCanvascalls this on every move and resize, so reloading unconditionally would re-upload every texture and orphan the replaced handles. Passes before and after by design.Full
ImGui.App.Testssuite: 473 passed, 0 failed (471 pre-existing, unchanged). Release build ofImGui.App: 0 warnings, 0 errors.🤖 Generated with Claude Code
https://claude.ai/code/session_01XD56Kg65nsraeL7vTL9MgU
Generated by Claude Code