From b3db0f3659d2deb51b086fc93e767182b2ac0dad Mon Sep 17 00:00:00 2001 From: Tyrannys <80750971+Tyrannys@users.noreply.github.com> Date: Fri, 27 Nov 2026 18:09:42 -0600 Subject: [PATCH 1/2] d3d12: title write-back ranges merge into disjoint ranges, none dropped DeviceState::note_title_write kept the reached title-memory ranges of a submission in a map, but its merge lost data (kevincommits' finding in #1608): - a range that began before an entry only extended that entry's end, so its prefix was never copied back ([200,300) then [100,250) kept only [200,300)); - a range spanning entries extended one without absorbing the others, leaving the map overlapping ([100,350) and [300,400)); - merged bytes were never counted, so kTitleWriteBackBytes could be passed. The merge moves to src/title_write_ranges.h (add_reached_range): a new range joins every entry it overlaps or touches, the byte count stays the union's, and the byte cap applies to merges that add bytes. The range cap still refuses only new entries. write_back_titles also checked `!&context`, the address of a member, which is never null; it now checks `!context`. New CPU test title_write_ranges follows the repros; 11 of its checks fail with the old merge. Co-Authored-By: Claude Opus 5.5 --- runtime/CMakeLists.txt | 3 + runtime/shims/d3d12_vulkan_device.cpp | 32 +++----- runtime/src/title_write_ranges.h | 41 +++++++++++ runtime/test/title_write_ranges_test.cpp | 93 ++++++++++++++++++++++++ 4 files changed, 147 insertions(+), 22 deletions(-) create mode 100644 runtime/src/title_write_ranges.h create mode 100644 runtime/test/title_write_ranges_test.cpp 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..ad8dbb8f03 --- /dev/null +++ b/runtime/src/title_write_ranges.h @@ -0,0 +1,41 @@ +// 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. +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..b5dec0cda0 --- /dev/null +++ b/runtime/test/title_write_ranges_test.cpp @@ -0,0 +1,93 @@ +// 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 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; +} From 7ec666eeffe524d2a33c0d67ea2c77a5a51c097e Mon Sep 17 00:00:00 2001 From: Tyrannys <80750971+Tyrannys@users.noreply.github.com> Date: Fri, 27 Nov 2026 18:35:54 -0600 Subject: [PATCH 2/2] title_write_ranges: pin the soft byte cap with a test The byte cap stays soft, as note_title_write had it before the merge moved: 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 range that adds bytes is refused. The header now says so, and the test pins akimfomin's case from #2517: cap 64, [0,63) then [128,132) goes 63 -> 67 bytes, the next new byte and a merge that adds bytes are refused, refusals leave the map unchanged, and a covered range fits. Co-Authored-By: Claude Opus 5.5 --- runtime/src/title_write_ranges.h | 3 +++ runtime/test/title_write_ranges_test.cpp | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/runtime/src/title_write_ranges.h b/runtime/src/title_write_ranges.h index ad8dbb8f03..b9e30c61e9 100644 --- a/runtime/src/title_write_ranges.h +++ b/runtime/src/title_write_ranges.h @@ -16,6 +16,9 @@ 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; diff --git a/runtime/test/title_write_ranges_test.cpp b/runtime/test/title_write_ranges_test.cpp index b5dec0cda0..ff1d2d2766 100644 --- a/runtime/test/title_write_ranges_test.cpp +++ b/runtime/test/title_write_ranges_test.cpp @@ -79,6 +79,16 @@ int main() { 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);