d3d12: copy shader writes in a title window back to the title's pages (TITLE-GPU-WRITE-BACK) #2032

Closed
BubsMcgee wants to merge 2 commits from upstream-pr/2032 into main
BubsMcgee commented 2026-11-25 18:35:56 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by BubsMcgee on 2026-11-25T18:35:56Z.


Ports the first gap from DexNom/VIctory (GPL-3.0-or-later), commits
cd94d35 and 940d32f, reworked for this layer.

The gap

Our title-memory copies are one way: ShadowPages copies the title's pages into the GPU copies before every
submission, and nothing copies a shader's writes back. A UAV/raw-address store into a title window landed in the copy
and was invisible to the title (and could be overwritten by the next refresh).

What this does

  • runtime/src/title_write_back.h: a registry of title windows (their title range and host copy) and the ranges a
    shader wrote, split at window edges, merged when adjacent, taken once for copy-back.
  • make_title_window registers the window; write_buffer_uav marks the buffer's range when a view that names the
    title's pages is made.
  • After the refresh in set_before_execute, flush_title_write_backs copies each marked range back. The refresh
    has just put every page the title wrote into the same copies, so a page copied back cannot lose a title write; the
    copy-back waits for the device first, and skips pages the title has no readable mapping for.

Limits and gating

  • LEONIDA_TITLE_WRITE_BACK=1 enables it; default off until a retail A/B.
  • Title windows only. A slot-133 shadow copy is not a window and is not registered yet (next stage).
  • The copy-back waits for the device instead of using the queue's timeline; that is the safe first form, not the
    fast one. Shader-side page marks (VIctory's view_marks) are the optimization once this is proven.

Tests

  • New title_write_back_test (9 checks): marks split at window edges, merge when adjacent, respect the window end,
    are taken once, and ignore addresses outside every window. title_write_back: OK.
  • The shim target builds; no behaviour changes with the switch unset.

Renamed to this repository's conventions (no VIctory file names). Attribution is in the header comment. AI-assisted.

Claimed as TITLE-GPU-WRITE-BACK in the coordination issue.

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5497009839 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2032). Originally posted by [BubsMcgee](https://github.com/BubsMcgee) on 2026-11-25T18:35:56Z. --- Ports the first gap from [DexNom/VIctory](https://github.com/DexNom/VIctory) (GPL-3.0-or-later), commits `cd94d35` and `940d32f`, reworked for this layer. ## The gap Our title-memory copies are one way: `ShadowPages` copies the title's pages into the GPU copies before every submission, and nothing copies a shader's writes back. A UAV/raw-address store into a title window landed in the copy and was invisible to the title (and could be overwritten by the next refresh). ## What this does - `runtime/src/title_write_back.h`: a registry of title windows (their title range and host copy) and the ranges a shader wrote, split at window edges, merged when adjacent, taken once for copy-back. - `make_title_window` registers the window; `write_buffer_uav` marks the buffer's range when a view that names the title's pages is made. - After the refresh in `set_before_execute`, `flush_title_write_backs` copies each marked range back. The refresh has just put every page the title wrote into the same copies, so a page copied back cannot lose a title write; the copy-back waits for the device first, and skips pages the title has no readable mapping for. ## Limits and gating - `LEONIDA_TITLE_WRITE_BACK=1` enables it; default off until a retail A/B. - Title windows only. A slot-133 shadow copy is not a window and is not registered yet (next stage). - The copy-back waits for the device instead of using the queue's timeline; that is the safe first form, not the fast one. Shader-side page marks (VIctory's `view_marks`) are the optimization once this is proven. ## Tests - New `title_write_back_test` (9 checks): marks split at window edges, merge when adjacent, respect the window end, are taken once, and ignore addresses outside every window. `title_write_back: OK`. - The shim target builds; no behaviour changes with the switch unset. Renamed to this repository's conventions (no VIctory file names). Attribution is in the header comment. AI-assisted. Claimed as `TITLE-GPU-WRITE-BACK` in the coordination issue.
BubsMcgee commented 2026-11-25 19:12:47 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by BubsMcgee on 2026-11-25T19:12:47Z.


Ready for review (gated, LEONIDA_TITLE_WRITE_BACK default off): marked title write-back ranges copy shadow bytes to title memory after the refresh, with a 9-check unit test ( itle_write_back_test). Scope is title windows only; slot-133 windows are not registered yet, and the copy waits on kDeviceWaitIdle rather than a timeline point. From DexNom/VIctory cd94d35/940d32f. Mergeable; red status is the checks backlog.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7171974542 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2032#issuecomment-7171974542). Originally posted by [BubsMcgee](https://github.com/BubsMcgee) on 2026-11-25T19:12:47Z. --- Ready for review (gated, LEONIDA_TITLE_WRITE_BACK default off): marked title write-back ranges copy shadow bytes to title memory after the refresh, with a 9-check unit test ( itle_write_back_test). Scope is title windows only; slot-133 windows are not registered yet, and the copy waits on kDeviceWaitIdle rather than a timeline point. From DexNom/VIctory cd94d35/940d32f. Mergeable; red status is the checks backlog.
Snooozs commented 2026-11-25 20:02:18 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Snooozs on 2026-11-25T20:02:18Z.


From the code at this head, not run:

The copy-back seems to run before the writes it should bring back. flush_title_write_backs() is called in before_execute_ (d3d12_vulkan_device.cpp:10924), before that submission replays. The marks come from the view writer, when a UAV over title memory is made, and take() empties the list at the next flush. A UAV is usually made before the dispatch that writes through it, so for a view used over several frames:

  • submission 1: the flush takes the mark and copies the copy, which the shader has not written yet, to the title's pages; then the shader writes the copy;
  • submission 2: no mark is left, so nothing is copied.

Marking at each dispatch that binds the UAV (or re-marking in the flush while the view is live), and copying after the submission has finished (a fence, next submission's start), would order it the way the description says. The test in title_write_back_test covers the registry's split and merge, not this order.

Two smaller ones: the flush copies whole pages with no check for title writes since the refresh, so a write the title makes between the refresh and the memcpy is overwritten (the comment rules that out for pages the refresh saw, not for later ones); and vkDeviceWaitIdle runs on the submit thread for any submission after a view was made.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7173100014 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2032#issuecomment-7173100014). Originally posted by [Snooozs](https://github.com/Snooozs) on 2026-11-25T20:02:18Z. --- From the code at this head, not run: **The copy-back seems to run before the writes it should bring back.** `flush_title_write_backs()` is called in `before_execute_` (`d3d12_vulkan_device.cpp:10924`), before that submission replays. The marks come from the view writer, when a UAV over title memory is made, and `take()` empties the list at the next flush. A UAV is usually made before the dispatch that writes through it, so for a view used over several frames: - submission 1: the flush takes the mark and copies the copy, which the shader has not written yet, to the title's pages; then the shader writes the copy; - submission 2: no mark is left, so nothing is copied. Marking at each dispatch that binds the UAV (or re-marking in the flush while the view is live), and copying after the submission has finished (a fence, next submission's start), would order it the way the description says. The test in `title_write_back_test` covers the registry's split and merge, not this order. Two smaller ones: the flush copies whole pages with no check for title writes since the refresh, so a write the title makes between the refresh and the `memcpy` is overwritten (the comment rules that out for pages the refresh saw, not for later ones); and `vkDeviceWaitIdle` runs on the submit thread for any submission after a view was made. AI-assisted (Claude Code, Claude Opus 5.5).
qhukz13 commented 2026-11-25 22:59:08 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by qhukz13 on 2026-11-25T22:59:08Z.


Review at f3c6c18b7 (code reading plus a run on a GTX 1050 Ti, see the end). The second commit answers Snooozs's ordering point: the flush now waits for the earlier submissions before the refresh, and the descriptor-table scan marks the views a submission binds. Licence is fine (Leonida is GPL-3.0 too). Three things I'd look at before the switch defaults on:

  1. A mark at the GPU-only address misses its window. make_title_window registers the window at source, which resolve_pool_memory takes from title_gpu_views::to_host_view(va), the CPU view. mark_view in the table scan (and register_title_view from write_buffer_uav) marks the raw V# base / view address. For a pool reached through its GPU-only twin that is the GPU address, so mark_vsharp finds no window and the write is not copied back. Passing the address through to_host_view() in add_view / mark_vsharp (and note_rebase) would key both sides the same way.
  2. readable_page() is checked, then the copy writes. It accepts PAGE_READONLY (and the read-only protection the layer's own watches put on title pages). A page the layer guards goes through the fault handler; a page the title itself mapped read-only (XMemMapPhysicalPages / VirtualProtect without write) would be an access violation in flush_title_write_backs on the submit thread. Checking for a writable protection, or writing through a guarded copy as the read side does (guarded_copy.h), would turn that into a skipped page.
  3. A GPU write in a page the title also wrote since the last refresh is lost. The order is wait -> refresh -> flush. The refresh copies a page the title wrote into the copy whole, over the shader's bytes in that page, and the flush then copies those title bytes back. With a page shared at finer grain (a title-written header next to GPU-written counters) the shader's writes vanish. Snooozs's point was the window between refresh and memcpy; this one is earlier. A per-page snapshot of the copy at the previous refresh would let the flush keep only the bytes the GPU changed; at least the limitation is worth a line in the header comment.

Minor: wait_title_write_backs waits on submitted_retire_ticket() at every full boundary while anything is live, which serialises the submit thread with the GPU for the whole session once one title window view exists. Fine behind the switch, but it may be what a retail A/B measures.

Run on a GTX 1050 Ti (Windows 10 Pro 19045, NVIDIA 582.66, MSVC RelWithDebInfo), this head merged onto main 5fab93967 (clean): title_write_back, d3d12_unmapped_vsharp, d3d12_console_descriptor_heap, d3d12_title_va, d3d12_title_window_cap, d3d12_api_descriptor_copy, d3d12_rebound_tables, d3d12_title_index_window, d3d12_title_vertex_window, xbox_geometry_upload_batch: 10/10, 3 runs each, plain and with LEONIDA_VK_VALIDATION=1, with the switch unset and with LEONIDA_TITLE_WRITE_BACK=1. No validation messages. With the switch on, no title write-back: ... copied line appears in any of them, so these tests show the switch does no harm but do not reach a copy-back; a synthetic case (a UAV over a title window that a dispatch writes, then the title page read back on the CPU after the next submission) would cover the ordering end to end, and would also be the place to test point 1 with a GPU-only twin.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7177013091 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2032#issuecomment-7177013091). Originally posted by [qhukz13](https://github.com/qhukz13) on 2026-11-25T22:59:08Z. --- Review at `f3c6c18b7` (code reading plus a run on a GTX 1050 Ti, see the end). The second commit answers Snooozs's ordering point: the flush now waits for the earlier submissions before the refresh, and the descriptor-table scan marks the views a submission binds. Licence is fine (Leonida is GPL-3.0 too). Three things I'd look at before the switch defaults on: 1. **A mark at the GPU-only address misses its window.** `make_title_window` registers the window at `source`, which `resolve_pool_memory` takes from `title_gpu_views::to_host_view(va)`, the CPU view. `mark_view` in the table scan (and `register_title_view` from `write_buffer_uav`) marks the raw V# base / view address. For a pool reached through its GPU-only twin that is the GPU address, so `mark_vsharp` finds no window and the write is not copied back. Passing the address through `to_host_view()` in `add_view` / `mark_vsharp` (and `note_rebase`) would key both sides the same way. 2. **`readable_page()` is checked, then the copy writes.** It accepts `PAGE_READONLY` (and the read-only protection the layer's own watches put on title pages). A page the layer guards goes through the fault handler; a page the title itself mapped read-only (`XMemMapPhysicalPages` / `VirtualProtect` without write) would be an access violation in `flush_title_write_backs` on the submit thread. Checking for a writable protection, or writing through a guarded copy as the read side does (`guarded_copy.h`), would turn that into a skipped page. 3. **A GPU write in a page the title also wrote since the last refresh is lost.** The order is wait -> refresh -> flush. The refresh copies a page the title wrote into the copy whole, over the shader's bytes in that page, and the flush then copies those title bytes back. With a page shared at finer grain (a title-written header next to GPU-written counters) the shader's writes vanish. Snooozs's point was the window between refresh and `memcpy`; this one is earlier. A per-page snapshot of the copy at the previous refresh would let the flush keep only the bytes the GPU changed; at least the limitation is worth a line in the header comment. Minor: `wait_title_write_backs` waits on `submitted_retire_ticket()` at every full boundary while anything is live, which serialises the submit thread with the GPU for the whole session once one title window view exists. Fine behind the switch, but it may be what a retail A/B measures. Run on a GTX 1050 Ti (Windows 10 Pro 19045, NVIDIA 582.66, MSVC RelWithDebInfo), this head merged onto main `5fab93967` (clean): `title_write_back`, `d3d12_unmapped_vsharp`, `d3d12_console_descriptor_heap`, `d3d12_title_va`, `d3d12_title_window_cap`, `d3d12_api_descriptor_copy`, `d3d12_rebound_tables`, `d3d12_title_index_window`, `d3d12_title_vertex_window`, `xbox_geometry_upload_batch`: 10/10, 3 runs each, plain and with `LEONIDA_VK_VALIDATION=1`, with the switch unset and with `LEONIDA_TITLE_WRITE_BACK=1`. No validation messages. With the switch on, no `title write-back: ... copied` line appears in any of them, so these tests show the switch does no harm but do not reach a copy-back; a synthetic case (a UAV over a title window that a dispatch writes, then the title page read back on the CPU after the next submission) would cover the ordering end to end, and would also be the place to test point 1 with a GPU-only twin. AI-assisted (Claude Code).
OreoFR commented 2026-11-29 12:42:29 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-29T12:42:29Z.


@BubsMcgee this PR now conflicts with main (runtime/shims/d3d12_vulkan_device.cpp). Could you merge main into your branch (no force-push needed)? It was verified before the conflict: https://github.com/ProjectLeonida/mandy/pull/2032#issuecomment-7177013091. If there's no update within 24 h, OreoFR may carry it in a new PR that keeps you as co-author (per the maintainers' plan). — OreoFR's agent (AI-assisted)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7256006036 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2032#issuecomment-7256006036). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-29T12:42:29Z. --- @BubsMcgee this PR now conflicts with `main` (`runtime/shims/d3d12_vulkan_device.cpp`). Could you merge `main` into your branch (no force-push needed)? It was verified before the conflict: https://github.com/ProjectLeonida/mandy/pull/2032#issuecomment-7177013091. If there's no update within 24 h, OreoFR may carry it in a new PR that keeps you as co-author (per the maintainers' plan). — OreoFR's agent (AI-assisted)
aslan2142 commented 2026-12-01 16:41:20 +00:00 (Migrated from github.com)

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


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:7318038656 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2032#issuecomment-7318038656). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:41:20Z. --- 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!2032
No description provided.