native: one host texture per range of title memory (share identical copies, give up covered placed copies) #359

Closed
m0rff wants to merge 7 commits from upstream-pr/359 into main
m0rff commented 2026-11-27 14:01:56 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by m0rff on 2026-11-27T14:01:56Z.


Refs #356. Stacked on #301: the first five commits are #301's. This PR's own change is the last commit, native: one host texture per range of title memory. I'll rebase it once #301 lands.

Why. On a 24 GB M4 Pro (Series S, budget off), host texture copies grew to 8.5 GB over 1.7 GB of distinct title memory during the world load, and macOS ran out of memory (#301, alicans' third run). Each title key got its own copy, even when other keys covered the same memory.

What changes.

  1. Identical copies share one texture (Title::realize, identical_copy). A title texture can take a live copy's texture instead of making its own when the two:

    • start at the same memory;
    • match in size, layout, dimensions, unordered-access flag and host format;
    • are both B4G4R4A4 or both not (that format is converted on upload);
    • are neither a render target, depth, nor GPU-written.

    Both would be detiled from the same bytes into the same format, so draws sample the same texels. The shared copy is counted once. A GPU write through either key marks the shared copy, as the R9G9B9E5-over-R32_UINT case already does. When the owner's title texture goes, the first key that shared it takes the copy over. Its entries already show that texture, so nothing is cleared, made or uploaded again.

  2. A placed copy goes when another placement covers its memory at another start, once nothing uses it (Title::give_up_overlapped marks it from texture_placed; give_up_covered gives it up at a sync). Before, it went only when a description was placed at exactly its address, or with its pool.

    • The guess. The title is done with a placed view once it places another over its memory. It is marked as a guess in the code and in docs/native-host.md. D3D12 makes an older alias's contents undefined for resource objects. For views of raw memory, the console's rule is not known.
    • When a covered copy goes. Only under the budget's own rule (evictable): no heap entry shows it and no recorded or submitted list holds it. It also waits while a submission in flight names it. Giving up a copy an entry shows would clear the entry, and drop the Metal reference it holds, under work the GPU may still be running. So a copy that is still shown waits until the title writes another record over that entry.
    • If the guess is wrong. A view or table that names a copy given up gets it made again: one late upload, and the draw in the same submission still reads it (keep_named). A view written again at the copy's own address is not a new placement, so two placements in use don't keep covering each other.
    • Never marked: render targets, depth, GPU-written copies, and copies ever viewed for unordered access.

Trace. The VICTORY_TRACE_TEXTURE_SYNC=1 budget line now also gives, per second, how many textures shared an identical copy (and how many share one now) and how many placed copies were given up as covered (with MiB), and how many covered copies are still in use (waiting on an entry). Compare it with the distinct figure. If "made again" climbs with the covered count, the guess is wrong for this title.

Not covered. Copies at other starts that stay live (separately placed mips or slices of one chain), and copies of one memory in another layout or format, still hold that memory once each. An object created over placed copies does not give them up; only placements do.

Tests (texture_tests). Each fails with its part removed:

  • an_identical_texture_over_the_same_memory_shares_its_copy
  • a_texture_sharing_a_released_copy_takes_it_over
  • a_placed_copy_another_placement_covers_is_given_up_once_unused (includes a copy an entry still shows, kept until that entry shows another)

The texture tests share one host and reuse pool addresses. Earlier tests' textures would now be shared by later identical ones, so texture_tests::pool releases what is left over its addresses first. That also stopped the probe-timing flakes in six runs out of six (they failed 2 runs in 5 before).

Checks:

  • cargo test -p victory-hle -p victory -p victory-gpu -p xtask passes. victory-gpu was run natively on arm64: 363 passed, six runs in a row.
  • Windows clippy with -D warnings is clean, and native clippy finds nothing new in the changed lines.
  • cargo xtask policy is ok.

Needs world runs. This machine has no copy of the game. What would settle it: the copies and distinct figures at the world load on a 24 GB Mac with the budget off, against #301's head, plus how many "made again" there were.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5527900653 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/359). Originally posted by [m0rff](https://github.com/m0rff) on 2026-11-27T14:01:56Z. --- Refs #356. Stacked on #301: the first five commits are #301's. This PR's own change is the last commit, `native: one host texture per range of title memory`. I'll rebase it once #301 lands. **Why.** On a 24 GB M4 Pro (Series S, budget off), host texture copies grew to 8.5 GB over 1.7 GB of distinct title memory during the world load, and macOS ran out of memory (#301, alicans' third run). Each title key got its own copy, even when other keys covered the same memory. **What changes.** 1. **Identical copies share one texture** (`Title::realize`, `identical_copy`). A title texture can take a live copy's texture instead of making its own when the two: - start at the same memory; - match in size, layout, dimensions, unordered-access flag and host format; - are both B4G4R4A4 or both not (that format is converted on upload); - are neither a render target, depth, nor GPU-written. Both would be detiled from the same bytes into the same format, so draws sample the same texels. The shared copy is counted once. A GPU write through either key marks the shared copy, as the R9G9B9E5-over-R32_UINT case already does. When the owner's title texture goes, the first key that shared it takes the copy over. Its entries already show that texture, so nothing is cleared, made or uploaded again. 2. **A placed copy goes when another placement covers its memory at another start, once nothing uses it** (`Title::give_up_overlapped` marks it from `texture_placed`; `give_up_covered` gives it up at a sync). Before, it went only when a description was placed at exactly its address, or with its pool. - **The guess.** The title is done with a placed view once it places another over its memory. It is marked as a guess in the code and in docs/native-host.md. D3D12 makes an older alias's contents undefined for resource objects. For views of raw memory, the console's rule is not known. - **When a covered copy goes.** Only under the budget's own rule (`evictable`): no heap entry shows it and no recorded or submitted list holds it. It also waits while a submission in flight names it. Giving up a copy an entry shows would clear the entry, and drop the Metal reference it holds, under work the GPU may still be running. So a copy that is still shown waits until the title writes another record over that entry. - **If the guess is wrong.** A view or table that names a copy given up gets it made again: one late upload, and the draw in the same submission still reads it (`keep_named`). A view written again at the copy's own address is not a new placement, so two placements in use don't keep covering each other. - **Never marked:** render targets, depth, GPU-written copies, and copies ever viewed for unordered access. **Trace.** The `VICTORY_TRACE_TEXTURE_SYNC=1` budget line now also gives, per second, how many textures shared an identical copy (and how many share one now) and how many placed copies were given up as covered (with MiB), and how many covered copies are still in use (waiting on an entry). Compare it with the distinct figure. If "made again" climbs with the covered count, the guess is wrong for this title. **Not covered.** Copies at other starts that stay live (separately placed mips or slices of one chain), and copies of one memory in another layout or format, still hold that memory once each. An object created over placed copies does not give them up; only placements do. **Tests** (`texture_tests`). Each fails with its part removed: - `an_identical_texture_over_the_same_memory_shares_its_copy` - `a_texture_sharing_a_released_copy_takes_it_over` - `a_placed_copy_another_placement_covers_is_given_up_once_unused` (includes a copy an entry still shows, kept until that entry shows another) The texture tests share one host and reuse pool addresses. Earlier tests' textures would now be shared by later identical ones, so `texture_tests::pool` releases what is left over its addresses first. That also stopped the probe-timing flakes in six runs out of six (they failed 2 runs in 5 before). **Checks:** - `cargo test -p victory-hle -p victory -p victory-gpu -p xtask` passes. victory-gpu was run natively on arm64: 363 passed, six runs in a row. - Windows clippy with `-D warnings` is clean, and native clippy finds nothing new in the changed lines. - `cargo xtask policy` is ok. **Needs world runs.** This machine has no copy of the game. What would settle it: the copies and distinct figures at the world load on a 24 GB Mac with the budget off, against #301's head, plus how many "made again" there were.
m0rff commented 2026-11-27 14:32:56 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by m0rff on 2026-11-27T14:32:56Z.


@alicans please don't run this head, nor #355: both are stacked on #301, which has a crash the maintainer found (comment on #301 at 14:25 UTC). I'll say here when they are rebased on a fix.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7220923264 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/359#issuecomment-7220923264). Originally posted by [m0rff](https://github.com/m0rff) on 2026-11-27T14:32:56Z. --- @alicans please don't run this head, nor #355: both are stacked on #301, which has a crash the maintainer found (comment on #301 at 14:25 UTC). I'll say here when they are rebased on a fix.
m0rff commented 2026-11-27 14:48:56 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by m0rff on 2026-11-27T14:48:56Z.


@alicans #355 and #359 are now rebased on the crash fix in #301 (a0fbd51a). The fix has not been run in the game, so it may still crash. If you try it, a world load of #359 with the budget off and VICTORY_TRACE_TEXTURE_SYNC=1 is the run that would help.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7221262976 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/359#issuecomment-7221262976). Originally posted by [m0rff](https://github.com/m0rff) on 2026-11-27T14:48:56Z. --- @alicans #355 and #359 are now rebased on the crash fix in #301 (a0fbd51a). The fix has not been run in the game, so it may still crash. If you try it, a world load of #359 with the budget off and VICTORY_TRACE_TEXTURE_SYNC=1 is the run that would help.
LakshanSivakumar commented 2026-11-27 16:23:31 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-27T16:23:31Z.


Merged in 582670f1, from the head before your rebase (55219dfa; the rebase only changed docs context, so the content is the same). I added one change on top: a submission takes the lists' snapshots for the budget only when the budget, an evicted texture or the trace needs them, instead of every submission (582670f1). Ran the tests, Windows clippy, policy and a release build, and a 170 s headless game run with the budget off: the world was entered and kept presenting to the end like the run without this PR, 0 GPU errors, no crash. I did not run with the budget on or with VICTORY_TRACE_TEXTURE_SYNC. Closing since it is on main. Thanks.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7223205802 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/359#issuecomment-7223205802). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-27T16:23:31Z. --- Merged in 582670f1, from the head before your rebase (55219dfa; the rebase only changed docs context, so the content is the same). I added one change on top: a submission takes the lists' snapshots for the budget only when the budget, an evicted texture or the trace needs them, instead of every submission (582670f1). Ran the tests, Windows clippy, policy and a release build, and a 170 s headless game run with the budget off: the world was entered and kept presenting to the end like the run without this PR, 0 GPU errors, no crash. I did not run with the budget on or with VICTORY_TRACE_TEXTURE_SYNC. Closing since it is on main. Thanks.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS!359
No description provided.