GPU: reuse own-byte copy buffers (VICTORY_EXACT_REUSE), log card-cap turn-downs, split bind time #1390

Closed
github-bot wants to merge 3 commits from github-bot/gh-1390 into main AGit
Owner

@⁠Alpacher on GitHub · 2026-11-26 22:30 UTC · original
Alpacher:pr/fxceless-v9-perf → main · +376 −22 · 3 files · 3 commits · closed without merging

From: Alpacher/victory-fork:pr/fxceless-v9-perf
Into: fxceless/victory-fork:main
Base: 52ac4ec ("GPU: dynamic descriptor slots go back only to the heap that took them (#1324)")

Commits (oldest first):

Commit Title
06b1fa4 GPU: reuse the buffers of views' own-byte copies (VICTORY_EXACT_REUSE)
515e421 GPU: say when a title buffer is turned down for a card copy by VICTORY_CARD_BUFFERS_MB
a522721 GPU: split bind time on the frame line (VICTORY_BIND_SPLIT)

Diff vs origin/main: 3 files changed, 376 insertions(+), 22 deletions(-)

 crates/victory-gpu/src/draw.rs   |  75 +++++++++-
 crates/victory-gpu/src/host.rs   |  11 ++
 crates/victory-gpu/src/shadow.rs | 312 ++++++++++++++++++++++++++++++++++++---

This PR adds no new crates, no Cargo.toml/Cargo.lock changes and no scripts, and it doesn't touch victory-shader. Every behaviour change sits behind an env var that has an =0 fallback to the exact old path.


TL;DR

  1. Exact-copy buffer reuse (perf).
    • shadow::exact() used to create a brand-new committed buffer for every view whose bytes needed their own copy. GTA VI's rings move every frame, so that meant hundreds of buffers a frame, created under the lock every list-recording thread takes.
    • Now freed buffers go into a pool by width. A buffer is reused only once the GPU has provably finished with it (a submission gate plus a frame gate).
    • On an RTX 5070, descriptor-table bind time fell from 1,073 ms to 212 ms a frame (all threads summed).
  2. Card-cap turn-down log (diagnostic).
    • When copies hit the VICTORY_CARD_BUFFERS_MB cap, big title buffers silently stayed in system memory and were compared on the CPU: 161.9 MB a frame on the 5070.
    • The refusal is now logged, so the cap can be found and raised. With the cap raised, the same compare dropped to 0.0 MB a frame.
  3. Bind-time split (diagnostic). A per-frame breakdown of State::bind (whole, early textures, descriptor tables, title memory by address, rest). This is what pinned the hot spot fixed in item 1.

Separately, for reviewers: the face "spike" corruption seen downstream was caused by PORT_CONSTANTS (in-place graphics region), not by exact reuse. See the section on it below.


Test rig and method

GPU NVIDIA GeForce RTX 5070 12 GB (budget reported 11,175 MB)
CPU AMD Ryzen 7 5800X3D (8C/16T)
RAM 31.9 GB
OS Windows 11
Title GTA VI (game_scarlett_master.exe), Series X mode
  • Numbers come from the frame-line tracing::info! output of victory.exe, read from full run logs. Where a line says "since the start", the number is cumulative.
  • FPS, frame time and CPU/GPU utilisation come from the user's HUD and overlay readings during the same sessions.
  • The measurements were taken on a downstream fork, which has this PR's code plus other work, so the end-to-end FPS figures include that other work too.
  • The per-subsystem numbers (bind split, CPU compare MB, mapped waits) isolate the code paths this PR changes.
  • Two of the A/B runs varied more than one variable, and this is called out wherever it applies.

1. Exact-copy buffer reuse: VICTORY_EXACT_REUSE

Files: crates/victory-gpu/src/shadow.rs (exact copies, pool, tests) and crates/victory-gpu/src/host.rs (two counters exposed)

Problem

Some title views (SRV/UAV/CBV over raw title memory) start at a byte offset that doesn't fall on their copy's element boundaries. shadow::exact() gives each such view its own byte-exact copy, keyed by (address, len). GTA VI suballocates constants and per-draw data from rings, so the view addresses change every frame.

What we measured:

  • 650–675 new exact copies a frame, with about 6,000 alive at a time.
  • Each one was a fresh CreateCommittedResource, made while holding the exact map lock that every list-recording thread contends on.
  • The old idle sweep walked the whole map, and its most expensive test (references(), an AddRef/Release round-trip on the COM object) ran first.

Mechanism

Pool (Spares<T>, SPARES)

  • Buffers of exact copies that are let go go into a pool keyed by width. The width is the copy's length rounded up to a multiple of 256 bytes (len.next_multiple_of(256), since main's Shadow has no width()).
  • The pool is capped at 256 MB. A single item larger than 1/8 of the cap is not pooled.
  • When the cap is reached, the oldest spares are retired through the existing host::retire(), so they get the normal deferred-release path.

Reuse gate (spare_gate)
A spare is stamped (started + lag, frame + frames) when it is given back. Here started is host::submissions_started() at that moment and frame is frame::current(). take() hands it out only when both of these hold:

  • host::submissions_finished() >= started + VICTORY_EXACT_REUSE_LAG (default 64). Every submission that could have named the buffer, plus 64 more, has completed on the GPU.
  • frame::current() >= frame + VICTORY_EXACT_REUSE_FRAMES (default 3).

When a copy is let go
A copy goes back to the pool only after the sweep finds it idle. The tests run cheapest first:

  1. Arc::strong_count == 1: only the map holds it.
  2. Not used for LATELY frames.
  3. references(): the COM refcount shows that no descriptor slot or list view (Kept) still holds it.

On reuse

  • The buffer is zeroed (write_bytes) and gets a new Shadow. Nothing from its last life carries over: no contents, no tracking state.

Lock scope

  • The sweep runs every SWEEP_EVERY = 32 new copies, not on every miss.
  • Taking a spare or creating a new buffer happens outside the exact lock. If two threads race to create the same key, the loser gives its buffer to the pool with gate (0, 0). Nobody ever saw that buffer, so it is reusable at once.

Stats

  • EXACT_STATS grows to 3 counters: copies asked for, copies made with a new buffer, and copies given a spare.
  • The exact_line frame line now shows reused counts and the pool size. Its existing text, including "VICTORY_VIEW_OWN_COPY=0", is kept.

host.rs additions (read-only Acquire loads of counters that already exist)

  • pub(crate) fn submissions_started() -> u64
  • pub(crate) fn submissions_finished() -> u64

Why it is safe

  • The GPU can't still be reading the buffer.
    • The submission gate counts finished submissions past the STARTED value seen at give-back, plus a 64-submission margin. That covers lists recorded but not yet sent. It is the same margin the codebase's deferred retire uses, for the same reason.
    • The frame gate adds 3 more frames on top.
  • No CPU-side or descriptor holders. The strong-count and COM-refcount checks mean no Arc, descriptor slot or list view still points at the buffer.
  • No stale data. The buffer is zeroed and gets a fresh Shadow, so a reused buffer is indistinguishable from a freshly created one.
  • Bounded memory. The pool is at most 256 MB. Overflow is retired through the normal path, and oversize items are never pooled.
  • Exact fallback. VICTORY_EXACT_REUSE=0 gives the pre-PR behaviour: always create, no pool, no gate.

Unit tests (new, shadow::spare_tests)

  • a_spare_is_taken_only_of_its_width_and_after_its_gate: width matching, and refusal before the gate.
  • a_spare_waits_past_the_lag_and_some_frames: spare_gate arithmetic, and that both conditions (submissions and frames) are needed.
  • spares_keep_to_their_bytes_oldest_first_and_refuse_big_ones: the byte cap, oldest-first eviction, and refusal of items over 1/8 of the cap.

Measured (RTX 5070, from the VICTORY_BIND_SPLIT line)

Run Reuse Binds/frame bind total (all threads) Descriptor tables Early textures Rest
run-v9-20261126-225548 off 9,325 1,132.9 ms 1,073.1 ms 16.7 ms 43.0 ms
run-v9-20261126-225146 on 9,589 250.2 ms 212.4 ms 5.7 ms 32.0 ms
  • Descriptor-table time per frame fell by about 80% (1,073 → 212 ms, about 5×), and total bind time by about 78%.
  • Across that run's frame lines, the reuse-on table time was 208–212 ms.
  • Caveat: the two runs also differed in render scale (0.5 vs 2/3) and in the downstream in-place port. The table cost is almost entirely buffer creation, though, which reuse removes directly.

Memory-mapping waits for new card memory

  • The downstream fork's VICTORY_WAIT_RESIDENT path waits for every new committed resource to be GPU-VA-mapped before use. Cumulative counts:
    • reuse off: 292,400 waits, 31.3 s in total (longest 10.9 ms), by 22:58 CET in the copied log. Extrapolated to the full session, the v10 audit put it at about 293,000 waits and about 59 s.
    • reuse on: 21,200 waits, 3.7 s (longest 16.4 ms).
  • So about 93% fewer waits, and the residency stall is essentially gone from the steady state.
  • main doesn't have that waiting path. On main, the same saving shows up as CreateCommittedResource calls avoided, plus the OS's own residency work.

Reported end to end: with reuse on, CPU utilisation went from about 89% to 59% in the same scene.


2. Card-cap turn-down log (diagnostic, no behaviour change)

File: crates/victory-gpu/src/shadow.rs (cap check in card_room_for, new CARD_CAPPED counter)

Problem

  • VICTORY_CARD_BUFFERS_MB caps the total size of title-buffer copies kept in the card's memory. It defaults to 512 MB when the budget is 12 GiB or less, and the RTX 5070 reports 11,175 MB.
  • Once the copies reached the cap, card_room_for returned None without logging anything, unlike its other turn-downs. Later big buffers stayed in system memory, where shader writes to them are compared on the CPU, and every GPU access to them crosses PCIe.
  • In the profiled run, 66 card copies (510.6 MB) filled the cap. After that, the frame line showed:

shader writes looked for on the CPU per frame: … 184.8 MB in 333 ranges of title buffers' copies (111.2 MB snapshotted), of them 161.9 MB in copies of 1 MiB or more

Mechanism

  • New static CARD_CAPPED: [AtomicU64; 2], holding the count and bytes turned down by the cap since the start.
  • On a cap refusal, an info! line is logged for the first 16 refusals and then every 64th:
    • no card copy for a title buffer of N KiB: the copies made hold X MB of at most Y MB (VICTORY_CARD_BUFFERS_MB); K buffers turned down so far for it (B MB), frame F

Why it is safe

  • It is logging only: two relaxed atomic adds plus a rate-limited log line. The refusal itself (fetch_sub and return None) is unchanged.

Measured

Run Card cap CPU-compared, copies ≥1 MiB
run-v8-20261126-215944 512 MB (default) 161.9 MB/frame
run-v9-20261126-225146 / -225548 1024 MB (VICTORY_CARD_BUFFERS_MB=1024) 0.0 MB/frame
  • With the cap raised, all CPU-compared copy traffic fell to 24.5–28.8 MB a frame, in small ranges only.
  • The default is not changed by this PR. The log tells a user with headroom which knob to turn.

3. Bind-time split: VICTORY_BIND_SPLIT

Files:

  • crates/victory-gpu/src/draw.rs: BIND_NS, bind_split(), BindLap, bind_split_line(), and laps in State::bind
  • crates/victory-gpu/src/host.rs: one call from the frame line

Problem

Dispatches and indirect executions were costing 40–60 µs of CPU each on the 5070, and no log line showed where that time went inside State::bind.

Mechanism

  • static BIND_NS: [AtomicU64; 5] holds nanoseconds for the whole of bind, the early texture pass, descriptor tables and title memory by address, plus a bind count.
  • BindLap is an RAII guard. It records Instant::now() on start(part) and adds the elapsed time on Drop, so every early return false in bind is still counted.
  • State::bind gets four laps:
    • 0: the whole function;
    • 1: around early_textures();
    • 2: around tables();
    • 3: around memory_ready().
  • bind_split_line(frames) is called from the host frame line, right before the heap-buffer-pass stats. It swaps the counters to zero and logs:
    • binding draws and dispatches per frame (VICTORY_BIND_SPLIT=0: not timed): N binds X ms (all threads), of them the early texture pass … ms, descriptor tables … ms, title memory by address … ms, the rest (root views and their refreshes, the heap pass, state) … ms
  • "The rest" is computed as whole − (early + tables + memory) with saturating subtraction.

Why it is safe

  • It is measurement only, with no change to binding logic. The expressions keep the same evaluation order, and each wrapped call is moved into a block whose value is returned unchanged.
  • The cost is one Instant::now() pair and one relaxed fetch_add per lap, about 4 laps a bind, at 9,000–10,000 binds a frame.
  • VICTORY_BIND_SPLIT=0 turns off both the timing (BindLap holds None) and the line. The flag is read once through a OnceLock.

New environment variables

Variable Default Meaning A/B switch
VICTORY_EXACT_REUSE on (unset ≠ 0) Reuse pooled exact-copy buffers =0 gives the pre-PR path: always create, no pool
VICTORY_EXACT_REUSE_LAG 64 Submissions that must finish past the give-back STARTED before reuse Raise it to be more conservative
VICTORY_EXACT_REUSE_FRAMES 3 Frames that must pass after give-back before reuse Raise it to be more conservative
VICTORY_BIND_SPLIT on (unset ≠ 0) Time State::bind and its parts, and log them per frame =0: not timed, no line
  • Item 2 (the card-cap log) has no switch. It is a rate-limited info! line on an existing refusal path.
  • The existing knob it points to, VICTORY_CARD_BUFFERS_MB, is unchanged (512 MB default when the budget is 12 GiB or less).

Root-cause note: face spikes came from PORT_CONSTANTS, not EXACT_REUSE

This is context for review, because exact reuse was first suspected.

Symptom (downstream). Characters' faces and skinned meshes showed vertex "spikes". The first user A/B toggled exact reuse, the in-place port and render scale together, so it pointed at reuse.

Finding, from code analysis plus a one-variable-at-a-time A/B:

  • VICTORY_PORT_CONSTANTS (renderer port 1) places the title's 185 MB graphics region in place. It is opened from its address as CUSTOM/system-memory heaps through OpenExistingHeapFromAddress1. The 174 MB heap at 0x34e10000 holds the constants ring and skinning output.
  • In place, the GPU's writes there are untracked: nothing is compared, landed or waited for. Meanwhile CPU-side exact copies of views into that heap snapshot its bytes. They capture stale constants and skinning data that the GPU has already rewritten, and skinned vertices are drawn from that stale data, which produces the spikes.
  • The in-place heap also lives in system memory, so every draw's constants and vertices cross PCIe. GPU time was 202 ms with the port on vs 88 ms with it off, even though the port-on run was at the lower 0.5 render scale.
  • The port off with exact reuse on gives clean faces. The user confirmed the spikes are gone in that configuration.

Relevance to main. main doesn't have the port-1 / in-place path (VICTORY_PORT_CONSTANTS) or VICTORY_PLACED_ORDER, so this PR can't trigger the issue. If the port lands later, it should stay opt-in, and exact copies shouldn't snapshot in-place GPU-written memory.


End-to-end results (downstream fork with this PR's code, RTX 5070 / 5800X3D, GTA VI, Series X mode)

Configuration FPS Frame time GPU busy CPU Visuals
Before: no reuse, in-place port on ~4 (4.4) 225–268 ms ~33% ~89% face spikes
After: reuse on, port off, placed-order on ~9 113.6 ms ~86% ~59% spikes gone
  • Moving from a CPU-bound to a GPU-bound profile (GPU 33% → 86%) is the signature of the bind-path fix. Most of the frame-time win comes from exact reuse and from turning the port off.
  • An earlier downstream run at 0.5 render scale (run-v8-20261126-220247) reached 9.4 fps / 106.1 ms.
  • These figures include downstream-only changes (port off by default, VICTORY_PLACED_ORDER). They are not a claim for this PR alone. This PR's own effect is the bind and table numbers in section 1.

Not ported (no equivalent code in main)

Downstream change Why it isn't here
VICTORY_THIN_OPS (bind split lived in list/thin.rs) main has no list/thin.rs, so the split is logged from the host frame line instead
VICTORY_CARD_STEPS_WHY main has no host/card_steps.rs
VICTORY_MAPPED_SHARED / VICTORY_WAIT_RESIDENT stats main has no wait_resident / mapped tracking
VICTORY_PLACED_ORDER, VICTORY_PORT_CONSTANTS main has no placed-copy ordering or in-place port
Descriptor-table sub-split (BIND_NS[8]) Depends on the downstream table code; the 4-part split is enough on main
Launcher scripts Out of scope

Adaptations from the downstream version:

  • The pool width uses shadow.len.next_multiple_of(256), because main's Shadow has no width().
  • Spares are retired through crate::host::retire, because main has no retire_resource / bury_lag. The 64-submission lag is carried in the reuse gate itself.

Verification

All of this was run on Windows 11 (RTX 5070 rig), on branch pr/fxceless-v9-perf at a522721, with an isolated target dir.

set CARGO_TARGET_DIR=target-pr-fxceless
cargo test  --locked --release --workspace --exclude xtask --no-fail-fast
cargo build --locked --release -p victory
  • Tests: 646 passed, 0 failed, 14 ignored across 22 test suites. The ignored tests are already ignored on main. The suite includes the 3 new shadow::spare_tests.
  • Build: cargo build --release -p victory succeeded with 0 warnings.
    • Output: victory.exe (9,050,112 bytes; SHA-256 begins 93D1015DCFA3B779).
  • rustfmt --edition 2024 --check parses all three touched files cleanly.
  • --locked: no lockfile changes.

Manual A/B on the rig:

  • VICTORY_EXACT_REUSE=0 vs on: the bind/table numbers above.
  • Raising the card cap removes the CPU compare.
  • VICTORY_BIND_SPLIT=0 removes the timing line.
**@⁠Alpacher** on GitHub · 2026-11-26 22:30 UTC · [original](https://github.com/fxceless/victory-fork/pull/1390) `Alpacher:pr/fxceless-v9-perf` → `main` · +376 −22 · 3 files · 3 commits · closed without merging **From:** `Alpacher/victory-fork:pr/fxceless-v9-perf` **Into:** `fxceless/victory-fork:main` **Base:** `52ac4ec` ("GPU: dynamic descriptor slots go back only to the heap that took them (#1324)") **Commits** (oldest first): | Commit | Title | |---|---| | `06b1fa4` | GPU: reuse the buffers of views' own-byte copies (VICTORY_EXACT_REUSE) | | `515e421` | GPU: say when a title buffer is turned down for a card copy by VICTORY_CARD_BUFFERS_MB | | `a522721` | GPU: split bind time on the frame line (VICTORY_BIND_SPLIT) | **Diff vs `origin/main`:** 3 files changed, 376 insertions(+), 22 deletions(-) ``` crates/victory-gpu/src/draw.rs | 75 +++++++++- crates/victory-gpu/src/host.rs | 11 ++ crates/victory-gpu/src/shadow.rs | 312 ++++++++++++++++++++++++++++++++++++--- ``` This PR adds no new crates, no `Cargo.toml`/`Cargo.lock` changes and no scripts, and it doesn't touch `victory-shader`. Every behaviour change sits behind an env var that has an `=0` fallback to the exact old path. --- ## TL;DR 1. **Exact-copy buffer reuse (perf).** - `shadow::exact()` used to create a brand-new committed buffer for every view whose bytes needed their own copy. GTA VI's rings move every frame, so that meant hundreds of buffers a frame, created under the lock every list-recording thread takes. - Now freed buffers go into a pool by width. A buffer is reused only once the GPU has provably finished with it (a submission gate plus a frame gate). - On an RTX 5070, **descriptor-table bind time fell from 1,073 ms to 212 ms a frame** (all threads summed). 2. **Card-cap turn-down log (diagnostic).** - When copies hit the `VICTORY_CARD_BUFFERS_MB` cap, big title buffers silently stayed in system memory and were compared on the CPU: **161.9 MB a frame** on the 5070. - The refusal is now logged, so the cap can be found and raised. With the cap raised, the same compare dropped to **0.0 MB a frame**. 3. **Bind-time split (diagnostic).** A per-frame breakdown of `State::bind` (whole, early textures, descriptor tables, title memory by address, rest). This is what pinned the hot spot fixed in item 1. Separately, for reviewers: **the face "spike" corruption seen downstream was caused by `PORT_CONSTANTS` (in-place graphics region), not by exact reuse.** See the section on it below. --- ## Test rig and method | | | |---|---| | GPU | NVIDIA GeForce RTX 5070 12 GB (budget reported 11,175 MB) | | CPU | AMD Ryzen 7 5800X3D (8C/16T) | | RAM | 31.9 GB | | OS | Windows 11 | | Title | GTA VI (`game_scarlett_master.exe`), Series X mode | - Numbers come from the frame-line `tracing::info!` output of `victory.exe`, read from full run logs. Where a line says "since the start", the number is cumulative. - FPS, frame time and CPU/GPU utilisation come from the user's HUD and overlay readings during the same sessions. - The measurements were taken on a downstream fork, which has this PR's code plus other work, so the end-to-end FPS figures include that other work too. - The per-subsystem numbers (bind split, CPU compare MB, mapped waits) isolate the code paths this PR changes. - Two of the A/B runs varied more than one variable, and this is called out wherever it applies. --- ## 1. Exact-copy buffer reuse: `VICTORY_EXACT_REUSE` **Files:** `crates/victory-gpu/src/shadow.rs` (exact copies, pool, tests) and `crates/victory-gpu/src/host.rs` (two counters exposed) ### Problem Some title views (SRV/UAV/CBV over raw title memory) start at a byte offset that doesn't fall on their copy's element boundaries. `shadow::exact()` gives each such view its own byte-exact copy, keyed by `(address, len)`. GTA VI suballocates constants and per-draw data from rings, so the view addresses change every frame. What we measured: - **650–675 new exact copies a frame**, with about 6,000 alive at a time. - Each one was a fresh `CreateCommittedResource`, made while holding the `exact` map lock that every list-recording thread contends on. - The old idle sweep walked the whole map, and its most expensive test (`references()`, an AddRef/Release round-trip on the COM object) ran first. ### Mechanism **Pool (`Spares<T>`, `SPARES`)** - Buffers of exact copies that are let go go into a pool keyed by **width**. The width is the copy's length rounded up to a multiple of 256 bytes (`len.next_multiple_of(256)`, since `main`'s `Shadow` has no `width()`). - The pool is capped at **256 MB**. A single item larger than 1/8 of the cap is not pooled. - When the cap is reached, the **oldest** spares are retired through the existing `host::retire()`, so they get the normal deferred-release path. **Reuse gate (`spare_gate`)** A spare is stamped `(started + lag, frame + frames)` when it is given back. Here `started` is `host::submissions_started()` at that moment and `frame` is `frame::current()`. `take()` hands it out only when both of these hold: - `host::submissions_finished() >= started + VICTORY_EXACT_REUSE_LAG` (default **64**). Every submission that could have named the buffer, plus 64 more, has completed on the GPU. - `frame::current() >= frame + VICTORY_EXACT_REUSE_FRAMES` (default **3**). **When a copy is let go** A copy goes back to the pool only after the sweep finds it idle. The tests run cheapest first: 1. `Arc::strong_count == 1`: only the map holds it. 2. Not used for `LATELY` frames. 3. `references()`: the COM refcount shows that no descriptor slot or list view (`Kept`) still holds it. **On reuse** - The buffer is **zeroed** (`write_bytes`) and gets a **new `Shadow`**. Nothing from its last life carries over: no contents, no tracking state. **Lock scope** - The sweep runs every `SWEEP_EVERY = 32` new copies, not on every miss. - Taking a spare or creating a new buffer happens **outside** the `exact` lock. If two threads race to create the same key, the loser gives its buffer to the pool with gate `(0, 0)`. Nobody ever saw that buffer, so it is reusable at once. **Stats** - `EXACT_STATS` grows to 3 counters: copies asked for, copies made with a new buffer, and copies given a spare. - The `exact_line` frame line now shows reused counts and the pool size. Its existing text, including "VICTORY_VIEW_OWN_COPY=0", is kept. **`host.rs` additions** (read-only Acquire loads of counters that already exist) - `pub(crate) fn submissions_started() -> u64` - `pub(crate) fn submissions_finished() -> u64` ### Why it is safe - **The GPU can't still be reading the buffer.** - The submission gate counts *finished* submissions past the `STARTED` value seen at give-back, plus a 64-submission margin. That covers lists recorded but not yet sent. It is the same margin the codebase's deferred retire uses, for the same reason. - The frame gate adds 3 more frames on top. - **No CPU-side or descriptor holders.** The strong-count and COM-refcount checks mean no `Arc`, descriptor slot or list view still points at the buffer. - **No stale data.** The buffer is zeroed and gets a fresh `Shadow`, so a reused buffer is indistinguishable from a freshly created one. - **Bounded memory.** The pool is at most 256 MB. Overflow is retired through the normal path, and oversize items are never pooled. - **Exact fallback.** `VICTORY_EXACT_REUSE=0` gives the pre-PR behaviour: always create, no pool, no gate. ### Unit tests (new, `shadow::spare_tests`) - `a_spare_is_taken_only_of_its_width_and_after_its_gate`: width matching, and refusal before the gate. - `a_spare_waits_past_the_lag_and_some_frames`: `spare_gate` arithmetic, and that both conditions (submissions *and* frames) are needed. - `spares_keep_to_their_bytes_oldest_first_and_refuse_big_ones`: the byte cap, oldest-first eviction, and refusal of items over 1/8 of the cap. ### Measured (RTX 5070, from the `VICTORY_BIND_SPLIT` line) | Run | Reuse | Binds/frame | `bind` total (all threads) | Descriptor tables | Early textures | Rest | |---|---|---|---|---|---|---| | `run-v9-20261126-225548` | **off** | 9,325 | 1,132.9 ms | **1,073.1 ms** | 16.7 ms | 43.0 ms | | `run-v9-20261126-225146` | **on** | 9,589 | 250.2 ms | **212.4 ms** | 5.7 ms | 32.0 ms | - Descriptor-table time per frame fell by about **80%** (1,073 → 212 ms, about 5×), and total bind time by about 78%. - Across that run's frame lines, the reuse-on table time was 208–212 ms. - Caveat: the two runs also differed in render scale (0.5 vs 2/3) and in the downstream in-place port. The table cost is almost entirely buffer creation, though, which reuse removes directly. **Memory-mapping waits for new card memory** - The downstream fork's `VICTORY_WAIT_RESIDENT` path waits for every new committed resource to be GPU-VA-mapped before use. Cumulative counts: - reuse **off**: **292,400 waits, 31.3 s** in total (longest 10.9 ms), by 22:58 CET in the copied log. Extrapolated to the full session, the v10 audit put it at about 293,000 waits and about **59 s**. - reuse **on**: **21,200 waits, 3.7 s** (longest 16.4 ms). - So about 93% fewer waits, and the residency stall is essentially gone from the steady state. - `main` doesn't have that waiting path. On `main`, the same saving shows up as `CreateCommittedResource` calls avoided, plus the OS's own residency work. **Reported end to end:** with reuse on, CPU utilisation went from about **89% to 59%** in the same scene. --- ## 2. Card-cap turn-down log (diagnostic, no behaviour change) **File:** `crates/victory-gpu/src/shadow.rs` (cap check in `card_room_for`, new `CARD_CAPPED` counter) ### Problem - `VICTORY_CARD_BUFFERS_MB` caps the total size of title-buffer copies kept in the card's memory. It defaults to 512 MB when the budget is 12 GiB or less, and the RTX 5070 reports 11,175 MB. - Once the copies reached the cap, `card_room_for` returned `None` **without logging anything**, unlike its other turn-downs. Later big buffers stayed in system memory, where shader writes to them are compared on the CPU, and every GPU access to them crosses PCIe. - In the profiled run, 66 card copies (510.6 MB) filled the cap. After that, the frame line showed: > shader writes looked for on the CPU per frame: … 184.8 MB in 333 ranges of title buffers' copies (111.2 MB snapshotted), of them **161.9 MB in copies of 1 MiB or more** ### Mechanism - New `static CARD_CAPPED: [AtomicU64; 2]`, holding the count and bytes turned down by the cap since the start. - On a cap refusal, an `info!` line is logged for the **first 16** refusals and then every **64th**: - `no card copy for a title buffer of N KiB: the copies made hold X MB of at most Y MB (VICTORY_CARD_BUFFERS_MB); K buffers turned down so far for it (B MB), frame F` ### Why it is safe - It is logging only: two relaxed atomic adds plus a rate-limited log line. The refusal itself (`fetch_sub` and `return None`) is unchanged. ### Measured | Run | Card cap | CPU-compared, copies ≥1 MiB | |---|---|---| | `run-v8-20261126-215944` | 512 MB (default) | **161.9 MB/frame** | | `run-v9-20261126-225146` / `-225548` | 1024 MB (`VICTORY_CARD_BUFFERS_MB=1024`) | **0.0 MB/frame** | - With the cap raised, all CPU-compared copy traffic fell to 24.5–28.8 MB a frame, in small ranges only. - The **default is not changed** by this PR. The log tells a user with headroom which knob to turn. --- ## 3. Bind-time split: `VICTORY_BIND_SPLIT` **Files:** - `crates/victory-gpu/src/draw.rs`: `BIND_NS`, `bind_split()`, `BindLap`, `bind_split_line()`, and laps in `State::bind` - `crates/victory-gpu/src/host.rs`: one call from the frame line ### Problem Dispatches and indirect executions were costing 40–60 µs of CPU each on the 5070, and no log line showed where that time went inside `State::bind`. ### Mechanism - `static BIND_NS: [AtomicU64; 5]` holds nanoseconds for the whole of `bind`, the early texture pass, descriptor tables and title memory by address, plus a bind count. - `BindLap` is an RAII guard. It records `Instant::now()` on `start(part)` and adds the elapsed time on `Drop`, so every early `return false` in `bind` is still counted. - `State::bind` gets four laps: - `0`: the whole function; - `1`: around `early_textures()`; - `2`: around `tables()`; - `3`: around `memory_ready()`. - `bind_split_line(frames)` is called from the host frame line, right before the heap-buffer-pass stats. It swaps the counters to zero and logs: - `binding draws and dispatches per frame (VICTORY_BIND_SPLIT=0: not timed): N binds X ms (all threads), of them the early texture pass … ms, descriptor tables … ms, title memory by address … ms, the rest (root views and their refreshes, the heap pass, state) … ms` - "The rest" is computed as `whole − (early + tables + memory)` with saturating subtraction. ### Why it is safe - It is measurement only, with no change to binding logic. The expressions keep the same evaluation order, and each wrapped call is moved into a block whose value is returned unchanged. - The cost is one `Instant::now()` pair and one relaxed `fetch_add` per lap, about 4 laps a bind, at 9,000–10,000 binds a frame. - `VICTORY_BIND_SPLIT=0` turns off both the timing (`BindLap` holds `None`) and the line. The flag is read once through a `OnceLock`. --- ## New environment variables | Variable | Default | Meaning | A/B switch | |---|---|---|---| | `VICTORY_EXACT_REUSE` | on (unset ≠ `0`) | Reuse pooled exact-copy buffers | `=0` gives the pre-PR path: always create, no pool | | `VICTORY_EXACT_REUSE_LAG` | `64` | Submissions that must *finish* past the give-back `STARTED` before reuse | Raise it to be more conservative | | `VICTORY_EXACT_REUSE_FRAMES` | `3` | Frames that must pass after give-back before reuse | Raise it to be more conservative | | `VICTORY_BIND_SPLIT` | on (unset ≠ `0`) | Time `State::bind` and its parts, and log them per frame | `=0`: not timed, no line | - Item 2 (the card-cap log) has no switch. It is a rate-limited `info!` line on an existing refusal path. - The existing knob it points to, `VICTORY_CARD_BUFFERS_MB`, is unchanged (512 MB default when the budget is 12 GiB or less). --- ## Root-cause note: face spikes came from `PORT_CONSTANTS`, not `EXACT_REUSE` This is context for review, because exact reuse was first suspected. **Symptom (downstream).** Characters' faces and skinned meshes showed vertex "spikes". The first user A/B toggled exact reuse, the in-place port and render scale together, so it pointed at reuse. **Finding**, from code analysis plus a one-variable-at-a-time A/B: - `VICTORY_PORT_CONSTANTS` (renderer port 1) places the title's 185 MB graphics region **in place**. It is opened from its address as CUSTOM/system-memory heaps through `OpenExistingHeapFromAddress1`. The 174 MB heap at `0x34e10000` holds the **constants ring and skinning output**. - In place, the GPU's writes there are **untracked**: nothing is compared, landed or waited for. Meanwhile CPU-side exact copies of views into that heap snapshot its bytes. They capture **stale constants and skinning data** that the GPU has already rewritten, and skinned vertices are drawn from that stale data, which produces the spikes. - The in-place heap also lives in system memory, so every draw's constants and vertices cross PCIe. GPU time was **202 ms with the port on vs 88 ms with it off**, even though the port-on run was at the *lower* 0.5 render scale. - **The port off with exact reuse on gives clean faces.** The user confirmed the spikes are gone in that configuration. **Relevance to `main`.** `main` doesn't have the port-1 / in-place path (`VICTORY_PORT_CONSTANTS`) or `VICTORY_PLACED_ORDER`, so this PR can't trigger the issue. If the port lands later, it should stay **opt-in**, and exact copies shouldn't snapshot in-place GPU-written memory. --- ## End-to-end results (downstream fork with this PR's code, RTX 5070 / 5800X3D, GTA VI, Series X mode) | Configuration | FPS | Frame time | GPU busy | CPU | Visuals | |---|---|---|---|---|---| | Before: no reuse, in-place port on | ~4 (4.4) | 225–268 ms | ~33% | ~89% | face spikes | | After: reuse on, port off, placed-order on | **~9** | **113.6 ms** | **~86%** | **~59%** | spikes gone | - Moving from a CPU-bound to a GPU-bound profile (GPU 33% → 86%) is the signature of the bind-path fix. Most of the frame-time win comes from exact reuse and from turning the port off. - An earlier downstream run at 0.5 render scale (`run-v8-20261126-220247`) reached 9.4 fps / 106.1 ms. - These figures include downstream-only changes (port off by default, `VICTORY_PLACED_ORDER`). They are not a claim for this PR alone. This PR's own effect is the bind and table numbers in section 1. --- ## Not ported (no equivalent code in `main`) | Downstream change | Why it isn't here | |---|---| | `VICTORY_THIN_OPS` (bind split lived in `list/thin.rs`) | `main` has no `list/thin.rs`, so the split is logged from the host frame line instead | | `VICTORY_CARD_STEPS_WHY` | `main` has no `host/card_steps.rs` | | `VICTORY_MAPPED_SHARED` / `VICTORY_WAIT_RESIDENT` stats | `main` has no `wait_resident` / mapped tracking | | `VICTORY_PLACED_ORDER`, `VICTORY_PORT_CONSTANTS` | `main` has no placed-copy ordering or in-place port | | Descriptor-table sub-split (`BIND_NS[8]`) | Depends on the downstream table code; the 4-part split is enough on `main` | | Launcher scripts | Out of scope | **Adaptations from the downstream version:** - The pool width uses `shadow.len.next_multiple_of(256)`, because `main`'s `Shadow` has no `width()`. - Spares are retired through `crate::host::retire`, because `main` has no `retire_resource` / `bury_lag`. The 64-submission lag is carried in the reuse gate itself. --- ## Verification All of this was run on Windows 11 (RTX 5070 rig), on branch `pr/fxceless-v9-perf` at `a522721`, with an isolated target dir. ``` set CARGO_TARGET_DIR=target-pr-fxceless cargo test --locked --release --workspace --exclude xtask --no-fail-fast cargo build --locked --release -p victory ``` - **Tests:** **646 passed, 0 failed, 14 ignored** across 22 test suites. The ignored tests are already ignored on `main`. The suite includes the 3 new `shadow::spare_tests`. - **Build:** `cargo build --release -p victory` succeeded with **0 warnings**. - Output: `victory.exe` (9,050,112 bytes; SHA-256 begins `93D1015DCFA3B779`). - `rustfmt --edition 2024 --check` parses all three touched files cleanly. - `--locked`: no lockfile changes. **Manual A/B on the rig:** - `VICTORY_EXACT_REUSE=0` vs on: the bind/table numbers above. - Raising the card cap removes the CPU compare. - `VICTORY_BIND_SPLIT=0` removes the timing line.
shadow::exact() made a new committed buffer for every new (address, len) a title view at a ring position asks for (300-675 a frame on an RTX 5070), under the lock every list-recording thread takes. Now the buffers of copies let go of are pooled by width (256 MB at most) and handed to a new copy only once nothing but the copy held them, STARTED + VICTORY_EXACT_REUSE_LAG (64) submissions have finished and VICTORY_EXACT_REUSE_FRAMES (3) frames have passed; zeroed, with a new Shadow. Creation and the idle sweep (every 32 new copies, cheap tests first) run outside the lock. VICTORY_EXACT_REUSE=0: as before.
The cap check in card_room_for refused silently: once 512 MB of copies were made (the default under a 12 GiB budget), later big buffers stayed in the PC's memory and were compared on the CPU (162 MB a frame on an RTX 5070) with nothing in the log.
State::bind's whole time and its parts (early texture pass, descriptor tables, title memory by address, the rest) per frame. VICTORY_BIND_SPLIT=0: not timed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 01:02:59 +00:00
github-bot closed this pull request 2026-11-27 02:34:16 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.