GPU: copies of single views reuse the buffers of dead ones #1977

Closed
0xkn0wh3r3 wants to merge 2 commits from upstream-pr/1979 into main
0xkn0wh3r3 commented 2026-11-25 02:15:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-25T02:15:07Z.


The problem

Every frame, hundreds of small host buffers are made and dropped for the title's buffer views. Each one is a new committed buffer, so this costs frame time.

A buffer view that does not start on an element of its title buffer's copy gets a copy of just its own bytes (shadow::exact). The title places such views at ring positions that move every frame:

  • the meshlet culling's 456-byte constants;
  • the colour bakes' parameter blocks.

So these copies die and are made by the hundred each frame. Each one was a new committed buffer, created and mapped under the global EXACT lock.

Measured on an RTX 5070 Ti (driver 621.24), 20 frames of 300 new 456-byte view copies at moving positions, 300 live at a time:

per frame per copy
main 46.2 ms about 150 us
with the pool 2.2 ms about 7 us

The fix

A dead copy's buffer is kept for the next copy of the same width:

  • When a copy of a single view dies (Shadow::drop), its buffer goes to a pool by width (SPARE, 256 MiB at most in all).
  • The next copy of that width (exact) takes it, and zeroes it over the view's bytes, as a new buffer would be.
  • Only a buffer nothing else references is pooled. A list that kept it may not be submitted yet, so handing it on would let the next copy overwrite bytes that list still reads.

Copies of whole buffers and of loose windows are unchanged, as are the copies' widths. The cost is up to 256 MiB of spare buffers held.

Testing

  • New test shadow::tests::view_copies_reuse_the_buffers_of_dead_ones_nothing_holds: checks that a copy takes a dead one's buffer, zeroed, and that a buffer a list still holds is not handed on.
  • cargo test -p victory-gpu on an RTX 5070 Ti (driver 621.24): all pass except descriptor::host::tests::dynamic_slots_are_held_until_released and memory::tests::lists_share_a_table_and_copies_keep_their_views. Those fail the same way on main (the NVIDIA 1,000,000-descriptor heap cap, #1835).
  • cargo clippy -p victory-gpu --all-targets, with and without --features diagnostics: clean.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6595602561 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1979). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-25T02:15:07Z. --- ## The problem Every frame, hundreds of small host buffers are made and dropped for the title's buffer views. Each one is a new committed buffer, so this costs frame time. A buffer view that does not start on an element of its title buffer's copy gets a copy of just its own bytes (`shadow::exact`). The title places such views at ring positions that move every frame: - the meshlet culling's 456-byte constants; - the colour bakes' parameter blocks. So these copies die and are made by the hundred each frame. Each one was a new committed buffer, created and mapped under the global `EXACT` lock. Measured on an RTX 5070 Ti (driver 621.24), 20 frames of 300 new 456-byte view copies at moving positions, 300 live at a time: | | per frame | per copy | |---|---|---| | main | 46.2 ms | about 150 us | | with the pool | 2.2 ms | about 7 us | ## The fix A dead copy's buffer is kept for the next copy of the same width: - When a copy of a single view dies (`Shadow::drop`), its buffer goes to a pool by width (`SPARE`, 256 MiB at most in all). - The next copy of that width (`exact`) takes it, and zeroes it over the view's bytes, as a new buffer would be. - Only a buffer nothing else references is pooled. A list that kept it may not be submitted yet, so handing it on would let the next copy overwrite bytes that list still reads. Copies of whole buffers and of loose windows are unchanged, as are the copies' widths. The cost is up to 256 MiB of spare buffers held. ## Testing - New test `shadow::tests::view_copies_reuse_the_buffers_of_dead_ones_nothing_holds`: checks that a copy takes a dead one's buffer, zeroed, and that a buffer a list still holds is not handed on. - `cargo test -p victory-gpu` on an RTX 5070 Ti (driver 621.24): all pass except `descriptor::host::tests::dynamic_slots_are_held_until_released` and `memory::tests::lists_share_a_table_and_copies_keep_their_views`. Those fail the same way on `main` (the NVIDIA 1,000,000-descriptor heap cap, #1835). - `cargo clippy -p victory-gpu --all-targets`, with and without `--features diagnostics`: clean.
catminou4 commented 2026-11-25 03:03:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T02:48:38Z.

Review: COMMENTED


Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff.

Single-view copies reuse buffers of dead ones — allocator reuse; check the lifetime proof (a 'dead' buffer must not still be referenced by an in-flight submission).

<!-- greyfinch-sync:Marvisak/VIctory:review:6051086926 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1979#pullrequestreview-6051086926). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T02:48:38Z. **Review:** COMMENTED --- Static review only: victory-gpu is Windows-only (D3D12/windows_sys) — nothing compiles or runs on macOS; source review of the diff. Single-view copies reuse buffers of dead ones — allocator reuse; check the lifetime proof (a 'dead' buffer must not still be referenced by an in-flight submission).
DesertGamer commented 2026-11-25 03:03:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DesertGamer on 2026-11-25T02:16:06Z.


VIctory V2: community PRs, merged and tested

Open PRs sitting in the queue? V2 pulls them into one build, screens each one (binaries, build scripts, hooks, new deps) and runs tests after every batch.

Where it stands today: launches, runs the menu at 30 fps, plays the intro video, reaches the start screen and loads the world (~50 s). Entering the world is the current blocker: rendering fails with a GPU "device removed" error (tracked in issue #1).

⚠️ Entering the world on a real GPU can hang it and make Windows reset the driver. Try it at your own risk.

Not playable yet, but every batch of merged fixes moves it closer. If you have a PR that's stuck, or a GPU to test on, I'd love your help:
https://DesertGamer/VIctory-V2

Unofficial community fork of Marvisak/VIctory, GPL-3.0.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7150512923 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1979#issuecomment-7150512923). Originally posted by [DesertGamer](https://github.com/DesertGamer) on 2026-11-25T02:16:06Z. --- ### VIctory V2: community PRs, merged and tested Open PRs sitting in the queue? V2 pulls them into one build, screens each one (binaries, build scripts, hooks, new deps) and runs tests after every batch. **Where it stands today:** launches, runs the menu at 30 fps, plays the intro video, reaches the start screen and loads the world (~50 s). Entering the world is the current blocker: rendering fails with a GPU "device removed" error (tracked in issue #1). :warning: Entering the world on a real GPU can hang it and make Windows reset the driver. Try it at your own risk. Not playable yet, but every batch of merged fixes moves it closer. If you have a PR that's stuck, or a GPU to test on, I'd love your help: https://DesertGamer/VIctory-V2 *Unofficial community fork of Marvisak/VIctory, GPL-3.0.*
jeremygold02 commented 2026-11-25 04:07:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-25T03:43:27Z.


Triage verdict — Keep the allocation win; use an explicit retirement condition (0a690f2)

Evidence

Checks and limits

  • The reported same-size benchmark is useful. The new test checks reuse and a manually cloned resource reference, but it does not record a copy that reads the old buffer. I compared source and the API contract; I did not reproduce early reuse or run a GPU test.

Next step

  • Make pool eligibility explicit: keep recorded and in-flight users attached to the buffer, then recycle after the final owner and the applicable submission fence release it. Add a synthetic test that records a read of the first view, drops its Shadow, and checks contents before and after the buffer becomes eligible for reuse.

This verification was AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7152182708 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1979#issuecomment-7152182708). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-25T03:43:27Z. --- **Triage verdict — Keep the allocation win; use an explicit retirement condition** (`0a690f2`) **Evidence** - `Shadow::drop` uses the value returned by `IUnknown::Release` to decide whether the mapped buffer can be zeroed and reused. Microsoft documents that [return value as test-only](https://learn.microsoft.com/en-us/windows/win32/api/unknwn/nf-unknwn-iunknown-release). [D3D12 command lists do not take references to recorded objects](https://learn.microsoft.com/en-us/windows/win32/direct3d12/recording-command-lists-and-bundles#reference-counting), so the reuse condition needs to follow VIctory’s own command-list ownership and completion rules. **Checks and limits** - The reported same-size benchmark is useful. The new test checks reuse and a manually cloned resource reference, but it does not record a copy that reads the old buffer. I compared source and the API contract; I did not reproduce early reuse or run a GPU test. **Next step** - Make pool eligibility explicit: keep recorded and in-flight users attached to the buffer, then recycle after the final owner and the applicable submission fence release it. Add a synthetic test that records a read of the first view, drops its `Shadow`, and checks contents before and after the buffer becomes eligible for reuse. This verification was AI-assisted.
0xkn0wh3r3 commented 2026-11-25 07:03:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-25T06:07:30Z.


Thanks @jeremygold02, that's a fair point. 4efedc7 makes the rule explicit:

  • Ownership: everything in VIctory that uses a buffer holds a COM reference to it, just as main relies on to avoid freeing a buffer still in use. That covers lists (what they keep, and the copies their steps name) until they're reset, view heap slots, and slots rewritten mid-submission (host::retire). A buffer is only given back when no other reference exists. Once that count reads zero it can't go back up, because taking a reference needs one, so the race COM's documentation warns about can't happen here.
  • GPU completion: a buffer given back now becomes reusable only after the submission running at that point has finished. host::release_retired, which already ends retired resources' lives after a submission, now also settles the pool.

Tests:

  • spare_buffers_wait_for_a_submission: runs on a pool instance of its own. Nothing given back is taken before a settle. After that, buffers come back by width, last given first, within the byte limit. It fails if buffers are made reusable straight away.
  • a_dead_copy_a_list_holds_is_not_handed_on: the test you asked for. A list records a GPU read of a copy and keeps its buffer, then the copy dies. The buffer isn't given back, and the submitted read sees the copy's original bytes.

I replaced the old reuse test. It depended on which buffer a new copy got from the process-wide pool, and other tests running in parallel could change that.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7154467080 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1979#issuecomment-7154467080). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-25T06:07:30Z. --- Thanks @jeremygold02, that's a fair point. `4efedc7` makes the rule explicit: - **Ownership:** everything in VIctory that uses a buffer holds a COM reference to it, just as `main` relies on to avoid freeing a buffer still in use. That covers lists (what they `keep`, and the copies their steps name) until they're reset, view heap slots, and slots rewritten mid-submission (`host::retire`). A buffer is only given back when no other reference exists. Once that count reads zero it can't go back up, because taking a reference needs one, so the race COM's documentation warns about can't happen here. - **GPU completion:** a buffer given back now becomes reusable only after the submission running at that point has finished. `host::release_retired`, which already ends retired resources' lives after a submission, now also settles the pool. Tests: - `spare_buffers_wait_for_a_submission`: runs on a pool instance of its own. Nothing given back is taken before a settle. After that, buffers come back by width, last given first, within the byte limit. It fails if buffers are made reusable straight away. - `a_dead_copy_a_list_holds_is_not_handed_on`: the test you asked for. A list records a GPU read of a copy and keeps its buffer, then the copy dies. The buffer isn't given back, and the submitted read sees the copy's original bytes. I replaced the old reuse test. It depended on which buffer a new copy got from the process-wide pool, and other tests running in parallel could change that.
Marvisak commented 2026-11-25 09:06:16 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T08:24:47Z.


Automated triage for the maintainers.

This pull request no longer merges cleanly into main (conflicts in crates/victory-gpu/src/host.rs, crates/victory-gpu/src/shadow.rs). It will be reviewed once it is rebased onto the current main. The earlier verdict (needs work) was for an older state of main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7156993746 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1979#issuecomment-7156993746). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T08:24:47Z. --- <!-- victory-triage v1 head=4efedc71d944d7afb1a0a50d740245f939f344f4 verdict=needs-rebase at=2026-11-25T16:19:00Z --> Automated triage for the maintainers. This pull request no longer merges cleanly into `main` (conflicts in `crates/victory-gpu/src/host.rs`, `crates/victory-gpu/src/shadow.rs`). It will be reviewed once it is rebased onto the current `main`. The earlier verdict (needs work) was for an older state of `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
upstream-sync closed this pull request 2026-11-25 19:04:49 +00:00
0xkn0wh3r3 commented 2026-11-25 19:06:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 0xkn0wh3r3 on 2026-11-25T18:50:59Z.


Closing: upstream 6cc6aa3 (copies of single views share large buffers) covers this. Single-view copies now take runs of shared 16 MiB arenas, and larger ones get an arena chunk that is reused too, so no committed buffer is made per copy for a pool to reuse. Lists and heap slots hold the Arc<Shadow>, so a run given back on drop is safe without this PR's after-a-submission rule.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7171487520 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1979#issuecomment-7171487520). Originally posted by [0xkn0wh3r3](https://github.com/0xkn0wh3r3) on 2026-11-25T18:50:59Z. --- Closing: upstream 6cc6aa3 (copies of single views share large buffers) covers this. Single-view copies now take runs of shared 16 MiB arenas, and larger ones get an arena chunk that is reused too, so no committed buffer is made per copy for a pool to reuse. Lists and heap slots hold the `Arc<Shadow>`, so a run given back on drop is safe without this PR's after-a-submission rule.

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
Marvisak/VIctory!1977
No description provided.