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
11 changes: 11 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2950,6 +2950,17 @@ submitting thread; at the maintainers' decision it was replaced by this one when
still goes through the compile gate and `LargeModuleGate` as before, so a smaller one a submission waits for can
queue behind it there; `async_pipeline_size_test` covers the decision and `d3d12_m4_compute_async_small_waits` a
submission whose two pipelines are under the threshold with the worker held (both dispatches run).
`LEONIDA_ASYNC_MIN_BYTES_GRAPHICS=<n>` gives graphics and mesh pipelines (all their stages) a threshold of their
own; unset, they take `LEONIDA_ASYNC_MIN_BYTES`, as before the variable, and compute always does. `0` makes every
graphics pipeline asynchronous, as `LEONIDA_ASYNC_PIPELINES=1` did on its own. The reason: @MoonEyes's thread dumps
(RTX 4060, `LEONIDA_ASYNC_MIN_BYTES=1048576`) showed the draw thread building lazy graphics variants under 1 MiB in
place (`PipelineState::variant` -> `build` -> `vkCreateGraphicsPipelines`, 2.5-11 s each, back to back), so no
present came for about 100 s. A graphics pipeline that is not built yet costs one skipped draw, while the image
needs the small lighting and post compute programs, so graphics can stay asynchronous while those are built where
they are needed. One startup line states both thresholds. `d3d12_pipeline_workers_async_graphics_zero` holds the
compile worker with compute over its threshold and graphics at `0`: every dispatch runs, every draw is skipped, and
the draws come once the worker is let go; `d3d12_pipeline_workers_async_graphics_unset` checks that graphics follow
`LEONIDA_ASYNC_MIN_BYTES` without the variable.
- **Warm-ups while the title submits.** A compute warm-up holds the compile gate for its whole create, and every
submission waits behind it: a native boot's submissions waited 140 s in all at the gate behind compute warm-ups, and
past the story load the worker went through 694 warm-ups of modules over 128 KiB (those over 640 KiB take 4-25 s
Expand Down
3 changes: 2 additions & 1 deletion docs/running.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ Reported by others: [#1608, comment 7184700506](https://github.com/ubiquitariani
Between the two: `LEONIDA_ASYNC_PIPELINES=1` with `LEONIDA_ASYNC_MIN_BYTES=2097152` builds only the pipelines of
2 MiB of SPIR-V or more asynchronously (the ray-tracing programs above), and the smaller lighting and post programs
where they are needed, as with `=0` ([d3d12_vulkan.md](d3d12_vulkan.md), "Only the large ones asynchronous"). Whether
that draws the world without the skip list: TBD.
that draws the world without the skip list: TBD. `LEONIDA_ASYNC_MIN_BYTES_GRAPHICS=0` with it leaves every graphics
pipeline asynchronous, so the draw thread does not stop presenting to build graphics variants in place.
- **Command:** TBD.
- **What to expect:** the opening night street from about 650 s, at 0.1-0.2 frames/s, no device loss through 934 s.
The black stretch before it is graphics pipelines of 1-2.6 MB SPIR-V compiling one at a time, 5-10 s each.
Expand Down
9 changes: 8 additions & 1 deletion runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ 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)

# Which pipelines LEONIDA_ASYNC_PIPELINES=1 builds asynchronously by size (LEONIDA_ASYNC_MIN_BYTES,
# Which pipelines LEONIDA_ASYNC_PIPELINES=1 builds asynchronously by size (LEONIDA_ASYNC_MIN_BYTES and _GRAPHICS,
# src/async_pipeline_size.h): pure stdlib.
add_executable(async_pipeline_size_test test/async_pipeline_size_test.cpp)
add_test(NAME async_pipeline_size COMMAND async_pipeline_size_test)
Expand Down Expand Up @@ -2645,6 +2645,13 @@ if(TARGET Vulkan::Headers)
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;LEONIDA_SERIAL_BIG_COMPILES=1")
# LEONIDA_ASYNC_PIPELINES=1 with LEONIDA_ASYNC_MIN_BYTES over every module and the compile worker held: with
# LEONIDA_ASYNC_MIN_BYTES_GRAPHICS=0 the draws are skipped while every dispatch builds its pipeline in place; unset,
# the draws build theirs in place too (src/async_pipeline_size.h).
add_test(NAME d3d12_pipeline_workers_async_graphics_zero COMMAND d3d12_pipeline_workers_test --async-graphics-zero)
add_test(NAME d3d12_pipeline_workers_async_graphics_unset COMMAND d3d12_pipeline_workers_test --async-graphics-unset)
set_tests_properties(d3d12_pipeline_workers_async_graphics_zero d3d12_pipeline_workers_async_graphics_unset
PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 180 FAIL_REGULAR_EXPRESSION "Validation Error;VUID-")
# 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").
Expand Down
1 change: 1 addition & 0 deletions runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,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_ASYNC_MIN_BYTES` | unset | `<n>`, with `LEONIDA_ASYNC_PIPELINES=1`: only a pipeline of n bytes of SPIR-V or more (compute: its module; graphics: all its stages) is built asynchronously, its draws or dispatches skipped until the compile worker has it; a smaller one is built where it is needed, as with asynchronous builds off, so small lighting and post programs are not skipped while the large ray-tracing ones compile. Unset or `0`: every pipeline is asynchronous. Text that is not a decimal number is ignored, with a log line. ([docs/d3d12_vulkan.md](../docs/d3d12_vulkan.md), "Only the large ones asynchronous") |
| `LEONIDA_ASYNC_MIN_BYTES_GRAPHICS` | unset | `<n>`, with `LEONIDA_ASYNC_PIPELINES=1`: the threshold of `LEONIDA_ASYNC_MIN_BYTES` for graphics and mesh pipelines only (all their stages), so a draw can wait for a different size than a dispatch. `0`: every graphics pipeline is asynchronous (its draws skipped until built) while compute keeps `LEONIDA_ASYNC_MIN_BYTES`. Unset: graphics take `LEONIDA_ASYNC_MIN_BYTES`, as before the variable. Text that is not a decimal number is ignored, with a log line. ([docs/d3d12_vulkan.md](../docs/d3d12_vulkan.md), "Only the large ones asynchronous") |
| `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_PIPELINE_PREWARM_ORDER` | unset (off) | `<file>`: an order log written with `LEONIDA_PIPELINE_ORDER_LOG=1` in an earlier run; `1`: `pipeline_order.log` beside the session cache. The graphics and mesh pipelines that run asked for first are queued, when the title creates them, as warm-ups on the compile workers in the recorded order: after every demanded build, one at a time and none while a demanded build runs. The run read is this GPU's with the most records. With `LEONIDA_PIPELINE_COMPILE=on-demand`, a console stream in the plan is parsed by its first prewarm job rather than at its first bind. A missing, garbled or other-GPU file is ignored with one `[rt] pipeline-prewarm:` line. See `docs/d3d12_vulkan.md`. |
Expand Down
46 changes: 31 additions & 15 deletions runtime/shims/d3d12_pipeline_compile.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,44 @@ inline bool async_replay_compile() {

// LEONIDA_ASYNC_MIN_BYTES=<n> (with LEONIDA_ASYNC_PIPELINES=1): only pipelines with n bytes of SPIR-V or more are built
// asynchronously; a smaller one is built where it is needed, as with asynchronous builds off. Unset or 0: every
// pipeline, as before the variable (src/async_pipeline_size.h).
inline std::uint64_t async_min_bytes() {
static const std::uint64_t bytes = [] {
char text[32]{};
const DWORD n = GetEnvironmentVariableA("LEONIDA_ASYNC_MIN_BYTES", text, sizeof(text));
bool valid = n < sizeof(text);
const std::uint64_t value = valid ? leonida::async_pipelines::parse_min_bytes(text, &valid) : 0;
if (!valid)
// pipeline, as before the variable (src/async_pipeline_size.h). LEONIDA_ASYNC_MIN_BYTES_GRAPHICS=<n> gives graphics and
// mesh pipelines their own threshold (0: every one asynchronous); unset, they take LEONIDA_ASYNC_MIN_BYTES's.
inline const leonida::async_pipelines::MinBytes& async_min_bytes() {
static const leonida::async_pipelines::MinBytes thresholds = [] {
// Text of a variable, or null when it is unset or empty; `valid` false when it is too long to be a number.
const auto read = [](const char* name, char (&text)[32], bool& valid) -> const char* {
const DWORD n = GetEnvironmentVariableA(name, text, sizeof(text));
valid = n < sizeof(text);
return valid && n ? text : nullptr;
};
char all_text[32]{}, graphics_text[32]{};
bool all_fits = true, graphics_fits = true, all_valid = true, graphics_valid = true;
const char* all = read("LEONIDA_ASYNC_MIN_BYTES", all_text, all_fits);
const char* graphics = read("LEONIDA_ASYNC_MIN_BYTES_GRAPHICS", graphics_text, graphics_fits);
// Text that is not a number counts as unset: 0 for LEONIDA_ASYNC_MIN_BYTES, compute's for the graphics one.
const auto value = leonida::async_pipelines::parse_thresholds(all, graphics, &all_valid, &graphics_valid);
if (!all_fits || !all_valid)
std::fprintf(stderr, "[rt] d3d12: LEONIDA_ASYNC_MIN_BYTES: not a number of bytes, ignored (every "
"asynchronous pipeline stays asynchronous)\n");
else if (value && async_replay_compile())
std::fprintf(stderr, "[rt] d3d12: LEONIDA_ASYNC_MIN_BYTES: pipelines under %llu bytes of SPIR-V are built "
"where they are needed; larger ones asynchronously\n", (unsigned long long)value);
if (!graphics_fits || !graphics_valid)
std::fprintf(stderr, "[rt] d3d12: LEONIDA_ASYNC_MIN_BYTES_GRAPHICS: not a number of bytes, ignored "
"(graphics pipelines take LEONIDA_ASYNC_MIN_BYTES)\n");
if ((value.compute || value.graphics_set) && async_replay_compile())
std::fprintf(stderr, "[rt] d3d12: LEONIDA_ASYNC_MIN_BYTES: compute pipelines under %llu bytes of SPIR-V and "
"graphics pipelines under %llu (%s) are built where they are needed; larger ones "
"asynchronously (0: every one)\n",
(unsigned long long)value.compute, (unsigned long long)value.graphics,
value.graphics_set ? "LEONIDA_ASYNC_MIN_BYTES_GRAPHICS"
: "LEONIDA_ASYNC_MIN_BYTES_GRAPHICS unset, the same");
return value;
}();
return bytes;
return thresholds;
}

// Whether a pipeline of `spirv_bytes` (compute: its module; graphics: all its stages) is built asynchronously.
inline bool async_replay_compile(std::uint64_t spirv_bytes) {
// Whether a pipeline of `spirv_bytes` (compute: its module; graphics and mesh: all its stages) is built asynchronously.
inline bool async_replay_compile(bool graphics, std::uint64_t spirv_bytes) {
return async_replay_compile() &&
leonida::async_pipelines::asynchronous(true, async_min_bytes(), spirv_bytes);
leonida::async_pipelines::asynchronous(true, async_min_bytes(), graphics, spirv_bytes);
}

// LOCAL FIX (post-load stalls): LEONIDA_FAST_PIPELINES=graphics, compute, or all (1 = all) creates those pipelines with
Expand Down
7 changes: 4 additions & 3 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12492,8 +12492,9 @@ class PipelineState final
// A cache-only create that the driver would have to compile stays null: the first draw builds it.
if (!graphics_.pipeline && !(cache_only && compile_status == VK_PIPELINE_COMPILE_REQUIRED)) return E_FAIL;
} // lazy: graphics_.pipeline stays null and the first draw builds it as variant 0
// LEONIDA_ASYNC_MIN_BYTES: a pipeline smaller than that (all its stages) is built where a draw needs it.
graphics_.async_build = async_replay_compile(spirv_bytes);
// LEONIDA_ASYNC_MIN_BYTES_GRAPHICS (unset: LEONIDA_ASYNC_MIN_BYTES): a pipeline smaller than that (all its
// stages) is built where a draw needs it.
graphics_.async_build = async_replay_compile(true, spirv_bytes);
graphics_.variant = [this](std::uint32_t unbound) {
return replay_pipeline("draw", [&] {
return graphics_.async_build ? queue_build(false, unbound) : variant(unbound, "draw");
Expand Down Expand Up @@ -12662,7 +12663,7 @@ class PipelineState final
pipeline_.build_context = this;
} else if (lazy_compute) {
// LEONIDA_ASYNC_MIN_BYTES: a module smaller than that is built where a dispatch needs it.
pipeline_.async_build = async_replay_compile(spirv_bytes);
pipeline_.async_build = async_replay_compile(false, spirv_bytes);
pipeline_.build = [](void* self) {
auto* pso = static_cast<PipelineState*>(self);
return replay_pipeline("dispatch", [&] {
Expand Down
31 changes: 31 additions & 0 deletions runtime/src/async_pipeline_size.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
// none, a submission waits minutes for each large ray-tracing program. LEONIDA_ASYNC_MIN_BYTES=<n> splits the two: a
// pipeline with less than n bytes of SPIR-V (compute: its module; graphics: all its stages) is built where it is
// needed, as without LEONIDA_ASYNC_PIPELINES, and only one of n bytes or more is asynchronous.
// LEONIDA_ASYNC_MIN_BYTES_GRAPHICS=<n> sets the graphics (and mesh) pipelines' threshold apart from compute's: a draw
// whose pipeline is skipped loses one draw, while the lighting and post compute programs make the image, so graphics
// can be left asynchronous (0) while small compute programs are built in place. Unset: graphics take
// LEONIDA_ASYNC_MIN_BYTES, as before the variable.
#pragma once

#include <cstdint>
Expand All @@ -31,10 +35,37 @@ inline std::uint64_t parse_min_bytes(const char* text, bool* valid = nullptr) {
return value;
}

// Both thresholds, as read from the environment: `compute` is LEONIDA_ASYNC_MIN_BYTES, `graphics` is
// LEONIDA_ASYNC_MIN_BYTES_GRAPHICS or, when that is unset, empty or not a number, the same as `compute`.
struct MinBytes {
std::uint64_t compute = 0;
std::uint64_t graphics = 0;
bool graphics_set = false; // LEONIDA_ASYNC_MIN_BYTES_GRAPHICS gave the graphics threshold
};

// `all_text`: LEONIDA_ASYNC_MIN_BYTES; `graphics_text`: LEONIDA_ASYNC_MIN_BYTES_GRAPHICS (null when unset). The
// `valid` flags (when given) say whether each text was usable; an unset or empty one is valid.
inline MinBytes parse_thresholds(const char* all_text, const char* graphics_text, bool* all_valid = nullptr,
bool* graphics_valid = nullptr) {
MinBytes out;
out.compute = parse_min_bytes(all_text, all_valid);
bool valid = true;
const std::uint64_t graphics = parse_min_bytes(graphics_text, &valid);
if (graphics_valid) *graphics_valid = valid;
out.graphics_set = valid && graphics_text && *graphics_text;
out.graphics = out.graphics_set ? graphics : out.compute;
return out;
}

// Whether a pipeline of `spirv_bytes` is built asynchronously: only with asynchronous builds on, and then when no
// threshold is set (0) or the pipeline is at least that large.
inline bool asynchronous(bool async_on, std::uint64_t min_bytes, std::uint64_t spirv_bytes) {
return async_on && (min_bytes == 0 || spirv_bytes >= min_bytes);
}

// The same for a graphics (or mesh) pipeline, whose size is all its stages, or a compute one (its module).
inline bool asynchronous(bool async_on, const MinBytes& thresholds, bool graphics, std::uint64_t spirv_bytes) {
return asynchronous(async_on, graphics ? thresholds.graphics : thresholds.compute, spirv_bytes);
}

} // namespace leonida::async_pipelines
Loading
Loading