Performance fixes #293

Closed
github-bot wants to merge 67 commits from gh-pr/293/head into gh-pr/293/base
Owner

@⁠infernalfire72 on GitHub · 2026-11-24 23:20 UTC · original
performance-fixes → main · +4588 −702 · 37 files · 67 commits · closed without merging

No description provided.

**@⁠infernalfire72** on GitHub · 2026-11-24 23:20 UTC · [original](https://github.com/fxceless/victory-fork/pull/293) `performance-fixes` → `main` · +4588 −702 · 37 files · 67 commits · closed without merging *No description provided.*
texture::upload previously checked Staging::dirty at command-list recording time, before Staging::refresh ran at submission. Once a title-written texture became clean (after an unchanged submission, or after a multi-list submission where Batch::texture_due skipped the second refresh while the second defer_host cleared dirty), the next CPU update skipped CopyTextureRegion and sampled stale host texels until a subsequent use; conversely, a changed texture left dirty=true after submission and forced a redundant GPU copy on the next unchanged submission.

Move CopyTextureRegion into HostList::prelude() right after run_deferred() refreshes staging buffers, so unchanged textures skip GPU copies and changed ones upload on the same submission. Add two D3D12 regression tests covering both cases.
Reads of title memory in place (texture refreshes at submission, rectangle
vertices) and the runtime's changes of its mapping (XMemMapPhysicalPages,
its scatter form, map_alias) shared one lock over all of title memory.
While the world streams in the title changes mappings 650-1,100 times a
frame, many a page at a time, so every refresh waited for changes anywhere
and every change for the refresh under way.

Now a read holds the bytes it reads and a change the bytes whose mapping it
changes (the range, and the whole of the views unmap takes away and maps
again); they wait for each other only where those meet. A change waiting
for a read keeps new reads of its bytes out, as the lock's writers did.
VICTORY_MAPPING_RANGES=0 holds all of title memory, as before. A new frame
line gives the changes per frame, the time they waited for reads in place
and the time they were under way.

Paired runs on one exe (1684989 + this change) with a warm shader cache,
RTX 5070 Ti, 8 min each, medians over the world; A with
VICTORY_MAPPING_RANGES=0, B without:
- frame 801 -> 486 ms (mean 1,099 -> 524, p90 2,199 -> 670)
- textures 371 -> 47 ms, of them waiting for title memory to hold still
  203 -> 0 ms (mean 389 -> 0.6)
- texture bytes read 233 -> 288 MB/s: nothing is left out
- the runtime's changes waiting for reads 37 -> 0 ms a frame
- GPU 150 -> 135 ms, copies 44 -> 45, copy back 79 -> 72; drops 375 -> 363
- no device removal in either; slow submissions 4 and 5
- frames: the same lit opening scenes, B further into them in the time
For comparison, VICTORY_TEXTURE_NO_WAIT=1 on 1684989 (pair 768 -> 530 ms)
reads 91 MB/s against 228 and leaves ~61 refreshes a frame for later.

Tests: victory-gpu (new: reads and changes of other bytes go on, and a
change that waits keeps new reads of its bytes out), victory-hle (the
read-in-place stress test of xmem), the workspace; clippy in both CI
configurations. Rebased onto 4e17478 without conflicts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A sampled texture that changed is detiled into its staging buffer on the
submitting thread, one subresource after another, at about 4.5 GB/s:
7-17 ms for a single 33-67 MB texture, and part of the frame line's
"textures" time. Layout::detile_rows detiles one band of a subresource's
rows, and a subresource of 8 MiB or more is now cut into bands of about
4 MiB that the kept threads (shadow::wide_run) detile at once. Smaller
ones, and 3D ones, are detiled whole as before.

Bands give the rows of the whole detile byte for byte (tests over every
mode, mips, odd sizes, block compression and array slices). On a Ryzen 5
5600X with DDR4-3600 (three kept threads and the caller): 33 MB 7.3 ->
4.2 ms, 67 MB 16.6-17.1 -> 7.4-8.5 ms (detile_in_bands, detile_threads).
A rebuild for another reason (a change in the gpu, hle or loader crates, a diag build next to a release one) emptied the whole shader cache, and the first run of every new executable translated each shader again on the title's own processors while the pipeline library's thousands of early starts asked for them (a 253 ms frame at 20-30 s). The key is now victory-shader's source hash (build.rs, from upstream PR Marvisak/VIctory#254 by wbkepler) and a schema version of this file's encoding, with the settings that change a translation as before. VICTORY_SHADER_CACHE_KEY=exe keys by the executable again. Adds is_cached, a lookup of an entry without reading or translating.

Co-authored-by: wbkepler <[email protected]>
Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
The pipeline library lists thousands of pipelines to start early. Each needs its shader's translation and the driver's compile; on every worker at once they took the processors from the title's own threads while it loaded. Early work is limited to ceil(workers/4) workers (VICTORY_EARLY_WORKERS, 0 for no limit) and an early pipeline whose shaders are neither translated nor in the shader cache is left for its first use. Requested pipelines go first and are not limited.

Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
Two gaps kept the ray-tracing programs off on a host whose waves are 32 lanes.

1. Buffer views picked from the view heap at a computed index. The shadow-ray
   trace programs read the address of their structure out of a heap descriptor
   they choose with a per-wave index, and walk the memory behind it. The
   translator could only take such an address from a fixed table position.
   The host now keeps, after the strides of `SLOT_STRIDES`, two more dwords
   per heap slot (low and high of the title address of the buffer view
   there), written where the strides are written, and the shader reads them
   by heap slot (`SLOT_ADDRESSES`). No new root parameter; the table grows
   from 8 MB to 24 MB per view heap.

2. `lane_uniform` proves three more things where no lane of a wave can part
   from the others: `tid >> 6` of the entry thread number (in a group whose x
   size is a multiple of 64) is the same in all lanes of a console wave; an
   `ds_read_b32` at such an address is; and a value joined by a phi, also at
   a loop header, is when its arguments are and every predecessor is entered
   by all the arriving lanes or none (a greatest fixed point over the loop's
   carried values and lane masks). The structure builder's block-scan
   programs fetch a wave-uniform running total with `v_readfirstlane` in
   such a loop; without the proof they asked for an exchange between the two
   host waves where not every lane runs.

Effect, with `VICTORY_HOST_WAVE=32` on a 32-lane card:
- 4 of the 5 shadow-ray trace programs (and their screen-space variants) now
  translate and validate; the adaptive one still stops at a lane mask read
  as data.
- 52 of the 52 structure-builder programs now translate, validate and
  create a host pipeline (3 did not).

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
THE BUG.
BeginQuery and EndQuery were empty no-ops and ResolveQueryData always returned synthetic visible counts (1 << 20 for OCCLUSION, 1 for BINARY_OCCLUSION), preventing the title from occlusion-culling hidden geometry.

WHY NOT HYPOTHETICAL.
Returning 1,048,576 passed samples for every occlusion query forces the title to keep drawing occluded world geometry every frame.

THE FIX.
Behind VICTORY_REAL_OCCLUSION=1 (off by default to match fork rollout switches), create a host ID3D12QueryHeap for occlusion heaps in query.rs, forward BeginQuery/EndQuery for OCCLUSION and BINARY_OCCLUSION in list.rs, and resolve directly into the destination Shadow buffer with refresh/gpu_writes copy-back tracking and checked bounds.

SCOPE.
Only occlusion (0) and binary occlusion (1) queries behind VICTORY_REAL_OCCLUSION=1. Timestamps, pipeline statistics, and default behavior when the switch is unset are unchanged.
THE BUG.
Every ExecuteIndirect call (even when batched via VICTORY_INDIRECT_WINDOW=1) runs a clamp compute shader between two global UAV barriers and state transitions and invalidates cached host compute pipeline/root-signature state.

WHY NOT HYPOTHETICAL.
In gameplay, thousands of indirect mesh draws per frame pay compute/graphics mode switches, scratch-buffer copies, barrier stalls, and state rebinds even when the title argument buffers are already valid.

THE FIX.
Add VICTORY_INDIRECT_CLAMP (enabled by default; 0/false/no/off disables clamping). When disabled, draw::State::indirect validates record alignment/stride via indirect::validate_layout, refreshes the argument/count shadow ranges, emits a single UAV barrier (indirect::wait_direct), and executes ExecuteIndirect directly from the title argument buffer without running the clamp shader, scratch copy, state transitions, or pipeline invalidation.

SCOPE.
Only the indirect argument clamping path in indirect.rs and draw.rs. Default behavior (clamping enabled) is unchanged.
The relay (VICTORY_RELAY) lands what shaders wrote on a thread of its own
while the submitting thread prepares the next submissions; stage 2 waits
only for the flights that declared memory a submission reads. It was off
unless switched on. In the world of the test title on an RTX 4090 the
frame went from about 230 to 132 ms with VICTORY_RELAY=2 (the card busy
about 79 % of the frame instead of 51 %), and a 30-minute run with it
had no error or crash at a steady 24-25 GB.

VICTORY_RELAY is now read in one place (shadow::relay_level): unset or
any other value means stage 2, `1` the first stage, `0` turns it off.

The test buffer_data_reaches_the_host_texture checked that the title's
list still held the texture after the submission. With the relay the
submission takes the host list and the flight lets it go once the card
has run it, which on a fast card can be before the check: the hold is
now checked before the submission, where both paths keep it, and after
it only when the submission is waited for.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Lj18ajyo1buNyoZcYjnZYC
Windows 10's own D3D12 runtime has no shader model 6.6, which every
translated pipeline needs (descriptor heaps indexed from shaders).

- apps/victory exports D3D12SDKVersion/D3D12SDKPath (build.rs tells the
  linker to) so the system loader picks up D3D12\D3D12Core.dll beside
  victory.exe.
- `cargo xtask agility` fetches the pinned Microsoft.Direct3D.D3D12
  1.620.2 NuGet package (SHA-256 checked) and copies the runtime into
  every built profile. The redistributable never enters the repository.
- victory-gpu: `host_check` runs before the title starts and `create`
  checks again; both log which D3D12 runtime is loaded and refuse a
  device without shader model 6.6 / resource binding tier 3 with a
  message that says how to fix it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Windows 10: load the D3D12 Agility SDK runtime for shader model 6.6
Every indirect execution was followed by a move of the list's clamped
arguments back to the common state, and the next clamp moved them from
common to unordered access. Drivers may treat a barrier into or out of
the common state as a full wait, so even the window path
(VICTORY_INDIRECT_WINDOW=1), whose draws skip the clamp, still recorded
one barrier between every two draws.

The list now remembers that its scratch buffer was left readable as
indirect arguments: executions read it with no barrier of their own, the
next clamp moves it straight back to unordered access, and it returns to
common once, when the list closes or a bigger buffer replaces it. The
waits for earlier shader writes around the clamp are unchanged.

Tests: two windows of executions with no barrier between them, on WARP
and through a host list; the tracked state across clamps, growth and
reset; the existing clamp tests follow the new states. With
VICTORY_D3D12_DEBUG=1 in a diagnostics build, the host-list tests also
check that the debug layer reports nothing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A frame's worth (3,200) of one-record indirect draws of our own, clamped
one at a time and in windows of 32, recorded with the old barriers (back
to the common state after every execution) and the new ones, timed with
GPU timestamps around the list. Ignored by default; any machine with a
GPU runs it in seconds, no title files needed:

cargo test -p victory-gpu --release --lib indirect::bench -- --ignored --nocapture

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A fresh table took dynamic slots for entries 0..=last used, so a draw that
reads entries 120..=123 held 124 slots. In heavy scenes on a capped NVIDIA
view heap (2^18 dynamic slots) the frame then asked for more than the pool
holds, tables were refused ("no free dynamic descriptor slots"), and the
indirect draws behind them were dropped until the scene got lighter.

A fresh table now takes from its first used entry or picked index to its
last, and its virtual slot 0 is moved back by that start, so entry k still
lies at base + k. The start is clamped to the mirrored count so base stays
whole. After upstream VIctory PR #1349 by wrothgaril, adapted to this
fork's picked indices.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The node test's reference (bvh.rs) and its emitter (emit/intersect.rs)
took a child whose pointer is 0xffff_ffff for a hit whenever the ray
entered its box. Its result dword was "none" either way, but sorted it
came with a finite distance, so it could stand before a child that is
there; a traversal that stops at the first "none" would then miss that
child.

Both now test the pointer too: a child that is not there is a miss, as
the reference's description of the result already said. The box test's
emitted code grows by two integer instructions per child (a compare and
an and): the parity tests' program (the 64-bit form) goes from 519 to
527 instructions.

The reference's new test is the reproducer of #173: a child that is not
there whose box is entered first, one child behind it, and two that are
not there with their boxes at the ray's origin, in both node widths. The
card tests run the same node and ray. With the reference corrected and
the emitter not, the four parity tests fail on the card; corrected, they
pass on the card and on WARP.

Not known: what the console returns for such a child, and whether the
title's builder ever leaves a box under one. The cases of #173 are
synthetic. docs/fork-ray-tracing.md lists it with the other assumptions.

Reproducer and patch: @Bennet0k, #173.

Co-Authored-By: GLM <[email protected]>
Measured on an 8 GB card (7,250 MiB budget), 5-minute runs in the world:
with VICTORY_CARD_BUFFERS_MIN_MB=1, VICTORY_CARD_BUFFERS_MB=2048,
VICTORY_CARD_REHASH_RUNS=1, VICTORY_WIDE_BACK=1 and VICTORY_WIDE_MERGE=1
the frame went from ~225 ms (4-4.5 fps) to ~155 ms (6-7 fps): preparing
150 -> 80 ms, copying back 95 -> 45-50 ms, the CPU's search for shader
writes 394 -> 28 MB a frame. No crash or device loss.

- VICTORY_WIDE_BACK, VICTORY_WIDE_MERGE, VICTORY_CARD_REHASH_RUNS: on
  unless set to 0.
- Card buffer limits: 1 MiB / 2048 MiB on budgets of 6 GiB to 20 GiB
  (was 16 / 512 up to 12 GiB and 4 / 2048 above). Unknown and smaller
  budgets keep 16 / 512; the env overrides still win.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:40:06 +00:00
github-bot closed this pull request 2026-11-26 03:40:06 +00:00

Pull request closed

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