Skip to content

D3D12.X: a draw's two-pass buffer goes past the headroom cap, not skipped - #3165

Draft
Anon64 wants to merge 2 commits into
xcl-dev:mainfrom
Anon64:fix/two-pass-draws-past-headroom
Draft

Anon64 wants to merge 2 commits into
xcl-dev:mainfrom
Anon64:fix/two-pass-draws-past-headroom

Conversation

@Anon64

@Anon64 Anon64 commented Nov 28, 2026 •

Copy link
Copy Markdown

When the two-pass buffers alive reach the headroom cap (25 % of local budget headroom, clamped), the record buffer and the argument scratch are refused and the draw that asked is skipped. A title whose later queue waits depend on that draw never wakes. On a 12 GB RTX 3060 (16 heap windows, XCL_PREPARE=0), a retail title skipped its first two-pass ExecuteIndirect at a 340 MiB cap. A second later five queues sat in Wait64BitValueX >= 0x713 (wait-before-signal, producer queue 1) with 0 draws for the remaining 13 minutes and no device removal. With XCL_TWO_PASS_MB=2048 the same run played on, with 794 MiB of two-pass buffers alive, and lit its G-buffer (#1989).

Those two paths now make the buffer up to the budget ceiling the cap is already clamped to (min(local budget / 4, 2048 MiB), 1024 MiB with no budget), and skip the draw only past that. A set XCL_TWO_PASS_MB is kept as a hard cap. Pools, batches and the other callers still stop at the headroom cap: they have their own fallbacks.

Builds on #2817's headroom cap (its wait and shrink run first). No open or merged PR changes the refusal: the open nvidia-dev PRs that touch two-pass code (#2999, #3052, #3056, #3105, #3116, #3131, #3012, #2987) leave it as it is, and main / amd-dev have no such cap.

What is in it

  • crates/xcl/src/hle/d3d12x/bind.rs: env_two_pass_cap (the existing XCL_TWO_PASS_MB read, moved out of two_pass_cap), draw_two_pass_ceiling, make_two_pass_past_cap; the record-buffer (ensure_res_buffer_with_cap) and argument-scratch refusals go through it. Two tests: the ceiling arithmetic, and a buffer the headroom cap refuses being made past it (WARP device).
  • ac81767f (review, to be squashed): past the cap, the record buffer runs its existing bucket walk (acquire_res_buf_with_cap) at the ceiling with no fence wait, so a smaller usable buffer is taken before the draw is refused. Test: a_record_buffer_past_the_headroom_cap_shrinks_to_what_fits_under_the_ceiling.

Tested

  • RTX 3060 12 GB (driver 581.80), i9-14900KF, 64 GB RAM, Windows 11 26200, Rust stable MSVC, debug tests / release smoke. XCL_ADAPTER=3060 (the machine also has a GTX 960).
  • cargo test -p xcl --lib two_pass: 38 passed, 0 failed (1 SKIPPED: two_pass_binds_set_the_shared_view_heap, no heap windows); cargo test -p xcl --lib -- headroom_cap past_the at ac81767f: 7 passed, 0 failed.
  • cargo test --workspace -- --test-threads=4 aborts on this card with CreateCommandQueue ... 0x8007000E after 4 title_textures failures, identically on the base 54225f58 without this change; cargo test -p xcl --lib title_textures passes 35/35 on both. So the whole-suite counts are not available from this machine.
  • This commit cherry-picked onto a local nvidia-dev tree with D3D12.X: XCL_TEXTURE_SYSMEM puts streamed texture levels in system memory #3039 + D3D12.X: empty shader slots keep a two-pass mesh pipeline's cache key #2975 merged (16 heap windows, XCL_PREPARE=0, no XCL_TWO_PASS_MB): at 91 s the record buffer (32 MiB) and argument scratch went past the 351 MiB headroom cap ("goes past the ... headroom cap" lines), 0 draws skipped, in play at 718 s with ~10 000 draws per 2 s; in-play G-buffer targets 0/1/3 at 52 / 59 / 43 % non-black (frame 1762). The 900 s run ended cleanly: exit 0x0, 2 471 frames, 0 device removals, 0 crashes, peak commit 40.1 GB. Without the change the same setup froze at ~102 s (see D3D12.X: add depth+UAV color twins, format-aliased placed texture copies, and dual-twin UAV barriers #1989).

Regression run
(main and this PR's head, XCL_PREPARE=0, otherwise plain settings; this title does not reach the cap without 16 windows. The main block replaces an earlier one whose build had failed, so that run used a stale binary. Peak commit varies between ~17 and ~37 GB from run to run here with the cache state.)

xcl-smoke v1
commit: b937fbab2964b2c142f5d13662b91b00f99b9848
gpu: NVIDIA GeForce RTX 3060
title: retail-1
seconds: 300
exit: 0x0
frames shown: 8796 (PresentX 8796)
presents per 2 s (last 3): 60 61 60
device removals: 0
crashes: 0
peak commit: 37547 MB
xcl-smoke v1
commit: ac81767fb5b24589acb9ac89503901c09e857116
gpu: NVIDIA GeForce RTX 3060
title: retail-1
seconds: 300
exit: 0x0
frames shown: 8884 (PresentX 8884)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17946 MB

Not tested

  • The whole suite to completion (aborts on the base too, above).
  • The 16-window setup without XCL_UMA_DRIVER=mixed device-hangs (0x887a0006) in the menus in about half the runs here, before any two-pass buffers; that is separate from this change.
  • AMD, other NVIDIA cards, cards under 12 GB (where the ceiling is lower).

AI use

  • Claude Code, Claude Opus 5.5: found the deadlock in the run logs, wrote the change, the tests and this description, and ran the tests and smoke runs.

🤖 Generated with Claude Code

…pped

The two-pass record buffer and argument scratch are refused once the
buffers alive reach the headroom cap (25 % of local budget headroom,
clamped), and the draw that asked is skipped. A title whose later queue
waits depend on that draw then never wakes: a retail title on a 12 GB
RTX 3060 (16 heap windows) skipped its first two-pass ExecuteIndirect at
a 340 MiB cap and, a second later, five queues sat in Wait64BitValueX on
a value the skipped work would have written, with 0 draws for the
remaining 13 minutes. With XCL_TWO_PASS_MB=2048 the same run played on
with 794 MiB of two-pass buffers alive and lit its G-buffer.

Those two paths now make the buffer up to the budget ceiling the cap is
clamped to (min(local budget / 4, 2048 MiB), 1024 MiB with no budget)
and skip the draw only past that. An XCL_TWO_PASS_MB cap is kept as it
is. Pools, batches and the other callers still stop at the headroom cap.

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

Copy link
Copy Markdown
Contributor

On head b2316919, the record-buffer ceiling retry can still refuse a draw when a smaller usable buffer fits. Both production callers of records_for pass one record as the minimum. After the headroom-cap shrink attempts are exhausted, ensure_res_buffer_with_cap calls make_two_pass_past_cap with the full preferred size.

I ran a deterministic CPU fixture using this head's cap arithmetic, atomic reservation, bucket rounding and acquire_res_buf_with_cap. I modeled a 12 GiB DXGI budget and 10 GiB of non-two-pass usage, giving a 512 MiB headroom cap and a 2,048 MiB ceiling. The request preferred 64 MiB, with one 47,632-byte record as its minimum (rounded to 256 KiB). The fixture supplied no current or reusable buffer, disabled waiting and modeled successful device allocation after admission.

Two-pass memory alive Current ceiling retry Existing shrink logic retried at ceiling
1,984 MiB 64 MiB 64 MiB
2,000 MiB Refused 48 MiB
2,016 MiB Refused 32 MiB
2,048 MiB Refused Refused

Could the record-buffer path reuse the existing bucket walk at the automatic ceiling with zero additional fence wait? The explicit XCL_TWO_PASS_MB limit and argument-scratch allocation can retain their current behavior. These controls establish the admission decision only. GPU allocation success and retail incidence remain unverified.

AI use: OpenAI Codex prepared and ran the CPU fixture, checked the production callers and wrote this report.

…ets at the ceiling

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

Anon64 commented Nov 28, 2026

Copy link
Copy Markdown
Author

@andrewDoing thanks, you're right. Pushed ac81767f: past the headroom cap, the record buffer now runs the same bucket walk (acquire_res_buf_with_cap) at the ceiling, with no fence wait, instead of only trying the preferred size. The argument scratch and an explicit XCL_TWO_PASS_MB keep their behaviour. It is a separate commit for now and will be squashed into the first before review.

Tested on an RTX 3060 12 GB: cargo test -p xcl --lib two_pass 38 passed, 0 failed (1 SKIPPED, no heap windows); a filtered run including the new a_record_buffer_past_the_headroom_cap_shrinks_to_what_fits_under_the_ceiling 7 passed, 0 failed. That test exercises the walk at a ceiling it sets itself, so it does not by itself prove the ensure_res_buffer_with_cap wiring; no retail rerun yet.

AI use: Claude Code (Claude Opus 5.5) wrote the change, the test and this reply, and ran the tests.

@andrewDoing

Copy link
Copy Markdown
Contributor

Retested the four admission boundaries at ac81767f with the same 64 MiB request and 47,632-byte single-record minimum. At 1,984 / 2,000 / 2,016 / 2,048 MiB alive under a 2,048 MiB ceiling, the revised bucket walk admitted 64 / 48 / 32 MiB / refused, respectively. I also checked that ensure_res_buffer_with_cap now passes its actual minimum and existing capacity to that walk with a zero fence wait. This was a CPU admission test with modeled allocation success, plus inspection of the caller. It did not exercise GPU allocation or retail behavior.

AI use: OpenAI Codex ran the controls and prepared this follow-up.

@andrewDoing

Copy link
Copy Markdown
Contributor

I verified the ceiling retry through records_for at ac81767f on an RTX 5070, driver 616.92. The two intermediate requests returned actual 48 MiB and 32 MiB GPU buffers. Both carried the correct data through a two-pass indirect compute workload.

Each row ran in a fresh process, with no reusable record buffer. I requested 64 MiB with a 47,632-byte minimum and batching enabled. A test-only memory query and the alive counter supplied a 12 GiB budget and 10 GiB of non-two-pass usage. Production cap calculation gave 512 MiB headroom and a 2,048 MiB ceiling. The fixture supplied the native device through the existing test-list GetDevice shim.

Mode Modeled two-pass bytes alive Result
Automatic 1,984 MiB 64 MiB, whole layout
Automatic 2,000 MiB 48 MiB, whole layout
Automatic 2,016 MiB 32 MiB, whole layout
Automatic 2,048 MiB Refused
Explicit XCL_TWO_PASS_MB=512 1,984 MiB Refused

For each admission I checked the returned capacity and native resource width, then passed that same resource to the existing two-pass GPU test. It wrote and read records for six indirect commands, waited on a real fence and checked all 16 expected output words. All five cases passed. The budget inputs were synthetic and the workload touched a small portion of each allocation. This verifies caller wiring, allocation, GPU usability, and the hard-cap boundary. It does not measure residency under pressure or reproduce the retail queue stall.

AI use: OpenAI Codex adapted the native fixture, ran the controls and prepared this report.

@tiagonleal

tiagonleal commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

A data point for why this is needed, from a 16 GB card that runs over its video memory budget. The binary in these runs is main 7a649b0e merged with #3304, release, without this PR.

RTX 5070 Ti 16 GB (617.42), Ryzen 7 9800X3D, 32 GB, Windows 11 26300. The newer retail title, after a full shader preparation for this binary, XCL_RAM_MB=12000 XCL_CS_PSO_WAIT_MS=200 XCL_UMA_DRIVER=mixed:1536:1792 XCL_TEXTURE_LEVEL_PAGES=1 XCL_PREPARE=0. The title enters the world at about 90 s and stays there until the run is closed.

In the world the process holds 17.6 to 18.8 GB of local video memory against a budget of about 15.0 GB, so there is no headroom and the cap sits on its floor (budget / 32, about 470 MiB here) for the rest of the run.

run XCL_TWO_PASS_MB cap in the world draws skipped / draws two-pass buffers not made most two-pass MiB alive
w5-1, 600 s unset 459 to 470 MiB 1,354,347 / 2,575,798 (at 485 s) 2,079,427 470
w5-2, 600 s unset 461 to 470 MiB 3,088,681 / 4,194,304 (at 564 s) 3,396,775 525
w6, 420 s 3072 3072 MiB 0 / 3,993,164 (at 406 s) 0 1,157
w7, 420 s 1536 1536 MiB 0 / 2,097,152 (at 304 s) 0 1,331

The counts are from the last console code so far and two-pass buffers log lines of each run.

So with the cap left to itself, half to three quarters of the draws in the world are skipped here, and the title needs 1.1 to 1.3 GiB of two-pass buffers alive. On this card your ceiling works out to 2048 MiB, which would cover that.

On screen it changes nothing I can measure. In every run, with the cap unset or raised, the world frames are black (no pixel above 2 of 255 in the frame dumps) except for the title's button-prompt box, which shows in 1 to 3 of the 9 to 25 frames dumped in the world. The 3D scene behind it is black in all of them, so the skipped draws are not what keeps the scene black on this card. (Edited: this paragraph first said the prompts only appear with the cap raised. That was wrong; I had looked at every fifth frame.)

No device removal in any of these runs. Lowest free RAM was 1.1 GB with 3072 and 2.2 GB with 1536, against 2.0 to 2.4 GB with the cap unset, so the larger buffers cost RAM here (the video memory over the budget ends up in shared memory).

This PR, cap unset (edited in: ac81767f merged onto the same tree, same switches without XCL_TWO_PASS_MB, one run of 420 s):

run cap in the world draws skipped / draws buffers not made indirect cmds clamped (groups dropped) waits (timed out) shrunk most MiB alive
w5-2, base, cap unset 461 to 470 MiB 3,088,681 / 4,194,304 3,396,775 8,251 (1,660,733) 117 (2,338) 446,199 525
w7, base, 1536 1536 MiB 0 / 2,097,152 0 99 (82,327) 0 (0) 0 1,331
w6, base, 3072 3072 MiB 0 / 3,993,164 0 89 (24,751) 0 (0) 0 1,157
w10, this PR, cap unset 464 to 466 MiB 0 / 4,194,304 (at 399 s) 0 30,134 (5,539,835) 195 (3,835) 690,995 637

So the PR does what it says here: no draw is skipped and nothing is refused. But on this card most draws do not get their preferred record buffer past the cap. ensure_res_buffer_with_cap goes to the ceiling only after acquire_res_buf_with_cap has waited and walked the smaller buckets under the headroom cap, and after the list's existing smaller buffer has been tried, so whenever a smaller bucket fits under the cap the draw takes it, and the clamp then drops the groups that have no record. That fits 637 MiB alive at a 465 MiB cap where the title uses 1.1 to 1.3 GiB when the cap is raised by hand, and 5.5 M groups dropped against 25 to 82 thousand. Trying the preferred size under the ceiling before shrinking would keep those groups on a card that sits on the floor cap.

The groups-dropped figures of the base and of this PR cannot be compared directly: a skipped draw loses all its groups and none of them are counted there.

Not tested: more than one run per setting for 1536, 3072 and this PR. All these runs had the default UMA layout (the log says 0 view windows), so the title's two-pass demand with XCL_UMA_WINDOWS=16 is not measured here. The picture is the same black scene in all of them, for a reason that is not this cap (the G-buffer targets are empty in a target dump although their draws ran).

AI use: Claude Code (Claude Fable 5.2) set up the runs, counted from the logs and frame dumps, and wrote this; Claude Opus 5.5 read bind.rs for the order of the wait, the shrink and the ceiling, and I checked it in the source.

  • Tiago

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head ac81767

No open or merged PR does this. It builds on the headroom cap from the merged #2817 (budget_two_pass_cap) and lets a draw's record buffer or argument scratch go past that cap, up to draw_two_pass_ceiling, instead of skipping the draw. XCL_TWO_PASS_MB still caps the buffers as before. #3555 only documents XCL_TWO_PASS_MB in the README.
No problems found in the diff (bind.rs make_two_pass_past_cap_with, ensure_res_buffer_with_cap, scratch_args). The two commits could be squashed into one.
Recommendation: keep open.

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

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head ac81767, diff read, not a merge-check; draft):

  • It rarely helps when VRAM is tight. The past-cap allocation runs only after the wait and the shrink under the cap (bind.rs ~2630, then ~2640), so the preferred record size is almost never made past the cap. A tester's run on the PR shows it: 5.5 M mesh groups still dropped, against 25-82 thousand with the cap raised by hand.
  • An uncached DXGI query on every fallback. make_two_pass_past_cap_with calls real::video_memory() directly (~339), which makes a DXGI factory each time, instead of two_pass_cap's 250 ms cache.
  • Memory past the budget. Up to min(budget / 4, 2 GiB) (~315) can be allocated when VRAM is already over budget, and it spills into system RAM; the tester saw free RAM drop to 1.1 GB with a 3072 MiB cap. With the live count above the cap, the capped callers (pools, batches) keep being refused.
  • Untested. Neither the record-buffer path (ensure_res_buffer_with_cap) nor the argument scratch (scratch_args) is tested, only the helpers. The refusal log (~253) still blames XCL_TWO_PASS_MB while printing the new ceiling.
  • The description's "main has no such cap" is out of date: D3D12.X: cap two-pass buffers by DXGI headroom with wait and shrink #2817's cap is on main now.

Fine: growth is bounded by the ceiling and counted as before, a failed allocation returns an error and the draw is skipped, and a set XCL_TWO_PASS_MB stays a hard cap. Going over the cap rather than skipping is the better trade when a skipped draw can hang the title.

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

saintwolf added a commit to saintwolf/xcl that referenced this pull request Dec 1, 2026
When local-VRAM two-pass buffers reach XCL_TWO_PASS_MB (or the DXGI headroom floor),
spill overflow UAV buffers to D3D12_MEMORY_POOL_L0 (up to XCL_TWO_PASS_L0_MB,
default 512 MiB; 0 disables) with a separate FREE_L0_RES pool instead of
spin-waiting and skipping draws.

Complementary to xcl-dev#3165 and xcl-dev#3555.
saintwolf added a commit to saintwolf/xcl that referenced this pull request Dec 1, 2026
When local-VRAM two-pass UAV buffers reach XCL_TWO_PASS_MB (or the dynamic DXGI headroom floor), spill full-size overflow record and argument-scratch buffers to D3D12_MEMORY_POOL_L0 (up to XCL_TWO_PASS_L0_MB, default 512 MiB; 0 disables) tracked separately in TWO_PASS_L0_ALIVE and FREE_L0_RES (trimmed on idle from put_free_bufs, release_allocator_res, and take_free). When L0 spillover is disabled, full, or unsupported by the adapter, fall back to the original local FENCE_WAIT_LIMIT wait + shrink/refusal path.

Complementary to xcl-dev#3165 (draft).
saintwolf added a commit to saintwolf/xcl that referenced this pull request Dec 1, 2026
When local-VRAM two-pass UAV buffers reach XCL_TWO_PASS_MB (or the dynamic DXGI headroom floor), spill full-size overflow record and argument-scratch buffers to D3D12_MEMORY_POOL_L0 (up to XCL_TWO_PASS_L0_MB, default 512 MiB; 0 disables) tracked separately in TWO_PASS_L0_ALIVE and FREE_L0_RES (trimmed on idle from put_free_bufs, release_allocator_res, and take_free). When L0 spillover is disabled, full, or unsupported by the adapter, fall back to the original local FENCE_WAIT_LIMIT wait + shrink/refusal path.

Complementary to xcl-dev#3165 (draft).
saintwolf added a commit to saintwolf/xcl that referenced this pull request Dec 1, 2026
When local-VRAM two-pass UAV buffers reach XCL_TWO_PASS_MB (or the dynamic DXGI headroom floor), spill full-size overflow record and argument-scratch buffers to D3D12_MEMORY_POOL_L0 (up to XCL_TWO_PASS_L0_MB, default 512 MiB; 0 disables) tracked separately in TWO_PASS_L0_ALIVE and FREE_L0_RES (trimmed on idle from put_free_bufs, release_allocator_res, and take_free). When L0 spillover is disabled, full, or unsupported by the adapter, fall back to the original local FENCE_WAIT_LIMIT wait + shrink/refusal path.

Complementary to xcl-dev#3165 (draft).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants