vkback: keep aliased targets per address and never alias address 0 #194

Open
github-bot wants to merge 1 commit from github-bot/gh-194 into main AGit
Owner

@⁠Mhyster on GitHub · 2026-12-02 07:07 UTC · original
Mhyster:fix/tex-targets-alias-stack → main · +85 −33 · 3 files · 1 commits · open

What

Fixes placed shader views reading black when multiple live GPU-written targets share a title address (208 of 1,188 targets in a boat run, targets.rs) or when unrelated targets are registered at address 0:

  1. Keep a stack of live targets per non-zero address (targets.rs Targets, placed.rs alias_at, slotwrite.rs write_view):
    • Previously, Targets::at stored a single u64 per address. If target B (e.g. a different size or a depth target) was created at the same address as live target A, B overwrote A so placed views of A's shape missed alias_at and created an unfilled copy from title memory (black). Likewise, destroying transient target B cleared at[address] to None even though A remained live.
    • Targets::at now stores Vec<u64> (oldest to newest), alias_at scans targets.all_at(address) newest-first for the first shape/bpp/non-depth match, and Targets::remove removes only the destroyed id (via addrs: HashMap<u64, Vec<u64>>, avoiding a full BTreeMap scan) so earlier live targets at that address remain visible.
  2. Never alias address 0 (targets.rs insert / all_at, placed.rs alias_at): address 0 is no title memory (placed.rs supersede_placed), so targets at 0 are kept only in own for diagnostics and never returned by all_at(0) / alias_at(0, _).

Reproduces the console's aliased render-target/UAV memory pools: placed views of any live GPU-written surface at an address find the matching surface instead of reading black.

The reference

  • No separate vkd3d-proton counterpart (vkd3d-proton backs placed resources directly on shared VkDeviceMemory heaps). Mirrors vkback's own placed.rs (alias_at, supersede_placed address-0 rule) and targets.rs census (208 of 1,188 displaced targets).

Evidence

  • Unit tests in greenfield/vkback/src/targets.rs:
    • aliased_targets_keep_newest_order_and_restore_on_remove
    • address_zero_never_aliases_targets
    • only_another_texture_at_the_address_counts_as_displaced (including re-insert promotion without duplication)
    • removing_an_id_frees_every_address_it_holds
    • only_textures_with_an_address_are_marked_bound
  • Local CI checks passed (Linux x86_64):
    • cargo test -p victory-shader -p victory-formats -p victory-core -p xtask --locked
    • (cd greenfield && cargo test -p umem -p frame -p vkback --lib --locked && cargo test -p vkback --test policy --locked) (169 passed, 3 ignored; 6/6 policy tests)
    • cargo clippy --all-targets --locked -- -D warnings on root native crates, greenfield, greenfield/wine, and greenfield/vkback/translate.
  • Not run: live game / AMD GPU screenshot check, Vulkan validation run (house scene), and A/B frame timing (frames.py) could not be run in this session (no live game run performed); cargo xwin clippy runs in GitHub Actions CI.

Checklist (docs/AGENTS.md)

  • One topic (rule 31); docs/TASKS.md not edited, the fixed item named above (rule 29)
  • "Allow edits by maintainers" on; after review, no force-push and no close-and-reopen (it drops the maintainers' commits)
  • No fallback or second path for a case that can't happen, in title data or in our own setup (rule 33); unexpected input fails loudly (rule 9)
  • Constants come from the console, the hardware or the title's data, not tuned by eye or ear (rule 9b)
  • Every switch is listed, read in one place, and a bad value stops the start (rule 2)
  • Queues and other externally synchronized Vulkan objects are used under their lock (rule 35)
  • Every unsafe block has a // SAFETY: comment; no raw pointer around the borrow checker (rule 36)
  • Clippy clean; tests call the production code; TRANSLATOR_VERSION bumped if the translator's output changes (rule 27)
  • Comments say what is true now and name functions, not line numbers (rule 3)
  • Nothing from the title, and no pointers to where non-free data can be found (hard constraints)
**@⁠Mhyster** on GitHub · 2026-12-02 07:07 UTC · [original](https://github.com/vktory6/VKtory/pull/194) `Mhyster:fix/tex-targets-alias-stack` → `main` · +85 −33 · 3 files · 1 commits · open ## What Fixes placed shader views reading black when multiple live GPU-written targets share a title address (208 of 1,188 targets in a boat run, `targets.rs`) or when unrelated targets are registered at address `0`: 1. **Keep a stack of live targets per non-zero address** (`targets.rs` `Targets`, `placed.rs` `alias_at`, `slotwrite.rs` `write_view`): - Previously, `Targets::at` stored a single `u64` per address. If target `B` (e.g. a different size or a depth target) was created at the same address as live target `A`, `B` overwrote `A` so placed views of `A`'s shape missed `alias_at` and created an unfilled copy from title memory (black). Likewise, destroying transient target `B` cleared `at[address]` to `None` even though `A` remained live. - `Targets::at` now stores `Vec<u64>` (oldest to newest), `alias_at` scans `targets.all_at(address)` newest-first for the first shape/bpp/non-depth match, and `Targets::remove` removes only the destroyed id (via `addrs: HashMap<u64, Vec<u64>>`, avoiding a full `BTreeMap` scan) so earlier live targets at that address remain visible. 2. **Never alias address `0`** (`targets.rs` `insert` / `all_at`, `placed.rs` `alias_at`): address `0` is no title memory (`placed.rs` `supersede_placed`), so targets at `0` are kept only in `own` for diagnostics and never returned by `all_at(0)` / `alias_at(0, _)`. Reproduces the console's aliased render-target/UAV memory pools: placed views of any live GPU-written surface at an address find the matching surface instead of reading black. ## The reference - No separate vkd3d-proton counterpart (vkd3d-proton backs placed resources directly on shared `VkDeviceMemory` heaps). Mirrors vkback's own `placed.rs` (`alias_at`, `supersede_placed` address-0 rule) and `targets.rs` census (`208 of 1,188` displaced targets). ## Evidence - Unit tests in `greenfield/vkback/src/targets.rs`: - `aliased_targets_keep_newest_order_and_restore_on_remove` - `address_zero_never_aliases_targets` - `only_another_texture_at_the_address_counts_as_displaced` (including re-insert promotion without duplication) - `removing_an_id_frees_every_address_it_holds` - `only_textures_with_an_address_are_marked_bound` - Local CI checks passed (Linux x86_64): - `cargo test -p victory-shader -p victory-formats -p victory-core -p xtask --locked` - `(cd greenfield && cargo test -p umem -p frame -p vkback --lib --locked && cargo test -p vkback --test policy --locked)` (169 passed, 3 ignored; 6/6 policy tests) - `cargo clippy --all-targets --locked -- -D warnings` on root native crates, `greenfield`, `greenfield/wine`, and `greenfield/vkback/translate`. - **Not run**: live game / AMD GPU screenshot check, Vulkan validation run (house scene), and A/B frame timing (`frames.py`) could not be run in this session (no live game run performed); `cargo xwin clippy` runs in GitHub Actions CI. ## Checklist (docs/AGENTS.md) - [x] One topic (rule 31); `docs/TASKS.md` not edited, the fixed item named above (rule 29) - [x] "Allow edits by maintainers" on; after review, no force-push and no close-and-reopen (it drops the maintainers' commits) - [x] No fallback or second path for a case that can't happen, in title data or in our own setup (rule 33); unexpected input fails loudly (rule 9) - [x] Constants come from the console, the hardware or the title's data, not tuned by eye or ear (rule 9b) - [x] Every switch is listed, read in one place, and a bad value stops the start (rule 2) - [x] Queues and other externally synchronized Vulkan objects are used under their lock (rule 35) - [x] Every `unsafe` block has a `// SAFETY:` comment; no raw pointer around the borrow checker (rule 36) - [x] Clippy clean; tests call the production code; `TRANSLATOR_VERSION` bumped if the translator's output changes (rule 27) - [x] Comments say what is true now and name functions, not line numbers (rule 3) - [x] Nothing from the title, and no pointers to where non-free data can be found (hard constraints)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 07:08:44 +00:00
Author
Owner

@⁠vktory6 on GitHub · 2026-12-02 15:28 UTC · original

Thanks. Never aliasing address 0 is sound. The per-address target stack is the open question in the TASKS item "Targets of one description over the same memory are separate images". On the console the memory holds the last writer's bytes, so picking the newest target of matching shape can show a stale target where the console shows the newer one. That item asks for a write-here/read-there count first: does the title read A after B took its address? #147 adds that probe. Holding this until #147's numbers are in. A counter for "alias picked a target that is not the newest" would let this PR answer it too.

**@⁠vktory6** on GitHub · 2026-12-02 15:28 UTC · [original](https://github.com/vktory6/VKtory/pull/194#issuecomment-7350389700) Thanks. Never aliasing address 0 is sound. The per-address target stack is the open question in the TASKS item "Targets of one description over the same memory are separate images". On the console the memory holds the last writer's bytes, so picking the newest target of matching shape can show a stale target where the console shows the newer one. That item asks for a write-here/read-there count first: does the title read A after B took its address? #147 adds that probe. Holding this until #147's numbers are in. A counter for "alias picked a target that is not the newest" would let this PR answer it too.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/194/head:github-bot/gh-194'
git switch 'github-bot/gh-194'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-194'
git switch 'github-bot/gh-194'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-194'
git switch 'github-bot/gh-194'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-194'
git switch 'main'
git merge --squash 'github-bot/gh-194'
git switch 'main'
git merge --ff-only 'github-bot/gh-194'
git switch 'main'
git merge 'github-bot/gh-194'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.