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

Closed
github-bot wants to merge 2 commits from gh-pr/1979/head into gh-pr/1979/base
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-25 02:15 UTC · original
0xkn0wh3r3:gpu/reuse-view-copy-buffers → main · +209 −5 · 2 files · 2 commits · closed without merging

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.
**@⁠0xkn0wh3r3** on GitHub · 2026-11-25 02:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/1979) `0xkn0wh3r3:gpu/reuse-view-copy-buffers` → `main` · +209 −5 · 2 files · 2 commits · closed without merging ## 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.
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, and each was a new committed
buffer, created and mapped under the global `EXACT` lock.

A dead copy's buffer now goes to a pool by width (256 MiB at most), and
the next copy of that width takes it, zeroed 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. Copies of whole buffers and of
loose windows are unchanged, as are the copies' widths.

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: 46.2 ms a frame on
main, 2.2 ms with the pool (about 150 us against 7 us a copy).

The new test checks that a copy takes a dead one's buffer, zeroed, and
that a buffer a list still holds is not handed on.
Review (#1979) asked for an explicit rule for when a dead single-view
copy's buffer may be reused, rather than resting on `Release`'s count.

What uses a buffer holds a COM reference to it, as on main, where
dropping the last one frees it: lists (what they keep, and the copies
their steps name) until they are reset, view heap slots, and slots
rewritten while a submission runs (`host::retire`). A buffer is given
back only when no other reference is held; with none, no one can take
one again (cloning needs one), so the count read at drop cannot rise.

On top of that, a buffer given back now waits until the submission
running then has run before it is spare: `host::release_retired`, which
already ends retired resources' lives after a submission, settles the
pool. The pool is a type of its own (`Pool`: give, settle, take, within
its byte limit).

Tests:
- `spare_buffers_wait_for_a_submission` (on a pool of its own, so other
  tests' copies and submissions do not interleave): nothing given back
  is taken before a settle; then the last given first, by width; the
  byte limit drops the rest. Settling at give fails it.
- `a_dead_copy_a_list_holds_is_not_handed_on`: a list records a GPU read
  of a copy and keeps its buffer; the copy dies; its buffer is not given
  back, and the submitted read sees the copy's bytes.
The previous test, which counted on which buffer a new copy got from the
process-wide pool, could pass or fail with other tests' copies running
in parallel; it is replaced by these two.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:50:35 +00:00
github-bot closed this pull request 2026-11-26 04:50:35 +00:00
Author
Owner

@⁠DesertGamer on GitHub · 2026-11-25 02:16 UTC · original

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.

**@⁠DesertGamer** on GitHub · 2026-11-25 02:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/1979#issuecomment-7150512923) ### 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.*
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-25 03:43 UTC · original

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.

**@⁠jeremygold02** on GitHub · 2026-11-25 03:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/1979#issuecomment-7152182708) **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.
Author
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-25 06:07 UTC · original

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.

**@⁠0xkn0wh3r3** on GitHub · 2026-11-25 06:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/1979#issuecomment-7154467080) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 08:24 UTC · original · edited 2026-11-25 16:05 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 08:24 UTC · [original](https://github.com/Marvisak/VIctory/pull/1979#issuecomment-7156993746) · edited 2026-11-25 16:05 UTC <!-- 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)_
Author
Owner

@⁠0xkn0wh3r3 on GitHub · 2026-11-25 18:50 UTC · original

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.

**@⁠0xkn0wh3r3** on GitHub · 2026-11-25 18:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/1979#issuecomment-7171487520) 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

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1979
No description provided.