Skip to content

D3D12.X: CopyIntoSubresource makes the blocks it wrote hot at once - #3437

Open
tiagonleal wants to merge 1 commit into
xcl-dev:mainfrom
tiagonleal:d3d12x/copy-into-makes-blocks-hot
Open

tiagonleal wants to merge 1 commit into
xcl-dev:mainfrom
tiagonleal:d3d12x/copy-into-makes-blocks-hot

Conversation

@tiagonleal

@tiagonleal tiagonleal commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Part of #3382. With the hot write-watch looks (the default), a look before ExecuteCommandLists asks the write watch only about the 2 MiB blocks written in the last 1.5 s (hot_for); the walk thread makes any other block hot when it finds a written page there, a walk pass later. A write into a block that was idle for longer, followed at once by a submit, reached the GPU one submit late.

CopyIntoSubresource is one such write, and it is XCL's own code, so it knows what it wrote. It now marks the blocks of the bytes it tiled hot with title_textures::mark_hot, which already did this for memory the title had just committed. The next look then takes those pages from the write watch, whose bits the copy's own stores set. Nothing is reset, so no page goes up twice and none is lost. The cost is one short lock and one map insert per 2 MiB block written.

This is what makes the test title's check a texture in title memory (XG layout, #44) follows CPU writes fail now and then in whole-suite runs and on CI: it rewrites a subresource with CopyIntoSubresource and submits at once.

What is in it

  • xg.rs: copy_into calls mark_hot on the bytes it wrote, for a 2D level and for a volume level
  • title_textures.rs: mark_hot is pub(crate); a #[cfg(test)] reader hot_block for the test
  • test hle::xg::tests::a_subresource_copy_makes_its_blocks_hot: a copy across a block boundary leaves both blocks hot
  • +47/-4, two files

Tested

  • Windows 11 Pro 10.0.26300, NVIDIA GeForce RTX 5070 Ti 16 GB (driver 617.42), AMD Ryzen 7 9800X3D, 32 GB RAM, rustc 1.100.0, debug
  • Forced timing, on the commit before the rebase onto main 0fe06eb0 (same diff): with a 2000 ms pause added in the test title before the rewrite (a local change, not in this PR), test_title_runs_under_the_debug_layer_with_dred run 5 times. Without the two mark_hot calls: 5 of 5 fail the check with subresource 2 row 0 differs from the title memory. With them: 5 of 5 pass.
  • a_subresource_copy_makes_its_blocks_hot fails without the two calls (xg.rs, the assert on both blocks) and passes with them.
  • cargo test -p xcl --lib hle::xg: 80 passed, 0 failed. cargo test -p xcl --lib title_textures -- --test-threads=4: 65 passed, 0 failed, 1 ignored.
  • cargo test --workspace --no-fail-fast -j 8 -- --test-threads=4 on head f4f2bff7: 2738 passed, 0 failed, 49 ignored (27 SKIPPED lines). main 0fe06eb0 on this machine: 2737 passed, 0 failed, 49 ignored. This is the second run on this head: in the first the lib test binary ended inside uma_driver::tests with coherent window could not be restored (the abort Tests: the mixed-window remake tests run in a child process #3187 is open for), before this PR's test had run.

Regression run
scripts/smoke.ps1 -Seconds 60 -Title 9cc6a39e (Newer Retail Game), release builds of both commits in clean trees, main 0fe06eb0 first, one run each, XCL_PREPARE=0, same DXIL cache. A warm-up run of main came before them and is not shown; a second run of main after them showed 1697 frames and a peak commit of 15302 MB.

main (0fe06eb0):

xcl-smoke v1
commit: 0fe06eb0f05459007ebfffc67bb9983acb3a832d
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1686 (PresentX 1686)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 14882 MB

This PR (f4f2bff7):

xcl-smoke v1
commit: f4f2bff7e828e486f45ec2eb4823a342d3b2ac08
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1692 (PresentX 1692)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15228 MB

Not tested

  • Raw stores by the title's own code into a block idle for more than 1.5 s still wait for the walk, as before. This PR does not close D3D12.X: a submit misses a CPU write into memory idle for 1.5 s (hot write-watch looks); intermittent e2e failure #3382 for them.
  • CopyFromSubresource is not touched: its destination is the title's linear buffer.
  • Command-list memory writes (fills, immediate writes, copies into title memory) were not audited for the same gap. DirectStorage, file reads and the kernelx page writes already tell the looks what they wrote through note_known_write.
  • No retail title was checked for a visible difference.
  • Not tested on AMD or Intel.

AI use

  • Claude Code with Claude Fable 5.2 and Anthropic subagents (Claude Opus 5.5, Claude Sonnet 5.5) helped developing.

🤖 Generated with Claude Code

With the hot looks on (the default), a look before ExecuteCommandLists asks
the write watch only about the 2 MiB blocks written in the last `hot_for`
(1.5 s); the walk thread makes another block hot when it finds a written page
there, a walk pass later. A write into a block idle for longer than that,
followed at once by a submit, reached the GPU one submit late. The test
title's "a texture in title memory (XG layout, xcl-dev#44) follows CPU writes"
check rewrites a subresource with CopyIntoSubresource and submits at once:
it failed now and then under load, and 5 of 5 runs with a 2 s pause before
the rewrite (xcl-dev#3382).

CopyIntoSubresource is XCL's own code, so it knows what it wrote: it now
marks the blocks of the bytes it tiled hot (`mark_hot`, made crate-visible),
and the next look takes them from the write watch, whose bits the copy's
stores set. Nothing is reset, so a page is not uploaded twice or lost; the
cost is one short lock and a map insert per block written. With the 2 s
pause, 5 of 5 runs pass.

Not covered: raw stores by title code still wait for the walk as before.
DirectStorage and the kernelx page writes already go through
note_known_write.

Part of xcl-dev#3382
@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Nov 29, 2026
@andrewDoing

Copy link
Copy Markdown
Contributor

On current head f4f2bff7, I checked the one-texel tail mip of a 1024×1024 RGBA8 texture and a 256×256×64 RGBA8 volume on a Ryzen 7 5700X3D, Windows 11 build 26300.10075, Rust 1.100.0, debug. The test used the actual CopyIntoSubresource vtable call against Windows MEM_WRITE_WATCH allocations after clearing their hot-block markers. Both cases round-tripped the four bytes. The first foreground look collected the exact page that Windows reported dirty, and the next native query found zero remaining dirty pages. No walk thread was needed.

Each copy touched one 4 KiB page. The current code marked the whole slice or volume hot. After touching all the allocation’s pages to make them resident, I alternated the stock range with a diagnostic control that retained only the actually written 2 MiB block. Both found the same page. The table gives per-process median scan times across four fresh processes, 96 samples per lane per process. Timing starts after the copy and includes choosing the hot ranges and taking the dirty pages.

Tail mip Hot blocks selected Range scanned Current median One-block control median
2D 3 5.375 MiB 6.5–8.1 µs 3.6–4.5 µs
3D volume 11 21.344 MiB 20.2 µs 4.1 µs

The one-block control only narrows this fixture’s known footprint. A footprint-aware implementation would need to preserve all touched blocks for larger and noncontiguous mips. The two existing test modules (hle::xg::tests and hle::d3d12x::title_textures::tests) also passed on the RTX 5070 (616.92), with 70 passed, zero failures, one ignored manual benchmark, and zero explicit skips. The timing probe measured CPU work only. Retail behavior and frame time remain unmeasured. Codex with GPT-6.2 Sol ran the test-only overlay and prepared the report.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

merge-check
base: 7d86c5d
head: f4f2bff
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2897 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read the whole diff (+47/-4). After an XG CopyIntoSubresource has tiled its bytes, the range it wrote is marked hot (mark_hot). The retail title in the run below calls XG CopyInto/FromSubresource (6-argument form), so the smoke reaches the change. Findings: (1) It marks more than the copy wrote. A 2D copy marks the whole array slice with every mip (xg.rs ~733, slice_bytes), and a volume copy marks the whole surface (~722, t.size()), but the copy writes only level i. So a title that streams one small mip of a large texture keeps every 2 MiB block of that slice hot for 1.5 s (hot_for), and all_hot() turns true for the texture. Nothing is missed, but the upload reads more than the copy wrote. MipLevel.offset would narrow it (the tail levels need care). (2) The test's destination (xg.rs ~831, align_up(ptr + 1, 2 MiB) - size / 2) is only inside the allocation when ptr sits at least size / 2 below a 2 MiB boundary. The Windows heap makes that true in practice, but nothing checks it. Fine: mark_hot runs after the stores and nothing resets the write watch, so no page is lost or uploaded twice. copy_into holds no lock and HOT is a leaf lock taken the way register and sparse.rs already take it. A destination that isn't a registered texture is harmless (hot_runs only uses blocks in watched ranges, stale entries expire). Before the first look and with XCL_WATCH_HOT=0 it does nothing, and the test skips. The test fails without the two calls.
performance: 60 s smoke of a retail title, main 7d86c5d vs this merge (32e188b8): 1663 -> 1672 frames shown (100 %; last presents per 2 s: 60 60 61), peak commit 17153 -> 17779 MB, 0 device removals, 0 crashes. The title calls XG CopyInto/FromSubresource in this run, so the changed path runs.
verdict: safe to merge (finding 1 is a follow-up worth doing: marking only the written level would keep the upload smaller)
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

@github-actions github-actions Bot added needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head f4f2bff

No other open or merged PR addresses #3382. mark_hot already exists on main for commits and sparse mappings; this adds it to xg.rs copy_into for both the 2D and the volume path, and makes it pub(crate) with a test-only reader. No problems found in the diff.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

@vasanex

vasanex commented Dec 2, 2026

Copy link
Copy Markdown

merge-check claim
head: f4f2bff

@vasanex

vasanex commented Dec 2, 2026

Copy link
Copy Markdown

merge-check
base: 1c8b551
head: f4f2bff
merge: clean (merged tree d92a7152, git diff --check clean)
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2910 passed, 0 failed, 51 ignored; SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 cannot disable the driver cache), the same set as on the base; failing: none. Base alone, same machine: 2909 passed, 0 failed, 51 ignored. The new test hle::xg::tests::a_subresource_copy_makes_its_blocks_hot passes.
review: whole diff read (xg.rs, title_textures.rs, +47/-4). No finding. Both exits of copy_into that write the destination (the volume path and the level path) now call title_textures::mark_hot on the bytes they tiled; mark_hot is the function main already uses for memory just committed, made pub(crate), and it returns at once when the hot looks are off. Nothing is reset in the write watch, so the pages are taken from it by the next look as usual. The test helper hot_block is #[cfg(test)] only. Not checked here: that the late upload this is for (#3382) is gone in a title.
performance: 60 s smoke of the same title, release, default settings, same machine, one run each. Base 1c8b551: 1608 frames shown, presents per 2 s (last 3) 60 60 61, 0 device removals, 0 crashes, peak commit 19911 MB. Merge result (local merge commit): 1496 frames shown, 60 60 60, 0 device removals, 0 crashes, peak commit 20052 MB. 93 % of the base's frames with the same rate at the end; four other merges on this base gave 1539 to 1624 in the same hour, so I read it as start-up spread of one run, not as an effect. Cost by default, reasoned: one short HOT lock and one map insert per 2 MiB block per CopyIntoSubresource call.
verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 3080 10 GB (driver 621.40), AMD Ryzen 9 7950X, Windows 11 Pro 26200, rustc 1.100.0, debug suite and release smoke / Claude Code with Claude Fable 5.2. Run and posted without a human check of each point.

@github-actions github-actions Bot added ready-to-merge Has 2 independent safe merge-checks on current head (#3041) and removed needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:hle kernelx, XGameRuntime, XAudio2, VFS, PE loader, TLS, EH gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) ready-to-merge Has 2 independent safe merge-checks on current head (#3041)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

D3D12.X: a submit misses a CPU write into memory idle for 1.5 s (hot write-watch looks); intermittent e2e failure

4 participants