diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 3c0112060b..b83a9ad54b 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -836,6 +836,9 @@ if(BUILD_TESTING) add_executable(xbox_present_readback_size_test test/xbox_present_readback_size_test.cpp) add_test(NAME xbox_present_readback_size COMMAND xbox_present_readback_size_test) + # The title-memory ranges a submission reached merge into disjoint ranges, none dropped (src/title_write_ranges.h). + add_executable(title_write_ranges_test test/title_write_ranges_test.cpp) + add_test(NAME title_write_ranges COMMAND title_write_ranges_test) # The title's own window, where PresentX shows its frames (shims/title_window.h): user32 only, no device. if(WIN32) diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 748d6144a5..406e532c76 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -74,6 +74,7 @@ #include "../src/title_va_alias.h" #include "../src/console_heap_slot_records.h" #include "../src/title_window_plan.h" +#include "../src/title_write_ranges.h" #include "../src/vulkan_present.h" #include "../src/vulkan_device_filter.h" #include "../src/host_image_copy_policy.h" @@ -3743,27 +3744,14 @@ struct DeviceState { if (!leonida::title_va_aliases().title_memory(va, lo, hi) || va >= hi) return; const std::uint64_t end = va + std::min(bytes, hi - va); std::lock_guard lock(title_write_back_mutex); - auto it = title_write_back.lower_bound(va); - if (it != title_write_back.begin()) { - auto previous = std::prev(it); - if (previous->second >= va) { - if (previous->second < end) previous->second = end; - return; - } - } - if (it != title_write_back.end() && it->first <= end) { - it->second = std::max(it->second, end); - return; - } - if (title_write_back.size() >= kTitleWriteBackRanges || title_write_back_bytes >= kTitleWriteBackBytes) { - if (!title_write_back_refused++) - log_once("past LEONIDA_TITLE_WRITEBACK's caps: reached ranges are not copied back this submission; " - "ranges refused:", - 1); - return; - } - title_write_back_bytes += end - va; - title_write_back.emplace(va, end); + // Joins every range it overlaps or touches (src/title_write_ranges.h): none of it is dropped, and the byte + // count stays the union's. + if (!leonida::add_reached_range(title_write_back, title_write_back_bytes, va, end, kTitleWriteBackRanges, + kTitleWriteBackBytes) && + !title_write_back_refused++) + log_once("past LEONIDA_TITLE_WRITEBACK's caps: reached ranges are not copied back this submission; " + "ranges refused:", + 1); } // What the GPU wrote into those copies goes back to the title's pages, so a later read by the title or the next @@ -3772,7 +3760,7 @@ struct DeviceState { // context reports that ticket done. Runs at the start of a refresh, under shadow_mutex and before the forward // copies, so a page the title wrote again is copied forward over the result. void write_back_titles() { - if (!&context) return; + if (!context) return; const std::uint64_t done = context->completed_retire_ticket(); std::vector> finished; // one entry per range, collected under the lock { diff --git a/runtime/src/title_write_ranges.h b/runtime/src/title_write_ranges.h new file mode 100644 index 0000000000..b9e30c61e9 --- /dev/null +++ b/runtime/src/title_write_ranges.h @@ -0,0 +1,44 @@ +// The title-memory ranges a submission reached, kept for the copy back after it (d3d12_vulkan_device.cpp, +// DeviceState::note_title_write and write_back_titles). +// +// The map holds disjoint ranges, start -> end. A new range joins every range it overlaps or touches, so the map stays +// disjoint, no byte of the new range is dropped, and `bytes` stays the size of the union (what the per-submission byte +// cap compares). Kept apart from DeviceState so the merge can be tested without a device (title_write_ranges_test). +#pragma once + +#include +#include +#include +#include +#include + +namespace leonida { + +// Adds [start, end) to `ranges`. False when the caps refuse it: a range that adds a new entry past `max_ranges`, or +// adds bytes once `bytes` has reached `max_bytes`. A range already covered always fits. +// The byte cap is soft, as it was before the merge moved here: it is checked before a range goes in, so the range +// that crosses it is kept whole (cutting it would drop GPU writes inside it), and the next one that adds bytes is +// refused. A refused range leaves `ranges` and `bytes` as they were. +inline bool add_reached_range(std::map& ranges, std::uint64_t& bytes, std::uint64_t start, + std::uint64_t end, std::size_t max_ranges, std::uint64_t max_bytes) { + if (start >= end) return true; + // The first range that overlaps or touches [start, end): the one before `start`, if it reaches it. + auto first = ranges.upper_bound(start); + if (first != ranges.begin() && std::prev(first)->second >= start) --first; + std::uint64_t lo = start, hi = end, covered = 0; + std::size_t joined = 0; + auto last = first; + for (; last != ranges.end() && last->first <= end; ++last, ++joined) { + lo = std::min(lo, last->first); + hi = std::max(hi, last->second); + covered += last->second - last->first; + } + const std::uint64_t added = (hi - lo) - covered; + if ((!joined && ranges.size() >= max_ranges) || (added && bytes >= max_bytes)) return false; + ranges.erase(first, last); + ranges.emplace(lo, hi); + bytes += added; + return true; +} + +} // namespace leonida diff --git a/runtime/test/title_write_ranges_test.cpp b/runtime/test/title_write_ranges_test.cpp new file mode 100644 index 0000000000..ff1d2d2766 --- /dev/null +++ b/runtime/test/title_write_ranges_test.cpp @@ -0,0 +1,103 @@ +// add_reached_range (src/title_write_ranges.h): the title-memory ranges a submission reached, kept for the copy back +// (DeviceState::note_title_write). kevincommits' finding in #1608: a range that began before an entry lost its prefix, +// a range that spanned entries left them overlapping, and the byte count missed what a merge added. No device, no +// game data: plain numbers. +#include "../src/title_write_ranges.h" + +#include +#include +#include +#include + +namespace { + +using Ranges = std::map; + +bool check(bool condition, const char* description) { + if (!condition) std::fprintf(stderr, "FAIL: %s\n", description); + return condition; +} + +struct Recorder { + Ranges ranges; + std::uint64_t bytes = 0; + bool add(std::uint64_t start, std::uint64_t end, std::size_t max_ranges = 8192, std::uint64_t max_bytes = 1ull << 40) { + return leonida::add_reached_range(ranges, bytes, start, end, max_ranges, max_bytes); + } + bool is(std::vector> want) const { + return Ranges(want.begin(), want.end()) == ranges; + } +}; + +} // namespace + +int main() { + bool ok = true; + + { // kevincommits' first repro: the prefix before an existing entry + Recorder r; + r.add(200, 300); + r.add(100, 250); + ok &= check(r.is({{100, 300}}), "[200,300) then [100,250) keeps [100,300), not only [200,300)"); + ok &= check(r.bytes == 200, "and counts 200 bytes"); + } + { // kevincommits' second repro: a range spanning two entries + Recorder r; + r.add(100, 200); + r.add(300, 400); + r.add(150, 350); + ok &= check(r.is({{100, 400}}), "[100,200), [300,400) then [150,350) leaves one disjoint [100,400)"); + ok &= check(r.bytes == 300, "and counts the union's 300 bytes, not 200"); + } + { // a range covering several entries and reaching past both ends + Recorder r; + r.add(10, 20); + r.add(30, 40); + r.add(50, 60); + r.add(5, 70); + ok &= check(r.is({{5, 70}}) && r.bytes == 65, "a range over three entries absorbs them all"); + } + { // extending an entry forward across the next one + Recorder r; + r.add(100, 200); + r.add(250, 300); + r.add(180, 260); + ok &= check(r.is({{100, 300}}) && r.bytes == 200, "extending the previous entry absorbs the next"); + } + { // touching ranges join; a gap keeps them apart; a covered range changes nothing + Recorder r; + r.add(0, 100); + r.add(100, 200); + r.add(300, 400); + r.add(120, 180); + ok &= check(r.is({{0, 200}, {300, 400}}) && r.bytes == 300, "touching joins, a gap stays, covered adds nothing"); + } + { // the byte cap holds for merges too + Recorder r; + ok &= check(r.add(0, 100, 8192, 100), "the first 100 bytes fit a 100-byte cap"); + ok &= check(!r.add(50, 200, 8192, 100), "a merge that adds bytes past the cap is refused"); + ok &= check(r.is({{0, 100}}) && r.bytes == 100, "and leaves the ranges as they were"); + ok &= check(r.add(10, 90, 8192, 100), "a range already covered still fits"); + } + { // the byte cap is soft: the range that crosses it goes in whole, the next byte past it does not (akimfomin, #2517) + Recorder r; + ok &= check(r.add(0, 63, 8192, 64) && r.bytes == 63, "[0,63) fits a 64-byte cap"); + ok &= check(r.add(128, 132, 8192, 64), "a disjoint [128,132) still goes in: 63 bytes is under the cap"); + ok &= check(r.is({{0, 63}, {128, 132}}) && r.bytes == 67, "and is kept whole: 63 -> 67 bytes"); + ok &= check(!r.add(200, 201, 8192, 64), "the next range that adds a byte is refused"); + ok &= check(!r.add(60, 70, 8192, 64), "so is a merge that adds bytes"); + ok &= check(r.is({{0, 63}, {128, 132}}) && r.bytes == 67, "and refusals leave the map and count unchanged"); + ok &= check(r.add(129, 131, 8192, 64) && r.bytes == 67, "a covered range still fits"); + } + { // the range cap refuses only new entries + Recorder r; + r.add(0, 10, 2); + r.add(20, 30, 2); + ok &= check(!r.add(40, 50, 2), "a third entry past a two-range cap is refused"); + ok &= check(r.add(5, 25, 2) && r.is({{0, 30}}), "a range joining entries fits the range cap"); + ok &= check(r.add(40, 50, 2) && r.is({{0, 30}, {40, 50}}), "and frees room for a new one"); + } + + if (ok) std::printf("title_write_ranges_test: PASS\n"); + return ok ? 0 : 1; +}