diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 45d4b4be47..213b921998 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2874,6 +2874,19 @@ submitting thread; at the maintainers' decision it was replaced by this one when at record time already: at `SetPipelineState` for compute and at each draw for graphics, with the variant its bound targets give (`request_pipeline_build`). A compute build the compile helper is still making (`LEONIDA_COMPILE_HELPER`) publishes nothing, so its next use asks again. +- **Only the large ones asynchronous** (`LEONIDA_ASYNC_MIN_BYTES=`, with `LEONIDA_ASYNC_PIPELINES=1`). With every + pipeline asynchronous, the small lighting and post programs are skipped with the large ones until the worker gets to + them, and on an RTX 4070 Laptop (NVIDIA 621.40) the world phase was reached with the screen black; with none, a + submission waits for each ray-tracing compute program of 2-26 MB of SPIR-V, 2-3 minutes each there, and 17 were built + in 30 minutes (measured by OreoFR on the wave64-emulated pack). With the variable, a pipeline under n bytes of SPIR-V + (compute: its module; graphics: all its stages, the size the large-compile rules use) takes the path it takes with + asynchronous builds off: no record-time request, prepare's `PipelineCompiles` builds it, and a compile-helper build + waits for its file. One of n bytes or more is asynchronous as above. The choice is made once per pipeline at create + (`async_build` in `src/vulkan_replay.h`, decided by `src/async_pipeline_size.h`). Unset, `0` or text that is not a + decimal number of bytes (which is logged): every pipeline is asynchronous, as before the variable. A large build + 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). - **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 161e3153dc..f1e46966e2 100644 --- a/docs/running.md +++ b/docs/running.md @@ -113,6 +113,10 @@ Reported by others: [#1608, comment 7184700506](https://github.com/ubiquitariani are the report's pack; a native wave64 pack builds them in seconds each ([above](#settings-every-platform-needs)). Where they still take minutes, `gta6rt --precompile-pipelines --big-threads N` bounds how many build at once (each held about 3.5 GB of host memory on an RTX 4070 Ti) and `--skip-listed` leaves the listed ones out. + 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. - **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 f8edc3508e..3144cef440 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -903,6 +903,11 @@ 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, + # 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) + # 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) @@ -3707,6 +3712,10 @@ if(TARGET Vulkan::Headers) target_compile_options(d3d12_m4_compute_test PRIVATE /W4 /permissive- /utf-8) add_test(NAME d3d12_m4_compute COMMAND d3d12_m4_compute_test) set_tests_properties(d3d12_m4_compute PROPERTIES SKIP_RETURN_CODE 77) + # LEONIDA_ASYNC_PIPELINES=1 with LEONIDA_ASYNC_MIN_BYTES over both modules and the compile worker held: the + # dispatches build their pipelines in place, as with asynchronous builds off (src/async_pipeline_size.h). + add_test(NAME d3d12_m4_compute_async_small_waits COMMAND d3d12_m4_compute_test --async-small-waits) + set_tests_properties(d3d12_m4_compute_async_small_waits PROPERTIES SKIP_RETURN_CODE 77) # Typed buffer UAVs and UAV counters: cs_buf_typed.asm writes typed views through V#s with a FORMAT, and # cs_buf_counter.asm appends to and consumes from a structured view whose counter is the second V# of its slot; diff --git a/runtime/README.md b/runtime/README.md index b440230c81..2c22314be2 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -327,6 +327,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_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_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. | diff --git a/runtime/shims/d3d12_pipeline_compile.h b/runtime/shims/d3d12_pipeline_compile.h index 2889d7fd36..a0ff0e86d0 100644 --- a/runtime/shims/d3d12_pipeline_compile.h +++ b/runtime/shims/d3d12_pipeline_compile.h @@ -1,6 +1,7 @@ // Timing and scheduling for Vulkan pipeline creation on the D3D12 device. #pragma once +#include "../src/async_pipeline_size.h" #include "../src/vulkan_device_fault.h" #include "../src/vulkan_replay.h" #include @@ -35,6 +36,32 @@ inline bool async_replay_compile() { return on && !leonida::strict_replay(); } +// 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) + 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); + return value; + }(); + return bytes; +} + +// 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) { + return async_replay_compile() && + leonida::async_pipelines::asynchronous(true, async_min_bytes(), spirv_bytes); +} + // LOCAL FIX (post-load stalls): LEONIDA_FAST_PIPELINES=graphics, compute, or all (1 = all) creates those pipelines with // VK_PIPELINE_CREATE_DISABLE_OPTIMIZATION_BIT. After the story load, graphics creates measured about 4.5 s each, and // every submission waits at the compile gate while one is in the driver (frames stopped for 130 s); the title's large diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index f2fa3a45c2..67f247465b 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -12246,7 +12246,8 @@ 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 - graphics_.async_build = async_replay_compile(); + // 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); graphics_.variant = [this](std::uint32_t unbound) { return replay_pipeline("draw", [&] { return graphics_.async_build ? queue_build(false, unbound) : variant(unbound, "draw"); @@ -12412,7 +12413,8 @@ class PipelineState final }; pipeline_.build_context = this; } else if (lazy_compute) { - pipeline_.async_build = async_replay_compile(); + // 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_.build = [](void* self) { auto* pso = static_cast(self); return replay_pipeline("dispatch", [&] { @@ -12487,7 +12489,8 @@ class PipelineState final // (LOCAL FIX, review: with LEONIDA_ASYNC_PIPELINES=1 it was deferred and returned E_FAIL). const bool warmup = !std::strcmp(trigger, "warmup"); const bool replay_trigger = !std::strcmp(trigger, "dispatch") || !std::strcmp(trigger, "dispatch-worker"); - const bool helper_nowait = warmup || (replay_trigger && async_replay_compile()); + // An asynchronous pipeline only (one under LEONIDA_ASYNC_MIN_BYTES waits for the file, as without them). + const bool helper_nowait = warmup || (replay_trigger && pipeline_.async_build); if (!helper_file_.empty()) { auto& helper = compile_helper::Client::instance(); std::string why; @@ -12593,7 +12596,7 @@ class PipelineState final // The compile helper is still making the journal file (build_compute): not built, so nothing is // published and the next use asks again. const bool deferred = compute && helper_deferred_.exchange(false); - if (async_replay_compile()) { + if (compute ? pipeline_.async_build : graphics_.async_build) { unsigned long long skipped = 0; { std::lock_guard publish(publication); @@ -12628,6 +12631,8 @@ class PipelineState final // Never waits; nothing to do when the variant is the last one asked for here. void request_early(std::uint32_t bound_targets, bool depth_bound) { if (!async_replay_compile() || !registered_) return; + // A pipeline under LEONIDA_ASYNC_MIN_BYTES is built where it is needed, as without asynchronous builds. + if (!(is_graphics_ ? graphics_.async_build : pipeline_.async_build)) return; std::uint32_t unbound = 0; if (is_graphics_) { for (std::size_t slot = 0; slot < graphics_.color_formats.size() && slot < 8; ++slot) diff --git a/runtime/src/async_pipeline_size.h b/runtime/src/async_pipeline_size.h new file mode 100644 index 0000000000..3146224c1a --- /dev/null +++ b/runtime/src/async_pipeline_size.h @@ -0,0 +1,40 @@ +// Which pipelines LEONIDA_ASYNC_PIPELINES=1 builds asynchronously, by size (LEONIDA_ASYNC_MIN_BYTES). Pure stdlib, so +// async_pipeline_size_test checks it without a device; shims/d3d12_pipeline_compile.h reads the variables. +// +// An asynchronous pipeline that is not built yet has its draws and dispatches skipped until the compile worker has +// it (queue_build in shims/d3d12_vulkan_device.cpp). With every pipeline asynchronous the small lighting and post +// programs are skipped with the large ones, and the screen can stay black for as long as anything is compiling; with +// 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. +#pragma once + +#include + +namespace leonida::async_pipelines { + +// LEONIDA_ASYNC_MIN_BYTES as text: decimal bytes. 0 (every pipeline asynchronous, as without the variable) for null, +// empty, "0" or text that is not a whole decimal number; `valid` (when given) says whether the text was usable, so the +// caller can say that it ignored it. +inline std::uint64_t parse_min_bytes(const char* text, bool* valid = nullptr) { + if (valid) *valid = true; + if (!text || !*text) return 0; + std::uint64_t value = 0; + for (const char* c = text; *c; ++c) { + const std::uint64_t digit = std::uint64_t(*c - '0'); + if (*c < '0' || *c > '9' || value > (UINT64_MAX - digit) / 10) { // not a digit, or over 2^64 - 1 + if (valid) *valid = false; + return 0; + } + value = value * 10 + digit; + } + return value; +} + +// 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); +} + +} // namespace leonida::async_pipelines diff --git a/runtime/test/async_pipeline_size_test.cpp b/runtime/test/async_pipeline_size_test.cpp new file mode 100644 index 0000000000..df231a7f4a --- /dev/null +++ b/runtime/test/async_pipeline_size_test.cpp @@ -0,0 +1,77 @@ +// 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. +#include "../src/async_pipeline_size.h" + +#include +#include +#include + +namespace { + +int failures = 0; + +#define CHECK(condition) do { \ + if (!(condition)) { \ + std::fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #condition); \ + ++failures; \ + } \ +} while (0) + +using leonida::async_pipelines::asynchronous; +using leonida::async_pipelines::parse_min_bytes; + +void parsing() { + bool valid = false; + // Unset or empty: no threshold, and nothing to complain about. + CHECK(parse_min_bytes(nullptr, &valid) == 0 && valid); + CHECK(parse_min_bytes("", &valid) == 0 && valid); + CHECK(parse_min_bytes("0", &valid) == 0 && valid); + CHECK(parse_min_bytes("2097152", &valid) == 2097152 && valid); + CHECK(parse_min_bytes("1", &valid) == 1 && valid); + CHECK(parse_min_bytes("18446744073709551615", &valid) == UINT64_MAX && valid); + CHECK(parse_min_bytes("2097152") == 2097152); // `valid` is optional + // Not a whole decimal number of bytes: ignored (0, every pipeline asynchronous) and reported as such. + for (const char* text : {"2M", "2MiB", "-1", " 1", "1 ", "0x200000", "1.5", "abc", "18446744073709551616", + "99999999999999999999"}) { + valid = true; + CHECK(parse_min_bytes(text, &valid) == 0); + CHECK(!valid); + if (valid) std::fprintf(stderr, " accepted: \"%s\"\n", text); + } +} + +void decision() { + constexpr std::uint64_t mib = 1u << 20; + // Asynchronous builds off: nothing is asynchronous, whatever the threshold. + for (std::uint64_t threshold : {std::uint64_t{0}, std::uint64_t{1}, 2 * mib}) + for (std::uint64_t bytes : {std::uint64_t{0}, std::uint64_t{4}, 2 * mib - 1, 2 * mib, 26 * mib}) + CHECK(!asynchronous(false, threshold, bytes)); + // On without a threshold (unset, or 0): every pipeline, as before the variable, the smallest included. + for (std::uint64_t bytes : {std::uint64_t{0}, std::uint64_t{4}, 64 * std::uint64_t{1024}, 2 * mib, 26 * mib}) + CHECK(asynchronous(true, 0, bytes)); + // On with 2 MiB: under it waits, at it and over it is asynchronous. + CHECK(!asynchronous(true, 2 * mib, 0)); + CHECK(!asynchronous(true, 2 * mib, 4)); + CHECK(!asynchronous(true, 2 * mib, 512 * std::uint64_t{1024})); // a lighting or post program + CHECK(!asynchronous(true, 2 * mib, 2 * mib - 1)); + CHECK(asynchronous(true, 2 * mib, 2 * mib)); + CHECK(asynchronous(true, 2 * mib, 26 * mib)); // the largest ray-tracing resolve programs + // A threshold of 1 byte: as unset for any pipeline with code. + CHECK(asynchronous(true, 1, 4)); + // A threshold over every pipeline: nothing asynchronous, as with asynchronous builds off. + CHECK(!asynchronous(true, UINT64_MAX, 26 * mib)); + CHECK(asynchronous(true, UINT64_MAX, UINT64_MAX)); +} + +} // namespace + +int main() { + parsing(); + decision(); + if (failures) { + std::fprintf(stderr, "async_pipeline_size_test: %d failure(s)\n", failures); + return 1; + } + std::printf("async_pipeline_size_test: ok\n"); + return 0; +} diff --git a/runtime/test/d3d12_m4_compute_test.cpp b/runtime/test/d3d12_m4_compute_test.cpp index 05d6fe1530..b620f1cae4 100644 --- a/runtime/test/d3d12_m4_compute_test.cpp +++ b/runtime/test/d3d12_m4_compute_test.cpp @@ -25,6 +25,10 @@ // argument buffer, the image and the indirect dispatch's output (which also shows that exactly the 3 groups the // first dispatch asked for ran). // Set LEONIDA_VK_VALIDATION=1 to run under VK_LAYER_KHRONOS_validation (0 errors required). +// --async-small-waits: LEONIDA_ASYNC_PIPELINES=1 with LEONIDA_ASYNC_MIN_BYTES over both modules (and the warm-up off), and +// every job of the compile worker held until the results are read: both pipelines are under the threshold, so the +// dispatches build them where they are needed, as with asynchronous builds off. The same words must match, and no build +// may have reached the worker. Without the threshold the held worker would leave both dispatches skipped. #include "vk_test_device.h" #include "d3d12_test_shaders.h" #include "../shims/d3d12_com.h" @@ -34,6 +38,8 @@ #include "../src/vulkan_indirect_clamp.h" #include +#include +#include #include #ifndef LEONIDA_TEST_M4_DIR @@ -99,6 +105,13 @@ Reference reference() { int main(int argc, char** argv) { const bool check_journal = argc == 2 && std::strcmp(argv[1], "--check-async-journal") == 0; + const bool small_waits = argc == 2 && std::strcmp(argv[1], "--async-small-waits") == 0; + if (small_waits) { // before the first PSO: the runtime reads these once + _putenv_s("LEONIDA_ASYNC_PIPELINES", "1"); + _putenv_s("LEONIDA_STRICT_REPLAY", "0"); + _putenv_s("LEONIDA_COMPUTE_WARMUP", "0"); + _putenv_s("LEONIDA_ASYNC_MIN_BYTES", "1073741824"); // 1 GiB: over any module + } vktest::Device d; d.queue_count_wanted = 2; // the copy queue and the compute queue each get a VkQueue where the family has two const auto device_status = d.initialize(); @@ -144,6 +157,28 @@ int main(int argc, char** argv) { rt::d3d12::set_device_outer(inner, device.p); EXPECT(rt::d3d12::bindless_set(device.p) != VK_NULL_HANDLE); + // --async-small-waits: every compile-worker job is counted and held until `release` (or the end of this scope, + // before the device goes, so a failed check does not leave the device's teardown waiting for a held build). + std::atomic worker_jobs{0}; + struct HeldWorker { + HANDLE release = nullptr; + ~HeldWorker() { + if (!release) return; + SetEvent(release); + rt::d3d12::PipelineCompiler::instance().set_job_probe(nullptr); + CloseHandle(release); + } + } held; + if (small_waits) { + EXPECT(rt::d3d12::async_replay_compile()); + held.release = CreateEventW(nullptr, TRUE, FALSE, nullptr); + EXPECT(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); + }); + } + // [0] four 32-bit constants (s4..s7), [1] a table of five UAVs (s8, its 32-bit VA): the console compute layout // (D3D-448). The fixtures are built with LEONIDA_USER_DATA_SHIFT=4 (runtime/CMakeLists.txt). D3D12_DESCRIPTOR_RANGE uav_range{D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 5, 0, 0, 0}; @@ -396,6 +431,15 @@ int main(int argc, char** argv) { } CloseHandle(event); EXPECT(copy_fence->GetCompletedValue() >= 2 && compute_fence->GetCompletedValue() >= 1); + if (small_waits) { + const unsigned jobs = worker_jobs.load(); + 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( + library.spirv_bytes(*library.find("cs", leonida::sha256_hex(indirect))))); + } void* mapped = nullptr; EXPECT(SUCCEEDED(readback->Map(0, nullptr, &mapped)));