Skip to content

KernelX: GPU-visible title memory outside the windows is write-watched (retail menu text complete) - #252

Merged
ivall merged 1 commit into
ivall:mainfrom
jessenic:fix/gpu-visible-write-watch
Nov 23, 2026
Merged

ivall merged 1 commit into
ivall:mainfrom
jessenic:fix/gpu-visible-write-watch

Conversation

@jessenic

Copy link
Copy Markdown
Contributor

What was wrong

In a retail game's menus the body text lost its x-height letters (a c e m n o r s u v w x z); letters with ascenders, descenders or dots and all capitals were fine.

XCL_DEBUG_MAGENTA with a colour per UI pixel shader showed two text paths: the letters that drew come from a shader with no texture (vector glyphs), and every missing letter from one shader that samples a single texture, a 2048x2048 A8_UNORM glyph atlas the CPU fills as text is laid out. That atlas is kept in step with the title's memory (d3d12x::title_textures). A GPU readback of it was all zeros while its CPU view held the glyphs.

The atlas lies in a 24 GB reservation of 64 KiB pages (VirtualAlloc flags 0x20402000). It does not fit a sparse window, so it fell through to plain memory without MEM_WRITE_WATCH, where GetWriteWatch sees nothing: the texture was uploaded once, empty, at its first submit and never again. A second allocation, 2.2 GB of large pages with a graphics protection (flags 0x20003000, protection 0x2404, too large for the coherent window), had the same problem. Together they held 128+ kept-in-step textures within the first 45 s.

Change

kernelx::VirtualAlloc's last fallback adds MEM_WRITE_WATCH for GPU-visible reservations (MEM_GRAPHICS, MEM_LARGE_PAGES / MEM_64K_PAGES, or a PAGE_GRAPHICS_* protection). It records them with the graphics reservations, whose ranges title_textures already watches and VirtualFree already forgets. Commits the title makes inside such a reservation later inherit the watch.

Relation to open PRs: #203 moves large-page reservations into sparse windows when they fit. This change covers what still falls through (the 24 GB reservation does not fit), so the two are complementary. #211 (merged) covered glyph caches that are render targets; this atlas is a plain texture.

Testing

  • New unit test gpu_visible_fallback_memory_is_write_watched: the three flag combinations land in watched memory, and a write is reported by GetWriteWatch.
  • cargo test --workspace: 652 passed, 0 failed, 40 ignored (RTX 5070 Ti, driver 32.0.16.1088).
  • Retail run on the same PC: the menus' body text is complete.

🤖 Generated with Claude Code

@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Review of 862a0d4. The change looks right to me, and its per-submit scan cost is small. Two things I'd fix or check: the test passes without reaching the new code, and writes into write-watched memory cost more than the scan.

1. The test does not reach the new branch here. In gpu_visible_fallback_memory_is_write_watched all three cases (0x20402000, 0x20003000 with 0x2404, MEM_GRAPHICS | 0x3000) land in a unified-memory window and take the continue. I added a print to that continue locally and ran the test on Windows 11 with an RTX 5070 Ti. The test passes on main for the same reason. To make it check the fallback, take the branch's condition out as a function and test that, or have the test fill the windows first.

2. The per-submit scan grows with the textures in the new ranges, not with the 24 GB.

  • Adding the reservations to GRAPHICS_RESERVATIONS only lets watch_ranges keep the kept-in-step textures inside them. Those are merged across gaps under 1 MiB and clipped to the regions, and each look scans only that.
  • Measured with GetWriteWatch(WRITE_WATCH_FLAG_RESET), run as refresh_once runs it (16,384-entry buffer, called again while full) on committed, resident, write-watched memory. Medians of 5–15 looks, Ryzen 9 9950X:
watched clean 1 page in 256 dirty 1 in 16 every page dirty
64 MiB 0.03 ms 0.03 ms 0.04 ms 0.22 ms
256 MiB 0.11 ms 0.13 ms 0.17 ms 1.38 ms
1 GiB 0.44 ms 0.51 ms 1.28 ms 13.5 ms
4 GiB 1.78 ms 2.09 ms 12.4 ms 105 ms

3. CPU writes into write-watched memory cost more than the scan.

  • After a reset (each look resets what it scans), the first CPU write to a page took 0.25–0.37 µs, against 0.007 µs unwatched: 4.0 ms against 0.11 ms for 64 MiB, 387 ms against 8.9 ms for 4 GiB.
  • The first write into freshly committed memory took 0.72 µs a page watched, against 0.27 µs unwatched. That is about 0.45 µs more a page, once.
  • Writes with no reset in between cost the same watched or not.
  • So in KernelX: GPU-visible title memory outside the windows is write-watched (retail menu text complete) #252's reservations every page the title writes pays about 0.45 µs once. Inside the scanned textures, a page the CPU writes again after each look pays 0.25–0.37 µs each time.
  • If the title maps physical pages into the 24 GB reservation, each 64 KiB page copied in pays about 16 × 0.45 µs ≈ 7 µs once. I have not shown that the reservation holding the physical pages is this one.

4. Merges.

An aside, not from #252: in refresh_once, a full buffer makes the next GetWriteWatch start again from the region's base. With every page dirty, a 4 GiB look took 105 ms that way, against 47 ms when each call starts after the last page reported.

Tested:

  • The write-watch numbers come from a standalone synthetic program (no XCL code, no game data), Windows 11 26200, Ryzen 9 9950X, run under a 12 GiB commit cap.
  • hle::kernelx tests on the merged tree above.
  • The test probe from point 1 (a local print only).

Not tested:

  • The game.
  • The full lib suite on the merged tree: only hle::kernelx ran.
  • AMD, Wine.
  • The real sizes of the textures in the new ranges, and how often the title writes them. The numbers above give the cost per GiB and per page, not the game's total.

🤖 Claude Opus 5.5

theaustinayers pushed a commit to theaustinayers/xcl that referenced this pull request Nov 23, 2026
…the CPU view (retail intro video, no green screen)

A retail game reserves 24 GB of 64 KiB pages twice in a row (VirtualAlloc flags
0x20402000): once PAGE_READWRITE, once PAGE_NOACCESS | PAGE_GRAPHICS_NOACCESS (0x801). It
maps physical pages only into the first (XMemMapPhysicalPages), yet places GPU resources at
the same offset in the second: its Bink video planes are decoded by the CPU into the first
view (BinkRegisterFrameBuffers), and the video shader samples R8 textures at those addresses
plus the reservation's size. On the console the second reservation is a GPU-only view of
the first; here it was separate, empty memory, so the video drew zero luma and chroma: a
green screen for ~15 s after the first A press, until the menus came.

kernelx::mirrors pairs such a no-access graphics reservation of 64 KiB pages with the most
recent CPU-readable one of the same size (forgotten when either is released).
title_textures keeps a texture placed in the GPU view, where nothing is mapped, in step with
the same offset in the CPU view: its bytes are read and its CPU writes watched there, while
its title address (GetGPUVirtualAddress, GPU copies into it) stays the GPU view's.

Needs the CPU view write-watched (the previous commit, PR ivall#252).

The 4K planes are now uploaded each video frame: 12.4 fps during the video against 17 fps
of green before (RTX 5070 Ti).

Test: gpu_view_reservations_name_their_cpu_view.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jessenic added a commit to jessenic/xcl that referenced this pull request Nov 23, 2026
… of PR ivall#252)

gpu_visible_fallback_memory_is_write_watched went through VirtualAlloc, where these sizes
land in a unified-memory window once any test has made the windows: it took its `continue`
and passed without the fix. The fallback's condition and reservation are now functions
(gpu_visible_fallback, reserve_watched) and the test calls them: the condition for the three
GPU-visible flag combinations and three plain ones, the reservation for write-watching
(GetWriteWatch reports the written page) and its bookkeeping. Without MEM_WRITE_WATCH the
test fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jessenic added a commit to jessenic/xcl that referenced this pull request Nov 23, 2026
…the CPU view (retail intro video, no green screen)

A retail game reserves 24 GB of 64 KiB pages twice in a row (VirtualAlloc flags
0x20402000): once PAGE_READWRITE, once PAGE_NOACCESS | PAGE_GRAPHICS_NOACCESS (0x801). It
maps physical pages only into the first (XMemMapPhysicalPages), yet places GPU resources at
the same offset in the second: its Bink video planes are decoded by the CPU into the first
view (BinkRegisterFrameBuffers), and the video shader samples R8 textures at those addresses
plus the reservation's size. On the console the second reservation is a GPU-only view of
the first; here it was separate, empty memory, so the video drew zero luma and chroma: a
green screen for ~15 s after the first A press, until the menus came.

kernelx::mirrors pairs such a no-access graphics reservation of 64 KiB pages with the most
recent CPU-readable one of the same size (forgotten when either is released).
title_textures keeps a texture placed in the GPU view, where nothing is mapped, in step with
the same offset in the CPU view: its bytes are read and its CPU writes watched there, while
its title address (GetGPUVirtualAddress, GPU copies into it) stays the GPU view's.

Needs the CPU view write-watched (the previous commit, PR ivall#252).

The 4K planes are now uploaded each video frame: 12.4 fps during the video against 17 fps
of green before (RTX 5070 Ti).

Test: gpu_view_reservations_name_their_cpu_view.

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

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review. Point by point:

1. Test not reaching the fallback: fixed in 065be41. The branch's condition and reservation are now functions (gpu_visible_fallback, reserve_watched), and gpu_visible_fallback_memory_is_write_watched calls them directly, so the unified-memory windows can't take the allocation first. It checks the condition for the three GPU-visible flag combinations and three plain ones (CPU-only reservation, a commit without a reservation, a fixed address), then that reserve_watched memory is watched (GetWriteWatch reports the written page) and recorded and forgotten with the graphics reservations. With MEM_WRITE_WATCH removed from reserve_watched, the test now fails; it passes with it.

2. The game's real numbers. main 7a033c8 against 7a033c8 + #252, same PC (RTX 5070 Ti, Ryzen 7 9700X), same script (XCL_PRESS_AT=45:A, 80 s), from the sparse windows timing: write-watch line (cumulative ms):

submits main + #252
256 98 ms 106 ms
512 233 ms 283 ms
1024 504 ms 1543 ms
2048 1009 ms 4300 ms
4096 2072 ms 8351 ms

That's about 0.5 ms a submit on main against 2.0–2.7 ms with #252 once the menus have loaded. Frames shown in the 80 s: 1297 against 1093.

I split the timed section with a temporary probe. Recomputing the ranges is negligible (58 ms over 2048 looks); GetWriteWatch itself is the cost. The watched ranges grow from 10 ranges / 0.4–0.8 GB (all committed) early on to 65–79 ranges after the menus. At your 0.43 ms/GiB that's roughly 5–6 GB of kept-in-step textures inside these reservations. So the cost scales with the textures, as you said, but in this game those textures are several GB: everything main was leaving stale. I'll look at cutting it in a follow-up rather than in this PR (for example, not rescanning textures nothing has written for a while, or scanning only textures bound since the last look).

3. Write cost. The reservation holding the physical pages is this one. A follow-up trace (#283) shows XMemMapPhysicalPages into the 24 GB 0x20402000 / PAGE_READWRITE reservation about 54,000 times in the first minute (64 KiB pages). At your ~7 µs a 64 KiB page, that's ~0.4 s once, spread over loading.

4. Merges: thanks, noted.

Aside (refresh_once restarting from the region's base on a full buffer): agreed, that's real and predates this PR. In this game the buffer never fills per look (about 30 dirty pages a submit), so it doesn't explain the numbers above. Worth its own small fix.

Tests: cargo test --workspace 652 passed, 0 failed, 40 ignored (RTX 5070 Ti).

🤖 Claude Opus 5.5

@simnJS

simnJS commented Nov 23, 2026

Copy link
Copy Markdown

Tested on Linux (Wine, NVIDIA): the menu text is still incomplete.

Tested commit: 862a0d4 (head of #252; its base 7a033c8 is 4 commits behind main 9ab7d76, none touching crates/). Compared with main at 9ab7d76, same machine, same protocol.

Tested

  • Ubuntu 26.04.1, RTX 4080 SUPER (NVIDIA 595.99.02), Ryzen 9 7950X3D. Proton Experimental 11.0 Wine (not Windows), vkd3d-proton v3.1 (patched as in Linux: docs/linux.md, scripts/linux and Wine start-up checks for vkd3d-proton and DXVK (#154, #241) #257), DXVK 3.1.1, DXC 1.9.2609. Rust 1.99.1, release, x86_64-pc-windows-gnu.
  • cargo build --release --locked --target x86_64-pc-windows-gnu -p xcl: builds.
  • The retail game, XCL_NETWORK=0 XCL_ACTIVITY=1 XCL_FRAME_DUMP=120, one run per build, the account prompt answered with a single A by hand: starts, 130 s without a crash, 60 presents per 2 s (30 fps), draws per 2 s 360 → 1952 (main, same protocol: 360 → 1920). No new WARN / ERROR line against main (same 64 distinct lines, 75 total).
  • Frame dumps: black → green (the intro video, solid (0,135,0)) → the "Brightness Level" screen, mean (65,65,65). The text under the "Brightness Level" title is still missing its lowercase letters without ascenders ("us e va ue un e s m o s / are v s e."), the same as main at 9ab7d76, which reaches the same screen with the same gaps in the same run protocol.
  • So here KernelX: GPU-visible title memory outside the windows is write-watched (retail menu text complete) #252 changes nothing I can see in the menu text. I did not trace whether the new write-watched VirtualAlloc path is taken in this run (no XCL_TRACE_* set), so I cannot say whether the allocation never reaches it under Wine or whether the glyph atlas has another cause here.

Not tested

  • Windows, AMD (including RDNA4). The test suite (cargo test --workspace) and scripts/smoke.ps1 were not run (the crate's tests run on Windows).
  • Runs are 70–130 s; nothing past the first menus. The press time of A was not recorded. One of the two main runs stayed on the purple loading screen to the end (A not registered or not pressed in time), so a single run per build is a thin sample.

AI use

  • Written with an AI coding agent from real runs (it built and launched each version; a person pressed A), then checked by the author. Logs, frame dumps and the script that reads them can be shared on request.

…u text)

A retail game reserves 24 GB of 64 KiB pages (VirtualAlloc flags 0x20402000) and commits
2.2 GB of large pages with a graphics protection (flags 0x20003000, protection 0x2404).
Neither fits a sparse or the coherent window, so both fell through to plain memory without
MEM_WRITE_WATCH. Textures the title places there are kept in step with the memory
(d3d12x::title_textures), which relies on GetWriteWatch to see CPU writes: they were
uploaded once, empty, and never again (128+ textures in the first 45 s).

One of them is the game's 2048x2048 A8_UNORM glyph atlas, which the CPU fills as text is
laid out. Its GPU copy stayed zero, so every glyph drawn from it (the x-height letters:
a c e m n o r s u v w x z) was missing from the menus; letters drawn by the vector path
(ascenders, descenders, capitals) were fine. Found by a GPU readback of the atlas (zero)
against its CPU view (glyphs) while the menu was on screen.

The fallback VirtualAlloc now reserves GPU-visible memory (gpu_visible_fallback:
MEM_GRAPHICS, MEM_LARGE_PAGES / MEM_64K_PAGES, or a PAGE_GRAPHICS_* protection) with
MEM_WRITE_WATCH and records it with the graphics reservations (reserve_watched), whose
ranges title_textures watches.

Cost, measured on the game (RTX 5070 Ti): the write-watch scan goes from ~0.5 ms to
2.0-2.7 ms a submit once the menus have loaded, ~5-6 GB of kept-in-step textures now
being scanned; 1093 frames in 80 s against 1297.

Test: gpu_visible_fallback_memory_is_write_watched calls the fallback directly (through
VirtualAlloc the unified-memory windows take these sizes when they have room); it fails
without MEM_WRITE_WATCH.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jessenic
jessenic force-pushed the fix/gpu-visible-write-watch branch from 065be41 to e93dc9c Compare November 23, 2026 22:47
josilo added a commit to josilo/xcl that referenced this pull request Nov 23, 2026
@simnJS

simnJS commented Nov 23, 2026

Copy link
Copy Markdown

Tested on Linux (Wine, NVIDIA): the menu text is still incomplete, and the glyph caches are not in the memory #252 changes.

Tested commit: 065be41 (head of #252; base 7a033c8, 4 commits behind main 0e946cf, none touching crates/). Compared with main at 0e946cf. This adds to my earlier report on 862a0d4.

Tested

  • Ubuntu 26.04, RTX 4080 SUPER (NVIDIA 595.99.02), Ryzen 9 7950X3D. Proton Experimental 11.0 Wine (not Windows), vkd3d-proton v3.1 (patched as in Linux: docs/linux.md, scripts/linux and Wine start-up checks for vkd3d-proton and DXVK (#154, #241) #257), DXVK 3.1.1, DXC 1.9.2609. Rust 1.99.1, release, x86_64-pc-windows-gnu.
  • cargo build --release --locked --target x86_64-pc-windows-gnu -p xcl: builds.
  • The retail game, XCL_NETWORK=0 XCL_ACTIVITY=1 XCL_FRAME_DUMP=120 XCL_TRACE_MEM=400, 70 s, A pressed once by hand. 065be41 reaches the "Brightness Level" screen (mean (65,65,65), frames 860–1220); the text under the title still reads "us e va ue un e s m o s / are v s e." – the same letters missing as on main. 60 presents per 2 s (30 fps), no crash, no new WARN / ERROR (57 distinct lines on both).
  • Is KernelX: GPU-visible title memory outside the windows is write-watched (retail menu text complete) #252's path taken? Yes, for the 24 GB reservations, no for the glyph caches. XCL_TRACE_MEM shows VirtualAlloc (GPU-visible, write-watched) for: 0x600000000 flags 0x20402000 protect 0x4 → 0x7ffff0000 and protect 0x801 → 0xdffff0000; 0x185c00000 flags 0x20402000 protect 0x2004 → 0x13ffff0000; 0xd4ac0000 flags 0x20402000 protect 0x2404 → 0x1585bf0000; and three of flags 0x20003000. On main the same calls are plain VirtualAlloc at the same addresses.
  • But the allocations with flags 0x20003000 and protect 0x2404 / 0x22004 (0x49400000, 0x89700000 = 2.2 GB, 0x23300000; 3 928 MB together) are served by VirtualAlloc (graphics, coherent window), before the fallback, on both main and 065be41: Wine's run has a 4095 MB coherent window (UMA: … 4095 MB coherent window at 0x400000000) and they fit in it. Only a fourth, 0x25400000 (protect 0x22004), found no room and reached KernelX: GPU-visible title memory outside the windows is write-watched (retail menu text complete) #252's path. Where the window is smaller (less free commit, uma::coherent_hint) the 2.2 GB request would reach it – I did not test that.
  • Where the glyph caches are (temporary logging, not in the PR): 71 small two-plane targets (0x12000–0x82000 bytes, the "glyph cache the CPU fills" of title_textures) – all in that coherent window at 0x700000000: VirtualQuery says allocation base 0x700000000, allocation protection 0x4, state MEM_COMMIT, type MEM_PRIVATE; the window is already MEM_WRITE_WATCH (uma.rs) and is in watch_regions(). So KernelX: GPU-visible title memory outside the windows is write-watched (retail menu text complete) #252 changes nothing for them here, which fits the identical picture.
  • What Wine returns (probes run from reserve_watched's first call, temporary): VirtualAlloc(MEM_RESERVE | MEM_WRITE_WATCH) of 4 MB → a block, VirtualQuery: state MEM_RESERVE (0x2000), type MEM_PRIVATE, allocation protection 0x4. GetWriteWatch on it: return 0, 0 pages, granularity 0x1000 (not 64 KiB). After a 64 KiB MEM_COMMIT inside it and writes to three pages: return 0, exactly those 3 pages; with the reset flag the next call returns 0 pages, then 1 page after one more write. MEM_RESERVE | MEM_COMMIT | MEM_WRITE_WATCH in one call behaves the same. The console's raw flags 0x20402000 | MEM_WRITE_WATCH are refused by Wine (NULL, GetLastError 87) – sanitize_alloc_type strips them first, as in the PR. GetWriteWatch never failed in the 70 s run (0 failures).
  • Does GetWriteWatch see the glyph caches' writes? Yes, at the start: three looks between 24.9 s and 25.4 s see a written page in 1, then 69, then 1 of the 71 caches (17 942 – 22 337 dirty pages in all). After that, none in the remaining 45 s – though much more text is drawn then, with letters missing: the "Brightness Level" sentence, and in the run with logging the long photosensitivity-warning page (frame 1220, about 60 s) where most lines lack many letters. In KernelX / title textures: a GPU view of the title's CPU memory reads the CPU view (retail intro video, no green screen) #283's logged run no glyph cache was marked GPU-written either (no GPU copy into them seen). So the missing letters are not a CPU write that the watch missed; the caches' content after 25 s is not the CPU writes this path watches. I did not find what writes them.
  • Probable cause in one sentence: on Wine the atlas lives in the coherent window, which was already write-watched on main, so KernelX: GPU-visible title memory outside the windows is write-watched (retail menu text complete) #252's write-watched fallback is never reached for it and the missing letters come from something else (not found).

Not tested

AI use

  • Written with an AI coding agent from real runs (a person pressed A), then checked by the author.

@jessenic

Copy link
Copy Markdown
Contributor Author

@simnJS thanks for testing on Wine, and for tracing where the allocations go there. One correction about which texture to look at: the menu's missing letters don't come from the 71 small two-plane targets (title_textures' "glyph cache the CPU fills" log, #211). In my runs those stay all zero on both the CPU and the GPU side while the text is on screen.

What draws the missing x-height letters on Windows here:

  • XCL_DEBUG_MAGENTA=b40d9ca24fe3d23d:ff0000,f618f478d0c51397:00ffff colours the two text shaders. Red (b40d9…, no texture bound) draws the letters that are present. Cyan (f618f…) draws exactly the missing ones.
  • f618f… samples one SRV: a 2048×2048 A8_UNORM, one mip, single plane texture kept in step with title memory (here at shared-heap index 204; that index may differ on your build).
  • That texture's CPU view held the glyphs (a, c, e, m, n, o, r, s, u… in its top rows) while its GPU copy was all zeros. It lay in the 24 GB 0x20402000 reservation, uploaded once at creation and never again. With KernelX: GPU-visible title memory outside the windows is write-watched (retail menu text complete) #252 it's re-uploaded and the text is complete here.

So on Wine the useful checks are: where that A8_UNORM 2048×2048 texture lies (title_textures::register's va, VirtualQuery of it), whether it's in a range watch_ranges keeps, and whether GetWriteWatch reports its pages after ~25 s. If it sits in the coherent window under Wine, #252 indeed changes nothing there, and the cause would be elsewhere.

🤖 Claude Opus 5.5

@ivall
ivall merged commit e93dc9c into ivall:main Nov 23, 2026
1 of 2 checks passed
forbesfields pushed a commit to forbesfields/xcl that referenced this pull request Nov 23, 2026
Handle 16-byte constant-buffer records, upload intro textures from their CPU view, and decode BC1 BCPack streams. Share bounded texture pages, batch upload staging, and tolerate pages decommitted during streaming reads.

Includes work from ivall/xcl ivall#208, ivall#244, ivall#252, ivall#254, ivall#269 and ivall#283. DirectStorage late destination pages are already in the base.

Assisted-by: OpenAI Codex (gpt-6.2-sol)
theaustinayers added a commit to theaustinayers/xcl that referenced this pull request Nov 23, 2026
The fork, rebuilt on current upstream main with only what upstream does not have: the launcher's folder picker, click-to-capture mouse handling with F8 and Page Up / Page Down, a heap per DXC compile thread, the limit on waits for pipelines still being built, the installer, build.ps1, BUILDING.md, the release workflow and the README. Everything else the fork carried is in upstream now (ivall#195 in parts, ivall#211, ivall#244, ivall#252, ivall#283, ivall#289) or was a diagnostic.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.

4 participants