gpu/heap_worker: add race-safe opt-in VICTORY_HEAP_WORKER #720

Open
github-bot wants to merge 1 commit from github-bot/gh-720 into main AGit
Owner

@⁠mint17 on GitHub · 2026-11-28 04:23 UTC · original
mint17:feat/heap-worker-race-safe → main · +1171 −93 · 8 files · 1 commits · open

Ports the opt-in VICTORY_HEAP_WORKER=1 background texture upload worker (off by default and gated on host::late()) into VIctory-NVIDIA while closing the descriptor-publication TOCTOU race and preserving bake, residency, prelude-upload, and relay guards (zero merge conflict with #693 or #704).

Changes

  • Dedicated victory-heap worker (heap_worker.rs): Opt-in via VICTORY_HEAP_WORKER=1 (off by default), 128 MiB / 80 ms batch flush, fast-core pinning (cores::keep_on_fast_cores), AtomicUsize urgent waiter guard with RAII decrement, Ready.list: Option<HostList> so same-only batches publish cleanly without allocating an empty list, plus split take_ready_for_late() and drain_for_bake(gpu) (settling in-flight GPU work via settle_for_fence() even on list-less batches).
  • Key-pinned TOCTOU-free descriptor publication (descriptor/host.rs): Refactors publish_uploaded_slot around publish_uploaded_slot_with(index, handle, key, ...) which re-verifies record_key(handle) == key under self.made.lock(index) and resolves View strictly from key (view_at) without re-reading handle or calling mirror(). Key-pins flip helpers (texture_slot_flip, texture_slot_show_after, show_later, texture_slot_show_ahead, mirror_after, flip), fixes the latent note_buffer re-queue loop with a RAII PublishingGuard checked in queue_texture(), restores quick_is ordering, and preserves VICTORY_SHOW_AFTER=1, VICTORY_SHOW_NEW_LATER=1, and VICTORY_HELD_LEVELS=1 shows_other mip-upgrade deferral in publish_for_late().
  • Worker & bake safety (resource.rs, draw.rs): Adds resource::hold (reusing existing object::add_ref_alive and resource::Held) to pin live texture objects across worker execution including State::card_accounting; preserves Tells/Told, -> bool bake return, bring_bake_sources, is_aliased, and placed RT/DS/UAV publication on the worker path.
  • VICTORY_JOIN_PARTS, VICTORY_PRELUDE_UPLOADS, & VICTORY_TEXTURE_ONCE compatibility (host.rs, list.rs, texture.rs): Hooks take_ready_for_late() in Host::send_late() via prepend_ahead_order() so joined-part split indices shift cleanly; adds Staging::refresh_ahead() preserving VICTORY_TEXTURE_ONCE=1, VICTORY_TEXTURE_GUARD=0, and VICTORY_PRELUDE_UPLOADS=1.

Verification

  • cargo test --locked -p victory-gpu --lib -- publish_rejects_slot_replaced_between_reads published_slot_stays_out_of_texture_queue publish_for_late_ prepend_ahead_lists_shifts_join_parts urgent_stops_worker_and_ready_drains_cleanly descriptor::host::tests -- --test-threads=1 (53 passed, 0 failed) and --features diagnostics (6 passed, 0 failed):
    • descriptor::host::bake_flip_tests::publish_rejects_slot_replaced_between_reads
    • descriptor::host::bake_flip_tests::published_slot_stays_out_of_texture_queue
    • descriptor::host::bake_flip_tests::publish_for_late_same_after_combines_show_after_and_new_later
    • descriptor::host::bake_flip_tests::publish_for_late_defers_shows_other_mip_upgrade
    • host::submission_tests::prepend_ahead_lists_shifts_join_parts
    • heap_worker::tests::urgent_stops_worker_and_ready_drains_cleanly
  • Zero Clippy warnings across all added/modified files (default + --all-features) and cargo test --locked -p xtask passed (6 passed, 0 failed, including asset policy).
  • Verified with synthetic unit and concurrency tests only (no game files); opt-in flag stays off by default.
**@⁠mint17** on GitHub · 2026-11-28 04:23 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/720) `mint17:feat/heap-worker-race-safe` → `main` · +1171 −93 · 8 files · 1 commits · open Ports the opt-in `VICTORY_HEAP_WORKER=1` background texture upload worker (off by default and gated on `host::late()`) into `VIctory-NVIDIA` while closing the descriptor-publication TOCTOU race and preserving bake, residency, prelude-upload, and relay guards (zero merge conflict with #693 or #704). ### Changes - **Dedicated `victory-heap` worker ([`heap_worker.rs`](crates/victory-gpu/src/heap_worker.rs)):** Opt-in via `VICTORY_HEAP_WORKER=1` (off by default), 128 MiB / 80 ms batch flush, fast-core pinning (`cores::keep_on_fast_cores`), `AtomicUsize` urgent waiter guard with RAII decrement, `Ready.list: Option<HostList>` so `same`-only batches publish cleanly without allocating an empty list, plus split `take_ready_for_late()` and `drain_for_bake(gpu)` (settling in-flight GPU work via `settle_for_fence()` even on list-less batches). - **Key-pinned TOCTOU-free descriptor publication (`descriptor/host.rs`):** Refactors `publish_uploaded_slot` around `publish_uploaded_slot_with(index, handle, key, ...)` which re-verifies `record_key(handle) == key` under `self.made.lock(index)` and resolves `View` strictly from `key` (`view_at`) without re-reading `handle` or calling `mirror()`. Key-pins flip helpers (`texture_slot_flip`, `texture_slot_show_after`, `show_later`, `texture_slot_show_ahead`, `mirror_after`, `flip`), fixes the latent `note_buffer` re-queue loop with a RAII `PublishingGuard` checked in `queue_texture()`, restores `quick_is` ordering, and preserves `VICTORY_SHOW_AFTER=1`, `VICTORY_SHOW_NEW_LATER=1`, and `VICTORY_HELD_LEVELS=1` `shows_other` mip-upgrade deferral in `publish_for_late()`. - **Worker & bake safety (`resource.rs`, `draw.rs`):** Adds `resource::hold` (reusing existing `object::add_ref_alive` and `resource::Held`) to pin live texture objects across worker execution including `State::card_accounting`; preserves `Tells`/`Told`, `-> bool` bake return, `bring_bake_sources`, `is_aliased`, and placed RT/DS/UAV publication on the worker path. - **`VICTORY_JOIN_PARTS`, `VICTORY_PRELUDE_UPLOADS`, & `VICTORY_TEXTURE_ONCE` compatibility (`host.rs`, `list.rs`, `texture.rs`):** Hooks `take_ready_for_late()` in `Host::send_late()` via `prepend_ahead_order()` so joined-part split indices shift cleanly; adds `Staging::refresh_ahead()` preserving `VICTORY_TEXTURE_ONCE=1`, `VICTORY_TEXTURE_GUARD=0`, and `VICTORY_PRELUDE_UPLOADS=1`. ### Verification - `cargo test --locked -p victory-gpu --lib -- publish_rejects_slot_replaced_between_reads published_slot_stays_out_of_texture_queue publish_for_late_ prepend_ahead_lists_shifts_join_parts urgent_stops_worker_and_ready_drains_cleanly descriptor::host::tests -- --test-threads=1` (53 passed, 0 failed) and `--features diagnostics` (6 passed, 0 failed): - `descriptor::host::bake_flip_tests::publish_rejects_slot_replaced_between_reads` - `descriptor::host::bake_flip_tests::published_slot_stays_out_of_texture_queue` - `descriptor::host::bake_flip_tests::publish_for_late_same_after_combines_show_after_and_new_later` - `descriptor::host::bake_flip_tests::publish_for_late_defers_shows_other_mip_upgrade` - `host::submission_tests::prepend_ahead_lists_shifts_join_parts` - `heap_worker::tests::urgent_stops_worker_and_ready_drains_cleanly` - Zero Clippy warnings across all added/modified files (default + `--all-features`) and `cargo test --locked -p xtask` passed (6 passed, 0 failed, including asset policy). - Verified with synthetic unit and concurrency tests only (no game files); opt-in flag stays off by default.
Port opt-in VICTORY_HEAP_WORKER=1 (off by default, gated by host::late())
into VIctory-NVIDIA while closing the descriptor-publication TOCTOU race
and preserving bake, residency, prelude-upload, and relay guards:

- Add crates/victory-gpu/src/heap_worker.rs (128 MiB / 80 ms flush,
  fast-core pinning, AtomicUsize urgent waiter guard with RAII decrement,
  Ready.list: Option<HostList> for same-only passes, take_ready_for_late
  and drain_for_bake with settle_for_fence).
- Add resource::hold (reusing existing object::add_ref_alive and
  resource::Held) to pin live texture objects across worker execution
  including State::card_accounting.
- Key-pin descriptor publication and flip helpers in descriptor/host.rs:
  publish_uploaded_slot_with re-verifies record_key(handle) == key under
  self.made.lock(index) and resolves View strictly from key without
  re-reading handle or calling mirror(); RAII PublishingGuard suppresses
  note_buffer re-queueing; publish_for_late preserves VICTORY_SHOW_AFTER=1,
  VICTORY_SHOW_NEW_LATER=1, and VICTORY_HELD_LEVELS=1 shows_other deferral
  (including list-less same-only batches and placed RT/DS/UAV slots).
- Hook take_ready_for_late() in Host::send_late() with prepend_ahead_order()
  to shift VICTORY_JOIN_PARTS boundaries, and add Staging::refresh_ahead()
  preserving VICTORY_TEXTURE_ONCE=1, VICTORY_TEXTURE_GUARD=0, and
  VICTORY_PRELUDE_UPLOADS=1.
- Add deterministic TOCTOU regression test
  publish_rejects_slot_replaced_between_reads plus unit tests for queue
  retention, show-after/mip-upgrade deferral, join-parts shifting, and
  urgent waiter handoff.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 04:25:09 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-28 04:46 UTC · original · approved these changes

LGTM — verified on Windows (RTX 5090):

  • cargo test -p victory-gpu --lib heap_worker:: (1/1) and descriptor:: (66/66, including all 4 new bake_flip_tests::) passed on RTX 5090
  • 0 new compiler warnings in touched files
  • cargo xtask policy passed (295 files ok)

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-28 04:46 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/720#pullrequestreview-6095867116) · **approved these changes** LGTM — verified on Windows (RTX 5090): - `cargo test -p victory-gpu --lib heap_worker::` (1/1) and `descriptor::` (66/66, including all 4 new `bake_flip_tests::`) passed on RTX 5090 - 0 new compiler warnings in touched files - `cargo xtask policy` passed (`295 files ok`) Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠nick0451 on GitHub · 2026-11-28 17:24 UTC · original

A smoke test only: the scene barely reaches this path.

RTX 3090 24 GB (driver 610.88), i9-14900K, 32 GB, Windows 11. A release build of this PR merged onto main at 63b902f. Series S, DLSS Performance, frame generation off. Every run used the same frozen save, outdoors on a balcony by the water (about 5,900 draws a frame), standing still. One warm-up start per build, then main and four PR builds were run in turn, three rounds each. Each run is 40 s to settle, 60 s measured, then a screenshot. An A/A run of main against itself in this setup differs by 2-3%.

  • With VICTORY_HEAP_WORKER=1: the worker's line reads 0.0 ms at work a frame, 0-0.2 lists. Standing still, almost nothing new streams in, so the worker is idle.
  • Speed: 24.3 fps median against 24.6 for main (runs 21.1, 24.6, 24.3; the first round was slow for every new build). Preparing is 27.9 against 25.0 ms, within noise for one session but worth watching in a streaming-heavy test.
  • Picture: normal in all end-of-run screenshots. No crash, device loss or ERROR line.
  • cargo test -p victory-gpu --lib: 459 passed (the 5 new tests included).

A real test needs movement or a scene change that streams textures; I'll add one when I can drive one repeatably.

Tested with an AI assistant (Claude Code); results are from this PC.

**@⁠nick0451** on GitHub · 2026-11-28 17:24 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/720#issuecomment-7242988257) A smoke test only: the scene barely reaches this path. RTX 3090 24 GB (driver 610.88), i9-14900K, 32 GB, Windows 11. A release build of this PR merged onto main at `63b902f`. Series S, DLSS Performance, frame generation off. Every run used the same frozen save, outdoors on a balcony by the water (about 5,900 draws a frame), standing still. One warm-up start per build, then main and four PR builds were run in turn, three rounds each. Each run is 40 s to settle, 60 s measured, then a screenshot. An A/A run of main against itself in this setup differs by 2-3%. - **With `VICTORY_HEAP_WORKER=1`:** the worker's line reads `0.0 ms at work` a frame, 0-0.2 lists. Standing still, almost nothing new streams in, so the worker is idle. - **Speed:** 24.3 fps median against 24.6 for main (runs 21.1, 24.6, 24.3; the first round was slow for every new build). Preparing is 27.9 against 25.0 ms, within noise for one session but worth watching in a streaming-heavy test. - **Picture:** normal in all end-of-run screenshots. No crash, device loss or `ERROR` line. - `cargo test -p victory-gpu --lib`: 459 passed (the 5 new tests included). A real test needs movement or a scene change that streams textures; I'll add one when I can drive one repeatably. Tested with an AI assistant (Claude Code); results are from this PC.
Author
Owner

@⁠paales on GitHub · 2026-11-30 20:51 UTC · original

Written by 🤖:

In-game numbers for this PR. PC: RTX 5070 Ti 16 GB, Ryzen 9 5900X, 32 GB, Windows 11. Series X, 1440p, DLSS Performance, frame generation off, 30 fps cap. Each run: a new game (settings save, no story save), one A press, the first cutscene for 60 s from its first frame. On this PC the same build's submitting thread took 24 ms a frame in one run and 35 ms in another half an hour later, so only large differences count.

I merged it by hand onto main 84bd61e2 with #3918, #1727, #3553, #3043, #4207 and #4138: branch heap-worker on paales/VIctory-NVIDIA. There were 18 conflict blocks in descriptor/host.rs, draw.rs, texture.rs, host.rs and lib.rs. main reads the slot's record once too now. The merge keeps:

  • main's queue-mark check, is_texture and the resource[0] != 1 guard, with held taken from this PR's record;
  • main's guard query (writes_since), with this PR's stale_ahead added to the turn check;
  • queue_texture_inner(…, None) where this PR re-queues (queue_texture is test-only in main now);
  • seed_pending (new in main) on the worker's list when the target is Ahead.

cargo test -p victory-gpu --lib -- descriptor::host heap_worker prepend_ahead draw:: texture::: 158 passed, 0 failed.

The same build, back to back on an idle PC:

VICTORY_HEAP_WORKER=0 VICTORY_HEAP_WORKER=1
seconds with a frame over 40 ms 35 33
seconds with a frame over 50 ms 23 16
seconds with a frame over 100 ms 2 1
textures picked by index, frames late on average (most) 3.60 (64) 0.44 (11)
the texture pass on the recording thread, per 5 s 1,142 ms 552 ms

The worker line said 5 to 6 ms a frame of its own work, about 55 slots shown a frame, none left to the submission. In the first 5 s of the scene, textures were 0.4 to 5.9 frames late with the worker, against 10 to 16 without it. Two runs on a busier PC were slower with the worker (the submitting thread 32 and 41 ms a frame), so a busy CPU can take the gain away. No crash, and nothing wrong in captures every 250 ms. I play with it on.

**@⁠paales** on GitHub · 2026-11-30 20:51 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/720#issuecomment-7291578366) _Written by 🤖:_ In-game numbers for this PR. PC: RTX 5070 Ti 16 GB, Ryzen 9 5900X, 32 GB, Windows 11. Series X, 1440p, DLSS Performance, frame generation off, 30 fps cap. Each run: a new game (settings save, no story save), one A press, the first cutscene for 60 s from its first frame. On this PC the same build's submitting thread took 24 ms a frame in one run and 35 ms in another half an hour later, so only large differences count. I merged it by hand onto `main` 84bd61e2 with #3918, #1727, #3553, #3043, #4207 and #4138: branch `heap-worker` on paales/VIctory-NVIDIA. There were 18 conflict blocks in `descriptor/host.rs`, `draw.rs`, `texture.rs`, `host.rs` and `lib.rs`. `main` reads the slot's record once too now. The merge keeps: - main's queue-mark check, `is_texture` and the `resource[0] != 1` guard, with `held` taken from this PR's `record`; - main's guard query (`writes_since`), with this PR's `stale_ahead` added to the turn check; - `queue_texture_inner(…, None)` where this PR re-queues (`queue_texture` is test-only in `main` now); - `seed_pending` (new in `main`) on the worker's list when the target is `Ahead`. `cargo test -p victory-gpu --lib -- descriptor::host heap_worker prepend_ahead draw:: texture::`: 158 passed, 0 failed. The same build, back to back on an idle PC: | | `VICTORY_HEAP_WORKER=0` | `VICTORY_HEAP_WORKER=1` | | --- | --- | --- | | seconds with a frame over 40 ms | 35 | 33 | | seconds with a frame over 50 ms | 23 | 16 | | seconds with a frame over 100 ms | 2 | 1 | | textures picked by index, frames late on average (most) | 3.60 (64) | 0.44 (11) | | the texture pass on the recording thread, per 5 s | 1,142 ms | 552 ms | The worker line said 5 to 6 ms a frame of its own work, about 55 slots shown a frame, none left to the submission. In the first 5 s of the scene, textures were 0.4 to 5.9 frames late with the worker, against 10 to 16 without it. Two runs on a busier PC were slower with the worker (the submitting thread 32 and 41 ms a frame), so a busy CPU can take the gain away. No crash, and nothing wrong in captures every 250 ms. I play with it on.
Author
Owner

@⁠paales on GitHub · 2026-11-30 22:38 UTC · original

Written by 🤖:

A follow-up with this PR in my build: with the pass on the worker thread, a larger pass budget fixes the missing textures at cuts.

Same PC (RTX 5070 Ti 16 GB, Ryzen 9 5900X), Series X, 1440p, DLSS Performance, frame generation off, 30 fps cap. Scene: a story save that loads into a mission cutscene in the city; A held at 35 s skips the interview into a montage of quick cuts (an airfield, a beach, a yard, a whale statue, a bridge). Play sets VICTORY_TEXSPK_PASS_MS=8, doubled to 16 on this card while it has room.

VICTORY_TEXSPK_PASS_MS=8 VICTORY_TEXSPK_PASS_MS=24
textures picked by index in the montage, frames late on average 11.0 to 23.4 0.6 to 3.2
the same, most 158 25
montage, frames per second 25.1 26.7
seconds with a frame over 100 ms (82 s) 18 14
the longest frame 2,295 ms 266 ms

The run with 24 ms also had VICTORY_WAIT_SPIN=300, VICTORY_WIDE_SPIN=100 and VICTORY_WIDE_THREADS=12; those touch the refreshes, not the texture pass. With 8 ms the worker worked 9 to 14 ms a frame and 181 of 228 passes stopped at their time, while up to 18,104 slots were due. On screen with 8 ms: the beach people without skin textures, a yard white and gray, and the whale statue gray for 1.6 s after its cut. With 24 ms all of them are textured at their cuts.

Before this PR, 24 ms on the recording thread made the frames slower (median 53 against 39 ms). With the worker, a default of about 24 ms seems right when VICTORY_HEAP_WORKER=1.

**@⁠paales** on GitHub · 2026-11-30 22:38 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/720#issuecomment-7294121500) _Written by 🤖:_ A follow-up with this PR in my build: with the pass on the worker thread, a larger pass budget fixes the missing textures at cuts. Same PC (RTX 5070 Ti 16 GB, Ryzen 9 5900X), Series X, 1440p, DLSS Performance, frame generation off, 30 fps cap. Scene: a story save that loads into a mission cutscene in the city; A held at 35 s skips the interview into a montage of quick cuts (an airfield, a beach, a yard, a whale statue, a bridge). Play sets `VICTORY_TEXSPK_PASS_MS=8`, doubled to 16 on this card while it has room. | | `VICTORY_TEXSPK_PASS_MS=8` | `VICTORY_TEXSPK_PASS_MS=24` | | --- | --- | --- | | textures picked by index in the montage, frames late on average | 11.0 to 23.4 | 0.6 to 3.2 | | the same, most | 158 | 25 | | montage, frames per second | 25.1 | 26.7 | | seconds with a frame over 100 ms (82 s) | 18 | 14 | | the longest frame | 2,295 ms | 266 ms | The run with 24 ms also had `VICTORY_WAIT_SPIN=300`, `VICTORY_WIDE_SPIN=100` and `VICTORY_WIDE_THREADS=12`; those touch the refreshes, not the texture pass. With 8 ms the worker worked 9 to 14 ms a frame and 181 of 228 passes stopped at their time, while up to 18,104 slots were due. On screen with 8 ms: the beach people without skin textures, a yard white and gray, and the whale statue gray for 1.6 s after its cut. With 24 ms all of them are textured at their cuts. Before this PR, 24 ms on the recording thread made the frames slower (median 53 against 39 ms). With the worker, a default of about 24 ms seems right when `VICTORY_HEAP_WORKER=1`.
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/descriptor/host.rs
  • crates/victory-gpu/src/draw.rs
  • crates/victory-gpu/src/host.rs
  • crates/victory-gpu/src/lib.rs
  • crates/victory-gpu/src/texture.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/720/head:github-bot/gh-720'
git switch 'github-bot/gh-720'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-720'
git switch 'github-bot/gh-720'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-720'
git switch 'github-bot/gh-720'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-720'
git switch 'main'
git merge --squash 'github-bot/gh-720'
git switch 'main'
git merge --ff-only 'github-bot/gh-720'
git switch 'main'
git merge 'github-bot/gh-720'
git push origin 'main'
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/trustno1-coder--VIctory-NVIDIA!720
No description provided.