D3D12.X: title_textures content hash skips remapped textures, double-reads changed textures, and caches failed uploads (#486) #504

Open
opened 2026-11-26 04:23:46 +00:00 by github-bot · 2 comments
Owner

@⁠mastrr9 on GitHub · 2026-11-24 01:04 UTC · original

Code audit of crates/xcl/src/hle/d3d12x/title_textures.rs on main (69f0635, merged via #486) found three follow-up issues in the unchanged-texture skip path:

  1. Unmapped/remapped shadowed textures stay skipped when their CPU bytes are unchanged (title_textures.rs:392-394, 490-492, 508-512, 831-840):

    • When texture A is shadowed by an overlapping placement B, register() / retire_range() calls vram::unmap_superseded(A), releasing A's unshared VRAM tiles back to the pool, while LAST_CONTENT still holds A -> hash.
    • When B is later released, forget(B) removes B (not A) from LAST_CONTENT, reactivates A with A.pending = true, and calls vram::remap_if_unmapped(A).
    • On the next refresh(), if A's CPU memory still matches its pre-unmap hash, same_as_last_upload(A, h) returns true and skips re-uploading A into its freshly remapped VRAM tiles (the existing test at line 1643 didn't catch this because it overwrites old_res with a new seed 0xD4 before refresh()).
    • Fix: Remove every resource in to_unmap / to_remap from LAST_CONTENT.
  2. Every changed texture is read and allocated twice (plus TOCTOU between hash and upload) (title_textures.rs:832, 843, 1050-1066):

    • upload_all calls read_committed(va, tex.size() as usize) to compute content_hash, drops that Vec<u8>, and then subresources(va, &tex) calls read_committed a second time for the exact same memory.
    • Besides doubling allocation + copy bandwidth on every changed video/UI/world texture, if the title modifies the memory between the two reads, LAST_CONTENT stores the hash of snapshot #1 while the GPU receives snapshot #2.
    • Fix: Read the bytes once into mem, hash &mem, and pass &mem into a subresources_from(&mem, &tex) helper (slicing &mem[s * c.slice_bytes..(s + 1) * c.slice_bytes] for 2D/array slices).
  3. Failed uploads poison LAST_CONTENT (title_textures.rs:833, 851-869):

    • same_as_last_upload inserts h into LAST_CONTENT before upload_queue() or texture::upload_batch() runs. If upload_queue fails, upload_batch returns Err, or an item index k appears in Ok(failed), LAST_CONTENT already recorded h and suppresses future retries until the memory changes.
    • Fix: Check LAST_CONTENT.get(&res) == Some(&h) before batching, and insert (res, h) only for batch entries that succeeded in upload_batch.

Not tested: on Windows yet (static code audit of 69f0635; preparing a clean fix + RTX 5080 test run next).

AI use: Audited with Antigravity (Gemini 4 Neon).

**@⁠mastrr9** on GitHub · 2026-11-24 01:04 UTC · [original](https://github.com/xcl-dev/xcl/issues/504) Code audit of `crates/xcl/src/hle/d3d12x/title_textures.rs` on `main` (`69f0635`, merged via #486) found three follow-up issues in the unchanged-texture skip path: 1. **Unmapped/remapped shadowed textures stay skipped when their CPU bytes are unchanged (`title_textures.rs:392-394, 490-492, 508-512, 831-840`):** - When texture `A` is shadowed by an overlapping placement `B`, `register()` / `retire_range()` calls `vram::unmap_superseded(A)`, releasing `A`'s unshared VRAM tiles back to the pool, while `LAST_CONTENT` still holds `A -> hash`. - When `B` is later released, `forget(B)` removes `B` (not `A`) from `LAST_CONTENT`, reactivates `A` with `A.pending = true`, and calls `vram::remap_if_unmapped(A)`. - On the next `refresh()`, if `A`'s CPU memory still matches its pre-unmap hash, `same_as_last_upload(A, h)` returns `true` and skips re-uploading `A` into its freshly remapped VRAM tiles (the existing test at line 1643 didn't catch this because it overwrites `old_res` with a new seed `0xD4` before `refresh()`). - **Fix:** Remove every resource in `to_unmap` / `to_remap` from `LAST_CONTENT`. 2. **Every changed texture is read and allocated twice (plus TOCTOU between hash and upload) (`title_textures.rs:832, 843, 1050-1066`):** - `upload_all` calls `read_committed(va, tex.size() as usize)` to compute `content_hash`, drops that `Vec<u8>`, and then `subresources(va, &tex)` calls `read_committed` a second time for the exact same memory. - Besides doubling allocation + copy bandwidth on every changed video/UI/world texture, if the title modifies the memory between the two reads, `LAST_CONTENT` stores the hash of snapshot #1 while the GPU receives snapshot #2. - **Fix:** Read the bytes once into `mem`, hash `&mem`, and pass `&mem` into a `subresources_from(&mem, &tex)` helper (slicing `&mem[s * c.slice_bytes..(s + 1) * c.slice_bytes]` for 2D/array slices). 3. **Failed uploads poison `LAST_CONTENT` (`title_textures.rs:833, 851-869`):** - `same_as_last_upload` inserts `h` into `LAST_CONTENT` before `upload_queue()` or `texture::upload_batch()` runs. If `upload_queue` fails, `upload_batch` returns `Err`, or an item index `k` appears in `Ok(failed)`, `LAST_CONTENT` already recorded `h` and suppresses future retries until the memory changes. - **Fix:** Check `LAST_CONTENT.get(&res) == Some(&h)` before batching, and insert `(res, h)` only for batch entries that succeeded in `upload_batch`. **Not tested:** on Windows yet (static code audit of `69f0635`; preparing a clean fix + RTX 5080 test run next). **AI use:** Audited with Antigravity (Gemini 4 Neon).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:23:46 +00:00
Author
Owner

@⁠Grimkaaah on GitHub · 2026-11-24 01:06 UTC · original

PR opened addressing all three follow-ups from the audit (LAST_CONTENT on unmap/remap, single read+hash, remember only after successful upload).

Made with Cursor

**@⁠Grimkaaah** on GitHub · 2026-11-24 01:06 UTC · [original](https://github.com/xcl-dev/xcl/issues/504#issuecomment-7118769450) PR opened addressing all three follow-ups from the audit (LAST_CONTENT on unmap/remap, single read+hash, remember only after successful upload). Made with [Cursor](https://cursor.com)
Author
Owner

@⁠mastrr9 on GitHub · 2026-11-24 01:09 UTC · original

Confirmed bug #1 live on Windows 11 / RTX 5080 (main 7ebf1b8, rustc 1.100.0 MSVC):

  • A GPU regression test that uploads old_res (0xAA), shadows/unmaps it with an overlapping placement, releases the overlapping placement without modifying old_res's CPU bytes, and runs refresh() logs title textures: 1 uploads left out so far – the texture's memory held what it held at its last upload and reads back [0, 0, 0, ...] instead of 0xAA.
  • Have a local fix for all three items (invalidate LAST_CONTENT on unmap/remap, single read_committed + subresources_from, commit hash only after upload_batch succeeds) passing cargo test --workspace -- --test-threads=4: 680 passed, 0 failed, 41 ignored (2 expected no wave64 skips). Waiting to run a clean scripts/smoke.ps1 regression check before opening the PR.

AI use: reproduced and verified with Antigravity (Gemini 4 Neon) on NVIDIA GeForce RTX 5080.

**@⁠mastrr9** on GitHub · 2026-11-24 01:09 UTC · [original](https://github.com/xcl-dev/xcl/issues/504#issuecomment-7118821961) Confirmed bug #1 live on Windows 11 / RTX 5080 (`main` `7ebf1b8`, `rustc 1.100.0` MSVC): - A GPU regression test that uploads `old_res` (`0xAA`), shadows/unmaps it with an overlapping placement, releases the overlapping placement **without** modifying `old_res`'s CPU bytes, and runs `refresh()` logs `title textures: 1 uploads left out so far – the texture's memory held what it held at its last upload` and reads back `[0, 0, 0, ...]` instead of `0xAA`. - Have a local fix for all three items (invalidate `LAST_CONTENT` on unmap/remap, single `read_committed` + `subresources_from`, commit hash only after `upload_batch` succeeds) passing `cargo test --workspace -- --test-threads=4`: **680 passed, 0 failed, 41 ignored** (2 expected `no wave64` skips). Waiting to run a clean `scripts/smoke.ps1` regression check before opening the PR. *AI use: reproduced and verified with Antigravity (Gemini 4 Neon) on NVIDIA GeForce RTX 5080.*
This discussion has been locked. Commenting is limited to contributors.
No description provided.