[PR #1547] [MERGED] d3d12: compile a submission's pipelines on worker threads (RT-PIPELINE-COMPILE-WORKERS) #1574

Closed
opened 2026-11-26 20:21:43 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1547
Author: @TheGeogeo
Created: 11/25/2026
Status: ✅ Merged
Merged: 11/25/2026
Merged by: @aslan2142

Base: main ← Head: perf/pipeline-compile-workers


📝 Commits (2)

  • a3980cd runtime: a bounded worker pool for pipeline compiles (RT-PIPELINE-COMPILE-WORKERS)
  • 3e1c49e d3d12: compile a submission's pipelines on worker threads (RT-PIPELINE-COMPILE-WORKERS)

📊 Changes

10 files changed (+2139 additions, -20 deletions)

View changed files

📝 docs/d3d12_vulkan.md (+20 -0)
📝 runtime/CMakeLists.txt (+27 -0)
📝 runtime/shims/d3d12_com.h (+2 -0)
📝 runtime/shims/d3d12_vulkan_device.cpp (+45 -7)
➕ runtime/src/pipeline_workers.h (+173 -0)
📝 runtime/src/vulkan_backend.h (+29 -1)
📝 runtime/src/vulkan_replay.h (+136 -12)
➕ runtime/test/d3d12_pipeline_workers_test.cpp (+851 -0)
➕ runtime/test/pipeline_workers_test.cpp (+292 -0)
➕ runtime/test/replay_pipeline_compiles_test.cpp (+564 -0)

📄 Description

Claimed in the coordination issue as: RT-PIPELINE-COMPILE-WORKERS (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7148953101)

What and why

Since #984 and #939 a graphics pipeline is compiled at its first draw and a compute pipeline at its first dispatch (unless the warm-up thread built it), and a draw that leaves an attachment unbound compiles a variant. All of that happens in prepare(), one pipeline after another on the thread that submits, with the resource lock held, so a submission that needs many new pipelines takes the sum of their compiles.

This compiles them side by side:

  • prepare() leaves a pipeline that is not built yet to PipelineCompiles, once per pipeline and variant. VulkanQueue::execute_now compiles them all on a bounded pool (runtime/src/pipeline_workers.h), the submitting thread among them, waits for all of them and then writes the command buffer. No draw or dispatch is left to a later frame or drawn with another pipeline.
  • The result is what prepare() would have returned had it built each one, in the order of the commands: a refused pipeline fails the list of a draw or an indirect dispatch and drops a dispatch (strict replay fails that too).
  • The resource lock is held while they compile, as it was while each compiled, so a pipeline state released meanwhile waits for the submission. The workers have ended before the session cache is read at device teardown.
  • LEONIDA_PIPELINE_THREADS=<n> sets the workers: unset, half the logical processors and at most 8; 0 keeps the path of before.
  • Graphics pipelines of 1 MiB of SPIR-V or more and compute pipelines over 128 KiB (LEONIDA_SERIAL_BIG_COMPILES) still compile one after another, beside the smaller ones: docs/pipeline_check.md measured 1.96 GB for one 5.7 MB module. big_compile itself is as it was (the finding in https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7150502283 is another topic).
  • The warm-up thread and LEONIDA_ASYNC_PIPELINES=1, which skips draws while one worker compiles, are not changed, and a pipeline in that mode is never left to the pool.

Nothing is compiled earlier than today.

Tested

Windows 11 (10.0.26300), MSVC 14.52, RelWithDebInfo, Visual Studio 18 2026 generator.

  • Synthetic measurement on main 2213d5cb, LEONIDA_COMPUTE_WARMUP=0 (new programs, never compiled before, so neither the process nor the driver's disk cache has seen them), first submission, three alternating runs, 8 workers:

    before after LEONIDA_PIPELINE_THREADS=0
    192 graphics pipelines 861 to 882 ms 126 to 143 ms 859 to 918 ms
    192 variants 856 to 863 ms 124 to 130 ms 864 to 872 ms
    96 compute pipelines 486 to 519 ms 74 to 81 ms 481 to 520 ms

    The second submission and CreatePipelineState take the same. The process holds about 50 MiB more (the driver keeps memory for each thread that compiles). Through one shared VkPipelineCache 192 graphics programs compile x4.9 faster with 8 threads and x7.3 with 16; with one cache per thread, merged, x5.5 and x9.2. Not measured again after the rebase onto main 5129be10.

  • New tests: pipeline_workers (the pool, CPU), replay_pipeline_compiles and _strict (3000 random submissions and fixed cases give the same result, pipelines, drops and compiles with and without the pool; CPU), d3d12_pipeline_workers and _eager (GPU: 48 graphics and 16 compute pipelines, the same bytes with 0, 1, 4 and 16 workers; refused compiles; two queues; pipeline states released and the device destroyed while a submission compiles). The GPU tests skip without a Vulkan device and below the 144-byte push constants.

  • Each new test fails for the changes that should fail it: 36 CPU and 15 GPU mutants (a dropped wait, lock, drain or dedup among them) all fail, the originals pass 30 repeated runs.

  • Full ctest of main 5129be10 and of this branch on it, compared test by test: 337 tests before (324 pass, 13 skip), 342 after (329, 13), no failure and no other status.

  • test_ctest_registration, test_docs_links, test_docs_cli_flags, test_text_write_line_endings, git diff --check and check_no_game_data.py pass.

Not run: the title. I do not have the package, so the effect on boot time and frame time is for someone who has it to measure. No Vulkan SDK here, so the Khronos validation layer did not run, and the d3d12 tests that need glslangValidator or llvm-mc were not built. Other GPUs were not run. The compile of one very large pipeline is not faster.

GPUs tested: NVIDIA GeForce RTX 3080, driver 621.24, Windows 11 (10.0.26300).

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (none added)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1547 **Author:** [@TheGeogeo](https://github.com/TheGeogeo) **Created:** 11/25/2026 **Status:** ✅ Merged **Merged:** 11/25/2026 **Merged by:** [@aslan2142](https://github.com/aslan2142) **Base:** `main` ← **Head:** `perf/pipeline-compile-workers` --- ### 📝 Commits (2) - [`a3980cd`](https://github.com/ubiquitarianism/leonida/commit/a3980cd437b28a047f8e0da3a698df32a34ecbc0) runtime: a bounded worker pool for pipeline compiles (RT-PIPELINE-COMPILE-WORKERS) - [`3e1c49e`](https://github.com/ubiquitarianism/leonida/commit/3e1c49e1bd0ab725c00400a52326fdd80add4840) d3d12: compile a submission's pipelines on worker threads (RT-PIPELINE-COMPILE-WORKERS) ### 📊 Changes **10 files changed** (+2139 additions, -20 deletions) <details> <summary>View changed files</summary> 📝 `docs/d3d12_vulkan.md` (+20 -0) 📝 `runtime/CMakeLists.txt` (+27 -0) 📝 `runtime/shims/d3d12_com.h` (+2 -0) 📝 `runtime/shims/d3d12_vulkan_device.cpp` (+45 -7) ➕ `runtime/src/pipeline_workers.h` (+173 -0) 📝 `runtime/src/vulkan_backend.h` (+29 -1) 📝 `runtime/src/vulkan_replay.h` (+136 -12) ➕ `runtime/test/d3d12_pipeline_workers_test.cpp` (+851 -0) ➕ `runtime/test/pipeline_workers_test.cpp` (+292 -0) ➕ `runtime/test/replay_pipeline_compiles_test.cpp` (+564 -0) </details> ### 📄 Description Claimed in the coordination issue as: RT-PIPELINE-COMPILE-WORKERS (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7148953101) ### What and why Since #984 and #939 a graphics pipeline is compiled at its first draw and a compute pipeline at its first dispatch (unless the warm-up thread built it), and a draw that leaves an attachment unbound compiles a variant. All of that happens in `prepare()`, one pipeline after another on the thread that submits, with the resource lock held, so a submission that needs many new pipelines takes the sum of their compiles. This compiles them side by side: - `prepare()` leaves a pipeline that is not built yet to `PipelineCompiles`, once per pipeline and variant. `VulkanQueue::execute_now` compiles them all on a bounded pool (`runtime/src/pipeline_workers.h`), the submitting thread among them, waits for all of them and then writes the command buffer. No draw or dispatch is left to a later frame or drawn with another pipeline. - The result is what `prepare()` would have returned had it built each one, in the order of the commands: a refused pipeline fails the list of a draw or an indirect dispatch and drops a dispatch (strict replay fails that too). - The resource lock is held while they compile, as it was while each compiled, so a pipeline state released meanwhile waits for the submission. The workers have ended before the session cache is read at device teardown. - `LEONIDA_PIPELINE_THREADS=<n>` sets the workers: unset, half the logical processors and at most 8; `0` keeps the path of before. - Graphics pipelines of 1 MiB of SPIR-V or more and compute pipelines over 128 KiB (`LEONIDA_SERIAL_BIG_COMPILES`) still compile one after another, beside the smaller ones: `docs/pipeline_check.md` measured 1.96 GB for one 5.7 MB module. `big_compile` itself is as it was (the finding in https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7150502283 is another topic). - The warm-up thread and `LEONIDA_ASYNC_PIPELINES=1`, which skips draws while one worker compiles, are not changed, and a pipeline in that mode is never left to the pool. Nothing is compiled earlier than today. ### Tested Windows 11 (10.0.26300), MSVC 14.52, RelWithDebInfo, Visual Studio 18 2026 generator. - Synthetic measurement on `main` 2213d5cb, `LEONIDA_COMPUTE_WARMUP=0` (new programs, never compiled before, so neither the process nor the driver's disk cache has seen them), first submission, three alternating runs, 8 workers: | | before | after | `LEONIDA_PIPELINE_THREADS=0` | |---|---|---|---| | 192 graphics pipelines | 861 to 882 ms | 126 to 143 ms | 859 to 918 ms | | 192 variants | 856 to 863 ms | 124 to 130 ms | 864 to 872 ms | | 96 compute pipelines | 486 to 519 ms | 74 to 81 ms | 481 to 520 ms | The second submission and `CreatePipelineState` take the same. The process holds about 50 MiB more (the driver keeps memory for each thread that compiles). Through one shared `VkPipelineCache` 192 graphics programs compile x4.9 faster with 8 threads and x7.3 with 16; with one cache per thread, merged, x5.5 and x9.2. Not measured again after the rebase onto `main` 5129be10. - New tests: `pipeline_workers` (the pool, CPU), `replay_pipeline_compiles` and `_strict` (3000 random submissions and fixed cases give the same result, pipelines, drops and compiles with and without the pool; CPU), `d3d12_pipeline_workers` and `_eager` (GPU: 48 graphics and 16 compute pipelines, the same bytes with 0, 1, 4 and 16 workers; refused compiles; two queues; pipeline states released and the device destroyed while a submission compiles). The GPU tests skip without a Vulkan device and below the 144-byte push constants. - Each new test fails for the changes that should fail it: 36 CPU and 15 GPU mutants (a dropped wait, lock, drain or dedup among them) all fail, the originals pass 30 repeated runs. - Full `ctest` of `main` 5129be10 and of this branch on it, compared test by test: 337 tests before (324 pass, 13 skip), 342 after (329, 13), no failure and no other status. - `test_ctest_registration`, `test_docs_links`, `test_docs_cli_flags`, `test_text_write_line_endings`, `git diff --check` and `check_no_game_data.py` pass. Not run: the title. I do not have the package, so the effect on boot time and frame time is for someone who has it to measure. No Vulkan SDK here, so the Khronos validation layer did not run, and the `d3d12` tests that need `glslangValidator` or `llvm-mc` were not built. Other GPUs were not run. The compile of one very large pipeline is not faster. GPUs tested: NVIDIA GeForce RTX 3080, driver 621.24, Windows 11 (10.0.26300). AI-assisted: yes (Claude Code) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (none added) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:21:43 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1574
No description provided.