diff --git a/runtime/README.md b/runtime/README.md index ec4c801452..3fa94e81a6 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -381,6 +381,7 @@ ones marked *diagnostic* only add log lines or exist to compare a run with and w | `LEONIDA_GEOMETRY_WHOLE_VIEWS` | unset | A geometry copy that is already filled copies only the 64 KiB tiles of title memory written since the last refresh (`shims/d3d12_geometry_views.h`). Any value other than `0` copies every view over a written tile whole, as before. | | `LEONIDA_TITLE_FILL_ASYNC` | unset | Trades correctness for speed. `1` stops device slot 70 views from hashing and filling the texture they name on the title's thread: a texture never filled or known stale is queued for a worker thread that fills one texture at a time. Until then a shader samples the image as it is, empty or a frame behind. | | `LEONIDA_TITLE_VIEW_REFRESH_MS` | `0` | The shortest time, in milliseconds, between two checks of a title texture's bytes when a device slot 70 view of it is written. `0` checks at every view. The periodic pass still checks the texture on its own period, and a texture known stale is checked at once. Has no effect with `LEONIDA_TITLE_FILL_ASYNC=1`. | +| `LEONIDA_TITLE_EVERY_BUDGET_US` | `0` | Microseconds one pass over the textures filled from the title's memory may spend on the textures it visits every pass (mapped by the title, or known stale), resuming where the last pass stopped; the texture that crosses the limit is finished. A mapped texture never filled yet is visited first whatever the limit. `0` visits all of them every pass. It limits the synchronous pass only: with `LEONIDA_TEXTURE_REFRESH_ASYNC=1` the pass runs on the refresh worker, and the limit does not apply. | | `LEONIDA_TITLE_TEXTURE_FILL_MS` | `8` | Milliseconds one pass over the textures filled from the title's memory may spend filling; a texture already started is finished. `0` removes the time limit and leaves `LEONIDA_TITLE_TEXTURE_FILL_MB` to bound the pass. | | `LEONIDA_MAP_READBACK` | unset | `1` writes the GPU's texels of a small render or unordered-access target the title placed into the title's pages when the title maps it, so a CPU read of the target sees what the GPU rendered (the title reads its exposure statistics that way). Only a single-row, single-mip, single-layer, single-sample 2D target of at most 4 KiB a row; the copy waits for the GPU's work already submitted, and a page that is not writable is left as it is. Unset, the title reads its own pages, which the GPU never writes. | | `LEONIDA_SLOT133_IMPORT` | on | *Diagnostic.* `0` imports no title-placed buffer (device slot 133) as host memory: each takes the shadow-buffer fallback, so no GPU store can reach the title's memory through an import. | diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index e8676b32c7..183d4acade 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -14790,6 +14790,13 @@ class Device final } const auto every_start = clock::now(); stats.written_ns += std::chrono::duration_cast(every_start - now).count(); + // LEONIDA_TITLE_EVERY_BUDGET_US (0, the default: no limit, as before) limits the synchronous pass, the one that + // runs before a submission on the title's submitting thread: the mapped and stale textures are visited for at + // most this long a pass, in address order from where the last pass stopped, instead of all of them every pass + // (thousands during a streaming burst on retail, 30-75 ms a pass). The texture that crosses the limit is + // finished; one not reached keeps its mark and waits for a later pass. With LEONIDA_TEXTURE_REFRESH_ASYNC the + // pass runs on the refresh worker, and the limit does not apply. + static const std::uint64_t every_budget_us = setting("LEONIDA_TITLE_EVERY_BUDGET_US", 0); if (async) { // Poll every persistent mapping each pass, independently of the bounded stale sweep. const auto mapped = leonida::title_texture_snapshot(state_.mapped_title_textures); @@ -14800,18 +14807,34 @@ class Device final ++stats.every_pass_visits; visit(*entry); } - } - for (const auto& entry : entries) - if (async ? !entry.second->mapped && entry.second->stale : entry.second->mapped || entry.second->stale) { - if (async) { + for (const auto& entry : entries) + if (!entry.second->mapped && entry.second->stale) { const auto it = state_.title_textures.find(entry.first); if (it == state_.title_textures.end() || it->second != entry.second) continue; + ++stats.every_pass_visits; + visit(*entry.second); } - ++stats.every_pass_visits; - visit(*entry.second); - } + } else if (!every_budget_us) { + for (const auto& entry : entries) + if (entry.second->mapped || entry.second->stale) { + ++stats.every_pass_visits; + visit(*entry.second); + } + } else { + leonida::visit_marked_title_textures( + entries, title_every_cursor_, + [&](TitleTexture& t) { + ++stats.every_pass_visits; + visit(t); + }, + // A mapped texture never filled yet does not wait for its turn: the title has written it and the GPU + // has nothing of it (a video plane of #1827 would show frozen until its turn came). + [](const TitleTexture& t) { return t.mapped && !t.fills; }, + [&] { return clock::now() - every_start >= std::chrono::microseconds(every_budget_us); }); + } stats.every_pass_ns += std::chrono::duration_cast(clock::now() - every_start).count(); - // The budget is the round robin's: mapped and stale textures above are checked every pass whatever it is. + // The budget is the round robin's: mapped and stale textures above are checked every pass whatever it is + // (unless LEONIDA_TITLE_EVERY_BUDGET_US limits them). const auto first_bytes = checked, first_hash_ns = stats.hash_ns; if (check_budget) { const auto sweep_start = clock::now(); @@ -15136,6 +15159,7 @@ class Device final } std::uint64_t title_slice_ = 0; // next registry slice on the async worker std::uint64_t title_refresh_cursor_ = 0; // under title_texture_mutex + std::uint64_t title_every_cursor_ = 0; // likewise: LEONIDA_TITLE_EVERY_BUDGET_US's resume point std::uint64_t title_map_generation_ = 0; // likewise: the map changes the passes have seen std::chrono::steady_clock::time_point title_retry_all_{}; // likewise leonida::TitleWriteRanges title_map_changes_; // likewise, scratch diff --git a/runtime/src/title_texture_refresh.h b/runtime/src/title_texture_refresh.h index a652e6281d..feb8a0cc27 100644 --- a/runtime/src/title_texture_refresh.h +++ b/runtime/src/title_texture_refresh.h @@ -107,4 +107,38 @@ void poll_title_textures(const std::vector& entries, std::uint64_t& curso } } +// Visit the marked textures of a snapshot (mapped by the title, or known stale) under a time limit +// (LEONIDA_TITLE_EVERY_BUDGET_US). Those `urgent` answers true for (a mapped texture never filled yet) are all +// visited first, in placement order, whatever the limit. The others are visited +// from `cursor`, as poll_title_textures does for the unmarked ones, until `exhausted()` answers true after a visit; +// the next pass resumes at the entry after the last one visited, so every marked texture gets its turn however many +// there are. An entry skipped as unmarked costs no check. +template +void visit_marked_title_textures(const std::vector& entries, std::uint64_t& cursor, Visit visit, Urgent urgent, + Exhausted exhausted) { + if (entries.empty()) return; + // Which entries are urgent is decided before any visit: a visit fills the texture, and it must not come round + // again in the rotation of the same pass. + std::vector was_urgent(entries.size()); + for (std::size_t i = 0; i < entries.size(); ++i) { + const auto& texture = entries[i].second; + was_urgent[i] = (texture->mapped || texture->stale) && urgent(*texture); + } + for (std::size_t i = 0; i < entries.size(); ++i) + if (was_urgent[i]) visit(*entries[i].second); + const auto first = std::lower_bound(entries.begin(), entries.end(), cursor, + [](const Entry& entry, std::uint64_t at) { return entry.first < at; }); + auto index = std::size_t(first - entries.begin()); + if (index == entries.size()) index = 0; + for (auto left = entries.size(); left; --left) { + const auto& [address, texture] = entries[index]; + cursor = address + 1; + if ((texture->mapped || texture->stale) && !was_urgent[index]) { + visit(*texture); + if (exhausted()) break; + } + index = index + 1 == entries.size() ? 0 : index + 1; + } +} + } // namespace leonida diff --git a/runtime/test/xg_xs_test.cpp b/runtime/test/xg_xs_test.cpp index c5272e0155..c6df1bc243 100644 --- a/runtime/test/xg_xs_test.cpp +++ b/runtime/test/xg_xs_test.cpp @@ -415,6 +415,62 @@ int main() { for (unsigned pass = 0; pass < 3; ++pass) poll([] { return true; }); check(polled == std::vector{4}, "an expired wall budget advances past priority entries to the next texture"); + // LEONIDA_TITLE_EVERY_BUDGET_US: the marked textures (mapped or stale) are visited from a cursor of their own + // until the budget runs out; the unmarked ones are the round robin's and are skipped here. + { + Entry m1{true, false, 11}, u1{false, false, 12}, s1{false, true, 13}, m2{true, false, 14}; + Entry u2{false, false, 15}, s2{false, true, 16}; + std::map marked{{100, &m1}, {200, &u1}, {300, &s1}, {400, &m2}, {500, &u2}}; + std::uint64_t every = 0; + std::vector seen; + std::vector urgent_ids; // stands for "mapped and never filled" + const auto is_urgent = [&](const Entry& t) { + return std::find(urgent_ids.begin(), urgent_ids.end(), t.id) != urgent_ids.end(); + }; + const auto visit_marked = [&](auto exhausted) { + const auto snapshot = leonida::title_texture_snapshot(marked); + leonida::visit_marked_title_textures( + snapshot, every, + [&](Entry& t) { + seen.push_back(t.id); + urgent_ids.erase(std::remove(urgent_ids.begin(), urgent_ids.end(), t.id), urgent_ids.end()); + }, + is_urgent, exhausted); + }; + visit_marked([] { return false; }); + check(seen == std::vector{11, 13, 14}, "without a limit every marked texture is visited once, in order"); + seen.clear(); + for (unsigned pass = 0; pass < 4; ++pass) visit_marked([] { return true; }); + check(seen == std::vector{11, 13, 14, 11}, + "a budget spent after each visit moves one marked texture a pass and wraps"); + seen.clear(); + marked.erase(300); + marked[350] = &s2; + visit_marked([&] { return seen.size() == 2; }); + check(seen == std::vector{16, 14}, "the cursor survives an erased entry and reaches a new stale one"); + seen.clear(); + visit_marked([] { return true; }); + check(seen == std::vector{11}, "the pass after resumes past the last visited entry"); + // The cursor now points past 100 (m1). Mark m2 (at 400) urgent, as a mapped texture never filled: it is + // visited first whatever the budget, and once filled (no longer urgent) it is not visited a second time + // in the same pass's rotation. + seen.clear(); + urgent_ids = {14}; + visit_marked([] { return true; }); + check(seen == std::vector{14, 16}, "an urgent texture is visited first, then one rotation step"); + seen.clear(); + urgent_ids = {14}; + visit_marked([] { return false; }); + check(seen == std::vector{14, 11, 16}, + "with no limit an urgent texture is still visited once, not again in the rotation"); + seen.clear(); + const std::vector> none; + leonida::visit_marked_title_textures( + none, every, [&](Entry& t) { seen.push_back(t.id); }, [](const Entry&) { return true; }, + [] { return false; }); + check(seen.empty(), "an empty registry visits nothing"); + } + // A fill made during a pass submits, and that submission destroys a retired image whose destructor erases its // registry entry; the registry mutex is recursive for its owner on Windows, so the erase lands inside the pass. // A walk over a snapshot keeps every entry of the pass alive and in order, which a walk over the registry