Skip to content

gpu: back slot 133 linear buffers by sharing or a submit-time copy (opt-in) - #2218

Open
MasterDomino wants to merge 15 commits into
R0Y00:mainfrom
MasterDomino:gpu/linear-buffer-shared-backing
Open

MasterDomino wants to merge 15 commits into
R0Y00:mainfrom
MasterDomino:gpu/linear-buffer-shared-backing

Conversation

@MasterDomino

@MasterDomino MasterDomino commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

What and why

This is the first production slice of the #1914 decision, as R0Y00 asked after #1942 merged. It wires #1942's comparison to one ordinary linear buffer that the CPU writes and the GPU reads, and it keeps the current backing as the default.

The buffer kind is device slot 133 (CreatePlacedResourceX) with a buffer description, row-major or unknown layout, and initial state COMMON. This is the narrowest production kind that is linear, CPU-written and GPU-read:

  • Its desktop backing is what production GPU work actually reads: ExecuteIndirect argument and index buffers, alias SRVs in translated draws, and desktop views.
  • Today it is silently wrong outside private memory. Over XMem pages, or on an adapter without existing heaps (the Gen9 row), slot 133 returns S_OK with a zero-filled buffer that does not alias the title memory.

Buffers in UNORDERED_ACCESS or acceleration-structure state, textures and command-list slot 148 keep their current paths.

X2W_D3D12_LINEAR_BUFFERS=1 (or =copy) opts in. Unset or 0, none of the new code runs.

Rebase onto main (ff06794b) and follow-ups

The branch is rebased onto ff06794b as a series. Main had changed slot 133, release() and queue_execute substantially.

Integration with main:

  • release(): main's budget end_use calls, drop_mesh_lease_owner and the list-kind discard_mesh_lease_recording are kept. The linear retire runs after retire_placement, under the opt-in.
  • queue_execute: main's submit lambda (red_producers().execute, retire_address_uploads, retire_draw_sampler_heaps, note_submits, commit_list_gpu_writes) and the mesh XMem split path are unchanged. The internal-queue refresh and the consumer Wait run once per guest call before the wrapper's locks, so they precede every list of the submission in both the render and non-render paths. That covers the producer fences red_producers adds and each split mesh submission, and leaves red_producers semantics as they are.
  • Slot 133 publish: publish_placed_buffer carries main's tail: the index_resource callback in publish_placement, x2w_budget::resource(..., placed) plus the heap's backing_budget with begin_use when aliased, and x2w_budget::committed(..., DEFAULT) otherwise. Main's default path uses it unchanged.
  • docs/experiments.md: main's "A lease for committed XMem section pages" and "GPU queue handoff to an XMem-backed root SRV" sections come first, then this one.

New commits on top:

  • Budgets (gpu: count linear-buffer backings in the D3D12 budget).
    • A shared buffer counts as placed memory, and its heap (opened once per private range) gets a backing-heap ticket that is in use while a buffer on it is held.
    • A copied buffer counts as a committed DEFAULT buffer.
    • Staging buffers count as upload memory when created; they live for the process.
    • New fixture case budget (share, copy): one placement goes live in its category (placed with its heap live, or buffer), and the title's release moves it to retained, with nothing unmeasured.
  • No double ownership with the XMem lease path (gpu: defer slot 133 linear buffers to the XMem lease path).
    • Lease committed XMem section pages for GPU access #2280's mesh lease path (X2W_MESH86_XMEM_SRV or X2W_MESH_INDIRECT_XMEM) takes the default copied placements of XMem pages whose console flags are 0. note_mesh_xmem_write requires Placement::copied_buffer and console.Flags == NONE, then mirrors GPU writes into a leased view and publishes it to mesh readers.
    • Those are exactly the GPU-read XMem buffers the linear copy path would back, so both could claim one placement.
    • While the lease path is on, slot 133 now keeps the default backing for such a placement and records HRESULT_FROM_WIN32(ERROR_BUSY) (0x800700aa, "deferred to the lease path") in the log line, the info and the totals. Other placements, and everything while the lease path is off, are unchanged.
    • New fixture path lease: the XMem buffer keeps the default backing (reads zero, as before) with the deferral reported, and a private one is still copied.
  • Buffers a list copy writes (gpu: stop refreshing a copied linear buffer that a list copy writes).
    • Main now forwards CopyBufferRegion and CopyResource into slot 133 placed buffers (list_copy.inl), so a copied linear buffer can be GPU-written. Those bytes never reach title memory, and a refresh whose changed span covered them would have copied older title bytes over them.
    • Such a buffer now keeps the GPU's bytes from the recording on and is no longer refreshed, as the default backing behaves. It is reported as gpu_written in the info and totals.
    • New case gpuw (copy path): a list CopyBufferRegion writes G, the title changes the words on both sides, and the read still sees G.
    • New mutant refresh-gpu-written keeps refreshing; it reads A instead, and fails 2 of 2.
  • Docs for all of the above.

Follow-up, not in this PR: with #2280's section lease (pinned slots mapped as one view, opened as a D3D12 heap and released after GPU completion), XMem views could be shared through a lease instead of copied. This PR keeps copying them; view sharing stays refused (0x80070032) because an open heap pins the title's own view.

Review changes (R0Y00)

The repro. One copied buffer held A, and its first read was complete. Two read-only direct lists were recorded for it, and the CPU wrote B once before submitting either. List 1 went to queue 1 behind an unsignalled gate. List 2 went to queue 2, completed before the gate opened, and read A in 1,024 of 1,024 words; list 1 later read B. No refresh failure was reported.

The cause. Queue 1's submission enqueued the refresh on queue 1 behind the gate, but uploaded was updated at enqueue. Queue 2's diff therefore saw no change and skipped its refresh.

Fix (src/startup_stubs/linear_buffer_backing.h):

  • One internal COPY queue per device runs every refresh. It never waits on a title fence. Each refresh (staging → backing) is executed there and signals an internal fence value V. Each copied buffer remembers the V of its last refresh. ExecuteCommandLists and Signal on that queue are serialized by a small mutex of their own, so values stay in order. The backing's main lock is still never held across a wait or ExecuteCommandLists.
  • Every consumer orders behind pending refreshes. Every submission through a queue wrapper, of any type, first makes its queue Wait(internal_fence, V). V is the highest value among the live copied buffers that has not completed; buffers the title released are not waited for. Recording uploaded at enqueue is now correct, because every later consumer waits for the copy to land.
  • No title queue ever waits on another title queue. A refresh never sits behind the title's own waits, so title-owned cross-queue fences (queue 1 waits on a fence that queue 2 signals after its lists) cannot form a cycle with it.
  • Staging reuse is gated on the internal fence. Only the copy reads staging, so title fences don't matter. after_submit, the per-title-queue fences and linear_submitted are gone.
  • Simultaneous access. The internal queue may write a backing while an earlier submission that reads it still waits on its queue. Buffers allow simultaneous access across queues, so this is valid D3D12. It is also what unified memory does on the console: a read that executes later sees the CPU's latest writes. This rationale is now in the header and in docs/startup-runtime.md.
  • Fallback. If the internal queue or its fence cannot be created, copied placements keep the default backing and record that HRESULT in the log line, the info and the totals. X2W_TEST_SUBMIT_FENCE_FAIL=1 still gives E_NOT_VALID_STATE. Nothing returns stale data silently.
  • Fixture seam. x2w_d3d12_linear_buffer_internal_queue returns the internal queue, so a fixture can hold refreshes in flight behind a fence of its own. Cases c, contention and gate2q.held use it. Without it, staging is free almost at once, and the in-flight cases would test nothing.

Tests.

  • gate2q (R0Y00's case; copy path, private and XMem): queue 2 completes while queue 1's gate is still closed and reads B; queue 1 then reads B.
  • gate2q.held: the internal queue is also held. Queue 2 must not complete in 200 ms before the refresh lands, then reads B. Queue 1 reads B.
  • xq: queue 1 waits on a title fence that queue 2 signals after its list. Both complete, with no deadlock, and both read B.
  • Mutant skip-consumer-wait: submissions no longer wait. gate2q.held then has queue 2 read A before the held refresh lands (2 of 2), and case c reads zeros.
  • Kept and adapted:
    • contention now holds the internal queue rather than a title fence. The probes still take 0 ms while the refresh waits about 310 ms. S1 and S2, both behind the title gate, now both read C, the CPU's last write before they ran. Under the old per-queue design S1 read B.
    • Case c holds the internal queue on the copy paths, so reuse-early still fails it.
    • q.comp and q.copy still pass: the consumers on COMPUTE and COPY queues wait for the internal fence.
  • Fixture capacity: the private pool grows from 4 to 8 MiB for the extra allocations.

Review changes (tedin7)

  • P1, merge conflict: resolved by the rebase onto b0669e06. In queue_execute, runtime: allocate command-list upload memory on the recording allocator #2216's retire_address_uploads(queue, ready) and linear_submitted(queue, linear) are both kept, in that order. Main's "Command-list upload allocation (slot 148)" section precedes this one in docs/startup-runtime.md.
  • P2, the lock: before_submit no longer holds mutex_ across a fence wait or ExecuteCommandLists.
    • Under the lock it snapshots the work: which buffers changed and which span of each.
    • Without the lock it takes staging (acquire_staging), creates staging (create_staging), waits up to 5 s for the oldest staging fence when the pool is full, copies into staging, takes and resets a refresh list (acquire_list), records it and runs ExecuteCommandLists.
    • Under the lock again it publishes the results: the uploaded bytes and counters, the staging the submission holds, or a stale mark for each buffer whose staging failed.
    • Copied state is held by shared_ptr, so a snapshot outlives a concurrent discard.
    • after_submit still signals the queue fence under the lock. That keeps fence values monotonic across threads; Signal does not wait.
    • The guarantees are unchanged: a staging buffer is chosen only when its fence has completed or it is new; the refresh still happens at submit; a failed staging still marks the buffer stale and the submission still runs.
    • In render mode, queue_execute now refreshes before it takes draw_lock and allocator_lock, so a staging wait no longer stalls list recording either. linear_submitted runs after the ready check, so an executed refresh always gets its fence value.
    • New contention path (X2W_TEST_LINEAR_STAGING_LIMIT=1): S1 holds the only staging buffer behind a gate, and S2's refresh waits for it on another thread. While S2 waits, three probes must finish: the info export, release() of the buffer, and a submission on a second direct queue. Each took 0 ms, and S2 was still waiting after all three. After the gate opens, S2 takes the same staging buffer (none created) and reads its own bytes.
    • New mutant hold-lock keeps the lock across the wait, as before. The info probe then blocks for 4.7 s until the wait times out, S2 goes stale and reads the old bytes, and the case fails 2 of 2.
    • A second queue's submission that needs a refresh of its own still waits when the pool is full. That is a wait for a staging buffer, not for the lock, so the probe submission refreshes nothing.
  • P2, non-direct queues: new rows q.comp and q.copy on the copy path. The buffer is read by a dispatch on a COMPUTE queue and by CopyBufferRegion on a COPY queue, each through its queue wrapper. The refresh runs on that queue: the buffer is promoted from COMMON to COPY_DEST there and decays after. The first submission reads B with 131,072 bytes copied, and a 16-word change then copies 64 bytes. This holds on both adapters with the debug layer on and off, with no debug-layer errors. Both queue types are supported, so neither is refused.
  • Nit: x2w_d3d12_linear_buffer_info returns false for a wrapper whose own().real is null. The totals export takes no wrapper.
  • Superseded by R0Y00's review: refreshes moved to the internal queue, so after_submit, linear_submitted and per-queue refresh lists are gone. The snapshot, unlocked work and publish structure is unchanged.

Design (src/startup_stubs/linear_buffer_backing.h)

XMem keeps the title memory (identity, mapping, offsets, lifetime). The new file owns the desktop backing and the fences that say when the GPU is done with it.

  • Share only after D3D12_FEATURE_EXISTING_HEAPS reports support. The query runs once per device, before any open. The buffer must lie inside one committed private allocation at a 64 KiB-aligned offset, and the heap must cover it. The heap is cached by allocation base: while it is open, the range cannot be released or decommitted (error 87, gpu: compare one linear buffer through sharing, copying and refused sharing #1942), so no other allocation can appear at that base. Each opened heap gets a generation.

  • No sharing of XMem views. This was measured here: a heap opened from an XMem view makes XMem's unmap of those pages fail with error 87, and it succeeds after release. Desktop resources live for the process, so sharing a view would keep the title from ever unmapping or freeing its pages. Sharing reports HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED) (0x80070032) and the buffer is copied. A buffer across several allocations, or at an unaligned offset, reports E_INVALIDARG. Both are decided before any D3D12 call.

  • Copy into a committed default buffer, refreshed from title memory at each submission through a queue wrapper's ExecuteCommandLists (render and non-render paths), just before the title's lists run, never at bind or record.

    • Only bytes that changed since the last upload are copied, through upload staging buffers.
    • The copy runs on the internal COPY queue (see Review changes (R0Y00)), and each submission waits for the pending refreshes of live copied buffers.
    • A staging buffer is reused only after its copy's internal fence value has completed.
  • Keep the default backing and record the refusal where neither path keeps its guarantees. Slot 133 then returns what it returns with the switch off; the console never fails CreatePlacedResourceX (see Title run). The refusals:

    • a GPU-written use (ALLOW_UNORDERED_ACCESS) that cannot be shared has no copy-back point: the sharing HRESULT, or DXGI_ERROR_UNSUPPORTED when sharing was not attempted;
    • no internal refresh queue or fence: E_NOT_VALID_STATE under X2W_TEST_SUBMIT_FENCE_FAIL=1, else the creation HRESULT;
    • the copy's default buffer cannot be created: that HRESULT.

    Each refusal is recorded three ways:

    • the slot 133 log line, with (kept default backing: linear refusal 0x..., use=... type=... existing-heaps=... share=...) appended;
    • x2w_d3d12_linear_buffer_info, as path 3 with refusal_hr;
    • the totals, as kept_default and last_refusal_hr.
  • Never hold a submission back, and never hold the lock across a wait. Work is snapshotted under the backing's lock, staged, recorded and executed without it, and published under it again (see Review changes). Consumers on direct, compute and copy queues all wait for the internal fence.

  • Refresh failures. If a copied buffer's refresh cannot be staged, the submission still runs. This happens when staging or a refresh list cannot be created, or when the pool stays full of buffers in flight past a 5 s wait (DXGI_ERROR_WAS_STILL_DRAWING). The buffer keeps its last upload and is marked stale until a later refresh succeeds. The first failures are logged with their HRESULT, and every one is counted: stale, refresh_failures and last_refresh_hr in the info, and refresh_failures and last_refresh_hr in the totals.

d3d12_xs_device.cpp changes in four places:

  • slot 133 takes the new path for the eligible kind, and otherwise falls through to the unchanged default code. The wrap and publish tail moves unchanged into publish_placed_buffer. With the switch off, the default log line gets an empty suffix.
  • release retires a copied buffer.
  • queue_execute refreshes and inserts the consumer wait before the title's lists. In render mode this happens before the wrapper's locks.
  • three fixture exports: x2w_d3d12_linear_buffer_info, x2w_d3d12_linear_buffer_totals (layouts in linear_buffer_report.h) and x2w_d3d12_linear_buffer_internal_queue.

Fixture-only switches:

  • X2W_TEST_EXISTING_HEAPS_UNSUPPORTED=1
  • X2W_TEST_LINEAR_REFRESH_FAIL=<n> (staging requests 2, 2 + n, ... fail with E_OUTOFMEMORY)
  • X2W_TEST_LINEAR_STAGING_LIMIT=<n> (staging pool of n buffers instead of 256)
  • X2W_TEST_LINEAR_BUFFER_MUTANT=<name>

Title run

Measured on an RX 9070 XT (driver 32.0.31044.16, Windows 11 build 26300, Release) with a warm shader cache, -Render -Translate -FakeUser -GpuMirror -StartupBelow40 -MemoryGiB 32 and the same post-confirmation input in every run. Counts only; no title is named here.

Earlier revision (32ddb0bc), which returned the refusal from slot 133:

  • Switch off: unchanged behaviour. The run stopped at 294 s at the slot 148 builder.
  • X2W_D3D12_LINEAR_BUFFERS=1: the title faulted at 1.76 s, reading address 0x18. Its first eligible slot 133 placement was width=0xae00000 flags=0x20004 type=0x40000 (MEM_MAPPED) use=gpu-written share=0x80070057, and slot 133 returned 0x80070057. On the console CreatePlacedResourceX does not fail, so the title used the null object.
  • Slot 133 mix in the switch-off run (count, size, default outcome):
    • 2,987 GPU-read buffers, 14.2 MiB, not aliased;
    • 506 GPU-written buffers, 856 MiB, not aliased;
    • 61 GPU-written buffers, 167 MiB, aliased (private).

That is why every refusal now keeps the default backing and is only recorded.

This revision (14769462), matched pair on one build:

Switch off X2W_D3D12_LINEAR_BUFFERS=1
End slot 148 builder stop at 290.1 s slot 148 builder stop at 294.1 s (same stop)
Frames presented in 285 s 8,487 (29.8 fps) 8,451 (29.6 fps)
Executes / translated draws (not drawn) 8,400 / 256,460 (0) 8,400 / 256,265 (3)
Pipelines translated / missing 18 / 0 18 / 2
Slot 133 linear-buffer placements default path 3,094 copied at submit, 64 shared, 507 kept the default backing with the refusal recorded (463 0x80070057 split or unaligned GPU-written, 44 0x80070032 XMem view)
Refresh failures (stale) n/a none logged
Peak commit 12,056 MiB 16,479 MiB

The opt-in no longer changes how far the run gets, and the frame rate stays within 1% of the switch-off arm. The peak-commit difference is not attributed: a switch-off cold-cache run on the same input earlier today peaked at 16,289 MiB, so peak commit varies with how much a run loads. The opt-in arm had 2 missing pipelines and 3 undrawn draws where the switch-off arm had none; I have not investigated that difference.

Title run on this head (9b8c36fe, rebased onto ff06794b)

Correction: in this pair, neither arm got past the legal and acceptance page. Both ended at 420 s on that page with its boxes unticked. The scripted presses (the last at about 272 s) ran identically in both arms but no longer reach confirmation on current main; the page most likely appears later than before. So this pair covers the startup phase up to that page, not the post-confirmation load. A rerun with input that reaches confirmation will follow.

This is a matched pair on one build with the same input: RX 9070 XT (driver 32.0.32029.2010), Windows 11 build 26300, warm cache, -Render -Translate -FakeUser -GpuMirror -StartupBelow40 -MemoryGiB 32, and a 420 s limit. The opt-in arm ran first. System shared commit was read with NtQuerySystemInformation before each arm and 10 s after it exited.

X2W_D3D12_LINEAR_BUFFERS=1 Switch off
End 420 s limit, no stop 420 s limit, no stop
Frames 11,787 in 411 s (28.7 fps) 11,456 in 413 s (27.8 fps)
Executes 11,700 11,400
Pipelines translated / missing 11 / 3 14 / 0
Translated draws (not drawn) 382,346 (27,004) 409,670 (0)
Slot 133 linear buffers 2,848 copied at submit, 56 shared, 496 kept the default backing with the refusal recorded (452 0x80070057, 44 0x80070032) default path
Shared commit before → after 3.55 → 3.53 GiB 3.53 → 3.64 GiB
  • Frame rate: the internal-queue refresh and the consumer waits cost no measurable frame rate.
  • Shared commit: the opt-in arm left none behind.
  • Pipelines: the opt-in arm's 3 missing pipelines and 27,004 undrawn draws are most likely ordering. It ran first, so it translated new pipelines that the switch-off arm then found in the cache. I haven't verified that.

Test

tests/linear_buffer_backing_check.cpp drives the production entry points, not a model:

  • slot 133 on the device wrapper;
  • list Reset (139) and Close (9) on wrapper lists recorded on their desktop lists;
  • ExecuteCommandLists (queue slot 10) on the queue wrapper, in render mode.

It reuses #1942's sentinels and cases. The GPU reads through a root SRV and writes through a root UAV. Title memory is either xmem.dll physical pages (X2W_XMEM_SHARED_VIEWS=1, five pages per view, buffer at +0x30000, first buffer on freed slots that must read zero) or one committed private range.

A kept default must meet three conditions:

  • return S_OK with an object;
  • behave as before: an XMem view not aliased, a private range aliased on an adapter with existing heaps;
  • carry the refusal HRESULT in the info, the totals and its own slot 133 log line. The fixture reads that line back from X2W_TRAP_LOG, written next to the executable as linear_buffer_backing_<path>_<adapter>.log.

The cases:

  • a, a2: A to B before submit, then a 16-word change that must copy 64 bytes.
  • b: GPU write, fence, alias readback.
  • c: three held submissions, then backing reuse only after completion.
  • d1: missing refresh, by executing past the wrapper.
  • d2: readback before the fence, ordered by a queue Wait on a gate.
  • split, gate, off: a buffer across two XMem views; no heap open when the feature query reports no support; no linear-buffer code with the switch off.
  • kept: the kept-default total equals the number of refusals.
  • stale: a failed refresh still runs and completes, reads the last upload and is reported stale; the next refresh clears it.
  • q.comp, q.copy: the refresh on a COMPUTE and a COPY queue.
  • contention: the info export, release() and another queue's submission finish while a refresh waits for the only staging buffer, whose copy is held on the internal queue.
  • gate2q, gate2q.held, xq: one buffer read on two queues after one CPU write: both see it, also with the refresh held, and with a title-owned cross-queue fence (no deadlock).

Results

Environment:

  • Commit: the series on ff06794b (head 9b8c36fe), Windows 11 build 26300.9974.
  • Adapters: in-box WARP (1414:008c, driver 10.0.26100.9818) and AMD Radeon RX 9070 XT (1002:7550, driver 32.0.32029.2010; the driver changed since the earlier runs). Both report D3D12_FEATURE_EXISTING_HEAPS.
  • Debug layer off and on (X2W_TEST_DEBUG_LAYER=1). No errors were stored.

The results were identical in all four configurations, including every totals line. All eight paths pass, with no skips.

Bytes are those copied from title memory into the backing. HRESULTs are the slot 133 result, then the sharing result and the recorded refusal.

Path Pool Backing Slot 133 / sharing / refusal Bytes copied Results
default XMem committed buffer, not aliased (old behaviour) S_OK / not attempted / none 0 a reads zero in all words; b leaves P (asserted as before)
default private aliased through the cached heap (old behaviour) S_OK / not attempted / none 0 a B; b G ^ key; totals all zero (no linear-buffer code ran)
real sharing private title memory, 1 heap open, 7 cache hits, generation 1 S_OK / S_OK / none 0 a B; a2 E; b G ^ key; c B, C, D held, then E at X's address on the same heap; d1 B both ways; d2 P before the fence, G ^ key after
real sharing XMem copied (view not shared); b and d2 kept default S_OK / 0x80070032, split 0x80070057 / b, d2 0x80070032 a 131,072; a2 64; split 131,072; c 4 × 131,072; d1 131,072 + 4,096 as forced copying; b and d2 leave P as before; 2 kept defaults counted
forced copying XMem, private committed buffer refreshed at submit; b and d2 kept default S_OK / not attempted / b, d2 0x887A0004 a 131,072; a2 64; c 4 × 131,072; d1 131,072 + 4,096 a B; a2 E; c B, C, D on staging 1, 2, 3, then staging 1 reused after the fence; d1 stale A past the wrapper, B through it; b and d2 as before (XMem P, private G ^ key); 4 kept
host sharing refusal XMem, private as forced copying S_OK / 0x887A0004 from the feature gate / b, d2 0x887A0004 as forced copying as forced copying; gate: 1 feature query, 0 heap opens; 4 kept
copy without a submit fence XMem, private kept default for every placement S_OK / not attempted / a 0x8007139F (E_NOT_VALID_STATE), b and d2 0x887A0004 0 a: XMem zero, private B; b and d2: XMem P, private G ^ key (all as before); 6 kept
forced copying, other queues (q.comp, q.copy) XMem, private copied, refreshed on the COMPUTE or COPY queue S_OK / not attempted / none 131,072, then 64 B in every word, then 16 E; no refresh failure
two queues, one buffer (gate2q, gate2q.held, xq) XMem, private copied S_OK / not attempted / none 131,072 for A, then 4,096 for B queue 2 completes with queue 1 still gated and reads B, queue 1 then reads B; .held: queue 2 waits for the held refresh; xq: both complete and read B
contention (one staging buffer) XMem, private copied S_OK / not attempted / none 540,672 for the run probes 0 ms each while S2 waited about 310 ms for the held copy; S1 C, S2 C; staging created 1; no refresh failure
forced copying (budget, gpuw) XMem, private copied S_OK / not attempted / none 131,072; gpuw then 0 budget: buffer objects live 0 -> 1 -> 0 after release, retained +1, unmeasured +0; gpuw: G after the title's writes around it, gpu_written 1
real sharing (budget) private shared S_OK / S_OK / none 0 placed objects live 0 -> 1 -> 0, retained +1, heap live 1
lease path on XMem kept default, deferred S_OK / not attempted / 0x800700AA 0 reads zero, as before; deferral in info, totals and the log
lease path on private copied S_OK / not attempted / none 131,072 B
failed refresh XMem, private copied S_OK / not attempted / none 131,072 + 4,096 per pool; 0 for the failed refresh the failed submission completes and reads A (last upload), stale reported with 0x8007000E in info and log; next submission reads B, stale cleared; 2 refresh failures in totals

Totals per run:

  • share: 19 placements: 9 shared, 8 copied, 2 kept default. 1,052,736 bytes copied.
  • copy: 30 placements: 26 copied, 4 kept default. 3,441,024 bytes copied. 3 staging buffers created and 37 reused.
  • refusal: 16 placements: 12 copied, 4 kept default. 1,581,184 bytes copied. 3 staging buffers created and 13 reused.
  • contention: 4 placements copied. 540,672 bytes copied. 1 staging buffer created and 7 reused.
  • untracked: 6 placements, all 6 kept default.
  • stale: 2 placements copied. 270,336 bytes copied. 2 refresh failures.
  • lease: 1 placement copied (private) and 1 kept default (XMem, deferred). 131,072 bytes copied.

Measured, not asserted (share path, XMem): on both adapters OpenExistingHeapFromAddress on the five-page view and a placed buffer at +0x30000 return S_OK. While they live, XMem's unmap fails with error 87. After their release it succeeds and the pages are freed. This is why views are copied.

Negative controls

Each control sets X2W_TEST_LINEAR_BUFFER_MUTANT, which removes one guarantee inside d3d12_xs.dll. Each must fail its case in both pools, and in all four configurations each does:

Mutant (path) Guarantee removed Result
skip-refresh (copy) copy at submit; the copy is taken at placement (bind) a reads A, 2 of 2 failed
reuse-early (copy) staging reuse waits for the copy's fence with the internal queue held, the held refreshes all take staging 1; X, Y and Z read D; c 2 of 2 failed
copy-gpu-writes (copy) GPU-written use not taken by the copy b and d2 leave P and report no refusal; b 2 of 2 failed
ignore-feature (refusal) feature gate before any open 1 heap opened under EXISTING_HEAPS = 0; gate 1 of 1 failed
refusal-silent (copy) a kept default is reported no refusal in the info and no log line; b 2 of 2 failed, kept total 0
hold-lock (contention) no lock held across the staging wait the info probe blocks 4.7 s; S2's wait times out, it goes stale and reads B; contention 2 of 2 failed
skip-consumer-wait (copy) consumers wait for pending refreshes gate2q.held: queue 2 completes before the held refresh lands and reads A; 2 of 2 failed (case c also reads zeros)
refresh-gpu-written (copy) a buffer a list copy wrote is no longer refreshed gpuw: the refresh of the title's two changed words spans the written region and copies A over G; 2 of 2 failed

Inside the run there are also these oracle controls:

  • d1 executes past the wrapper and must see the stale A.
  • The default path must show the old zero stand-in over XMem.
  • d2 must see P before the gate opens.
  • The failed refresh must still read A.

Commands

.\tools\dev.ps1
cmake --preset release
cmake --build --preset release
ctest --preset release --no-tests=error -R '^(linear_buffer_backing|startup_upload_arena|startup_upload_lifetime|startup_list_resubmit|startup_address_console_|xmem_section_lease_|unified_buffer_paths_)' -j 4
# one path by hand, debug layer on:
$env:X2W_TEST_DEBUG_LAYER = '1'; .\out\build\release\linear_buffer_backing_check.exe hardware stale
  • Release, this revision (series on ff06794b):
    • The build is clean; main's generated CPU translation sources still print C4805 warnings, none from this series.
    • ctest --preset release --no-tests=error -R '^(linear_buffer_backing|startup_upload_arena|startup_upload_lifetime|startup_list_resubmit|startup_address_console_|xmem_section_lease_|unified_buffer_paths_)' -j 4 passed 53/53, covering WARP and hardware. That is 24 linear_buffer_backing (8 paths on 2 adapters, plus 8 controls), 5 unified_buffer_paths, 6 xmem_section_lease, 8 startup_list_resubmit, 6 startup_address_console, 2 startup_upload_arena and 2 startup_upload_lifetime.
    • With X2W_TEST_DEBUG_LAYER=1, -R '^linear_buffer_backing' passed 24/24.
    • linear_buffer_backing_check was also run directly: all 8 paths and all 8 mutants, on both adapters, debug layer off and on. Every path passes, every mutant fails all its targeted rows, and the path totals are identical across the four configurations.
    • System commit (out/diag/perfinfo.py), before the release build and after all of these runs:
      • SharedCommittedPages: 926,231 → 922,226 pages, −4,005 pages (about −15.6 MiB). No shared-commit growth.
      • CommittedPages: +79,010 pages (about 309 MiB) over the same window, which includes the release build and other activity on the machine.
  • Previous revision (52d779d9), environmental failure: one earlier debug-layer ctest attempt failed unified_buffer_paths_{warp,hardware} with setup errors. The cause was the system commit limit, with 51 MB free of 184 GB while a title run was active. The section pool's 256 MiB pagefile section could not be created, and git itself failed with 0xC000012D. The same binaries then failed identically without the debug layer. Once commit was free again, the same command passed 25/25.
  • Dev, previous revision (14769462); not rerun for this one: ctest --preset dev --no-tests=error -R '^(vm_|era_|xmem|unified_buffer_paths|startup_|linear_buffer_backing)' -j 6 selected 513 tests:
    • 508 passed, including all 17 linear_buffer_backing_* and 5 unified_buffer_paths_* tests.
    • 4 skipped (77): startup_view_heap_{warp,hardware} and startup_logical_view_{warp,hardware}.
    • 1 failed: startup_run_ps1. It compares the translator's --watch path against the 8.3 short form of %TEMP% and gets the long form, because the profile path on this host contains a space. It also fails on unmodified main on this host, so it is pre-existing.
  • The startup_gamesave_* C3066 build failure seen on an earlier base is fixed on main by tests: call the saved QueryApiImpl through load_real_query() #2204. It no longer occurs here.

Skips

None on WARP or the RX 9070 XT. linear_buffer_backing_share_hardware returns 77 on an adapter without existing-heap support, and refusal covers that case there. On a Gen9 adapter the share run would skip while the other paths run. That has not been measured here.

Not covered

  • Table SRVs of copied buffers in translated draws still take frame_copy at record, and graphics root SRVs still take upload_from_title at bind. Neither reads the slot 133 backing; moving them to the submit copy point is a separate change.
  • GPU-written buffers that cannot be shared keep the default backing. In the switch-off title run, 506 of them (856 MiB) were not aliased, and that backing neither aliases nor copies. This slice records them but does not fix them. They need a copy-back point or a releasable shared backing.
  • Shared buffers and their heaps live for the process, as every wrapper does. A private range under one cannot be released, as with the default backing. Releasing the backing after the title's release and its last fence, which sharing XMem views would also need, is follow-up work. One option is a heap per XMem section (OpenExistingHeapFromFileMapping) with section and offset from XMem.
  • The copy compares every live copied buffer's bytes on each submit. That costs CPU time proportional to the copied bytes while the switch is on (14.2 MiB of GPU-read buffers in the title run). It has not been measured.
  • One matched title pair only (above). A passing fixture and that pair do not establish full-game startup; the per-submit compare cost was not profiled beyond the frame rate.
  • A copied buffer that a list copy wrote stops following its title memory, as the default backing does. Merging the GPU's bytes with later title writes would need a copy-back point.
  • Sharing XMem views through Lease committed XMem section pages for GPU access #2280's section lease instead of copying them (follow-up).
  • Unsynchronized concurrent CPU and GPU writes, textures, render targets, acceleration structures and slot 148 are outside this slice.
  • Not run on a Gen9 adapter; the Gen9 case is simulated with X2W_TEST_EXISTING_HEAPS_UNSUPPORTED=1.

Refs #1914
Refs #874

🤖 Generated with Claude Code

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of head 14769462719628f1ce3bfd871eb43738d2c073aa (opt-in slot 133 linear-buffer backing).

Commands executed locally (Linux worktree)

  • git diff --stat origin/main...HEAD: 7 files, +2039/-21 (CMakeLists, docs x2, d3d12_xs_device.cpp, linear_buffer_backing.h/report.h, linear_buffer_backing_check.cpp).
  • python3 tests/docs_links_check.py: passed (18 files, 410 links).
  • python3 tests/bidi_controls_check.py: passed (1490 files).
  • git diff --check origin/main...HEAD: clean.
  • git merge-tree --write-tree --name-only origin/main HEAD: content conflicts in docs/startup-runtime.md and src/startup_stubs/d3d12_xs_device.cpp.
  • python3 x2w_pipe.py win2025 2218 --regex linear_buffer_backing (default 3-slot pool): dev 17/17 and release 17/17 passed on Win2025 + D3D12 WARP. The 5 --mutate controls (skip-refresh, reuse-early, copy-gpu-writes, ignore-feature, refusal-silent) are WILL_FAIL tests and passed, so each mutant is killed by its case. The hardware variants rely on SKIP_RETURN_CODE 77 where no hardware adapter exists; I did not see them exercised on real hardware.
  • gh pr checks: no checks reported on the branch.

P1

  • The PR is CONFLICTING with main (files above). It needs a rebase, and the Win2025 result must be repeated on the rebased head, because the conflict is inside d3d12_xs_device.cpp (the device_place_resource_x / queue_execute hunks this PR rewrites).

P2

  • src/startup_stubs/linear_buffer_backing.h before_submit (about L315-405) holds mutex_ while it calls take_staging (waits up to 5000 ms on WaitForSingleObject) and queue->ExecuteCommandLists. Another queue's submission, or a release() of a placed resource calling retire, blocks behind it. Opt-in only, but consider dropping the lock around the wait, or documenting it in docs/experiments.md.
  • before_submit refreshes on whichever queue is executing (including compute or copy queues) and relies on implicit COMMON -> COPY_DEST promotion. This is valid for buffers, but no test submits on a non-direct queue. Add one, or state the limit in the docs.

Nit

  • x2w_d3d12_linear_buffer_info reads w->own().real after the kind check only. That is fine, but a nullptr guard on real would match the sibling exports.
  • Hardware-adapter variants are registered, but only WARP evidence is attached.

Title is neutral. The new code is original in-repo work; I found nothing that breaks clean-room.

AI-assisted; commands listed were executed locally.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note on my review above: it covers the earlier head 14769462719628f1ce3bfd871eb43738d2c073aa. The branch was updated to 60220de68a948cf66ff96e66c6ac2b1323595800 while I was reviewing, and it now reports MERGEABLE, so the P1 (merge conflict) no longer applies. The Win2025 WARP result (dev 17/17, release 17/17) and the P2 items were not re-run on the new head. Please re-verify them there before Ready.

AI-assisted; commands listed were executed locally.

MasterDomino added a commit to MasterDomino/Xbox2Windows that referenced this pull request Nov 25, 2026
Review of R0Y00#2218:
- before_submit snapshots the copied buffers' work under mutex_, then
  takes, creates or waits for staging (a full pool waits up to 5 s for
  the oldest fence), fills it, resets and records a refresh list and
  runs ExecuteCommandLists without the lock, and publishes the result
  under it again. A release(), the fixture exports or another queue's
  submission no longer wait behind a fence wait or ExecuteCommandLists.
  Staging is still reused only after its fence, the refresh still runs
  at submit, and a buffer whose staging fails is still marked stale.
- In render mode the refresh runs before queue_execute takes draw_lock
  and allocator_lock, so a staging wait does not stall list recording.
- x2w_d3d12_linear_buffer_info returns false for a wrapper without a
  desktop object.

The fixture adds a contention path (X2W_TEST_LINEAR_STAGING_LIMIT=1):
while a refresh waits for the only staging buffer behind a gate, the
info export, release() and a second queue's submission must finish,
and a hold-lock mutant that keeps the lock across the wait must fail.
The copy path also refreshes on COMPUTE and COPY queues, read by a
dispatch and by CopyBufferRegion.

Refs R0Y00#1914
Refs R0Y00#874

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

Copy link
Copy Markdown
Collaborator Author

@tedin7, thanks for the review. All of it is addressed in 52d779d9, on 60220de6 (main b0669e06, which includes #2216).

  • P1 (conflict): already resolved by the rebase you noted.
  • P2 (lock): before_submit now takes a snapshot of what changed under mutex_, then releases it while it takes, creates or waits for staging, fills it, records the refresh and runs ExecuteCommandLists. It re-takes the lock to publish. In render mode, queue_execute refreshes before taking draw_lock and allocator_lock. after_submit still signals under the lock, which keeps fence values ordered across threads; Signal doesn't wait.
    • New contention path: the staging pool is limited to one buffer with the test-only X2W_TEST_LINEAR_STAGING_LIMIT=1. S1 holds it behind a gate while S2 waits on another thread. During the wait, the info export, a release() and a submit on a second direct queue each take 0 ms. After the gate opens, S2 reuses the same staging buffer and reads C.
    • New hold-lock mutant: it keeps the lock across the wait. The info probe then blocks about 4.7 s, S2 goes stale, and the case fails 2/2.
  • P2 (queues): new q.comp and q.copy rows read the copied buffer with a dispatch on a COMPUTE queue and a CopyBufferRegion on a COPY queue, each through its wrapper. The refresh runs on that queue and relies on implicit COMMON to COPY_DEST promotion. Both read B with 131,072 bytes copied, then 64 bytes after a 16-word change. The debug layer stays clean on both adapters.
  • Nit: x2w_d3d12_linear_buffer_info returns false when the wrapper's real is null.
  • Hardware: every row ran on the RX 9070 XT as well as WARP.

Results:

  • Release, debug layer off: ctest --preset release --no-tests=error -R '^(linear_buffer_backing|unified_buffer_paths|startup_upload_arena|startup_upload_lifetime|startup_list_resubmit|startup_address_console_)' -j 4 passed 43/43 on WARP and hardware.
  • Debug layer on (X2W_TEST_DEBUG_LAYER=1): linear_buffer_backing|unified_buffer_paths passed 25/25.
  • Mutants: each of the 6 fails all its targeted rows.

One environmental note. On this host, a debug-on run of #1942's unified_buffer_paths_warp failed setup when system commit was exhausted, because its 256 MiB pagefile section couldn't be created. Leftover local test processes caused that, not this change. The same command passes once commit is available.

AI-assisted.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of head 52d779d9dea7b2127393bdcc095e30995917321b (earlier review covered 14769462).

Previous findings

  • P1 (merge conflict with main): resolved. gh pr view reports MERGEABLE; git diff --stat origin/main...HEAD is 7 files, +2321/-22.
  • P2 (lock held across fence wait and ExecuteCommandLists in before_submit): resolved. src/startup_stubs/linear_buffer_backing.h now snapshots work under mutex_, stages/records/executes without it, and publishes under it again (L318-L430). retire() during a flight is safe: publish re-checks copy.live. A new contention case plus a hold-lock mutant cover it.
  • P2 (no test on a non-direct queue): resolved. The copy path now has a q.comp/q.copy case (tests/linear_buffer_backing_check.cpp L925-L960) that submits through a COMPUTE queue (dispatch read) and a COPY queue (CopyBufferRegion read). The comment at L391 documents the implicit COMMON -> COPY_DEST promotion.
  • Nit (real null guard on the info export): resolved, d3d12_xs_device.cpp:1349 checks !w->own().real.

Commands executed locally

  • python3 tests/docs_links_check.py: passed (18 files, 412 links).
  • python3 tests/bidi_controls_check.py: passed (1498 files).
  • git diff --check origin/main...HEAD: clean.
  • gh pr checks: Linux Python checks, gfx1013 fixtures and Windows build+test all pass on this head.
  • python3 x2w_pipe.py win2025 2218 --regex linear_buffer_backing (default 3-slot pool): dev phase 20/20 passed (warp + hardware variants, contention, and the 6 WILL_FAIL controls including hold_lock and the new queue case, all killed as expected). The release phase failed twice at compile of d3d12_xs_device.cpp with sccache: error reading compile response from server (connection reset, infrastructure, not a compiler diagnostic). So release ctest has NOT been confirmed on this head by me; the repo's Windows CI job is green.
  • Mutation: the PR's built-in mutants (skip-refresh, reuse-early, copy-gpu-writes, ignore-feature, refusal-silent, hold-lock) all ran as WILL_FAIL controls and passed (each killed). I did not write a new mutant of my own, the Linux host cannot build the D3D12 test.

P1

  • none.

P2

  • Release-phase WARP result is missing on this head (sccache failure above). Please re-run the release phase before Ready.

Nit

  • In before_submit, two threads submitting at once can each snapshot the same uploaded diff and publish out of order, so uploaded may briefly lag the GPU copy. The effect is only an extra (redundant) refresh next submit, not stale data; a one-line comment would make that explicit.
  • The hardware variants reported Passed on the Win2025 VM; I could not confirm whether a real GPU adapter was used there, so I do not count them as real-GPU evidence.

Title is neutral; the new code is original in-repo work; nothing breaks clean-room.

AI-assisted; commands listed were executed locally.

@MasterDomino

Copy link
Copy Markdown
Collaborator Author

Thanks for the re-review.

  • Release on this head: I ran the release phase on 52d779d9 locally, on Windows 11 build 26300, with WARP and a real AMD Radeon RX 9070 XT (1002:7550, driver 32.0.31044.16), debug layer off: ctest --preset release --no-tests=error -R '^(linear_buffer_backing|unified_buffer_paths|startup_upload_arena|startup_upload_lifetime|startup_list_resubmit|startup_address_console_)' -j 4 passed 43/43. The hardware rows there ran on that GPU.
  • Nit (concurrent publish): agreed. I'm adding a comment at the publish step in before_submit. It says that two submissions snapshotting the same buffer can publish uploaded in either order, so it may briefly lag the GPU copy. The next diff is then redundant or larger than needed, but it never misses a change. I'll push it once this host can rebuild; it's short on memory right now. It changes no code.

AI-assisted.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reproduced a copy-path ordering failure on 52d779d9 after the existing Windows Release suite passed 37/37. In the fixture, I placed one copied buffer holding A and completed its first read. I recorded two read-only direct lists for that same buffer, wrote B once on CPU before submitting either list, and queued the first list behind an unsignaled fence. The second queue completed before the gate opened. After opening the gate, the first list read B in 1,024/1,024 words. The second had already read A in 1,024/1,024 words. Both WARP and RTX 4060 Laptop reproduced it for private memory and XMem, and neither reported a refresh failure. There are no concurrent guest writes. before_submit records the new bytes in uploaded when a copy is enqueued, so another queue skips its refresh before those bytes are available. Please track the pending refresh per backing and order foreign-queue consumers behind it, or explicitly refuse this use before the consumer runs. Keep the different-buffer contention case and add this same-buffer gate. Both reads should see B, or the second must hit an explicit refusal rather than finishing with stale A.

MasterDomino added a commit to MasterDomino/Xbox2Windows that referenced this pull request Nov 25, 2026
Two submissions that snapshot the same copied buffer can publish
`uploaded` in either order, so it may briefly lag the GPU copy. The next
diff is then redundant or larger than needed, never one that misses a
change. Comment only (tedin7's review nit on R0Y00#2218).

Refs R0Y00#1914

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

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review of head 793b52b5fa2ea08ecbe739b3ac540a4a194cf162 (previous review covered 52d779d9).

Previous findings

  • P2 (release-phase WARP result missing on the earlier head): resolved. I re-ran it on this head, see below: release /O2 20/20 passed.
  • Nit (concurrent publishes may leave uploaded lagging): resolved. The only change since 52d779d9 is a comment in src/startup_stubs/linear_buffer_backing.h (before_submit, around L403-L407, +5/-1). It states that the worst case is a redundant or larger refresh next submit, never a missed change. The wording matches what the code does (publish re-checks under mutex_).
  • Nit (hardware variants are not proof of a real GPU): still open, informational only. I did not confirm an actual hardware adapter on the VM.

Commands executed locally

  • git diff --stat 52d779d9 793b52b5: 1 file, +5/-1, comment only. No logic change, so the earlier code review still holds.
  • python3 tests/docs_links_check.py: passed (18 files, 412 links).
  • python3 tests/bidi_controls_check.py: passed (1498 files).
  • git diff --check origin/main...HEAD: clean.
  • python3 x2w_pipe.py win2025 2218 --regex linear_buffer_backing --wait 3600 (default 3-slot pool): dev phase passed (warp + hardware variants, contention, and the WILL_FAIL controls, all as expected; I only captured the tail of the dev output, so I do not quote a dev count). Release /O2: build 92.55s, ctest --preset release -R linear_buffer_backing: 20/20 passed, including contention_warp, contention_hardware and the hold_lock control.
  • Mutation: the PR's built-in mutants (skip_refresh, reuse_early, copy_gpu_writes, ignore_feature, refusal_silent, hold_lock) ran as WILL_FAIL controls and all were killed. I wrote no mutant of my own: the Linux host cannot build the D3D12 test, and this head changes only a comment.
  • gh pr checks: at review time the Windows build and gfx1013 jobs were still pending; Linux Python checks passed. Please wait for those to turn green before Ready.
  • PR is MERGEABLE; the title is neutral; the new code is original in-repo work, so clean-room is intact.

P1

  • none.

P2

  • none. The previous P2 is resolved.

Nit

  • The hardware ctest variants pass on the VM, but I cannot show that a real GPU adapter was used, so I do not count them as real-GPU evidence.

AI-assisted; commands listed were executed locally.

@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

@MasterDomino could you rebase this branch onto current main and force-push?

GitHub reports head 793b52b5 as conflicting with main, so it cannot be merged as is.

Files that conflict with current main (4aaea119):

  • docs/experiments.md
  • src/startup_stubs/d3d12_xs_device.cpp

Commands run locally:

git fetch origin main pull/2218/head
git merge-tree --write-tree --name-only origin/main FETCH_HEAD
gh pr view 2218 --json mergeable   # CONFLICTING

Once it is rebased and CI is green it will be re-checked right away.

AI-assisted; commands listed were executed locally.

@MasterDomino
MasterDomino force-pushed the gpu/linear-buffer-shared-backing branch from 793b52b to 9b8c36f Compare November 25, 2026 14:59
MasterDomino added a commit to MasterDomino/Xbox2Windows that referenced this pull request Nov 25, 2026
Review of R0Y00#2218:
- before_submit snapshots the copied buffers' work under mutex_, then
  takes, creates or waits for staging (a full pool waits up to 5 s for
  the oldest fence), fills it, resets and records a refresh list and
  runs ExecuteCommandLists without the lock, and publishes the result
  under it again. A release(), the fixture exports or another queue's
  submission no longer wait behind a fence wait or ExecuteCommandLists.
  Staging is still reused only after its fence, the refresh still runs
  at submit, and a buffer whose staging fails is still marked stale.
- In render mode the refresh runs before queue_execute takes draw_lock
  and allocator_lock, so a staging wait does not stall list recording.
- x2w_d3d12_linear_buffer_info returns false for a wrapper without a
  desktop object.

The fixture adds a contention path (X2W_TEST_LINEAR_STAGING_LIMIT=1):
while a refresh waits for the only staging buffer behind a gate, the
info export, release() and a second queue's submission must finish,
and a hold-lock mutant that keeps the lock across the wait must fail.
The copy path also refreshes on COMPUTE and COPY queues, read by a
dispatch and by CopyBufferRegion.

Refs R0Y00#1914
Refs R0Y00#874

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MasterDomino added a commit to MasterDomino/Xbox2Windows that referenced this pull request Nov 25, 2026
Two submissions that snapshot the same copied buffer can publish
`uploaded` in either order, so it may briefly lag the GPU copy. The next
diff is then redundant or larger than needed, never one that misses a
change. Comment only (tedin7's review nit on R0Y00#2218).

Refs R0Y00#1914

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MasterDomino added a commit to MasterDomino/Xbox2Windows that referenced this pull request Nov 25, 2026
Review of R0Y00#2218 (R0Y00): one copied buffer, one CPU write, then a list
on queue 1 behind an unsignalled gate and a list on queue 2. Queue 2
read the old bytes: queue 1's refresh was enqueued behind its gate but
recorded as uploaded, so queue 2 skipped its own.

- Every refresh now runs on one internal COPY queue per device, which
  never waits on a title fence, and signals an internal fence value.
  Each copied buffer remembers the value of its last refresh.
- Every submission through a queue wrapper first makes its queue wait
  for the highest such value among the live copied buffers that has not
  completed. A consumer on any queue runs after every refresh enqueued
  before it, and no title queue waits on another, so title-owned
  cross-queue fences cannot form a cycle with a refresh.
- Staging is reused once its copy's internal fence value completes.
  The per-title-queue fences, after_submit and linear_submitted go.
- If the internal queue or fence cannot be created, copied placements
  keep the default backing with that HRESULT recorded.
- x2w_d3d12_linear_buffer_internal_queue lets fixtures hold refreshes.

The fixture adds gate2q (both reads see the CPU write), gate2q.held
(the second queue waits for a held refresh), xq (queue 1 waits on a
fence queue 2 signals: no deadlock, both read the write) and a
skip-consumer-wait mutant that must fail gate2q.held. Case c and
contention hold the internal queue so staging stays in flight.

Refs R0Y00#1914
Refs R0Y00#874

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

Copy link
Copy Markdown
Collaborator Author

@R0Y00, your same-buffer cross-queue case is fixed, and the branch is rebased onto main ff06794b as tedin7 asked. Head is 9b8c36fe.

The stale read (3bfa3a9d):

  • Every refresh now runs on one internal COPY queue per device, which never waits on a title fence.
  • Every submission through a queue wrapper first does Wait(internal fence, V), where V covers the live copied buffers' latest refreshes. So recording the uploaded bytes at enqueue is correct.
  • Title queues never wait on each other, so the title's own cross-queue fences can't deadlock with a refresh. Buffers allow simultaneous access, so the refresh may land before an earlier gated reader runs, which matches unified memory.
  • New cases: gate2q is your repro: queue 2 completes while queue 1's gate is closed, and both read B. gate2q.held does the same with the internal queue held. xq has queue 1 waiting on a title fence that queue 2 signals: both complete and read B.
  • New mutant: skip-consumer-wait reproduces the stale A.

Rebase onto main's changes:

  • Kept from main: the budgets, the mesh-lease bookkeeping, the red_producers submit wrapper and commit_list_gpu_writes. The refresh and wait run once per guest call, ahead of every list.
  • New commits:
    • 2088e004: linear backings count in the D3D12 budget, with a new budget case.
    • 1330a3dc: while the mesh XMem lease path (Lease committed XMem section pages for GPU access #2280) is on, the placements it can claim keep the default backing, and the refusal is recorded as 0x800700aa. New lease case. Sharing XMem views through a lease is noted as a follow-up.
    • bae6cf37: main's list copies can now write a copied buffer, so a buffer that a list copy writes keeps the GPU's bytes and is no longer refreshed. New gpuw case and a refresh-gpu-written mutant.

Results:

  • Release: 53/53 for linear_buffer_backing|startup_upload_*|startup_list_resubmit|startup_address_console_|xmem_section_lease_|unified_buffer_paths_. The linear tests pass with the debug layer on, and every mutant fails its targets on WARP and the RX 9070 XT.
  • Title pair on this head: both arms ran the 420 s limit with no stop, at 28.7 fps with the opt-in against 27.8 fps without, and the opt-in arm left no shared commit behind. The table is in the PR body.

AI-assisted.

@MasterDomino

Copy link
Copy Markdown
Collaborator Author

Correction to the title-pair line in my comment above: neither arm of that pair got past the legal and acceptance page. Both ended at 420 s on that page with its boxes unticked. The same scripted presses ran in both arms, but on current main they no longer reach confirmation. So the pair covers startup up to that page, not the post-confirmation load. The other numbers stand for that phase: the frame rate, no shared commit left behind, and the placement counts. I'll rerun with input that reaches confirmation and post that separately. The PR body is corrected too.

AI-assisted.

@MasterDomino

Copy link
Copy Markdown
Collaborator Author

Title pair rerun on 9b8c36fe, this time past confirmation. On current main the legal and acceptance page appears later than before, so the scripted passes now repeat every 35 s from 190 s to 400 s. Other settings: RX 9070 XT (driver 32.0.32029.2010), Windows 11 build 26300, warm cache, -Render -Translate -FakeUser -GpuMirror -StartupBelow40 -MemoryGiB 32, a 600 s limit, and fresh storage for each arm. The opt-in arm ran first.

X2W_D3D12_LINEAR_BUFFERS=1 Switch off
Past confirmation yes: 13,083 storage requests, 268,582 decompressions, peak commit 31.1 GB yes: 12,387 / 273,475 / 32.5 GB
Last screen the opening credit sequence, rendered the opening credit sequence, rendered
End second-chance access violation in the main executable at 507.2 s second-chance access violation in the main executable at 492.3 s, at a different site
Frames 11,344 in 502 s (22.6 fps) 11,586 in 486 s (23.8 fps)
Executes 158,943 149,322
Pipelines translated / missing 48 / 438 58 / 434
Slot 133 linear buffers 13,890 copied at submit, 59 shared, 562 kept the default backing with the refusal recorded (518 0x80070057, 44 0x80070032) default path
Shared commit before → 10 s after exit 3.65 → 3.92 GiB 3.92 → 3.94 GiB
  • No visible difference. Both arms render the opening credits the same way, up to their crashes. Under the opt-in, about 13,900 GPU-read buffers now hold the title's bytes instead of zero-filled stand-ins, but nothing in this stretch looks different because of it. The frame rate is about 5% lower with the opt-in. That's a single pair, so it could be noise.
  • The crash isn't the opt-in's. The switch-off arm crashes too, at a different site, so it isn't caused by this PR. It's a new stop on current main; c374c7bf ran this input to 600 s without one. I'll report it on P0: main executable startup progress #870 separately.
  • About 250 MiB of shared commit stayed after the opt-in arm, still unexplained. It was still there 10 minutes later. I tested the obvious candidate on this host: heaps opened with OpenExistingHeapFromAddress over a private range, released and also left alive at process exit, with and without a placed buffer. None of them leave anything behind (within ±10 MiB, the same as the control). The linear_buffer_backing fixtures leave nothing either. It's one sample against a desktop drift of up to about 50 MiB, so I'll repeat the arm before attributing it.

AI-assisted.

@MasterDomino
MasterDomino force-pushed the gpu/linear-buffer-shared-backing branch from 9b8c36f to 07455f0 Compare November 25, 2026 18:35
MasterDomino added a commit to MasterDomino/Xbox2Windows that referenced this pull request Nov 25, 2026
Review of R0Y00#2218:
- before_submit snapshots the copied buffers' work under mutex_, then
  takes, creates or waits for staging (a full pool waits up to 5 s for
  the oldest fence), fills it, resets and records a refresh list and
  runs ExecuteCommandLists without the lock, and publishes the result
  under it again. A release(), the fixture exports or another queue's
  submission no longer wait behind a fence wait or ExecuteCommandLists.
  Staging is still reused only after its fence, the refresh still runs
  at submit, and a buffer whose staging fails is still marked stale.
- In render mode the refresh runs before queue_execute takes draw_lock
  and allocator_lock, so a staging wait does not stall list recording.
- x2w_d3d12_linear_buffer_info returns false for a wrapper without a
  desktop object.

The fixture adds a contention path (X2W_TEST_LINEAR_STAGING_LIMIT=1):
while a refresh waits for the only staging buffer behind a gate, the
info export, release() and a second queue's submission must finish,
and a hold-lock mutant that keeps the lock across the wait must fail.
The copy path also refreshes on COMPUTE and COPY queues, read by a
dispatch and by CopyBufferRegion.

Refs R0Y00#1914
Refs R0Y00#874

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MasterDomino added a commit to MasterDomino/Xbox2Windows that referenced this pull request Nov 25, 2026
Two submissions that snapshot the same copied buffer can publish
`uploaded` in either order, so it may briefly lag the GPU copy. The next
diff is then redundant or larger than needed, never one that misses a
change. Comment only (tedin7's review nit on R0Y00#2218).

Refs R0Y00#1914

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MasterDomino added a commit to MasterDomino/Xbox2Windows that referenced this pull request Nov 25, 2026
Review of R0Y00#2218 (R0Y00): one copied buffer, one CPU write, then a list
on queue 1 behind an unsignalled gate and a list on queue 2. Queue 2
read the old bytes: queue 1's refresh was enqueued behind its gate but
recorded as uploaded, so queue 2 skipped its own.

- Every refresh now runs on one internal COPY queue per device, which
  never waits on a title fence, and signals an internal fence value.
  Each copied buffer remembers the value of its last refresh.
- Every submission through a queue wrapper first makes its queue wait
  for the highest such value among the live copied buffers that has not
  completed. A consumer on any queue runs after every refresh enqueued
  before it, and no title queue waits on another, so title-owned
  cross-queue fences cannot form a cycle with a refresh.
- Staging is reused once its copy's internal fence value completes.
  The per-title-queue fences, after_submit and linear_submitted go.
- If the internal queue or fence cannot be created, copied placements
  keep the default backing with that HRESULT recorded.
- x2w_d3d12_linear_buffer_internal_queue lets fixtures hold refreshes.

The fixture adds gate2q (both reads see the CPU write), gate2q.held
(the second queue waits for a held refresh), xq (queue 1 waits on a
fence queue 2 signals: no deadlock, both read the write) and a
skip-consumer-wait mutant that must fail gate2q.held. Case c and
contention hold the internal queue so staging stays in flight.

Refs R0Y00#1914
Refs R0Y00#874

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

Copy link
Copy Markdown
Collaborator Author

Rebased onto main at c887a65a (after #2443). The new head is 07455f08, with the same 9 commits. I resolved the conflicts in src/startup_stubs/d3d12_xs_device.cpp, and the set of changed lines is identical to the previous head: 8 files, +2680/-28. CI is running.

How the conflicts were resolved

  • release(): keeps the retire handling from startup: opt-in release of retired committed 2D textures after list and fence lifetimes (slice 1) #2443 (retire_placement, retire_drop_recording, retire_when_idle) next to linear_buffers().retire(...).
  • queue_execute order: drain_retired_resources() first. Then linear_refresh(queue), which runs the refreshes on the internal COPY queue and makes the title queue wait on its fence. Then the title's ExecuteCommandLists, then retire_note_submission(...).
  • What that preserves: the consumer wait still comes before the title's lists, and the retire bookkeeping still follows ExecuteCommandLists. The internal queue is not registered as a retire queue, because it is not a title queue.

Re-verified at 07455f08 (Windows 11 build 26300; RX 9070 XT, driver 32.0.32029.2010; WARP 10.0.26100.9818):

  • ctest --preset dev --no-tests=error -R 'linear_buffer' -V: 24/24 passed. That is all 8 paths on both adapters, plus the 8 mutant controls (WILL_FAIL), each still failing its targeted rows. skip-consumer-wait still fails gate2q.held with consumer waits +0.
  • ctest --preset dev --no-tests=error -R '^startup_resource_(retire|budget)': 14/14 passed. startup: opt-in release of retired committed 2D textures after list and fence lifetimes (slice 1) #2443's fixtures are unaffected.
  • ctest --preset dev --no-tests=error -R '^startup_' -j 6: 663 passed, 0 failed, 6 capability skips (77): view_heap ×2, logical_view ×2, small_texture_4k_addrlib and run_sh.

Review and merge. Under the updated merge scope, our own PR goes to another maintainer.

  • @tedin7, could you take the review and merge once CI is green?
  • @R0Y00, your change request from 52d779d9 (the cross-queue stale read) was addressed at 9b8c36fe with the internal copy queue and the consumer Wait; see my comment above. Could you re-review it, or dismiss it if you're satisfied, so it doesn't block?

AI-assisted; commands listed were executed locally.

MasterDomino and others added 4 commits November 25, 2026 21:21
…pt-in)

X2W_D3D12_LINEAR_BUFFERS=1 (or =copy) gives device slot 133 buffers in
COMMON state (row-major or unknown layout) a backing that keeps the
title's bytes visible to the GPU. Unset, slot 133 is unchanged.

- Share only after D3D12_FEATURE_EXISTING_HEAPS reports support (queried
  once, before any open), for a buffer inside one committed private
  allocation at a 64 KiB-aligned offset. The heap is cached by base,
  which the open heap keeps from being released.
- An XMem view is not shared: a heap opened from a view makes XMem's
  unmap of those pages fail with error 87 (measured), and desktop
  resources live for the process. Sharing reports 0x80070032 and the
  buffer is copied.
- Copy into a committed default buffer at each submission through the
  queue wrapper, before the title's lists run, never at bind or record.
  Only changed bytes are copied, through staging buffers that are reused
  only after their submission's queue fence completes.
- Refuse with a defined HRESULT where the copy cannot keep the
  guarantees: a GPU-written use returns the sharing HRESULT (or
  DXGI_ERROR_UNSUPPORTED when sharing was not attempted), no submit
  fence returns E_NOT_VALID_STATE, and a submission whose refresh
  cannot be staged is not executed.

tests/linear_buffer_backing_check.cpp drives slot 133, wrapper lists
and the queue wrapper in d3d12_xs.dll over XMem pages and a private
range with the R0Y00#1942 cases and sentinels: default (old behaviour
asserted), share, copy, host sharing refusal (EXISTING_HEAPS forced to
0) and a copy without a submit fence, plus four mutants that each remove
one guarantee inside the DLL and must fail their case.

Refs R0Y00#1914
Refs R0Y00#874

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A title run with X2W_D3D12_LINEAR_BUFFERS=1 faulted at 1.76 s: the first
eligible placement, a GPU-written buffer across XMem views, was refused
with 0x80070057, and the title, for which CreatePlacedResourceX never
fails on the console, used the null object.

- Where neither sharing nor the copy keeps its guarantees (a GPU-written
  use that is not shared, no submit fence, no copy buffer), slot 133 now
  keeps the default backing and returns what it returns with the switch
  off. The refusal HRESULT is appended to the slot 133 log line, kept in
  x2w_d3d12_linear_buffer_info (path 3, refusal_hr) and counted in the
  totals (kept_default, last_refusal_hr).
- A submission whose refresh cannot be staged now runs. The buffer keeps
  its last upload and is reported stale (info: stale, refresh_failures,
  last_refresh_hr; totals: refresh_failures, last_refresh_hr) until a
  later refresh succeeds; the first failures are logged.

The fixture asserts the old behaviour and the reported refusal for every
kept default (the log line read back through X2W_TRAP_LOG), adds a stale
path (X2W_TEST_LINEAR_REFRESH_FAIL) and a refusal-silent mutant that
drops the report and must fail.

Refs R0Y00#1914
Refs R0Y00#874

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review of R0Y00#2218:
- before_submit snapshots the copied buffers' work under mutex_, then
  takes, creates or waits for staging (a full pool waits up to 5 s for
  the oldest fence), fills it, resets and records a refresh list and
  runs ExecuteCommandLists without the lock, and publishes the result
  under it again. A release(), the fixture exports or another queue's
  submission no longer wait behind a fence wait or ExecuteCommandLists.
  Staging is still reused only after its fence, the refresh still runs
  at submit, and a buffer whose staging fails is still marked stale.
- In render mode the refresh runs before queue_execute takes draw_lock
  and allocator_lock, so a staging wait does not stall list recording.
- x2w_d3d12_linear_buffer_info returns false for a wrapper without a
  desktop object.

The fixture adds a contention path (X2W_TEST_LINEAR_STAGING_LIMIT=1):
while a refresh waits for the only staging buffer behind a gate, the
info export, release() and a second queue's submission must finish,
and a hold-lock mutant that keeps the lock across the wait must fail.
The copy path also refreshes on COMPUTE and COPY queues, read by a
dispatch and by CopyBufferRegion.

Refs R0Y00#1914
Refs R0Y00#874

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Two submissions that snapshot the same copied buffer can publish
`uploaded` in either order, so it may briefly lag the GPU copy. The next
diff is then redundant or larger than needed, never one that misses a
change. Comment only (tedin7's review nit on R0Y00#2218).

Refs R0Y00#1914

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MasterDomino and others added 5 commits November 25, 2026 21:21
Review of R0Y00#2218 (R0Y00): one copied buffer, one CPU write, then a list
on queue 1 behind an unsignalled gate and a list on queue 2. Queue 2
read the old bytes: queue 1's refresh was enqueued behind its gate but
recorded as uploaded, so queue 2 skipped its own.

- Every refresh now runs on one internal COPY queue per device, which
  never waits on a title fence, and signals an internal fence value.
  Each copied buffer remembers the value of its last refresh.
- Every submission through a queue wrapper first makes its queue wait
  for the highest such value among the live copied buffers that has not
  completed. A consumer on any queue runs after every refresh enqueued
  before it, and no title queue waits on another, so title-owned
  cross-queue fences cannot form a cycle with a refresh.
- Staging is reused once its copy's internal fence value completes.
  The per-title-queue fences, after_submit and linear_submitted go.
- If the internal queue or fence cannot be created, copied placements
  keep the default backing with that HRESULT recorded.
- x2w_d3d12_linear_buffer_internal_queue lets fixtures hold refreshes.

The fixture adds gate2q (both reads see the CPU write), gate2q.held
(the second queue waits for a held refresh), xq (queue 1 waits on a
fence queue 2 signals: no deadlock, both read the write) and a
skip-consumer-wait mutant that must fail gate2q.held. Case c and
contention hold the internal queue so staging stays in flight.

Refs R0Y00#1914
Refs R0Y00#874

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With X2W_D3D12_BUDGET=1, slot 133's linear buffers are counted as the
default backing counts its own: a shared buffer as a placed resource
whose heap (opened once per private range) has a backing-heap ticket in
use while a buffer on it is held, a copied buffer as a committed
default buffer. The pool's staging buffers are counted as upload memory
when they are created; they live for the process.

The fixture enables the budget and checks, on the share and copy
paths, that one placement goes live in its category, the heap is live
for a shared one, and the title's release moves it to retained, with
nothing unmeasured.

Refs R0Y00#1914
Refs R0Y00#874

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The mesh XMem lease path (R0Y00#2280; X2W_MESH86_XMEM_SRV or
X2W_MESH_INDIRECT_XMEM) takes the default copied placements of XMem
pages whose console flags are 0: it mirrors their GPU writes into a
leased view of the section and publishes that to mesh readers. Those
are exactly the GPU-read XMem buffers the linear copy path would back.

While the lease path is on, slot 133 now keeps the default backing for
such a placement and records the deferral as a refusal,
HRESULT_FROM_WIN32(ERROR_BUSY), in the log line, the info and the
totals, so one placement never has two owners. Other placements are
unaffected, and nothing changes while the lease path is off.

The fixture adds a lease path (=copy with X2W_MESH86_XMEM_SRV=1): an
XMem buffer keeps the default backing with the deferral reported, and
a private one is still copied.

Refs R0Y00#1914
Refs R0Y00#874

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Main now forwards CopyBufferRegion and CopyResource into slot 133
placed buffers (list_copy.inl), so a copied linear buffer can be
written by the GPU. Those bytes never reach the title memory, and a
later refresh whose changed span covers them copied the title's older
bytes over them.

A copied buffer that a recorded copy writes now keeps the GPU's bytes
from that recording on and is no longer refreshed, as the default
backing behaves for it. The info (gpu_written) and the totals report
it, and the first few are logged.

The fixture adds gpuw on the copy path: a list CopyBufferRegion (list
slot 15) writes G into the buffer, the title changes the words on both
sides, and the next read must still see G. The refresh-gpu-written
mutant keeps refreshing and must fail it.

Refs R0Y00#1914
Refs R0Y00#874

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

Describe, for slot 133's opt-in linear buffers on current main: the
refresh and consumer Wait ahead of red_producers and split mesh
submissions, the D3D12 budget categories, a copied buffer that a list
copy wrote, and the deferral to the mesh XMem lease path. Record the
budget, gpuw and lease cases, the refresh-gpu-written control and the
results at ff06794 on WARP and an RX 9070 XT (driver
32.0.32029.2010), and note sharing XMem views through the section
lease (R0Y00#2280) as a follow-up.

Refs R0Y00#1914
Refs R0Y00#874

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MasterDomino
MasterDomino force-pushed the gpu/linear-buffer-shared-backing branch from 07455f0 to bfdd583 Compare November 25, 2026 20:30
@MasterDomino

Copy link
Copy Markdown
Collaborator Author

Rebased again after today's merges, including #2468 (slice 2) and #2504. The head is now bfdd583f on main 2851685e, and it merges cleanly with current main. The set of changed lines is identical to 07455f08 (8 files, +2680/-28); only context lines moved.

Conflicts, both in d3d12_xs_device.cpp, with both sides kept:

  • release(): slice 2's retire_note_title_release runs before our linear_buffers().retire(...). They don't interact: one updates written_textures, and ours uses the pointer only as a map key.
  • queue_execute: runtime: trace paired slot 86 input writers and fences #2504's split path is kept. The internal-fence Wait is still queued before every title submission, including each split submission, and the retire bookkeeping still follows ExecuteCommandLists.

Re-verified at bfdd583f (RX 9070 XT, driver 32.0.32029.2010, and WARP):

  • linear_buffer: 24/24, including all 8 mutant controls. skip-consumer-wait still fails gate2q.held.
  • ^startup_(resource|list_copy|address_srv): 39/39, which includes the slice 1 and slice 2 retire fixtures.
  • Broad ^startup_: 724 passed, 0 failed, 8 capability skips.

@tedin7, @R0Y00: the review and merge request from my earlier comment still stands. R0Y00's change request from 52d779d9 was addressed at 9b8c36fe.

AI-assisted; commands executed locally.

@MasterDomino

Copy link
Copy Markdown
Collaborator Author

@R0Y00, a point-by-point answer to your change request from 52d779d9 (the same-buffer, cross-queue stale read), verified at the current head bfdd583f:

your request at bfdd583f
track the pending refresh per backing, and order foreign-queue consumers behind it Refreshes are no longer recorded as uploaded when enqueued on the consumer's queue. They run on one internal COPY queue per device and signal an internal fence, and every title submission that reads the backing queues a Wait on that fence before its lists (queue_execute, before ExecuteCommandLists, including #2504's split submissions).
add this same-buffer gate Case gate2q in tests/linear_buffer_backing_check.cpp:1018 is your repro: one buffer holds A and is read once; two read-only lists on two queues read it; the CPU writes B once before either submission; queue 1 is behind a title gate while queue 2 completes first.
both reads should see B gate2q: queue 2 completed with queue 1 still gated and read B 1024/1024; queue 1 then read B 1024/1024. Same for private and xmem, on WARP and the RX 9070 XT.
(stronger) the second consumer must not finish before the refresh lands gate2q.held also holds the internal refresh queue. Queue 2 does not complete until it is released (after waiting for the held refresh), then both read B. The skip-consumer-wait mutant control fails exactly that row (WITHOUT WAITING for the held refresh, consumer waits +0).
keep the different-buffer contention case contention is still there and passes; the hold-lock mutant fails it.

Full run at bfdd583f: ctest --preset dev --no-tests=error -R 'linear_buffer' -V gives 24/24, including the 8 mutant controls.

Since this is our own PR, I can't approve it. Could you re-review, or dismiss your change request if this resolves it? @tedin7 has reviewed the earlier heads; either of you can merge once you're satisfied.

AI-assisted; commands executed locally.

@R0Y00 R0Y00 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed bfdd583f. In Windows Release, the original gated two-queue case now reads B in all 1,024 words on both queues. Holding the internal refresh still blocks the second consumer until the refresh completes, and skip-consumer-wait reproduces the stale read.

Blocking: a discarded copy permanently disables CPU-to-GPU refresh. I added a local source-built case alongside case_gpuw and left the shim unchanged. Place A and complete its first GPU read. On another wrapper list record slot 15 CopyBufferRegion into that buffer, Close, and Reset without submitting the copy. Write B on the CPU, then submit a separate read. The expected read is B with gpu_written=0. Both XMem and private memory returned A in 1,024/1,024 words with gpu_written=1 on WARP and RTX 4060 Laptop.

note_linear_gpu_write changes the backing at recording time. Please tie the transition to the submission of the recording that actually writes the buffer. The existing pending-write tracking in this file already handles Reset and failed Close for textures. Keep the submitted-copy case that preserves G, and add the discarded-copy case for the buffer path. Please also check ordering against earlier overlapping GPU access when committing a write or refreshing a buffer. A consumer fence orders reads after the refresh; simultaneous access alone only permits concurrent operations on disjoint ranges. DirectX specification

Commands for the modified fixture:

cmake --build --preset release --target linear_buffer_backing_check
ctest --preset release --no-tests=error -j 1 -R '^linear_buffer_backing_' -V

Windows build 26200, WARP 10.0.26100.9818 and RTX 4060 Laptop driver 32.0.16.1656, debug layer off. The two copy test entries failed on the added rows. The other 22 entries passed, including all eight mutation controls.

Comment thread src/startup_stubs/list_copy.inl Outdated
note_mesh_xmem_write(self, to.wrapper, destination_offset, bytes, 15, source_kind,
srv_word || mesh_record ? from.resource : nullptr, source_offset);
}
note_linear_gpu_write(to);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call permanently marks the backing before the recording has been submitted. A closed list discarded with Reset leaves the buffer stale. The full WARP and hardware reproduction is in the review summary.

@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

@MasterDomino Win2025 run for head bfdd583fbe9b4d4bc0102bf5866fa3a3300d6ec6 (Windows Server 2025, MSVC, D3D12 WARP):

Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit bfdd583fbe9b4d4bc0102bf5866fa3a3300d6ec6:

  • Debug (--preset dev):
    • Build (cmake --build --preset dev --target d3d12_xs_device_obj d3d12_xs_shim linear_buffer_backing_check -- -j 2): 49.52s (configure 2.70s)
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release --target d3d12_xs_device_obj d3d12_xs_shim linear_buffer_backing_check -- -j 2): 95.40s (configure 4.53s)
  • sccache: 45/257 hits; wall 199.7s

AI-assisted; commands listed were executed locally.

@MasterDomino

Copy link
Copy Markdown
Collaborator Author

Thanks, @R0Y00 and @tedin7 for the Win2025 run. Both points are understood and I'm working on them:

  1. Move the GPU-written transition from recording to submission. It will use the same pending-write path that already handles textures: committed when the recording is submitted, and dropped on Reset, destruction or a failed Close. I'll add your discarded-copy case for both the private and XMem paths (expected: B, gpu_written=0), keep the submitted-copy case that preserves G, and add a mutant that restores the recording-time mark.
  2. Order the refresh and the write commit behind earlier overlapping GPU access to the same backing, not only order later consumers behind the refresh. That comes with a case that would read torn data without the wait.

I'll post the new head with the results.

AI-assisted.

@legacy-code-archaeologist legacy-code-archaeologist left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read the current head and found two correctness problems in src/startup_stubs/linear_buffer_backing.h. Both come from reading the code; I have not reproduced either one.

1. Two concurrent submissions can leave a copied buffer stale for good (refresh(), the Execute/Signal block and the publish below it)

Two submissions on different threads can snapshot the same changed span against the same uploaded. Each then copies the title bytes into its own staging buffer and runs ExecuteCommandLists and Signal outside mutex_. The internal queue runs the two copies in internal_mutex_ order, but uploaded is written later in mutex_ order, and the two orders are independent.

Sequence:

  1. Thread A copies the title bytes X into staging.
  2. The CPU writes Y.
  3. Thread B copies Y into staging.
  4. B takes internal_mutex_ first and gets value 1. A follows with value 2, so the backing ends up holding X.
  5. A publishes uploaded = X, then B publishes uploaded = Y.
  6. The next submission compares the title (Y) with uploaded (Y), finds no change and never refreshes. The GPU keeps reading X until the title changes those bytes again.

The comment above the publish says that the worst case is a redundant copy. That does not hold for this interleaving.

Possible fixes:

  • Do the staging memcpy and the Execute/Signal in one critical section, per buffer or under internal_mutex_.
  • Or publish uploaded for a buffer only from a refresh whose fence value is higher than the last one published for it.

2. Title memory is read after the lock is dropped, so a concurrent release can fault in the DLL (before_submit snapshot → refresh() memcpy)

before_submit collects every live copied buffer, not only the ones the submitted lists use, and calls probe_readable under mutex_. refresh() then copies w.source + w.offset without mutex_. If another thread releases one of those buffers (retire()) and unmaps or frees its pages in between, the memcpy raises an access violation inside d3d12_xs.dll. It only needs a release of an unrelated buffer on another thread while some queue submits.

Possible fixes:

  • Recheck live and readability right before the memcpy, with retire() and the memcpy under the same lock.
  • Or guard the copy with SEH and mark that buffer stale.

AI-assisted (Claude Code).

@MasterDomino

Copy link
Copy Markdown
Collaborator Author

Thanks, @legacy-code-archaeologist. Both points look right from the code. I'm folding them into the same rework as R0Y00's discarded-copy and ordering findings, since they touch the same refresh path:

  • (A) The staging copy, Execute/Signal and the uploaded publish will follow a single order per buffer, so a later snapshot can't be overwritten by an earlier one landing second. The "redundant copy" comment will be corrected.
  • (B) retire() and the title-memory copy will sit under the same lock, with live rechecked just before copying.

Each will get a deterministic control where the fixture can force the interleaving, plus a mutant control. Thanks also, @tedin7, for the Win2025 build. New head and results to follow.

AI-assisted.

MasterDomino and others added 6 commits November 25, 2026 21:50
A list copy into a copied linear buffer changed the backing when the
copy was recorded. A recording that was closed and then reset without
being submitted therefore stopped the buffer's refreshes for good: the
next read saw the old upload instead of the CPU's later bytes, on XMem
and private memory alike (review repro on WARP and an RTX 4060).

The write is now pending on the recording, next to the texture writes
that list_copy.inl already tracks: commit_list_gpu_writes marks the
buffer GPU-written after the recording's ExecuteCommandLists, and a
Reset or a failed Close (discard_list_gpu_writes) drops it. The
recording's own submission still refreshes the buffer first, and its
queue waits for that refresh before the copy runs.

The gpuw-at-record mutant restores the recording-time mark.

Refs R0Y00#1914
Refs R0Y00#874

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A refresh's copy into a copied linear buffer was ordered only before
later consumers (their queues Wait for the internal fence). An earlier
submission that still reads or writes the backing, on any queue, could
run at the same time as the internal COPY queue overwrote it.
Simultaneous access allows concurrent operations on one buffer only on
disjoint ranges.

Each queue wrapper now signals a user fence of its own after the
title's lists (render and plain submissions). Before a refresh's copy
executes, the internal queue Waits for every queue's latest user fence
value that has not completed (totals: producer_waits). The wrappers
cannot tell which buffers a submission reads, since titles bind them by
GPU virtual address, so every earlier submission counts as a user of
every copied backing.

A queue whose latest submission sits behind a title Wait (queue slot
17) on a fence that has not been reached is not waited for (totals:
producer_wait_skips, logged): a later submission that waits for this
refresh may be the one that signals that fence, and waiting would
deadlock both queues. That submission has not started and reads the
CPU's newer bytes once it runs. A fence reached while the copy still
executes can still overlap the read; the header says so.

A submission now also waits for the refresh it enqueued itself, even if
the title released the buffer meanwhile; before, such a refresh was
left out of the consumer Wait.

The contention case asserts the new order: S1, gated, reads B because
C's copy waits for it; S2 reads C. The gate opens before the internal
queue is drained. The skip-producer-wait mutant removes the wait.

Refs R0Y00#1914
Refs R0Y00#874

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Two submissions on different threads could snapshot one copied buffer
against the same upload record, stage the title's bytes at different
times, execute their copies in one order and publish the upload record
in the other. With X staged first, the CPU then writing Y and Y staged
second, Y's copy could take the lower internal fence value and X's the
higher, so the backing held X while the record said Y. No later
submission refreshed the buffer until the title changed those bytes
again. The comment that called this a redundant copy at worst was
wrong.

A copied buffer now remembers the internal fence value of its newest
applied publish. A newer refresh applies its span; an older one that
arrives after it drops the record (totals: reordered_publishes), and
the next submission copies the whole buffer. An unknown record is set
again only by a copy of the whole buffer.

The publish-out-of-order mutant applies every publish in lock order,
as before.

Refs R0Y00#1914
Refs R0Y00#874

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
before_submit snapshots every live copied buffer, not only those the
submitted lists read, and probes its title memory under the lock.
refresh() then copied that memory into staging without the lock. If
the title released one of those buffers on another thread and freed or
unmapped its pages in between, the copy faulted inside d3d12_xs.dll.

The title memory is now probed again right before it is staged, and
the copy runs under a fault guard (access violation or in-page error).
A buffer whose memory is gone is skipped and counted (unreadable), as
the snapshot already does; a read that faults anyway is counted
(copy_faults) and fails that buffer's refresh. A buffer released after
the snapshot whose pages are still mapped is staged for that
submission, which may read it (the contention case does).

The wrappers cannot tell which buffers a submission's lists read, so
the snapshot still covers every live copied buffer.

The copy-after-retire mutant skips the second probe; the guard then
catches the faulting read.

Refs R0Y00#1914
Refs R0Y00#874

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

New cases on the copy path, on XMem and private memory:

- gpuw.discard (the review repro): a list copy into a copied buffer is
  recorded, closed and reset without a submission; after the CPU writes
  B the next read must see B, with gpu_written 0.
- war: a read of A on queue 1 behind a title gate; the CPU writes B and
  queue 2 submits a read. Queue 2 must not complete while the gate is
  closed, queue 1 must read A, queue 2 B, and producer_waits must grow.
- xq.write: queue 1 waits through its wrapper (slot 17) on a title fence
  and submits a read; after the CPU writes B, queue 2's read must
  complete while queue 1 still waits (no deadlock), both read B, and the
  skip is counted.
- order: two threads refresh one buffer, parked through a new fixture
  hook (x2w_d3d12_linear_buffer_test_pause) so that the older copy
  publishes last; the next submission must refresh and read D.
- retire: a submission parks after its snapshot while the title
  releases X and frees its pages; X must be skipped as unreadable, with
  no copy fault and no failed refresh, and Y must read C.

Four controls run on WARP with WILL_FAIL, each failing its own case:
gpuw-at-record, skip-producer-wait, publish-out-of-order and
copy-after-retire. The totals line prints the new counters.

Refs R0Y00#1914
Refs R0Y00#874

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nd refresh races

Describe the review fixes for slot 133's copied linear buffers. A list
copy marks a buffer GPU-written only when its recording is submitted.
A refresh's copy waits for every queue's earlier submission, except
one that sits behind an unreached title Wait, which is skipped so that
title fences cannot close a cycle. Uploads are published in the order
their copies execute. Title memory is probed again and read under a
fault guard right before it is staged.

Add the gpuw.discard, war, xq.write, order and retire cases, the
changed contention expectation (S1 reads B), the four new controls,
and the results at 981b519 on WARP and an RX 9070 XT (driver
32.0.32029.2010) in the dev and release presets. Note the remaining
overlap window and the cost of treating every submission as a user of
every backing.

Refs R0Y00#1914
Refs R0Y00#874

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

Copy link
Copy Markdown
Collaborator Author

@R0Y00, @legacy-code-archaeologist: all four points are addressed at 58971a4a, a fast-forward from bfdd583f in six focused commits. One design point needs your decision; it's marked below.

# finding fix (commit) new case / mutant control
1 A discarded copy permanently disables refresh (R0Y00) e880a3cb: note_linear_gpu_write no longer marks the backing at recording. It queues the buffer in a per-list pending map under the same pending_writes_lock as the texture writes. commit_list_gpu_writes commits it after ExecuteCommandLists, and discard_list_gpu_writes drops it on Reset and on a failed Close. gpuw.discard: recorded, closed, reset unsubmitted, then the CPU wrote B. Result: read B 1024/1024, gpu_written 0. Mutant gpuw-at-record: read A, gpu_written 1. gpuw still keeps G.
2 Refresh not ordered behind earlier overlapping access (R0Y00) 07c7a19e: each queue signals a user fence after the title's lists. Just before a refresh executes, the internal queue Waits on every queue's latest unreached user-fence value. The shim can't tell which buffers a submission reads, so every earlier submission counts as a user of every copied backing (conservative). war: queue 1 gated with a read of A, then the CPU wrote B and queue 2 submitted. Result: queue 1 read A, queue 2 read B, producer waits +1. Mutant skip-producer-wait: queue 1 read B.
A Concurrent refreshes publish out of execution order 8c52d38d: each publish records the internal fence value it applied. An older refresh that publishes after a newer one clears the upload record, so the next submission copies the whole buffer. The "redundant copy at worst" comment is corrected. order, driven by a test-only pause hook: thread A stages C and parks; thread B stages D and executes first; A publishes, then B. Result: the next read is D, reordered publishes +1. Mutant publish-out-of-order: read C.
B Title memory read after the lock is dropped b605d7c9: the title memory is probed again immediately before staging, and the copy runs under an SEH guard (access violation or in-page error). Memory that is gone counts as unreadable; a read that faults anyway fails that buffer's refresh instead of faulting the DLL. retire: parked after the snapshot while the title released X and freed its pages. Result: unreadable +1, copy faults +0, and the other buffer still refreshed. Mutant copy-after-retire: the guard catches the fault (0x800703E6), with no crash.

Decision needed: one exception to "wait on all prior users" (fix 2).

  • The deadlock: waiting on every earlier submission deadlocks a common pattern. Queue 1 Waits on a title fence F and submits; the CPU writes; queue 2 submits, which triggers a refresh, and signals F afterwards. The refresh would wait for queue 1, queue 1 waits for F, and F waits for queue 2.
  • The exception: a queue whose latest submission sits behind a title Wait (slot 17) on an unreached fence is skipped. It's counted as producer_wait_skips and logged.
  • Proof there's no deadlock: case xq.write. Queue 2 completed while queue 1 still waited and read B; queue 1 then read B.
  • The window left open: that skipped submission hasn't started yet, so it reads the newer bytes. If F is reached while the copy is still executing, its read could overlap the copy.
  • Not visible to the shim: a Wait issued on the desktop queue directly, bypassing the wrapper. That queue is waited for normally.

Is the exception acceptable, or would you prefer the conservative behaviour plus an explicit refusal for this pattern? I didn't add a mutant that removes the guard, because it would hang the GPU.

Changes to existing behaviour:

  • contention: now asserts that S1 reads B (C's copy waits for S1) and S2 reads C. Before, both read C.
  • A submission now also waits for its own pending refresh even if the title released that buffer in the meantime. Before, a retired buffer's refresh was left out of the consumer wait.

Runs (Windows 11 build 26300; WARP 10.0.26100.9818; AMD Radeon RX 9070 XT, driver 32.0.32029.2010):

command result
ctest --preset dev --no-tests=error -R 'linear_buffer' -V 28/28 (16 path entries + 12 mutant controls, each failing its targeted rows); I reran this one myself at 58971a4a
the same in --preset release (linear_buffer_backing_) 28/28
`ctest --preset dev --no-tests=error -R '^startup_(resource list_copy
`ctest --preset dev --no-tests=error -R '^startup_' -E 'heap_leak mapping

Not covered: the reverse publish order (correct by construction, with no separate case), and runs with the debug layer enabled.

AI-assisted; commands listed were executed locally.

@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Closing at the author's request (@MasterDomino asked us directly). The review findings from @R0Y00 and @legacy-code-archaeologist stay in this thread for the follow-up.

AI-assisted; commands listed were executed locally.

@tedin7 tedin7 closed this Nov 25, 2026
@MasterDomino MasterDomino reopened this Nov 25, 2026
@MasterDomino

Copy link
Copy Markdown
Collaborator Author

@tedin7, reopening: I didn't ask for this to be closed, so I think a request got crossed somewhere. If you saw a message that looked like it came from me, could you point me to it?

Nothing has changed since the close. The head is still 58971a4a, with all four review findings addressed as described in my 21:20Z comment. Still open: @R0Y00's re-review, and his decision on the deadlock-guard exception in fix 2.

AI-assisted.

R0Y00 commented Nov 25, 2026

Copy link
Copy Markdown
Owner

For the xq.write decision, keep the conservative ordering and explicitly refuse a submission that needs an overlapping refresh with no safe completion order. Skipping a user fence because that queue is currently blocked does not establish that the fence stays blocked until the copy finishes. A different producer can signal it during the copy. An opt-in should still preserve the visibility rule agreed in #1914.

Please detect the unsafe case before enqueueing any refresh or affected guest list, log/count a distinct refusal, and leave write/refresh bookkeeping consistent. Keep the wrapper’s ordinary fence Signal path usable so a refused list cannot strand the earlier queue. Test it with the earlier queue gated and a second CPU write, then release the gate and verify the earlier read of A, unchanged output for the refused read, queue progress after the explicit signal, and a later safe submission reading B. Keep the existing successful gated and ordinary write-after-read controls. A proven disjoint resource or an immutable per-submission backing could support more cases separately.

@tedin7

tedin7 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

@MasterDomino sorry about that, the close was our mistake: a request got crossed on our side and we acted on it without checking with you here first. Thanks for reopening. We will not touch the state of this PR again; the review is with @R0Y00 as you wrote.

AI-assisted; commands listed were executed locally.

@MasterDomino

Copy link
Copy Markdown
Collaborator Author

Thanks, @R0Y00: understood, and thanks @tedin7, no harm done.

For xq.write I'll drop the skip and keep the conservative ordering:

  • Detect before enqueueing: a submission is checked before any refresh or guest list is enqueued. If it needs an overlapping refresh and an earlier submission on another queue is still behind an unreached title Wait, there's no safe completion order. That submission is refused, with its own logged and counted reason, and write and refresh bookkeeping is left unchanged.
  • The fence path stays usable: the wrapper's ordinary fence Signal keeps working, so a refused list can't strand the earlier queue.
  • New case: the earlier queue gated, then a second CPU write. Release the gate and check:
    • the earlier read gives A;
    • the refused read's output is unchanged;
    • the queue progresses after the explicit signal;
    • a later safe submission reads B.
  • Controls: the existing gated and ordinary write-after-read controls stay, plus a mutant that accepts the unsafe submission.

New head and results to follow.

AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants