Skip to content

D3D12.X: a ray-tracing build's scratch gets window tiles of its own - #3522

Open
Lakanya wants to merge 2 commits into
xcl-dev:mainfrom
Lakanya:fix/bvh-builder-scratch-memory
Open

Lakanya wants to merge 2 commits into
xcl-dev:mainfrom
Lakanya:fix/bvh-builder-scratch-memory

Conversation

@Lakanya

@Lakanya Lakanya commented Nov 30, 2026 •

Copy link
Copy Markdown

Stacked on #3521; only the top commit (5abd576b) belongs here.

The second half of what XboxBvhBuilder.dll needs under XCL_SPARSE_LAZY=1. The title maps physical pages over the scratch of its builds whose bytes XCL keeps at another address, so the scratch address stays reserved: no commit lists its tiles and no CPU view links them, and neither will_write (#3521) nor the mirror path gives them video memory. In a retail title, 256 of 256 builds checked had no video memory under their scratch when their submission ran, so the builder's passes read zeros back from it. Part of #505.

What is in it (one commit, 5abd576b; updated after review and rebased on #3521's new head)

  • crates/xcl/src/hle/d3d12x/sparse.rs: will_write_unbacked, asked like will_write and taken by the next submit. The range's window tiles with no entry and no CPU view under them get video memory of their own (State::give_own_tiles, through ensure_mapped, cleared if reused). The pages the CPU has not committed are holes, so nothing goes up or comes back there. Tiles already listed are left to will_write.
    • A tile that already shows a CPU view not linked yet gets no tile of its own: it is asked for as a mirror in the same submit (take_asks in map_mirrors), so what the CPU wrote there stays visible.
    • A tile of its own that comes to show a CPU view later (a page mapped there: remap_mirrors, or an ask: take_asks) gives its tile up (yield_own, parked and taken back by new_mirror) and is linked over it.
    • When tiles of their own find no video memory, the asks are kept and the submit returns RetryRequired (give_own_asks), so a build never runs over scratch with nothing behind it. After the bounded retries of title_textures::refresh_for the title is stopped with that error, as for any other failed preparation.
  • crates/xcl/src/hle/d3d12x/bvh_builder.rs: a build also gives its scratch, for the size the builder's own prebuild query gives. A PERFORM_UPDATE build gives the UpdateScratchDataSizeInBytes of the ALLOW_UPDATE build it updates (kept from that build's query, carried through clones and compactions, and found for in-place updates); not known, the larger of the two scratch sizes the update's own query gives. Every range the hook gives goes through both asks.
  • Tests: an_unbacked_gpu_write_gets_tiles_of_its_own; own_tiles_that_find_no_memory_hold_the_submit_back (NO_TILES: refused, asked again, refused again, then given and the submit goes on); a_scratch_write_never_hides_a_cpu_view_not_linked_yet (the first mirror ask and a partial scratch write in one submit; the CPU's bytes on the GPU after that submit and after a later remap); an_own_tile_gives_way_to_a_page_mapped_there_later; an_update_writes_the_update_scratch_of_the_structure_it_updates (both size orderings across a 64 KiB tile boundary, in place, of a copy, unknown).

Tested

  • NVIDIA GeForce RTX 5070 Laptop GPU 8 GB (driver 621.40), AMD Ryzen AI 7 350, Windows 11 Pro build 26300.10075, Rust 1.100.0. Tests in debug, the regression runs in release. XCL_DIRECT_DXIL, XCL_HEAP_WINDOWS, XCL_HEAP_FLAT and XCL_SPARSE_LAZY unset for the tests.
  • cargo build --workspace, then cargo test --workspace --no-fail-fast -- --test-threads=4 on 5abd576b: the 41 integration harnesses and binaries 148 passed, 0 failed, 20 ignored. The library harness ends early (0x80004005) on this PR and on main 9dfbe3ea alike: a test's title reaches end_title ("sparse upload preparation still failed after bounded retries"). With --test-threads=1 it ends during bind::tests::gpu_two_pass_indirect_commands_keep_their_own_records, after the tests before it (that test passes in hle::d3d12x::bind alone). The RetryRequired this PR adds is taken only when XCL_SPARSE_LAZY=1 asks for tiles, which no test outside the new ones does.
  • So the library groups this PR touches, each in its own process, cargo test -p xcl --lib <filters> -- --test-threads=4:
    • hle::d3d12x::sparse hle::d3d12x::bvh_builder: 150 passed, 0 failed, 1 ignored, none skipped (145 on D3D12.X: XboxBvhBuilder builds give their destinations video memory #3521).
    • hle::d3d12x::bind: 116 passed, 0 failed, 0 ignored.
    • hle::d3d12x::list_x: 113 passed, 0 failed, 0 ignored (the same on main 9dfbe3ea).
    • The rest of the library (--skip hle::d3d12x::bind --skip hle::d3d12x::list_x) ends at the same end_title on this PR and on main, so it has no count; gpu_direct_image_get_resinfo_d16 fails in it on both.
  • Each new test fails without its fix (each put back to the old code, then run): the update scratch test with the build's own scratch size, the NO_TILES test with the asks dropped, the give-way test with remap_mirrors skipping every tile of the window's own, and the CPU-view test with both the check in give_own_tiles and the one in take_asks removed (with only the first removed it still passed, because take_asks links the tile in the same submit; the test now also checks before the submit that no tile of its own was made, which was not run against that removal).
  • SKIPPED on this adapter before the library harness ended: 22 "no wave64", heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows). None is a test of this PR.

Regression run
scripts/smoke.ps1 -Game <retail title> -Seconds 600, the release build of each commit, the same environment for both: XCL_DEVICE_TYPE=lockhart XCL_DIRECT_DXIL=1 XCL_DXC_THREADS=6 XCL_SPARSE_LAZY=1 XCL_SMALL_TEXTURES=1 XCL_TEXTURE_LEVEL_PAGES=1 XCL_COHERENT_HEAPS=sparse:1024 XCL_TWO_PASS_BATCH=1 XCL_TWO_PASS_MB=1536 XCL_HEAP_WINDOWS=1 XCL_UMA_WINDOWS=16 XCL_HEAP_FLAT=1 XCL_DEPTH_UAV_TWIN=1 XCL_FRAME_DUMP=300 XCL_DIRECT_COMPACT=0, scripted A presses (XCL_PRESS_A_AT), both run on 2026-12-01 (this PR's first, with the translation cache cold for this switch).

Why XCL_DIRECT_COMPACT=0: without it, this head's runs (twice) and 7737e074 (the first revision rebased on #3521's new head, without this revision's fixes) had the device removed (0x887a0006) at 120–132 s, around frame 1900, with no DRED breadcrumbs. Those runs reach frame ~1900 with 25–31 presents per 2 s, further and faster than any earlier run of this title here, and so reach a compute program main builds in the direct DXIL compact form (cs 95cc13288a27a96e: 20,965 blocks, 226,680 B DXIL). #3261 let that program take the compact form; the removal it then causes on NVIDIA at the first world frame is what #3516 (open) fixes, and XCL_DIRECT_COMPACT=0 avoids on main. #3521's own run builds the same program at 150 s and ends its 600 s without a removal. With the compact form off, this head ran 600 s with no removal or crash.

main:

xcl-smoke v1
commit: 9dfbe3ea6653a9c76ef3ea0d66187a4b5407483b
gpu: NVIDIA GeForce RTX 5070 Laptop GPU
title: 2c508222
seconds: 600
exit: 0x0
frames shown: 1875 (PresentX 1875)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 33282 MB

this PR's head:

xcl-smoke v1
commit: 5abd576bc772444ad8d3a2365143fb6db6142831
gpu: NVIDIA GeForce RTX 5070 Laptop GPU
title: 2c508222
seconds: 600
exit: 0x0
frames shown: 1859 (PresentX 1859)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 33033 MB

Not tested

  • That the structures are correct on this commit: the per-build checks are lab instrumentation and were not run on it.
  • That the lighting is better: the frames of the two runs are not compared.
  • The regression runs do not check the change itself, and may not reach it: XCL logs nothing per build. In the lab the scratch tiles showed no CPU view (tile_shows gave NoView), so the CPU-view paths are covered by the tests only.
  • A full library count (see Tested).
  • The title with the compact form on (the default): this head's runs end in the device removal described under Regression run until Recompiler: programs whose compact table needs more than 12,288 words take the plain form again #3516 or an equivalent lands.
  • Both regression runs stop presenting about 2 minutes in (the last frame at 112 s on this head, 121 s on main) and run on to 600 s: video memory is ~5 GB over this 8 GB card's budget there, on main as well.
  • RehydrateRaytracingAccelerationStructureBatched2X is not hooked (D3D12.X: XboxBvhBuilder builds give their destinations video memory #3521).
  • Without XCL_SPARSE_LAZY=1 nothing changes and that path was not run.
  • AMD, other NVIDIA cards, wave64 GPUs.

Follow-ups (from the reviews, left out of this PR to keep it under ~400 lines)

  • Tiles of their own are never given back when the title releases or unmaps the address and no CPU view comes there: memory grows with every distinct scratch address, and a later commit there may show old scratch bytes. It needs a release signal (decommit/unmap of a reserved range). In the lab the builder reused the same scratch.
  • A tile linked as a mirror instead of getting a tile of its own gets its memory within the mirror path's per-submit fill budget, like any GPU-view tile: a scratch write there in a submit that runs out of budget is not backed (not seen in the lab, where the scratch had no view).

AI use

  • Claude Code (Claude Opus 5.5): found the cause with lab instrumentation, wrote the change and its tests, addressed the review (CPU views not linked yet, RetryRequired when no video memory, update scratch sizes), ran the tests and the regression runs, and wrote this description.

🤖 Generated with Claude Code

@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 area:memory Virtual/physical memory, arenas, VRAM budget, UMA gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks) labels Nov 30, 2026
@thehollowedone

Copy link
Copy Markdown
Contributor

Source review of current head b46e9e33. These three paths need focused regressions before the scratch allocation can be relied on:

  1. A first scratch mapping can hide an existing CPU view. before_execute_locked processes the new own-tile requests before resolving queued mirror requests. If a GPU-view tile already points to a committed CPU tile but has no cached mirror link yet, ensure_mapped can create a private src == 0 tile. The later mirror lookup skips already mapped tiles, and remapping skips src == 0 entries. A partial scratch write can therefore lose visibility of neighboring CPU bytes. Check the actual CPU view before assigning an own tile, and let an own tile yield when a real CPU view is mapped later. A regression should queue the first mirror request and a partial scratch write in one submit, then verify a CPU-written marker survives that submit and a later remap.

  2. Tile allocation failure can let an unbacked build submit. ensure_mapped breaks if take_tile() fails, after the new request has been drained. With a fresh reserved scratch tile and no unrelated dirty pages or clears, preparation remains Ready. Keep the unmet request and return RetryRequired before submitting the build. The existing NO_TILES test injection can check failure, recovery, and resubmission of the same recorded list.

  3. Update builds use the initial-build scratch size. build_writes always prepares ScratchDataSizeInBytes. The DXR prebuild contract defines a separate UpdateScratchDataSizeInBytes, obtained for the original ALLOW_UPDATE build and used for subsequent updates. If the sizes differ, this can leave a required update tile unbacked or map an unnecessary neighbor. Retain that update requirement from the original prebuild query, select it when PERFORM_UPDATE is set, and test both size orderings across a 64 KiB tile boundary. Merely reading the update field from a query made with PERFORM_UPDATE may return zero, so the original requirement matters.

The #3521 review covers different destination-copy and postbuild-output paths. These findings are from source and the documented API contract; I did not run a build, GPU test, or title on #3522. AI use: Codex (GPT-6.2 Sol) checked the exact submit paths and contract; Codex (GPT-6.2 Astra) independently traced the mapping and allocation lifetimes.

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

merge-check
base: 7d86c5d
head: b46e9e3
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2899 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 (bvh_builder.rs, sparse.rs +62; stacked on #3521). Nothing changes by default (only the hook, behind XCL_SPARSE_LAZY=1, calls will_write_unbacked), so the run above doesn't reach it. Findings: (1) it can permanently hide a GPU view's memory (from reading, high confidence in the mechanism): give_own_tiles -> ensure_mapped (~8160, before map_mirrors / take_asks ~7756) only skips tiles already linked to the title's memory (mirror_src), not tiles that have CPU memory behind them but aren't linked yet (first use, request still queued, or parked after their memory was handed back). Such a tile becomes one of XCL's own; take_asks (~6766) then treats it as having no view and drops its request, and remap_mirrors (~6850) skips src == 0 tiles, so it never shows the title's memory again. Destinations and copies take the same path via give(). (2) These tiles are never given back (~3844, ~7014): nothing decommits them and remaps skip them, so memory grows with every distinct scratch address and survives release and reuse of the address, and a later commit there shows old scratch bytes where the title expects zeros (the PR lists this as not checked). (3) Compact copies get the source's maximum size (~120) and PERFORM_UPDATE builds use ScratchDataSizeInBytes instead of UpdateScratchDataSizeInBytes (~108), so reserved-only tiles past the title's allocation get permanent tiles. (4) When take_tile() returns None, the rest of the request is dropped without a log (~3877). (5) The test calls give_own_tiles directly; nothing covers the OWN_ASKS submit path or a tile with CPU memory behind it. Fine: the new tiles are mapped on the sparse queue and the fence is signalled before the title queue's wait at the same submit, tiles reused from the pool are cleared, and pages never committed are skipped for uploads and copies back.
performance: no smoke run (the default path doesn't reach this change); not measured.
verdict: not yet: own-tile backing can permanently hide a GPU view's memory for tiles with CPU memory behind them that aren't linked yet (finding 1), and those tiles are never given back (finding 2); nothing changes without XCL_SPARSE_LAZY=1
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.

NathanOPUS and others added 2 commits December 1, 2026 10:53
With XCL_SPARSE_LAZY=1 a tile the title commits has no video memory
until the CPU's first bytes go up to it, or a placed buffer or a copy
names it. XboxBvhBuilder.dll writes its acceleration structures with
compute programs through the sparse windows, which none of those see:
in a retail title the destination tiles of its builds and of every
compaction copy had none, so the structures read as zeros and rays hit
nothing.

With the switch, the builder's batched and single build exports and its
copy export are hooked when the title resolves them. Each build asks the
builder's own GetRaytracingAccelerationStructurePrebuildInfo for the
most it writes (XCL's prebuild_size underestimates some top levels) and
gives that range to sparse::will_write, which maps it before the list
runs; a clone or compacted copy writes as much as its source's build
or the copy that made it, and a destination whose new size is unknown
forgets its old one. A build's postbuild infos get the bytes of their
type at their own DestBuffer, which can be on a tile of its own. The
prebuild query is made before the size map's lock is taken.
Without XCL_SPARSE_LAZY=1 nothing is hooked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The title maps physical pages over the scratch of XboxBvhBuilder's
builds whose bytes XCL keeps at another address, so the scratch address
stays reserved: no commit lists its tiles and no CPU view links them.
With XCL_SPARSE_LAZY=1 the builder's scratch writes went nowhere in 256
of 256 builds checked in a retail title, and its passes read zeros back.

sparse::will_write_unbacked asks, like will_write, for the next submit:
the range's window tiles with no entry and no CPU view under them get
video memory of their own, the pages the CPU has not committed holes
(nothing goes up or comes back there). The builder hook asks it for the
scratch range the builder's own prebuild query gives, and for its
destinations and copies too (tiles already listed are left to
will_write).

A tile that already shows a CPU view not linked yet is not given a tile
of its own: it is asked for as a mirror in the same submit, so the CPU's
bytes there stay visible; a tile of its own that comes to show a CPU
view later (a page mapped there) gives its tile up and is linked over
it. When tiles of its own find no video memory the asks are kept and
the submit returns RetryRequired, so a build never runs over scratch
with nothing behind it. A PERFORM_UPDATE build's scratch is the
UpdateScratchDataSizeInBytes of the ALLOW_UPDATE build (or copy) it
updates, kept from that build's query; not known, the larger of the two
scratch sizes the update's own query gives.

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

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Triage (#1607) – head b46e9e3

Stacked on #3521; only b46e9e3 belongs here. The 117 shared added lines are #3521's commit, so this is not a duplicate. No other open PR gives unbacked scratch tiles video memory.
sparse.rs:will_write_unbacked / State::give_own_tiles follow the will_write pattern (asked lock-free, taken at the next submit). No problems found in the PR's own commit.
Recommendation: keep open.

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

@Lakanya
Lakanya force-pushed the fix/bvh-builder-scratch-memory branch from b46e9e3 to 5abd576 Compare December 1, 2026 10:24
@github-actions github-actions Bot added the needs-pr-check Failing the PR description check (CONTRIBUTING.md) label Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Re-review (head 5abd576; diff from b46e9e3, which I merge-checked, read). Not a merge-check: PR description is red until the smoke pair is rerun on this head, and no full suite has run on it yet.

  • Fixed:
    • Own tiles hiding a CPU view. give_own_tiles checks tile_shows and asks for a link instead (sparse.rs ~3861-3863), and an own tile gives way to a CPU view that appears later (yield_own ~3886, ~6815-6821, ~6908).
    • Update scratch sizes. A PERFORM_UPDATE build uses the saved UpdateScratchDataSizeInBytes (bvh_builder.rs ~123-133).
    • take_tile() failures. These are no longer silent: the tiles stay asked for and the submit returns RetryRequired (~7644-7656, ~8234-8240).
  • Still open: own tiles are never given back. Only yield_own removes them, and no decommit or release path frees them. A later commit at that address shows the old scratch bytes instead of zeros, because the existing entry isn't added to to_zero (~5865-5885). The own set is never cleared on commit or decommit either. Unverified edge case: a title commits over an own tile in a GPU view, a page is then mapped there, and yield_own parks the title's own memory.
  • New behaviour to state in the description. When video memory runs out, the title now stops with end_title(0x80004005) after refresh_for's three quick retries (title_textures.rs ~2052-2063). That's better than a silently wrong build, but the message is the generic "sparse upload preparation..." one and doesn't mention the scratch.
  • Unverified: whether a pool tile parked by yield_own while a build that uses it is still running on the GPU is safe.
  • give_own_asks is tested directly, but the before_execute_locked path still isn't. "What is in it" doesn't describe the new code, and still names b46e9e33 as the top commit.

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

@Lakanya

Lakanya commented Dec 1, 2026

Copy link
Copy Markdown
Author

Thanks both. Updated to 5abd576b (rebased on #3521's 2e2d9320, review fixes squashed into the one commit):

  1. A first scratch mapping hiding a CPU view (thehollowedone 1, Foniass 1): give_own_tiles now checks what the tile shows first. One over a CPU view not linked yet gets no tile of its own; it is asked for as a mirror in the same submit. A tile of its own that comes to show a CPU view later (a remap, or an ask) gives its tile up and is linked over it. a_scratch_write_never_hides_a_cpu_view_not_linked_yet queues the first mirror ask and a partial scratch write in one submit and checks the CPU's bytes after that submit and after a later remap; an_own_tile_gives_way_to_a_page_mapped_there_later.
  2. Allocation failure letting an unbacked build submit (thehollowedone 2, Foniass 4): the tiles that find none stay asked for and the submit returns RetryRequired; after the bounded retries the title stops with that error instead of running the build over nothing. own_tiles_that_find_no_memory_hold_the_submit_back uses NO_TILES for failure, retry and recovery (through the submit's helper, not a real recorded list).
  3. Update builds' scratch size (thehollowedone 3, Foniass 3): a PERFORM_UPDATE build gives the UpdateScratchDataSizeInBytes kept from the ALLOW_UPDATE build it updates (also through clones/compactions and in place); not known, the larger of the two sizes its own query gives. an_update_writes_the_update_scratch_of_the_structure_it_updates checks both orderings across a 64 KiB boundary.

Each new test fails with its fix reverted (the CPU-view one with both checks removed). Left as follow-ups, listed in the description: giving tiles of their own back when the address is released (Foniass 2; needs a release signal), and mirror tiles being bound by the per-submit fill budget.

Tests and a new xcl-smoke pair are in the description; the commit is now +352/−29. The pair is run with XCL_DIRECT_COMPACT=0 on both sides: without it this head reaches, at ~130 s, the compute program whose compact form hangs NVIDIA (#3261 / #3516), and the device was removed there.

AI use: Claude Code (Claude Opus 5.5) made the changes, ran the tests and the regression run, and wrote this reply.

@github-actions github-actions Bot added needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU needs:nvidia-test Needs a test report or merge-check on an NVIDIA GPU and removed needs-pr-check Failing the PR description check (CONTRIBUTING.md) labels Dec 1, 2026
@AAsobu

AAsobu commented Dec 1, 2026 •

Copy link
Copy Markdown

merge-check
base: b2b1f10
head: 5abd576
merge: clean (brings #3521's commit 2e2d9320 with it, as the PR is stacked on it)
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2912 passed, 0 failed, 51 ignored; SKIPPED: 28 (22 no wave64, 2 no heap windows, 3 no D3D12 debug layer / d3d12SDKLayers.dll, 1 no GPU_UPLOAD heaps); failing: none. The 4 bvh_builder tests and the 5 new sparse tests ran and pass (none skipped: tiled resources tier 2, reserved buffers and unified memory are all present here).
review: read the whole diff (+642/-6 with #3521's part: bvh_builder.rs, sparse.rs, resolver.rs, mod.rs). Without XCL_SPARSE_LAZY=1 the resolver hook returns the original export and will_write_unbacked returns at once, so the default path is unchanged. With it, the three build/copy exports of XboxBvhBuilder.dll are wrapped and call the module's own prebuild query (no stream) before the builder records; the round-1 points (a scratch tile over a CPU view not linked yet, a failed allocation letting the build run over nothing, the update scratch size) are each addressed with a test as the author lists. Open, as the description says: tiles of their own are never given back when the address is released (Foniass's finding 2), so under XCL_SPARSE_LAZY=1 they can accumulate over a long run; the opt-in path only.

  • Added 2026-12-02: two later reviews of this head (bmjask, Decodere) list new findings, all in the opt-in path. I checked these in the code of this merge; I have not reproduced them at run time:
    • A shortage stops the title: before_execute_locked returns RetryRequired from give_own_asks (sparse.rs:8236) before before_execute_on_with (:8252), whose maintenance (land_finished_readbacks, flush_parked_with, drain_quarantined_with, retire_free_pools_with, :7825-7851) could free tiles. The caller tries 3 times with only yield_now between (title_textures.rs:2053-2060), then stops the title. So a temporary lack of video memory for a scratch tile is fatal.
    • own is cleared only in yield_own (self.own.remove at sparse.rs:3889 is the only one). A tile that is decommitted, or committed by the title, stays in own, and a later CPU view calls yield_own on what is now an ordinary committed tile and parks it.
    • Not checked: the global OWN_ASKS served by whichever queue submits next, a scratch tile with a CPU view only queued in asks, the double tile_shows in remap_mirrors, and yield_own recycling a tile without a fence wait.
  • The default path is still untouched: without XCL_SPARSE_LAZY=1, will_write_unbacked returns at once, OWN_ASKS stays empty and give_own_asks returns true. My four runs with the switch hit none of the above in 160-190 s in the world each.
    performance: retail title, in the world, every run with XCL_SPARSE_LAZY=1. Release builds of main b2b1f109 (A) and this merge (B), runs A, B, A, B of 300 s, xcl.exe with the xcl run: xcl nvprofile puts xcl.exe in an NVIDIA profile with a bigger shader heap (#2403) #2590 profile at 2048 MB, the same env as my D3D12.X: a new texture leaves the undefined layout from its creation state #3527 check today (XCL_PREPARE=0 XCL_DIRECT_DXIL=1 XCL_DXC_THREADS=4 XCL_NETWORK=0 XCL_DRED=1 XCL_ACTIVITY=1 XCL_PSO_WAIT_MS=1000 XCL_TEXTURE_LEVEL_PAGES=1 XCL_HUGE_PSO_CONCURRENCY=4 XCL_HUGE_PSO_WANTED_CONCURRENCY=4, start prompt by XCL_PRESS_A_AT, no input), and the default XCL_DIRECT_COMPACT. The log of each B run shows the batched build and copy exports wrapped. From world entry + 30 s: presents per 2 s A 1.03 / 1.05, B 0.83 / 1.09 (B1 was the first run of a new binary and still building driver pipelines); draws per 2 s A 3745 / 4041, B 2966 / 4288; peak process commit A 42,746 / 43,122 MB, B 40,970 / 41,711 MB; peak VRAM A 17,142 / 17,324 MB, B 16,200 / 17,135 MB; 0 device removals in all four (world entry at 109-137 s, so each run spent 160-190 s in the world without the hang the PR's smoke pair avoids with XCL_DIRECT_COMPACT=0). On screen (the user watched all four): in B1 one city shot came out sharp and coloured where A1 had it washed out, but B2 did not repeat it, so I do not count it as a change.
    verdict: not yet: the default path is untouched, but the opt-in path has new findings I confirmed in the code (a temporary video-memory shortage for an own scratch tile stops the title, because the retry returns before the maintenance that could free tiles; own is never cleared on decommit or commit); XCL_SPARSE_LAZY=1 should not be offered with them open. It carries D3D12.X: XboxBvhBuilder builds give their destinations video memory #3521's commit, so that one should be merged with it or first
    hardware / tool and model: NVIDIA GeForce RTX 3090 24 GB (driver 621.24, wave32 only), Intel Core i5-10600K, 32 GB, Windows 11 Home build 26300, rustc 1.100.0 / Claude Code (Claude Opus 5.5), results checked by me

@github-actions github-actions Bot added needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) and removed needs-1st-check Passes gates; needs first independent merge-check (#3041) needs:nvidia-test Needs a test report or merge-check on an NVIDIA GPU labels Dec 1, 2026
@Decodere

Decodere commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Diff review (head 5abd576bc, read-only code review — not a test report):

Good progress on the round-1 fixes (tile_shows check in give_own_tiles, RetryRequired on unmet own-tile allocations, and UpdateScratchDataSizeInBytes tracking in bvh_builder.rs), and the default path remains untouched without XCL_SPARSE_LAZY=1. Reading the full sparse.rs diff turned up 2 lifetime/synchronization issues in the opt-in self.own path:

  1. Stale self.own membership and unzeroed scratch bytes across commit_range_with_protection / decommit_range (crates/xcl/src/hle/d3d12x/sparse.rs:3860, :3869, :3887, :5836-5887, :5891-5941, :6816-6822, :6908):
    give_own_tiles inserts t into self.own (sparse.rs:3869) and MIRROR_ASKED (sparse.rs:3860), and yield_own (sparse.rs:3887) is the only place self.own.remove is called:

    • Commit over an own tile (commit_range_with_protection, sparse.rs:5836-5887): both ensure_listed (sparse.rs:3775) and ensure_mapped (sparse.rs:3915) skip t because self.mapped.contains_key(&t) is already true. Lines 5868-5879 then clear entry.holes bits without adding (t, TILE) to self.to_zero (exposing stale GPU scratch bytes on newly committed pages instead of zeros) and without removing t from self.own.
    • Decommit + recommit (decommit_range, sparse.rs:5891-5941): decommit_range removes t from self.mapped (sparse.rs:5925) and parks its pool tile (sparse.rs:5933), but never removes t from self.own.
      In both cases, a subsequent regular committed tile at t still has self.own.contains(&t) == true and e.src == 0, so take_asks (sparse.rs:6816-6821) or remap_mirrors (sparse.rs:6908) seeing Under::Cpu(_) calls yield_own(t) and removes/parks the title's live committed tile. Clearing t from self.own in decommit_range and in commit_range_with_protection (and queueing (t, TILE) into self.to_zero when committing over an own tile) closes both paths.
  2. yield_own parks and immediately recycles an own tile without waiting for in-flight GPU scratch writes (crates/xcl/src/hle/d3d12x/sparse.rs:3886-3899, :6664-6666, :6701, :7829-7845, :8024, :8036):
    When remap_mirrors (sparse.rs:6908, called at :7829) or take_asks (sparse.rs:6820, called inside map_mirrors at :7830) calls yield_own(w), line 3896 inserts (w, e.pool) into self.parked with no GPU fence wait or quarantine. In the same submit, new_mirror (sparse.rs:6664-6666, :6701) can immediately pop (w, e.pool) back out of self.parked (bypassing flush_parked_with / quarantine_tiles_for at :7845, :6115) and schedule upload_zeros (:8024) + upload_dirty (:8036) onto the sparse copy queue (self.queue), which does not wait on earlier title-queue submissions. If a previously submitted acceleration-structure build is still writing scratch to w on a title queue when w transitions to a CPU view, the clear/fill races directly with the in-flight GPU build.

AI use: Antigravity (Gemini 4 Neon) audited the full diff against main and drafted this review.

@bmjask bmjask 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.

Review of 5abd576b: 6 finding(s). 6 are inline.


These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.

let asks = std::mem::take(&mut *WRITE_ASKS.lock().unwrap_or_else(|e| e.into_inner()));
s.give_memory(&asks);
}
if !give_own_asks(s, &OWN_ASKS, &OWN_ASKING) {

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.

give_own_asks returns RetryRequired before the submit's maintenance in before_execute_on_with (land_finished_readbacks, flush_parked_with, drain_quarantined_with, retire_free_pools_with), so a retry never frees the video memory it's waiting for. When VRAM is short (take_tile/CreateHeap fails for one scratch tile while parked or quarantined tiles could be freed), refresh_for retries before_execute 3 times with only yield_now in between. Each try returns here before any parked or quarantined tile is released, so all three fail identically and the title is stopped. A temporary shortage becomes fatal. Running that maintenance before give_own_asks, or between retries, would let it recover.

// `remapped`)
MIRROR_ASKED.insert(t, ());
if matches!(tile_shows(t), Under::Cpu(_)) {
self.asks.push(t);

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.

A scratch tile that shows a CPU view is only pushed to self.asks, which may not be served this submit, yet give_own_asks still reports success, so the build runs over a tile with no video memory. That happens when map_mirrors returns before take_asks (non-empty later, or the fill budget used up), when take_asks takes only MIRROR_ASKS_MAX asks at a time, or when fill_later gives no memory because the CPU tile's holes are 0xFFFF. The builder's scratch writes go nowhere and its reads return zeros, despite the PR's promise that a build never runs over scratch with nothing behind it. A linked view tile that was refused (mirror_cap reached, or no memory) and has no mapped entry is also skipped at ~3857 (mirror_src) and not covered by will_write.


/// Window tile `w`'s video memory of its own (`give_own_tiles`) is parked, to be taken back
/// when it is linked over the CPU view it now shows (`new_mirror`). False: it has none.
fn yield_own(&mut self, w: usize) -> bool {

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.

The own set is only cleared by yield_own. It isn't cleared when the tile leaves mapped (decommit_tiles drops an all-holes own tile on any decommit touching it), or when the CPU commits pages there (commit_range makes it an ordinary committed tile). Suppose own tile T is given, then the title commits pages at T (or decommits and recommits it). T is now a normal committed tile with src == 0 but still in own. A later remap or ask that finds a CPU view at T calls yield_own, which removes the entry, its runs and its to_zero pieces and parks the tile, so the committed pages' GPU bytes and tracking are lost. Before this PR such a tile was never handed to a view.

(holes, !holes)
});
if self.mapped.contains_key(&t) {
self.own.insert(t);

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.

Own tiles have no release path and no cap. They're given back only when a CPU view appears, aren't counted against mirror_cap, and remap_mirrors skips them when the page is unmapped (the Nothing arm). A title that moves its BVH scratch between addresses over time keeps 64 KiB of VRAM per old scratch tile forever, so the pools grow until take_tile fails. That then hits the fatal RetryRequired path above and starves mirror tiles. Releasing own tiles that no recorded build references (e.g. when the build's list is retired) would bound it.

/// build's scratch): that address stays reserved, so no commit gives its tiles an entry and no
/// CPU view links them. With `lazy_commits`, such tiles get video memory of their own at the next
/// submit, before the list's (`State::give_own_tiles`). Never waits for the state.
pub(super) fn will_write_unbacked(va: usize, len: usize) {

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.

OWN_ASKS is one global list, filled at record time and taken by whichever queue submits next. So the RetryRequired can land on an unrelated queue's submit: if a build is recorded on a compute list and a copy or present submit on another queue drains OWN_ASKS first and can't get tiles, that unrelated submit fails 3 times and the title is stopped. The reverse also happens: a bundle or list executed again after its own tile was yielded or decommitted runs over scratch with no memory, because its ask was used up at the first submit. Attaching the asks to the recording, and serving them when that list is executed, would fix both.

if self.mapped.get(&g).is_some_and(|e| e.src == 0) {
// (a tile of the window's own is not a view's – but one given for a GPU write where
// nothing showed a CPU view gives way to one shown now)
if self.mapped.get(&g).is_some_and(|e| e.src == 0) && !(self.own.contains(&g) && matches!(tile_shows(g), Under::Cpu(_)) && self.yield_own(g)) {

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.

Not certain about this one: remap_mirrors calls tile_shows twice, once inside the yield_own condition and again for now, and the view can change in between. If another thread unmaps the page between the two calls, yield_own has already parked the tile and removed it from own. Then now is Nothing with linked None, so the loop continues without linking and without giving the memory back, and the next scratch write over T goes nowhere until a new ask.

@github-actions github-actions Bot 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
@kole07139

Copy link
Copy Markdown

merge-check claim
head: 5abd576

@kole07139

Copy link
Copy Markdown

merge-check
base: 1c8b551
head: 5abd576
merge: clean (brings #3521's commit 2e2d932 with it)
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2919 passed, 0 failed, 51 ignored; SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 driver cache needs Developer Mode; the same set as main). The library harness ran to its end; the PR's 10 tests (bvh_builder and the new sparse ones) passed, none skipped. Failing: none.
review: read #3522's own commit (+352/-29) in this merge; #3521's part I checked on #3521 this morning. With XCL_SPARSE_LAZY unset nothing changes (will_write_unbacked returns at once, so give_own_asks is a no-op). With it, I agree with AAsobu's updated check and confirm these review points of this head in the code of this merge:

  • (1) A shortage of video memory for a scratch tile is fatal, not retried.
    • give_own_asks returns RetryRequired (sparse.rs:8236-8241) before before_execute_on_with (:8252) and the maintenance in it that could free tiles.
    • refresh_for tries 3 times with only yield_now between (title_textures.rs:2052-2061), then prepare_title_submission stops the title; mapped_after_wait stops on the first (list_x.rs:3620).
    • The description states the stop. But "the submit is tried again" can't help as written: a temporary shortage, such as tiles still cooling, in quarantine or parked, ends the run. D3D12.X: XboxBvhBuilder builds give their destinations video memory #3521's give_memory lets the title go on in the same situation.
    • This is from the code. I didn't reach it at run time: in a 600 s run of this title on this card, "no video memory for more tiles" appeared only after the device had been removed.
  • (2) OWN_ASKS is one global list (sparse.rs:7627) taken by whichever queue submits next (:8236), so that stop can land on an unrelated queue's submit (bmjask).
  • (3) own is cleared only in yield_own (sparse.rs:3889), not on decommit or commit (Decodere, AAsobu).
  • Retail title with XCL_SPARSE_LAZY=1: the hooks are installed (Batched2X and Copy). As a counting probe on the same hooks showed on D3D12.X: XboxBvhBuilder builds give their destinations video memory #3521, the title makes no build before world load, which this card doesn't reach. So none of the opt-in path ran here.
  • A possible fix for (1): take the own-tile asks after the submit's maintenance, or retry them through the existing SubmitStep::Wait / Restart path, and fall back as give_memory does (log once) rather than stop. Keeping the asks per queue would cover (2).
    performance: none by default. Retail title, main 1c8b551 vs this merge (1842da70), 60 s each, 0 device removals and 0 crashes:
  • defaults: 1563 -> 1572 frames, peak commit 18614 -> 18654 MB;
  • XCL_SPARSE_LAZY=1: 1594 -> 1593 frames, 17236 -> 19119 MB.
    verdict: not yet: with XCL_SPARSE_LAZY=1 a video-memory shortage for a build's scratch stops the title (the retry returns before the maintenance that could free tiles), the asks are global across queues, and own is not cleared on decommit/commit; these are open review points of this head, confirmed here in the code. The default path is unchanged.
    hardware / tool and model: NVIDIA GeForce RTX 3070 8 GB (driver 32.0.16.2140), Intel Core i9-9900K, Windows 11 Pro N 26200 / Claude Code with Claude Opus 5.5 (ran the merge, build, suite and smoke runs, checked the code, and wrote this check)

AI-written; posted without a human check of each point.

@bmjask

bmjask commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.

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 area:memory Virtual/physical memory, arenas, VRAM budget, UMA gpu:both Code affects both AMD and NVIDIA GPUs (scope tag; see needs:*-test for remaining checks)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants