native: release placed texture copies whose title memory another placement takes #400

Closed
github-bot wants to merge 1 commit from github-bot/gh-396 into main AGit
Owner

@⁠Ketan-Kapse on GitHub · 2026-11-27 16:27 UTC · original
Ketan-Kapse:pr/placed-overlap → main · +749 −10 · 7 files · 1 commits · closed without merging

Refs #383, #79

What this changes

Host copies of placed title textures were never released when the title reused their memory at another start address or size. Title::texture_placed released the previous placement only at the SAME address ("guess: the title is done with it once it places another there"), and release_placed only when the whole pool went. The old detiled copy stayed tracked, kept being hashed by the scanner, was uploaded again when its now foreign bytes changed, and stayed resident through the heap entries that still showed it.

This PR, on the native host only:

  • Release on overlap (default on, VICTORY_PLACED_OVERLAP=0 turns it off). On placement, every other placed texture whose title range intersects the new one is released the way the same-address case is. The object that owns the placement and aliases of that owner are kept. A sorted range index makes the lookup O(log n + hits).
  • Ping-pong guard. Two live descriptions over intersecting memory would release each other on every placement. A pair that displaces each other back is counted and from then on coexists (bounded: 4096 recent displacements, 4096 exemptions; when the exemption budget is full the rule stops guessing).
  • Heap entries of a displaced texture are cleared (this rule and the existing same-address case), or the Metal texture stays retained and nothing is freed. An unchanged record naming it is checked again when its table is next bound. Final release (release_resource) keeps its meaning.
  • Diagnostic VICTORY_TRACE_TEXTURE_MEMORY=1: every 64 presented frames one texture memory: line with tracked copies by kind (placed / object / targets), placed bytes against the union of their title ranges, what the rule released, ping-pong count, pending.

It is a guess about title behaviour (intersecting placements mean reused bytes) and is marked as one in the code and in docs/native-host.md.

Measured

Apple M4 Pro, 24 GB, macOS 27.0.1, --console series-s, windowed, VICTORY_METAL_RESIDENCY_SETS=1. Counts only.

Main 728845d1 plus this branch, same binary, the rule off and on:

rule off (VICTORY_PLACED_OVERLAP=0) rule on
placed host copies 49,249, 11.12 GB at frame 1920 25,023, 6.27 GB at frame 2432
union of their title ranges 6.52 GB (4.60 GB counted more than once) 6.25 GB
released by the rule 0 69,588 textures, 17.75 GB
GPU "In use system memory", peak 19.0 GB 17.4 GB
swap 4.8 of 5.1 GB used at +5 min 2.5 of 4.1 GB at +8 min
ping-pong pairs - 2,242
end stopped by my watchdog at about +5 min, frame 1920 (under 300 MB of swap free) ran to about +8 min, frame 2432, then see below

On our fork (older base, more local changes), the same rule took the world from about 0.8 to 1.6 fps over a 10-minute run with no GPU errors: 75,334 textures (19.3 GB) released, kernel time of the command buffers per frame 1,908 ms to 720 ms.

Not verified, please read

  • The rule-on run on main ended badly. After about 8 minutes, 9 command buffers failed with kIOGPUCommandBufferCallbackErrorTimeout, and 30 s later the title took an unhandled access violation (0xc0000005, a null read) and the host aborted. The rule-off run did not time out, but it only lived 5 minutes before swap ran out, so it never reached that point. I cannot say whether the rule contributes to the timeout or whether it is the world-load timeout others report in #79. One run each.
  • On our fork the same rule ran 10 minutes with no failed command buffer.
  • Shaders that reach heap entries by indexing the heap themselves see a cleared entry until the record is mirrored again. I saw no missing textures on our fork, where the picture is lit; on main the world is flat grey here, so the picture could not be judged.
  • An idle rule (release placed textures no bound table named for N frames) froze the GPU queue at world load on our fork. It is NOT in this PR.

How it was tested

  • cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory: passes except native_list_imports::list_imports_binds_the_probe_and_does_not_run_it, which cannot build its Windows probe on this Mac (cross tools not installed). Not touched by this change.
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings: clean.
  • Ran the game: see above.
  • victory-gpu (release, x86_64 under Rosetta, on Metal): 372 passed, 1 failed, 5 ignored. The failure is list::tests::unordered_access_clears_have_the_same_slots_in_both_layouts, which fails the same way on main 728845d1 here. victory-metal: 37, 12 and 16 passed.
  • New tests (synthetic data only): overlap at another start releases; the owner and its aliases stay; a ping-pong pair is detected and then coexists; the range union arithmetic; native::host::tests::displaced_texture_records_retry_when_the_table_is_checked (entries cleared, the unchanged record retried, shown again once the key is registered again, final release unchanged).
  • cargo xtask policy passes.

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md
  • New diagnostics are behind a VICTORY_* variable and documented

🤖 Generated with Claude Code

**@⁠Ketan-Kapse** on GitHub · 2026-11-27 16:27 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/396) `Ketan-Kapse:pr/placed-overlap` → `main` · +749 −10 · 7 files · 1 commits · closed without merging Refs #383, #79 ## What this changes Host copies of placed title textures were never released when the title reused their memory at another start address or size. `Title::texture_placed` released the previous placement only at the SAME address ("guess: the title is done with it once it places another there"), and `release_placed` only when the whole pool went. The old detiled copy stayed tracked, kept being hashed by the scanner, was uploaded again when its now foreign bytes changed, and stayed resident through the heap entries that still showed it. This PR, on the native host only: - **Release on overlap** (default on, `VICTORY_PLACED_OVERLAP=0` turns it off). On placement, every other placed texture whose title range intersects the new one is released the way the same-address case is. The object that owns the placement and aliases of that owner are kept. A sorted range index makes the lookup O(log n + hits). - **Ping-pong guard.** Two live descriptions over intersecting memory would release each other on every placement. A pair that displaces each other back is counted and from then on coexists (bounded: 4096 recent displacements, 4096 exemptions; when the exemption budget is full the rule stops guessing). - **Heap entries of a displaced texture are cleared** (this rule and the existing same-address case), or the Metal texture stays retained and nothing is freed. An unchanged record naming it is checked again when its table is next bound. Final release (`release_resource`) keeps its meaning. - **Diagnostic** `VICTORY_TRACE_TEXTURE_MEMORY=1`: every 64 presented frames one `texture memory:` line with tracked copies by kind (placed / object / targets), placed bytes against the union of their title ranges, what the rule released, ping-pong count, pending. It is a guess about title behaviour (intersecting placements mean reused bytes) and is marked as one in the code and in `docs/native-host.md`. ## Measured Apple M4 Pro, 24 GB, macOS 27.0.1, `--console series-s`, windowed, `VICTORY_METAL_RESIDENCY_SETS=1`. Counts only. Main 728845d1 plus this branch, same binary, the rule off and on: | | rule off (`VICTORY_PLACED_OVERLAP=0`) | rule on | |---|---|---| | placed host copies | 49,249, 11.12 GB at frame 1920 | 25,023, 6.27 GB at frame 2432 | | union of their title ranges | 6.52 GB (4.60 GB counted more than once) | 6.25 GB | | released by the rule | 0 | 69,588 textures, 17.75 GB | | GPU "In use system memory", peak | 19.0 GB | 17.4 GB | | swap | 4.8 of 5.1 GB used at +5 min | 2.5 of 4.1 GB at +8 min | | ping-pong pairs | - | 2,242 | | end | stopped by my watchdog at about +5 min, frame 1920 (under 300 MB of swap free) | ran to about +8 min, frame 2432, then see below | On our fork (older base, more local changes), the same rule took the world from about 0.8 to 1.6 fps over a 10-minute run with no GPU errors: 75,334 textures (19.3 GB) released, kernel time of the command buffers per frame 1,908 ms to 720 ms. ## Not verified, please read - **The rule-on run on main ended badly.** After about 8 minutes, 9 command buffers failed with `kIOGPUCommandBufferCallbackErrorTimeout`, and 30 s later the title took an unhandled access violation (0xc0000005, a null read) and the host aborted. The rule-off run did not time out, but it only lived 5 minutes before swap ran out, so it never reached that point. I cannot say whether the rule contributes to the timeout or whether it is the world-load timeout others report in #79. One run each. - On our fork the same rule ran 10 minutes with no failed command buffer. - Shaders that reach heap entries by indexing the heap themselves see a cleared entry until the record is mirrored again. I saw no missing textures on our fork, where the picture is lit; on main the world is flat grey here, so the picture could not be judged. - An idle rule (release placed textures no bound table named for N frames) froze the GPU queue at world load on our fork. It is NOT in this PR. ## How it was tested - [x] `cargo test --target x86_64-apple-darwin -p victory-hle -p victory-loader -p victory`: passes except `native_list_imports::list_imports_binds_the_probe_and_does_not_run_it`, which cannot build its Windows probe on this Mac (cross tools not installed). Not touched by this change. - [x] `cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings`: clean. - [x] Ran the game: see above. - victory-gpu (release, x86_64 under Rosetta, on Metal): 372 passed, 1 failed, 5 ignored. The failure is `list::tests::unordered_access_clears_have_the_same_slots_in_both_layouts`, which fails the same way on main 728845d1 here. victory-metal: 37, 12 and 16 passed. - New tests (synthetic data only): overlap at another start releases; the owner and its aliases stay; a ping-pong pair is detected and then coexists; the range union arithmetic; `native::host::tests::displaced_texture_records_retry_when_the_table_is_checked` (entries cleared, the unchanged record retried, shown again once the key is registered again, final release unchanged). - `cargo xtask policy` passes. ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] Guesses about undocumented console behaviour are marked in the code and in docs/native-host.md - [x] New diagnostics are behind a `VICTORY_*` variable and documented 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A placed texture was released only when another description was placed at the
same address, or when its pool went. A streaming allocator that reuses a region
at another start or size left the old detiled host copy tracked, scanned,
uploaded again when its now foreign bytes changed, and resident through the
heap entries that still showed it.

On placement, every other placed texture whose title range intersects the new
one is released (not the object that owns the placement, nor aliases of that
owner). Pairs that displace each other are detected and then coexist.
VICTORY_PLACED_OVERLAP=0 turns the rule off. The heap entries of a displaced
texture (this rule and the existing same-address case) are cleared, and an
unchanged record naming it is checked again when its table is next bound.

VICTORY_TRACE_TEXTURE_MEMORY=1 logs, every 64 presented frames, the tracked
host copies by kind, the placed bytes against the union of their title ranges,
and what the rule released.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 16:29:52 +00:00
github-bot closed this pull request 2026-11-27 16:38:09 +00:00
Author
Owner

@⁠Ketan-Kapse on GitHub · 2026-11-27 16:31 UTC · original

Closing: main already covers this. I cut the branch from 728845d1 and missed that 55219dfa ("one host texture per range of title memory") and 740bbb2d (the byte budget) had landed. They release a placed copy another placement covers, with a safer rule than this PR's (given up only once no heap entry, list or submission in flight uses it; this PR released at once).

The numbers in the description may still be useful as data for #79 and #359: on 728845d1, 24 GB M4 Pro, Series S, placed copies were 11.12 GB over a 6.52 GB union of title ranges at frame 1920 (4.60 GB counted more than once), against 6.27 GB with overlap release. I will measure current main the same way and report on #383.

**@⁠Ketan-Kapse** on GitHub · 2026-11-27 16:31 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/396#issuecomment-7223364243) Closing: main already covers this. I cut the branch from 728845d1 and missed that 55219dfa ("one host texture per range of title memory") and 740bbb2d (the byte budget) had landed. They release a placed copy another placement covers, with a safer rule than this PR's (given up only once no heap entry, list or submission in flight uses it; this PR released at once). The numbers in the description may still be useful as data for #79 and #359: on 728845d1, 24 GB M4 Pro, Series S, placed copies were 11.12 GB over a 6.52 GB union of title ranges at frame 1920 (4.60 GB counted more than once), against 6.27 GB with overlap release. I will measure current main the same way and report on #383.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.