Repository navigation
Conversation
|
merge-check Tested candidate for chained copies and stale destinationsdiff --git a/crates/xcl/src/hle/d3d12x/bvh_builder.rs b/crates/xcl/src/hle/d3d12x/bvh_builder.rs
index 02aef8f9..f3f6c13c 100644
--- a/crates/xcl/src/hle/d3d12x/bvh_builder.rs
+++ b/crates/xcl/src/hle/d3d12x/bvh_builder.rs
@@ -104,11 +104,18 @@ unsafe fn build_writes(prebuild: PrebuildFn, this: *mut c_void, b: &D3D12_BUILD_
/// What a copy of `source` to `dest` writes: as much as `source`'s build could write (a clone
/// or a compacted copy is no larger). Other modes, and sources whose build was not seen: None.
-fn copy_writes(dest: u64, source: u64, mode: D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE, sizes: &HashMap<u64, u64>) -> Option<(u64, u64)> {
- if dest == 0 || (mode != D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_CLONE && mode != D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_COMPACT) {
+fn copy_writes(dest: u64, source: u64, mode: D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE, sizes: &mut HashMap<u64, u64>) -> Option<(u64, u64)> {
+ if dest == 0 {
return None;
}
- sizes.get(&source).map(|&n| (dest, n))
+ let known = (mode == D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_CLONE || mode == D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_COMPACT)
+ .then(|| sizes.get(&source).copied())
+ .flatten();
+ sizes.remove(&dest);
+ known.map(|n| {
+ sizes.insert(dest, n);
+ (dest, n)
+ })
}
/// The GPU writes `[va, va + len)`: its tiles get video memory before the list runs.
@@ -186,11 +193,14 @@ mod tests {
assert_eq!(build_writes(fake_prebuild, std::ptr::null_mut(), &b, &mut sizes), None, "no destination");
}
assert_eq!(sizes.len(), 1);
- assert_eq!(copy_writes(0x10_5000_0000, 0x10_4000_0000, D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_COMPACT, &sizes), Some((0x10_5000_0000, 0x3000)));
- assert_eq!(copy_writes(0x10_5000_0000, 0x10_4000_0000, D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_CLONE, &sizes), Some((0x10_5000_0000, 0x3000)));
- assert_eq!(copy_writes(0x10_5000_0000, 0x10_4000_0000, D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_SERIALIZE, &sizes), None, "a serialized copy has another size");
- assert_eq!(copy_writes(0x10_5000_0000, 0x10_6000_0000, D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_COMPACT, &sizes), None, "a source whose build was not seen");
- assert_eq!(copy_writes(0, 0x10_4000_0000, D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_COMPACT, &sizes), None);
+ assert_eq!(copy_writes(0x10_5000_0000, 0x10_4000_0000, D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_COMPACT, &mut sizes), Some((0x10_5000_0000, 0x3000)));
+ assert_eq!(copy_writes(0x10_5000_0000, 0x10_4000_0000, D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_CLONE, &mut sizes), Some((0x10_5000_0000, 0x3000)));
+ assert_eq!(copy_writes(0x10_6000_0000, 0x10_5000_0000, D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_CLONE, &mut sizes), Some((0x10_6000_0000, 0x3000)), "a second copy must inherit its source's tracked size");
+ assert_eq!(copy_writes(0x10_5000_0000, 0x10_4000_0000, D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_SERIALIZE, &mut sizes), None, "a serialized copy has another size");
+ assert!(!sizes.contains_key(&0x10_5000_0000), "an unsupported write invalidates the old destination size");
+ assert_eq!(copy_writes(0x10_6000_0000, 0x10_7000_0000, D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_COMPACT, &mut sizes), None, "a source whose build was not seen");
+ assert!(!sizes.contains_key(&0x10_6000_0000), "an unknown copy invalidates the old destination size");
+ assert_eq!(copy_writes(0, 0x10_4000_0000, D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_COMPACT, &mut sizes), None);
}
/// Only the build and copy exports are hooked; the batched element is laid out as the |
|
merge-check AI-written and posted by Foniass without a human check of each point. |
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>
|
#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. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human. |
0744aa0 to
2e2d932
Compare
|
Thanks both. Updated to
Left as follow-ups, listed in the description: pruning the size map (needs a release signal), Tests and a new AI use: Claude Code (Claude Opus 5.5) made the changes, ran the tests and the regression run, and wrote this reply. |
|
Re-review (head 2e2d932; diff from 0744aa0, which I merge-checked, read). Not a merge-check:
AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point. |
|
merge-check
|
bmjask
left a comment
There was a problem hiding this comment.
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.
| if dest == 0 { | ||
| return None; | ||
| } | ||
| let same_size = mode == D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_CLONE || mode == D3D12_RAYTRACING_ACCELERATION_STRUCTURE_COPY_MODE_COMPACT; |
There was a problem hiding this comment.
copy_writes gives 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 from copy_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.
| return None; | ||
| } | ||
| if info.ResultDataMaxSizeInBytes == 0 { | ||
| sizes.remove(&dest); |
There was a problem hiding this comment.
Not 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_writes forgets 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.
| /// The builder's own `prebuild` answer for build `b`'s inputs (asked without a stream). | ||
| unsafe fn prebuild_info(prebuild: PrebuildFn, this: *mut c_void, b: &D3D12_BUILD_RAYTRACING_ACCELERATION_STRUCTURE_DESC) -> D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO { | ||
| let mut info = D3D12_RAYTRACING_ACCELERATION_STRUCTURE_PREBUILD_INFO::default(); | ||
| prebuild(this, &b.Inputs, std::ptr::null(), &mut info); |
There was a problem hiding this comment.
Not 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 (as bvh::prebuild_size underestimates 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.
| if count > 0 && crate::gpu::shader::readable(descs as usize, count as usize * std::mem::size_of::<BatchedDesc>()) { | ||
| for d in std::slice::from_raw_parts(descs, count as usize) { | ||
| give_build(this, &d.build); | ||
| give_postbuild(d.postbuild_count, d.postbuild); |
There was a problem hiding this comment.
Not 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.
|
merge-check claim |
|
merge-check
AI-written; posted without a human check of each point. |
|
@Prime1997 could you take a look at this one for review and merge? My automated review notes are above, if any. |
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 D3D12.X: a ray-tracing build's scratch gets window tiles of its own #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