Skip to content

fix(gpu): present to the title's window, not a pseudoconsole's 0x0 window - #2587

Open
MoonEyes wants to merge 4 commits into
ProjectLeonida:mainfrom
MoonEyes:port-nv-present-title-window
Open

MoonEyes wants to merge 4 commits into
ProjectLeonida:mainfrom
MoonEyes:port-nv-present-title-window

Conversation

@MoonEyes

@MoonEyes MoonEyes commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

Item: PORT-NV-PRESENT-TITLE-WINDOW
Claim: #2570 (comment)
Source: trustno1-coder/VIctory-NVIDIA 3f054e46 (GPL-3.0), author JRamos1993 (kept as commit author, Co-authored-by, and a row in the d3d12/README.md credit table).

When leonida.exe runs under a pseudoconsole, the process owns a 0x0 console window that can sit above the title's own window in the enumeration order, and the swap chain ended up presenting to it. The host now picks the window to present to with a pure function, pick_title_window: the window the title hooked (leonida.wndproc) wins, even while its client area is still empty; without one, the first window with a client area; a pseudoconsole's window alone is never chosen.

Tests (no game files): 4 new unit tests in leonida-gpu host.rs (the_titles_window_wins_over_a_pseudo_console_above_it, the_titles_window_counts_even_while_its_client_area_is_empty, without_one_of_the_titles_the_first_with_a_client_area, a_pseudo_console_alone_is_no_window_to_present_to) pass on this branch merged with main 5cb4592. descriptor::host::tests::views_off_their_elements_get_a_copy_of_their_bytes fails on main too and is unrelated. check_no_game_data.py is clean.

Not fixed here: a swap chain already made on the fallback window does not move to the title's window when it appears later. main does not move it either (see the comments); a follow-up of its own.

Not tested in the title: the selection is checked as a pure function only. Windows 11, RTX 4060 for the build; no GPU run.

AI-assisted (Claude Code, Claude Opus 5.5); reviewed by a separate agent before opening.

🤖 Generated with Claude Code

JRamos1993 and others added 2 commits November 28, 2026 20:37
…ndow

title_window() took the first visible top-level window of the process.
Started under a pseudoconsole (Windows Terminal, or a parent that has
one), the process also owns a visible PseudoConsoleWindow of 0x0 pixels;
when that one was above the title's window in z-order, the swap chain was
made for it and the title's window stayed white.

The title's window is now the one our window procedure hook marked
(leonida.wndproc); without one, the first visible window with a client
area. The choice is a pure function with unit tests.

Item: PORT-NV-PRESENT-TITLE-WINDOW
Claim: ProjectLeonida#2570 (comment)
Ported from trustno1-coder/VIctory-NVIDIA 3f054e46778d4348b312f9aef02c4772f15007d0
(paths crates/victory-* -> d3d12/crates/leonida-*, property victory.wndproc ->
leonida.wndproc; a plain cherry-pick -x cannot apply across the rename).

Co-authored-by: JRamos1993 <32806235+JRamos1993@users.noreply.github.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@MoonEyes

Copy link
Copy Markdown
Collaborator Author

@DadaCap could you review this one when you have a moment? It merges cleanly with main. The red checks are the three tests that fail on main 5cb4592 too, already claimed: test_valu_cvt_f16_int16_mods (translator f16), test_the_loader_gets_the_stack_reserve_cmake_gives_it (loader stack) and test_resolve_pools_deduplicates_macos_case_and_accents_and_keeps_case_sensitive (path dedup). None of them touches this diff. The ERROR: lines in those logs come from tests that pass.

AI-assisted (Claude Code, Claude Opus 5.5), posted for MoonEyes.

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 2757a1d7b (d3d12/crates/leonida-gpu/src/host.rs).

pick_title_window cleanly prefers a window tagged with the "leonida.wndproc" property over the first sized window and skips 0x0 windows such as PseudoConsoleWindow, preventing swapchain creation against a pseudoconsole HWND.

Local verification:

  • cargo test --manifest-path d3d12/Cargo.toml -p leonida-gpu --lib title_window_tests: 4/4 passed (a_pseudo_console_alone_is_no_window_to_present_to, the_titles_window_counts_even_while_its_client_area_is_empty, the_titles_window_wins_over_a_pseudo_console_above_it, without_one_of_the_titles_the_first_with_a_client_area).
  • python -I scripts/check_no_game_data.py: passed.

AI-assisted (Antigravity, Gemini).

@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Physical Hardware Verification — Windows 11 + NVIDIA GeForce RTX 3090 (610.88)

Verified head commit 2757a1d7bedba7fedba62284eb8daf9da9d0abeb cleanly merged (no-commit) onto origin/main (1bc055e6c2dedaa0de23ec79d8c4007c1dff1fb3; git merge-tree --write-tree rc=0).

  • Cross-PR merge note: #2587 merges cleanly with #2589, and has a trivial 1-line adjacent table-row conflict at the end of the settings table in d3d12/README.md with #2588 (git merge-tree rc=1 on d3d12/README.md only).

Red -> Green Verification

  • Red (origin/main 1bc055e6c2):
    • Compiling #2587's title_window_tests on origin/main fails (E0432: Candidate and pick_title_window absent).
    • Running a live Win32 EnumWindows probe creating a 64x64 visible process window marked with SetPropW(..., w!("leonida.wndproc"), ...) and a 0x0 WS_EX_TOPMOST visible unhooked window above it in z-order fails on origin/main: title_window() stops at the first visible window (picked == Some(pseudo) instead of Some(titled)).
  • Green (origin/main + 2757a1d7be):
    • cargo test --locked -p leonida-gpu --lib title_window -- --test-threads=4: 4/4 unit tests passed (the_titles_window_wins_over_a_pseudo_console_above_it, the_titles_window_counts_even_while_its_client_area_is_empty, without_one_of_the_titles_the_first_with_a_client_area, a_pseudo_console_alone_is_no_window_to_present_to), plus the live Win32 EnumWindows probe now selects Some(titled) over the topmost 0x0 unhooked window.
    • cargo test --locked -p leonida-hle --lib lifecycle -- --test-threads=4: 10/10 passed.
    • python -I tests/test_docs_encoding.py (1/1), python -I tests/test_docs_links.py (17/17), python -I tests/test_docs_env_vars.py (5/5), and python -I scripts/check_no_game_data.py d3d12/README.md d3d12/crates/leonida-gpu/src/host.rs d3d12/crates/leonida-hle/src/modules/lifecycle.rs (0 issues) all pass.

Code Review Notes

  • TITLE_HOOK (w!("leonida.wndproc")) matches TITLE_PROC in leonida_hle::modules::lifecycle, EnumWindows safely collects Candidate entries into stack-owned found: Vec<Candidate>, and fallback selection cleanly ignores 0x0 unhooked pseudoconsole windows while preserving non-empty unhooked windows for standalone host tests. No blockers found.

AI-assisted.

@TheGeogeo

Copy link
Copy Markdown
Contributor

Tested 2757a1d7b merged (no commit) with main aa918e5d9, Windows 11 10.0.26300, Rust 1.98.1 (NVIDIA GeForce RTX 3080, driver 621.40 in the machine; these are host-side unit tests).

  • Green: cargo test -p leonida-gpu --lib --locked: 519 passed, 0 failed, 46 ignored (plain main: 515 passed, 0 failed, 46 ignored). The 4 title_window_tests pass. On current main the 4 crate failures from the description are gone. The leonida-gpu test build gives 10 warnings, the same as on main.
  • Red: with pick_title_window reduced to main's rule (candidates.first(), the first visible window), all 4 tests fail (left: Some(1), right: Some(2) or None).
  • check_no_game_data.py is clean on the 3 files.
  • Not checked: the selection in the title (no game run).

No blocker from my side. AI-assisted (Claude Code).

@timbot103 timbot103 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static audit of 2757a1d7b against 48bae6cad. The picker skips a 0×0 pseudoconsole HWND. If it is the only candidate on the first present, the host can still cache a swapchain on its fallback window. Once the hooked title HWND appears, host.rs:11788 on the base prevents selection from running again. Please allow the presenter to move from a fallback HWND to the visible title HWND and cover the sequence with a synthetic window test that presents before and after the hook appears. The supplied Windows enumeration probe starts with the marked window already present.

AI-assisted.

@merzleckeier

Copy link
Copy Markdown
Contributor

I reproduced the late-window case from the review with real Win32 windows and the production D3D12 presenter. Tested 2757a1d7b merged on 3535eb5c4 under Wine 11.22 and VKD3D-Proton 71cae091 on an RTX 4070 SUPER (620.73.04) and Radeon 610M (RADV Mesa 26.3.1). The fixture checks IDXGISwapChain::GetHwnd after each present.

  • With the marked window present before the first frame, the chain used it over an unmarked normal window.
  • With only a visible 0×0 window initially, the first frame used a fallback swap chain. After a marked window appeared, the picker returned it, but subsequent presents stayed on the fallback, including after resizing the swap-chain buffers. The retarget assertion went red on both GPUs.
  • As a test-only control I waited for the outstanding work and cleared the cached presenter before the second present. Both GPUs then picked the marked window and kept it across resize. All four existing picker unit tests also passed.

The 0×0 HWND was a synthetic popup. The Gamescope headless wrapper occasionally exited during teardown after the Rust test had printed its result. The recorded comparisons come from the DXGI assertions during the test. AI-assisted with OpenAI Codex.

@enlight3d

Copy link
Copy Markdown
Contributor

RUN (independent run, #2587 head 2757a1d on main 878b024; merged locally with git merge --no-ff, no conflicts; also no conflict with #2566 head 7b5cb29 per git merge-tree --write-tree).

Commands, in d3d12/, target x86_64-pc-windows-msvc, toolchain 1.98.1:

  • cargo check --tests -p leonida-gpu -p leonida-hle: OK on the merged tree.
  • cargo test -p leonida-gpu -p leonida-hle: green. leonida-gpu 548 passed / 0 failed / 47 ignored (the 4 title_window_tests among them), leonida-hle 215 passed / 0 failed / 2 ignored, plus the other test binaries of both crates.
  • Red, behavioural: with pick_title_window reduced to the old behaviour (the first visible window), cargo test -p leonida-gpu --lib title_window gives 0 passed / 4 failed.
  • Red, build: with the PR's non-test changes reverted and the tests kept, the tests do not compile (E0432, Candidate and pick_title_window unresolved). That alone proves little, hence the behavioural run above.
  • Not covered by the tests: that EnumWindows/GetPropW see the hook's property on a real title window under a pseudoconsole (only the pure selection is tested).

No runtime/ C++ or Python in this PR, so no link or import check applies.
Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB, NVIDIA 621.40.

# Conflicts:
#	d3d12/README.md
@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Nov 29, 2026
@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

Verified by OreoFR

PR head 5f36be0d9 (a merge of main on top of the tested 2757a1d7b; the PR's own change is the same) merged with git merge --no-ff onto main 060c47160, no conflict. Windows 11 Pro 10.0.26300, RTX 4070 Laptop 8 GB, NVIDIA 621.40, Rust 1.98.1 (host-side unit tests, no GPU used). New here: a red at this head.

  • Crates: cargo test -p leonida-gpu -p leonida-hle --locked in d3d12/, own target dir.
  • Green: leonida-gpu 553 passed / 0 failed / 47 ignored (main 549/0/47), the 4 host::title_window_tests pass; leonida-hle 218 passed / 0 failed / 2 ignored (+1 +1 in the other test binaries), the same as main.
  • Red: the tests sit next to pick_title_window, a new function, so restoring host.rs to main does not compile. I kept the PR and replaced only the body of pick_title_window with main's rule (the first visible window: candidates.first()): all 4 title_window_tests fail (for example the pseudoconsole case picks Some(1) instead of Some(2)).
  • Clippy (--all-targets, leonida-gpu + leonida-hle): no new lint in the files the PR touches.
  • [integration] PageMirror, GPU detile, async queue & RDNA2->SPIR-V performance stack (INTEGRATION-PERF-CPU-GPU-R6) #2566 (eade9562c) rebase needed after this: no (git merge-tree is clean).
  • Not covered by these tests: timbot103's point about a swap chain already cached on a fallback window before the title's window appears. The unit tests check only which window is picked.

Verdict: READY (green, red seen). Ready to merge per the maintainers' plan.

This verification was AI-assisted (Claude Code, oreofr-agent).

…-window

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

Copy link
Copy Markdown
Contributor

Independent check on Windows (not the author), at 27bfe203b.

  • cargo test --locked -p leonida-gpu --lib (shared warm target): 567 passed, 0 failed, 52 ignored in 14.1 s.

The present-to-the-title's-window behaviour needs the game/pseudoconsole scenario; this is compile + regression coverage of the crate.

Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, NVIDIA RTX 4070 Ti SUPER (driver 621.01), Rust 1.98.1 pinned.

AI-assisted: yes (opencode).

@enlight3d

Copy link
Copy Markdown
Contributor

IN-GAME RUN (gate, independent): #2587 head 27bfe20, RTX 3090 24 GB, NVIDIA 621.40, Windows 11.

Setup: d3d12/scripts/build-nvidia.ps1 (diag profile, no DLSS files), scripts/play-d3d12.ps1 -SeriesS -NoDlss -NoFrameGen -Log, the same save restored before the run, standing still once in the world. Other jobs (cargo, a shader translation) ran on the PC at the same time, so the frame rates are not comparable with other runs.

Gate: no device loss in the 300 s warm-up (world at 92 s) or in the 420 s run (world at 72 s, so 348 s in the world; ended at the time limit). Median 10.4 fps (p5 2.4).

The late memory build-up that main shows on this card in most runs (list scratch kept in card memory while the release thread's queue grows) is present here too, at the same scale as on main (queue about 226,000, mapped scratch 8.1 GB at the end, VRAM peak 19.8 GB). It is not caused by this PR.

Not checked: Series X, walking, picture. The cargo part of this verification follows separately.

@enlight3d

Copy link
Copy Markdown
Contributor

RUN (independent run on Windows, #2587 head 27bfe20, tested as it is: it already has main d367675 merged in. The PR's own changes are line for line the same as on its earlier heads 5f36be0 and 2757a1d; only main's commits came in. The head still merges cleanly into main 8f41d7d. CI on 27bfe20: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests all pass.) This replaces my earlier RUN on 2757a1d.

Commands, in d3d12/, target x86_64-pc-windows-msvc, toolchain 1.98.1, all with --locked, at low priority:

  • cargo test -p leonida-gpu --no-fail-fast: green, 567 passed / 0 failed / 52 ignored. Main d367675 gives 563 / 0 / 52, so the 4 new title_window_tests pass.
  • cargo test -p leonida-hle --no-fail-fast: 220 / 0 / 2, as on main.
  • cargo clippy -p leonida-gpu -p leonida-hle --all-targets (without -D warnings, which already fails on main in leonida-shader and leonida-lua), on main and on the head: no new warning in host.rs or lifecycle.rs (3 already there, the same 3 on the head).
  • Red, behavioural: with pick_title_window put back to the old rule (the first visible window, whatever its size or mark) and the tests kept, cargo test -p leonida-gpu --lib title_window gives 0 passed / 4 failed. Each test gets window 1, the 0x0 one, where it expects the title's window 2, or no window at all.
  • Not covered by the tests: that EnumWindows/GetPropW see the hook's property on a real title window under a pseudoconsole. Only the pure selection is tested; the in-game check is the A/B agent's.

No runtime/ C++ or Python in this PR, so no link or import check applies.
Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB, NVIDIA 621.40.

@MoonEyes

MoonEyes commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator Author

@timbot103 @merzleckeier thanks, the point is right and it reproduces: once a swap chain is made on the fallback window, nothing moves it.

READ IN CODE, main 0d41b0a0c and head 27bfe203b:

  • The caching is on main too and this pull request does not change it: host.rs makes the presenter once, when presenter.is_none(), and never asks title_window() again (main host.rs:13233).
  • What changes for a process whose only visible window at the first present is the 0x0 one: on main the chain is made for that window and stays there, so nothing is ever shown. Here the picker returns None, the chain goes on a window of our own, and it stays there. Neither moves to the title's window later, as merzleckeier measured.
  • In the order this pull request is about (the title's window is already there, under a pseudoconsole's window), it picks the title's window, which main does not.
  • The shim marks a window inside CreateWindowExW/CreateWindowExA, before the title gets its handle back, so every window the title makes through them is marked. I did not measure whether the title can present before one is visible. In two of my runs on main (RTX 4060) the swap chain was made once per run; that says nothing about this order.

So I would keep this pull request to the choice of the window, and take the move in a follow-up of its own: it has to wait for the queue, drop the cached presenter with its overlays and the frame-generation swap chain, and remake it, which is a larger change on the present path than this one. merzleckeier's control (wait, drop, remake) is the right shape for its test. If a maintainer would rather have it here, say so and I will do it here.

Not fixed here, and said so in the description: the presenter does not move from the fallback window.

Posted by an AI agent (Claude Code) for MoonEyes.

@imShangLi

Copy link
Copy Markdown
Contributor

VERIFY-2587 — Linux, Fedora 45, Rust 1.98.1 (the pinned toolchain), Wine 11.21, no GPU device, no game files

Verdict: PASS. The window the swap chain is made for is now chosen by a rule that cannot pick the pseudoconsole's 0x0 window, the rule is a pure function with tests, and nothing that passed before fails now.

Head 27bfe203, base the current main 85ec1d33, merge base d367675c, 3 files, +81/−7: d3d12/crates/leonida-gpu/src/host.rs, d3d12/crates/leonida-hle/src/modules/lifecycle.rs, d3d12/README.md.

1. What it changes

The swap chain was made for the first visible top-level window of the process:

*(found.0 as *mut Option<HWND>) = Some(hwnd);
return false.into();

Started under a pseudoconsole (Windows Terminal, or any process that owns one), the process also owns a visible PseudoConsoleWindow of 0 x 0 pixels. When that one was above the title's window in z-order, the swap chain was made for it and the title's window stayed white — no frame ever shown.

The change collects every visible top-level window of the process with two facts about each — whether our own window-procedure hook marked it (leonida.wndproc, the property leonida_hle::modules::lifecycle puts on the windows the title makes), and whether its client area is not empty — and picks by a rule that is a pure function:

/// Which of `candidates` (in z-order, topmost first) is the title's: the
/// one our hook marked, else the first with a client area. Pure, for testing.
fn pick_title_window(candidates: &[Candidate]) -> Option<usize> {
    candidates.iter().find(|c| c.hooked)
        .or_else(|| candidates.iter().find(|c| c.sized))
        .map(|c| c.hwnd)
}

The hooked window wins even if it is below the pseudoconsole in z-order, which is the case that was broken; without a hooked window, a 0x0 window is skipped in favour of one with a client area.

2. What I checked

  • The rule is total. pick_title_window returns None for an empty list, the hooked one when there is one, and otherwise the first with a client area — no path returns a 0x0 window unless every window is 0x0 ✓.
  • The mark is the one the HLE module puts. TITLE_HOOK = "leonida.wndproc" is the property the lifecycle module sets on the windows the title makes; I checked that the string in the GPU crate is the same one the HLE side writes.
  • The enumeration still only looks at this process. The callback keeps the GetCurrentProcessId() check and IsWindowVisible ✓; the change adds facts to the list instead of taking the first window and stopping.
  • The README row says the picture goes to the title's own window and names the test; the test exists ✓.

3. What the tests do here

leonida-gpu and leonida-hle do not build on Linux (upstream: "The crates are Windows-only today … Linux follows #2577"), so I cross-compiled them for x86_64-pc-windows-gnu and ran the tests under Wine 11.21.

This pull request changes no test, so the red side cannot be built (there is nothing to take); the base and the head were compared instead:

the merge base 615 tests
the pull request's head 619 tests
the new tests, only on the head 4 — all pass
tests that pass on the base and fail on the head 0
failing on both sides 3 — the watch tests that need Microsoft's VirtualAlloc/VirtualQuery behaviour, which Wine does not have

4. What I did not check

  • The window enumeration itself. EnumWindows, GetClientRect and GetPropW need a real desktop with the title's window and a pseudoconsole; the pure part (the rule) is what I could run.
  • That the swap chain then shows the title's picture. That needs a GPU, a window and the game.
  • The leonida_hle::modules::lifecycle side (four lines): that the hook marks the title's window and not another one.
  • Windows and MSVC. Everything above is Linux with Wine 11.21 (staging) and no GPU device.

AI-assisted: MiniMax M3.1 Flash.

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.