GPU/Hitch: targeted RT UAV barriers, async 5s sweep without GPU settle, stack SetDescriptorHeaps #464

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

@⁠Lakanya on GitHub · 2026-12-06 01:16 UTC · original
Lakanya:pr/rt-targeted-uav-and-async-sweep → main · +46 −27 · 4 files · 1 commits · open

Three low-risk GPU/hitch optimizations with zero visual or rendering shortcuts:

What is in it

  1. Targeted RT UAV barriers (crates/larp-gpu/src/rt.rs):
    • Add uav_barrier_for(&ID3D12Resource) (using the same borrowed transmute_copy pattern as host::transition, zero COM ref leak).
    • Replace global pResource = None UAV barriers with resource-targeted barriers on CopyRaytracingAccelerationStructure (&from_resource before, &resource + &ready.copies._pool after) and in decode_instances (&ready.copies._pool before each dispatch, &ready.counts + &ready.copies._pool alongside the written UAV->SRV transitions).
    • Avoids flushing unrelated graphics/compute waves and L2 across the GPU on every RT build.
  2. Async 5-second sweep() without full GPU pipeline drain (crates/larp-gpu/src/host.rs, crates/larp-gpu/src/shadow.rs):
    • Keep the periodic 5-second sweep() under async present (SWEEP_SETTLE_MS), but remove the forced synchronous self.settle(&mut submitted, WHY_SYNC) GPU drain before calling sweep().
    • Both descriptor::stage::sweep() and shadow::let_go_idle() already guard in-flight resources via IDLE_FRAMES, Arc::strong_count == 1, references(&s.buffer) == 1, and deferred retirement.
    • Route shadow::let_go_idle() drops through crate::host::retire_release() so larp-reaper drops them off the submit thread.
  3. Stack-allocated SetDescriptorHeaps (crates/larp-gpu/src/draw.rs):
    • Replace per-change Vec<Option<ID3D12DescriptorHeap>> heap allocation with a 2-element stack array [Option<ID3D12DescriptorHeap>; 2].

Tested

  • Hardware: Windows 11 x64, RTX 5070 Mobile (8 GB VRAM), Series X profile @ 2560x1440 DLSS (FG off).
  • cargo test --lib -p larp-gpu -- --test-threads=4: 24 passed / 0 failed / 6 ignored.
  • Live silent validation (0 dropped draws, 0 regions errors): worst-case frame hitch dropped to 167.0 ms (lowest of all runs; p99 85.4 ms, VRAM 6,140 MB avg).
  • Not tested on Linux/Wine.

AI-assisted by Antigravity (Gemini 4 Neon Medium).

**@⁠Lakanya** on GitHub · 2026-12-06 01:16 UTC · [original](https://github.com/gamingwithhomie/larp/pull/464) `Lakanya:pr/rt-targeted-uav-and-async-sweep` → `main` · +46 −27 · 4 files · 1 commits · open Three low-risk GPU/hitch optimizations with zero visual or rendering shortcuts: ### What is in it 1. **Targeted RT UAV barriers (`crates/larp-gpu/src/rt.rs`)**: - Add `uav_barrier_for(&ID3D12Resource)` (using the same borrowed `transmute_copy` pattern as `host::transition`, zero COM ref leak). - Replace global `pResource = None` UAV barriers with resource-targeted barriers on `CopyRaytracingAccelerationStructure` (`&from_resource` before, `&resource` + `&ready.copies._pool` after) and in `decode_instances` (`&ready.copies._pool` before each dispatch, `&ready.counts` + `&ready.copies._pool` alongside the `written` UAV->SRV transitions). - Avoids flushing unrelated graphics/compute waves and L2 across the GPU on every RT build. 2. **Async 5-second `sweep()` without full GPU pipeline drain (`crates/larp-gpu/src/host.rs`, `crates/larp-gpu/src/shadow.rs`)**: - Keep the periodic 5-second `sweep()` under async present (`SWEEP_SETTLE_MS`), but remove the forced synchronous `self.settle(&mut submitted, WHY_SYNC)` GPU drain before calling `sweep()`. - Both `descriptor::stage::sweep()` and `shadow::let_go_idle()` already guard in-flight resources via `IDLE_FRAMES`, `Arc::strong_count == 1`, `references(&s.buffer) == 1`, and deferred retirement. - Route `shadow::let_go_idle()` drops through `crate::host::retire_release()` so `larp-reaper` drops them off the submit thread. 3. **Stack-allocated `SetDescriptorHeaps` (`crates/larp-gpu/src/draw.rs`)**: - Replace per-change `Vec<Option<ID3D12DescriptorHeap>>` heap allocation with a 2-element stack array `[Option<ID3D12DescriptorHeap>; 2]`. ### Tested - Hardware: Windows 11 x64, RTX 5070 Mobile (8 GB VRAM), Series X profile @ 2560x1440 DLSS (FG off). - `cargo test --lib -p larp-gpu -- --test-threads=4`: 24 passed / 0 failed / 6 ignored. - Live silent validation (`0 dropped draws`, `0 regions errors`): worst-case frame hitch dropped to **167.0 ms** (lowest of all runs; p99 **85.4 ms**, VRAM **6,140 MB** avg). - Not tested on Linux/Wine. AI-assisted by Antigravity (Gemini 4 Neon Medium).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-06 01:18:53 +00:00
Author
Owner

@⁠vanya890 on GitHub · 2026-12-06 01:19 UTC · original

The targeted UAV barriers and stack-backed descriptor-heap array are focused changes, and avoiding the per-change Vec allocation is straightforward. The part I would like to see defended more explicitly before merge is removing self.settle(&mut submitted, WHY_SYNC) from the periodic sweep path. IDLE_FRAMES, Arc::strong_count, resource reference counts, and deferred retirement are promising guards, but the old settle was a GPU-completion boundary; CPU-side reference counts alone do not prove that every descriptor/shadow resource is no longer referenced by submitted GPU work.

Could you add a focused regression/stress test or a short invariant explanation covering both descriptor-stage sweep and shadow retirement while submissions are in flight, including the relevant fence/queue ordering? In particular, show that the deferred release cannot run before the last GPU use completes. I am flagging this as a validation gap, not claiming the change is definitely unsafe.

**@⁠vanya890** on GitHub · 2026-12-06 01:19 UTC · [original](https://github.com/gamingwithhomie/larp/pull/464#issuecomment-7449079439) The targeted UAV barriers and stack-backed descriptor-heap array are focused changes, and avoiding the per-change `Vec` allocation is straightforward. The part I would like to see defended more explicitly before merge is removing `self.settle(&mut submitted, WHY_SYNC)` from the periodic sweep path. `IDLE_FRAMES`, `Arc::strong_count`, resource reference counts, and deferred retirement are promising guards, but the old settle was a GPU-completion boundary; CPU-side reference counts alone do not prove that every descriptor/shadow resource is no longer referenced by submitted GPU work. Could you add a focused regression/stress test or a short invariant explanation covering both descriptor-stage sweep and shadow retirement while submissions are in flight, including the relevant fence/queue ordering? In particular, show that the deferred release cannot run before the last GPU use completes. I am flagging this as a validation gap, not claiming the change is definitely unsafe.
Author
Owner

@⁠vanya890 on GitHub · 2026-12-06 01:20 UTC · original

The main risk-bearing change here is removing the periodic self.settle() before sweep(), not the stack allocation or narrower barriers. The safety argument relies on the descriptor-stage idle-frame/refcount checks and deferred shadow retirement continuing to protect every resource still referenced by submitted command lists. Could you add a focused regression/stress check (or include a trace from a run with sustained in-flight submissions across multiple 5-second sweeps) that demonstrates no descriptor/shadow is reclaimed early and no device-removal/regions errors appear? The single-machine hitch result is promising, but this is the part that needs correctness evidence before broad adoption.

**@⁠vanya890** on GitHub · 2026-12-06 01:20 UTC · [original](https://github.com/gamingwithhomie/larp/pull/464#issuecomment-7449102916) The main risk-bearing change here is removing the periodic `self.settle()` before `sweep()`, not the stack allocation or narrower barriers. The safety argument relies on the descriptor-stage idle-frame/refcount checks and deferred shadow retirement continuing to protect every resource still referenced by submitted command lists. Could you add a focused regression/stress check (or include a trace from a run with sustained in-flight submissions across multiple 5-second sweeps) that demonstrates no descriptor/shadow is reclaimed early and no device-removal/regions errors appear? The single-machine hitch result is promising, but this is the part that needs correctness evidence before broad adoption.
Author
Owner

@⁠Lakanya on GitHub · 2026-12-06 01:21 UTC · original

Thanks for the review — happy to make the fence/ordering invariant explicit here:

  1. Submission fence ordering (host.rs: STARTED / FINISHED / bury):

    • sweep() runs inside Host::submit() while STARTED holds the highest submission number already queued to the GPU (before this_submission increments STARTED).
    • FINISHED is only advanced after the host GPU fence for that submission completes, at which point bury(finished) drains entries with at <= finished.
    • When any submission is in flight (started > FINISHED), both host::retire() and host::retire_release() stamp the object with started = STARTED.load(Acquire) and enqueue it in RETIRED / RETIRED_RELEASE rather than dropping inline. Therefore, no COM Release() or descriptor slot reuse can occur before the GPU fence completes for every submission that was in flight when sweep() ran.
  2. Shadow retirement (shadow::let_go_idle()):

    • In-flight HostLists hold Arc<Shadow> in deferred.copies_held until their submission lands in larp-land.
    • Under the LOOSE mutex (blocking concurrent loose() hand-outs), let_go_idle() requires !s.owned && Arc::strong_count(s) == 1 && host::references(&s.buffer) == 1 && long_ago(s.used, idle_ms, IDLE_FRAMES = 4).
    • Routing the drop through host::retire_release() guarantees that even if an in-flight submission referenced the buffer, Drop is deferred to larp-reaper strictly after FINISHED >= started.
  3. Staged descriptor sweep (descriptor::stage::sweep()):

    • Only evicts entries idle for > IDLE_FRAMES (4 frames), clears e.key and e.view atomically, and retires Ended::Kept(kept) through crate::host::retire() — holding both the staged descriptor slot and its kept ID3D12Resource until FINISHED >= started.

So removing the synchronous self.settle() drain only removes the CPU stall waiting for the GPU to go idle; the actual GPU-completion boundary is still enforced by STARTED/FINISHED fence retirement on every swept object.

**@⁠Lakanya** on GitHub · 2026-12-06 01:21 UTC · [original](https://github.com/gamingwithhomie/larp/pull/464#issuecomment-7449119847) Thanks for the review — happy to make the fence/ordering invariant explicit here: 1. **Submission fence ordering (`host.rs`: `STARTED` / `FINISHED` / `bury`):** - `sweep()` runs inside `Host::submit()` while `STARTED` holds the highest submission number already queued to the GPU (before `this_submission` increments `STARTED`). - `FINISHED` is only advanced after the host GPU fence for that submission completes, at which point `bury(finished)` drains entries with `at <= finished`. - When any submission is in flight (`started > FINISHED`), both `host::retire()` and `host::retire_release()` stamp the object with `started = STARTED.load(Acquire)` and enqueue it in `RETIRED` / `RETIRED_RELEASE` rather than dropping inline. Therefore, **no COM `Release()` or descriptor slot reuse can occur before the GPU fence completes for every submission that was in flight when `sweep()` ran**. 2. **Shadow retirement (`shadow::let_go_idle()`):** - In-flight `HostList`s hold `Arc<Shadow>` in `deferred.copies_held` until their submission lands in `larp-land`. - Under the `LOOSE` mutex (blocking concurrent `loose()` hand-outs), `let_go_idle()` requires `!s.owned && Arc::strong_count(s) == 1 && host::references(&s.buffer) == 1 && long_ago(s.used, idle_ms, IDLE_FRAMES = 4)`. - Routing the drop through `host::retire_release()` guarantees that even if an in-flight submission referenced the buffer, `Drop` is deferred to `larp-reaper` strictly after `FINISHED >= started`. 3. **Staged descriptor sweep (`descriptor::stage::sweep()`):** - Only evicts entries idle for `> IDLE_FRAMES` (4 frames), clears `e.key` and `e.view` atomically, and retires `Ended::Kept(kept)` through `crate::host::retire()` — holding both the staged descriptor slot and its kept `ID3D12Resource` until `FINISHED >= started`. So removing the synchronous `self.settle()` drain only removes the CPU stall waiting for the GPU to go idle; the actual GPU-completion boundary is still enforced by `STARTED`/`FINISHED` fence retirement on every swept object.
Author
Owner

@⁠vanya890 on GitHub · 2026-12-06 01:22 UTC · original

The resource scoping is materially better than the previous global UAV barriers: the AS copy now orders the source resource before the copy and the written destination afterward, with the positions pool included only when the copy path uses it. The periodic sweep also no longer forces a full submit-thread settle, while the existing idle/refcount/deferred-retirement guards remain responsible for lifetime safety. The stack array is bounded to the two descriptor heaps represented by the state. Please retain the RT-heavy stress validation in the PR evidence, since barrier/lifetime regressions can be workload-dependent.

**@⁠vanya890** on GitHub · 2026-12-06 01:22 UTC · [original](https://github.com/gamingwithhomie/larp/pull/464#issuecomment-7449124755) The resource scoping is materially better than the previous global UAV barriers: the AS copy now orders the source resource before the copy and the written destination afterward, with the positions pool included only when the copy path uses it. The periodic sweep also no longer forces a full submit-thread settle, while the existing idle/refcount/deferred-retirement guards remain responsible for lifetime safety. The stack array is bounded to the two descriptor heaps represented by the state. Please retain the RT-heavy stress validation in the PR evidence, since barrier/lifetime regressions can be workload-dependent.
Author
Owner

@⁠TrinityForever15 on GitHub · 2026-12-06 03:17 UTC · original

Running on RX 6650 XT 8 GB (RDNA 2), Series S profile, with #393/#392/#435, on top of amd-test v1.1.7. No RT artifacts or device-removed errors with the per-resource UAV barriers. The combined stutter numbers are in #393 (1% low 112 ms, longest frame 434 ms, down from 6,519 ms). Thanks! https://github.com/iceboerg00/larp-amd/issues/64

**@⁠TrinityForever15** on GitHub · 2026-12-06 03:17 UTC · [original](https://github.com/gamingwithhomie/larp/pull/464#issuecomment-7450572333) Running on RX 6650 XT 8 GB (RDNA 2), Series S profile, with #393/#392/#435, on top of `amd-test` v1.1.7. No RT artifacts or device-removed errors with the per-resource UAV barriers. The combined stutter numbers are in #393 (1% low 112 ms, longest frame 434 ms, down from 6,519 ms). Thanks! https://github.com/iceboerg00/larp-amd/issues/64
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-464'
git switch 'github-bot/gh-464'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-464'
git switch 'github-bot/gh-464'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-464'
git switch 'main'
git merge --squash 'github-bot/gh-464'
git switch 'main'
git merge --ff-only 'github-bot/gh-464'
git switch 'main'
git merge 'github-bot/gh-464'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.