From 5fac4560f88a790c6c29c0c092f90536202e7a59 Mon Sep 17 00:00:00 2001 From: BlankenshipG Date: Mon, 5 Oct 2026 09:49:31 -0400 Subject: [PATCH] fix(engine): submit ImGui draw data after the Avalonia blit The aelrynth merge took upstream's EngineFrameController, which submits the ImGui frame inside OnEndScene, before EndSceneHook blits the Avalonia quad, so panels hid the ILT Hub and other overlay windows. Restore 6b94752's order: OnEndScene keeps the built frame and EndSceneHook calls RenderDeferredImGui after OverlayTextureRenderer. The Win32Backend mouse priority from 6b94752 survived; only its doc comment moved. Co-authored-by: Cursor --- .../2026-10-05-imgui-over-avalonia-port.md | 41 ++++++++++++++++ src/RynthCore.Engine/D3D9/EndSceneHook.cs | 5 ++ .../ImGui/EngineFrameController.cs | 48 ++++++++++++++++--- src/RynthCore.Engine/ImGui/Win32Backend.cs | 8 ++-- 4 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 Changelog/2026-10-05-imgui-over-avalonia-port.md diff --git a/Changelog/2026-10-05-imgui-over-avalonia-port.md b/Changelog/2026-10-05-imgui-over-avalonia-port.md new file mode 100644 index 0000000..438a8e7 --- /dev/null +++ b/Changelog/2026-10-05-imgui-over-avalonia-port.md @@ -0,0 +1,41 @@ +# ImGui windows draw over the Avalonia panels again (merge branch) + +Date: 2026-10-05 +Branch: RynthCore `merge/aelrynth-2026.10.5.1` (local) +Restores: SK-local 6b94752 (draw order part) + +## Problem + +In 2026.10.5.4 the ILT Hub, Mini Remote and Inventory HUDs open, but an Avalonia panel covering +the same screen area hides them. The Hub, saved at about (540,105), 633x786, only shows where no +panel covers it. + +SK-local 6b94752 submitted ImGui draw data after the Avalonia blit. The aelrynth merge took +upstream's `EngineFrameController`. Upstream builds the ImGui frame in `BuildImGuiFrame` and +submits it inside `OnEndScene`, before `EndSceneHook` blits the Avalonia quad. So the panels drew +on top again. + +## Fix + +- `EngineFrameController.OnEndScene` keeps the built frame (device + timing) instead of submitting + it. The pending frame is cleared at the start of every `OnEndScene`, so a frame that throws early + never resubmits stale draw data. +- New `EngineFrameController.RenderDeferredImGui()` submits it once. `UiFrameStats` gets build + + pop-out + submit time, as before. +- `EndSceneHook` calls `RenderDeferredImGui()` after `OverlayTextureRenderer.Render`. It runs even + when the Avalonia blit doesn't (character select), so ImGui draws exactly as before there. +- Popped-out panels still render inside `OnEndScene`. They use their own render targets and restore + AC's target and viewport. + +## Mouse priority (checked, unchanged) + +6b94752's `Win32Backend.ImGuiOwnsMouseOverAvalonia` survived the merge. A mouse message skips the +Avalonia forward while ImGui wants the mouse (`io.WantCaptureMouse` or a monster nameplate), unless +an Avalonia drag/resize holds pointer capture. So clicks on a visible ImGui window go to ImGui, not +to the panel underneath. Only a misplaced doc comment was fixed. + +## Notes + +- ImGui windows and in-client ImGui panels now cover any Avalonia panel or popup they overlap. + Avalonia gets the click only where no ImGui window is under the cursor. +- Engine build only; not in an installer yet. diff --git a/src/RynthCore.Engine/D3D9/EndSceneHook.cs b/src/RynthCore.Engine/D3D9/EndSceneHook.cs index 35f0e90..4f40c7a 100644 --- a/src/RynthCore.Engine/D3D9/EndSceneHook.cs +++ b/src/RynthCore.Engine/D3D9/EndSceneHook.cs @@ -449,6 +449,11 @@ internal static class EndSceneHook if (_uiFrameCount < 30) RynthLog.D3D9($"EndSceneHook: OverlayTextureRenderer.Render error: {ovEx.GetType().Name}: {ovEx.Message}"); } + + // ImGui draw data (in-client panels, plugin overlay windows such as the ILT Hub) + // is submitted LAST so it sits on top of the Avalonia layer. Built in OnEndScene; + // guarded internally, so an ImGui draw failure can't skip the original EndScene. + EngineFrameController.RenderDeferredImGui(); } catch (Exception ex) { diff --git a/src/RynthCore.Engine/ImGui/EngineFrameController.cs b/src/RynthCore.Engine/ImGui/EngineFrameController.cs index c99ee1a..73e7a0f 100644 --- a/src/RynthCore.Engine/ImGui/EngineFrameController.cs +++ b/src/RynthCore.Engine/ImGui/EngineFrameController.cs @@ -77,6 +77,13 @@ internal static class EngineFrameController private static long _drawSubmits; private static long _nextHeartbeatAt; + // Device for this frame's built ImGui draw data, submitted by RenderDeferredImGui after the + // Avalonia blit. Render thread only; Zero = nothing pending. The draw data stays valid until + // the next NewFrame on the main context. + private static IntPtr _deferredSubmitDevice; + // Build + pop-out time of the frame waiting in _deferredSubmitDevice (UiFrameStats adds the submit). + private static long _deferredUiTicks; + private static long _lastFrameTicks; private static int _frameCount; @@ -254,6 +261,8 @@ internal static class EngineFrameController // EngineSettings.EnableImGuiBackend, so world→screen capture, nav- // marker rendering, and plugin lifecycle stay alive when the ImGui // surface is disabled. + // A frame that throws before its build must not resubmit last frame's draw data. + _deferredSubmitDevice = IntPtr.Zero; try { // Resolve game HWND and seed EntryPoint.GameHwnd. Required even @@ -369,10 +378,10 @@ internal static class EngineFrameController // calls it at all. // ── ImGui-gated work ───────────────────────────────────────── - // The frame is BUILT here and SUBMITTED after the Nav3D fallback and - // the vital HUD, so ImGui windows sit above both. The Avalonia quad - // (EndSceneHook, after this returns) stays topmost while both stacks - // run, matching input order: Avalonia gets the first hit test. + // The frame is BUILT here and SUBMITTED by RenderDeferredImGui, which + // EndSceneHook calls after the Avalonia quad, so ImGui windows sit above + // the Nav3D fallback, the vital HUD and the Avalonia panels. Input order + // matches: Win32Backend gives ImGui the mouse while it wants it. bool imguiEnabled = Plugins.EngineSettings.EnableImGuiBackend; bool imguiBuilt = false; long uiTicks = 0; @@ -395,12 +404,19 @@ internal static class EngineFrameController if (imguiEnabled) { long t0 = Stopwatch.GetTimestamp(); - if (imguiBuilt) - SubmitImGuiFrame(pDevice); // Popped-out panels: their own contexts, windows and render targets. if (_imguiInitialized) ImGuiPopOuts.RenderAll(pDevice, _uiScale, PanelsLive); - UiFrameStats.Record(uiTicks + (Stopwatch.GetTimestamp() - t0)); + long ticks = uiTicks + (Stopwatch.GetTimestamp() - t0); + if (imguiBuilt) + { + _deferredUiTicks = ticks; + _deferredSubmitDevice = pDevice; + } + else + { + UiFrameStats.Record(ticks); + } } } catch (Exception ex) @@ -786,6 +802,24 @@ internal static class EngineFrameController _prevAvaloniaText = avaloniaText; } + /// + /// Submits this frame's ImGui draw data. EndSceneHook calls it after + /// OverlayTextureRenderer.Render so ImGui windows draw on top of the Avalonia layer. + /// Runs whether or not the Avalonia blit ran (character select, Avalonia off), and is + /// a no-op when built nothing this frame. Never throws. + /// + public static void RenderDeferredImGui() + { + IntPtr device = _deferredSubmitDevice; + if (device == IntPtr.Zero) + return; + _deferredSubmitDevice = IntPtr.Zero; // one submission per frame, even if it throws + + long t0 = Stopwatch.GetTimestamp(); + SubmitImGuiFrame(device); + UiFrameStats.Record(_deferredUiTicks + (Stopwatch.GetTimestamp() - t0)); + } + /// Draws the frame produced. private static void SubmitImGuiFrame(IntPtr pDevice) { diff --git a/src/RynthCore.Engine/ImGui/Win32Backend.cs b/src/RynthCore.Engine/ImGui/Win32Backend.cs index b92982e..6298357 100644 --- a/src/RynthCore.Engine/ImGui/Win32Backend.cs +++ b/src/RynthCore.Engine/ImGui/Win32Backend.cs @@ -1085,10 +1085,6 @@ internal static unsafe class Win32Backend } } - /// - /// If the cursor is over the Avalonia shell panel, PostMessage the mouse event to - /// the off-screen Avalonia HWND and return true (caller should suppress game delivery). - /// /// /// ImGui windows are drawn on top of the Avalonia layer, so when the cursor is over /// an ImGui window (or an ImGui widget is active) mouse input must go to ImGui @@ -1100,6 +1096,10 @@ internal static unsafe class Win32Backend return _wantCaptureMouse && !AvaloniaOverlay.HasPointerCapture; } + /// + /// If the cursor is over the Avalonia shell panel, PostMessage the mouse event to + /// the off-screen Avalonia HWND and return true (caller should suppress game delivery). + /// private static bool TryForwardToAvalonia(uint msg, IntPtr wParam, IntPtr lParam) { IntPtr avHwnd = AvaloniaOverlay.AvaloniaHwnd;