Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2899,6 +2899,73 @@ submitting thread; at the maintainers' decision it was replaced by this one when
title submits (a submission in the last second, `src/pipeline_wait.h`); the pipeline is built at its first use
instead. Opt-in: unset or 0, every warm-up runs, as on `main`, and `LargeModuleGate` (#1889) orders them behind a
submission's compiles. Smaller warm-ups, and every warm-up before the first submission, run as before.
- **A bounded, adaptive compile gate, opt-in** (`SubmitMutex::enter_submit`, `src/vulkan_device_fault.h`,
`LEONIDA_COMPILE_GATE=adaptive`). Unset, the gate stays the classic one: compute creates only, each waited for
however long it takes, no probes, as before. The adaptive gate is opt-in because of what it costs on a driver that
does hold submissions behind a create (630.20, below): no run of it there has been reported yet. The gate keeps a
`vkQueueSubmit` out of the driver while a create is in it, since on NVIDIA 630.20 one that entered during a long
`vkCreateComputePipelines` waited 10 s and lost the device. The waiting submission holds the submit lock, so all
queues stop: on an RTX 4070 Laptop (NVIDIA 621.40) one compute create of a 2.3 MB module stayed in the driver for
more than 54 s during the story load, the submission waited for all of it and the title's 30 s hang watchdog ended
the process; with `LEONIDA_COMPILE_GATE=0` the same build went past that point. With the adaptive gate a
submission waits at most `LEONIDA_COMPILE_GATE_WAIT_MS` (15 s by default there) and then goes ahead; the creates in
the driver at that moment hold no later submission back, and creates that enter after it are waited for again. On
630.20 that loses only creates over 25 s (15 s here, then the driver's 10 s), which the watchdog ends anyway.
On the same laptop the three submissions that went ahead came back from the driver in 11, 1 and 0 ms with the
create still there, and no device was lost; but consecutive long creates made submissions wait the full 15 s again
and again, and the title's watchdog still ended the run. So the adaptive gate adapts: every submission that
enters the driver next to a create it no longer waits for is a probe. Two that come back in under 250 ms, at least
1 s apart, with that create still in the driver turn the gate off for the rest of the run (a driver that holds a
submission behind a create holds it as long as the create stays, and 630.20 held one for the full 10 s). Two, and a
second apart, because back-to-back creates take and release that driver's lock, so one submission can slip in
between; the second costs no extra wait, since after one has gone ahead the next enter at once while the create is
still there. One probe that stays 2 s or more keeps the gate on for the rest of the run (presents and idle waits do
not probe, below). Both verdicts are logged once. `LEONIDA_COMPILE_GATE=1` gates always and never probes.
In the next run on that laptop the story load passed, but no wait reached 15 s, so no probe came: 80 submissions
waited 3-7 s each (90 s in all, the longest 6.7 s), back to back, and the title's watchdog ended the run at 433 s.
So until a verdict is in, a submission also goes ahead once the submissions' waits at the gate in the last 10 s, its
own included, add up to `LEONIDA_COMPILE_GATE_PROBE_MS` (2500 ms). A budget over a
window rather than a shorter limit per wait, because it also counts many short waits, and one wait reaches it as
soon as a 2.5 s limit would. Before the verdict the gate then stops the queues for at most about a quarter of any
10 s, after a quick verdict not at all. On a driver that does hold submissions (630.20) the first probe now enters
the driver after 2.5 s of waiting instead of 15 s, and loses the device if the create stays there 10 s more (the
classic gate waits that create out). One probe per kind and run: it holds the submit lock while it is in the
driver, so no other submission enters until it comes back, and it then gives the held verdict (2 s or more, back
within 500 ms of the create leaving), after which no submission goes ahead beside a create of that kind before the
held limit. The held threshold stays 2 s, well inside the 10 s such a driver gives before the loss. The limit and the
budget apply with the title's watchdog kept kicked too (`LEONIDA_TITLE_WATCHDOG=0` and
`LEONIDA_TITLE_WATCHDOG_KICK`): nothing ends the run then, but the stops still cost the frames, and an RTX 4060 run
that way had 459 waits, 155 s in all and the longest 2.9 s, by t=202 s. Only `LEONIDA_COMPILE_GATE_WAIT_MS=0` (with
no `LEONIDA_COMPILE_GATE_PROBE_MS`) or `LEONIDA_COMPILE_GATE=1` (with no `LEONIDA_COMPILE_GATE_WAIT_MS`) waits
however long the creates take and never adapts.
Tested by `submit_mutex` (no GPU); the probe budget is not yet measured in the title.
The verdict is kept per kind of create, compute and graphics (mesh pipelines included), and graphics creates
pass this gate and `LEONIDA_COMPILE_GATE=1` too (the classic gate, as before, only compute builds). On 621.40 every probe beside a compute create came back in
0-12 ms (and on an RTX 4060 with 610.88, 133 of them), but a run whose gate those probes had turned off lost the
device 9 s later with a submission held 7.8 s beside a mesh create, and with `LEONIDA_COMPILE_GATE=0` a
`vkQueueSubmit` sat 54 s in the driver beside a 56 s mesh create (541 KB) and the device was lost. A probe counts
for each kind that had a create in the driver from before the submission entered until it came back; a held one
counts against all of them, since which create held it cannot be told. A kind that is off is still timed: a
submission that stays 2 s or more beside one of its creates turns that kind back on for the rest of the run. A
held probe (pending or off) counts only if the submission came back within 500 ms after a create of that kind,
there when it entered, left the driver: a submission held behind a create comes back when the create does. On the
4070 Laptop (621.40, 8 GB nearly full) a 36 s submission came back 18 s before the 58 s compute create beside it;
counted as held, it re-armed the gate, which then kept the next submission 78 s behind compute creates. A
synthetic probe on the same machine (one to three threads creating 2 MB graphics pipelines of 8-10 s each, with
and without `VK_NV_device_diagnostics_config` and a shared pipeline cache) never held a submission past 80 ms.
The recent leaves are kept in a ring, so a create that entered after the submission and left after the one it
entered beside does not hide that one. Only submissions (`vkQueueSubmit`, `vkQueueBindSparse`) probe: presents and
idle or semaphore waits pass the gate the same way but can take seconds on their own, so they judge nothing, and
they never go ahead at the probe budget, only at the limit. While a create of a kind that holds submissions is in
the driver, no submission goes ahead at the limit or the budget, and such a create is waited for even when one went
past it before the verdict, up to the held limit (`LEONIDA_COMPILE_GATE_HELD_WAIT_MS`, 60 s; 0 waits however long,
and a held limit under the plain one applies on its own): then the creates in the driver are gone past and the
next submissions no longer wait for them. This is checked on every pass of the wait: once such a create leaves
while creates of a kind with no verdict stay, the plain limit and budget apply again. A false
"holds" verdict so costs at most that per create, not the rest of the run. On a device loss the log names every
create in the driver (kind, program, SPIR-V size, thread, how long it had been there, whether submissions waited
for it) and each kind's gate. Long graphics creates would leave the title's process only through the compile
helper, which builds compute pipelines alone today.

**Open:** whether the game uses `D3D12_PIPELINE_STATE_STREAM_DESC` for mesh/RT and which subobject types appear.
The trace will enumerate them.
Expand Down
5 changes: 4 additions & 1 deletion runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,10 @@ 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` | unset | `1`: a compute pipeline build holds the device's submit lock for its driver calls, so no compute compile runs while a queue submits. Unset: the asynchronous workers compile while other queues submit. |
| `LEONIDA_COMPILE_GATE` | on (classic) | 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: the classic gate, as before the adaptive one: compute creates only, waited for however long they take (`LEONIDA_COMPILE_GATE_WAIT_MS` can bound it), no probes. `adaptive`: the adaptive gate, opt-in until it has run on a driver that holds submissions (630.20), where 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_WAIT_MS` | 0 (15000 with `LEONIDA_COMPILE_GATE=adaptive`) | `<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 `LEONIDA_COMPILE_GATE=adaptive` (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 with `LEONIDA_COMPILE_GATE=adaptive`. |
| `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_GRAPHICS_COMPILE_WORKER` | on | A second compile worker that takes graphics pipeline builds only, so a long compute build does not hold them up (`shims/d3d12_pipeline_compile.h`). `0`: the one general worker builds everything. |
| `LEONIDA_CONSOLE_HEAP_GPU` | unset | `1`: the V#s of the console descriptor heaps are rebased by a compute pass at the start of each submission (`src/console_heap_gpu.h`) instead of on the CPU. The CPU still finds the changed slots and registers the T#s; a V# the pass cannot answer is resolved after its submission and its slot queued again. |
Expand Down
8 changes: 5 additions & 3 deletions runtime/shims/d3d12_pipeline_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,15 @@ inline bool compile_worker_trigger(const char* trigger) {
// behind it go first, and does so before its vkCreateShaderModule and vkDestroyShaderModule too; a title thread
// 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.
// - 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.
// - 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.
class DriverTurn {
public:
DriverTurn(leonida::SubmitMutex& submits, const char* trigger, const char* call, bool pipeline,
const char* name = nullptr)
const char* name = nullptr,
leonida::SubmitMutex::CreateKind kind = leonida::SubmitMutex::CreateKind::compute, std::size_t bytes = 0)
: worker_(compile_worker_trigger(trigger)) {
// Normal priority first: a below-normal worker that waits its turn at normal priority cannot keep losing the
// race to the next submit, and holds the gate (below) only at normal priority.
Expand All @@ -187,7 +189,7 @@ class DriverTurn {
if (waited >= 50) note("waited for the submit lock", trigger, call, name, waited);
}
if (pipeline) {
gate_.emplace(submits);
gate_.emplace(submits, leonida::SubmitMutex::CreateInfo{kind, name, trigger, bytes});
if (gate_->waited_ms() >= 50) note("waited at the compile gate", trigger, call, name, gate_->waited_ms());
}
}
Expand Down
Loading
Loading