Skip to content

d3d12: title write-back ranges merge into disjoint ranges, none dropped (RT-TITLE-WRITE-NOTE-MERGE) - #2517

Open
Tyrannys wants to merge 2 commits into
ProjectLeonida:mainfrom
Tyrannys:rt-title-write-note-merge
Open

Tyrannys wants to merge 2 commits into
ProjectLeonida:mainfrom
Tyrannys:rt-title-write-note-merge

Conversation

@Tyrannys

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-TITLE-WRITE-NOTE-MERGE (#1608 (comment))

What and why

kevincommits' finding in #1608 (item 1 of #1608 (comment)). DeviceState::note_title_write keeps the title-memory ranges a submission reached, for the copy back after it. Its merge lost data:

  • A range that began before an entry only moved that entry's end, so the prefix was never copied back. [200,300) then [100,250) kept only [200,300).
  • A range that spanned entries extended one of them without absorbing the others, so the map overlapped. [100,200), [300,400) then [150,350) left [100,350) and [300,400).
  • Merged bytes were never counted, so kTitleWriteBackBytes could be exceeded (200 counted against a 300-byte union).

The merge moves to src/title_write_ranges.h (leonida::add_reached_range):

  • A new range joins every entry it overlaps or touches.
  • The byte count stays the size of the union.
  • The byte cap also applies to merges that add bytes, and a range that is already covered always fits.
  • The range cap still refuses only new entries.

write_back_titles also checked if (!&context). That is the address of a std::unique_ptr member, so it is never null. It now checks if (!context).

This touches different lines of d3d12_vulkan_device.cpp than #2470 and #2461. It merges cleanly with #2461. #2470 conflicts with current main on its own, before this PR.

Tested

  • New CPU test title_write_ranges follows kevincommits' repros, plus cases for touching ranges, multi-entry spans and both caps. It passes. With the old merge put back, 11 of its checks fail.
  • d3d12_vulkan_device.cpp builds (MSVC, Release).
  • ctest -R "title_write_ranges|d3d12_title|d3d12_placed_write_back|d3d12_shadow": 14/14 pass.

GPUs tested: profile M: NVIDIA GeForce RTX 4070 SUPER, 12 GB, desktop, driver 621.01, Windows 11

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

DeviceState::note_title_write kept the reached title-memory ranges of a
submission in a map, but its merge lost data (kevincommits' finding in
ProjectLeonida#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 <noreply@anthropic.com>
@akimfomin

Copy link
Copy Markdown
Contributor

PortBridge traced the byte accounting at b3db0f3. A small check would pin the contract before merge: cap 64, already covered [0,63), then add disjoint [128,132). The current check accepts it and reports 67 bytes. That matches the helper’s soft-cap wording, but the calling code describes submission caps. Is the first overshoot intentional? If so, test the 63 → 67 transition and refusal of the next noncovered byte. If strict, compare the added bytes with the remaining capacity and check that refusal leaves the map unchanged.

The title-free CPU controls also cover a new range bridging two existing ranges without losing the prefix. Vice-Tay #388 uses the same half-open comparisons for a different operation, invalidation, so its state transition should remain separate.

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 ProjectLeonida#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 <noreply@anthropic.com>
@Tyrannys

Copy link
Copy Markdown
Contributor Author

Thanks @akimfomin. The overshoot is intentional: the cap stays soft, as note_title_write had it on main before this PR (it checked title_write_back_bytes >= kTitleWriteBackBytes before adding). The range that crosses the cap is kept whole, because cutting it would drop GPU writes inside it. The next range that adds bytes is refused.

7ec666e states this in src/title_write_ranges.h and pins your case in title_write_ranges_test:

  • cap 64, [0,63), then [128,132) is accepted and the count goes 63 -> 67;
  • the next range that adds a byte ([200,201)) is refused, and so is a merge that adds bytes ([60,70));
  • refusals leave the map and the count unchanged;
  • a covered range ([129,131)) still fits.

The test passes. AI-assisted (Claude Code).

@timbot103

Copy link
Copy Markdown
Contributor

Reviewed at updated head 7ec666eef on Windows 11 (MSVC 2022 19.44).

Verification

  • Built title_write_ranges_test and gta6rt.
  • title_write_ranges_test: PASS (including the new soft byte-cap 63 → 67 regression case added in 7ec666eef).

Notes

  • Interval merging, union byte delta accounting, soft byte-cap semantics, range-cap behavior on bridges, and production mutex/reset usage in d3d12_vulkan_device.cpp (3742-3774) all look correct, and the drive-by if (!&context) → if (!context) fix is a nice catch.
  • Optional hardening (title_write_ranges.h:35-41): When an incoming range is already covered (added == 0), ranges.erase(first, last); ranges.emplace(lo, hi); still frees and reallocates the map node (and if emplace throws after erase, previously accepted ranges are lost while bytes still counts them). Returning true early when added == 0 and reusing an extracted node for merges (ranges.extract(first) or updating first->second in place when lo == first->first) makes covered checks and merges allocation-free and strong-exception-safe.

Approve as-is; the node-reuse tweak is optional.

AI-assisted review + local MSVC verification.

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the diff and tested merged onto main (493867366) with Ninja Release (MSVC) and LEONIDA_VK_VALIDATION=1.

Extracting leonida::add_reached_range into runtime/src/title_write_ranges.h cleanly keeps the map disjoint across prefix/multi-entry merges, tracks the exact union byte count ((hi - lo) - covered), and respects both range and soft byte caps; fixing if (!&context) to if (!context) in write_back_titles is a nice catch too.

Tests run

  • ctest --output-on-failure -R "^(title_write_ranges|d3d12_placed_write_back)$" (with LEONIDA_VK_VALIDATION=1): 2/2 passed (title_write_ranges, d3d12_placed_write_back 0.25 s).

GPUs tested: NVIDIA GeForce RTX 5070 12 GB (Windows 11, driver 610.88, Vulkan 1.4.341, VK_LAYER_KHRONOS_validation 1.4.363).

AI-assisted (Antigravity, Gemini).

@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#2517 | head 7ec666e (merged cleanly with main 3ca0707) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(title_write_ranges|title_write_watch|title_write_watch_on|d3d12_existing_heap)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) | red on main: title_write_ranges_test.cpp fails to compile (C1083: missing title_write_ranges.h; on main, note_title_write dropped prefixes before existing entries, left bridging ranges overlapping, undercounted merged bytes, and checked if (!&context)) | green on PR+main: 4/4 CTests pass (title_write_ranges including the 63 -> 67 soft byte-cap check, title_write_watch 18/18, title_write_watch_on 18/18, and d3d12_existing_heap on RTX 3090); check_no_game_data.py 0 violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants