Skip to content

D3D12.X: make shadowed textures active again when a draw samples them - #2956

Draft
BBareth wants to merge 1 commit into
xcl-dev:nvidia-devfrom
BBareth:d3d12x/uncover-sampled-shadowed
Draft

BBareth wants to merge 1 commit into
xcl-dev:nvidia-devfrom
BBareth:d3d12x/uncover-sampled-shadowed

Conversation

@BBareth

@BBareth BBareth commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

When a newer texture is placed over part of an older one, title_textures::Table::insert shadows the older one (and unmaps it if the new placement covers a different range). Shadowed textures are never uploaded again. A title can still sample them, though. GTA 6 does this with the Bink Y/Cr/Cb planes of its credit-card scene: a newer placement lands over part of the planes, the GPU then reads zeros from them, and the cards render green with blocky streaks. In one 355 s run, more than 131k draws sampled shadowed textures.

This change makes a shadowed texture active again when a draw binds it. The active textures that overlap it are shadowed but not unmapped. The texture is remapped if it was unmapped, marked pending unless the GPU wrote it, and its known content is forgotten, so the next submit uploads it again. When two overlapping textures are made active in turn before a submit (one draw sampling both, or two draws in one submission), the first is shadowed again, but it stays pending and the next submit still uploads it, since a draw already recorded samples it. ettehn and andrewDoing found this case in review.

Part of #505. Based on nvidia-dev eae0150. It merges into the current tip a5e2d85 without conflicts, and none of the 15 newer commits touches shadowing.

What is in it

  • crates/xcl/src/hle/d3d12x/title_textures.rs:
    • Table::uncover, uncover_drawn and has_shadowed (an AtomicBool, so draws skip the scan when nothing is shadowed);
    • Table::displaced: pending textures that uncover shadowed again, which the next refresh still uploads;
    • the unit test a_sampled_shadowed_texture_is_active_again, and the GPU test gpu_both_overlapping_textures_a_draw_samples_go_up (A then B made active, one refresh, both read back; on 8fb4127, without displaced, A reads back its old bytes).
  • crates/xcl/src/hle/d3d12x/bind.rs: uncover_bound_tables reads the first 64 slots of each bound descriptor table that hold a T#. It resolves them to title textures through console_heap::named_by_index and passes them to uncover_drawn. It is called from bind_abi and, for graphics only, from prepare_two_pass_stage.

Tested

  • Hardware and build: RTX 5070 Ti (driver 32.0.16.2124), i7-12700K, Windows 11 Pro 26200, rustc 1.95.0, release builds.
  • cargo test --workspace --release --no-fail-fast -- --test-threads=4 on d527335: 1815 passed, 12 failed, 48 ignored.
    • 11 of them are failures of the base commit eae0150 without this change (listed in the earlier base run and in retrope13's report). split_compute_prefers_the_offline_whole_pipeline and two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs each count twice, because their harness runs them again:
      • fuzz_validate_and_gpu_0..3;
      • gpu_compute_thread_limits_leave_the_lanes_past_them_out;
      • d3d12_mesh_stream console_ms_in_as_slot_and_ms_with_as_are_refused_cleanly;
      • three gpu_compute_e2e failures (async_compute_pso_registers_real_pso_and_not_standin, and split_compute_prefers_the_offline_whole_pipeline twice);
      • gpu_two_pass_cache_e2e, twice.
    • The 12th, sparse::tests::a_tile_decommitted_and_committed_again_keeps_its_video_memory (assert!(s.drain_quarantined())), is in a file this PR does not touch. It passed 3 of 3 runs on its own and in cargo test --release -p xcl --lib hle::d3d12x::sparse (62 passed), so I take it for a timing flake under the parallel suite.
    • The two texture_upload overflow tests that failed on the base in my earlier run passed this time.
    • 30 SKIPPED lines, for these reasons: no wave64 (21), no heap windows, no debug layer / d3d12SDKLayers.dll, and refused f16 instructions.
  • cargo test --release -p xcl --lib title_textures: 31 passed.
  • GTA 6 (retail 1.0.2.1), 240 s with one A press at 70 s, run on the base eae0150 and on the first version of this PR (8fb4127, before displaced), with the same environment:
    • frames: base 2730, PR 2880;
    • in both runs, presents become erratic after about 100 s and stop at about 130 s, while the world loads. This stall exists without this change too;
    • the PR run logged the texture being made active again 7 times, from 131.6 s.
  • On a lab tree with the same change, based on the older nvidia-dev commit 782fde7, the credit cards render correctly (frames 1559–1589 of that run). Without the change, they are green.

Regression run

The main block is from b49be18, the last main commit before the shader-preparation screen (#2555). On the current main tip, shader preparation did not finish within 2 h on this machine. All blocks were made with XCL_PRESS_A_AT=70 and XCL_DXC_DIR set, one run at a time, with warm shader caches. I ran two rounds and include both, unedited. The PR blocks were rerun on d527335; the main and base blocks are from the same rounds as before.

This does not meet the limits against main.

  • Frames: the PR has 82 % and 77 % of main's frames.
  • Peak commit: the PR is at 132 % and 141 % of main's.
  • The nvidia-dev base without this change already fails the commit limit against main (117 % and 139 %), and has 85 % and 82 % of main's frames.

Against its own base, the PR has 97 % and 95 % of the frames, and 112 % and 102 % of the peak commit.

  • In every nvidia-dev block, presents are 0 at the end (the title stops presenting while the world loads); on main they are not.
  • The PR runs logged 7 and 8 "made active again" lines, so the code path ran.

Round 1, main b49be18:

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 120
exit: 0x0
frames shown: 2911 (PresentX 2911)
presents per 2 s (last 3): 0 2 15
device removals: 0
crashes: 0
peak commit: 26703 MB

Round 1, nvidia-dev base eae0150 (without this change):

xcl-smoke v1
commit: eae015021c639866ab061cdff2e9daf8ba9daac5
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 120
exit: 0x0
frames shown: 2471 (PresentX 2471)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 31342 MB

Round 1, this PR, d527335:

xcl-smoke v1
commit: d52733518f8a980a8218ffd0a78d2c527f4b4bc5
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 120
exit: 0x0
frames shown: 2397 (PresentX 2397)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 35198 MB

Round 2, main b49be18:

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 120
exit: 0x0
frames shown: 2952 (PresentX 2953)
presents per 2 s (last 3): 4 5 13
device removals: 0
crashes: 0
peak commit: 26041 MB

Round 2, nvidia-dev base eae0150:

xcl-smoke v1
commit: eae015021c639866ab061cdff2e9daf8ba9daac5
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 120
exit: 0x0
frames shown: 2409 (PresentX 2409)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 36078 MB

Round 2, this PR, d527335:

xcl-smoke v1
commit: d52733518f8a980a8218ffd0a78d2c527f4b4bc5
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 120
exit: 0x0
frames shown: 2280 (PresentX 2280)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 36624 MB

Not tested

  • The credit cards on nvidia-dev itself: with this environment, both the base and this PR stall while loading, before the scene is reached. The visual fix was checked only on the lab tree described above.
  • AMD GPUs.
  • Any title other than GTA 6.
  • A texture that is active (not pending) and is displaced later in the same submission misses that submission's dirty-page updates, because those are tracked only for active entries. It catches up at the next refresh that has work. No test covers this.
  • Frame-time cost when draws keep alternating between two overlapping textures (each upload is whole). This was not measured.
  • The CI run on the no-GPU runner failed sparse::tests::a_gpu_write_into_a_gpu_view_lands_in_its_cpu_view with device removal (0x887a0005). That test is in sparse.rs, which this PR does not touch; it passes here.

AI use

  • Claude Code with Claude Opus 5.5 found the root cause on the lab tree, planned and reviewed the port, analysed the runs, and wrote the commit message and this description. It also opened this draft on the author's instruction. The author reviews the draft before marking it ready.
  • Codex CLI with gpt-6.2-sol ported the fix to nvidia-dev, wrote the unit test, and ran the first test suite and the first GTA 6 runs.
  • Claude Code with Claude Opus 5.5 wrote the displaced fix for the case found in review and its GPU test, and ran the test suite and the smoke runs on d527335.

🤖 Generated with Claude Code

@ettehn

ettehn commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

I checked the overlapping-descriptor case called out under Not tested on 8fb41275. A standalone Windows x64 fixture used the PR’s descriptor scanner, uncover_drawn and Table with fake resources. A starts shadowed and B is the active overlapping placement. With one bound table, the results were:

Slots scanned Active after the scan Pending upload
A A A
A, B B B
B, A A A

There is a potential correctness case beyond repeated uploads. The scanner visits table entries regardless of which slot the shader actually samples. If a draw uses only A with B also present later in the table, B can undo A’s reactivation before submission and remove A from the pending set. This verifies CPU state and descriptor traversal only, with COM and GPU operations stubbed. Pixel output and frame-time cost remain unmeasured.

OpenAI Codex assisted with verification.

@andrewDoing

Copy link
Copy Markdown

I followed up on the overlapping-descriptor observation with a native GPU readback on RTX 5070 / NVIDIA 616.92. This uses the direct parent eae015021c639866ab061cdff2e9daf8ba9daac5 and PR head 8fb41275ee8b5fbb3191f559cc767d6e9072e832, each in a separate build target and each case in a fresh process.

Two 64×64 R8 committed textures had partially overlapping guest ranges. Each run verified A's initial GPU pixels and B's updated GPU pixels, then supplied an ordering directly to uncover_drawn (on the PR head), called the production refresh, and read A back by GPU copy:

Revision Reactivation order A texels matching current guest bytes
Parent A, before reactivation existed 0 / 4,096
Head A 4,096 / 4,096
Head A then B 0 / 4,096
Head B then A 4,096 / 4,096

The A-then-B case preserves all 4,096 old A texels, while the A-only and B-then-A controls restore all 4,096. This extends the earlier CPU-state report to the pixel consequence of the ordering. The fixture supplied the resource order itself; it did not execute descriptor scanning or shader sampling, use tiled resources, or run a retail title. It therefore does not establish the frequency or visual impact in gameplay.

AI use: OpenAI Codex with GPT-6.2 Astra built and ran the fixture; Codex reviewed the results and prepared this report.

@andrewDoing

Copy link
Copy Markdown

I tested a separate-draw case on RTX 5070 / NVIDIA 616.92, comparing the same two native draws on parent eae015021c639866ab061cdff2e9daf8ba9daac5 and head 8fb41275ee8b5fbb3191f559cc767d6e9072e832 in fresh processes. Two committed R8 textures have partially overlapping guest ranges. Each draw's pixel shader reads only its own texture and writes one 2,048-pixel half of an output target.

For the head I supplied only the corresponding resource to uncover_drawn immediately before recording each draw, then called production refresh after closing each list and before execution. This models the ordering of bind-time reactivation and submission-time refresh, but does not invoke the guest bind or KickoffX paths themselves.

Revision Submission pattern A half matching current bytes B half matching current bytes
Parent One list / one refresh 0 / 2,048 2,048 / 2,048
Parent Separate lists / refreshes 0 / 2,048 2,048 / 2,048
Head One list / one refresh 0 / 2,048 2,048 / 2,048
Head Separate lists / refreshes 2,048 / 2,048 2,048 / 2,048

All stale A pixels matched the old value. Thus this synthetic committed-texture case also needs attention across draws in one submission: identifying exactly which resource each shader reads is insufficient if the second reactivation cancels the first before the shared refresh. The fixture did not exercise real guest binding, tiled resources, a retail title, or performance.

AI use: OpenAI Codex with GPT-6.2 Astra built and ran the fixture; Codex reviewed and prepared this report.

@retrope13

Copy link
Copy Markdown

Verified PR #2956 (8fb41275) independently on Windows 11 across NVIDIA Ada Lovelace (8 GB) and Intel UHD Graphics (targeted title_textures + bind runs) and reviewed the diff. All targeted tests pass on both GPUs and the full workspace suite shows zero new failures beyond the documented nvidia-dev eae01502 baseline.

Environment

  • OS: Windows 11 26H2 (build 26300.9818)
  • CPU / RAM: Intel Core i5-14500HX (14C/20T), 24 GB RAM
  • GPUs:
    • NVIDIA GeForce RTX 4060 Laptop GPU (8 GB, Ada Lovelace, wave32, driver 32.0.16.2140)
    • Intel(R) UHD Graphics (driver 32.0.101.7092)
  • Toolchain: rustc 1.100.0 (stable-x86_64-pc-windows-msvc), VS 2022 Build Tools 17.14.45, Windows SDK 10.0.26100.0 (DXC 1.9.2602.17)

Test Results (8fb41275)

Command Adapter Result
cargo test --locked -p xcl --lib -- title_textures -- --test-threads=1 NVIDIA RTX 4060 Laptop 30 passed, 0 failed (2.63s, includes a_sampled_shadowed_texture_is_active_again)
cargo test --locked -p xcl --lib -- hle::d3d12x::bind:: -- --test-threads=1 NVIDIA RTX 4060 Laptop 53 passed, 0 failed (0.48s)
cargo test --locked -p xcl --lib -- title_textures -- --test-threads=1 Intel(R) UHD Graphics 30 passed, 0 failed (2.14s)
cargo test --locked -p xcl --lib -- hle::d3d12x::bind:: -- --test-threads=1 Intel(R) UHD Graphics 53 passed, 0 failed (0.51s)
cargo build --workspace --locked && cargo test --workspace --locked --no-fail-fast -- --test-threads=4 NVIDIA RTX 4060 Laptop 1,817 passed, 9 failed, 48 ignored (30 SKIPPED lines)

All 9 failures in the workspace run are a subset of the documented pre-existing nvidia-dev eae01502 failures (gpu_compute_thread_limits_leave_the_lanes_past_them_out, texture_upload::tests::gpu_parts_of_a_texture_go_up_and_the_rest_stays, texture_upload::tests::pieces_hold_the_texels_of_their_levels, console_ms_in_as_slot_and_ms_with_as_are_refused_cleanly, async_compute_pso_registers_real_pso_and_not_standin, split_compute_prefers_the_offline_whole_pipeline ×2, two_pass_driver_cache_reuses_both_passes_and_recovers_corrupt_blobs ×2; fuzz_validate_and_gpu_0..3 passed here).

Diff Review Notes

  • crates/xcl/src/hle/d3d12x/title_textures.rs:
    • Table::uncover preserves by_va's disjointness invariant, updates pending_res and pending_delta cleanly on both displaced and restored entries, keeps displaced textures mapped for in-flight draws, resets max_shadowed_size when shadowed empties, and HAS_SHADOWED is updated across all four mutation sites (register, retire_overlapping, forget, uncover_drawn).
    • uncover_drawn clones resources under TEXTURES and drops the mutex before calling vram::remap_if_unmapped / vram::map_levels, avoiding any TEXTURES ↔ vram::PAGES lock-order inversion.
  • crates/xcl/src/hle/d3d12x/bind.rs:
    • uncover_bound_tables short-circuits via has_shadowed(), safely falls back to copy_readable outside committed window 0, filters to texture T# descriptors (slot[15] >> 4 >= 8 && index != 0), and resolves byte-copied descriptors through console_heap::named_by_index.

Not Tested

  • Live retail title runs / visual verification of shadowed Bink planes (no retail game files used).
  • AMD or native wave64 hardware.
  • The 48 #[ignore] tests in the workspace suite.

(AI disclosure: diff review, test execution commands, and summary formatting were assisted by Antigravity / Gemini 4; all tests were executed locally on the hardware listed above.)

Table::insert shadows the active textures a newer one overlaps, and a
placed texture over a different range is unmapped as well. Shadowed
textures are never uploaded again, but a title can keep sampling them:
GTA 6 samples the Bink planes of its credit-card scene after a newer
placement lands over part of them, so the GPU read zeros and the cards
rendered green with blocky streaks (over 131k draws sampled shadowed
textures in one 355 s run).

When a draw binds descriptor tables, the textures they name that are
shadowed become active again: the active ones overlapping them are
shadowed without being unmapped, the texture is marked pending (unless
the GPU wrote it), remapped if it was unmapped, and its known content
is forgotten so the next submit uploads it.

When two overlapping textures are made active in turn before a submit
(one draw sampling both, or two draws of one submission), the second
shadows the first again. The first stays pending and the next submit
uploads it all the same, since a draw already recorded samples it.

Assisted-by: Codex CLI (gpt-6.2-sol)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@BBareth
BBareth force-pushed the d3d12x/uncover-sampled-shadowed branch from 8fb4127 to d527335 Compare November 27, 2026 07:11
@BBareth

BBareth commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @ettehn and @andrewDoing, the A-then-B case was a real bug, and your readbacks pinned it down. Thanks @retrope13 for the test run.

Fixed in d527335, squashed into the one commit. When a texture that is still pending is shadowed again by a later uncover (one draw sampling both, or two draws in one submission), it now goes into Table::displaced. The next refresh uploads it anyway, since a draw already recorded samples it, and it stays pending, so it goes up whole again if it is made active later.

Tests:

  • The unit test a_sampled_shadowed_texture_is_active_again now also checks that A stays pending and is queued for the next submit.
  • New GPU test gpu_both_overlapping_textures_a_draw_samples_go_up: two overlapping committed R8 textures, uncover_drawn with A then B, one refresh, then A and B read back. On 8fb4127, A reads back the old bytes (0x11); with the fix, both match the current guest bytes. RTX 5070 Ti, driver 32.0.16.2124.

Still open, and listed under Not tested: a texture that is active (not pending) and is displaced later in the same submission misses that submission's dirty-page updates, because those are tracked only for active entries. It catches up at the next refresh that has work.

AI use: Claude Code (Claude Opus 5.5) wrote this fix, its tests and this reply.

🤖 Generated with Claude Code

@andrewDoing

Copy link
Copy Markdown

I reran the same two-draw native RTX 5070 fixture from my earlier report on updated head d52733518f8a980a8218ffd0a78d2c527f4b4bc5, in an isolated build and fresh processes.

  • Both draws in one list / one refresh: A and B each produced 2,048 / 2,048 current pixels, with zero old A pixels. The previous head produced 0 / 2,048 current A pixels in this case.
  • Separate lists / refreshes: A and B each produced 2,048 / 2,048 current pixels.

Both focused runs passed without skips. As before, the fixture supplies each draw's resource directly to uncover_drawn and models refresh timing; it does not run guest binding or a retail title. It does not cover the separate active-but-not-pending dirty-page limitation noted in the update.

AI use: OpenAI Codex with GPT-6.2 Astra ran the retest; Codex reviewed and prepared this report.

@retrope13

Copy link
Copy Markdown

Re-verified d5273351 (nvidia-dev) locally on Windows 11 26H2 (rustc 1.100.0, debug) across NVIDIA, Intel, and WARP:

Results (--test-threads=1)

  • NVIDIA GeForce RTX 4060 Laptop GPU (driver 32.0.16.2140):
    • cargo test --locked -p xcl --lib hle::d3d12x::title_textures::tests:: -- --test-threads=1: 31 passed, 0 failed (including a_sampled_shadowed_texture_is_active_again and gpu_both_overlapping_textures_a_draw_samples_go_up)
    • cargo test --locked -p xcl --lib hle::d3d12x::bind:: -- --test-threads=1: 53 passed, 0 failed
  • Intel(R) UHD Graphics (driver 32.0.101.7092):
    • title_textures::tests::: 31 passed, 0 failed
    • bind::: 53 passed, 0 failed
  • WARP (XCL_ADAPTER=warp):
    • title_textures::tests::: 31 passed, 0 failed
    • bind::: 53 passed, 0 failed

Code-reading note on Table::displaced (crates/xcl/src/hle/d3d12x/title_textures.rs)

One small edge case spotted while reading d5273351:

  • In Table::uncover (lines 377-399), if a shadowed CPU-written texture A is uncovered (A.pending = true, PENDING: 0 → 1) and then an overlapping shadowed gpu_written texture B is uncovered before refresh, A moves into self.displaced and pending_delta drops PENDING back to 0 (while B.pending stays false).
  • In refresh_parts (line 1856):
    if dirty_len == 0 && PENDING.load(Ordering::Relaxed) == 0 && AGAIN.lock().unwrap().is_empty() {
        return;
    }
    If no pages were dirtied since the last look (dirty_len == 0) and AGAIN is empty, refresh_parts returns early without draining t.displaced or uploading A (tracking an atomic HAS_DISPLACED or including displaced in that early-return check would close that gap; clearing self.displaced.remove(&res) in Table::remove / Table::uncover would also avoid stale entries across COM address reuse).

Not Tested

  • Full workspace suite on d5273351 or retail titles / xcl-smoke.
  • Reproducing the gpu_written B + dirty_len == 0 edge case above in a dedicated GPU test (code reading only).
  • bind::tests::two_pass_binds_set_the_shared_view_heap (skipped without XCL_HEAP_WINDOWS=1).

AI disclosure: Antigravity (Gemini 4) assisted with reading the d5273351 diff, running the local test commands, and drafting this report; all test runs executed natively on my Windows 11 RTX 4060 / Intel UHD laptop.

@andrewDoing

Copy link
Copy Markdown

I reproduced @retrope13's gpu_written / displaced early-return case with native GPU output on RTX 5070 / driver 616.92, at head d52733518f8a980a8218ffd0a78d2c527f4b4bc5.

The fixture uses partially overlapping committed R8 textures. It first uploads A's old value, updates the guest bytes, and gives B a distinct value through a real GPU copy followed by note_gpu_write. It then supplies A and B individually to uncover_drawn as it records two actual draws, with no subsequent guest writes. Each pixel shader reads only its own texture. Before refresh, A is displaced and AGAIN is empty.

Case Pending before refresh A output (2,048 pixels) B output (2,048 pixels) A displaced after refresh
Normal 0 All old 0x19 All GPU-written 0xc5 Yes
Control: unrelated disjoint pending texture 1 All current 0x83 All GPU-written 0xc5 No

Each case ran in a fresh process. The normal case fails the expected-A assertion; the control passes it. This supports the early-return hypothesis and shows its pixel consequence in this fixture while preserving the GPU-written B value. The fixture supplies reactivation and the write notification directly; guest binding, tiled textures, retail incidence, and performance were not tested.

AI use: OpenAI Codex with GPT-6.2 Astra built and ran the fixture; Codex reviewed and prepared this report.

@thehollowedone

Copy link
Copy Markdown
Contributor

Follow-up to the native GPU reproduction on this head: I tested a narrow guard for the gpu_written B / displaced A early return on #2956 head d5273351. PENDING counts active pending textures, so it can reach zero while Table::displaced still owes an upload for the next refresh. The candidate mirrors whether that set is nonempty in an atomic flag, includes it in the idle-return check, and removes queued IDs when a resource is released, retired, replaced or reactivated.

On Windows 11 build 26300, RTX 3080 Ti (621.35), Ryzen 7 9800X3D, Rust 1.99.1 MSVC, DXC 1.8.2502.11, with XCL_REQUIRE_GPU=1:

  • I extended the PR's native gpu_both_overlapping_textures_a_draw_samples_go_up test. After a guest write is consumed while A is shadowed, it marks B GPU-written through note_gpu_write, uncovers A then B, confirms PENDING == 0 and A displaced, and reads A after the next refresh. On the unchanged production head, 0 passed, 1 failed, 0 ignored, with A retaining its old pixel value. With the candidate, 1 passed, 0 failed, 0 ignored, all 4,096 A pixels current; no skip. This extension models the GPU-written state after B's GPU content was already updated by the fixture; it does not perform a new GPU copy for that marking step.
  • cargo test --offline --locked -j 4 -p xcl --lib hle::d3d12x::title_textures::tests:: -- --test-threads=1 with the candidate: 31 passed, 0 failed, 0 ignored. The unit test also checks that reactivated and released resources leave displaced.
Local candidate diff (not committed)
diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs
index 5c139fea..bc4d6806 100644
--- a/crates/xcl/src/hle/d3d12x/title_textures.rs
+++ b/crates/xcl/src/hle/d3d12x/title_textures.rs
@@ -269,2 +269,3 @@ impl Table {
             if let Some(old) = self.by_res.remove(&ores) {
+                self.displaced.remove(&ores);
                 self.unlink_src(ores, old.src, old.tex.size() as usize);
@@ -282,2 +283,3 @@ impl Table {
             if let Some(old) = self.shadowed.remove(&sres) {
+                self.displaced.remove(&sres);
                 if old.placed && !old.unmapped {
@@ -300,2 +302,3 @@ impl Table {
     fn insert(&mut self, res: usize, mut entry: Entry) -> (isize, Vec<usize>) {
+        self.displaced.remove(&res);
         let va = entry.va;
@@ -365,2 +368,3 @@ impl Table {
         let mut e = self.shadowed.remove(&res)?;
+        self.displaced.remove(&res);
         self.shadowed_by_va.remove(&(e.va, res));
@@ -410,2 +414,3 @@ impl Table {
     fn remove(&mut self, res: usize) -> (bool, isize, Vec<usize>) {
+        self.displaced.remove(&res);
         if let Some(old) = self.shadowed.remove(&res) {
@@ -436,2 +441,3 @@ impl Table {
             if let Some(mut e) = self.shadowed.remove(&sres) {
+                self.displaced.remove(&sres);
                 // (the one that lay over it may have been uploaded over the same video memory)
@@ -498,2 +504,3 @@ static TEXTURES_LEN: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicU
 static HAS_SHADOWED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
+static HAS_DISPLACED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
 /// cached `watch_ranges` keyed by `watch_key`
@@ -589,2 +596,3 @@ pub(super) unsafe fn register(device: *mut c_void, res: *mut c_void, va: usize,
         HAS_SHADOWED.store(!t.shadowed.is_empty(), Ordering::Relaxed);
+        HAS_DISPLACED.store(!t.displaced.is_empty(), Ordering::Release);
         clone_resources(&unmap_ptrs)
@@ -895,2 +903,3 @@ pub(super) unsafe fn retire_overlapping(va: usize, len: usize) {
         HAS_SHADOWED.store(!t.shadowed.is_empty(), Ordering::Relaxed);
+        HAS_DISPLACED.store(!t.displaced.is_empty(), Ordering::Release);
         (retired, clone_resources(&unmap_ptrs))
@@ -914,2 +923,3 @@ pub fn forget(res: usize) {
         HAS_SHADOWED.store(!t.shadowed.is_empty(), Ordering::Relaxed);
+        HAS_DISPLACED.store(!t.displaced.is_empty(), Ordering::Release);
         (removed, unsafe { clone_resources(&remap_ptrs) })
@@ -958,2 +968,3 @@ pub(super) fn uncover_drawn(res: &[usize]) {
         HAS_SHADOWED.store(!t.shadowed.is_empty(), Ordering::Relaxed);
+        HAS_DISPLACED.store(!t.displaced.is_empty(), Ordering::Release);
         (restored, unsafe { clone_resources(&remap) })
@@ -1855,3 +1866,3 @@ unsafe fn refresh_parts(queue: *mut c_void, look: bool) {
     recheck_streamed_unwatched(&ranges, recheck_frame);
-    if dirty_len == 0 && PENDING.load(Ordering::Relaxed) == 0 && AGAIN.lock().unwrap().is_empty() {
+    if dirty_len == 0 && PENDING.load(Ordering::Relaxed) == 0 && !HAS_DISPLACED.load(Ordering::Acquire) && AGAIN.lock().unwrap().is_empty() {
         return;
@@ -1867,2 +1878,3 @@ unsafe fn refresh_parts(queue: *mut c_void, look: bool) {
         let displaced = std::mem::take(&mut t.displaced);
+        HAS_DISPLACED.store(false, Ordering::Release);
         names.extend(displaced.iter().copied());
@@ -3913,2 +3925,3 @@ pub(super) mod tests {
         assert!(tbl.by_res.contains_key(&1));
+        assert!(!tbl.displaced.contains(&1), "reactivated A is owned by the active pending queue");
 
@@ -3922,2 +3935,13 @@ pub(super) mod tests {
         assert!(tbl.by_res.contains_key(&10) && !tbl.by_res[&11].pending && tbl.shadowed.contains_key(&12));
+
+        let mut released = Table::new();
+        let mut gpu_b = entry(0x30_0000, &tex_64k, false, false);
+        gpu_b.gpu_written = true;
+        released.insert(20, entry(0x30_4000, &tex_16k, true, false));
+        released.insert(21, gpu_b);
+        assert!(released.uncover(20).is_some());
+        assert!(released.uncover(21).is_some());
+        assert!(released.displaced.contains(&20));
+        assert!(released.remove(20).0);
+        assert!(!released.displaced.contains(&20), "released resources cannot be uploaded after address reuse");
     }
@@ -3977,2 +4001,15 @@ pub(super) mod tests {
             assert_eq!(read_r8(dev, &queue, &b), vec![0x33; 64 * 64]);
+            // Consume the next guest write while A is shadowed, then model a GPU write to B.
+            // B must not become pending when it displaces A before the following refresh.
+            std::ptr::write_bytes(base as *mut u8, 0x44, 2 * size);
+            refresh(queue.as_raw());
+            note_gpu_write(0, kb);
+            uncover_drawn(&[ka, kb]);
+            assert_eq!(PENDING.load(Ordering::Relaxed), 0);
+            assert!(TEXTURES.lock().unwrap().displaced.contains(&ka));
+            assert!(HAS_DISPLACED.load(Ordering::Acquire));
+            refresh(queue.as_raw());
+            assert!(!HAS_DISPLACED.load(Ordering::Acquire));
+            let a_pixels = read_r8(dev, &queue, &a);
+            assert_eq!(a_pixels.iter().filter(|&&v| v == 0x44).count(), 64 * 64, "displaced A goes up with no dirty pages or active pending texture");
             drop((_rb, _ra));

This closes the measured early-return path for committed textures. The PR's separate active-but-not-pending dirty-page limitation still needs a focused regression and a design that retains those bytes through the refresh; this test does not validate placed/tiled overlaps, guest binding, a retail title, the full workspace suite, or paired smoke. AI use: Codex (GPT-6.2 Sol) wrote and ran the local GPU regression and candidate; Codex (GPT-6.2 Astra) independently traced the displaced-set lifecycle and refresh ordering.

@thehollowedone

Copy link
Copy Markdown
Contributor

On #2956 head d5273351, I reproduced the active-but-not-pending case listed under Not tested. This is separate from the pending A / GPU-written B early return.

The added table regression models A as already uploaded (pending = false), with A and B overlapping in guest video memory but reading from distinct CPU source blocks. After uncover(B) displaces A, Table::uncover unlinks A's source and does not add A to displaced. The next cached_watch_ranges can omit A's 16 MiB source block, and refresh_parts has no displaced A to consider. The assertion below fails on the unchanged head at “A's source must stay watched until refresh.”

Windows 11 build 26300, Ryzen 7 9800X3D, RTX 3080 Ti (621.35), Rust 1.99.1 MSVC. cargo test --offline --locked -j 4 -p xcl --lib a_sampled_shadowed_texture_is_active_again -- --test-threads=1: 0 passed, 1 failed, 0 ignored, no GPU skip notice. This verifies table bookkeeping, not GPU pixels or retail incidence for this case.

Adding A only to displaced would not restore its missing watched range. A repair needs to keep the source watched, include an already-uploaded displaced A in dirty-page selection, and upload it only when dirty. The lifetime needs care: refresh_parts drains the global displaced set on any refresh, including an unrelated submission. Keeping the watch for one refresh alone can therefore lose a later CPU write before the command list that sampled A submits. Please retain the sampled resource obligation through the owning list's execution or replay (and clear it on reset/release), or provide an equivalent bounded way to keep displaced aliases eligible. Source changes, unmapping, and AGAIN retries also need to preserve or retire that obligation correctly. I have not validated a production fix for those lifecycle paths. A GPU regression that submits the recorded draw after an intervening refresh and later CPU write would distinguish a complete fix from a one-refresh fix; a runtime change also needs the required paired smoke.

Failing table regression
diff --git a/crates/xcl/src/hle/d3d12x/title_textures.rs b/crates/xcl/src/hle/d3d12x/title_textures.rs
index 5c139fea..8b22c9b3 100644
--- a/crates/xcl/src/hle/d3d12x/title_textures.rs
+++ b/crates/xcl/src/hle/d3d12x/title_textures.rs
@@ -3921,4 +3921,17 @@ pub(super) mod tests {
         assert_eq!(tbl.uncover(11), Some((0, false)));
         assert!(tbl.by_res.contains_key(&10) && !tbl.by_res[&11].pending && tbl.shadowed.contains_key(&12));
+
+        // An already uploaded A is displaced by B before its next dirty-page scan.
+        let mut dirty_tbl = Table::new();
+        let mut b = entry(0x30_0000, &tex_64k, false, true);
+        b.src = 0x5000_0000;
+        let mut a = entry(0x30_4000, &tex_16k, false, true);
+        a.src = 0x4000_0000;
+        assert_eq!(dirty_tbl.insert(20, b).0, 0);
+        assert_eq!(dirty_tbl.insert(21, a).0, 0);
+        assert!(dirty_tbl.coarse.contains_key(&(0x4000_0000 >> SRC_COARSE_SHIFT)));
+        assert_eq!(dirty_tbl.uncover(20), Some((1, true)));
+        assert!(dirty_tbl.coarse.contains_key(&(0x4000_0000 >> SRC_COARSE_SHIFT)), "A's source must stay watched until refresh");
+        assert!(dirty_tbl.displaced.contains(&21), "refresh must consider dirty A");
     }

AI use: Codex (GPT-6.2 Sol) traced the relevant state transitions, wrote and ran the table regression, checked the lifecycle conditions, and drafted this report.

@LocranKing

Copy link
Copy Markdown

Tested PR #2956 (d5273351) on Windows 11 with a paired A/B 180 s open-world run against its base (eae01502) and the title_textures unit tests.

Environment

  • GPU: NVIDIA GeForce RTX 3060 Ti (8024 MiB VRAM, driver 32.0.16.2140)
  • CPU / RAM: AMD Ryzen 5 5500 (6C/12T), 32 GB DDR4, Windows 11 Build 26300
  • Shader cache & runtime: Frozen nvidia-dev DXIL cache (XCL_DIRECT_DXIL=1, XCL_HEAP_WINDOWS=1, XCL_TEXTURE_LEVEL_PAGES=1, XCL_PREPARE_GRAPHICS=0, XCL_DRED=1), Series S (lockhart) profile.

Paired 180 s Open-World Smoke Comparison (lockhart)

Run Exit Crashes / Removals Frames (PresentX) Heavy world start Peak Commit Peak Local VRAM Peak Non-Local Target Pages
Base eae01502 0x0 0 / 0 1764 109.31 s 25679 MB 8285 MB 4288 MB 3647 MB
#2956 (d5273351) 0x0 0 / 0 1911 122.32 s 25488 MB 8414 MB 4353 MB 3632 MB

In #2956, once inside the open world (120.85 s–121.94 s), the title triggered the new reactivation path and logged:

  • D3D12.X: 65 shadowed textures sampled by draws so far were made active again (0 needed remapping in this batch) (with 1 needing remapping per batch in earlier batches at 120.85 s–121.81 s).

Unit tests (title_textures)

Note on test stability when running cargo test --locked -p xcl --lib -- title_textures on d5273351 (31 tests total; placed_title_textures_share_vram_and_preserve_pixels is newly added on d5273351 and not present on eae01502):

  1. First default parallel batch run: 30 passed; 1 failed (hle::d3d12x::title_textures::tests::placed_title_textures_share_vram_and_preserve_pixels ... FAILED).
  2. Re-run with -- --test-threads=1: 31 passed; 0 failed (2.20 s).
  3. Follow-up standalone default parallel run: 31 passed; 0 failed (2.26 s).

So placed_title_textures_share_vram_and_preserve_pixels passed reliably sequentially and on retry, with one observed intermittent failure under parallel execution.

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.

6 participants