Skip to content

GPU: pipelines of different names load from the library at the same time - #1000

Merged
Marvisak merged 1 commit into
Marvisak:mainfrom
TG0000:perf/parallel-pipeline-loads
Nov 25, 2026
Merged

Marvisak merged 1 commit into
Marvisak:mainfrom
TG0000:perf/parallel-pipeline-loads

Conversation

@TG0000

@TG0000 TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Summary

library::pipeline() held the pipeline library's mutex while a pipeline loaded. On AMD a load is not always a lookup: the library file holds only about 140 bytes per pipeline, and what the driver's own cache lacks is compiled inside the load, for up to seconds. All eight pipeline workers then ran as one, and the backlog kept recording threads in make::wait.

Loads now run outside the library's lock, and only same-name pipelines exclude each other. Microsoft documents the library as thread-safe with exactly that exception. Stores and serialising keep the lock. One file: pipeline/library.rs.

Measured (RX 6800, Ryzen 9 5900X, Windows 11, diagnostics build, sampled stacks)

Two runs per side, order main / PR / PR / main:

main this PR
Worker CPU while a backlog is made 1.05 cores 7.8 cores
Worker samples blocked on the lock 481 0
Pipelines still being made, in the world 1,315 / 1,219 54 / 43
Draws run per frame 3 / 20 1,860 / 2,086
Time per frame outside preparing, GPU and copy-back 303 / 196 ms 37 / 24 ms
Total time per frame 799 / 750 ms 1,090 / 1,379 ms

Frames are not shorter on their own. Main looks faster because it drops almost every draw; here they run and their cost shows.

Caveats: the worker rows use a build on top of the copy changes of TG0000:perf/world-frame-copies for main's side (same lock) and the first run under a new exe name for this PR's side; the driver's cache appears to be per exe, and the second PR run had it warm while main's second run did not, so the first run of each is the stricter comparison. The lock is proven by the stacks; that make::wait is the unaccounted time rests on frames pinned at exactly 500 ms, the backlog and the A/B, not on a stack inside it.

Verification

Rust 1.98.1 MSVC, base ffe310e: cargo test --workspace --locked 345 passed, 0 failed, including a GPU test with 8 threads loading while others store and serialise; cargo clippy --workspace --all-targets --locked -- -D warnings clean with and without --all-features. Not tested: NVIDIA, Intel, Wine.

Overlap

AI-assisted: written and measured by Claude Opus 5.5. No game data is in this PR.

🤖 Generated with Claude Code

The pipeline workers held the library's lock while a pipeline loaded. A
load is not always a lookup: an AMD driver's library holds about 140 bytes
per pipeline, and what the driver's own cache lacks is compiled inside the
load, for up to seconds. All eight workers then waited for the one that
held the lock.

The library is documented as thread-safe but for loads of one pipeline on
several threads (ID3D12Device1::CreatePipelineLibrary, "Thread Safety"),
so only those exclude each other now: a name is held by one thread at a
time, and loads run without the library's lock. Stores and serialising
keep it.

Measured on an RX 6800 and a Ryzen 9 5900X (sampled stacks, diagnostics
build, the same start from a kept pipeline library):
- worker CPU while a backlog is made: 1.05 cores -> 7.8; samples blocked on
  the lock: 481 -> 0
- in the world, two runs each: pipelines still being made 1219-1315 -> 43-54,
  draws run per frame 3-20 -> 1860-2086, time per frame outside preparing,
  GPU and copy-back 196-303 ms -> 24-37 ms

The frame itself is not shorter on its own: the draws that used to be
dropped now run (750-799 -> 1090-1379 ms per frame).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MichaelSmithAI

Copy link
Copy Markdown

On an RTX 4090 under Wine/vkd3d-proton 3.2, all five focused library tests passed with f821cb9 plus #844’s null-pointer fix for opening an empty host library. The 96-pipeline fixture exercised the host D3D12 library directly, with concurrent loads of distinct names while other threads stored and serialized. The separate name-gate tests verified that different names can proceed together and callers for the same name take turns.

We also integrated #983’s early-pipeline list changes. All seven library tests passed. In a separate test using VIctory’s save API, saving before first use preserved both legacy keys, and the next save promoted the newly used pipeline. The first run of that fixture hit an existing temporary directory after Wine reused a process ID. It passed after switching to a unique directory.

These runs verify library behavior on Wine. We did not measure worker backlog, frame time, or title output on the RTX 4090.

@ThomasHFWright

Copy link
Copy Markdown

Independent NVIDIA/Wine validation of f821cb9d7550180d7fb1bd8f7318174e3a680041, based on ffe310e. This covers compatibility/correctness of the tests, not the performance improvement measured on AMD.

Environment: Linux, RTX 5090 / NVIDIA 620.73.04, Wine 11.22, vkd3d-proton 30b0eb9bc12dc9f, DXVK dxgi, Rust 1.98.1, x86_64-pc-windows-msvc. An app-local native d3dcompiler_47.dll from an existing Winetricks cache was supplied identically to both full-suite configurations so the synthetic HLSL fixtures could compile. All executions were offline under Xvfb.

The empty-library fix in #347 is needed on this runtime. With this PR alone, the focused library suite gives 3 passed / 2 failed: both a_rejected_blob_gives_an_empty_library and pipelines_load_on_many_threads_at_once fail opening the empty library. Applying #347 (197c7acf) makes all five library tests pass. In that focused run I changed the no-device/no-compiler returns into assertions and printed the compiled shader count: all 96 synthetic shaders compiled, so the concurrency test actually ran. That test-only instrumentation was removed before the full suite.

Full workspace unit/integration suite, built with cargo test --workspace --all-features --locked --offline --target x86_64-pc-windows-msvc --no-run, then all 14 generated test executables run under Wine with --nocapture --test-threads=1:

Configuration Passed Failed Ignored
Unmodified ffe310e 341 5 6
ffe310e + #1000 + #347 345 4 6

The GPU crate improves from 52 passed / 1 failed / 1 ignored to 56 passed / 0 failed / 1 ignored. The three added tests pass, as does the empty-library test fixed by #347. No additional failures appeared.

The four failures shared with baseline are modules::net::tests::only_loopback_is_reachable (missing Wine Winsock export), loads_binds_and_calls_probe (nested Linux Cargo from a Windows process), emit::tests::stub_pipeline_is_created (checksum-rejection expectation differs under vkd3d-proton), and policy::tests::repository_passes_policy (Windows xtask cannot launch host Git). cargo test -p xtask separately passes all four tests natively on Linux, and asset policy passes. Windows-target GPU Clippy with all targets/features and -D warnings also passes.

Limits: no gameplay or performance measurement; no native Windows or genuine Windows WARP run; doctests were not executed under Wine. Some existing tests return early when optional console compilers or the DXIL validator are absent, so the aggregate is the test harness's result, not a claim those optional paths were exercised. No game data was used or attached.

AI-assisted testing with Codex.

@TG0000

TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for both runs. So on vkd3d-proton the library tests need #347 or #844 (opening an empty host library); that failure is in code this PR does not change, so I leave it to those PRs.

@Zipettte

Copy link
Copy Markdown

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at f821cb9: cargo test -p victory-gpu passes, 53 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

MfDev0 pushed a commit to MfDev0/victory that referenced this pull request Nov 24, 2026
pipeline() held the library mutex across LoadPipeline. On AMD a load
can compile for seconds (the library keeps ~140 bytes per pipeline), so
650 pipelines kept the eight workers on one core for minutes while title
frames waited. The library is free-threaded except for concurrent loads
of one name: hold the name instead (also keeps two threads from making
the same pipeline), and keep the lock only for stores and serialising.

From upstream PR Marvisak#1000 (f821cb9).

Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
@TG0000

TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

A correction on the in-game rows. A later warm main / branch / branch / main run of this branch together with #999 did not reproduce the smaller backlog: 528 pipelines still being made on main and 748 on the branch (medians), 522 and 811 draws run, and 1,433 and 1,434 ms per frame. Each side had its own shader cache (509 and 456 entries), and draws differ up to 5x between two runs of the same exe, so that comparison does not settle it either way. Please read "pipelines still being made", "draws run" and the frame times in the table as not reproduced.

What stands is the lock itself: the workers blocked on it in the sampled stacks, and the test with concurrent loads.

@mercuryVM mercuryVM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested f821cb9 on Windows 11, RTX 5070, MSVC: cargo test -p victory-gpu passes (53 passed, 1 ignored), including the three new tests.

The change is right:

  • The D3D12 docs say ID3D12PipelineLibrary synchronizes itself internally. The one exception is several threads loading the same pipeline, which may change that entry's state in the library. That exception is exactly what Names::hold covers. Different names now overlap, while stores and Serialize stay under the library mutex.
  • The Held guard's Drop takes the set's lock, removes the name and calls notify_all. one_name_is_held_by_one_thread_at_a_time checks that a panicking holder releases the name.
  • Cloning the ID3D12PipelineLibrary1 interface out of the lock is a refcount bump. The blob the library was opened from lives in the same static and is never dropped, as the comment says.

Nits:

  • pipelines_load_on_many_threads_at_once returns silently when there is no host GPU or no D3DCompile, so on CI it can pass without testing anything. An eprintln! on those early returns would make that visible.
  • Two threads asking for one missing name now compile it twice? No: the second waits in hold and then finds the first one's stored pipeline. This might be worth one line in the doc comment, since it depends on the store happening before _held drops. It does today, because _held lives to the end of pipeline.

AI-assisted (Claude Code).

@TG0000

TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the careful check, also of the same-name exception in the docs. Both nits are fair: the test should say when it is skipped (no host GPU or no D3DCompile), and the order "stored before _held drops" deserves a line in the comment. I will add them with the next change this branch needs, so that your test run stays valid for the current head.

AI-assisted (Claude Code).

edsuuu referenced this pull request in edsuuu/VI-BR-PORT Nov 24, 2026
…ry at the same time

Head f821cb9 (TG0000). Conflicts in pipeline/library.rs: our imports
(Arc, mpsc for #275's saver thread) with #1000's Condvar; the load takes
#1000's form (the interface cloned out of the lock, a hold per name).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Mumogg

Mumogg commented Nov 24, 2026 •

Copy link
Copy Markdown

Test report from Linux: this is also what starves a warm start under Wine + vkd3d-proton on NVIDIA, and the PR fixes it here.

Setup: RTX 5070 Ti (driver 615.78.08), Ryzen 5 9600X (6 workers), Nobara 44, Wine 11.22 Staging, vkd3d-proton from GE-Proton11-14. Tree: r4tedx integration/testers update 9 (94e0ea5) plus small local changes; diagnostics build; 1.0.2.1, network off. Same warm cache directory for every run (1,167 pipelines listed for an early start, library file 270 kB, so about 190 bytes per pipeline: vkd3d-proton compiles inside the load as well).

Before, measured directly. With a timer around the lock and the load in library::pipeline(), one 300 s warm run:

loads completed 280 (all found in the library)
time inside LoadPipeline, summed 298 s of the 300 s run
time waiting for the library's lock, summed over workers 1,149 s
longest single load / longest wait for the lock 12.3 s / 45.6 s

So the workers ran strictly one at a time for the whole run. The world's first vertex or mesh pipeline arrived after about 570 s in an earlier 600 s run; a cold cache got 234 mesh and 109 vertex pipelines in its first world minute.

After. First with the load moved outside the lock by hand (the interface cloned under the lock, as here): 907 loads in 300 s instead of 280, lock wait 0. Then with this PR's code (f821cb9, merged into that tree; the only conflict was the use std::sync line): all 1,620 early pipelines were loaded 101 s after start (host pipelines all made made=0 loaded=1621 started_early=1620), no device removal, no error from the library in three runs with concurrent loads. That last run also had a two-level queue (see my note on #546), so the 101 s is not this PR alone.

Not tested: AMD, native Windows, a library file from a different driver version.

(AI-assisted, Claude Code: it found the lock from the logs, added the timers and ran the comparisons on my machine.)

Edit: the Wine version was 11.22 Staging, not 11.21 (the system had updated before these runs).

@TG0000

TG0000 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for measuring it directly under Wine and vkd3d-proton: the time inside LoadPipeline and the wait for the lock are the part I could not measure here (Windows, RX 6800 only).

yourmomsbasement pushed a commit to yourmomsbasement/VIctory_Dev that referenced this pull request Nov 24, 2026
…ied together

Merges Marvisak#72 Marvisak#347 Marvisak#1000 Marvisak#358 Marvisak#352 Marvisak#626 Marvisak#1152 Marvisak#1129 Marvisak#1203 Marvisak#24 Marvisak#642 Marvisak#427 Marvisak#485
Marvisak#550 Marvisak#174 Marvisak#1279 onto main (ffe310e), each head unchanged, no conflicts.

One change of mine: three of the PRs each add a `mod tests` at the end of
pipeline/host.rs, so the merge defines `tests` three times (E0428). The
second and third are named `library_tests` and `blend_tests`; nothing else
changes.

cargo clippy --workspace --all-targets --locked -- -D warnings, with and
without --all-features, is clean and cargo test --workspace --locked passes
(367 passed, 6 ignored; main has 342 passed, 6 ignored).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
@Marvisak
Marvisak merged commit b00c14a into Marvisak:main Nov 25, 2026
1 check passed
zhaibingye pushed a commit to zhaibingye/VIctory that referenced this pull request Nov 25, 2026
A load still took the library's lock to clone the interface, and a save
holds that lock while it serialises the whole library (230-600 MB in the
world, Marvisak#1683, Marvisak#1653). Saves run every 20 s while pipelines are being
made, some on the pipeline workers themselves, so every load waited out
each save: the part of Marvisak#1615 that Marvisak#1000 left.

The library is kept a second time beside its lock, and loads use that
one without locking. A load adds nothing to the library, so the size a
save asks for still holds when it serialises; the library is
free-threaded but for loads of one name, which NAMES keeps apart. Stores
and saves keep the lock.

New test: a stored pipeline loads while the lock is held as a save holds
it. With the old locking it times out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants