diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 50dd51614b..bd9f62ec04 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -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=` 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 diff --git a/docs/running.md b/docs/running.md index f1e46966e2..3b71966d95 100644 --- a/docs/running.md +++ b/docs/running.md @@ -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. diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 46ea96aaf7..880d8f26cf 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -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) @@ -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"). diff --git a/runtime/README.md b/runtime/README.md index bd9105ea14..90c040c05c 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -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 | ``, 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 | ``, 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) | ``: 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`. | diff --git a/runtime/shims/d3d12_pipeline_compile.h b/runtime/shims/d3d12_pipeline_compile.h index 7fb47b72fa..674fba6938 100644 --- a/runtime/shims/d3d12_pipeline_compile.h +++ b/runtime/shims/d3d12_pipeline_compile.h @@ -38,28 +38,44 @@ inline bool async_replay_compile() { // LEONIDA_ASYNC_MIN_BYTES= (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= 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 diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 031fa22e12..e5cbcdee11 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -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"); @@ -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(self); return replay_pipeline("dispatch", [&] { diff --git a/runtime/src/async_pipeline_size.h b/runtime/src/async_pipeline_size.h index 3146224c1a..a4b4cabded 100644 --- a/runtime/src/async_pipeline_size.h +++ b/runtime/src/async_pipeline_size.h @@ -7,6 +7,10 @@ // none, a submission waits minutes for each large ray-tracing program. LEONIDA_ASYNC_MIN_BYTES= 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= 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 @@ -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 diff --git a/runtime/test/async_pipeline_size_test.cpp b/runtime/test/async_pipeline_size_test.cpp index df231a7f4a..a1dc02af68 100644 --- a/runtime/test/async_pipeline_size_test.cpp +++ b/runtime/test/async_pipeline_size_test.cpp @@ -1,5 +1,6 @@ -// Which pipelines LEONIDA_ASYNC_PIPELINES=1 builds asynchronously by size, LEONIDA_ASYNC_MIN_BYTES -// (runtime/src/async_pipeline_size.h): the threshold's parsing and the decision. Pure stdlib, no Vulkan. +// Which pipelines LEONIDA_ASYNC_PIPELINES=1 builds asynchronously by size, LEONIDA_ASYNC_MIN_BYTES and +// LEONIDA_ASYNC_MIN_BYTES_GRAPHICS (runtime/src/async_pipeline_size.h): the thresholds' parsing, the graphics one's +// fallback to LEONIDA_ASYNC_MIN_BYTES, and the decision for each kind of pipeline. Pure stdlib, no Vulkan. #include "../src/async_pipeline_size.h" #include @@ -18,7 +19,9 @@ int failures = 0; } while (0) using leonida::async_pipelines::asynchronous; +using leonida::async_pipelines::MinBytes; using leonida::async_pipelines::parse_min_bytes; +using leonida::async_pipelines::parse_thresholds; void parsing() { bool valid = false; @@ -63,11 +66,87 @@ void decision() { CHECK(asynchronous(true, UINT64_MAX, UINT64_MAX)); } +// LEONIDA_ASYNC_MIN_BYTES_GRAPHICS: its own threshold for graphics when set, LEONIDA_ASYNC_MIN_BYTES's otherwise. +void graphics_thresholds() { + bool all_valid = false, graphics_valid = false; + // Both unset: no threshold for either, as before both variables. + MinBytes t = parse_thresholds(nullptr, nullptr, &all_valid, &graphics_valid); + CHECK(t.compute == 0 && t.graphics == 0 && !t.graphics_set && all_valid && graphics_valid); + // Graphics unset or empty: the same threshold as compute (behaviour unchanged from LEONIDA_ASYNC_MIN_BYTES alone). + for (const char* graphics : {static_cast(nullptr), ""}) { + t = parse_thresholds("1048576", graphics, &all_valid, &graphics_valid); + CHECK(t.compute == 1048576 && t.graphics == 1048576 && !t.graphics_set && all_valid && graphics_valid); + } + // Graphics 0: every graphics pipeline asynchronous, compute keeps its threshold. + t = parse_thresholds("1048576", "0", &all_valid, &graphics_valid); + CHECK(t.compute == 1048576 && t.graphics == 0 && t.graphics_set && all_valid && graphics_valid); + // Graphics set without the general one: compute has no threshold (every compute pipeline asynchronous). + t = parse_thresholds(nullptr, "2097152", &all_valid, &graphics_valid); + CHECK(t.compute == 0 && t.graphics == 2097152 && t.graphics_set && all_valid && graphics_valid); + // Graphics higher than compute. + t = parse_thresholds("4096", "18446744073709551615", &all_valid, &graphics_valid); + CHECK(t.compute == 4096 && t.graphics == UINT64_MAX && t.graphics_set); + // Graphics text that is not a number: reported, and graphics fall back to LEONIDA_ASYNC_MIN_BYTES. + for (const char* text : {"1M", "-1", "0x10", " 0", "abc", "18446744073709551616"}) { + t = parse_thresholds("1048576", text, &all_valid, &graphics_valid); + CHECK(t.compute == 1048576 && t.graphics == 1048576 && !t.graphics_set && all_valid && !graphics_valid); + if (graphics_valid) std::fprintf(stderr, " accepted: \"%s\"\n", text); + } + // The general one not a number: compute has no threshold, and an unset graphics one follows it. + t = parse_thresholds("2M", nullptr, &all_valid, &graphics_valid); + CHECK(t.compute == 0 && t.graphics == 0 && !t.graphics_set && !all_valid && graphics_valid); + t = parse_thresholds("2M", "0", &all_valid, &graphics_valid); + CHECK(t.compute == 0 && t.graphics == 0 && t.graphics_set && !all_valid && graphics_valid); + t = parse_thresholds("2M", "65536", &all_valid, &graphics_valid); + CHECK(t.compute == 0 && t.graphics == 65536 && t.graphics_set && !all_valid && graphics_valid); + // The flags are optional. + t = parse_thresholds("8", "16"); + CHECK(t.compute == 8 && t.graphics == 16 && t.graphics_set); +} + +// The decision per kind: a graphics pipeline (all its stages) against the graphics threshold, a compute one (its +// module) against LEONIDA_ASYNC_MIN_BYTES. +void per_kind_decision() { + constexpr std::uint64_t kib = 1024, mib = kib * kib; + // Graphics unset: both kinds decide exactly as asynchronous(on, LEONIDA_ASYNC_MIN_BYTES, bytes) did. + for (const char* all : {static_cast(nullptr), "0", "1", "1048576", "2097152", "18446744073709551615"}) { + const MinBytes t = parse_thresholds(all, nullptr); + const std::uint64_t threshold = parse_min_bytes(all); + for (bool on : {false, true}) + for (std::uint64_t bytes : {std::uint64_t{0}, std::uint64_t{4}, 512 * kib, mib - 1, mib, 2 * mib, 26 * mib}) + for (bool graphics : {false, true}) + CHECK(asynchronous(on, t, graphics, bytes) == asynchronous(on, threshold, bytes)); + } + // LEONIDA_ASYNC_MIN_BYTES=1 MiB with graphics 0 (the draw-thread case): every graphics pipeline asynchronous, the + // smallest included; a compute pipeline under 1 MiB (lighting, post) is built in place, one of 1 MiB or more is not. + const MinBytes split = parse_thresholds("1048576", "0"); + for (std::uint64_t bytes : {std::uint64_t{0}, std::uint64_t{4}, 64 * kib, mib - 1, mib, 2600 * kib}) + CHECK(asynchronous(true, split, true, bytes)); + CHECK(!asynchronous(true, split, false, 4)); + CHECK(!asynchronous(true, split, false, 512 * kib)); + CHECK(!asynchronous(true, split, false, mib - 1)); + CHECK(asynchronous(true, split, false, mib)); + CHECK(asynchronous(true, split, false, 26 * mib)); + // Asynchronous builds off: nothing, whatever the thresholds. + for (std::uint64_t bytes : {std::uint64_t{0}, std::uint64_t{4}, 26 * mib}) { + CHECK(!asynchronous(false, split, true, bytes)); + CHECK(!asynchronous(false, split, false, bytes)); + } + // A graphics threshold over compute's: graphics under it wait while compute of the same size is asynchronous. + const MinBytes high = parse_thresholds("65536", "4194304"); + CHECK(!asynchronous(true, high, true, 2 * mib)); + CHECK(asynchronous(true, high, false, 2 * mib)); + CHECK(asynchronous(true, high, true, 4 * mib)); + CHECK(!asynchronous(true, high, false, 4 * kib)); +} + } // namespace int main() { parsing(); decision(); + graphics_thresholds(); + per_kind_decision(); if (failures) { std::fprintf(stderr, "async_pipeline_size_test: %d failure(s)\n", failures); return 1; diff --git a/runtime/test/d3d12_m4_compute_test.cpp b/runtime/test/d3d12_m4_compute_test.cpp index b620f1cae4..da5eea7bea 100644 --- a/runtime/test/d3d12_m4_compute_test.cpp +++ b/runtime/test/d3d12_m4_compute_test.cpp @@ -436,8 +436,9 @@ int main(int argc, char** argv) { if (jobs) std::fprintf(stderr, "%u build(s) went to the compile worker under LEONIDA_ASYNC_MIN_BYTES\n", jobs); EXPECT(jobs == 0); // Both modules are under the threshold, so neither is asynchronous. - EXPECT(!rt::d3d12::async_replay_compile(library.spirv_bytes(*library.find("cs", leonida::sha256_hex(fill))))); - EXPECT(!rt::d3d12::async_replay_compile( + EXPECT(!rt::d3d12::async_replay_compile(false, + library.spirv_bytes(*library.find("cs", leonida::sha256_hex(fill))))); + EXPECT(!rt::d3d12::async_replay_compile(false, library.spirv_bytes(*library.find("cs", leonida::sha256_hex(indirect))))); } diff --git a/runtime/test/d3d12_pipeline_workers_test.cpp b/runtime/test/d3d12_pipeline_workers_test.cpp index 5c8e5556b5..6dab42b4ae 100644 --- a/runtime/test/d3d12_pipeline_workers_test.cpp +++ b/runtime/test/d3d12_pipeline_workers_test.cpp @@ -15,12 +15,15 @@ // With LEONIDA_LAZY_PIPELINES=0 and LEONIDA_LAZY_COMPUTE=0 the pipelines are compiled when they are created and only the // variants are left to the submission; the cases that need more than that are then left out. // The calls that create pipelines and read the cache are watched, and held back, through the device's vkGetDeviceProcAddr. +// --async-graphics-zero and --async-graphics-unset: LEONIDA_ASYNC_PIPELINES=1 with LEONIDA_ASYNC_MIN_BYTES over every +// module, and LEONIDA_ASYNC_MIN_BYTES_GRAPHICS 0 or unset, with the compile worker held (async_thresholds below). // Self-contained: builds its SPIR-V in memory. Set LEONIDA_VK_VALIDATION=1 to run under VK_LAYER_KHRONOS_validation. #include "vk_test_device.h" #include "d3d12_test_shaders.h" #include "test_temp_dir.h" #include "../shims/d3d12_com.h" #include "../shims/d3d12_xs_device.h" +#include "../shims/d3d12_pipeline_compile.h" #include "../src/shader_library.h" #include @@ -45,6 +48,7 @@ namespace { using namespace std::chrono_literals; +using Clock = std::chrono::steady_clock; using d3d12test::Ref; using leonida::ReplayResult; using Words = std::vector; @@ -934,6 +938,83 @@ bool prewarm_from_recorded_order(const Setup& setup) { return true; } +// ---- LEONIDA_ASYNC_MIN_BYTES_GRAPHICS (src/async_pipeline_size.h), with LEONIDA_ASYNC_PIPELINES=1. ---- + +// --async-graphics-zero: LEONIDA_ASYNC_MIN_BYTES over every module and LEONIDA_ASYNC_MIN_BYTES_GRAPHICS=0, with every +// job of the compile worker held. The first submission builds each compute pipeline where its dispatch needs it (every +// buffer word is written) and no graphics one: their draws are skipped, so the image stays as cleared, and their +// builds wait on the worker. Once the worker is let go, a later submission draws every cell. +// --async-graphics-unset: the same with LEONIDA_ASYNC_MIN_BYTES_GRAPHICS unset. Graphics pipelines then take +// LEONIDA_ASYNC_MIN_BYTES and are built in place too: the first submission draws everything and the worker gets nothing. +bool async_thresholds(const Setup& setup, bool graphics_async) { + CHECK(first_use); // lazy pipelines: nothing is built at creation + CHECK(rt::d3d12::async_replay_compile()); + CHECK(rt::d3d12::async_min_bytes().compute == 1073741824); + CHECK(rt::d3d12::async_min_bytes().graphics == (graphics_async ? 0u : 1073741824u)); + Ref dev; + CHECK(setup.device("0", dev)); + Pipelines pipelines; + Frame frame; + // Declared after the device and the pipelines, so it lets the worker go before they are released, also when a + // check fails. + std::atomic worker_jobs{0}; + struct HeldWorker { + HANDLE release = nullptr; + void let_go() { + if (!release) return; + SetEvent(release); + rt::d3d12::PipelineCompiler::instance().set_job_probe(nullptr); + } + ~HeldWorker() { + let_go(); + if (release) CloseHandle(release); + } + } held; + held.release = CreateEventW(nullptr, TRUE, FALSE, nullptr); + CHECK(held.release != nullptr); + rt::d3d12::PipelineCompiler::instance().set_job_probe([&worker_jobs, release = held.release](const void*) { + worker_jobs.fetch_add(1); + WaitForSingleObject(release, INFINITE); + }); + hooks.reset(); + CHECK(pipelines.create(dev.p) && frame.create(dev.p, pipelines)); + CHECK(hooks.creates == 0); + CHECK(frame.submit() == ReplayResult::success); + std::vector bytes = frame.read(); + CHECK(bytes.size() == 4 * kExtent * kExtent + 4 * kCompute); + if (!graphics_async) { + CHECK(expected(bytes)); + CHECK(hooks.graphics == int(kGraphics) && hooks.compute == int(kCompute)); + CHECK(worker_jobs == 0); + return true; + } + // Every compute pipeline was built in place (the worker is held), no graphics one reached the driver. + for (unsigned n = 0; n < kCompute; ++n) { + std::uint32_t value = 0; + std::memcpy(&value, &bytes[4 * kExtent * kExtent + 4 * n], 4); + CHECK(value == 1000 + n); + } + CHECK(std::all_of(bytes.begin(), bytes.begin() + 4 * kExtent * kExtent, [](std::uint8_t b) { return b == 0; })); + CHECK(hooks.compute == int(kCompute) && hooks.graphics == 0); + CHECK(worker_jobs >= 1); + // The worker let go: it builds the graphics pipelines (and variants), and a later submission draws them. + held.let_go(); + const auto deadline = Clock::now() + 60s; + const auto drawn = [&] { + for (unsigned n = 0; n < kGraphics; ++n) + if (bytes[4 * ((n / 8 * 2) * kExtent + n % 8 * 2) + 3] != 255) return false; + return true; + }; + do { + std::this_thread::sleep_for(20ms); + CHECK(frame.submit() == ReplayResult::success); + bytes = frame.read(); + } while (!drawn() && Clock::now() < deadline); + CHECK(expected(bytes)); + CHECK(hooks.graphics == int(kGraphics) && hooks.compute == int(kCompute)); // compute was not built again + return true; +} + bool run(const Setup& setup) { if (!same_image(setup)) return false; return !first_use || (refused_graphics(setup) && refused_compute(setup) && two_queues(setup) && @@ -943,7 +1024,19 @@ bool run(const Setup& setup) { } // namespace -int main() { +int main(int argc, char** argv) { + const bool graphics_zero = argc == 2 && std::strcmp(argv[1], "--async-graphics-zero") == 0; + const bool graphics_unset = argc == 2 && std::strcmp(argv[1], "--async-graphics-unset") == 0; + if (argc != 1 && !graphics_zero && !graphics_unset) { + std::fprintf(stderr, "usage: d3d12_pipeline_workers_test [--async-graphics-zero | --async-graphics-unset]\n"); + return 2; + } + if (graphics_zero || graphics_unset) { // before the first pipeline state: the runtime reads these once + _putenv_s("LEONIDA_ASYNC_PIPELINES", "1"); + _putenv_s("LEONIDA_STRICT_REPLAY", "0"); + _putenv_s("LEONIDA_ASYNC_MIN_BYTES", "1073741824"); // 1 GiB: over any module + _putenv_s("LEONIDA_ASYNC_MIN_BYTES_GRAPHICS", graphics_zero ? "0" : ""); // "" removes it + } vktest::Device vk; vk.request_buffer_device_address = true; const auto status = vk.initialize(); @@ -964,7 +1057,8 @@ int main() { _putenv_s("LEONIDA_COMPUTE_WARMUP", "0"); // every compile is a submission's const testfiles::ScratchDir library("leonida_pipeline_workers_test"); if (!write_library(library.path())) return 1; - if (!run({&vk, library.path().string()})) return 1; + const Setup setup{&vk, library.path().string()}; + if (graphics_zero || graphics_unset ? !async_thresholds(setup, graphics_zero) : !run(setup)) return 1; if (vk.validation_errors()) { std::fprintf(stderr, "FAIL: %u validation errors\n", vk.validation_errors()); return 1;