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

Open
github-bot wants to merge 2 commits from github-bot/gh-2032 into main AGit
Owner

@⁠BubsMcgee on GitHub · 2026-11-25 18:35 UTC · original
BubsMcgee:fix/title-gpu-write-back → main · +386 −0 · 4 files · 2 commits · open

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.

**@⁠BubsMcgee** on GitHub · 2026-11-25 18:35 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2032) `BubsMcgee:fix/title-gpu-write-back` → `main` · +386 −0 · 4 files · 2 commits · open 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.
Mark a title-window buffer UAV when a descriptor table that holds it is scanned for a submission, carry the marks to the next refresh boundary, and wait on the queues' timelines (wait_retire_ticket) instead of vkDeviceWaitIdle before refreshing. A rebased V# is aliased to its view so later scans still mark it, and the registered range is the view's own, not the resource width.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:15:25 +00:00
Author
Owner

@⁠BubsMcgee on GitHub · 2026-11-25 19:12 UTC · original

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.

**@⁠BubsMcgee** on GitHub · 2026-11-25 19:12 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2032#issuecomment-7171974542) 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.
Author
Owner

@⁠Snooozs on GitHub · 2026-11-25 20:02 UTC · original

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

**@⁠Snooozs** on GitHub · 2026-11-25 20:02 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2032#issuecomment-7173100014) 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).
Author
Owner

@⁠qhukz13 on GitHub · 2026-11-25 22:59 UTC · original

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

**@⁠qhukz13** on GitHub · 2026-11-25 22:59 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2032#issuecomment-7177013091) 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).
This pull request has changes conflicting with the target branch.
  • runtime/shims/d3d12_vulkan_device.cpp
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2032/head:github-bot/gh-2032'
git switch 'github-bot/gh-2032'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2032'
git switch 'github-bot/gh-2032'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2032'
git switch 'github-bot/gh-2032'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2032'
git switch 'main'
git merge --squash 'github-bot/gh-2032'
git switch 'main'
git merge --ff-only 'github-bot/gh-2032'
git switch 'main'
git merge 'github-bot/gh-2032'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.