Skip to content
24 changes: 24 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2737,6 +2737,30 @@ Pipeline creation:
a submission waits for them, and what is skipped, is "Pipelines still being built" below. Strict replay builds
synchronously and waits for every build. `LEONIDA_COMPUTE_WARMUP=0` restricts the worker to pipelines requested by
the title.
`LEONIDA_PIPELINE_ORDER_LOG=1` records the order in which the title first asks for pipelines
(RT-PIPELINE-PREWARM-ORDER, phase 1; `runtime/src/pipeline_order.h`). It is off by default: nothing reads the file yet,
so a run that does not ask for it writes nothing and its pipelines carry no hook. For each pipeline and variant (the
unbound-slot mask for graphics, 0 for compute) that a dispatch or draw names for the first time, one line is
appended to `pipeline_order.log` beside `pipeline_cache.session.bin` (so `LEONIDA_PIPELINE_CACHE_DIR` moves it):
`P <c|g|m> <key> <variant hex> <created ms> <first use ms> <build ms or -> <ready 0|1> *<checksum>`, with the
times in milliseconds since the process started. The key is the SHA-256 of the title's own description of the PSO
(`GetCachedBlob`'s key; a mesh pipeline's MS is folded in), not of the SPIR-V, so a translator change keeps it. A
build that ends after its line was written adds `B <key> <variant hex> <build ms> *<checksum>`; a session starts
with `S 1 <vendor hex> <device hex>`. A compute pipeline's `P` line is followed by
`E <key> <variant hex> <name hash hex> *<checksum>`: the name hash is 16 hex digits of FNV-1a 64 over the name of
the library entry the pipeline was made from (the name itself is not written). A reader from before `E` lines
skips them (counted as lines not read). Each line ends with an FNV-1a checksum: a cut or damaged line is skipped on
read, never fatal. The hook sits in replay's `ComputePipeline::requested` / `GraphicsPipeline::requested`, called
where a submission is prepared, also for a handle that exists already (a warm-up build, a cached one); the
recorder decides "first" with one compare-and-swap in a table made at start, never blocks and allocates nothing (a non-blocking `try_lock` on the first request of a pipeline and of a variant),
and a thread of its own appends every 250 ms, so a killed process loses at most that. The record holds 8192 lines
per run (the rest is counted, not kept); a file past 8 MiB is set aside as `pipeline_order.log.prev`. At exit (the
exit cache save, or the device's release) one line gives the counts: `[rt] pipeline-order: requested N, already
ready M, median lead X ms`. "Lead" is first use minus creation. A PSO made from a pipeline state stream on the
console path is created when it is first bound, so its lead is the title's, not an upper bound on a head start.
A root signature the runtime does not know is hashed by its address, so the key of a pipeline made with one can
change from one run to the next. The file is local, like the cache files, and is not meant to be attached to a
report: quote the counts. It changes no build, order or cache.
A queued build holds no reference to its PSO or device (`PipelineCompiler`, `runtime/shims/d3d12_pipeline_compile.h`).
A PSO's last release drops its queued builds and returns at once; one already running is left to finish, and the
worker deletes the PSO after it (`PipelineState::last_release`). The PSO's device reference goes at the release, so
Expand Down
13 changes: 13 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,12 @@ if(BUILD_TESTING)
add_executable(pipeline_cache_store_test test/pipeline_cache_store_test.cpp)
add_test(NAME pipeline_cache_store COMMAND pipeline_cache_store_test)

# RT-PIPELINE-PREWARM-ORDER phase 1: the file format and the recorder of src/pipeline_order.h (pure stdlib, no GPU).
add_executable(pipeline_order_test test/pipeline_order_test.cpp)
target_link_libraries(pipeline_order_test PRIVATE Threads::Threads)
add_test(NAME pipeline_order COMMAND pipeline_order_test)
set_tests_properties(pipeline_order PROPERTIES TIMEOUT 120)

add_executable(heavy_pipeline_cache_test test/heavy_pipeline_cache_test.cpp)
add_test(NAME heavy_pipeline_cache COMMAND heavy_pipeline_cache_test)

Expand Down Expand Up @@ -4523,6 +4529,13 @@ if(TARGET Vulkan::Headers)
add_test(NAME replay_pipeline_compiles COMMAND replay_pipeline_compiles_test)
add_test(NAME replay_pipeline_compiles_strict COMMAND replay_pipeline_compiles_test)
set_tests_properties(replay_pipeline_compiles_strict PROPERTIES ENVIRONMENT "LEONIDA_STRICT_REPLAY=1")
# The request hook of ComputePipeline / GraphicsPipeline that pipeline_order.h's recorder hangs on (no GPU).
add_executable(replay_pipeline_order_hook_test test/replay_pipeline_order_hook_test.cpp)
target_link_libraries(replay_pipeline_order_hook_test PRIVATE Vulkan::Headers)
if(WIN32)
target_compile_definitions(replay_pipeline_order_hook_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
endif()
add_test(NAME replay_pipeline_order_hook COMMAND replay_pipeline_order_hook_test)
# Compiling unbuilt pipelines releases resource_mutex_ and tracker_mutex_ while keeping pipeline_mutex_ (no GPU).
add_executable(replay_pipeline_compile_unlock_test test/replay_pipeline_compile_unlock_test.cpp)
target_link_libraries(replay_pipeline_compile_unlock_test PRIVATE Vulkan::Headers Threads::Threads)
Expand Down
1 change: 1 addition & 0 deletions runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w
| `LEONIDA_LAZY_PIPELINES` | on | Graphics pipelines are built at their first draw. `0` builds each at `CreatePipelineState`, as before; the title creates far more pipelines at startup than it draws with. |
| `LEONIDA_LAZY_COMPUTE` | on | Compute pipelines are built at the first dispatch that names them, or earlier by the warm-up thread. `0` builds each at `CreateComputePipelineState`. |
| `LEONIDA_COMPUTE_WARMUP_MAX_BYTES` | `1048576` | The largest SPIR-V module, in bytes, the background warm-up thread (`LEONIDA_COMPUTE_WARMUP`) builds ahead of its first dispatch. Larger modules wait for that dispatch. |
| `LEONIDA_PIPELINE_ORDER_LOG` | unset (off) | `1` appends, for each pipeline and variant (the unbound-slot mask of a graphics pipeline) that a dispatch or draw names for the first time, one line to `pipeline_order.log` beside `pipeline_cache.session.bin`: the PSO's description key, the variant, when the title created the PSO and when it first used it (milliseconds since the process started), the build time and whether the handle was ready already. A line of counts is printed at exit (`[rt] pipeline-order: requested N, already ready M, median lead X ms`). The hook never blocks and allocates nothing; it takes only a non-blocking `try_lock`, on the first request of a pipeline and of a variant. Record only: nothing builds, orders or skips differently, and no cache file is touched. At most 8192 lines per run; a file past 8 MiB is set aside as `pipeline_order.log.prev`. The file holds hashes and numbers, no shader or game data, and stays local. See `docs/d3d12_vulkan.md`. |
| `LEONIDA_SERIAL_BIG_COMPILES` | unset | Large pipelines (compute modules over 128 KiB of SPIR-V, graphics pipelines of 1 MiB or more) compile side by side while their estimates, 512 bytes of memory for each byte of SPIR-V, fit a budget (`LEONIDA_BIG_COMPILE_BUDGET_MB`): one always does, what a submission waits for goes before the warm-up, and the warm-up takes a quarter of the budget at most. `1` compiles the compute ones one at a time, and the graphics ones of a submission one after another, as before there was a budget. `0` lets all of them compile side by side. |
| `LEONIDA_BIG_COMPILE_BUDGET_MB` | unset | The budget, in MiB: a fixed number (`0`: one large pipeline at a time) instead of three quarters of the memory that is free at the moment, which is the smaller of the free RAM and the room under the commit limit, read again for each pipeline. Text that is not a number is ignored. |
| `LEONIDA_CONSOLE_FORMATS` | on | Texture descriptions the console accepts and a PC GPU may not (an R9G9B9E5 render target, an sRGB RGBA8 or BGRA8 texture with unordered access) get a host format that can do the job. `0` refuses them, as a PC D3D12 runtime does. |
Expand Down
Loading
Loading