Skip to content
23 changes: 22 additions & 1 deletion docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2935,8 +2935,29 @@ submitting thread; at the maintainers' decision it was replaced by this one when
`after_running` and the teardown rules hold as they were. `LEONIDA_COMPILE_THREADS=1` is the layout before the pool:
one general worker and the graphics-only one; there too a PSO has one build running at a time (two variants of one
graphics PSO, one on each worker, let its last release delete it under the other build, qhukz13). A submission still
waits at the compile gate for every compute create in flight (and graphics creates too with `LEONIDA_COMPILE_GATE=adaptive` or `1`; the classic default gates compute builds only), so more workers
waits at the compile gate for every demanded compute create in flight (and graphics creates too with `LEONIDA_COMPILE_GATE=adaptive` or `1`; the
classic default gates compute builds only; warm-ups: next paragraph), so more workers
do not shorten that wait (`SubmitMutex::CompileTurn`; `LEONIDA_COMPILE_GATE=0` turns it off).
- **Warm-ups do not hold the compile gate** (RT-COMPILE-GATE-WARMUP-FREE). With the adaptive gate, a submission
waits at the compile gate (`SubmitMutex::CompileTurn`) only for the demanded creates in the driver, not for a
warm-up's: a compute create made by a compile worker's speculative job (`compile_job_speculative`: the compute
warm-up) passes the gate as speculative. The checkpoint is
a speculative job too, but makes no create. A warm-up's pipeline is simply not ready yet; whoever needs it builds or
waits for it as before. A warm-up still never enters the driver while a submission is in it or waits at the gate,
and at most `LEONIDA_COMPILE_GATE_WARMUP_MAX` of them (default 1) are in the driver at once, so the creates beside a
submission stay bounded: the pool's demanded builds plus that many warm-ups, and the pool starts no warm-up while a
demanded build runs. Why: on HasiKe's RTX 2070 SUPER (#2233) a cold boot crawled at about one submission a second
behind 290 s of serial warm-ups with the gate on, and ran at about 100 frames a second with `LEONIDA_COMPILE_GATE=0`;
MoonEyes measured 142 s of submission waits behind warm-ups (#1608). The gate stays for demanded creates, which the
title waits for anyway. A speculative create holds the gate as a demanded one does (`SubmitMutex::warmup_passes`,
lixonik's review of #2275): with the classic gate and with `LEONIDA_COMPILE_GATE=1`, which are in use where the
driver holds a submission behind a create or that is not known (`auto`'s calibration on 630.20), and where no probe
ever says otherwise; as a graphics or mesh prewarm (`LEONIDA_PIPELINE_PREWARM_ORDER`, also a speculative job), since the calibration measures only a compute create and a
submission was held 54 s beside a mesh create on 621.40; and as a create of a kind the adaptive gate found to hold
submissions, where a submission beside it would wait in the driver instead. `LEONIDA_COMPILE_GATE_WARMUPS=1` makes
every warm-up hold the gate, as before; `=0` lets compute ones pass with every gate, the classic one and `=1` too;
`LEONIDA_COMPILE_GATE=0` still turns the whole gate off. The paragraphs above and below that say a warm-up holds the
gate for its whole create describe those cases.
- **Only the large ones asynchronous** (`LEONIDA_ASYNC_MIN_BYTES=<n>`, with `LEONIDA_ASYNC_PIPELINES=1`). With every
pipeline asynchronous, the small lighting and post programs are skipped with the large ones until the worker gets to
them, and on an RTX 4070 Laptop (NVIDIA 621.40) the world phase was reached with the screen black; with none, a
Expand Down
4 changes: 3 additions & 1 deletion runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,13 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w
| `LEONIDA_TEXTURE_REFRESH_ASYNC` | unset | `1` moves the before-submission refresh of the title textures (`refresh_title_textures`) to a worker: a submission only wakes it, and fills by itself only the mapped textures that were never filled, so their first draw has their bytes. Persistent mappings are hashed whole on the worker every pass; the rest is swept 128 textures at a time. A texture changed since may be sampled a frame behind. |
| `LEONIDA_SUBMIT_ASYNC` | `0` | `<n>`: each queue's `ExecuteCommandLists`, `Signal`, `Wait` and markers run in call order on a worker of the queue's own (`src/vulkan_backend.h`), at most n batches queued; the caller of the next one waits for room. The replay, the before-submission refreshes and `vkQueueSubmit` then leave the title's thread. A batch that fails is reported as a held one is. `0`: on the caller's thread. |
| `LEONIDA_COMPUTE_WARMUP_LIVE_MAX_BYTES` | unset | `<n>`: a compute warm-up of a module over n bytes of SPIR-V is not started while the title submits (a submission in the last second); the pipeline is built at its first use instead (docs/d3d12_vulkan.md, "Pipelines still being built"). Unset or `0`: every warm-up runs, ordered behind a submission's compiles by the large-compile gate. |
| `LEONIDA_COMPILE_GATE` | `auto` (adaptive or classic, by calibration) | The compile gate (`SubmitMutex::CompileTurn`, `src/vulkan_device_fault.h`): a queue submission (or a present or device wait) does not enter the driver while a pipeline create is in it, and waits in the runtime instead, since on NVIDIA 630.20 a `vkQueueSubmit` that entered during a long `vkCreateComputePipelines` waited 10 s in the driver and lost the device. Creates pass together; one that arrives while a submission waits queues behind it. Unset or `auto`: the device measures the driver once, before the title has it (`src/compile_gate_calibration.h`, `docs/d3d12_vulkan.md`): a worker thread creates a synthetic compute pipeline that takes about a second to compile while the queue gets empty submissions, each waited for. Submissions back in under 50 ms with the create still in the driver give the adaptive gate; one held until the create leaves, a create too fast to tell, a submission slow for another reason or any failure give the classic gate. The verdict is kept per GPU and driver (vendor, device, driver version, pipeline cache UUID) in `compile_gate_calibration.txt` beside the pipeline cache, so the measurement runs once per driver (a failed or unclear calibration is not kept: the next launch measures again), and is logged once: `[rt] compile-gate: auto: adaptive (calibrated on NVIDIA 621.40 in 0.7 s: 115 submissions came back in <= 0.5 ms beside a 722 ms create; cached in ...)`, or `auto: classic (...)`, or `(cached calibration of ...)`. Only gta6rt's title process calibrates: `--precompile-pipelines`, `--compile-server` and tests keep the classic gate. `classic`: compute creates only, waited for however long they take (`LEONIDA_COMPILE_GATE_WAIT_MS` can bound it), no probes, the default before `auto`. `adaptive`: the adaptive gate whatever the calibration says; on a driver that holds submissions (630.20) its first probe of a kind enters the driver after the probe budget and loses the device if that create stays 10 s more. The adaptive gate's wait is bounded by `LEONIDA_COMPILE_GATE_WAIT_MS`. Each submission that enters the driver next to a create the gate went past (at that limit, or earlier at the probe budget, `LEONIDA_COMPILE_GATE_PROBE_MS`) is a probe; two that come back in under 250 ms, at least 1 s apart, with the create still in the driver turn the gate off for the rest of the run (NVIDIA 621.40: 0-11 ms), and one that stays 2 s or more keeps it on; both are logged once. The verdict is kept per kind of create, compute and graphics (mesh included; graphics creates pass the gate since then, only compute ones did before): on NVIDIA 621.40 submissions came back in 0-12 ms beside compute creates, but one sat 54 s in the driver beside a 56 s mesh create and the device was lost. A kind that is off is still timed, and a submission that stays 2 s beside one of its creates turns it back on. A submission never goes ahead, at the limit or the budget, beside a create of a kind whose verdict is that it holds submissions: it waits for that create up to `LEONIDA_COMPILE_GATE_HELD_WAIT_MS`. On a device loss the log names the creates in the driver (kind, program, size, how long) and each kind's gate. `1`: always gate, graphics creates too, with no limit unless `LEONIDA_COMPILE_GATE_WAIT_MS` sets one, never probe or turn it off, and a compute pipeline build also holds the submit lock for its driver calls (`compute_driver_turn`, `shims/d3d12_vulkan_device.cpp`). `0`: no gate. |
| `LEONIDA_COMPILE_GATE` | `auto` (adaptive or classic, by calibration) | The compile gate (`SubmitMutex::CompileTurn`, `src/vulkan_device_fault.h`): a queue submission (or a present or device wait) does not enter the driver while a pipeline create is in it, and waits in the runtime instead, since on NVIDIA 630.20 a `vkQueueSubmit` that entered during a long `vkCreateComputePipelines` waited 10 s in the driver and lost the device. Creates pass together; one that arrives while a submission waits queues behind it. Unset or `auto`: the device measures the driver once, before the title has it (`src/compile_gate_calibration.h`, `docs/d3d12_vulkan.md`): a worker thread creates a synthetic compute pipeline that takes about a second to compile while the queue gets empty submissions, each waited for. Submissions back in under 50 ms with the create still in the driver give the adaptive gate; one held until the create leaves, a create too fast to tell, a submission slow for another reason or any failure give the classic gate. The verdict is kept per GPU and driver (vendor, device, driver version, pipeline cache UUID) in `compile_gate_calibration.txt` beside the pipeline cache, so the measurement runs once per driver (a failed or unclear calibration is not kept: the next launch measures again), and is logged once: `[rt] compile-gate: auto: adaptive (calibrated on NVIDIA 621.40 in 0.7 s: 115 submissions came back in <= 0.5 ms beside a 722 ms create; cached in ...)`, or `auto: classic (...)`, or `(cached calibration of ...)`. Only gta6rt's title process calibrates: `--precompile-pipelines`, `--compile-server` and tests keep the classic gate. `classic`: compute creates only, waited for however long they take (`LEONIDA_COMPILE_GATE_WAIT_MS` can bound it), no probes, the default before `auto`. `adaptive`: the adaptive gate whatever the calibration says; on a driver that holds submissions (630.20) its first probe of a kind enters the driver after the probe budget and loses the device if that create stays 10 s more. The adaptive gate's wait is bounded by `LEONIDA_COMPILE_GATE_WAIT_MS`. Each submission that enters the driver next to a create the gate went past (at that limit, or earlier at the probe budget, `LEONIDA_COMPILE_GATE_PROBE_MS`) is a probe; two that come back in under 250 ms, at least 1 s apart, with the create still in the driver turn the gate off for the rest of the run (NVIDIA 621.40: 0-11 ms), and one that stays 2 s or more keeps it on; both are logged once. The verdict is kept per kind of create, compute and graphics (mesh included; graphics creates pass the gate since then, only compute ones did before): on NVIDIA 621.40 submissions came back in 0-12 ms beside compute creates, but one sat 54 s in the driver beside a 56 s mesh create and the device was lost. A kind that is off is still timed, and a submission that stays 2 s beside one of its creates turns it back on. A submission never goes ahead, at the limit or the budget, beside a create of a kind whose verdict is that it holds submissions: it waits for that create up to `LEONIDA_COMPILE_GATE_HELD_WAIT_MS`. On a device loss the log names the creates in the driver (kind, program, size, how long) and each kind's gate. `1`: always gate, graphics creates too, with no limit unless `LEONIDA_COMPILE_GATE_WAIT_MS` sets one, never probe or turn it off, and a compute pipeline build also holds the submit lock for its driver calls (`compute_driver_turn`, `shims/d3d12_vulkan_device.cpp`). `0`: no gate. See `LEONIDA_COMPILE_GATE_WARMUPS` for warm-ups. |
| `LEONIDA_COMPILE_GATE_WAIT_MS` | 0 (15000 with the adaptive gate) | `<ms>`: how long a submission waits at the compile gate before it goes ahead anyway; the creates in the driver then hold no later submission back (creates that enter after it are waited for again), and the log says how long the submission then took in the driver. The waiter holds the submit lock, so every queue stops behind it, and the title's hang watchdog ends the process after 30 s (one create stayed in the driver for over 54 s on NVIDIA 621.40). 15 s keeps the 630.20 protection for creates of up to 25 s. The same with the watchdog kept kicked: nothing ends the run then, but the stops still cost the frames (an RTX 4060 run that way had 459 waits, 155 s in all, by t=202 s). `0`: no limit, and with `LEONIDA_COMPILE_GATE_PROBE_MS` unset no probes either: the gate waits however long the creates take and never adapts. |
| `LEONIDA_COMPILE_GATE_HELD_WAIT_MS` | 60000 | `<ms>`: with the adaptive gate, how long a submission waits for creates of a kind the gate found to hold submissions (`docs/d3d12_vulkan.md`) before it goes ahead anyway; those creates are then not waited for again. `0`: until they leave. |
| `LEONIDA_COMPILE_GATE_PROBE_MS` | 2500 with the adaptive gate (0 with `LEONIDA_COMPILE_GATE_WAIT_MS=0`) | `<ms>`: the adaptive gate's probe budget. Until it knows whether the driver holds a submission behind a create, a submission also goes ahead once the submissions' waits at the gate in the last 10 s, its own included, add up to this; a single wait reaches it at 2.5 s. On NVIDIA 621.40 waits of 3-7 s, none near the 15 s limit, came back to back and kept the title's main loop from reporting for 30 s; on an RTX 4060 with the watchdog kept kicked, 459 waits of up to 2.9 s took 155 s by t=202 s. The cost on a driver that does hold submissions (630.20): the first probe enters the driver after 2.5 s of waiting and loses the device if the create stays 10 s more; the held verdict then stops the early probes. `0`: probes only at `LEONIDA_COMPILE_GATE_WAIT_MS`. Used only by the adaptive gate. |
| `LEONIDA_COMPILE_GATE_RECALIBRATE` | unset | `1`: with `LEONIDA_COMPILE_GATE` unset or `auto`, the device measures the driver again instead of reading the verdict kept in `compile_gate_calibration.txt`, and writes the new one there (after a driver update the key changes and it measures anyway). |
| `LEONIDA_COMPILE_GATE_WARMUPS` | unset (compute warm-ups free with the adaptive gate) | Unset: with the adaptive gate (`LEONIDA_COMPILE_GATE=adaptive`, or `auto` where the calibration chose it), a submission at the compile gate (`SubmitMutex::CompileTurn`) does not wait for a speculative compute create, one a compile worker's speculative job makes (`compile_job_speculative`: the compute warm-up), though such a create still never enters the driver while a submission is in it or waits; it waits for demanded creates as before, and for any create of a kind the adaptive gate found to hold submissions. Graphics and mesh prewarms (`LEONIDA_PIPELINE_PREWARM_ORDER`), speculative jobs too, hold the gate as demanded creates do (the calibration measures only a compute create, and NVIDIA 621.40 held a submission 54 s beside a mesh create), and so do all speculative creates with the classic gate and with `LEONIDA_COMPILE_GATE=1`, which are in use where the driver holds submissions or that is not known. `1`: every speculative create holds the gate as a demanded create does, the behaviour before. `0`: a speculative compute create passes with every gate, the classic one and `1` included (on a driver that holds submissions a submission may then wait in the driver beside it). See `docs/d3d12_vulkan.md`, "Warm-ups do not hold the compile gate". |
| `LEONIDA_COMPILE_GATE_WARMUP_MAX` | `1` | `<n>` (1-64): at most n speculative creates that pass the compile gate without holding submissions (`LEONIDA_COMPILE_GATE_WARMUPS`) in the driver at once. |
| `LEONIDA_FAST_PIPELINES` | unset | `graphics`, `compute` or `all` (`1` = all) creates those pipelines with `VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT` (`shims/d3d12_pipeline_compile.h`): slower shaders, much shorter builds. Part of the compute journal key, so optimized and fast builds keep separate files. |
| `LEONIDA_COMPILE_THREADS` | half the logical processors, 2-8 | `<n>`: the compile workers of the asynchronous builds (`LEONIDA_ASYNC_PIPELINES=1`), the compute warm-up and the cache checkpoint (`shims/d3d12_pipeline_compile.h`, `CompilePool`). A demanded build goes before warm-ups; large and speculative builds take at most n-1 workers, so one stays free for a small build a submission waits for; one build of a PSO at a time; warm-ups one at a time, and none while a demanded build is queued or running. `1`: one general worker plus the graphics-only one (`LEONIDA_GRAPHICS_COMPILE_WORKER`), the layout before the pool. |
| `LEONIDA_COMPILE_HEAVY_MAX` | threads-1 (`1` with `LEONIDA_SERIAL_BIG_COMPILES=1`) | `<n>`: at most n builds of large modules (compute over 128 KiB, graphics of 1 MiB of SPIR-V or more) on the compile workers at once, 1 to threads-1. They also keep within the memory budget of `LEONIDA_BIG_COMPILE_BUDGET_MB`. |
Expand Down
15 changes: 13 additions & 2 deletions runtime/shims/d3d12_pipeline_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,17 @@ inline bool compile_worker_trigger(const char* trigger) {
// waits only for the holder, and only before a pipeline create (it may wait out a vkDeviceWaitIdle or a present:
// waits of 50 ms or more are logged).
// - a pipeline create passes the compile gate (SubmitMutex::CompileTurn) for the length of the call, as its kind
// (compute, or graphics with mesh): the adaptive gate keeps a verdict per kind.
// (compute, or graphics with mesh): the adaptive gate keeps a verdict per kind. As speculative when a compile worker
// runs it for a speculative job (compile_job_speculative); the gate decides whether that lets it pass without
// holding submissions (SubmitMutex::warmup_passes: a compute create, with the adaptive gate).
// - the compile worker, which runs below normal priority, runs the call at normal priority, so a preempted worker
// does not sit on a driver lock a submit waits for.
// True on a compile worker while it runs a speculative job (any job of a priority other than 0: today the compute warm-up,
// the graphics and mesh prewarms of LEONIDA_PIPELINE_PREWARM_ORDER, and the checkpoint, which makes no create;
// PipelineCompiler::run): its pipeline creates ask the compile gate to pass them as speculative, which a compute one
// with the adaptive gate does without holding submissions (SubmitMutex::CompileTurn, warmup_passes).
inline thread_local bool compile_job_speculative = false;

class DriverTurn {
public:
DriverTurn(leonida::SubmitMutex& submits, const char* trigger, const char* call, bool pipeline,
Expand All @@ -189,7 +197,8 @@ class DriverTurn {
if (waited >= 50) note("waited for the submit lock", trigger, call, name, waited);
}
if (pipeline) {
gate_.emplace(submits, leonida::SubmitMutex::CreateInfo{kind, name, trigger, bytes});
gate_.emplace(submits, leonida::SubmitMutex::CreateInfo{kind, name, trigger, bytes},
compile_job_speculative);
if (gate_->waited_ms() >= 50) note("waited at the compile gate", trigger, call, name, gate_->waited_ms());
}
}
Expand Down Expand Up @@ -607,12 +616,14 @@ class PipelineCompiler {
stats_.most_running = (std::max)(stats_.most_running, running_.size());
const auto probe = probe_;
lock.unlock();
compile_job_speculative = speculative; // its creates pass the gate as speculative (DriverTurn)
try {
if (probe) probe(job.owner);
job.work();
} catch (...) {
std::fprintf(stderr, "[rt] pipeline compile worker: a job threw\n");
}
compile_job_speculative = false;
job.work = nullptr; // its captures go before the job counts as done
lock.lock();
++stats_.completed;
Expand Down
Loading
Loading