Skip to content

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

Open
mint17 wants to merge 1 commit into
trustno1-coder:mainfrom
mint17:feat/heap-worker-race-safe
Open

mint17 wants to merge 1 commit into
trustno1-coder:mainfrom
mint17:feat/heap-worker-race-safe

Conversation

@mint17

@mint17 mint17 commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

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.

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.

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants