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

Closed
github-bot wants to merge 7 commits from github-bot/gh-359 into main AGit
Owner

@⁠m0rff on GitHub · 2026-11-27 14:01 UTC · original
m0rff:m0rff/one-copy-per-range → main · +1302 −21 · 11 files · 7 commits · closed without merging

Refs #359. 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** on GitHub · 2026-11-27 14:01 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/359) `m0rff:m0rff/one-copy-per-range` → `main` · +1302 −21 · 11 files · 7 commits · closed without merging Refs #359. 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.
Past what Metal's recommended working set leaves after the title's memory
(VICTORY_METAL_TEXTURE_BUDGET_MB overrides it, 0 for none), each submission
gives up the least recently used sampled copies that no heap entry shows,
no recorded or submitted list holds, and that title memory alone fills. A
given-up copy goes the way a released one does, so it leaves the heap
residency sets; the next view that names it, or a record copied into a heap
with plain stores, makes it again. The texture sync trace logs the live,
shown, evictable and given-up bytes once a second.

Refs #294
Review of #301 asked what a draw reads when its heap entry names a
texture given up under the budget. A record the title stores into a
heap with plain stores is mirrored only when a list binding its table
is encoded, after the submission's sync. An entry naming a copy not
made then was cleared, and a shader samples a cleared entry as
transparent black: (0, 0, 0, 0) on an M2 Pro, macOS 26.6
(victory-metal resources::sampling_a_cleared_entry_reads_transparent_black).
Two ways led there: a copy given up before, and a live copy no
mirrored entry showed, given up at that very sync.

Before a submission's sync, when over the budget or with any copy
given up, the host now reads the records of every table the
submission's draws and dispatches bind, and those of work held behind
fences (Title::keep_named, NativeHost::resources_named, over the same
entries remirror_tables checks, now NativeHost::table_span). A
given-up copy they name is made again and uploaded at that sync; none
they name is given up at it. Under the budget with nothing given up,
nothing is read. glue::submit takes the lists' segments before the
sync to do this.

Left (marked shortcut): a record changed after that read, before held
work is encoded, and an entry mirrored outside a submission keep the
cleared entry until the next submission.

Tests: a_texture_given_up_is_back_for_the_draw_whose_table_names_it
and a_texture_the_submission_names_is_not_given_up_under_its_draw
draw through such a record over budget and read the pixel: both read
(0, 0, 0, 0) before, the texture's texel now.
Copies are one per title texture (an object, or an address and resource
description for placed views), counted by their layout size, which is
never more than their footprint. Views of one texture share its copy.
Textures over one memory (aliasing objects, a placed description inside
another's memory) each hold one: so copies can pass the title's memory.
The budget trace now logs the distinct title memory under the copies and
the copies outside the title's pools.

Refs #294
A title texture laid out exactly as a live copy over the same memory
(same layout, dimensions and host format, no upload conversion, not a
render target, depth or GPU-written) uses that copy's texture instead of
making its own; when the copy goes, those sharing it are given up the
way the budget gives a copy up, and made again when named.

A placed copy is given up when another placement covers its memory at
another start (guess, marked: the title is done with a placed view once
it places another over its memory), unless it holds what title memory
does not or a submission in flight names it. A view written again at a
given-up copy's own address is not a new placement.

The budget trace counts both.

Refs #356
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 14:08:07 +00:00
github-bot force-pushed github-bot/gh-359 from 25544f3307 to c8ecbb9747 2026-11-27 14:16:55 +00:00 Compare
Author
Owner

@⁠m0rff on GitHub · 2026-11-27 14:32 UTC · original

@⁠alicans please don't run this head, nor #358: 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** on GitHub · 2026-11-27 14:32 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/359#issuecomment-7220923264) @⁠alicans please don't run this head, nor #358: 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.
github-bot force-pushed github-bot/gh-359 from c8ecbb9747 to 55219dfa23 2026-11-27 14:46:01 +00:00 Compare
Author
Owner

@⁠m0rff on GitHub · 2026-11-27 14:48 UTC · original

@⁠alicans #358 and #362 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 #362 with the budget off and VICTORY_TRACE_TEXTURE_SYNC=1 is the run that would help.

**@⁠m0rff** on GitHub · 2026-11-27 14:48 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/359#issuecomment-7221262976) @⁠alicans #358 and #362 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 #362 with the budget off and VICTORY_TRACE_TEXTURE_SYNC=1 is the run that would help.
github-bot referenced this pull request from a commit 2026-11-27 16:13:14 +00:00
github-bot force-pushed github-bot/gh-359 from 55219dfa23 to 69d0038adb 2026-11-27 16:13:46 +00:00 Compare
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-27 16:23 UTC · original

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.

**@⁠LakshanSivakumar** on GitHub · 2026-11-27 16:23 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/359#issuecomment-7223205802) 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.
github-bot closed this pull request 2026-11-27 16:29:56 +00:00

Pull request closed

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