metal: a typed buffer view written back into a heap entry names the texture made for it before (#47) #398

Open
Tarraga wants to merge 1 commit from upstream-pr/398 into main
Tarraga commented 2026-11-27 16:44:29 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Tarraga on 2026-11-27T16:44:29Z.


Refs #47. This covers only the piece I claimed there: typed buffer views made again as new textures.

What this changes

Problem

The title writes the same records into its shader-visible heaps again and again (#43). Each record mirrored again is a raw view first. materialize_typed_buffers then promotes it again at the next draw that reads it typed. Every promotion called newTextureWithDescriptor for a texture buffer the heap had held before, on the submitting thread, under the host's lock.

Evidence

From one run of main 4cf84c83 (M4 Pro, 48 GB, Series S, headless, 425 s, reached the cell). The numbers come from main's own lines, typed_views on native draws so far and native typed buffer views:

  • 6,100-6,400 promotions a world frame (frames 3264-3456);
  • 145,000-149,000 live typed views at the same time.

Our working branch also mirrors whole heaps at submission, so it mirrors more entries: 10,500 a frame there against about 160,000 live. Those numbers are not main's.

Cost of one view, typed_view_cost (10,000 views, three runs each):

x86_64 under Rosetta (how the game runs) arm64
written back, without the cache 1.58-1.64 µs 0.78-0.81 µs
written back, from the cache 0.44-0.48 µs 0.28-0.34 µs
first time, with the cache (made and inserted) 2.01-2.21 µs 0.97-1.01 µs

At main's rate, that saves about 7 ms of a world frame under the lock. At about 1.3 fps, that is roughly 1% of the frame. It's a small gain, and I'm not claiming more.

Fix

Metal::write_typed_buffer keeps the texture it makes, keyed by buffer, offset, width, pixel format and writability, and names it again when the same view is written back.

  • The cached texture holds its buffer, so the buffer's address in the key is not reused while the entry is cached.
  • The entry's three words are the same as with a freshly made texture. Heap residency is unchanged: an entry's last let-go still removes its allocation from the heap's set and commits.
  • Bounded: at 131,072 entries (VICTORY_TYPED_VIEW_CACHE=<entries>) the cache is emptied and starts again.

Default

The cache is on by default, and this does change behaviour for others: a buffer the host has let go stays alive in the cache until the next reset, together with its texture objects. It does not change what any entry holds, and its cost doesn't depend on the GPU's speed. That is why I left it on. VICTORY_TYPED_VIEW_CACHE=0 makes a texture every time, as before. If you would rather have it off until someone with 16 GB has run it, I'll flip the default.

How it was tested

  • gfx::typed_view_tests::a_typed_view_written_again_reuses_its_texture_and_others_get_their_own (victory-metal, on Metal). It writes the same view into two entries and back into the first, then views that differ in offset, width, format or writability. It also replaces an entry with a raw view and promotes it again, and checks the entry words and typed_views counts throughout.
    • Passes on arm64 and on x86_64 (D3DMetal 4.0b3's converter).
    • With VICTORY_TYPED_VIEW_CACHE=0 (what main does) it fails: left: (3, 0) right: (1, 2), three textures made instead of one.
  • cargo test -p victory-metal --lib --release on arm64: 43 pass, 2 ignored (benchmarks).
  • cargo xtask policy passes.
  • Windows clippy: not run locally (this Mac has no x86_64-pc-windows-gnullvm target); I'm leaving it to CI. Host clippy shows no warning in the changed lines.
  • Ran the game: not with this change. The run above is of main without it, for the counts.
  • The tests ran on 4cf84c83. The rebase onto b998bb65 only brings crates/victory-metal/tests/resources.rs into victory-metal, not the library.

Checklist

  • No game files, game code, disassembly listings or SDK files are included
  • No guesses about undocumented console behaviour (a host-side cache only)
  • The switch is behind VICTORY_TYPED_VIEW_CACHE and documented in docs/native-host.md (diagnostics list and "Typed and structured buffer views lazily carry a Metal texture_buffer")
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:5530117823 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/398). Originally posted by [Tarraga](https://github.com/Tarraga) on 2026-11-27T16:44:29Z. --- Refs #47. This covers only the piece I claimed there: typed buffer views made again as new textures. ## What this changes ### Problem The title writes the same records into its shader-visible heaps again and again (#43). Each record mirrored again is a raw view first. `materialize_typed_buffers` then promotes it again at the next draw that reads it typed. Every promotion called `newTextureWithDescriptor` for a texture buffer the heap had held before, on the submitting thread, under the host's lock. ### Evidence From one run of main 4cf84c83 (M4 Pro, 48 GB, Series S, headless, 425 s, reached the cell). The numbers come from main's own lines, `typed_views` on `native draws so far` and `native typed buffer views`: - 6,100-6,400 promotions a world frame (frames 3264-3456); - 145,000-149,000 live typed views at the same time. Our working branch also mirrors whole heaps at submission, so it mirrors more entries: 10,500 a frame there against about 160,000 live. Those numbers are not main's. Cost of one view, `typed_view_cost` (10,000 views, three runs each): | | x86_64 under Rosetta (how the game runs) | arm64 | |---|---|---| | written back, without the cache | 1.58-1.64 µs | 0.78-0.81 µs | | written back, from the cache | 0.44-0.48 µs | 0.28-0.34 µs | | first time, with the cache (made and inserted) | 2.01-2.21 µs | 0.97-1.01 µs | At main's rate, that saves about 7 ms of a world frame under the lock. At about 1.3 fps, that is roughly 1% of the frame. It's a small gain, and I'm not claiming more. ### Fix `Metal::write_typed_buffer` keeps the texture it makes, keyed by buffer, offset, width, pixel format and writability, and names it again when the same view is written back. - The cached texture holds its buffer, so the buffer's address in the key is not reused while the entry is cached. - The entry's three words are the same as with a freshly made texture. Heap residency is unchanged: an entry's last let-go still removes its allocation from the heap's set and commits. - Bounded: at 131,072 entries (`VICTORY_TYPED_VIEW_CACHE=<entries>`) the cache is emptied and starts again. ### Default The cache is on by default, and this does change behaviour for others: a buffer the host has let go stays alive in the cache until the next reset, together with its texture objects. It does not change what any entry holds, and its cost doesn't depend on the GPU's speed. That is why I left it on. `VICTORY_TYPED_VIEW_CACHE=0` makes a texture every time, as before. If you would rather have it off until someone with 16 GB has run it, I'll flip the default. ## How it was tested - `gfx::typed_view_tests::a_typed_view_written_again_reuses_its_texture_and_others_get_their_own` (victory-metal, on Metal). It writes the same view into two entries and back into the first, then views that differ in offset, width, format or writability. It also replaces an entry with a raw view and promotes it again, and checks the entry words and `typed_views` counts throughout. - Passes on arm64 and on x86_64 (D3DMetal 4.0b3's converter). - With `VICTORY_TYPED_VIEW_CACHE=0` (what main does) it fails: `left: (3, 0) right: (1, 2)`, three textures made instead of one. - `cargo test -p victory-metal --lib --release` on arm64: 43 pass, 2 ignored (benchmarks). - `cargo xtask policy` passes. - [ ] Windows clippy: not run locally (this Mac has no `x86_64-pc-windows-gnullvm` target); I'm leaving it to CI. Host clippy shows no warning in the changed lines. - [ ] Ran the game: not with this change. The run above is of main without it, for the counts. - The tests ran on 4cf84c83. The rebase onto b998bb65 only brings `crates/victory-metal/tests/resources.rs` into victory-metal, not the library. ## Checklist - [x] No game files, game code, disassembly listings or SDK files are included - [x] No guesses about undocumented console behaviour (a host-side cache only) - [x] The switch is behind `VICTORY_TYPED_VIEW_CACHE` and documented in docs/native-host.md (diagnostics list and "Typed and structured buffer views lazily carry a Metal `texture_buffer`")
AnastasisKon commented 2026-11-28 17:14:54 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-28T17:14:54Z.

Review: APPROVED


Read the diff: typed buffer views re-promoted from a heap entry now name (cache) the texture made for that entry before, instead of calling newTextureWithDescriptor again on the submitting thread per re-promotion — 158/-19 in gfx.rs, small metal.rs addition, docs paragraph added. Verified on the head bc4bdda: cargo test -p victory-metal --lib 43 passed / 0 failed on this M3 Pro, and the workspace windows clippy with -D warnings is clean. Approving.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6099237041 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/398#pullrequestreview-6099237041). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-28T17:14:54Z. **Review:** APPROVED --- Read the diff: typed buffer views re-promoted from a heap entry now name (cache) the texture made for that entry before, instead of calling `newTextureWithDescriptor` again on the submitting thread per re-promotion — 158/-19 in `gfx.rs`, small `metal.rs` addition, docs paragraph added. Verified on the head bc4bdda: `cargo test -p victory-metal --lib` 43 passed / 0 failed on this M3 Pro, and the workspace windows clippy with `-D warnings` is clean. Approving.
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/398:upstream-pr/398'
git switch 'upstream-pr/398'

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