Skip to content

D3D12.X: clear stale PC views when CBV, BVH, or buffer-at-VA overwrites a slot - #3313

Closed
Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:samplers/clear-stale-view-on-cbv-buf-at
Closed

Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:samplers/clear-stale-view-on-cbv-buf-at

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Clear stale PC SRV/UAV descriptors and NAMES tracking when a console view slot is overwritten by a UMA-only view (CBV, raytracing acceleration structure, or buffer view by address), and guard buffer-by-address descriptor writes against sampler-heap slots.

What is in it

  • crates/xcl/src/hle/d3d12x/console_heap.rs:
    • Add clear_views(s: &Slot) to clear note_resource and, whenever a view slot previously tracked a live resource or GONE (names_of(s) != (0, 0)), null both its title-heap mirror descriptors (pc_srv, pc_uav) and its shared-heap descriptors (dst, dst + inc) and reset both NAMES entries to 0 using the same HEAPS -> GLOBAL lease + SLOT_WRITES lock order as publish. Slots already at (0, 0) return immediately with two relaxed atomic loads and no locks or D3D12 calls.
  • crates/xcl/src/hle/d3d12x/real.rs:
    • Call console_heap::clear_views(&slot) from dev_create_cbv_x, the D3D12_SRV_DIMENSION_RAYTRACING_ACCELERATION_STRUCTURE branch of dev_create_srv_inner, and the BUFFER branches of dev_create_srv_at_x (#70) and dev_create_uav_at_x (#71).
    • Filter out sampler slots (.filter(|s| !s.sampler)) in the buffer branches of dev_create_srv_at_x and dev_create_uav_at_x, matching dev_create_cbv_x.
    • Add a child-isolated WARP + D3D12 debug-layer test stale_view_clear_tests::cbv_bvh_and_buffer_view_at_clear_stale_pc_views_and_skip_sampler_slots covering all four overwrite paths, subsequent CopyDescriptorsSimple, resource_gone reporting 0 stale slots, and sampler-slot preservation.

Tested

  • Targeted child-isolated WARP + D3D12 debug-layer test (cargo test -p xcl --lib -- hle::d3d12x::real::stale_view_clear_tests::):
    • Before the fix: failed with slot 0 must not retain stale tex reference after overwrite/copy ((tex, tex) instead of (0, 0)).
    • After the fix: 1 passed; 0 failed (0.23s, zero debug-layer errors).
  • Full workspace test suite on Windows 11 Pro (rustc 1.99.0, RTX 5070 Ti driver 32.0.16.1074, DXC 1.9.2609.5):
    • Command: cargo test --workspace -j 2 -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests::
    • Result: 2600 passed; 0 failed; 49 ignored (324.5s).
    • SKIPPED: 1 (1 no heap windows).

Regression run
Baseline (main ancestor f0e8fdd5c3942ddd399d5ee11384c8595c5aacff) and this PR (d310d833960f51efbe0f9362e958c4a8e4e7f3b6), 150 s on RTX 5070 Ti:

xcl-smoke v1
commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1695 (PresentX 1695)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 31546 MB
xcl-smoke v1
commit: d310d833960f51efbe0f9362e958c4a8e4e7f3b6
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1554 (PresentX 1554)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 33050 MB

Not tested

  • AMD RDNA hardware, macOS/Linux hosts, and non-shared (shared_srv == 0) console view heaps that are never registered into GLOBAL.
  • test_title_runs_and_game_folder_is_untouched and uma_driver::tests:: were skipped in the workspace test run.

AI use

  • Implemented, debugged, and verified with an AI coding assistant; all code, tests, workspace results, and smoke output were reviewed and verified locally.

@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory 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 29, 2026
@Foniass

Foniass commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

Review (head d310d83, diff read, not a merge-check):

  • No visible bug on main is shown: shaders read those slots through UMA, and resource_gone already nulls views of dead resources.
  • Cost on a hot path: dev_create_cbv_x now calls console_heap::clear_views on every CBV. When the slot held a PC SRV/UAV it takes the HEAPS, GLOBAL and SLOT_WRITES locks and does two descriptor copies. That is not measured; the PR's own smoke shows −8 % frames and +1.5 GB peak (inside the limits, but in the wrong direction).
  • The sampler-slot filter is a separate change.
  • The description reports SKIPPED: 1 where D3D12.X: use null PC buffer UAV for shifted UMA buffers #3297, on the same GPU and command, reports 26.

Suggested verdict: not yet (show the bug, measure the cost).

(Review written with Claude Code, Claude Opus 5.5.)

@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 d310d833: 4 finding(s). 3 are inline. These are on lines outside the diff:

crates/xcl/src/hle/d3d12x/real.rs:3724: This only covers views with no PC view at all. Paths that publish one half still leave the other half naming the old resource. If a slot holds an SRV and a UAV of tex and the title makes a buffer UAV in it, publish(uav=true, buf) gives names (tex, buf), and the SRV half and its mirror still name tex. A CopyDescriptors of the slot then carries tex into the destination, and resource_gone counts it. Null-resource SRV paths that publish only the SRV half behave the same. A single "slot overwritten" step in slot creation (clear both halves, then publish the new one) would fix every path.


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.

return;
};
let _w = SLOT_WRITES.lock().unwrap();
copy_views(&dev, 2, pc_srv, nulls_cpu);

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.

clear_views writes null views into the shared-heap SRV/UAV pair at the slot's index, but the module documents that titles keep T#s, each holding a shared-heap index, in their own memory. If a title makes a texture SRV in slot k, copies the T# words into a material table, then reuses slot k for a CBV, the copied T# still samples the texture on the console. Before this PR the PC shared index still held the live view. Now it's null, so the material renders black. This might also be related to the 1695 -> 1554 frames in the PR's smoke run.

// translated shaders read BVH nodes from UMA (`bvh_intersect`), never from a PC DXR SRV,
// so write the RDNA2 BVH descriptor directly and skip forwarding the console VA to PC.
if let Some(v) = desc.as_ref().filter(|v| v.ViewDimension == D3D12_SRV_DIMENSION_RAYTRACING_ACCELERATION_STRUCTURE) {
super::console_heap::clear_views(&slot);

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 !s.sampler guard was added to the buffer branches of #70/#71 but not to the RTAS branch, which this PR also touches. dev_create_srv_inner takes console_heap::slot(cpu) with no sampler filter. clear_views returns early for samplers, then console_heap::write(&slot, &[bvh w0..w3,0,0,0,0]) writes 4 words over the S#, including word 3 [11:0], the PC sampler index. Later samples through that slot use a garbage sampler index, which is the defect this PR fixes for #70/#71.

return;
}
note_resource(s, 0);
if s.shared_srv == 0 || names_of(s) == (0, 0) {

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 early return when shared_srv == 0 or names_of(s) == (0, 0) skips nulling the mirror descriptors at pc_srv/pc_uav, even if they hold a live view. If slot k got its views via CopyDescriptors from a source with no shared place (names (0,0)), the mirror holds tex while NAMES says 0. A CBV over k skips the clear, and a later copy of k publishes the mirror's tex view into another shared index with NAMES 0. resource_gone(tex) never nulls it, so after tex is released the shared heap keeps a descriptor of a destroyed resource. Nulling the mirror needs no GLOBAL state and could be done unconditionally.

@Kalmarv

Kalmarv commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Closing: conflicts in real.rs with no observed bug on main (resource_gone already nulls views) and a measured smoke regression.

@Kalmarv Kalmarv closed this 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: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.

3 participants