native: trace how host texture copies overlap (identical vs different layouts, owner changes) #358

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

@⁠m0rff on GitHub · 2026-11-27 13:49 UTC · original
m0rff:m0rff/texture-overlap-trace → main · +237 −1 · 6 files · 1 commits · open

Refs #355

One commit on main. It only measures and changes nothing a draw samples. It is meant to show what the copies that #362's sharing and covered give-up leave behind are made of (#359: 7507 MiB of copies over 1566 MiB of distinct memory on a world load).

What this adds

VICTORY_TRACE_TEXTURE_OVERLAP=1 is off by default. When set, it logs once a second:

host texture copies overlap: <n> copies, <MiB> held again by identical layouts at one start, <MiB> by different layouts; ranges that changed owner in the last second: <n> (<MiB>) from an identical layout, <n> (<MiB>) from a different one, <n> (<MiB>) from a copy no longer there
  • Held again. Each byte is counted once less than the number of copies over it (overlap::excess), split into two kinds:
    • Identical layout at one start: the same pool offset and the same Layout (dimensions, mips, element size, swizzle; not the format). Since #362, copies that also have the same host format at the same title address share one texture. They sit in Title::copies_of, not Title::textures, so they are not counted here. What this figure counts is the rest: another host format over the same layout, which one texture with views could serve, or the same pages reached through two title addresses (sharing goes by title address, this trace by pool offset).
    • Different layouts: another Layout, or another start over the same bytes. These could not share a texture.
  • Owner changes. The owner of a range is the copy that last uploaded into it (Overlap::take, an interval map updated after each upload in sync_apply). An upload that takes bytes from another copy is counted by what that copy was:
    • a live copy with an identical layout;
    • a live copy with a different one, the case where one texture per range would re-upload or sample black;
    • a copy that is gone (released, given up by the budget, or covered), which is memory being reused.

With the variable unset, the cost is one cached OnceLock check per upload and per sync.

Rebased on #362

an_upload_over_another_copy_counts_what_that_copy_was failed on top of #362 because its second texture had the same description over the same memory as the first, so #362 makes it share the first one's texture. realize puts it in copies_of, note_upload finds no copy of its own and returns, and only two of the three kinds were counted. The trace is right about this: a key that shares is never uploaded on its own, so its ranges never change owner. The test now:

  • checks that a key sharing a copy counts nothing and is not counted in the budget again;
  • gets the identical-layout case from B8G8R8A8 over RGBA8 (same layout, another host format, so a copy of its own);
  • keeps the swizzled copy (different) and the released one (gone).

How it was tested

  • overlap::tests::an_upload_takes_the_bytes_it_covers_from_their_last_owner, overlap::tests::the_excess_splits_into_identical_and_different_layouts, texture_tests::an_upload_over_another_copy_counts_what_that_copy_was.
  • cargo test -p victory-hle -p victory -p victory-gpu -p xtask (victory-gpu native arm64: 415 passed, 6 ignored); cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings clean; cargo xtask policy: 399 files ok.
  • Ran the game: not done, as there are no game files on this machine. A world load with this trace and VICTORY_TRACE_TEXTURE_SYNC=1 is asked for on #359.

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • Guesses are marked in the code and in docs/native-host.md (none new: it only counts)
  • The switch is a VICTORY_* variable, off by default, documented in docs/native-host.md

docs/native-host.md also says what #362's sharing can catch: two placed copies at one address never share, because texture_placed releases the copy of another description there before the new one is made, and the same description at the same address is already the same key.

**@⁠m0rff** on GitHub · 2026-11-27 13:49 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/355) `m0rff:m0rff/texture-overlap-trace` → `main` · +237 −1 · 6 files · 1 commits · open Refs #355 One commit on `main`. It only measures and changes nothing a draw samples. It is meant to show what the copies that #362's sharing and covered give-up leave behind are made of (#359: 7507 MiB of copies over 1566 MiB of distinct memory on a world load). ## What this adds `VICTORY_TRACE_TEXTURE_OVERLAP=1` is off by default. When set, it logs once a second: ``` host texture copies overlap: <n> copies, <MiB> held again by identical layouts at one start, <MiB> by different layouts; ranges that changed owner in the last second: <n> (<MiB>) from an identical layout, <n> (<MiB>) from a different one, <n> (<MiB>) from a copy no longer there ``` - **Held again.** Each byte is counted once less than the number of copies over it (`overlap::excess`), split into two kinds: - **Identical layout at one start:** the same pool offset and the same `Layout` (dimensions, mips, element size, swizzle; not the format). Since #362, copies that also have the same host format at the same title address share one texture. They sit in `Title::copies_of`, not `Title::textures`, so they are not counted here. What this figure counts is the rest: another host format over the same layout, which one texture with views could serve, or the same pages reached through two title addresses (sharing goes by title address, this trace by pool offset). - **Different layouts:** another `Layout`, or another start over the same bytes. These could not share a texture. - **Owner changes.** The owner of a range is the copy that last uploaded into it (`Overlap::take`, an interval map updated after each upload in `sync_apply`). An upload that takes bytes from another copy is counted by what that copy was: - a live copy with an identical layout; - a live copy with a different one, the case where one texture per range would re-upload or sample black; - a copy that is gone (released, given up by the budget, or covered), which is memory being reused. With the variable unset, the cost is one cached `OnceLock` check per upload and per sync. ## Rebased on #362 `an_upload_over_another_copy_counts_what_that_copy_was` failed on top of #362 because its second texture had the same description over the same memory as the first, so #362 makes it share the first one's texture. `realize` puts it in `copies_of`, `note_upload` finds no copy of its own and returns, and only two of the three kinds were counted. The trace is right about this: a key that shares is never uploaded on its own, so its ranges never change owner. The test now: - checks that a key sharing a copy counts nothing and is not counted in the budget again; - gets the identical-layout case from B8G8R8A8 over RGBA8 (same layout, another host format, so a copy of its own); - keeps the swizzled copy (different) and the released one (gone). ## How it was tested - [x] `overlap::tests::an_upload_takes_the_bytes_it_covers_from_their_last_owner`, `overlap::tests::the_excess_splits_into_identical_and_different_layouts`, `texture_tests::an_upload_over_another_copy_counts_what_that_copy_was`. - [x] `cargo test -p victory-hle -p victory -p victory-gpu -p xtask` (victory-gpu native arm64: 415 passed, 6 ignored); `cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings` clean; `cargo xtask policy`: 399 files ok. - [ ] Ran the game: not done, as there are no game files on this machine. A world load with this trace and `VICTORY_TRACE_TEXTURE_SYNC=1` is asked for on #359. ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] Guesses are marked in the code and in docs/native-host.md (none new: it only counts) - [x] The switch is a `VICTORY_*` variable, off by default, documented in docs/native-host.md docs/native-host.md also says what #362's sharing can catch: two placed copies at one address never share, because `texture_placed` releases the copy of another description there before the new one is made, and the same description at the same address is already the same key.
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
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 13:54:39 +00:00
github-bot force-pushed github-bot/gh-355 from 4c42697d59 to 538dbd1139 2026-11-27 14:45:59 +00:00 Compare
github-bot force-pushed github-bot/gh-355 from 538dbd1139 to 961bb6543f 2026-11-27 16:13:44 +00:00 Compare
github-bot referenced this pull request from a commit 2026-11-27 16:23:10 +00:00
Author
Owner

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

#301 and #362 are on main now (582670f1). I left this one out: on top of #362 its test an_upload_over_another_copy_counts_what_that_copy_was fails (counts [(0, 0), (1, 4096), (1, 4096)] instead of three (1, 4096)), I think because #362 makes the identical copy share one texture. Could you rebase it on main and update the test? The trace itself looks fine and is off by default.

**@⁠LakshanSivakumar** on GitHub · 2026-11-27 16:23 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/355#issuecomment-7223208524) #301 and #362 are on main now (582670f1). I left this one out: on top of #362 its test `an_upload_over_another_copy_counts_what_that_copy_was` fails (counts `[(0, 0), (1, 4096), (1, 4096)]` instead of three `(1, 4096)`), I think because #362 makes the identical copy share one texture. Could you rebase it on main and update the test? The trace itself looks fine and is off by default.
github-bot force-pushed github-bot/gh-355 from 961bb6543f to 10892d1b78 2026-11-27 22:51:55 +00:00 Compare
Author
Owner

@⁠m0rff on GitHub · 2026-11-27 22:51 UTC · original

@⁠LakshanSivakumar rebased on main (b998bb65) as one commit, 10892d1b.

You were right about the cause. The test's second texture had the same description over the same memory as the first, so #362 has it share the first one's texture. realize puts it in copies_of instead of textures, note_upload finds no copy of its own, and nothing is counted, which gave [(0, 0), (1, 4096), (1, 4096)]. The trace is right to count nothing there: a key that shares a copy is never uploaded on its own, so its range never changes owner. I changed the test, not the trace. It now checks that a shared key counts nothing and is not counted in the budget again, and it gets the identical-layout case from B8G8R8A8 over RGBA8 (same layout, another host format, so a copy of its own). The swizzled copy and the released one stay as they were, one count each.

The rebase also had conflicts with give_up_covered and uploaded_mips from main. I kept both sides; the trace only adds lines next to them. The PR description now says what "identical" counts after #362. I'll mark this ready once CI is green.

**@⁠m0rff** on GitHub · 2026-11-27 22:51 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/355#issuecomment-7229677232) @⁠LakshanSivakumar rebased on main (`b998bb65`) as one commit, `10892d1b`. You were right about the cause. The test's second texture had the same description over the same memory as the first, so #362 has it share the first one's texture. `realize` puts it in `copies_of` instead of `textures`, `note_upload` finds no copy of its own, and nothing is counted, which gave `[(0, 0), (1, 4096), (1, 4096)]`. The trace is right to count nothing there: a key that shares a copy is never uploaded on its own, so its range never changes owner. I changed the test, not the trace. It now checks that a shared key counts nothing and is not counted in the budget again, and it gets the identical-layout case from B8G8R8A8 over RGBA8 (same layout, another host format, so a copy of its own). The swizzled copy and the released one stay as they were, one count each. The rebase also had conflicts with `give_up_covered` and `uploaded_mips` from main. I kept both sides; the trace only adds lines next to them. The PR description now says what "identical" counts after #362. I'll mark this ready once CI is green.
Author
Owner

@⁠Olima34 on GitHub · 2026-11-28 00:37 UTC · original · requested changes

Thanks for this PR! We're helping the maintainer clear the queue tonight, and main has moved a lot (the preview branch landed as 86cefd22, then a batch of PRs; main is now a6a58361). This branch now conflicts with main in crates/victory-gpu/src/native/textures.rs.

Could you merge current main into the branch (a merge commit is fine, no need to rebase) and resolve the conflict? Once GitHub shows it as mergeable and CI is green on the new head, we'll read it through and merge it if it holds up.

**@⁠Olima34** on GitHub · 2026-11-28 00:37 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/355#pullrequestreview-6094669784) · **requested changes** Thanks for this PR! We're helping the maintainer clear the queue tonight, and main has moved a lot (the preview branch landed as 86cefd22, then a batch of PRs; main is now a6a58361). This branch now conflicts with main in `crates/victory-gpu/src/native/textures.rs`. Could you merge current `main` into the branch (a merge commit is fine, no need to rebase) and resolve the conflict? Once GitHub shows it as mergeable and CI is green on the new head, we'll read it through and merge it if it holds up.
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/native/textures.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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