Skip to content
62 changes: 53 additions & 9 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2387,22 +2387,47 @@ Pipeline creation:
compute programs and drops their dispatches, direct and indirect; the first dropped dispatch of each is logged.
It is a way past a program that hangs the GPU while the program or its input is fixed, not a fix.
`LEONIDA_STARTUP_CACHE_CHECKPOINTS=1` also schedules a snapshot after a successful pipeline build,
at most once per 120 seconds. It starts on the compile worker when queued builds have drained and the device
has no active builds. These snapshots have an additional 512 MiB ceiling. The driver fills a mapped staging
at most once per 120 seconds. It starts on a compile worker when queued builds have drained, no worker is in a build
and the device has no active builds. These snapshots have an additional 512 MiB ceiling. The driver fills a mapped staging
file directly. A successful snapshot atomically replaces the session. An interrupted write preserves the
previous cache; the next launch removes unheld staging files older than ten minutes.
`LEONIDA_TRACE_PIPELINE_COMPILE=1` records each graphics and compute build's trigger, thread, elapsed time,
SPIR-V size, shader names and Vulkan application-cache feedback (1 hit, 0 miss, -1 unavailable). It also
times cache reads, merges, writes and draw/dispatch pipeline lookups that take at least 1 ms.
`LEONIDA_ASYNC_PIPELINES=1` schedules cold first-use builds on the warm-up worker ahead of speculative work.
`LEONIDA_ASYNC_PIPELINES=1` schedules cold first-use builds on the compile workers ahead of speculative work.
Replay skips a draw or dispatch while its pipeline is pending. Later uses find the completed handle.
A skipped dispatch can lose a one-time resource update. Synchronous first use is the default and strict
replay always uses it. `LEONIDA_COMPUTE_WARMUP=0` restricts the worker to pipelines requested by the title.
replay always uses it. `LEONIDA_COMPUTE_WARMUP=0` restricts the workers to pipelines requested by the title.
The compile workers (`PipelineCompiler`) run those builds, the warm-up and the cache checkpoint on
`LEONIDA_COMPILE_THREADS` threads below normal priority: unset, half the logical processors, at least 2 and at most
8; `1` is the one thread there was (RT-ASYNC-COMPILE-POOL). A worker takes the first job it may start: the builds a
draw or dispatch asked for in the order they came, then the warm-up's by module size, then the checkpoint. All but
one of the workers at most are in heavy builds at a time, which are the warm-up's and the demanded ones of large
pipelines (below; by size, whatever `LEONIDA_SERIAL_BIG_COMPILES` says), so one worker is left for a build that is
not heavy and a submission asked for: it no longer waits behind a module the driver takes minutes over, or behind
the warm-up. Light builds are not limited, so graphics pipelines below the large size (a mesh pipeline of a few
hundred KiB, say) can still occupy every worker at once. Of
the very large builds, 1 MiB of SPIR-V or more, one runs at a time, demanded or not
(`LEONIDA_COMPILE_HEAVY_MAX=<n>`; `0`: no limit of their own): the memory gate lets in what fits the free memory,
and a driver may fail modules it takes half an hour over when it compiles several side by side. A large build
waits for its place at the memory gate (below) inside its job, and counts as heavy while it waits. One PSO has one
build at a time, the next of its variants after the one that runs. A checkpoint starts only when no worker is in a
build, and gives way to one that starts while it reads the cache's size. With several workers the pipelines become
ready in an order that differs from run to run, and so do the draws skipped meanwhile; the driver holds memory for
each thread that compiles (about 50 MiB, below). The pool shortens the time a demanded pipeline is pending and does
not end it: a draw or dispatch is still skipped for as long as its own build takes. Measured with synthetic modules
no run had compiled (`d3d12_compile_pool_test measure 4194304 200 <seed>`; RTX 4060, NVIDIA 610.88, Windows 11, 12
logical processors, two alternating runs each, on a machine that was building other things): a dispatch asks for a
compute module of 4 MiB, which the driver takes 92 to 173 s over, and the next submission for 200 modules of
12 KiB. With 6 workers the 200 are ready 1.3 and 6.9 s after the first dispatch, 200 compiles on 5 other threads
inside the large one; with one thread, after 99 and 152 s, behind it. Peak commit 714 and 720 MiB against 645 and
681. Tested by `pipeline_compiler_pool` (the rules, with made-up jobs), `d3d12_compile_teardown` and
`d3d12_compile_pool` (a GPU; each with the default, 1 and 4 workers).
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
the release that tears the device down waits for that build, drops the device's cache checkpoint, and the last
device stops and joins the worker before any Vulkan object goes; the session cache is written after that. gta6rt,
device stops and joins the workers before any Vulkan object goes; the session cache is written after that. gta6rt,
which ends with the device alive, drops the queue and waits up to 2 s for a running build before `ExitProcess` and on
a console Ctrl+C, Ctrl+Break or close; after that nothing is queued, and a first use builds in place
(`d3d12_compile_teardown_test`).
Expand Down Expand Up @@ -2439,16 +2464,35 @@ Pipeline creation:
is when each is compiled alone, so a PSO 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` compiles each pipeline on the submitting thread where `prepare()` meets it.
Graphics pipelines of 1 MiB of SPIR-V or more, and compute pipelines over 128 KiB (`LEONIDA_SERIAL_BIG_COMPILES`),
are compiled one after another beside the smaller ones, because a compile takes the driver some hundred bytes of
memory for each byte of SPIR-V ([pipeline_check.md](pipeline_check.md)); so are the variants of one PSO.
`LEONIDA_ASYNC_PIPELINES=1` is not changed: its builds stay on the compile worker. Measured with synthetic programs
Large pipelines (below) are compiled within a memory budget, and the variants of one PSO one after another.
`LEONIDA_ASYNC_PIPELINES=1` is not changed: its builds stay on the compile workers (above). Measured with synthetic programs
never compiled before and the warm-up off (RTX 3080, NVIDIA 621.24, Windows 11, 16 logical processors, 8 workers,
three runs each): the first submission of 192 graphics pipelines takes 126 to 143 ms instead of 861 to 882, of 192
variants 124 to 130 instead of 856 to 863, of 96 compute pipelines 74 to 81 instead of 486 to 519, and the process
holds about 50 MiB more (the driver keeps memory for each thread that compiles). One `VkPipelineCache` is enough: one
cache per thread, merged, was about as fast. Tested by `pipeline_workers`, `replay_pipeline_compiles` and
`d3d12_pipeline_workers`.
- Large pipelines: a compile takes the driver some hundred bytes of memory for each byte of SPIR-V
([pipeline_check.md](pipeline_check.md), "Limiting concurrent compilation of large modules"), so compute modules over
128 KiB and graphics pipelines of 1 MiB or more compile side by side only while their estimates (512 bytes for each
byte of SPIR-V) fit a budget: three quarters of the smaller of the free RAM and the room under the commit limit, read
again whenever a place at the gate (`runtime/src/large_module_gate.h`, one for the process) is given or returned. One
always gets in when none is, so a pipeline larger than the budget compiles alone. An estimate counts in full until
the compile ends, although the driver has taken part of it by then. They get in in the order they came, the ones a
submission waits for (and those of `LEONIDA_ASYNC_PIPELINES`) before the warm-up's, which take a quarter of the
budget at most and none while a submission that has large ones is compiling on its workers. A thread that waits for
its place holds no PSO's lock, and one that has a place waits for none. `PipelineCompiles::build` gives a submission
as many jobs for large ones as its smallest could be in at once, largest first; where the next one does not fit, the
job compiles small ones in the meantime. `LEONIDA_SERIAL_BIG_COMPILES=1` compiles them one after another instead, as
before there was a budget, `=0` has no limit, `LEONIDA_BIG_COMPILE_BUDGET_MB` fixes the budget. Measured with
synthetic integer modules never compiled before and the warm-up off (RTX 3080, NVIDIA 621.24, Ryzen 7 5800X3D, 8
workers and the submitting thread, three alternating runs each): the first submission of 16 modules of 256 KiB takes
0.8 s instead of 4.0 to 4.8 s, 16 of 512 KiB 2.0 to 2.2 s instead of 10.2 to 11.4, 15 of 1 MiB 5.8 to 6.1 s instead
of 29.2 to 31.2, 12 of 0.5 to 2 MiB 8.0 to 10.5 s instead of 38.9 to 39.9. While the 15 of 1 MiB compile, private
memory peaks 656 to 676 MiB over what it was, instead of 140 to 147 (one compile takes 85 to 131 bytes for each byte
of SPIR-V here, and the driver keeps memory for each thread that compiles). With a budget of 1536 MiB, two at once,
they take 18.5 s and peak 202 MiB over. Not measured: other drivers, and the title. Tested by `big_compile_gate`,
`replay_pipeline_compiles` and `d3d12_big_compute`.

**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
2 changes: 1 addition & 1 deletion docs/pipeline_check.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ acceptance only.

A SPIR-V module larger than 4 MiB is "large". [#884](https://github.com/ItsNotRexed/Leonida/issues/884)
showed why that matters: several compiles that each needed gigabytes of host RAM ran at the same time. Large
modules now share a memory budget (`runtime/test/large_module_gate.h`): each one reserves 512 bytes of RAM per
modules now share a memory budget (`runtime/src/large_module_gate.h`): each one reserves 512 bytes of RAM per
SPIR-V byte, and they enter the driver while their reservations fit three quarters of the memory available when
the run starts, up to `--jobs` of them. One large module always gets in when none is compiling, so a module
bigger than the whole budget still compiles, alone among the large ones. Small modules are never held back. On
Expand Down
72 changes: 70 additions & 2 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,12 @@ if(BUILD_TESTING)
add_executable(xdxr_build_output_test test/xdxr_build_output_test.cpp)
add_test(NAME xdxr_build_output COMMAND xdxr_build_output_test)

# The gate of large_module_gate.h as the runtime uses it for large pipelines (LEONIDA_BIG_COMPILE_BUDGET_MB): pure
# stdlib.
add_executable(big_compile_gate_test test/big_compile_gate_test.cpp)
target_link_libraries(big_compile_gate_test PRIVATE Threads::Threads)
add_test(NAME big_compile_gate COMMAND big_compile_gate_test)
set_tests_properties(big_compile_gate PROPERTIES TIMEOUT 120)
# vk_pipeline_check's memory budget for large modules: pure stdlib, no Vulkan device.
add_executable(large_module_gate_test test/large_module_gate_test.cpp)
find_package(Threads REQUIRED)
Expand Down Expand Up @@ -2377,8 +2383,53 @@ if(TARGET Vulkan::Headers)
add_test(NAME d3d12_pipeline_workers_eager COMMAND d3d12_pipeline_workers_test)
set_tests_properties(d3d12_pipeline_workers d3d12_pipeline_workers_eager PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 180
FAIL_REGULAR_EXPRESSION "Validation Error;VUID-")
# One large pipeline after another: the jobs of the pool are then known (d3d12_big_compute has the budget).
set_tests_properties(d3d12_pipeline_workers PROPERTIES ENVIRONMENT "LEONIDA_SERIAL_BIG_COMPILES=1")
set_tests_properties(d3d12_pipeline_workers_eager PROPERTIES
ENVIRONMENT "LEONIDA_LAZY_PIPELINES=0;LEONIDA_LAZY_COMPUTE=0")
ENVIRONMENT "LEONIDA_LAZY_PIPELINES=0;LEONIDA_LAZY_COMPUTE=0;LEONIDA_SERIAL_BIG_COMPILES=1")
# Large pipelines compiled within a memory budget, with real large compute modules: the same values for each setting
# (read once for each process). 260 MiB holds three of the modules, 1 MiB none. The modules' compile runs on the test's
# threads, so they get the loader's stack (docs/pipeline_check.md, "Stack reserve").
add_executable(d3d12_big_compute_test test/d3d12_big_compute_test.cpp)
target_link_libraries(d3d12_big_compute_test PRIVATE gta6rt_d3d12_com)
target_compile_definitions(d3d12_big_compute_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(d3d12_big_compute_test PRIVATE /W4 /permissive- /utf-8)
target_link_options(d3d12_big_compute_test PRIVATE /STACK:${GTA6RT_STACK})
add_test(NAME d3d12_big_compute COMMAND d3d12_big_compute_test)
add_test(NAME d3d12_big_compute_budget COMMAND d3d12_big_compute_test)
add_test(NAME d3d12_big_compute_tight COMMAND d3d12_big_compute_test)
add_test(NAME d3d12_big_compute_serial COMMAND d3d12_big_compute_test)
add_test(NAME d3d12_big_compute_unlimited COMMAND d3d12_big_compute_test)
add_test(NAME d3d12_big_compute_warmup COMMAND d3d12_big_compute_test warmup)
add_test(NAME d3d12_big_compute_eager COMMAND d3d12_big_compute_test eager)
set_tests_properties(d3d12_big_compute d3d12_big_compute_budget d3d12_big_compute_tight d3d12_big_compute_serial
d3d12_big_compute_unlimited d3d12_big_compute_warmup d3d12_big_compute_eager
PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600
FAIL_REGULAR_EXPRESSION "Validation Error;VUID-")
set_tests_properties(d3d12_big_compute_budget PROPERTIES ENVIRONMENT "LEONIDA_BIG_COMPILE_BUDGET_MB=260")
set_tests_properties(d3d12_big_compute_tight d3d12_big_compute_warmup PROPERTIES
ENVIRONMENT "LEONIDA_BIG_COMPILE_BUDGET_MB=1")
set_tests_properties(d3d12_big_compute_eager PROPERTIES
ENVIRONMENT "LEONIDA_BIG_COMPILE_BUDGET_MB=1;LEONIDA_LAZY_COMPUTE=0")
set_tests_properties(d3d12_big_compute_serial PROPERTIES # neither of these two has a budget
ENVIRONMENT "LEONIDA_SERIAL_BIG_COMPILES=1;LEONIDA_BIG_COMPILE_BUDGET_MB=4096")
set_tests_properties(d3d12_big_compute_unlimited PROPERTIES
ENVIRONMENT "LEONIDA_SERIAL_BIG_COMPILES=0;LEONIDA_BIG_COMPILE_BUDGET_MB=1")
# The pipeline compiler's workers under LEONIDA_ASYNC_PIPELINES=1, with a large compute module's build held in flight:
# with more than one worker the small pipelines a submission asks for are built meanwhile, with one they wait, as
# before the pool. The builds run on the compiler's threads, which get the loader's stack as the title's do.
add_executable(d3d12_compile_pool_test test/d3d12_compile_pool_test.cpp)
target_link_libraries(d3d12_compile_pool_test PRIVATE gta6rt_d3d12_com psapi)
target_compile_definitions(d3d12_compile_pool_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(d3d12_compile_pool_test PRIVATE /W4 /permissive- /utf-8)
target_link_options(d3d12_compile_pool_test PRIVATE /STACK:${GTA6RT_STACK})
add_test(NAME d3d12_compile_pool COMMAND d3d12_compile_pool_test)
add_test(NAME d3d12_compile_pool_one_thread COMMAND d3d12_compile_pool_test)
add_test(NAME d3d12_compile_pool_four_threads COMMAND d3d12_compile_pool_test)
set_tests_properties(d3d12_compile_pool d3d12_compile_pool_one_thread d3d12_compile_pool_four_threads PROPERTIES
SKIP_RETURN_CODE 77 TIMEOUT 600 FAIL_REGULAR_EXPRESSION "Validation Error;VUID-")
set_tests_properties(d3d12_compile_pool_one_thread PROPERTIES ENVIRONMENT "LEONIDA_COMPILE_THREADS=1")
set_tests_properties(d3d12_compile_pool_four_threads PROPERTIES ENVIRONMENT "LEONIDA_COMPILE_THREADS=4")
# ExecuteCommandLists refuses a list whose type isn't the queue's (debug-layer message 836).
add_executable(d3d12_queue_list_type_test test/d3d12_queue_list_type_test.cpp)
target_link_libraries(d3d12_queue_list_type_test PRIVATE gta6rt_d3d12_com)
Expand Down Expand Up @@ -3628,8 +3679,15 @@ if(TARGET Vulkan::Headers)
target_compile_definitions(d3d12_compile_teardown_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX
LEONIDA_TEST_CS_SPV="${LEONIDA_TEST_CS}")
target_compile_options(d3d12_compile_teardown_test PRIVATE /W4 /permissive- /utf-8)
# Three times: with the compiler's default number of workers, with the one thread there was before the pool,
# and with 4, which is more than the default of a machine with fewer than 8 logical processors.
add_test(NAME d3d12_compile_teardown COMMAND d3d12_compile_teardown_test)
set_tests_properties(d3d12_compile_teardown PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 300)
add_test(NAME d3d12_compile_teardown_one_thread COMMAND d3d12_compile_teardown_test)
add_test(NAME d3d12_compile_teardown_four_threads COMMAND d3d12_compile_teardown_test)
set_tests_properties(d3d12_compile_teardown d3d12_compile_teardown_one_thread d3d12_compile_teardown_four_threads
PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 300)
set_tests_properties(d3d12_compile_teardown_one_thread PROPERTIES ENVIRONMENT "LEONIDA_COMPILE_THREADS=1")
set_tests_properties(d3d12_compile_teardown_four_threads PROPERTIES ENVIRONMENT "LEONIDA_COMPILE_THREADS=4")
# A buffer at the title's own VA (device slot 133) read by the same shader through a root view and a V#.
add_executable(d3d12_title_va_test test/d3d12_title_va_test.cpp shims/d3d12_xs_device.cpp
test/xmem_geometry_aliases_stub.cpp ${LEONIDA_TEST_CS})
Expand Down Expand Up @@ -4258,6 +4316,16 @@ 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")
if(WIN32)
# The rules of the workers that run LEONIDA_ASYNC_PIPELINES builds, the warm-up and the cache checkpoint
# (shims/d3d12_pipeline_compile.h), with made-up jobs: no Vulkan device, no GPU.
add_executable(pipeline_compiler_pool_test test/pipeline_compiler_pool_test.cpp)
target_link_libraries(pipeline_compiler_pool_test PRIVATE Vulkan::Headers Threads::Threads)
target_compile_definitions(pipeline_compiler_pool_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX)
target_compile_options(pipeline_compiler_pool_test PRIVATE /W4 /permissive- /utf-8)
add_test(NAME pipeline_compiler_pool COMMAND pipeline_compiler_pool_test)
set_tests_properties(pipeline_compiler_pool PROPERTIES TIMEOUT 180)
endif()
add_executable(replay_quad_list_test test/replay_quad_list_test.cpp)
target_link_libraries(replay_quad_list_test PRIVATE Vulkan::Headers)
if(WIN32)
Expand Down
Loading
Loading