perf(gpu): add opt-in marked heap-slot refresh pass and async texture worker #3439

Open
legayclightning22 wants to merge 1 commit from upstream-pr/3439 into main
legayclightning22 commented 2026-11-27 02:20:20 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by legayclightning22 on 2026-11-27T02:20:20Z.


Summary

Adds opt-in dirty/marked descriptor-heap slot tracking (VICTORY_MARKED_HEAP_PASS) to avoid full heap rescans across passes, alongside an opt-in async texture upload worker and direct constant upload path.

Changes

  • Track dirty/marked descriptor heap slots so only modified view slots are refreshed rather than rescanning the full heap every 16 passes.
  • Offload indexed-texture upload preparation to a background worker thread with clean drain/shutdown and route direct constants cleanly.
  • Add unit tests covering marked-slot tracking and async worker queue drain/shutdown.

Verification

  • cargo test --locked -p victory-gpu --lib
  • cargo clippy --locked -p victory-gpu --all-targets --all-features -- -D warnings
  • cargo xtask policy
<!-- greyfinch-sync:Marvisak/VIctory:issue:5520066163 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3439). Originally posted by [legayclightning22](https://github.com/legayclightning22) on 2026-11-27T02:20:20Z. --- ## Summary Adds opt-in dirty/marked descriptor-heap slot tracking (`VICTORY_MARKED_HEAP_PASS`) to avoid full heap rescans across passes, alongside an opt-in async texture upload worker and direct constant upload path. ## Changes - Track dirty/marked descriptor heap slots so only modified view slots are refreshed rather than rescanning the full heap every 16 passes. - Offload indexed-texture upload preparation to a background worker thread with clean drain/shutdown and route direct constants cleanly. - Add unit tests covering marked-slot tracking and async worker queue drain/shutdown. ## Verification - `cargo test --locked -p victory-gpu --lib` - `cargo clippy --locked -p victory-gpu --all-targets --all-features -- -D warnings` - `cargo xtask policy`
Dukestar69 commented 2026-11-27 02:29:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-27T02:29:17Z.

Review: APPROVED


LGTM — verified on Windows (RTX 5090):

  • cargo test -p victory-gpu --lib passed (233/233, including heap_pass::tests:: and texture_worker::tests::, skipping the 2 pre-existing origin/main RTX 5090 failures)
  • cargo clippy -p victory-gpu --all-targets -- -D warnings passed with 0 warnings
  • cargo xtask policy passed (193 files ok)

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

<!-- greyfinch-sync:Marvisak/VIctory:review:6082701140 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3439#pullrequestreview-6082701140). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-27T02:29:17Z. **Review:** APPROVED --- LGTM — verified on Windows (RTX 5090): - `cargo test -p victory-gpu --lib` passed (233/233, including `heap_pass::tests::` and `texture_worker::tests::`, skipping the 2 pre-existing `origin/main` RTX 5090 failures) - `cargo clippy -p victory-gpu --all-targets -- -D warnings` passed with 0 warnings - `cargo xtask policy` passed (`193 files ok`) Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
fuzeofficlal commented 2026-11-27 02:40:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-27T02:40:17Z.

Review: CHANGES_REQUESTED


Default behavior (VICTORY_MARKED_HEAP_PASS, VICTORY_TEXTURE_WORKER, and VICTORY_DIRECT_CONSTANTS all off) builds and passes cleanly, but all three opt-in features currently fail correctness checks (two break the existing bindless::tests::heap_views_are_current test immediately on discrete D3D12 hardware):

Blocking Issues

  1. VICTORY_MARKED_HEAP_PASS=1 freezes all CPU writes to already-bound bindless buffers and textures for 255 consecutive submissions (crates/victory-gpu/src/heap_pass.rs:327-357, crates/victory-gpu/src/bindless.rs:248-265, 356-380)

    • Reproduced live on discrete D3D12 by running the existing bindless::tests::heap_views_are_current with VICTORY_MARKED_HEAP_PASS=1: fails immediately on Round 1 (assertion left == right failed: round 1, dword 0: left: 1064, right: 5016) because the GPU still reads Round 0's stale bytes!
    • Marked::note(slot) is only called when a descriptor slot is rewritten (Viewed::set / forget), and Marked::plan clears self.dirty on every pass. On the next 255 submissions where the title updates buffer/texture memory without rewriting descriptor slots, plan() returns empty and skips both !crate::watch::clean_bit(*bit) and texture refresh checks completely.
    • In addition, bindless.rs:251-263 and 359-364 build full mapped and all Vecs of every slot/hull on every pass and perform an O(|dirty| * |slots|) .iter().find(...) lookup inside plan().
    • Fix: Descriptor-slot dirty tracking cannot replace memory-dirty tracking (watch::clean_bit / texture watch pages). Any heap-pass optimization must still refresh every live copy whose underlying title memory changed since the last submission.
  2. VICTORY_TEXTURE_WORKER=1 renders with unuploaded textures and races texture::recycle() against in-flight staging chunks (crates/victory-gpu/src/texture_worker.rs:535-583, crates/victory-gpu/src/bindless.rs:213-305)

    • Reproduced live on discrete D3D12 by running the existing bindless::tests::heap_views_are_current with VICTORY_TEXTURE_WORKER=1: fails immediately on Round 0 (assertion left == right failed: round 0, dword 0: left: 1000, right: 1064), because Submission 0's draw executes before the background worker uploads its texture (sampling 0 instead of 64).
    • Staging overwrite race: bindless::uploads calls take_ready() (retrieving ready command lists that have not yet been submitted to the GPU), then immediately calls crate::texture::recycle(), resetting CHUNKS to (current=0, used=0) and returning BUFFERS.used to BUFFERS.free, and then calls texture_worker::submit(work) — allowing the worker thread to carve CHUNKS.all[0] + 0 and overwrite the exact staging memory the GPU is about to copy from!
    • Resource churn & lifecycle: prepare() creates a brand-new HostList (new ID3D12CommandAllocator) for every job even when zero textures changed (dropped immediately after execution instead of recycled), Uploads::lists() chains an extra empty self.held.0 list, and take_ready() removes keys from PENDING and drops next.work before the GPU finishes executing next.list.
    • Fix: Do not recycle staging chunks/buffers still referenced by pending or unexecuted worker command lists, synchronize or fence uploads required by the current submission (or keep dedicated per-job staging + recycled command lists), and keep PENDING entries until submission/completion.
  3. VICTORY_DIRECT_CONSTANTS=1 writes a CBV descriptor into an SRV descriptor table range and drops heap retention (crates/victory-gpu/src/draw.rs:271-292, 1375-1383, crates/victory-gpu/src/descriptor/host.rs:419-432)

    • Translated shaders read constant buffers in descriptor tables as raw ByteAddressBuffer SRV descriptors (D3D12_DESCRIPTOR_RANGE_TYPE_SRV, see descriptor/host.rs:1471-1492 and victory-shader/src/emit/lower.rs:9495).
    • HostHeap::constant_at instead writes a CBV (CreateConstantBufferView) into that SRV slot (invalid under D3D12 specification / Debug Layer / GBV), calls self.keep(index, None) without keeping shadow.buffer alive in the heap slot, and continues past readable(...).
    • Fix: Keep the descriptor type aligned with the shader's declared SRV range (a raw R32_TYPELESS buffer SRV, just as make() builds) and retain the backing ID3D12Resource via self.keep(...).

Minor Note

  • Remove the four #[allow(clippy::undocumented_unsafe_blocks)] attributes (descriptor/host.rs:418, texture_worker.rs:449, 457, 534) and add explicit // SAFETY: comments matching the rest of the crate.
<!-- greyfinch-sync:Marvisak/VIctory:review:6082778945 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3439#pullrequestreview-6082778945). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-27T02:40:17Z. **Review:** CHANGES_REQUESTED --- Default behavior (`VICTORY_MARKED_HEAP_PASS`, `VICTORY_TEXTURE_WORKER`, and `VICTORY_DIRECT_CONSTANTS` all off) builds and passes cleanly, but all three opt-in features currently fail correctness checks (two break the existing `bindless::tests::heap_views_are_current` test immediately on discrete D3D12 hardware): ### Blocking Issues 1. **`VICTORY_MARKED_HEAP_PASS=1` freezes all CPU writes to already-bound bindless buffers and textures for 255 consecutive submissions (`crates/victory-gpu/src/heap_pass.rs:327-357`, `crates/victory-gpu/src/bindless.rs:248-265, 356-380`)** - **Reproduced live on discrete D3D12** by running the existing `bindless::tests::heap_views_are_current` with `VICTORY_MARKED_HEAP_PASS=1`: fails immediately on Round 1 (`assertion left == right failed: round 1, dword 0: left: 1064, right: 5016`) because the GPU still reads Round 0's stale bytes! - `Marked::note(slot)` is only called when a descriptor slot is rewritten (`Viewed::set` / `forget`), and `Marked::plan` clears `self.dirty` on every pass. On the next 255 submissions where the title updates buffer/texture memory without rewriting descriptor slots, `plan()` returns empty and skips both `!crate::watch::clean_bit(*bit)` and texture refresh checks completely. - In addition, `bindless.rs:251-263` and `359-364` build full `mapped` and `all` `Vec`s of every slot/hull on every pass and perform an `O(|dirty| * |slots|)` `.iter().find(...)` lookup inside `plan()`. - **Fix**: Descriptor-slot dirty tracking cannot replace memory-dirty tracking (`watch::clean_bit` / texture watch pages). Any heap-pass optimization must still refresh every live copy whose underlying title memory changed since the last submission. 2. **`VICTORY_TEXTURE_WORKER=1` renders with unuploaded textures and races `texture::recycle()` against in-flight staging chunks (`crates/victory-gpu/src/texture_worker.rs:535-583`, `crates/victory-gpu/src/bindless.rs:213-305`)** - **Reproduced live on discrete D3D12** by running the existing `bindless::tests::heap_views_are_current` with `VICTORY_TEXTURE_WORKER=1`: fails immediately on Round 0 (`assertion left == right failed: round 0, dword 0: left: 1000, right: 1064`), because Submission 0's draw executes before the background worker uploads its texture (sampling `0` instead of `64`). - **Staging overwrite race**: `bindless::uploads` calls `take_ready()` (retrieving `ready` command lists that have **not yet been submitted to the GPU**), then immediately calls `crate::texture::recycle()`, resetting `CHUNKS` to `(current=0, used=0)` and returning `BUFFERS.used` to `BUFFERS.free`, and then calls `texture_worker::submit(work)` — allowing the worker thread to carve `CHUNKS.all[0] + 0` and overwrite the exact staging memory the GPU is about to copy from! - **Resource churn & lifecycle**: `prepare()` creates a brand-new `HostList` (new `ID3D12CommandAllocator`) for every job even when zero textures changed (dropped immediately after execution instead of recycled), `Uploads::lists()` chains an extra empty `self.held.0` list, and `take_ready()` removes keys from `PENDING` and drops `next.work` before the GPU finishes executing `next.list`. - **Fix**: Do not recycle staging chunks/buffers still referenced by pending or unexecuted worker command lists, synchronize or fence uploads required by the current submission (or keep dedicated per-job staging + recycled command lists), and keep `PENDING` entries until submission/completion. 3. **`VICTORY_DIRECT_CONSTANTS=1` writes a CBV descriptor into an SRV descriptor table range and drops heap retention (`crates/victory-gpu/src/draw.rs:271-292, 1375-1383`, `crates/victory-gpu/src/descriptor/host.rs:419-432`)** - Translated shaders read constant buffers in descriptor tables as raw `ByteAddressBuffer` **SRV** descriptors (`D3D12_DESCRIPTOR_RANGE_TYPE_SRV`, see `descriptor/host.rs:1471-1492` and `victory-shader/src/emit/lower.rs:9495`). - `HostHeap::constant_at` instead writes a **CBV** (`CreateConstantBufferView`) into that SRV slot (invalid under D3D12 specification / Debug Layer / GBV), calls `self.keep(index, None)` without keeping `shadow.buffer` alive in the heap slot, and `continue`s past `readable(...)`. - **Fix**: Keep the descriptor type aligned with the shader's declared `SRV` range (a raw `R32_TYPELESS` buffer SRV, just as `make()` builds) and retain the backing `ID3D12Resource` via `self.keep(...)`. ### Minor Note - Remove the four `#[allow(clippy::undocumented_unsafe_blocks)]` attributes (`descriptor/host.rs:418`, `texture_worker.rs:449, 457, 534`) and add explicit `// SAFETY:` comments matching the rest of the crate.
legayclightning22 commented 2026-11-28 04:41:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by legayclightning22 on 2026-11-28T04:41:27Z.


Thanks @fuzeofficlal — pushed an updated commit normalizing the raw buffer SRV GPU-VA word in descriptor::host::tests::direct_constants_are_raw_srvs_and_retain_their_buffer (crates/victory-gpu/src/descriptor/host.rs) so both slots compare at matching buffer offsets while verifying raw SRV layout and backing buffer retention.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7234197714 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3439#issuecomment-7234197714). Originally posted by [legayclightning22](https://github.com/legayclightning22) on 2026-11-28T04:41:27Z. --- Thanks @fuzeofficlal — pushed an updated commit normalizing the raw buffer SRV GPU-VA word in `descriptor::host::tests::direct_constants_are_raw_srvs_and_retain_their_buffer` (`crates/victory-gpu/src/descriptor/host.rs`) so both slots compare at matching buffer offsets while verifying raw SRV layout and backing buffer retention.
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/3439:upstream-pr/3439'
git switch 'upstream-pr/3439'

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