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

Open
m0rff wants to merge 1 commit from upstream-pr/355 into main
m0rff commented 2026-11-27 13:49:59 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by m0rff on 2026-11-27T13:49:59Z.


Refs #352

One commit on main. It only measures and changes nothing a draw samples. It is meant to show what the copies that #359's sharing and covered give-up leave behind are made of (#356: 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 #359, 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 #359

an_upload_over_another_copy_counts_what_that_copy_was failed on top of #359 because its second texture had the same description over the same memory as the first, so #359 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 #356.

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 #359'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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5527736938 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/355). Originally posted by [m0rff](https://github.com/m0rff) on 2026-11-27T13:49:59Z. --- Refs #352 One commit on `main`. It only measures and changes nothing a draw samples. It is meant to show what the copies that #359's sharing and covered give-up leave behind are made of (#356: 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 #359, 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 #359 `an_upload_over_another_copy_counts_what_that_copy_was` failed on top of #359 because its second texture had the same description over the same memory as the first, so #359 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 #356. ## 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 #359'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.
LakshanSivakumar commented 2026-11-27 16:23:39 +00:00 (Migrated from github.com)

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


#301 and #359 are on main now (582670f1). I left this one out: on top of #359 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 #359 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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7223208524 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/355#issuecomment-7223208524). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-27T16:23:39Z. --- #301 and #359 are on main now (582670f1). I left this one out: on top of #359 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 #359 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.
m0rff commented 2026-11-27 22:51:42 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by m0rff on 2026-11-27T22:51:42Z.


@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 #359 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 #359. I'll mark this ready once CI is green.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7229677232 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/355#issuecomment-7229677232). Originally posted by [m0rff](https://github.com/m0rff) on 2026-11-27T22:51:42Z. --- @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 #359 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 #359. I'll mark this ready once CI is green.
Olima34 commented 2026-11-28 00:37:18 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-28T00:37:18Z.

Review: CHANGES_REQUESTED


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6094669784 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/355#pullrequestreview-6094669784). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-28T00:37:18Z. **Review:** CHANGES_REQUESTED --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/355:upstream-pr/355'
git switch 'upstream-pr/355'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/355'
git switch 'upstream-pr/355'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/355'
git switch 'upstream-pr/355'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/355'
git switch 'main'
git merge --squash 'upstream-pr/355'
git switch 'main'
git merge --ff-only 'upstream-pr/355'
git switch 'main'
git merge 'upstream-pr/355'
git push origin 'main'
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!355
No description provided.