Skip to content

D3D12.X: disable lazy level pages when a sampled texture aliases a live GPU page - #3188

Open
Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:neon/lazy-aliased-nv
Open

Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:neon/lazy-aliased-nv

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Port of amd-dev dece6dd9 (part 1, by @raylitebull) to nvidia-dev.

Under XCL_TEXTURE_LEVEL_PAGES=1, vram::place_pages sets lazy = true for every sampled 2D single-slice texture (D3D12_RESOURCE_FLAG_NONE), deferring tile mapping until map_levels_with_texels sees non-zero CPU texels at the placement VA. When a sampled texture is placed at the same 64 KiB-aligned VA as a live GPU-owned texture (render target, depth buffer, or UAV), CPU memory at that VA is all zeros, so its tiles are never mapped and shaders read zeros. Mapping eagerly when Pages::pages already holds a live page at canonical_va(va) reuses the existing shared pages at zero extra VRAM cost.

This is separate from #3116 (which issues a NULL-range UpdateTileMappings and fence wait on lazy placement to avoid page faults) and #3094 (XCL_ALIAS_COPY=1, which copies across different formats).

What is in it

  • crates/xcl/src/hle/d3d12x/vram.rs: add has_live_page and should_use_lazy_levels, and disable lazy level pages in place_pages when va % PAGE == 0 and Pages::pages already contains canonical_va(va).
  • CPU unit test vram::tests::should_use_lazy_levels_skips_gpu_aliased_sampled_textures.
  • D3D12 GPU test vram::tests::gpu_sampled_texture_aliasing_live_gpu_target_maps_eagerly_with_level_pages: places an R8G8B8A8_UNORM render target at a 64 KiB-aligned VA, clears it on the GPU, places a sampled R8G8B8A8_UNORM texture at the same VA with LEVEL_PAGES_FOR_TEST = true, and asserts !has_lazy_levels(srv) and that reading back the sampled view returns the cleared texels [64, 128, 192, 255].

Tested

  • NVIDIA GeForce RTX 5070 Ti, Windows 11, release test build.
  • cargo test --release -p xcl --lib -- should_use_lazy_levels gpu_sampled_texture_aliasing --nocapture: 2 passed, 0 failed.

Not tested

  • Full lib/workspace test suite on this head.
  • AMD/Intel GPUs, Wine, or rendered world frame comparison.

Regression run
150 s each, same title (ea3b5e4d), same GPU (NVIDIA GeForce RTX 5070 Ti), release build, warm DXIL cache (XCL_DIRECT_DXIL=all, XCL_HEAP_WINDOWS=1, XCL_DRED=1).

main (b49be183):

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1917 (PresentX 1917)
presents per 2 s (last 3): 0 0 2
device removals: 0
crashes: 0
peak commit: 33080 MB

nvidia-dev base (54225f58):

xcl-smoke v1
commit: 54225f5884a94cd23b7aab3f703a7c975070aa77
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 2209 (PresentX 2209)
presents per 2 s (last 3): 33 6 19
device removals: 0
crashes: 0
peak commit: 33702 MB

This PR's head (ae348ff5):

xcl-smoke v1
commit: ae348ff5a6cfaf857455742e6ba345937e8aac85
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 2198 (PresentX 2198)
presents per 2 s (last 3): 5 3 0
device removals: 0
crashes: 0
peak commit: 36462 MB

AI use
Antigravity (Gemini 4 Neon) ported the change, wrote the GPU test, ran the tests and smoke run, and drafted this description.

@andrewDoing

Copy link
Copy Markdown
Contributor

At ae348ff5a6cfaf857455742e6ba345937e8aac85 on RTX 5070 12 GB, driver 616.92, the sampled texture reused all four pages when the producer covered it completely. With a smaller producer at the same start address, it reused one page and acquired three more.

The sampled texture was 256×256 RGBA8 (four 64 KiB tiles). In each fresh process I GPU-cleared a live RGBA8 render target, waited for the queue, then placed the sampled texture with level pages enabled. I compared the pool/tile identities and XCL's per-device allocated texture-page counter before and after the sampled placement.

Live render target relative to the sampled texture Sampled placement Shared tiles Additional allocated page bytes Fenced readback of producer pixels through sampled resource
256×256, same start Eager 4 0 65,536 / 65,536 matched
128×128, same start Eager 1 196,608 (192 KiB) 16,384 / 16,384 matched
128×128, starts 64 KiB later, inside the sampled range Lazy 0 0 No GPU read of unmapped resource
128×128, disjoint Lazy 0 0 No GPU read of unmapped resource

For these aligned resources, a live first page triggered eager placement of the entire sampled texture. A live second page alone did not. The 192 KiB is the increase in XCL's allocated texture-page accounting for the three previously absent pages; DXGI residency and heap capacity were not measured. Four focused tests passed, zero skipped. Retail title behavior was outside this test.

AI use: OpenAI Codex (GPT-6) prepared the test-only fixture, ran the four native controls and wrote the report.

@Aniseto

Aniseto commented Nov 28, 2026

Copy link
Copy Markdown

Ran the aliased lazy-level-page tests at ae348ff5 on an NVIDIA Blackwell desktop part; the GPU test ran here, with no SKIPPED line.

System

  • GPU: NVIDIA GeForce RTX 5070 Ti (Blackwell, 32-lane waves only, mesh shaders), driver 32.0.16.2124
  • 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 should_use_lazy_levels_skips_gpu_aliased_sampled_textures -- --test-threads=1: 1 passed, 0 failed, 0 ignored.
  • XCL_ADAPTER=NVIDIA cargo test -p xcl --lib gpu_sampled_texture_aliasing_live_gpu_target_maps_eagerly_with_level_pages -- --test-threads=1: 1 passed, 0 failed, 0 ignored.

What this adds
Both tests pass: a sampled texture whose VA aliases a live GPU target skips lazy level pages, and the readback sees the target's cleared texels. The thread already has an RTX 5070 12 GB report; this adds a Blackwell 5070 Ti datapoint. These are synthetic tests, not a title launch.

Not tested
A retail title, a paired smoke run, the full workspace suite, 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.

@thehollowedone

thehollowedone commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Source review of head 74ef846. Three focused points before treating the GPU fixture as merge evidence:

  1. has_live_page tests membership in Pages::pages, but bind_range populates that map for ordinary sampled uploads as well. A live one-tile sampled texture at VA A followed by a four-tile sampled texture at A therefore selects eager placement, allocates the three missing tiles (192 KiB), and loses per-level reclamation without any GPU-owned producer. The current partial-overlap report covers a GPU target, not this sampled-only control. The eager decision needs producer ownership, not just page presence; please add that control and a released-producer case.

  2. The GPU fixture clears the producer in RENDER_TARGET and creates the reserved sampled alias in COPY_SOURCE. Its fence orders commands, but does not make those states compatible for per-tile data inheritance. Microsoft's D3D12 rule requires these reserved textures to be in COMMON for that inheritance, with an aliasing barrier between uses. Transition the producer to COMMON after the clear, create the alias in COMMON, issue the aliasing barrier, then transition/promote the alias for readback.

  3. The fixture's tier2(&dev) guard checks ResourceHeapTier, while Pages::new requires TiledResourcesTier >= 2. Check both capabilities before asserting a live page, so a heap-tier-2/tiled-tier-1 adapter skips this fixture correctly.

Update — RTX 3080 Ti reproduction of point 1. I extended the existing GPU fixture with an unaliased sampled control, then placed a one-tile sampled texture at another VA and a four-tile sampled texture at the same VA. The control remained lazy. On the unchanged head 74ef846, the sampled-only overlap became eager and allocated 196,608 additional page bytes. cargo test --offline --locked -j 4 -p xcl --lib gpu_sampled_texture_aliasing_live_gpu_target_maps_eagerly_with_level_pages -- --test-threads=1: 0 passed, 1 failed, 0 ignored, zero skips (Windows 11 build 26300, RTX 3080 Ti / 621.35, Ryzen 7 9800X3D, rustc 1.99.1 MSVC). The original GPU-target readback and unaliased control completed before this assertion. This is a test-only regression; no production fix or title behavior was tested.

Test-only regression diff on 74ef846
diff --git a/crates/xcl/src/hle/d3d12x/vram.rs b/crates/xcl/src/hle/d3d12x/vram.rs
index b65441ec..5f578d29 100644
--- a/crates/xcl/src/hle/d3d12x/vram.rs
+++ b/crates/xcl/src/hle/d3d12x/vram.rs
@@ -5178,6 +5178,37 @@ mod tests {
             rb.Unmap(0, None);
             assert_eq!(px, [64, 128, 192, 255], "sampled view did not read GPU-written target texels");
 
+            // A sampled texture without a preceding page is eligible for lazy levels.
+            let larger = D3D12_RESOURCE_DESC { Flags: D3D12_RESOURCE_FLAG_NONE, ..rt_desc };
+            let mut raw_control: *mut c_void = std::ptr::null_mut();
+            let hr = place_pages(dev.as_raw(), va + 0x2000_0000, &larger, D3D12_RESOURCE_STATE_COPY_SOURCE.0 as u32, std::ptr::null(), Some(bytes), iid, &mut raw_control);
+            assert!(hr.is_some_and(|h| h >= 0) && !raw_control.is_null(), "control sampled placement failed: {hr:?}");
+            let control = ID3D12Resource::from_raw(raw_control);
+            assert!(has_lazy_levels(control.as_raw() as usize), "an unaliased sampled texture should have lazy levels");
+            forget(control.as_raw() as usize);
+            drop(control);
+
+            // A sampled upload at a different VA is not evidence of a GPU-owned producer.
+            let sampled_va = va + 0x1000_0000;
+            LEVEL_PAGES_FOR_TEST.store(false, Ordering::Relaxed);
+            let small = D3D12_RESOURCE_DESC { Width: 128, Height: 128, Flags: D3D12_RESOURCE_FLAG_NONE, ..rt_desc };
+            let mut raw_small: *mut c_void = std::ptr::null_mut();
+            let hr = place_pages(dev.as_raw(), sampled_va, &small, D3D12_RESOURCE_STATE_COPY_SOURCE.0 as u32, std::ptr::null(), Some(128 * 128 * 4), iid, &mut raw_small);
+            assert!(hr.is_some_and(|h| h >= 0) && !raw_small.is_null(), "first sampled placement failed: {hr:?}");
+            let small = ID3D12Resource::from_raw(raw_small);
+            assert!(has_live_page(dev.as_raw() as usize, sampled_va));
+            let bytes_before = page_bytes().0;
+            LEVEL_PAGES_FOR_TEST.store(true, Ordering::Relaxed);
+            let mut raw_larger: *mut c_void = std::ptr::null_mut();
+            let hr = place_pages(dev.as_raw(), sampled_va, &larger, D3D12_RESOURCE_STATE_COPY_SOURCE.0 as u32, std::ptr::null(), Some(bytes), iid, &mut raw_larger);
+            assert!(hr.is_some_and(|h| h >= 0) && !raw_larger.is_null(), "second sampled placement failed: {hr:?}");
+            let larger = ID3D12Resource::from_raw(raw_larger);
+            assert!(has_lazy_levels(larger.as_raw() as usize), "sampled-only overlap should not disable lazy level pages ({} additional page bytes)", page_bytes().0 - bytes_before);
+            forget(larger.as_raw() as usize);
+            forget(small.as_raw() as usize);
+            drop(larger);
+            drop(small);
+
             forget(srv.as_raw() as usize);
             forget(rt.as_raw() as usize);
             drop(srv);

For a production fix, count GPU-owned references on each physical Own or Extra page key alongside total users. After resource tiling is known, check the keys the sampled binding would acquire under the same PAGES lock that commits the placement, stopping at the first producer. Keep the count through a deferred unmap; decrement when its NULL mapping completes or the last producer releases, and restore it on a fresh remap. A sampled user can keep a page resident without a producer. This avoids sampled/idle false positives and a check-to-bind race without scanning every target. Add controls for last-producer release with a sampled user remaining, two overlapping producers, a later-page overlap, and producer bind/release during sampled creation. This ownership design is source-reviewed, not implemented or tested here. Points 2 and 3 above remain source review only. The description's smoke block still names ae348ff5, so the current 74ef846 head also needs its required clean-head block. AI use: Codex (GPT-6.2 Astra) traced the pinned code and ownership lifecycle; Codex (GPT-6.2 Sol) checked the execution paths, ran the synthetic RTX regression, and wrote this review.

@throny

throny commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

merge-check claim
head: 74ef846

@throny

throny commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

merge-check
base: beeb81b
head: 74ef846
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2596 passed, 0 failed, 49 ignored (27 SKIPPED lines). Base alone at beeb81b (main after the merge of nvidia-dev): 2594 passed, 0 failed, 49 ignored.
review: the tests pass, but the points of the earlier review in the thread are unanswered and hold in the source. (1) The alias test only asks whether the first page at the texture's address exists in the page table. That table also holds the pages of other sampled textures and idle pages kept for a short time, so a sampled texture placed where another sampled texture was also loses its lazy levels; and a target that begins one page later (or an address that is not page-aligned) is not seen, so that case still reads zeros. (2) When it triggers, every level of the texture gets pages; only the tiles shared with the target cost nothing, so "zero extra video memory" in the description does not hold in general. (3) The check takes the pages lock on every placement even when level pages are off (the default); asking the switch first would avoid that. Also open: the GPU fixture relies on data surviving a state change that D3D12 does not promise, checks the heap tier where it means the tiled-resources tier, and prints SKIPPED itself instead of using skip_test. The smoke blocks in the description are for an earlier head, so the description check is red. No conflict with #3179 or #3192 (another file).
performance: default runs pay one extra lock per placement; with level pages on, more memory for each texture it triggers on. No translator source.
verdict: not yet: narrow the alias test to live targets (and to the texture's whole range), and refresh the smoke blocks.
hardware / tool and model: RTX 4080 16 GB (621.40), i9-12900K, Windows 11 26300, Rust 1.100.0, debug. Claude Code; merge, build and suite run by Claude Fable 5.2, diff review by Claude Opus 5.5 (every hunk read; nothing run on the GPU beyond the suite).

@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) needs-pr-check Failing the PR description check (CONTRIBUTING.md) labels Nov 28, 2026
@Aniseto

Aniseto commented Nov 29, 2026

Copy link
Copy Markdown

Re-ran the aliased lazy-level-page tests at 74ef8461 after the head moved from ae348ff5; this is a new response in the same thread, not an edit of my earlier report: #3188 (comment)

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 should_use_lazy_levels_skips_gpu_aliased_sampled_textures -- --test-threads=1: 1 passed, 0 failed, 0 ignored.
  • XCL_ADAPTER=NVIDIA cargo test -p xcl --lib gpu_sampled_texture_aliasing_live_gpu_target_maps_eagerly_with_level_pages -- --test-threads=1: 1 passed, 0 failed, 0 ignored. The GPU test ran here, with no SKIPPED line.

What this adds
Both tests still pass at the new head: a sampled texture whose VA aliases a live GPU target skips lazy level pages and reads back the target's cleared texels. This adds the new head and driver 32.0.16.2140 as dimensions; I did not run a title.

Not tested
A retail title, a paired smoke run, the full workspace suite, 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.

@ettehn

ettehn commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

I prototyped the producer-ownership approach from the review above on main d52ff48c. The current 74ef8461 also conflicts in vram.rs against that base.

The candidate counts mapped GPU owners by exact Own and Extra keys. After tiling, the sampled placement uses owned_pages and the actual PC tile count to query only the keys it would bind. The ordered index supports range lookups inside the existing placement lock. A deferred unmap keeps the producer entries through the title-queue wait and drops them when the NULL mapping is submitted. Cancelling that unmap transfers the same owners back.

Windows Release CPU tests on the local candidate passed 6/6, including later and unaligned overlap, exact extra-tile keys, the last of two producer releases, and the existing level-ownership controls. A positive overlap still maps the entire texture, including non-shared tiles.

Native GPU follow-up. Windows Release on an RTX 5070 Ti Laptop (621.24). The combined synthetic run passed 7/7 with zero skips. Two 128×128 RGBA8 reserved textures shared one physical page. With the producer transitioned to COMMON, an aliasing barrier and a fenced copy, all 16,384 pixels matched the GPU clear. After releasing the producer’s mapping, a sampled-only placement stayed lazy. A separate control verified that a 256×256 sampled texture reused the existing physical tile when the only GPU producer began on its second page. The latter control checked mapping metadata. Streaming cost remains unmeasured. D3D12 aliasing and data inheritance

OpenAI Codex (GPT-6.2 Sol) assisted with implementation and Windows verification.

…ve GPU page

Port of amd-dev dece6dd (part 1) to nvidia-dev: under
XCL_TEXTURE_LEVEL_PAGES=1, a sampled Texture2D placed at the same 64 KiB
page as a live GPU-owned texture (render target, depth buffer, or UAV)
has all-zero CPU memory at that VA, so map_levels_with_texels never maps
its tiles and shaders read zeros.

Disable lazy level pages in vram::place_pages when the 64 KiB-aligned VA
already has a live page in Pages::pages, mapping eagerly onto the
existing shared pages at zero extra VRAM cost.

Includes CPU unit tests and a D3D12 GPU test verifying that a sampled
Texture2D placed at the VA of a cleared render target under
XCL_TEXTURE_LEVEL_PAGES=1 maps eagerly and reads back the GPU-written
texels.

Co-Authored-By: Antigravity (Gemini 4 Neon) <noreply@google.com>
@Kalmarv
Kalmarv force-pushed the neon/lazy-aliased-nv branch from 74ef846 to c8faf77 Compare December 1, 2026 00:50
@github-actions github-actions Bot removed the needs-pr-check Failing the PR description check (CONTRIBUTING.md) label Dec 1, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Review (head c8faf77, diff read, not a merge-check: the PR description check fails, so not built or run here yet):

  • The alias check catches more than GPU targets. has_live_page (vram.rs ~2972-2975) only asks whether Pages::pages holds the address, and that map also holds pages of sampled textures and idle pages with no users, kept for 2 s (release_tiles, IDLE). So a streamed texture placed again at the same address maps all its levels eagerly, undoing level pages for exactly the textures the feature saves memory on. Checking that the page has users and that a GPU-written target owns it would fix that.
  • It also only looks at the first 64 KiB page, misses targets placed in an arena, and does nothing if the target is placed after the texture.
  • The check runs on every place_pages call even with level pages off (~2999); testing level_pages() first would avoid the extra lock and lookups.
  • The smoke run didn't set XCL_TEXTURE_LEVEL_PAGES=1, so it never reached this code, and it ran on ae348ff (on top of nvidia-dev) rather than this head, which is why PR description fails. The full suite wasn't run.
  • The GPU test skips with println!("SKIPPED...") (~5687) instead of skip_test, and fails rather than skips if the target falls back to an arena.

Fine: the diagnosis matches main (lazy levels only get pages from non-zero CPU texels, map_levels_with_texels_in), the GPU test fails without the change and holds VRAM_TEST_LOCK and resets LEVEL_PAGES_FOR_TEST, and with level pages off placement behaves as before.

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

Test report (not a merge-check: PR description red)
base: b2b1f10
head: c8faf77
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2904 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: not repeated here; my review of this same head is at #3188 (comment), and its findings stand. This run adds what reading couldn't: whether the change merges, builds and passes the whole suite on current main.
performance: no smoke run.
assessment: merges, builds and passes the whole suite on current main (0 failed); the findings in my review stand
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.

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.

7 participants