GPU: batch hoisted mode-7 indirect argument clamps into 64-lane SIMD dispatches (22x faster) #120

Open
github-bot wants to merge 2 commits from github-bot/gh-120 into main AGit
Owner

@⁠ESAT586 on GitHub · 2026-11-25 14:53 UTC · original
ESAT586:belobug/batch-hoisted-clamps → main · +1275 −9 · 2 files · 2 commits · open

Summary

Batches default VICTORY_HOIST_CLAMPS=7 hoisted indirect argument clamps into a single 64-lane SIMD compute shader (BATCH_SOURCE) flushed in HostList::clamp_end(), preserving exact sequential draw-order BUDGET charging (so objects never flicker from frame to frame) while eliminating thousands of per-draw Dispatch(1, 1, 1) calls and global UAV(None) barriers.

Why

VICTORY_HOIST_CLAMPS=7 currently solves the non-deterministic budget cutoff flickering seen in mode 1 by recording one SetComputeRootSignature + SetPipelineState + Dispatch(1, 1, 1) + global UAV(None) barrier per hoisted indirect draw (~9,000 dispatches and ~9,000 full pipeline UAV barriers per frame).

What Changed

  • crates/victory-gpu/src/indirect.rs:
    • Added BATCH_SOURCE (cs_5_1) accepting a persistent mapped upload buffer of packed 32-byte job descriptors (kind, stride, records, args_offset, has_count, count_offset, dst_offset).
    • Processes up to 64 consecutive single-record jobs (records == 1) in one wave and tiles multi-record jobs (records > 1) in 64-lane waves, with lane 0 accumulating BUDGET = 1 << 27 strictly in draw order (left saturation matches SOURCE bit-for-bit).
    • Preserves all existing non-destructive src -> dst scratch copy semantics: count_buf awareness (eff_count = min(records, count_buf.Load(count_offset)), inactive slots untouched and uncharged), full trailing custom stride word preservation (off >= 8 / 12), and zero mutations to src_args or count_buf.
    • Added ClampBatch / JobUpload / flush_before() coalescing adjacent jobs sharing (src_gpu, dst_gpu) and compatible count_gpu (allowing uncounted count = None jobs to merge into adjacent counted runs), emitting one Dispatch(1, 1, 1) per run and one scoped UAV(Some(budget_resource)) barrier between runs.
    • Handles live VICTORY_HOIST_FILE transitions between mode 7 and non-7 diagnostic modes safely by flushing prior queued jobs and serializing budget across batch <-> unbatched boundaries.
    • Added 4 GPU/WARP tests covering exact byte-for-byte parity with per-draw clamping, >256 KiB chunk rollover, multi-source / multi-count runs, HostList::reset() upload reuse, live mode 7 -> mode 1 -> mode 7 interleaving, plus an ignored 9,000-draw release benchmark.
  • crates/victory-gpu/src/host.rs:
    • Added clamp_batch: Mutex<crate::indirect::ClampBatch> to HostList, reset in HostList::reset(), flushed at the start of HostList::clamp_end() right before transitioning scratch chunks to INDIRECT_ARGUMENT, plus budget_resource().

Benchmark (RTX 3060 Ti, --release)

cargo test --release -p victory-gpu --locked -- --ignored --nocapture benchmark_batched_vs_per_draw_mode7_clamps

9000 hoisted mode-7 indirect draw clamps: per-draw 42.91 ms/frame vs batched 1.95 ms/frame (22.0x)

Collapses 9,000 mode 7 dispatches + 9,000 global UAV(None) barriers across two 256 KiB clamp_room chunks into 2 dispatches + 1 scoped budget UAV barrier, saving ~41 ms/frame with identical draw-order budget determinism.

Verification

  • RTX 3060 Ti: cargo test -p victory-gpu --locked -- indirect::tests -> 9 passed, 0 failed.
  • D3D12 Debug Layer (VICTORY_D3D12_DEBUG=1 RUST_LOG=warn, --features diagnostics --test-threads=1): indirect::tests -> 9 passed, 0 failed, 0 D3D12 warnings/errors.
  • WARP (VICTORY_D3D12_WARP=1, --features diagnostics --test-threads=1): indirect::tests -> 9 passed, 0 failed (full victory-gpu suite: 117/118 passed; only pre-existing #102 watch::tests::written_pages_are_reported_to_every_asker failed).
  • Clippy (--all-targets & --all-features): 0 warnings in crates/victory-gpu/src/indirect.rs and modified lines of crates/victory-gpu/src/host.rs.
  • Policy: cargo xtask policy --staged -> asset policy: 2 files ok.

(Related upstream PR on Marvisak/VIctory: Marvisak/VIctory#2472)

**@⁠ESAT586** on GitHub · 2026-11-25 14:53 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/120) `ESAT586:belobug/batch-hoisted-clamps` → `main` · +1275 −9 · 2 files · 2 commits · open ## Summary Batches default `VICTORY_HOIST_CLAMPS=7` hoisted indirect argument clamps into a single 64-lane SIMD compute shader (`BATCH_SOURCE`) flushed in `HostList::clamp_end()`, preserving exact sequential draw-order `BUDGET` charging (so objects never flicker from frame to frame) while eliminating thousands of per-draw `Dispatch(1, 1, 1)` calls and global `UAV(None)` barriers. ### Why `VICTORY_HOIST_CLAMPS=7` currently solves the non-deterministic budget cutoff flickering seen in `mode 1` by recording one `SetComputeRootSignature` + `SetPipelineState` + `Dispatch(1, 1, 1)` + global `UAV(None)` barrier **per hoisted indirect draw** (~9,000 dispatches and ~9,000 full pipeline UAV barriers per frame). ### What Changed - **[`crates/victory-gpu/src/indirect.rs`](crates/victory-gpu/src/indirect.rs)**: - Added `BATCH_SOURCE` (`cs_5_1`) accepting a persistent mapped upload buffer of packed 32-byte job descriptors (`kind`, `stride`, `records`, `args_offset`, `has_count`, `count_offset`, `dst_offset`). - Processes up to 64 consecutive single-record jobs (`records == 1`) in one wave and tiles multi-record jobs (`records > 1`) in 64-lane waves, with lane 0 accumulating `BUDGET = 1 << 27` strictly in draw order (`left` saturation matches `SOURCE` bit-for-bit). - Preserves all existing non-destructive `src -> dst` scratch copy semantics: `count_buf` awareness (`eff_count = min(records, count_buf.Load(count_offset))`, inactive slots untouched and uncharged), full trailing custom stride word preservation (`off >= 8` / `12`), and zero mutations to `src_args` or `count_buf`. - Added `ClampBatch` / `JobUpload` / `flush_before()` coalescing adjacent jobs sharing `(src_gpu, dst_gpu)` and compatible `count_gpu` (allowing uncounted `count = None` jobs to merge into adjacent counted runs), emitting **one `Dispatch(1, 1, 1)` per run** and **one scoped `UAV(Some(budget_resource))` barrier** between runs. - Handles live `VICTORY_HOIST_FILE` transitions between `mode 7` and non-7 diagnostic modes safely by flushing prior queued jobs and serializing `budget` across batch <-> unbatched boundaries. - Added 4 GPU/WARP tests covering exact byte-for-byte parity with per-draw clamping, >256 KiB chunk rollover, multi-source / multi-count runs, `HostList::reset()` upload reuse, live `mode 7 -> mode 1 -> mode 7` interleaving, plus an ignored 9,000-draw release benchmark. - **[`crates/victory-gpu/src/host.rs`](crates/victory-gpu/src/host.rs)**: - Added `clamp_batch: Mutex<crate::indirect::ClampBatch>` to `HostList`, reset in `HostList::reset()`, flushed at the start of `HostList::clamp_end()` right before transitioning scratch chunks to `INDIRECT_ARGUMENT`, plus `budget_resource()`. ## Benchmark (RTX 3060 Ti, `--release`) ```text cargo test --release -p victory-gpu --locked -- --ignored --nocapture benchmark_batched_vs_per_draw_mode7_clamps 9000 hoisted mode-7 indirect draw clamps: per-draw 42.91 ms/frame vs batched 1.95 ms/frame (22.0x) ``` Collapses 9,000 `mode 7` dispatches + 9,000 global `UAV(None)` barriers across two 256 KiB `clamp_room` chunks into **2 dispatches + 1 scoped budget UAV barrier**, saving **~41 ms/frame** with identical draw-order budget determinism. ## Verification - **RTX 3060 Ti**: `cargo test -p victory-gpu --locked -- indirect::tests` -> **9 passed, 0 failed**. - **D3D12 Debug Layer (`VICTORY_D3D12_DEBUG=1 RUST_LOG=warn`, `--features diagnostics --test-threads=1`)**: `indirect::tests` -> **9 passed, 0 failed, 0 D3D12 warnings/errors**. - **WARP (`VICTORY_D3D12_WARP=1`, `--features diagnostics --test-threads=1`)**: `indirect::tests` -> **9 passed, 0 failed** (full `victory-gpu` suite: 117/118 passed; only pre-existing #102 `watch::tests::written_pages_are_reported_to_every_asker` failed). - **Clippy (`--all-targets` & `--all-features`)**: 0 warnings in `crates/victory-gpu/src/indirect.rs` and modified lines of `crates/victory-gpu/src/host.rs`. - **Policy**: `cargo xtask policy --staged` -> `asset policy: 2 files ok`. *(Related upstream PR on `Marvisak/VIctory`: Marvisak/VIctory#2472)*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:36:10 +00:00
Author
Owner

@⁠jmal423 on GitHub · 2026-11-25 16:30 UTC · original

Tested this PR on Windows 11 / AMD Radeon RX 9070 XT (Series X target):

With this PR applied, the GPU hangs shortly after entering the prison cell (DXGI_ERROR_DEVICE_HUNG, 0x887A0006), both with VICTORY_ASYNC_SUBMIT=1 and without it. Reverting this PR eliminates the DEVICE_HUNG immediately.

**@⁠jmal423** on GitHub · 2026-11-25 16:30 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/120#issuecomment-7168092419) Tested this PR on Windows 11 / AMD Radeon RX 9070 XT (Series X target): With this PR applied, the GPU hangs shortly after entering the prison cell (`DXGI_ERROR_DEVICE_HUNG`, `0x887A0006`), both with `VICTORY_ASYNC_SUBMIT=1` and without it. Reverting this PR eliminates the `DEVICE_HUNG` immediately.
Author
Owner

@⁠ESAT586 on GitHub · 2026-11-25 16:58 UTC · original

Thanks for testing on RX 9070 XT and isolating this to the PR, @⁠jmal423!

Disassembling BATCH_SOURCE with fxc /T cs_5_1 /Fc exposed three FXC non-short-circuiting bugs that caused hardware GPUVM page faults (DXGI_ERROR_DEVICE_HUNG) on AMD:

  1. Unconditional OOB jobs Root-SRV load: has_job ? jobs.Load4((j + lane) * 32) : 0 compiled to unconditional ld_raw on T0 (jobs) before masking. Since D3D12 root SRVs have no hardware bounds check, inactive tail lanes (j + lane >= end) read up to 2,016 bytes past the end of the upload buffer whenever a list approached a 4 KiB / 64 KiB page boundary.
  2. Unconditional count_buf Root-SRV load when has_count == 0: (j1.x == 0) || (count_buf.Load(j1.y) >= 1u) compiled to an unconditional ld_raw at offset 0 of T2 (count_buf, which fell back to src_base). On sparse CreateReservedResource Shadow buffers where Tile 0 (0..64 KiB) is unmapped and only src_offset is backed, every uncounted draw faulted on offset 0.
  3. Unconditional OOB LDS read at costs[64]: while (run < 64 && costs[run] == 1) compiled to ld_structured g0[64] past the 64-element groupshared uint costs[64] array whenever 64 consecutive single-record draws filled a wave.

Fixed in the latest commit

  • Replaced ternary / || / && around all root-SRV and LDS loads with explicit [branch] / [loop] + break guards (verified in fxc /Fc DXBC disassembly: every ld_raw and ld_structured is now strictly inside if_nz / after breakc_nz, and no udiv can divide by zero).
  • Bound upload_gpu (always resident, non-sparse) to T2 when a run has no count buffer, used full UAV barriers between distinct buffer runs (preventing 128-byte cache-line hazards across adjacent 16-byte slices in a clamp_room chunk), and mirrored job payloads in host RAM so mid-list buffer growth never reads back from write-combined upload/ReBAR memory.
  • Added regression test batched_bound_before_guards_page_boundary_and_sparse_unmapped_tile_zero covering exact 64 KiB job-buffer boundaries (2047 / 2048 jobs) and uncounted draws from a CreateReservedResource sparse buffer with Tile 0 unmapped — verified 10/10 on RTX 3060 Ti, 10/10 with D3D12 Debug Layer + GPU-Based Validation (VICTORY_D3D12_DEBUG=1 VICTORY_D3D12_GBV=1, 0 warnings/errors), and 10/10 on WARP.

Could you re-test on your RX 9070 XT when you get a chance?

**@⁠ESAT586** on GitHub · 2026-11-25 16:58 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/120#issuecomment-7168807559) Thanks for testing on RX 9070 XT and isolating this to the PR, @⁠jmal423! Disassembling `BATCH_SOURCE` with `fxc /T cs_5_1 /Fc` exposed **three FXC non-short-circuiting bugs** that caused hardware GPUVM page faults (`DXGI_ERROR_DEVICE_HUNG`) on AMD: 1. **Unconditional OOB `jobs` Root-SRV load**: `has_job ? jobs.Load4((j + lane) * 32) : 0` compiled to unconditional `ld_raw` on `T0` (`jobs`) before masking. Since D3D12 root SRVs have no hardware bounds check, inactive tail lanes (`j + lane >= end`) read up to 2,016 bytes past the end of the upload buffer whenever a list approached a 4 KiB / 64 KiB page boundary. 2. **Unconditional `count_buf` Root-SRV load when `has_count == 0`**: `(j1.x == 0) || (count_buf.Load(j1.y) >= 1u)` compiled to an unconditional `ld_raw` at offset `0` of `T2` (`count_buf`, which fell back to `src_base`). On sparse `CreateReservedResource` `Shadow` buffers where Tile 0 (`0..64 KiB`) is unmapped and only `src_offset` is backed, every uncounted draw faulted on offset `0`. 3. **Unconditional OOB LDS read at `costs[64]`**: `while (run < 64 && costs[run] == 1)` compiled to `ld_structured g0[64]` past the 64-element `groupshared uint costs[64]` array whenever 64 consecutive single-record draws filled a wave. ### Fixed in the latest commit - Replaced ternary / `||` / `&&` around all root-SRV and LDS loads with explicit `[branch]` / `[loop]` + `break` guards (verified in `fxc /Fc` DXBC disassembly: every `ld_raw` and `ld_structured` is now strictly inside `if_nz` / after `breakc_nz`, and no `udiv` can divide by zero). - Bound `upload_gpu` (always resident, non-sparse) to `T2` when a run has no count buffer, used full UAV barriers between distinct buffer runs (preventing 128-byte cache-line hazards across adjacent 16-byte slices in a `clamp_room` chunk), and mirrored job payloads in host RAM so mid-list buffer growth never reads back from write-combined upload/ReBAR memory. - Added regression test `batched_bound_before_guards_page_boundary_and_sparse_unmapped_tile_zero` covering exact 64 KiB job-buffer boundaries (`2047` / `2048` jobs) and uncounted draws from a `CreateReservedResource` sparse buffer with Tile 0 unmapped — verified **10/10** on RTX 3060 Ti, **10/10** with D3D12 Debug Layer + GPU-Based Validation (`VICTORY_D3D12_DEBUG=1 VICTORY_D3D12_GBV=1`, 0 warnings/errors), and **10/10** on WARP. Could you re-test on your RX 9070 XT when you get a chance?
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/120/head:github-bot/gh-120'
git switch 'github-bot/gh-120'

Merge

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