victory-gpu: opt-in batched worker resource retirement (VICTORY_BATCH_RETIRE=1) #295

Manually merged
github merged 4 commits from github-bot/gh-295 into gh-pr/295/base 2026-11-27 16:36:32 +00:00 AGit
Owner

@⁠Vladimir-Human on GitHub · 2026-11-27 14:05 UTC · original
Vladimir-Human:perf/opt-in-batched-retire → main · +443 −5 · 2 files · 4 commits · merged 2026-11-27 16:30 UTC

Summary

Split out of #268 so the ID3D12Resource retirement lifetime can be reviewed and measured independently against a matched baseline.

Adds opt-in thread-local batching for retired ID3D12Resource objects on command-list translation worker threads behind VICTORY_BATCH_RETIRE=1 (default off; live VICTORY_AB toggle supported in diagnostics builds).

Adapted from voidopcode/vilocity commit 4315c77.

Changes (2 files: crates/victory-gpu/src/host.rs, crates/victory-gpu/src/list/thin.rs)

  • Opt-in switch (host.rs): batch_retire_enabled() defaults to false and enables batching only when VICTORY_BATCH_RETIRE=1 (or VICTORY_AB selects it in diagnostics). When off, retire_resource_with hands entries directly to RETIRED and immediately flushes any buffered leftovers if toggled off mid-run.
  • Idempotent thread-local batching (host.rs): RetiredBatch { eligible, entries } accumulates up to BATCH_MOST (256) retired resources per eligible worker thread before handing them over to RETIRED in one lock acquisition. retire_in_batches() is idempotent and never discards already-buffered entries; RetiredBatch::drop flushes remaining entries on thread exit (safe under TLS teardown via PART.try_with(...).unwrap_or(0)).
  • Stamp-ordered shard handover (host.rs): Every insertion into a RETIRED shard (retire, direct retire_resource_with, flush_retired, RetiredBatch::drop) routes through insert_retired / merge_retired_batch, keeping each shard ordered by ascending submission stamp so bury()'s prefix drain always releases older resources at their own fence deadline.
  • Worker boundaries (list/thin.rs): Marks command-list translation worker threads eligible at startup (retire_in_batches()) and flushes buffered retirements after every recording.run() (flush_retired()).

Tests & Verification

Verified with VICTORY_D3D12_DEBUG=1 (0 D3D12 debug errors):

  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings (default & --features diagnostics): 0 warnings
  • xtask policy --staged & cargo test -p xtask --locked: 11/11 passed
  • 8 unit + real D3D12 HW/WARP tests in host::retire_batch_tests:
    • a_late_batch_does_not_hide_eligible_retirements
    • batch_retire_switch_defaults_off_and_accepts_only_one
    • a_thread_without_a_batch_keeps_nothing_and_a_full_batch_signals_flush
    • batched_retirement_off_hands_over_immediately_and_flushes_leftovers
    • retire_in_batches_is_idempotent_with_buffered_entries
    • batched_resource_retirement_flushes_concurrently_without_losing_entries
    • batched_retirement_preserves_resource_lifetime_until_bury_on_gpu
    • mixed_batched_and_direct_retirement_releases_older_resource_at_its_own_deadline_on_gpu (reproduces the same-thread N batched resource -> N + 1 direct retire(()) + resource handover across batching off, explicit flush_retired(), and RetiredBatch::drop, verifying refcount drops to 1 at bury(N + bury_lag()) while N + 1 stays alive until bury(N + 1 + bury_lag()))
  • HW RTX 4070 SUPER (--features diagnostics, VICTORY_D3D12_DEBUG=1, --test-threads=1): off 11/11 (retire_batch_tests + late_tests); VICTORY_BATCH_RETIRE=1 59/59 (retire_batch_tests + late_tests + descriptor::host::tests)
  • WARP (VICTORY_D3D12_WARP=1, --features diagnostics, VICTORY_D3D12_DEBUG=1, --test-threads=1): VICTORY_BATCH_RETIRE=1 11/11
**@⁠Vladimir-Human** on GitHub · 2026-11-27 14:05 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/295) `Vladimir-Human:perf/opt-in-batched-retire` → `main` · +443 −5 · 2 files · 4 commits · merged 2026-11-27 16:30 UTC ## Summary Split out of #268 so the `ID3D12Resource` retirement lifetime can be reviewed and measured independently against a matched baseline. Adds opt-in thread-local batching for retired `ID3D12Resource` objects on command-list translation worker threads behind `VICTORY_BATCH_RETIRE=1` (default **off**; live `VICTORY_AB` toggle supported in `diagnostics` builds). Adapted from `voidopcode/vilocity` commit `4315c77`. ## Changes (`2 files: crates/victory-gpu/src/host.rs`, `crates/victory-gpu/src/list/thin.rs`) - **Opt-in switch (`host.rs`):** `batch_retire_enabled()` defaults to `false` and enables batching only when `VICTORY_BATCH_RETIRE=1` (or `VICTORY_AB` selects it in `diagnostics`). When off, `retire_resource_with` hands entries directly to `RETIRED` and immediately flushes any buffered leftovers if toggled off mid-run. - **Idempotent thread-local batching (`host.rs`):** `RetiredBatch { eligible, entries }` accumulates up to `BATCH_MOST` (`256`) retired resources per eligible worker thread before handing them over to `RETIRED` in one lock acquisition. `retire_in_batches()` is idempotent and never discards already-buffered entries; `RetiredBatch::drop` flushes remaining entries on thread exit (safe under TLS teardown via `PART.try_with(...).unwrap_or(0)`). - **Stamp-ordered shard handover (`host.rs`):** Every insertion into a `RETIRED` shard (`retire`, direct `retire_resource_with`, `flush_retired`, `RetiredBatch::drop`) routes through `insert_retired` / `merge_retired_batch`, keeping each shard ordered by ascending submission stamp so `bury()`'s prefix drain always releases older resources at their own fence deadline. - **Worker boundaries (`list/thin.rs`):** Marks command-list translation worker threads eligible at startup (`retire_in_batches()`) and flushes buffered retirements after every `recording.run()` (`flush_retired()`). ## Tests & Verification Verified with `VICTORY_D3D12_DEBUG=1` (0 D3D12 debug errors): - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` (default & `--features diagnostics`): **0 warnings** - `xtask policy --staged` & `cargo test -p xtask --locked`: **11/11 passed** - 8 unit + real D3D12 HW/WARP tests in `host::retire_batch_tests`: - `a_late_batch_does_not_hide_eligible_retirements` - `batch_retire_switch_defaults_off_and_accepts_only_one` - `a_thread_without_a_batch_keeps_nothing_and_a_full_batch_signals_flush` - `batched_retirement_off_hands_over_immediately_and_flushes_leftovers` - `retire_in_batches_is_idempotent_with_buffered_entries` - `batched_resource_retirement_flushes_concurrently_without_losing_entries` - `batched_retirement_preserves_resource_lifetime_until_bury_on_gpu` - `mixed_batched_and_direct_retirement_releases_older_resource_at_its_own_deadline_on_gpu` (reproduces the same-thread `N` batched resource -> `N + 1` direct `retire(())` + resource handover across batching off, explicit `flush_retired()`, and `RetiredBatch::drop`, verifying refcount drops to `1` at `bury(N + bury_lag())` while `N + 1` stays alive until `bury(N + 1 + bury_lag())`) - HW RTX 4070 SUPER (`--features diagnostics`, `VICTORY_D3D12_DEBUG=1`, `--test-threads=1`): off **11/11** (`retire_batch_tests` + `late_tests`); `VICTORY_BATCH_RETIRE=1` **59/59** (`retire_batch_tests` + `late_tests` + `descriptor::host::tests`) - WARP (`VICTORY_D3D12_WARP=1`, `--features diagnostics`, `VICTORY_D3D12_DEBUG=1`, `--test-threads=1`): `VICTORY_BATCH_RETIRE=1` **11/11**
Add opt-in thread-local batching for retired ID3D12Resource objects on
command-list translation worker threads behind VICTORY_BATCH_RETIRE=1
(off by default; VICTORY_AB live toggle supported in diagnostics builds),
isolated to the resource retirement lifetime for independent matched-fixture
evaluation.

- Keep baseline immediate RETIRED handoff unchanged when unset or not
  equal to 1, and flush any buffered leftovers immediately if toggled off.
- Accumulate up to BATCH_MOST (256) retired resources per eligible worker
  thread in host.rs (RetiredBatch) and flush at recording boundaries in
  list/thin.rs, with Drop flushing any remaining entries on thread exit
  so resources are never dropped prematurely before bury().
- Make retire_in_batches() idempotent so repeated calls never discard
  already-buffered entries.
- Add unit and D3D12 HW/WARP tests covering switch gating, capacity
  flushing, off-toggle flushing, idempotency, concurrent multi-thread
  flushing, and COM refcount lifetime preservation until bury().

Adapted from voidopcode/vilocity commit 4315c77.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 14:19:23 +00:00
Author
Owner

@⁠efsebayern on GitHub · 2026-11-27 14:17 UTC · original

I reviewed 3e83d9d on Windows with an RTX 4080 SUPER. I’d address one retirement-ordering case before merging.

flush_retired() appends the batch to the shard’s deque. bury() drains only the eligible prefix. A newer entry can therefore block an older, eligible resource behind it.

I reproduced this in a focused test with the D3D12 debug layer enabled. On the same thread, I retired a resource at stamp N, advanced the stamp by one, called retire(()), flushed the batch, and called bury(N + bury_lag()). With batching off, the resource’s COM refcount dropped to 1 (the probe’s reference). With batching on, it stayed at 2 until bury(N + 1 + bury_lag()). The repro shows delayed release. I suggest preserving stamp order when handing off a batch, or making reclamation handle out-of-order entries, and adding this mixed-retirement case to the tests.

The local diagnostics run with batching enabled on the RTX passed 140 host and descriptor tests (17 ignored). The six focused retirement tests also passed with batching disabled on the RTX and with batching enabled under WARP. Gameplay performance remains unmeasured.

**@⁠efsebayern** on GitHub · 2026-11-27 14:17 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/295#issuecomment-7220602643) I reviewed `3e83d9d` on Windows with an RTX 4080 SUPER. I’d address one retirement-ordering case before merging. [`flush_retired()`](https://github.com/TaynanGT/Vice-Tay/blob/3e83d9d1e8a03fa55a55825ad2df5869038e10ac/crates/victory-gpu/src/host.rs#L705) appends the batch to the shard’s deque. [`bury()`](https://github.com/TaynanGT/Vice-Tay/blob/3e83d9d1e8a03fa55a55825ad2df5869038e10ac/crates/victory-gpu/src/host.rs#L895) drains only the eligible prefix. A newer entry can therefore block an older, eligible resource behind it. I reproduced this in a focused test with the D3D12 debug layer enabled. On the same thread, I retired a resource at stamp N, advanced the stamp by one, called `retire(())`, flushed the batch, and called `bury(N + bury_lag())`. With batching off, the resource’s COM refcount dropped to 1 (the probe’s reference). With batching on, it stayed at 2 until `bury(N + 1 + bury_lag())`. The repro shows delayed release. I suggest preserving stamp order when handing off a batch, or making reclamation handle out-of-order entries, and adding this mixed-retirement case to the tests. The local diagnostics run with batching enabled on the RTX passed 140 host and descriptor tests (17 ignored). The six focused retirement tests also passed with batching disabled on the RTX and with batching enabled under WARP. Gameplay performance remains unmeasured.
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 14:29 UTC · original

@⁠efsebayern The mixed-retirement test shows exactly where the eligible entry gets stuck. @⁠Vladimir-Human Each shard needs to keep the original stamp order when a batch is handed over. The regression should check that the older resource releases at its own fence deadline. Keeping #295 separate lets #268 be measured on its own.

**@⁠TaynanGT** on GitHub · 2026-11-27 14:29 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/295#issuecomment-7220846294) @⁠efsebayern The mixed-retirement test shows exactly where the eligible entry gets stuck. @⁠Vladimir-Human Each shard needs to keep the original stamp order when a batch is handed over. The regression should check that the older resource releases at its own fence deadline. Keeping #295 separate lets #268 be measured on its own.
Merge late worker batches with the original submission stamps so the eligible prefix can be released. Cover mixed retirement order with a queue-local regression.
github-bot force-pushed github-bot/gh-295 from f31478fca7 to 2f27f38f56 2026-11-27 15:05:34 +00:00 Compare
Author
Owner

@⁠Vladimir-Human on GitHub · 2026-11-27 15:03 UTC · original

@⁠efsebayern @⁠TaynanGT Thanks for the sharp catch and clear repro — updated #295 (rebased onto main b67e8d8, commit 2f27f38):

  • Stamp-ordered shard handover (crates/victory-gpu/src/host.rs): Routed every RETIRED shard insertion (retire, direct retire_resource_with, flush_retired, and RetiredBatch::drop) through push_retired_ordered / extend_retired_ordered. The monotonic fast path still performs a zero-allocation extend; when older batched entries arrive behind newer direct entries, extend_retired_ordered performs a stable two-pointer merge over only queue[split..] so each shard keeps ascending stamp order and bury() releases older resources at their own fence deadline.
  • Mixed-retirement regression tests (host::retire_batch_tests):
    • ordered_handover_preserves_ascending_stamps_across_mixed_and_unsorted_batches (unit coverage for fast-path append, out-of-order single insert, interleaved/unsorted merges, and equal-stamp FIFO stability).
    • mixed_batched_and_direct_retirement_releases_older_resource_at_its_own_deadline_on_gpu (reproduces the exact same-thread N batched resource -> N + 1 direct retire(()) + resource -> handover case across batching off, explicit flush_retired(), and RetiredBatch::drop, asserting refcount stays >= 2 before N + bury_lag(), drops to 1 immediately at bury(N + bury_lag()) while N + 1 remains >= 2, and releases N + 1 at bury(N + 1 + bury_lag()) with 0 D3D12 debug errors on both HW and WARP).
**@⁠Vladimir-Human** on GitHub · 2026-11-27 15:03 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/295#issuecomment-7221578210) @⁠efsebayern @⁠TaynanGT Thanks for the sharp catch and clear repro — updated #295 (rebased onto `main` `b67e8d8`, commit `2f27f38`): - **Stamp-ordered shard handover (`crates/victory-gpu/src/host.rs`):** Routed every `RETIRED` shard insertion (`retire`, direct `retire_resource_with`, `flush_retired`, and `RetiredBatch::drop`) through `push_retired_ordered` / `extend_retired_ordered`. The monotonic fast path still performs a zero-allocation `extend`; when older batched entries arrive behind newer direct entries, `extend_retired_ordered` performs a stable two-pointer merge over only `queue[split..]` so each shard keeps ascending stamp order and `bury()` releases older resources at their own fence deadline. - **Mixed-retirement regression tests (`host::retire_batch_tests`):** - `ordered_handover_preserves_ascending_stamps_across_mixed_and_unsorted_batches` (unit coverage for fast-path append, out-of-order single insert, interleaved/unsorted merges, and equal-stamp FIFO stability). - `mixed_batched_and_direct_retirement_releases_older_resource_at_its_own_deadline_on_gpu` (reproduces the exact same-thread `N` batched resource -> `N + 1` direct `retire(())` + resource -> handover case across batching off, explicit `flush_retired()`, and `RetiredBatch::drop`, asserting refcount stays `>= 2` before `N + bury_lag()`, drops to `1` immediately at `bury(N + bury_lag())` while `N + 1` remains `>= 2`, and releases `N + 1` at `bury(N + 1 + bury_lag())` with 0 D3D12 debug errors on both HW and WARP).
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 15:08 UTC · original

@⁠Vladimir-Human The GPU test now covers the exact case the reviewer found, including handoff at thread exit. 2f27f38 is the revision to use for the next timing run. Keep the same scene and track both retirement counts and frame time so we can see the cost of the ordered handoff.

**@⁠TaynanGT** on GitHub · 2026-11-27 15:08 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/295#issuecomment-7221684179) @⁠Vladimir-Human The GPU test now covers the exact case the reviewer found, including handoff at thread exit. `2f27f38` is the revision to use for the next timing run. Keep the same scene and track both retirement counts and frame time so we can see the cost of the ordered handoff.
github-bot force-pushed github-bot/gh-295 from 2f27f38f56 to 19828f3ce5 2026-11-27 15:20:22 +00:00 Compare
Author
Owner

@⁠Vladimir-Human on GitHub · 2026-11-27 15:12 UTC · original

Quick follow-up @⁠TaynanGT @⁠efsebayern: I noticed your commits f31478f and 045236e landed on the PR branch right before my previous push, so I restored 3e83d9d -> f31478f -> 045236e intact (insert_retired, merge_retired_batch, and a_late_batch_does_not_hide_eligible_retirements untouched) and layered only the D3D12 COM-refcount mixed-retirement GPU regression test (mixed_batched_and_direct_retirement_releases_older_resource_at_its_own_deadline_on_gpu) plus RETIRE_TEST_LOCK on top in 19828f3.

**@⁠Vladimir-Human** on GitHub · 2026-11-27 15:12 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/295#issuecomment-7221775965) Quick follow-up @⁠TaynanGT @⁠efsebayern: I noticed your commits `f31478f` and `045236e` landed on the PR branch right before my previous push, so I restored `3e83d9d -> f31478f -> 045236e` intact (`insert_retired`, `merge_retired_batch`, and `a_late_batch_does_not_hide_eligible_retirements` untouched) and layered only the D3D12 COM-refcount mixed-retirement GPU regression test (`mixed_batched_and_direct_retirement_releases_older_resource_at_its_own_deadline_on_gpu`) plus `RETIRE_TEST_LOCK` on top in `19828f3`.
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 15:18 UTC · original

@⁠Vladimir-Human Thanks for restoring the two commits and adding the COM refcount check on top. 19828f3 is synced locally. The GPU test now exercises the release deadline itself, alongside the queue-local ordering test.

**@⁠TaynanGT** on GitHub · 2026-11-27 15:18 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/295#issuecomment-7221897802) @⁠Vladimir-Human Thanks for restoring the two commits and adding the COM refcount check on top. `19828f3` is synced locally. The GPU test now exercises the release deadline itself, alongside the queue-local ordering test.
github-bot changed target branch from main to gh-pr/295/base 2026-11-27 16:36:30 +00:00
github manually merged commit 19828f3ce5 into gh-pr/295/base 2026-11-27 16:36:32 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.