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

Open
mint17 wants to merge 1 commit from upstream-pr/720 into main
mint17 commented 2026-11-28 04:23:55 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by mint17 on 2026-11-28T04:23:55Z.


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.
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5536624737 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/720). Originally posted by [mint17](https://github.com/mint17) on 2026-11-28T04:23:55Z. --- 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.
Dukestar69 commented 2026-11-28 04:46:32 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Dukestar69 on 2026-11-28T04:46:32Z.

Review: APPROVED


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).

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:review:6095867116 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/720#pullrequestreview-6095867116). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-28T04:46:32Z. **Review:** APPROVED --- 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).
nick0451 commented 2026-11-28 17:24:09 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by nick0451 on 2026-11-28T17:24:09Z.


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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7242988257 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/720#issuecomment-7242988257). Originally posted by [nick0451](https://github.com/nick0451) on 2026-11-28T17:24:09Z. --- 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.
paales commented 2026-11-30 20:51:56 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by paales on 2026-11-30T20:51:56Z.


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, #4206 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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7291578366 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/720#issuecomment-7291578366). Originally posted by [paales](https://github.com/paales) on 2026-11-30T20:51:56Z. --- _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, #4206 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 commented 2026-11-30 22:38:51 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by paales on 2026-11-30T22:38:51Z.


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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7294121500 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/720#issuecomment-7294121500). Originally posted by [paales](https://github.com/paales) on 2026-11-30T22:38:51Z. --- _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 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/720:upstream-pr/720'
git switch 'upstream-pr/720'

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