Skip to content

D3D12.X: defer GPU-owned target pages until first use - #3395

Draft
lnqigr wants to merge 2 commits into
xcl-dev:mainfrom
lnqigr:d3d12x/lazy-target-pages
Draft

lnqigr wants to merge 2 commits into
xcl-dev:mainfrom
lnqigr:d3d12x/lazy-target-pages

Conversation

@lnqigr

@lnqigr lnqigr commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #3443; only the top commit (bdf38bf4) belongs here.

Adds opt-in XCL_LAZY_TARGETS=1. GPU-owned placed targets bind their 64 KiB video-memory tiles at first use, so a title can create render targets without allocating tiles it may never use. Related to #3211 and #1773.

What is in it

  • Track deferred targets and copied RTV/DSV handles in vram.rs. Bind the tiles before command-list or upload use.
  • Look for lazy targets bound through descriptor tables in every SRV/UAV range the root signature declares, with D3D12.X: look for superseded targets in every declared table range #3443's range walk (for_each_declared_table_slot in bind.rs), not only in the first 64 slots of each table. Test: lazy_targets_are_mapped_in_every_declared_table_range.
  • Reject a command list when first use cannot allocate the tiles. A later recording can retry. Added tests for first-use rendering, copied descriptors, and submission refusal. Documented the flag in README.md.

Tested

  • Windows 11 build 26300.9974, Intel Core i9-14900HX, NVIDIA GeForce RTX 4060 Laptop GPU (8 GB, driver 610.88), Rust 1.100.0.
  • At 6a4c17fd, the four-thread workspace run aborted in the PSO library tests on the test fixture address 0x4332. The two-pass cache integration test failed once and passed on the repeat. The full library target passed on the main baseline. The parallel failure on the candidate remains unresolved.
  • cargo test --offline --locked --workspace --no-fail-fast -j 4 -- --test-threads=1 --show-output completed with 2,621 passed, 0 failed, 49 ignored, and 30 SKIPPED reports.
  • The focused VRAM tests ran on the GPU (71 passed, 0 failed, 1 ignored, 0 SKIPPED). The command-list refusal test passed in both four-thread attempts.
  • At bdf38bf4 (this commit on D3D12.X: look for superseded targets in every declared table range #3443's 7afecf84): .\scripts\test.ps1 (cargo test --workspace -- --test-threads=4) completed with 2,743 passed, 0 failed, 49 ignored, and 31 SKIPPED reports. The new table-range test fails when the old 64-slot walk is put back (it finds the view at slot 10, not the one at slot 100).
  • cargo build --offline --locked --release -p xcl -j 4 and 60 s of scripts/smoke.ps1 on title dc7b94e1 for each configuration. On 6a4c17fd, enabling the flag reduced local VRAM from 6,057 to 3,048 MB (−3,009 MB, −49.7%) and target-pool capacity from 3,584 to 576 MB at the final activity sample. The PR blocks below are from bdf38bf4, without input; on that run the flag lowered peak commit from 16,363 to 13,323 MB.

Regression run

Main baseline

xcl-smoke v1
commit: d8bd9a38204667f3ca7dd9eeffcc2f1c937b07b0
gpu: NVIDIA GeForce RTX 4060 Laptop GPU
title: dc7b94e1
seconds: 60
exit: 0x0
frames shown: 1499 (PresentX 1499)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14690 MB

PR head with the flag off

xcl-smoke v1
commit: bdf38bf4361a4d7a7af678cce7d9a5e4e356e1ef
gpu: NVIDIA GeForce RTX 4060 Laptop GPU
title: dc7b94e1
seconds: 60
exit: 0x0
frames shown: 1636 (PresentX 1636)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16363 MB

PR head with the flag on

xcl-smoke v1
commit: bdf38bf4361a4d7a7af678cce7d9a5e4e356e1ef
gpu: NVIDIA GeForce RTX 4060 Laptop GPU
title: dc7b94e1
seconds: 60
exit: 0x0
frames shown: 1641 (PresentX 1641)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13323 MB

Not tested

  • A title that binds a lazy target past slot 64 of a table: only the unit test covers the range walk.
  • AMD, Intel, gameplay beyond the tested menu loop, and direct bindless UAV accesses that bypass the tracked paths. The D3D12 debug layer was unavailable.
  • Actual GPU memory exhaustion during a retail run. The command-list refusal test exercises the failure-handling path.

AI use

Antigravity (Gemini 4 Neon) wrote the original implementation and GPU tests, and ran the initial benchmarks. Codex (GPT-6) reviewed it, implemented the allocation-failure handling and its regression test, ran the final tests and smoke checks, and edited this description. Claude Code (Claude Opus 5.5) moved the commit onto #3443, made the lazy-target scan walk the declared table ranges, added its test, reran the suite and the two PR smoke runs, and updated this description.

@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory area:docs-ci Documentation, scripts, workflows, repo governance 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
@Aniseto

Aniseto commented Nov 29, 2026

Copy link
Copy Markdown

Ran the lazy-target deferral tests at 6a4c17fd on an NVIDIA Blackwell desktop part.

System

  • GPU: NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only, mesh shaders), driver 32.0.16.2140 (621.40)
  • CPU: Intel Core i5-12400F, 64 GB RAM
  • OS: Windows 11 Pro build 26200 x64
  • Toolchain: rustc 1.100.0 (0ef1bc386 2026-11-09), MSVC 14.44.35207, DXC 1.9.2609.5
  • Build: debug

Results

  • XCL_ADAPTER=NVIDIA cargo test -p xcl --lib gpu_lazy_target_defers_vram_until_first_use_and_renders_correctly -- --test-threads=1: 1 passed, 0 failed, 0 ignored. The GPU test ran here, with no SKIPPED line.
  • XCL_ADAPTER=NVIDIA cargo test -p xcl --lib target_view_copy_pieces_and_table_follow_handles -- --test-threads=1: 1 passed, 0 failed, 0 ignored.

What this adds
Both tests pass: VRAM for a GPU-owned placed target is deferred until first use and the render still reads correctly, and copied RTV/DSV handles follow the target views. The thread has no comments yet, so this is the first datapoint in it and the first from an RTX 5070 Ti. Synthetic GPU tests only; no title run.

Not tested
A retail title, a paired smoke run, the full workspace suite here, AMD and Intel GPUs, native wave64 hosts, Wine, release profile, game data.

AI-assisted (OpenCode), model: DeepSeek V4.1 Flash Free - ran the commands above on this machine and wrote this report; no game data was used.

A superseded target bound through a descriptor table is remapped when a draw
or dispatch binds the table (xcl-dev#3179), but only the first 256 slots of each
table were read. A view further in – a bindless SRV range, or a range at an
explicit offset past slot 256 – was never seen: the target kept no tiles,
its writes were dropped and its reads returned zeros.

The root signature's SRV and UAV ranges of each descriptor table (RTS0 1.0
and 1.1, appended and unbounded ones included) are now kept in the layout,
and the scan reads those ranges and nothing else. An unbounded range ends
with the view heap the table lies in (console_heap::view_heap_remaining_slots);
outside any view heap it has no known extent and is not read. CBV and sampler
ranges cannot name a placed target and are skipped.

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

KDenKC commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

A note from reading 6a4c17fd (code reading, not a run): ensure_table_lazy_targets in hle/d3d12x/bind.rs (around lines 1567-1601, called from bind.rs ~5256 and console_list.rs ~1056) checks readable(base, 64 * 32) and then loops for k in 0..64usize, so it only looks at the first 64 slots of each Param::Table. A lazy GPU-owned target bound past slot 64 (a bindless SRV/UAV range or an explicit offset) would never reach vram::ensure_target_mapped, so with XCL_LAZY_TARGETS=1 its tiles stay unmapped: writes dropped, reads zero.

#3443 (Lakanya, open) handles the same kind of fixed slot cap in the superseded-target scan: it records each table's declared SRV/UAV ranges (Layout::table_ranges), caps unbounded ranges with console_heap::view_heap_remaining_slots, and scans in 256-slot chunks (scan_superseded_in_tables_with). Walking the declared ranges the same way here (skipping CBV and sampler ranges) would close the gap without touching the rest of the deferral.

AI use: an agent swarm: Claude Opus 5.5, Claude Sonnet 5.5, GPT-6.2-Sol, Gemini 4 Neon and Gemini 3.8 Flash; I checked the lines above against the pinned commit.

@lnqigr
lnqigr force-pushed the d3d12x/lazy-target-pages branch from 6a4c17f to a2513de Compare November 29, 2026 22:51
@github-actions github-actions Bot added the needs-pr-check Failing the PR description check (CONTRIBUTING.md) label Nov 29, 2026
Keep XCL_LAZY_TARGETS opt-in. Defer shared video-memory tiles for
GPU-owned placed targets and map them before command-list or upload
use. Track copied RTV and DSV handles while mapping is pending.

Reject a command list if first use cannot allocate its tiles.
Unmapped targets remain eligible for a retry after reset.
Cover deferred allocation, first use, and submission refusal.

Views bound through descriptor tables are looked for in every SRV/UAV
range the root signature declares, with the range walk of xcl-dev#3443 (now
for_each_declared_table_slot), not only in the first 64 slots of each
table: a target bound further in would otherwise keep no tiles.

Related to xcl-dev#3211 and xcl-dev#1773.

Assisted-by: Antigravity (Gemini 4 Neon)
Assisted-by: Codex (GPT-6)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lnqigr
lnqigr force-pushed the d3d12x/lazy-target-pages branch from a2513de to bdf38bf Compare November 29, 2026 23:52
@github-actions github-actions Bot removed the needs-pr-check Failing the PR description check (CONTRIBUTING.md) label Nov 29, 2026
@lnqigr lnqigr closed this Nov 30, 2026
@lnqigr lnqigr reopened this Nov 30, 2026
@lnqigr lnqigr closed this Nov 30, 2026
@lnqigr lnqigr reopened this Nov 30, 2026
@Foniass

Foniass commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Review (head bdf38bf, diff read, not a merge-check; draft, off by default (XCL_LAZY_TARGETS=1 turns it on), PR description red):

  • With the switch on, every draw and dispatch pays a table scan that never stops. It runs while any lazy target has no tiles (bind.rs ~5362), and targets the title never uses never get tiles. The scan is D3D12.X: look for superseded targets in every declared table range #3443's: unbounded ranges are read to the end of the view heap, the global HEAPS lock is taken per table, and there are two LAZY_UNMAPPED lookups per view. For a heap of 1M descriptors that is 32 MB read per bind. The smoke runs are capped at 60 fps, so this is unmeasured.
  • First uses that can be missed (reasoned): shaders that index the heap directly, descriptors written into a table after the draw is recorded, and tables set through ExecuteIndirect arguments. A missed target reads zeros and drops its writes. Placement needs tiled-resource tier 2, so it's not a device removal. Transition barriers (list_x.rs ~6454) are the only other net.
  • A change with the switch off. real.rs ~4125 / ~4131 now read the poison flag after flush_two_pass and before_close, and a poisoned list skips note_list_closed. It isn't in the description and has no test.
  • Tiles are now bound on the title's recording threads, under the global PAGES lock, possibly creating a 64 MiB pool heap mid-frame. Before, that happened when the target was made. The ordering is sound: mappings go on the dedicated queue, and before_execute makes the submitting queue wait.
  • Tests.
    • The GPU test calls ensure_target_view_mapped directly. No test goes through the hooked set-targets, barrier, copy or clear paths.
    • vram.rs ~5893 raises LAZY_UNMAPPED_COUNT for the whole process, which turns the lazy paths on for tests running beside it. ~5929 sets the test flag before the skip check. Either may explain the four-thread abort the description mentions (unverified).
  • Measurements.
    • The -3,009 MB VRAM figure is from 6a4c17f, not this head.
    • With the switch off the PR's run is about 11 % above main in peak commit (16,363 vs 14,690 MB), with no explanation. The PR's base is about 200 commits behind main.
  • Size. The PR carries D3D12.X: look for superseded targets in every declared table range #3443, which should land first. This PR's own commit (+749) splits into a stack:
    1. the vram.rs tracking, not yet called, with its tests;
    2. the cl_close poison change, with a test;
    3. the first-use hooks plus the switch and README;
    4. the page_owners stats.

Fine: off by default, with one relaxed atomic load per hook. Two threads mapping one target are serialised under PAGES. A failed allocation poisons the list and KickoffX refuses it. Copies of RTV / DSV handles are tracked, and release bookkeeping is right. The two red CI runs are standin::tests::concurrent_shell_miss_..., which main fixed in 5454468 (not in this PR's base).

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

@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head bdf38bf

Stacked on #3443: the lines shared with it are #3443's commit (7afecf84), and only bdf38bf4 belongs here, so it is not a duplicate. Read the top commit. XCL_LAZY_TARGETS is opt-in, but two default-path changes come with it, both in real.rs. cl_close now reads the poison state after flush_two_pass/before_close and skips note_list_closed for a poisoned list. The KickoffX/Close warnings no longer name the cause. ensure_target_mapped binds tiles while a list is recording. The diff does not show how the tile mapping is ordered before that list runs on the GPU, as vram::before_execute does for level pages.
Recommendation: keep open.

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

@throny

throny commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Queued an in-world A/B of this on an RTX 4080 16 GB: current main + this head bdf38bf4, same build with XCL_LAZY_TARGETS unset vs =1, 900 s each with XCL_RESIDENCY_PRIORITY=1 and XCL_GPU_PROFILE=1. Our runs are ~3.3 GB over the video-memory budget with render targets the largest part (~6.4 GB), so this is the case it targets; numbers here when done.

AI use: Claude Code (Claude Opus 5.5).

@throny

throny commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

In-world result, RTX 4080 16 GB (621.40), current main 6bfb5c51 + this head bdf38bf4 (plus our local input-timing patch), same build, 900 s each, XCL_RESIDENCY_PRIORITY=1, default two-pass reserve, no draws skipped in either run:

run XCL_LAZY_TARGETS presents/s after 240 s video memory peak / budget
389 unset 3.31 19.1 / 15.1 GB
390 1 2.77 18.0 / 15.0 GB

So it saves about 1.1 GB of video memory (render-target pages 6.6 → 5.4 GB at the end of a run) but costs ~16 % presents/s here, where the card stays over budget either way. No device removal or crash. (An earlier pair at XCL_TWO_PASS_MB=512 is not comparable: that limit made XCL skip 41 % of draws in the run without the switch and 1.4 % with it.)

AI use: Claude Code (Claude Opus 5.5).

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:docs-ci Documentation, scripts, workflows, repo governance 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.

6 participants