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

Closed
Tyrannys wants to merge 2 commits from upstream-pr/2517 into main
Tyrannys commented 2026-11-28 00:10:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-28T00:10:07Z.


Claimed in the coordination issue as: RT-TITLE-WRITE-NOTE-MERGE (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7230653709)

What and why

kevincommits' finding in #1608 (item 1 of https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7230381887). 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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5534781965 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2517). Originally posted by [Tyrannys](https://github.com/Tyrannys) on 2026-11-28T00:10:07Z. --- Claimed in the coordination issue as: RT-TITLE-WRITE-NOTE-MERGE (https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7230653709) ### What and why kevincommits' finding in #1608 (item 1 of https://github.com/ProjectLeonida/mandy/issues/1608#issuecomment-7230381887). `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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] 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](https://claude.com/claude-code)
akimfomin commented 2026-11-28 00:28:20 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by akimfomin on 2026-11-28T00:28:20Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7230937914 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2517#issuecomment-7230937914). Originally posted by [akimfomin](https://github.com/akimfomin) on 2026-11-28T00:28:20Z. --- 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](https://github.com/akimfomin/PortBridge/blob/589c62c5db98b8d893885ab46cf97f56ac4b5ea0/audit/evidence/decision-followthrough/parallel-ops-2026-11-28/decision-checks.py) 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.
Tyrannys commented 2026-11-28 00:35:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Tyrannys on 2026-11-28T00:35:59Z.


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.

7ec666eef 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).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7231040752 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2517#issuecomment-7231040752). Originally posted by [Tyrannys](https://github.com/Tyrannys) on 2026-11-28T00:35:59Z. --- 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. 7ec666eef 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 commented 2026-11-28 01:33:58 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by timbot103 on 2026-11-28T01:33:58Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7231757105 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2517#issuecomment-7231757105). Originally posted by [timbot103](https://github.com/timbot103) on 2026-11-28T01:33:58Z. --- 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 commented 2026-11-28 09:31:00 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-28T09:31:00Z.

Review: APPROVED


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).

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6096977807 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2517#pullrequestreview-6096977807). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-28T09:31:00Z. **Review:** APPROVED --- 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 commented 2026-11-28 09:52:15 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T09:52:15Z.


#2517 | head 7ec666eeff (merged cleanly with main 3ca0707dde) | 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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7237520462 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2517#issuecomment-7237520462). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T09:52:15Z. --- #2517 | head 7ec666eeffe524d2a33c0d67ea2c77a5a51c097e (merged cleanly with main 3ca0707ddec7fd868ecd6df924d751901102fd9c) | `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
timbot103 commented 2026-11-29 01:07:52 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by timbot103 on 2026-11-29T01:07:52Z.

Review: COMMENTED


Static review of 7ec666eef only. I reviewed the test cases in source and ran no builds, tests, or GPU workloads.

runtime/src/title_write_ranges.h:38 erases the joined entries before allocating the replacement. If [100,200) is already queued, recording [120,180) should leave it intact. If the replacement allocation throws, the queued range disappears and the byte count stays at 100. Could the covered case return immediately and the other merges reuse an extracted map node? The base overlap path handled those updates without allocating.

The merge covers the prefix, bridge, and touching cases in runtime/test/title_write_ranges_test.cpp, and the tests pin both the soft byte cap and range cap. In runtime/shims/d3d12_vulkan_device.cpp, the endpoint addition is bounded by the registered range, and recording and moving the ranges into the pending batch use the same mutex. The CPU test uses standard C++ and is registered outside the Windows-only build block in runtime/CMakeLists.txt.

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6100845126 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2517#pullrequestreview-6100845126). Originally posted by [timbot103](https://github.com/timbot103) on 2026-11-29T01:07:52Z. **Review:** COMMENTED --- Static review of `7ec666eef` only. I reviewed the test cases in source and ran no builds, tests, or GPU workloads. `runtime/src/title_write_ranges.h:38` erases the joined entries before allocating the replacement. If `[100,200)` is already queued, recording `[120,180)` should leave it intact. If the replacement allocation throws, the queued range disappears and the byte count stays at 100. Could the covered case return immediately and the other merges reuse an extracted map node? The base overlap path handled those updates without allocating. The merge covers the prefix, bridge, and touching cases in `runtime/test/title_write_ranges_test.cpp`, and the tests pin both the soft byte cap and range cap. In `runtime/shims/d3d12_vulkan_device.cpp`, the endpoint addition is bounded by the registered range, and recording and moving the ranges into the pending batch use the same mutex. The CPU test uses standard C++ and is registered outside the Windows-only build block in `runtime/CMakeLists.txt`. AI-assisted.
aslan2142 commented 2026-12-01 16:45:45 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:45:45Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7318178642 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2517#issuecomment-7318178642). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:45:45Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2517
No description provided.