Skip to content

d3d12_xs: queue slot 31 is CopyPageMappingsBatchX, mapped through xmem (RT-QUEUE31) - #968

Merged
aslan2142 merged 5 commits into
ProjectLeonida:mainfrom
CLHelper:runtime/queue-slot31
Nov 25, 2026
Merged

aslan2142 merged 5 commits into
ProjectLeonida:mainfrom
CLHelper:runtime/queue-slot31

Conversation

@CLHelper

@CLHelper CLHelper commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-QUEUE31 (#14 (comment))

What and why

The retail title calls its command queue's slot 31, one of the console-only slots past ID3D12CommandQueue, as CopyPageMappingsBatchX(count, batches, pool, pool pages, flags). main refuses it with E_NOTIMPL (the XboxID3D12CommandQueue::Slot31 unsupported lines in the #432 and #602 runs). The title does not test the result: it goes on to use the destinations as if the pages were mapped there.

The shape, read statically from my own 1.0.2.1 copy (RVAs and instruction shapes only):

  • game_scarlett_master.exe+0xf2cc7f: call [vtable + 0xf8] with EDX the batch count, R8 the batches, R9 the base of the title's physical-page pool, the pool's size in 64 KiB pages as the fifth argument and 0 as the sixth.
  • +0xf2ca74 fills a batch as {UINT64 destination; UINT32 range count; const range* ranges} (24 bytes) and a range as {UINT32 kind, start, count} (12 bytes).
  • Kinds: 0 maps pool pages start, start + 1, ...; 1 maps pool page start at every destination page; 2 leaves the destination unmapped; 3 leaves it as it is. Each range moves the destination on by count pages.
  • The same call shape is in VIctory's queue (copy_page_mappings; there the newer layout has every slot from 14 on two later than older titles, so their 29 is this 31).
  • In a boot, the pool size argument equals the page count of the title's own XMemAllocatePhysicalPages call.

The change:

  • xmem.cpp: copy_page_mappings (exposed as rt::xmem::copy_page_mappings_fn in xmem.h) maps at each destination page the physical page that is mapped at its source page, as a second view of the same section, so both addresses are one memory, exactly what two XMemMapPhysicalPages calls with one page give. Every source page is looked up before any destination page changes, so an overlapping range maps what was there before. A source page with nothing mapped, or a null source, leaves the destination unmapped.
  • d3d12_xs_device.cpp: queue slot 31 walks the batches and hands each range to that hook. It returns S_OK when every range was applied and E_INVALIDARG when one was refused (outside the pool, an unknown kind, or xmem refusing it, logged once). When xmem is not linked (tests that compile the proxy alone) the slot is refused as before, so check_tail is unchanged.

Known gap (raised in review): the mappings change when the call is made, while on the console the batch takes effect in queue order. The title does not drain the queue before the call in the function that makes it (XMemMapPhysicalPagesScatter at +0xf2cc3e, then slot 31 at +0xf2cc7f); whether earlier work can still read a destination page then is not measured. A strict version would apply each batch when the queue reaches it.

Not in scope: the console maps GPU page tables, the layer maps CPU views. Whether the GPU sees a destination page follows from how that window reaches the GPU, the same as for any XMemMapPhysicalPages window; this pull request does not change that.

Tested

Native Windows 11, MSVC 19.44 (VS 2022 Build Tools), Ninja, RelWithDebInfo, on main 3d3770c16 (rebased onto a28927849, a docs-only change):

  • xmem_test: new test_copy_page_mappings (a run, one page repeated, a run ending in an unmapped pool page, a null source, an overlapping range, a destination outside any window, unaligned destination and source); a write through the destination is read back through the pool. All checks pass.
  • xs_proxy_mock_test: new check_queue_copy_page_mappings with a recording stand-in for the hook (the four kinds, the destination moving on past a kind-3 range, a range past the pool, an unknown kind, xmem refusing, no batches, and the old refusal without xmem). PASS.
  • Mutation check: making xmem ignore repeat_first fails xmem_test; not moving the destination on for kind 3 fails xs_proxy_mock_test.
  • On the retail boot (RTX 4060 Laptop, NVIDIA 576.80, @DadaCap's boot-prototype fa497b930 with an earlier local version of the same mapping): by the 32nd call every page the title sent was handled (3,150 mapped, 211 unmapped, none refused). What it changes further on I cannot say yet: on that machine the run ends at about 25 s on a lost Vulkan device first, which this does not touch.

GPUs tested: none needed for the two tests (no GPU); the boot above on an RTX 4060 Laptop, NVIDIA 576.80, Windows 11.

AI-assisted: yes

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)

…m (RT-QUEUE31)

The retail title calls its command queue's slot 31 (game_scarlett_master.exe+0xf2cc7f) with a batch count, 24-byte
batches {destination, range count, ranges}, the base and page count of its physical-page pool, and 0. Each 12-byte
range {kind, start, count} maps pool pages at the destination: 0 a run, 1 one page repeated, 2 unmapped, 3 unchanged.
main refused the slot with E_NOTIMPL; the title does not test the result and goes on to read the destinations.

Each range now reaches rt::xmem::copy_page_mappings_fn, which maps the physical page behind every source page at its
destination page (a second view of the same section, so both addresses are one memory) and unmaps where the source has
nothing or the kind is 2. The hook is set by xmem.cpp; tests that compile the proxy alone keep the old refusal.

Tests: xmem_test (a run, one page repeated, an unmapped source page, a null source, an overlapping range, bad
addresses) and xs_proxy_mock_test (the four kinds, a range past the pool, an unknown kind, xmem refusing, no xmem).

AI-assisted.
@Snooozs

Snooozs commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Independent check: xs_proxy_mock* (6) and xmem pass; merged together with #958 they also pass (7/7). With #969 there is a textual conflict in runtime/shims/xmem.h only: both add a hook at the end of the namespace.

One question from reading: the call is a queue operation, but the mappings change at once on the calling thread, while work submitted earlier on that queue may still be reading the old mapping. Is that known to be safe for the title's use (for example because it fences first), or should it wait for the queue's submitted work? I have no measurement either way.

Windows 10 Pro 22H2 (19045), VS 2022 RelWithDebInfo, merged onto main 3295055 without conflicts, targets rebuilt, CPU only, no game files. AI-assisted (Claude Code): I read the whole diff and ran this on that machine.

@oscarlosu-lab

Copy link
Copy Markdown
Contributor

Tested f2268abea (RT-QUEUE31, CopyPageMappingsBatchX) cherry-picked onto #984 (ac7c3449d) on native Windows 11 (MSVC 19.52 x64 RelWithDebInfo, -DLEONIDA_SDL3_FETCH=ON, RTX 3060 12 GB driver 621.35, 32 GB RAM):

  • xmem_test.exe: OK (316 checks), including all copy_page_mappings and CopyPageMappingsBatchX tests.
  • Retail boot (421 s, 28352 translated shaders, local history.bcph):
    • queue slot 31 (CopyPageMappingsBatchX) first call: 1 batch(es), pool 0x... of 63632 pages
    • 46 XboxID3D12CommandQueue::Slot31 calls logged, 0 refusal / not mapped errors.
    • PresentX ok 9000, failed 0, 0 device loss, 40/40 non-black captures (brightest 168.77 144.08 126.01).
    • States reached t=95s game=3 session=d (remained at session=d through t=415s).

@CLHelper

Copy link
Copy Markdown
Contributor Author

@Snooozs thanks for the check, and @oscarlosu-lab for the retail run on #984 (46 calls, 0 refusals on a 12 GB card; on my 8 GB laptop the same port handled every call too).

On ordering: you are right that this applies the mappings at call time, while on the console the batch is a queue operation and takes effect in queue order. What I can say from the executable (static, my own 1.0.2.1 copy):

  • In the function that builds the batches (+0xf2c470), the call to slot 31 (+0xf2cc7f) comes right after XMemMapPhysicalPagesScatter (+0xf2cc3e), which remaps pool pages on the CPU at once, and there is no fence wait between them in that function. So the title itself does not drain the queue there.
  • Whether earlier work can still be reading a destination page at that point depends on the title's frame and fence scheme elsewhere; I have not measured that, and I do not want to claim it is safe.

So it is a known gap, and I will put it in the description. If it shows up as corruption, the strict version is to apply each batch when the queue reaches it (signal a fence after the work already submitted, apply on its completion, and hold the next submission on that queue until it is applied). I would rather add that with a run that shows the difference than without one; if you or anyone sees stale or torn pages that fit this, tell me and I will do it in this pull request.

On #969: the xmem.h conflict is only the two hooks at the end of the namespace; whichever lands second keeps both.

AI-assisted.

@oscarlosu-lab

Copy link
Copy Markdown
Contributor

Root cause & fix for slot-70 views staying MEM_RESERVE after CopyPageMappingsBatchX (tested on Windows 11, RTX 3060 12 GB, #984 + #968)

On Windows, the title reserves a CPU-mappable view and a paired GPU-only view of the same size (title_gpu_views::note_reservation), and CopyPageMappingsBatchX (queue slot 31) targets the GPU-only view. Because copy_page_mappings only mapped physical pages onto destination, the paired CPU view stayed MEM_RESERVE (Protect = 0), so texture_memory_readable() (d3d12_vulkan_device.cpp) kept waiting on those slot-70 texture views.

Minimal diff (runtime/shims/xmem.cpp)

Mirror map_page / unmap_page onto leonida::title_gpu_views::to_cpu(to, pages * kPhysicalPageSize) when cpu_to != to and a window exists for the CPU alias, and fall back to to_cpu(src_va) when looking up source pages in g_mapped:

    const std::lock_guard lock(g_phys_mutex);
    const uintptr_t cpu_to = leonida::title_gpu_views::to_cpu(to, pages * kPhysicalPageSize);
    const Window* const cpu_window = (cpu_to != to) ? find_window(cpu_to, pages * kPhysicalPageSize) : nullptr;
    if (!source) {
        for (uint64_t i = 0; i < pages; ++i) {
            if (!unmap_page(to + i * kPhysicalPageSize)) return false;
            if (cpu_window && !unmap_page(cpu_to + i * kPhysicalPageSize)) return false;
        }
        return true;
    }
    const Window* const window = find_window(to, pages * kPhysicalPageSize);
    if (!window) {
        SetLastError(ERROR_INVALID_ADDRESS);
        return false;
    }
    std::vector<uint64_t> found(static_cast<size_t>(pages), 0);
    for (uint64_t i = 0; i < pages; ++i) {
        const uintptr_t src_va = from + (repeat_first ? 0 : i) * kPhysicalPageSize;
        auto it = g_mapped.find(src_va);
        if (it == g_mapped.end()) {
            const uintptr_t cpu_src = leonida::title_gpu_views::to_cpu(src_va, kPhysicalPageSize);
            if (cpu_src != src_va) it = g_mapped.find(cpu_src);
        }
        if (it != g_mapped.end()) found[static_cast<size_t>(i)] = it->second;
    }
    const DWORD protect = view_protect(*window);
    const DWORD cpu_protect = cpu_window ? view_protect(*cpu_window) : 0;
    for (uint64_t i = 0; i < pages; ++i) {
        const uint64_t page = found[static_cast<size_t>(i)];
        if (!map_page(to + i * kPhysicalPageSize, page, protect)) return false;
        if (cpu_window && !map_page(cpu_to + i * kPhysicalPageSize, page, cpu_protect)) return false;
    }
    return true;

Measured impact (RTX 3060 12 GB, Windows 11, over PR #984 + #968)

  • xmem_test.exe: OK (325 checks), including 9 new checks verifying VirtualQuery == MEM_COMMIT, bidirectional read/write coherence, and mirrored unmapping on a paired CPU + GPU-only reservation.
  • Live boot (420 s, LEONIDA_SKIP_INT3=1):
    • slot-70 views awaiting readable memory: 86 → 40 (-53.5%)
    • over memory not readable yet: 64 → 26 (-59.4%), with 562 made, 2,309 fills (6,618.6 MiB, 0 failed), PresentX ok 8912, failed 0, 40/40 non-black captures.
  • Note on the remaining 26 views: this is a partial improvement — the title still remains in session=d (no ExecuteIndirect within 420 s). In a separate 130 s diagnostic run checking found == 0 in copy_page_mappings, 0 calls had missing source pages (g_mapped hit every source page in those first 130 s, though later calls past 130 s were not sampled), so the remaining 26 unreadable views appear to be ranges not yet mapped by CopyPageMappingsBatchX / XMemMapPhysicalPages.

@azsekazvamdimitar

Copy link
Copy Markdown
Contributor

Verified under Wine and Proton, which the earlier batches could not run: #968 at f2268abea, merged on main 6293c1c4f, clang-cl + xwin cross build, RTX 3090 (NVIDIA 590.48.01), WineHQ 11.22 (winewayland) and GE-Proton11-14 (winex11).

tests main main + #968 main + #968 + #983
xmem 197 checks, 2 fail 217 checks, 2 fail pass
xs_proxy_mock and its five variants (no_reserve, no_protect, no_handler, env_slots, env_slots_invalid) pass pass pass

Same result under both Wines. The 20 new copy_page_mappings checks pass under Wine. The two failures are the ones main has: Wine raises no fault on a reserve-only view, so the first touch never commits the page (a mapped page is a read-write view with the window's protection, the page mapped there is a plain read-write view). #983 handles them, and with it merged too the run is green. The two merge cleanly. Not run on Windows or with the retail package. AI-assisted.

aslan2142 and others added 4 commits November 24, 2026 20:09
Merges the local m984-on-main (1d89b20), main with ProjectLeonida#984
merged, into ProjectLeonida#968's head f2268ab.

Conflicts and their resolutions:

- runtime/shims/xmem.h: both sides appended to the end of rt::xmem. Kept
  geometry_aliases and transfer_physical from ProjectLeonida#984 and appended ProjectLeonida#968's
  CopyPageMappingsFn typedef and the inline copy_page_mappings_fn after them.
- runtime/shims/xmem.cpp: copy_page_mappings and the self-registering
  g_copy_page_mappings_set stay right after XMemMapPhysicalPagesScatter,
  followed by ProjectLeonida#984's shadows_write_watch. One-line port: ProjectLeonida#984's g_mapped stores
  the page number itself (HostUnorderedMap<uintptr_t, uint64_t>), so
  `it->second.page` becomes `it->second`.
- runtime/shims/d3d12_xs_device.cpp:
  - includes: ProjectLeonida#968's "xmem.h" beside "d3d12_trace.h", then ProjectLeonida#984's includes;
  - constants: ProjectLeonida#984's block kept, kQueueCopyPageMappingsBatchX = 31 added after
    kQueueExecuteCommandListsX and its static_assert;
  - the slot-31 section (ConsolePageRange/Batch, copy_console_page_range,
    queue_copy_page_mappings) goes after ProjectLeonida#984's queue_execute_command_lists and
    before the "PresentX is at queue slot 30" comment;
  - child_slot: ProjectLeonida#984's queue block kept; the slot-31 dispatch (beyond the PC
    table and with xmem's hook set) goes just before the qroutes lookup, ahead
    of the presentation routes and the console-only S_OK tail. Newer-layout
    PresentX is 32, so 31 does not clash.
- runtime/test/xs_proxy_mock_test.cpp:
  - includes: "../shims/xmem.h" beside ProjectLeonida#984's d3d12_xs_frame_events.h;
  - ProjectLeonida#984's descriptor-heap and later checks kept, check_queue_copy_page_mappings
    after them. Without xmem the merged proxy answers slot 31 with the
    console-only S_OK, so that check's `== E_NOTIMPL` becomes `== S_OK` (and its
    summary line says so);
  - main: `|| check_queue_copy_page_mappings()` appended after
    check_presentation_wiring().

runtime/test/xmem_test.cpp merged without conflict: test_copy_page_mappings runs
after test_physical_pages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Applies the fix oscarlosu-lab proposed on ProjectLeonida#968:
ProjectLeonida#968 (comment)

What was wrong: queue slot 31 (CopyPageMappingsBatchX) targets the title's
GPU-only reservation, but the D3D12 layer reads that memory at its paired CPU
view (title_gpu_views::to_cpu). copy_page_mappings mapped the pages only at the
GPU-only address, so the paired CPU view stayed reserved and the layer's reads of
that memory (the slot-70 views waiting for readable memory) never became
possible.

The change, in copy_page_mappings:
- when the destination is in a paired GPU-only range and its CPU range is a
  physical-page window, every page mapped or unmapped at the destination is
  mapped or unmapped at the CPU address as well, with that window's protection;
- a source page not mapped at a GPU-only address is looked up at its CPU
  address.

Cost: a second view per copied page, so such a page is mapped at three
addresses (pool, GPU-only, CPU) and counts as aliased: the shadow tracker copies
it at every submission. Risk: the pairing is a heuristic (the CPU reservation of
the same size made just before the GPU-only one); if it is ever wrong, the
mirror replaces the title's own mappings in that CPU range.

The comment reports 9 alias checks but does not give them, so the test is new:
xmem_test's test_copy_page_mappings_cpu_view reserves a CPU + GPU-only pair and
checks that after a batch copy onto the GPU-only range the CPU range is
committed and shows the same bytes (and a write there is one memory with the
pool), that a GPU-only source mapped only at its CPU range is found there, and
that a null source unmaps both ranges.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- docs/linux_proton.md: the branch no longer ignores slot 31; say what it
  maps now (destination and paired CPU view) and the two gaps that remain.
- docs/d3d12_vulkan.md, section 4.9: a row for queue slot 31 with how its
  argument layout was established (read statically from the executable's call
  site, corroborated by a public project's queue, the pool size matching a
  boot, as ProjectLeonida#968 states it) and the known gap (mappings apply when the call is
  made, not in queue order); the PresentX row's "every other queue slot"
  now points at it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
)

main now contains the ProjectLeonida#984 merge this branch was built on, plus its last
commits (the five tests of ProjectLeonida#984 settled under Wine, and the title's hang
watchdog staying armed by default). No conflicts: runtime/test/xmem_test.cpp
and docs/d3d12_vulkan.md merged cleanly, with test_copy_page_mappings and
test_copy_page_mappings_cpu_view each defined and called once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aslan2142
aslan2142 merged commit 0aecc5e into ProjectLeonida:main Nov 25, 2026
3 of 4 checks passed
@aslan2142

Copy link
Copy Markdown
Collaborator

Merged, right after #984, which needs this to reach the story load. Thank you, @CLHelper, and @oscarlosu-lab for the CPU-view fix.

  • Brought up to date with main including [integration] Linux/Proton and native Windows: boot through the story load (RT-PROTON-STORY-LOAD) #984 by merging, not rebasing: your commit f2268abea is untouched and nothing was force-pushed. On that tree g_mapped stores the page number itself, so it->second.page became it->second; the rest went in by placement (the slot 31 dispatch sits ahead of the presentation routes and the console-only tail).
  • Without xmem the proxy now answers slot 31 with the console-only S_OK, not E_NOTIMPL, so the first check in check_queue_copy_page_mappings expects S_OK.
  • The CPU-view fix from d3d12_xs: queue slot 31 is CopyPageMappingsBatchX, mapped through xmem (RT-QUEUE31) #968 (comment) is in its own commit: copy_page_mappings also maps and unmaps at title_gpu_views::to_cpu(destination), and looks a source page up there. The comment's alias checks were not posted, so a new xmem_test case checks that after a batch copy the paired CPU address reads the same bytes, that a write there is the same memory as the pool, that a source mapped only at its CPU address is found, and that a null source unmaps both; with the fix reverted, three of those checks fail on both devices.
  • The page trackers need no extra hook: map_page and unmap_page already report to the page aliases and the geometry-write tracking.
  • Cost: each copied page is aliased three times, so the shadow tracker copies it at every submission; if the size-based pairing is ever wrong, the mirror would overwrite the title's own CPU mappings.
  • Tests: clang-cl build, the full runtime suite under Wine on llvmpipe and an RDNA2 iGPU: no change from [integration] Linux/Proton and native Windows: boot through the story load (RT-PROTON-STORY-LOAD) #984 on the iGPU; xs_proxy_mock failed once on llvmpipe under load and passes alone (it has done so before). queue_proxy, device_proxy and every xs_proxy_mock variant pass on both devices. After the merge with main including [integration] Linux/Proton and native Windows: boot through the story load (RT-PROTON-STORY-LOAD) #984 (18a22c1ae), the full suite on both devices matches [integration] Linux/Proton and native Windows: boot through the story load (RT-PROTON-STORY-LOAD) #984's own final run exactly (306 pass and 12 fail on llvmpipe, 301 and 18 on the iGPU, 0 changed), and all 14 tests this PR touches pass on both, including the three xmem tests.
  • Open: the mappings still apply when the call is made, not in queue order, as the description says; a game run would show whether the remaining slot-70 views now get their memory. docs/linux_proton.md and docs/d3d12_vulkan.md now describe slot 31 as handled.

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

aslan2142 added a commit to Rain156/leonida-contrib that referenced this pull request Nov 25, 2026
…the ProjectLeonida#939 merge

main now holds the ProjectLeonida#984 merge (18a22c1) and ProjectLeonida#968, queue slot 31 as CopyPageMappingsBatchX. Two conflicts: the include list of d3d12_xs_device.cpp (both includes kept) and the call order in xmem_test.cpp (the two page-mapping tests of ProjectLeonida#968 run after test_physical_pages, after the early return of ProjectLeonida#939's file-pools mode). The slot 31 dispatch of ProjectLeonida#968 stays ahead of the presentation routes, so ProjectLeonida#939's rule for queue slots 30 to 32 on a device that had a presentation handler does not reach it where xmem is linked.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
aslan2142 added a commit to Rain156/leonida-contrib that referenced this pull request Nov 25, 2026
Fourteen commits main gained after ProjectLeonida#968: ProjectLeonida#413, ProjectLeonida#1114, ProjectLeonida#1090 and ProjectLeonida#1491. Two conflicts, both in the shader check lists: scripts/run_gpu_tests.py imports both sides' cases, and tests/test_shader_checks.py counts eight cases next to COMPUTE (main's cs_w32_salu_ext) and ProjectLeonida#939's IMAGE_STORE_CASES table in place of main's fixed seven store shaders.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants