D3D12.X: XboxBvhBuilder builds give their destinations video memory #3521
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3521"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by Lakanya on 2026-11-30T11:45:59Z.
With
XCL_SPARSE_LAZY=1a tile the title commits gets 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 what the builder wrote went nowhere: the structures read as zeros and rays hit nothing. This gives those ranges video memory before the builder records its passes, through the existingsparse::will_write(the same path a list's memory writes take). Part of #505.What is in it (one commit,
2e2d9320; updated after review)crates/xcl/src/hle/d3d12x/bvh_builder.rs(new): withXCL_SPARSE_LAZY=1, the builder'sBuildRaytracingAccelerationStructureBatched2X,BuildRaytracingAccelerationStructureandCopyRaytracingAccelerationStructureexports are hooked when the title resolves them. Each build asks the builder's ownGetRaytracingAccelerationStructurePrebuildInfofor the most it writes (bvh::prebuild_sizeunderestimates some top levels: 0x198400 against the builder's 0x227000 for one with flags 0x4) and hands the destination range tosparse::will_write. The query is made before the size map's lock is taken.DestBufferbacked for their type's size (8 bytes, 16 for serialization), which can be on a tile of its own.crates/xcl/src/resolver.rs: exports resolved from a mapped module go throughbvh_builder::intercept(it returns the original address for every other module and export).crates/xcl/src/hle/d3d12x/sparse.rs:lazy_askedispub(super); one GPU test.a_build_and_its_copies_write_the_builders_own_result_size(now with chained copies),a_reused_destination_never_keeps_a_stale_size,postbuild_infos_write_their_own_buffers,the_build_and_copy_exports_are_hooked; GPU:a_postbuild_result_on_a_tile_of_its_own_gets_memory(a compacted-size result on a separate lazy tile gets that tile's memory, the tiles of the destination and between are left alone).This is one of two fixes the builder needs under
XCL_SPARSE_LAZY=1; the build scratch, which no commit lists at all, is the stacked PR after this one (#3522). #1319 (merged) fixed the third cause found on the way.Tested
XCL_DIRECT_DXIL,XCL_HEAP_WINDOWS,XCL_HEAP_FLATandXCL_SPARSE_LAZYunset for the tests.cargo build --workspace, thencargo test --workspace --no-fail-fast -- --test-threads=4on2e2d9320: the 41 integration harnesses and binaries 148 passed, 0 failed, 20 ignored. The library harness ends early (0x80004005) on this PR and onmain9dfbe3eaalike: a test's title reachesend_title("sparse upload preparation still failed after bounded retries"). It does with--test-threads=1too (checked on #3522's head: duringbind::tests::gpu_two_pass_indirect_commands_keep_their_own_records, after the tests before it; that test passes inhle::d3d12x::bindalone).cargo test -p xcl --lib <filters> -- --test-threads=4:hle::d3d12x::sparse hle::d3d12x::bvh_builder: 145 passed, 0 failed, 1 ignored, none skipped.hle::d3d12x::bind: 116 passed, 0 failed, 0 ignored. In 8 runs of it on this commit one had 1 failure (not identified; 3 of 3 on0744aa01passed); it reaches none of this code.hle::d3d12x::list_x: 113 passed, 0 failed, 0 ignored (the same onmain9dfbe3ea).--skip hle::d3d12x::bind --skip hle::d3d12x::list_x) ends at the sameend_titleon this PR and onmain, so it has no count;gpu_direct_image_get_resinfo_d16fails in it on both.copy_writes' old logic put back,a_build_and_its_copies_write_the_builders_own_result_sizeanda_reused_destination_never_keeps_a_stale_sizefail. The reviewer's chained-copy assertion is in the first.SKIPPEDon 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, scripted A presses (XCL_PRESS_A_AT). Themainblock is the one from the first revision of this PR (2026-11-30); this PR's was run again on its new head (2026-12-01).main:
this PR's head:
Not tested
sparse::State::give_memorywith the ranges this PR computes, not through the hook with a real builder.RehydrateRaytracingAccelerationStructureBatched2X(structures loaded compressed from the title's files) is not hooked: its destinations may still have no video memory.XCL_SPARSE_LAZY=1nothing changes and that path was not run.Follow-ups (from the reviews, left out of this PR to keep it small)
will_writegoes throughgive_memory/materialize(force=false), which leaves tiles without memory and logs nothing when video memory is short, so under VRAM pressure a build can still write to a destination with nothing behind it. A compacted copy is also given the source's maximum size, not its compacted size (memory only).AI use
🤖 Generated with Claude Code
Imported from ivall/xcl. Originally posted by thehollowedone on 2026-11-30T23:26:00Z.
merge-check
base:
d0832e614fhead:
0744aa01bfmerge: clean (local merge a3877d341313a238250b19464f54a30db7da84c6)
build:
cargo build --workspace --target-dir ..\target-> oktests: A focused, test-only chained-copy assertion on the unchanged PR logic failed:
cargo test -p xcl --lib a_build_and_its_copies_write_the_builders_own_result_size --target-dir ..\target -- --nocapture --test-threads=1-> 0 passed, 1 failed, 0 ignored, 0 skipped. With the local candidate below, the same command -> 1 passed, 0 failed, 0 ignored, 0 skipped. The full workspace suite and paired smoke were not run after this blocking failure.review: A build A, clone A to B, then clone B to C has no tracked size for B. The second copy therefore does not prepare C's destination tiles. Reusing B after an older, smaller build can instead use B's stale size. The candidate records each known copy destination and invalidates a destination when its new size is unknown. Separately, both build wrappers forward postbuild descriptors without preparing their
DestBuffer. For example, a compacted-size result on a separate lazy tile can remain unbacked; Microsoft's postbuild descriptor contract identifies the destination and the compacted-size result is eight bytes. Add a separate-tile regression for this path. These are source and synthetic findings, not a title-run observation.performance: The candidate adds one size-map update per intercepted copy; no frame-time or memory measurement was made. The current map retains entries indefinitely, and
give_buildholds its mutex while calling the builder's prebuild query. Querying before taking that lock would avoid serializing unrelated builds; lifecycle cleanup still needs an owner or release signal.verdict: not yet: chained copies can leave GPU outputs without backing; postbuild output preparation also needs coverage and a fix.
hardware / tool and model: NVIDIA RTX 3080 Ti (621.40), Ryzen 7 9800X3D, Windows 11 build 26300, rustc 1.99.1 MSVC / OpenAI Codex (GPT-6 Sol) ran the local build and regression; OpenAI Codex (GPT-6.2 Astra) independently traced the hook paths.
Tested candidate for chained copies and stale destinations
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T08:25:47Z.
merge-check
base:
7d86c5d2b7head:
0744aa01bfmerge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2898 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 (new
bvh_builder.rs,d3d12x/mod.rs, one line insparse.rs,resolver.rs). Nothing changes by default: withoutXCL_SPARSE_LAZY=1interceptreturns the original address (one name compare per resolved export), so neither the suite nor a smoke run reaches the new hook. Findings: (1) a COMPACT copy is given the source'sResultDataMaxSizeInBytes(bvh_builder.rs~111), so committed tiles past the smaller compacted allocation get video memory early (memory only). (2) The requests go throughgive_memory/materialize(force=false), which skips the remaining tiles and drops the request without a log once memory is short, so under VRAM pressure a build still writes nowhere. (3)SIZES(~41) is never pruned and its lock is held across the builder's prebuild call (~127), serialising recording threads. (4) The tests only cover pure functions; theBatchedDescsize check compares the struct with its own definition plus 32, so it can't catch a wrong layout, and the resolver hook is untested. By its own description this doesn't make the structures correct without #3522. Fine: it reuses thewill_writepath, so tiles are mapped on the sparse queue and the title queue waits on the fence before its lists run; it only backs tiles the title committed, so nothing new leaks.performance: no smoke run (the default path doesn't reach this change); not measured.
verdict: safe to merge together with #3522 once that one's findings are fixed (on its own it doesn't make the structures correct, per its description); 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.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T10:08:25Z.
Triage (#1607) – head
2e2d932#3522 (same author) is stacked on this PR and adds scratch tiles on top of it; that is not a duplicate. No other open or merged PR hooks XboxBvhBuilder's build/copy exports.
Notes on the diff:
bvh_builder.rs:give_buildnow makes the builder's prebuild query before takingSIZES.BatchedDescassumes a 32-byte tail after the build desc; nothing beyond the size assert checks that layout against the DLL.Recommendation: keep open.
AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
Imported from ivall/xcl. Originally posted by Lakanya on 2026-12-01T10:40:57Z.
Thanks both. Updated to
2e2d9320(squashed into the one commit):a_build_and_its_copies_write_the_builders_own_result_size, anda_reused_destination_never_keeps_a_stale_sizecovers B built smaller before. Both fail with the oldcopy_writes.DestBufferfor its type's size (8, or 16 for serialization).postbuild_infos_write_their_own_buffers, and the GPU testa_postbuild_result_on_a_tile_of_its_own_gets_memory(compacted-size result on a separate lazy tile).give_memory. The hook itself and theBatchedDesclayout are still only checked as before.Left as follow-ups, listed in the description: pruning the size map (needs a release signal),
will_writedropping tiles without a log when video memory is short, and compacted copies being given the source's maximum size (memory only).Tests and a new
xcl-smokeblock for the head are in the description. The library harness ends at anend_titleonmain9dfbe3eatoo, so the library counts are per group.AI use: Claude Code (Claude Opus 5.5) made the changes, ran the tests and the regression run, and wrote this reply.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T10:42:13Z.
Re-review (head
2e2d9320; diff from0744aa01, which I merge-checked, read). Not a merge-check:PR descriptionis red until the smoke pair is rerun on this head, and no full suite has run on it yet.bvh_builder.rs~117-130). PostbuildDestBuffers now get memory, with the right sizes: 8 bytes for compacted, current and visualization, 16 for serialization (~136-150, ~173-196). The prebuild query now runs before theSIZESlock is taken (~164-166).give_memory,materialize(force=false)).SIZESis never pruned (~41).BatchedDescsize assert is unchanged.BatchedDesclayout matters more now:d.postbuildis read and its targets get memory.readable()prevents a crash, but a wrong layout would back the wrong addresses (unverified against the DLL).a_reused_destination_never_keeps_a_stale_sizeand the chained-copy asserts fail on the old code. The two postbuild tests only fail to compile without the change, and the sparse one exercisesgive_memory, notgive_postbuildbeing called.AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Imported from ivall/xcl. Originally posted by fromthemoonx on 2026-12-02T00:07:12Z.
merge-check
base:
1c8b551475head:
2e2d9320aemerge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2914 passed, 0 failed, 51 ignored (base: 2909 passed, 0 failed, 51 ignored); SKIPPED: 25 (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps), the same set as base; failing: none from this PR. The whole-suite run ended the lib binary with 0xc0000005 during
eh::tests::thread_dumps_while_threads_run_and_exit(#3586, on main too). The counts are the lib binary run on its own (2766 passed, 0 failed, 31 ignored) plus the other binaries (148 passed, 0 failed, 20 ignored).review: read the whole diff (4 files, +315 -2). No correctness problem found. Foniass's re-review notes stand.
XCL_SPARSE_LAZY=1only, the resolver hands the title hooks for XboxBvhBuilder's batched build, single build and copy exports. Without the switch,interceptreturns the export as before.SIZESis locked) and gives the destination that much video memory throughsparse::will_write. Clone and compact copies carry their source's size forward; other modes, and a build with no size, forget the destination's.DestBuffers get their type's bytes.hle::d3d12x::bvh_builder::tests::*,sparse::tests::a_postbuild_result_on_a_tile_of_its_own_gets_memory): 3 of 3 runs passed. The sparse one ran here (tiled resources tier 2), not skipped.a_build_and_its_copies_write_the_builders_own_result_sizeanda_reused_destination_never_keeps_a_stale_size. Good.the_build_and_copy_exports_are_hookedand the size helpers.performance: none by default (
XCL_SPARSE_LAZYunset). With it, one prebuild query per build plus the memory it gives.verdict: safe to merge
hardware / tool and model: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300 / Claude Code (Claude Opus 5.5)
Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T10:46:57Z.
Review: COMMENTED
Review of
2e2d9320: 4 finding(s). 4 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.
Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T10:46:58Z.
Code review location:
crates/xcl/src/hle/d3d12x/bvh_builder.rs; line 121copy_writesgives memory only for CLONE and COMPACT. SERIALIZE and DESERIALIZE copies are also GPU compute writes through the windows, but they get none and also forget the destination's size. A title that serializes structure X into a freshly committed lazy buffer to cache it, or deserializes a cached blob into a new lazy destination, gets None fromcopy_writes. The destination tiles stay LAZY, the builder's writes go nowhere, and the blob or deserialized BVH reads as zeros. Both cases have an upper bound: source size plus the serialization header for SERIALIZE, and the blob header's deserialized size for DESERIALIZE.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T10:46:58Z.
Code review location:
crates/xcl/src/hle/d3d12x/bvh_builder.rs; line 106Not certain about this one, because it depends on what XboxBvhBuilder's prebuild returns for update inputs: an update build (PERFORM_UPDATE, often in place with dest == source) re-queries the builder's prebuild with update inputs, and if that returns ResultDataMaxSizeInBytes == 0,
build_writesforgets the size the original build recorded. A BLAS built with ALLOW_UPDATE|ALLOW_COMPACTION (0x3000 recorded), refit in place, then compacted into Y:sizes.remove(X)ran,copy_writes(Y, X, COMPACT)finds no source size, Y gets no memory, and the compacted structure reads as zeros, the bug this PR fixes, coming back after a refit. Updates could keep the existing size instead of re-querying.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T10:46:58Z.
Code review location:
crates/xcl/src/hle/d3d12x/bvh_builder.rs; line 93Not certain about this one: the prebuild query always passes a null D3D12XBOX_XDXR_STREAM_DESC, even for batched builds whose element carries builder data in
_tail(possibly a stream), so the size can differ from what the real build writes. If a Batched2X element built with an XDXR stream writes more than the stream-less prebuild answer (asbvh::prebuild_sizeunderestimates flags-0x4 top levels: 0x198400 vs 0x227000), only part of the destination gets memory and the BVH's tail lands on LAZY tiles, so traversal reads zero nodes.Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T10:46:58Z.
Code review location:
crates/xcl/src/hle/d3d12x/bvh_builder.rs; line 185Not certain about this one: postbuild DestBuffers get GPU memory but are never added to the list's read-back set (list_x
note_write/sparse_writes). So a title that reads the compacted size on the CPU from a window the GPU has a copy of (read_back(va)true), after waiting on the fence, still reads 0, and then allocates or compacts with size 0 or skips compaction.Imported from ivall/xcl. Originally posted by kole07139 on 2026-12-02T10:53:33Z.
merge-check claim
head:
2e2d9320aeImported from ivall/xcl. Originally posted by kole07139 on 2026-12-02T11:28:39Z.
merge-check
base:
1c8b551475head:
2e2d9320aemerge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2914 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 here, and the PR's 5 tests passed; the sparse GPU one ran (not skipped). Failing: none.
review: read the whole diff (bvh_builder.rs, mod.rs, sparse.rs, resolver.rs). No correctness problem found.
interceptreturns the original address unless the module is xboxbvhbuilder.dll andXCL_SPARSE_LAZY=1was given, so nothing changes by default. The resolver pays one case-insensitive name compare per export it resolves.will_writeonly queues the range (it gets memory at the next submit, before the list) and acts only withlazy_commits(), so the hooks never wait on the sparse state.XCL_SPARSE_LAZY=1, the hooks are installed forBuildRaytracingAccelerationStructureBatched2XandCopyRaytracingAccelerationStructure(0.8 s; the title doesn't resolve the single-build export).BatchedDesclayout and the copy sizes are still not exercised by a real title here, as in fromthemoonx's check.SIZESnever pruned;BatchedDesclayout unverified against the DLL.performance: none by default. Retail title, main
1c8b5514vs this merge (9e545b7a), 0 device removals and 0 crashes in every run:XCL_SPARSE_LAZY=1: 1562 -> 1565 frames, 17252 -> 17627 MB.XCL_SPARSE_LAZY=1, two runs each:verdict: safe to merge
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, smoke runs and probe, and wrote this check)
AI-written; posted without a human check of each point.
Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T14:12:05Z.
@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.