Commit Graph

7 Commits

Author SHA1 Message Date
5543ba9f5f d3d12: eliminate orphan title windows, align mirror chunks, and speed up in-place heap filtering
Round 1A:
- Use VsharpPath::scratch for in-place console heaps in finish_in_place and fix_console_slot so they never allocate general-purpose title windows.
- Never fall through to make_title_window / ShadowPages in resolve_title_memory for addresses already covered by the page mirror.
- Guard vsharp_address's window-creation fallback with bytes <= kTitleWindowMax so oversized V# spans cannot allocate and reject orphan windows.
- Advance <= 2 GiB reservations in title_mirror_space::take past chunk boundaries so placed buffers stay within a single 2 GiB sparse chunk.
- Track arena-backed title windows and slot-133 shadows under ArenaUse::arena_window and report them in memory_breakdown.

Round 1C:
- Add re-entrancy-safe two-pass filter_heap_page_pieces() ready for TitleMirrorHooks::filter_pieces changed-piece mask.
- Reject guaranteed-unresolvable V# halves (null, unbacked bit-47, out-of-arena spans, non-title addresses without an alias) in O(1) inside filter_slot_locked without inserting into heap.records or deferring.
- Clear deferred slot records before finish_in_place and answer repeated V# halves from heap.records under page_mirror.mutex plus a per-batch dedup cache.

Tests:
- Added Linux unit tests in console_heap_slot_records_test (chunk alignment, arena window breakdown, deferred record clearing/answering).
- Added Wine regression tests in d3d12_heap_in_place_test and d3d12_page_mirror_test.

AI-assisted: Antigravity, Gemini 4 Neon
2026-11-27 10:51:23 -05:00
ace94aef23 Fix page mirror write-back ordering, batching, lock scope, and phase accounting
Run write_back_titles() first in before_execute_() before refresh_page_mirror()
and refresh_console_heaps(), never force-drain unretired batches, coalesce
intervals in note_title_write() and across retired batches, batch GPU readback
copies without holding page_mirror.mutex across vkWaitSemaphores, stop marking
read-only CBVs/SRVs writable, hoist page_mirror.mutex in finish_in_place(),
reuse filter_heap_page scratch, gate diagnostic VirtualQuery behind
LEONIDA_VSHARP_NOTHING_DETAIL=1, and attribute mirror refresh/flush/lock waits
to submit_phases.

AI-assisted: Antigravity (Gemini 4 Neon)
2026-11-27 09:41:01 -05:00
c1148ff101 page mirror: the write-back copies homes out with the GPU instead of reading them over the bus (RT-PAGE-MIRROR)
Run 64w spent a constant 14.5 s in a submission's shadows phase whenever new V# ranges reached the write-back. A
console V# has no read/write bit, so each new range is noted as possibly written, up to the 64 MiB cap a submission.
page_mirror_write_back then summed every 256-byte piece of those homes on the CPU. The homes are in device-local,
host-visible memory, and an uncached CPU read over the bus costs about 1.7 us per 8 bytes: 64 MiB takes 14.5 s. It
was measured here at 1.6 s for 16 MiB on the RX 7900 XTX. The bind wait was not the cause: it is a GPU-side wait.

Now the homes of the mirrored pages in the range are copied by the GPU into a host-cached buffer, 16 MiB at a time,
on the device's staging queue. The copy waits for the mirror's last bind batch and signals a timeline semaphore of
its own. The merge then reads the copy (TitleMirror::homes_in, and write_back with a staged copy). Under the
mirror's lock nothing is filled between the copy and the merge. The CPU never reads a home now.

The mirror's 10 s line gives the write-back's calls, ms each, the GPU copy's share, the longest, and the MiB copied
out.

Test: d3d12_page_mirror part 9 runs the write-back over 64 MiB with one piece written by the GPU. That piece goes
back, and the call must take under a second. It took 10 ms here, and 6.3 s before the change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 07:56:20 -05:00
5cf7f89886 page mirror: the XMemAlloc heap in the mirror's range; pages written on both sides merged at the 256-byte piece (RT-PAGE-MIRROR)
The XMemAlloc heap (src/title_heap.h) is the runtime's own over a 32 GiB reservation that xmem places in the
mirror's range. It does not use RtlCreateHeap over a given base, which faults under Wine. Size classes of 64 bytes up
to 1 KiB, then powers of two up to 32 KiB, in 1 MiB spans. Larger blocks are runs of 64 KiB pages, committed when
handed out and decommitted when freed. Freed blocks are kept on lists outside the title's memory, and one lock covers
every call. Committed pages are fed to the mirror as they are committed, so a pointer the title writes into a block
reads the block's bytes without anything naming the range first. The title sees the same as before: 64-byte
alignment, size 0 as 1 byte, and XMemFree of a block from either entry point. When the range is full, blocks come
from the host's heap as before. With LEONIDA_TITLE_HEAP=0 they come from the CRT.

Pages written on both sides: each home keeps a 64-bit sum per 256-byte piece of what both sides last agreed on.
- An upload copies only the pieces the title changed, so a piece only the GPU wrote keeps the GPU's bytes.
- Write-back copies only the pieces the GPU changed while the title's piece still matches the sum.
- A piece both sides changed keeps the title's bytes and is counted.
The mirror's 10 s line gives uploaded and written-back KiB per submission and per frame, and the count of pieces
written on both sides.

Tests:
- title_heap: classes, large blocks, and 20000 random allocations and frees checked against a reference.
- d3d12_page_mirror part 7: one page, the GPU writes piece A and the title piece B in one window, and both survive.
- d3d12_page_mirror part 8: the GPU reads XMemAlloc blocks through pointers nothing named, and a freed large block
  reads zero.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 06:18:52 -05:00
0cdc80d207 d3d12: slot-133 buffers, title windows and pool extents onto the page mirror; GPU writes into mirrored pages go back; hot/cold tracking; homes by budget (RT-PAGE-MIRROR)
Step C, with the previous WIP commit.
- Resource::create_mirror_buffer: a slot-133 buffer over title memory the mirror holds in one chunk is bound to that
  chunk at heap_base + its title address: no import, no shadow, no per-submission walk or guard of its own. Its GPU VA
  is the title address; V#s and root views naming it keep that address. Title windows and stream-pool extents are not
  made for mirrored memory (resolve_title_memory answers the mirror first, since step B).
- Write-back (DeviceState::page_mirror_write_back, from write_back_titles after the reaching submission retired): the
  256-byte pieces whose home differs from the title's page are written back through kernel_writes, on pages nothing
  wrote since they were last armed (TitleMirrorTracking::unwritten); a page written on both sides keeps the title's
  bytes, which are uploaded over the home before the next submission. Ranges are noted (note_title_write, with its
  caps) where a V#, a writable geometry range, a title-memory resolution or a BVH range is answered by the mirror.
- The mirror's tracking is HotColdTracking (src/title_mirror_hot_cold.h); it now answers unwritten() for cold pages
  from the version its guard was armed at (an additive change to that file and to the interface, default false).
- Homes go to device-local memory while VK_EXT_memory_budget keeps 1.5 GiB (or an eighth of the budget) for the
  title's textures and render targets after a unit, to host memory otherwise, decided per 32 MiB unit and logged when
  it switches; the mirror's line and the mem-probe item count units of each kind.
- The mirror's line adds hot and cold pages, write faults, hot and cold copies, and KiB uploaded a submission and a
  frame.

Not moved: the XMemAlloc heap (HeapCreate places it). RtlCreateHeap over a reservation in the mirror's range faults
under Wine (it writes the reserved base), so it stays outside the mirror; slot-133 buffers there keep their shadows.

Tests: d3d12_page_mirror_test (Wine, iGPU): a slot-133 buffer over mirrored memory has the title's address as its GPU
VA and is no shadow; a GPU write into its home goes back to the title's page (one 256-byte piece), a page written on
both sides keeps the title's write. Linux ctest (llvmpipe) 122/122; xmem_test OK.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 05:56:53 -05:00
edb0f8502f d3d12: geometry onto the page mirror: V#s, root views, stream-pool ranges, BVH ranges and draw bindings of mirrored title memory name heap_base + the title address, no copy (RT-PAGE-MIRROR)
Step B. Where the page mirror holds a title range, every reader takes the title's address as its shader VA (heap_base +
address is the mirror's chunk), so no geometry copy, pool extent or title buffer is made for it:
- vsharp_address (title memory, after the alias table: slot-133 buffers keep their shadows until step C),
  geometry_va, resolve_title_memory (before pool extents and windows), copy_title_range, make_title_buffer (vertex and
  index views), device_reachable;
- VulkanContext::buffer_at falls back to the mirror (set_mirror_buffer): a vertex, index, copy or indirect binding of a
  mirrored title address is its chunk and offset, cut at the chunk's end (counted).
Ordinary memory is mirrored when a reader first names it (page_mirror_resolve: each page its own home, watched, then
filled; at most 256 MiB not yet mirrored at once, else the old backing), and leaves on release or decommit
(geometry_writes::note_released). xmem also places the title's own VirtualAlloc reservations in the mirror's range, and
records which reservations are ordinary memory. The binds go in one batch at the end of the before-execute hook
(flush_page_mirror), after every reader of the submission has named what it reads.

The geometry copies remain only for title memory outside the mirror (the XMemAlloc heap) and on devices without the
mirror (llvmpipe). The mem-probe breakdown line has a "page mirror" item: units allocated, homes bound, pages
mirrored, chunks; the mirror's line counts ranges resolved, refused, ordinary pages mirrored and released, and
bindings cut; the console heap line counts V#s resolved into the mirror.

Not here: write-back of GPU writes into mirrored pages to the title's pages. The geometry copies these readers used
had none either (only slot-133 shadows do, and they stay until step C), so nothing is lost; the per-page rule for
pages dirty on both sides comes with step C.

Tests: d3d12_page_mirror_test (Wine, iGPU): committed XMemVirtualAlloc memory named by a constant-buffer view keeps the
title address as its V# base, the GPU reads it and a later title write there; a binding of it, and of a GPU-only pool
page, is served by the mirror's chunk; a page named on its own is mirrored. memory_breakdown_test: the new item.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 05:25:43 -05:00
b699078893 d3d12, xmem: the page mirror: the title's GPU-readable pages at heap_base + their title address (RT-PAGE-MIRROR)
A V# or 64-bit pointer the title writes into its own memory names a title address, and the translated shaders read
heap_base + sext48(address). Nothing was there: run 63 lost the device on such a V# into the stream pool's GPU-only
window, which read zeros before step 2. The mirror puts the title's bytes there.

- src/title_mirror_space.h: xmem places every XMemVirtualAlloc reservation the title lets it place (address null) in
  [16 TiB, 48 TiB), handed out in order at reservation time, so no title pointer ever holds an old address. RADV makes
  sparse buffers at a chosen address only in its top 64 TiB, where heap_base (the sparse arena) is; heap_base + an
  address there stays inside it. A reservation the range cannot take is placed by the host, as before.
- src/title_mirror.h (TitleMirror, Vulkan through hooks): 2 GiB sparse chunks at heap_base + title address
  (bufferDeviceAddressCaptureReplay), one home per physical page (64 KiB of a 32 MiB host-visible unit), every page
  that shows a physical page bound to its one home (aliases share bytes and GPU writes; a remap is a rebind), the binds
  of a submission in one batch (the last bind of a page wins), homes freed after the retire ticket.
- src/title_frames.h Feed/Attach, xmem: every change of what a page of the range shows (host views in note_mapping,
  table pages in note_table_page), in order, with the physical page's scratch bytes; attaching feeds the pages mapped
  now under g_phys_mutex.
- src/title_mirror_tracking.h: the dirty set behind a small interface (watch, unwatch, collect: re-armed before the
  caller copies), with GuardTracking (a geometry-watch cell guard per page) until a measured policy replaces it.
- DeviceState::create_page_mirror / refresh_page_mirror (first in the before-execute hook): chunks for new
  reservations, the feed bound, written pages filled again once, the batch on the arena's bind queue signalling a
  timeline value; VulkanContext::note_mirror_bind makes every queue's next submission wait for it (the ordering rule).
  Off, with one line saying why, without capture-replay addresses, aliased strict sparse buffers, the bind queue, a
  sparse arena, signed V# offsets, xmem or the write-fault handler (llvmpipe), or with LEONIDA_PAGE_MIRROR=0.
  page_mirror_covers / page_mirror_address for the readers that move onto it. A log line every 10 s.

Tests: vk_title_mirror_test (native and under Wine, Raphael iGPU and RX 7900 XTX): a title-written V# read from a buffer
and dereferenced as the translated shaders do reaches its page, an alias the same, an unbound page reads zero; a remap,
a write (one upload for two aliases) and an unmap reach the next dispatch that waits for the batch; a home is freed only
after its ticket. d3d12_page_mirror_test (Wine, the game's device over xmem's pages): after slot-31 copies the GPU-only
page, its twin and the title's view hold the bytes at heap_base + address; a title write through its view reaches them
with one upload; a remap is a rebind with no host remap; an unmapped page reads zero. xmem_test: reservations land in
the range, the pair still pairs, the feed reports attach, host maps, table copies and unmaps in order, nothing after
detaching. Linux ctest (llvmpipe) 121/121.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-27 04:55:14 -05:00