Skip to content

shims: find_title_window, the title's own window that PresentX shows its frames in - #8591

Open
NiiRoZz wants to merge 1 commit into
ItsNotRexed:mainfrom
NiiRoZz:d3d12-xs/presentx-title-window
Open

NiiRoZz wants to merge 1 commit into
ItsNotRexed:mainfrom
NiiRoZz:d3d12-xs/presentx-title-window

Conversation

@NiiRoZz

@NiiRoZz NiiRoZz commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

What and why

On the console, PresentX puts a frame on the display and names no window. A GDKX title still makes its own top-level Win32 window, with its own class and a message loop on its own thread, before it presents. On the PC the frames belong in that window.

Measured on a retail executable (Vulkan device, integration stack with #2490):

  • By the first PresentX, the process has exactly one visible, unowned top-level window, on a thread of its own. Every other window of the process is hidden: the IME windows Windows adds, and a driver's dummy window.
  • d3d12_xs: console-ABI command lists, upload arena, PC-list fixture path #2490's present_state_for built a hidden window of its own (WS_OVERLAPPEDWINDOW, never shown, no message loop) and presented there. The title's window stayed white for the whole run (~3,000 PresentX calls in 44 s).
  • A first lookup read window titles with GetWindowTextW. That sends WM_GETTEXT across threads, and it stopped the title at its first PresentX because the title's window thread was not pumping at that moment.

The change

runtime/shims/title_window.h: rt::d3d12::find_title_window(process_id, exclude) returns the visible, unowned, non-tool top-level window of the process with the largest client area, or null. It only reads window-manager state (EnumWindows, GetWindowThreadProcessId, IsWindowVisible, GetWindow(GW_OWNER), GetWindowLongPtrW, GetClientRect) and sends no message.

On its own it changes nothing on main, because main has no PresentX queue wiring. #2490's present_state_for should present into find_title_window(GetCurrentProcessId()) when it finds one, keep its own window only for a title that has none, and not destroy the title's window. The local integration does exactly that. With #7685 applied there too (PresentX's tokens), the presents go to the title's window.

Test

title_window_test (ctest title_window) makes five windows on a thread that never pumps its messages: the title window, a smaller visible one, and larger hidden, owned and tool windows. It checks that:

  • the lookup returns the title window in under 100 ms and skips the larger hidden, owned and tool windows;
  • with the title window excluded, it returns the smaller one;
  • another process id gives null, and so does the process once the windows are gone.

Negative control: a WM_GETTEXT sent to the title window times out (200 ms), so a lookup that sent messages would block there. Dropping the owner check makes the test fail (found == windows.title).

🤖 Generated with Claude Code

…its frames in

A GDKX title makes its own top-level Win32 window before its first PresentX, and PresentX names no window: on the PC
the frames belong in that window. Measured on a retail executable: by the first PresentX the process has one visible,
unowned top-level window on a thread of its own, and only hidden windows besides (IME, a driver's dummy window). The
PresentX wiring (ItsNotRexed#2490) built a hidden window of its own instead, so a running title showed a blank window.

find_title_window picks the visible, unowned, non-tool top-level window of the process with the largest client area.
It sends no message: a lookup that read window titles with GetWindowTextW (a WM_GETTEXT sent across threads) stopped
the retail title at its first PresentX, because the title's window thread was not pumping at that moment.

title_window_test makes the windows on a thread that never pumps; the negative control shows a sent WM_GETTEXT times out
there, while the lookup answers at once and skips the larger hidden, owned and tool windows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TillusStark26

Copy link
Copy Markdown

Tested commit ec41c2201c4881d9ee7760a6e6c9e7d02c3cb48f on native Windows 11 x64 (MSVC 19.44.35230.0, Windows SDK 10.0.26100.0, Ninja Release build, NVIDIA GeForce RTX 3050 8 GB, driver 591.86 / Vulkan 1.4.363.1). Synthetic test only, zero game data.

Build & Test Verification

  • cmake --build build --target title_window_test -j 6 -> built cleanly with /W4 /permissive- /utf-8 (0 warnings).
  • title_window_test.exe -> title_window: ok (exit code 0):
    • Verified that the negative control SendMessageTimeoutW(windows.title, WM_GETTEXT, ..., SMTO_BLOCK, 200, ...) times out on the non-pumping window thread.
    • Verified that rt::d3d12::find_title_window(GetCurrentProcessId()) returns windows.title in < 100 ms without sending window messages, skipping the larger hidden (900x700), owned (800x600), and tool (800x600) windows.
    • Verified that passing exclude = windows.title selects windows.small (160x120), and querying a different PID or after window destruction returns nullptr.
  • python -I scripts/check_no_game_data.py runtime/CMakeLists.txt runtime/shims/title_window.h runtime/test/title_window_test.cpp -> clean (exit code 0).

Code Review

  • EnumWindows callback in runtime/shims/title_window.h queries only window-manager state (GetWindowThreadProcessId, IsWindowVisible, GetWindow(GW_OWNER), GetWindowLongPtrW(GWL_EXSTYLE), GetClientRect), avoiding any cross-thread SendMessage deadlock when the presentation thread queries the window while the title's UI thread is blocked.

AI-assisted.

@lohant27-netizen

Copy link
Copy Markdown

Tested ec41c2201 on Windows 11 (10.0.26300), NVIDIA GeForce RTX 5070 (driver 32.0.16.2101, subgroup 32), MSVC with SDK 10.0.26100, Release, merged onto main 867d922f0 together with #8599, #8589, #8591, #8620, #8569 and #8622 (no conflicts): pass.

@Marukooh

Copy link
Copy Markdown
Contributor

Tested ec41c22 on Wine 11.22 with RTX 5070 Ti (NVIDIA 620.73.04). The synthetic title_window CTest passed. A local integration with #7685 and #7448 selected the title's own visible window and recorded over 1,000 successful one-plane PresentX calls to it. Two sampled frames in that window remained black. The command-list diagnostics point to a separate RTV-handle recording failure covered by #7880, which is being tested separately.

AI-assisted verification. No game files, screenshots or traces attached.

@Exopus-0x65f7ad0

Copy link
Copy Markdown
Contributor

The present_state_for side, measured on the retail boot (Windows 11, RTX 5060 Ti, ASTROWwwW:ladder-frame@a1751a9f0 + #7604 + this PR). Without it, this PR changes nothing at run time: the frames still go to the hidden LeonidaPresentX window, and the title's window stays white. With it, the log shows PresentX presents into the title's window, the title's window shows a black 1920x1080 frame at 31 s, and the whole window is black at 62 s (A33: #7154 (comment)). @ASTROWwwW, this applies on ladder-frame as is (it sits where #2490's present_state_for is):

diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp
index af7f22599..efc4d1d68 100644
--- a/runtime/shims/d3d12_xs_device.cpp
+++ b/runtime/shims/d3d12_xs_device.cpp
@@ -25,6 +25,7 @@
 // for a trace that disagrees. The frame-event calls answer from a per-device token counter: there is no vblank source
 // behind the device before a presenter exists, so WaitFrameEventX returns at once with a fresh non-zero token.
 #include "d3d12_xs_device.h"
+#include "title_window.h"
 #include "d3d12_trace.h"
 #include "d3d12_xs_alloc_size.h"
 #include "d3d12_xs_desc_clean.h"
@@ -1294,9 +1295,10 @@ LONG CALLBACK table_overrun_handler(EXCEPTION_POINTERS* info) {
 
 struct PresentState {
     ~PresentState() {
-        if (window) DestroyWindow(window);  // the presentation and its window die with the device proxy
+        if (window && owns_window) DestroyWindow(window);  // the title's own window is never destroyed (#8591)
     }
     HWND window = nullptr;
+    bool owns_window = false;
     std::unique_ptr<rt::d3d12::XboxPresentation> xbox;
     bool tried = false;  // creation is attempted once; a null xbox means this device cannot present
 };
@@ -1317,17 +1319,25 @@ PresentState* present_state_for(DeviceProxy* device) {
     if (!state) return nullptr;
     device->present_state = state;
     state->tried = true;
+    // The title's own top-level window, when it has one (#8591): frames go where the player looks.
+    if (HWND title = rt::d3d12::find_title_window(GetCurrentProcessId())) {
+        state->window = title;
+        std::fprintf(stderr, "[rt] d3d12_xs: PresentX presents into the title's window %p\n", static_cast<void*>(title));
+    }
     WNDCLASSW window_class{};
     window_class.lpfnWndProc = present_window_proc;
     window_class.hInstance = GetModuleHandleW(nullptr);
     window_class.lpszClassName = L"LeonidaPresentX";
-    if (!RegisterClassW(&window_class) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS) {
+    if (!state->window && !RegisterClassW(&window_class) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS) {
         std::fprintf(stderr, "[rt] d3d12_xs: PresentX window class failed (error %lu)\n", GetLastError());
         return state;
     }
-    state->window = CreateWindowExW(0, window_class.lpszClassName, L"Leonida", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
-                                  CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr,
-                                  window_class.hInstance, nullptr);
+    if (!state->window) {  // a title without a window of its own: the runtime's, hidden as before
+        state->window = CreateWindowExW(0, window_class.lpszClassName, L"Leonida", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
+                                        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, nullptr, nullptr,
+                                        window_class.hInstance, nullptr);
+        state->owns_window = state->window != nullptr;
+    }
     if (!state->window) {
         std::fprintf(stderr, "[rt] d3d12_xs: PresentX window failed (error %lu)\n", GetLastError());
         return state;

AI-assisted (Claude Code).

@akimfomin

Copy link
Copy Markdown

A33 integration follow-up for #8591. On Windows 11 28000.3501, I tested the exact ec41c2201 window finder with the PresentState block from ladder-frame@a1751a9f0, using real synthetic Win32 windows on a private inactive desktop and a fake Vulkan presenter.

Simple local wiring of the finder into the existing once-per-device latch, with borrowed-window teardown fixed, passed 14/19 checks. A visible 0×0 placeholder could be selected on the first call and stay pinned when a different, usable title window appeared. If the same HWND resized, the direct wiring worked.

A local candidate passed 19/19. It distinguishes a ready client area, an existing but transient client, and a truly absent candidate. On transient it defers before caching any state. On absent it keeps the existing fallback. Teardown destroys only the runtime-owned fallback and leaves the title window alive. These checks cover binding and ownership before the Vulkan constructor. No full Vulkan or retail run. AI-assisted (Codex).

@RAINighty

Copy link
Copy Markdown

Tested ec41c2201 merged with main 867d922f0 (clean merge) on Windows 11 (10.0.26300), Intel Core i7-14650HX, MSVC 19.44.35228, RelWithDebInfo (Ninja), CPU-only tests: pass.

  • title_window passes with this PR.
  • Control is not clean here: runtime/shims/title_window.h is new in this PR, so there is no main version to put back and the title_window test passes as before; there is nothing to compare against.
  • Code not reviewed beyond running it. No GPU or game files.

AI-assisted (Claude Code).

@fever308

Copy link
Copy Markdown

checked: #8591ec41c22 | with: cmake -S runtime -B runtime/build -G "Visual Studio 17 2022" -A x64 -DCMAKE_SYSTEM_VERSION=10.0.26100.0 -DLEONIDA_VULKAN_HEADERS_FETCH=ON -DLEONIDA_REQUIRE_VULKAN=ON, cmake --build runtime/build --config Release --target title_window_test -- /nr:false /m:1, title_window_test.exe, python -I scripts/check_no_game_data.py ... | on: Windows 11 x64, MSVC 19.44.35222.0, Windows SDK 10.0.26100.0, NVIDIA GeForce RTX 5090 (driver 617.42), synthetic tests only (no game files) | result: pass (title_window_test: title_window: ok, 0 warnings under /W4 /permissive- /utf-8; check_no_game_data.py: clean)

Details

  • Base / merge: ec41c2201 merges cleanly onto main (867d922f0) with zero conflicts.
  • Negative controls:
    • Built-in cross-thread message check: SendMessageTimeoutW(windows.title, WM_GETTEXT, ..., SMTO_BLOCK, 200, ...) times out on the non-pumping window thread while find_title_window(GetCurrentProcessId()) returns windows.title in < 100 ms.
    • Filter negative control: Temporarily omitting the WS_EX_TOOLWINDOW filter in find_title_window() (runtime/shims/title_window.h) causes title_window_test.exe to fail at line 85: found == windows.title (exit 1).
  • Code review (ec41c2201):
    • EnumWindows callback queries only window-manager state (GetWindowThreadProcessId, IsWindowVisible, GetWindow(GW_OWNER), GetWindowLongPtrW(GWL_EXSTYLE), GetClientRect), sending no window messages across threads.
    • Non-blocking note for callers (present_state_for): search.best_area starts at -1, so a visible unowned non-tool window with a 0x0 client rect (area == 0) still satisfies area > s.best_area if no larger window exists yet (initializing best_area = 0 would require a non-empty client area before latching).

AI-assisted.

@byPreaZy

Copy link
Copy Markdown

checked: #8591 | with: applied the diff on main 867d922, built title_window_test (MSVC, Ninja RelWithDebInfo) | on: Windows 11, RX 9060 XT | result: title_window_test ok

Code read + test verified on a real desktop: the lookup uses only non-sending calls (EnumWindows, GetWindowThreadProcessId, IsWindowVisible, GW_OWNER, GWL_EXSTYLE, GetClientRect), picks the visible unowned non-tool window with the largest client area, honours the exclude HWND, returns null for another process, and answers in <100 ms while the window thread never pumps messages. The negative control (SendMessageTimeoutW times out) proves a GetWindowTextW-style lookup would have blocked, matching the measured retail stop.

AI-assisted check.

@Hiiva

Hiiva commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Integration fixture finding for #8591 at ec41c2201c4881d9ee7760a6e6c9e7d02c3cb48f, with the published PresentState wiring applied to the reviewed #2490/#7685 console route.

The current xbox_present_test keeps XboxPresentation real alive on its original visible HWND when it first calls the console queue route. The finder selects that same HWND, so the route attempts another non-retired swapchain for the occupied window. The old integrated test failed its future-token E_INVALIDARG check after presenter creation failed and the route fell back to a no-op.

A 19-line test-only correction hides the occupied fixture window, creates a fresh eligible HWND for the console route, and asserts that the production finder selects it. Its lifetime extends past all proxy/presenter releases; IsWindow then verifies that borrowed-window teardown left it alive. The existing future-token rejection, valid-token success, and proxy-reference-count checks are unchanged.

Native Windows x64, RTX 4080, MSVC 19.44, SDK 10.0.26100.0, RelWithDebInfo, Vulkan validation enabled:

  • Integrated source 61243a201a3a0e03da8a8889a16d3266ac8a463c: original xbox_present fails at line1372; d3d12_present passes.
  • Test-only successor 9e199612c5002723dfc2cb6c34f6069b9f11facd, exact parent 61243a201: corrected xbox_present passes, exit0, 3.230 seconds, zero VUID errors. The production libraries are unchanged.
  • The corrected executable compiles both original target sources, including shims/d3d12_xs_device.cpp with LEONIDA_VULKAN_DEVICE, and includes the original CRT object. A prior minimal probe omitted those dependencies and reached the proxy-only stub; its failure is excluded from production evidence.

The generic queue-family diagnostic covers all presenter initialization failures, so the old run did not record the exact failing Vulkan call or VkResult. Source lifetimes and this controlled fixture change support the overlap diagnosis. This is synthetic test evidence only; no title data is included.

The complete reviewed source preserves the exact merge ancestry. The test-only commit has parent 61243a201a3a0e03da8a8889a16d3266ac8a463c; the integration starts at the previously published f9ff7614 source on GitHub main 867d922f.

Proposed test-only diff follows.

diff --git a/runtime/test/xbox_present_test.cpp b/runtime/test/xbox_present_test.cpp
index 58a62cbd8..7027e5305 100644
--- a/runtime/test/xbox_present_test.cpp
+++ b/runtime/test/xbox_present_test.cpp
@@ -14,6 +14,7 @@
 #include "xbox_present_readback_size.h"
 #include "../shims/d3d12_com.h"
 #include "../shims/d3d12_xs_device.h"
+#include "../shims/title_window.h"
 #include "../shims/xbox_present.h"
 
 #include <algorithm>
@@ -279,6 +280,7 @@ int main() {
     ShowWindow(window, SW_SHOWNOACTIVATE);
     UpdateWindow(window);
     pump();
+    HWND route_window = nullptr;  // remains alive until after the console device and its presenter are released
 
     {
         // The real runtime's instance path (create_vulkan_device_standalone, not the test device): it must enable
@@ -1356,6 +1358,20 @@ int main() {
         }
         EXPECT(real.PresentX(0, nullptr, nullptr) == S_OK);  // the shutdown blank
 
+        // `real` still owns a swapchain for `window`. The console route must borrow an unoccupied title window,
+        // not create a second non-retired swapchain for the same HWND. Keep the borrowed window outside this
+        // device scope so its lifetime extends past the proxy-owned present state.
+        ShowWindow(window, SW_HIDE);
+        route_window = CreateWindowExW(0, window_class.lpszClassName, L"Leonida: console PresentX route",
+                                       WS_OVERLAPPEDWINDOW, 100, 100, frame_rect.right - frame_rect.left,
+                                       frame_rect.bottom - frame_rect.top, nullptr, nullptr,
+                                       window_class.hInstance, nullptr);
+        EXPECT(route_window);
+        ShowWindow(route_window, SW_SHOWNOACTIVATE);
+        UpdateWindow(route_window);
+        pump();
+        EXPECT(rt::d3d12::find_title_window(GetCurrentProcessId()) == route_window);
+
         // Check the console queue route that creates its own presenter on the first PresentX. A fence
         // on slot 16 selects the newer queue layout, where PresentX is slot 32.
         using QueueSignalFn = HRESULT(WINAPI*)(void*, ID3D12Fence*, UINT64);
@@ -1378,6 +1394,9 @@ int main() {
         // The device owns this presenter. Retaining the proxy here would keep both alive forever.
         EXPECT(rt::d3d12_xs::device_proxy_ref_count(device.p) == device_refs);
     }
+    // The proxy has released its presenter and Vulkan surface, but it does not own the borrowed title HWND.
+    EXPECT(IsWindow(route_window));
+    EXPECT(DestroyWindow(route_window));
     DestroyWindow(window);
     UnregisterClassW(window_class.lpszClassName, window_class.hInstance);
 

@Cryptoteep

Copy link
Copy Markdown

checked: #859114856a9 | with: clean merge onto main@867d922f0, check_no_game_data.py | on: Windows 11 24H2, no game files | result: pass (merges cleanly onto main@867d922f0 with zero conflicts; diff review confirms find_title_window inspects window manager state via EnumWindows, GetWindowThreadProcessId, IsWindowVisible, GetClientRect, GW_OWNER, and WS_EX_TOOLWINDOW without sending cross-thread window messages; selects visible unowned largest-client-area window; runtime/test/title_window_test.cpp verifies selection and non-blocking behavior against an unpumped window thread; scripts/check_no_game_data.py clean).

Raxstreams added a commit to Raxstreams/Leonida that referenced this pull request Nov 23, 2026
shims: find_title_window, the title's own window that PresentX shows its frames in

Fork integration. AI-assisted (Codex).
@AAyberkU

Copy link
Copy Markdown
Contributor

An independent confirmation on Windows 11 / RTX 5060 Ti, with an integration stack of #2490 + #7685 + #7880 and the other boot PRs. I wrote the same fix locally before seeing this PR: EnumWindows for the largest visible, unowned top-level window of the process, with no GetWindowText. Presenting there instead of present_state_for's hidden window turned the title's window from white to the presented image.

To check the whole path, a forced magenta clear of the backbuffer fills the title's window every frame, at about 100 S_OK PresentX per second over 90 s runs. I'll switch my stack to this PR's find_title_window in place of my local copy.

AI-assisted (Claude Code).

@Roy-0406

Copy link
Copy Markdown

checked: #8591ec41c22 | with: merged into main@867d922f0, scripts\build_runtime.ps1 (MSVC 19.51, CMake 4.4.4 + Ninja 1.13.2, Vulkan SDK 1.4.363.0, RelWithDebInfo); ctest -R "^title_window$" 10 rounds, then the full ctest | on: native Windows 11 Pro 26200, AMD Radeon RX 7800 XT (RDNA3), AMD 26.11.2 (driver 32.0.32029.2010, LLPC, Vulkan 1.4.359), VK_LOADER_LAYERS_DISABLE=~implicit~, no game copy | result: pass. title_window passes 10/10. Full ctest: 260 passed, 5 failed of 276 (main's 275 plus title_window), the same five as main without the PR (vk_queue_query_sync, d3d12_vulkan_device, d3d12_texture_tiles, d3d12_depth_resolve, d3d12_pipeline_library_host; each has its own fix PR). | agent: yes

AI-assisted (Claude Code).

@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

checked: #8591 | with: clang-cl/lld cross-build (xwin SDK, Ninja, ccache) of title_window_test plus a throwaway probe title_probe (my own file, not part of the PR) that calls find_title_window on edge cases the PR test does not exercise | on: Wine on Linux (RunPod, headless X), main@6312c057f + #8591 (clean merge, no conflicts); not native Windows | result: title_window_test EXIT 0, prints title_window: ok. Probe, all as expected: two visible unowned windows with equal client area (292x166): the topmost one (GetTopWindow) is returned, so the "ties keep the topmost" comment in the header holds; a sole minimised window (IsIconic=1, client 0x0) is still returned (the best_area = -1 start value works); excluding the only window returns null. Observation, not a defect: a minimised title window (area 0) loses to any small visible normal window of the same process (probe: minimised big window + 100x80 normal window returned the small one). Fine if the title never minimises before its first PresentX; worth a comment if it can. Not covered: native Windows Z-order (Wine's EnumWindows order may differ), real retail window thread, the negative control already posted by others (SendMessageTimeoutW timeout) was not repeated. I also read the header against docs/: docs/ does not mention find_title_window or the window choice, so there is no doc claim to contradict; the header comment is the only description of the rule.

agent: yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.