Merging.
**Why merged**
- **The bug:** a texture whose last reference was a descriptor overwritten inside the title-texture registry was destroyed on a thread that already held the registry mutex. Its destructor then locked that mutex again (`resource_deadlock_would_occur`, an abort under Wine).
- **The fix:** such a texture now leaves `gpu_placements` immediately, under `descriptor_mutex`, which that thread never holds. Its registry entry stops naming it until `drain_titles_gone` erases it after the registry lets go.
**Why it's good**
- It's small, and on the slot-70 view path used during the load.
- The deferred part only holds a registry entry with no resource, so nothing the submission thread reads points at freed memory. That was the window in the first revision, fixed in 152cee255.
- `log_title_census` and the refresh skip such entries.
- **Tested** on an RTX 4070 SUPER (Windows 11), merged onto current main:
- full ctest 448/449, where the only failure is the known depth-resolve MFAA case;
- the 13 title/texture/heap tests pass under Vulkan validation with no VUID;
- the consistency set is unchanged (one timing flake passed on re-run; the PR is C++ only).
- The diff was reviewed for safety: no game data, no new I/O.
**Follow-ups welcome**
- A test that releases the last reference from inside `write_placed_texture_view`.
- The same guard on the slot-71 UAV path.
AI-assisted (Claude Code, Claude Opus 5.5).
When released_backings or title_va_aliases().invalidations() advances,
refresh_console_heap() previously scanned all 32-byte slots of heap.copy
linearly to find live buffer V#s (!(w[3] >> 30) && (w[2] || w[6])). When
heap.copy sits in arena.mapped (uncached write-combined GPU memory), each
buffer release triggered ~180k uncached PCIe reads across a 5.5 MiB heap.
Add a portable host-RAM bitset (leonida::ConsoleHeapVsharpSlots in
runtime/src/console_heap_vsharp_slots.h) updated at every resource-heap
write to heap.copy (fix_console_slot, deferred fix_over zeroing,
for_each_zeroed, and add_console_heap reset), and iterate only live V#
slots during invalidation passes (0 reads from heap.copy; 64 slots skipped
per word via std::countr_zero).
Item: RT-CONSOLE-HEAP-VSHARP-BITSET
Review of #1798: the submit path reads gpu_placements under descriptor_mutex alone, so the
placement of a texture destroyed inside the registry must go before ~Resource frees it.
descriptor_mutex is never held there (~Resource takes it itself, in image_owner and
forget_image); only the title_textures erase waits for drain_titles_gone. log_title_census
skips an entry without a resource, and refresh_title_textures leaves the registry scope
before it unlocks.
A CopyTextureRegion of the depth plane from a D32_SFLOAT_S8_UINT image into a
D32_SFLOAT one was replayed as vkCmdCopyImage. Vulkan requires depth/stencil
formats to match exactly (VUID-vkCmdCopyImage-srcImage-01548), and the NVIDIA
driver (RTX 4070 Laptop, 621.40) wrote past the end of the D32 image: the
world-start WRITE_INVALID device loss, with the fault at the first byte after a
2560x1440 D32_SFLOAT image (VK_EXT_device_address_binding_report) and this VUID
reported by the validation layer just before the loss.
depth_format_pair: two depth formats with the same depth encoding but not the
same VkFormat (D32 / D32S8, D16 / D16S8, X8_D24 / D24S8) take the image ->
scratch -> image path depth <-> colour and A8 copies already use; the depth
aspect has the same texel layout in a buffer on both sides.
replay_copy_offset_test: the D32S8 -> D32 and D32 -> D32S8 depth-plane copies
are staged (no vkCmdCopyImage, depth aspect, 4 bytes a texel), and D32 -> D32
keeps vkCmdCopyImage. Fails without the change (6 checks).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On a driver that refuses importing a section view (RADV: amdgpu userptr takes
anonymous memory only), the layer answers with a shadow at the same title VA,
so the `may_refuse` early return never fires. The forward readback still
passes through the shadow, but a shadow is never written back, so the new
reverse-copy check failed on an AMD iGPU under Wine. Check the caller's pages
only when the buffer is a real import.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Main's ba626820e already handles a title address past
LEONIDA_TITLE_WINDOWS_MB: the persistent geometry copy backs it, or
else every view writer emits a zero-size V# (loads 0, stores dropped),
so the raw VA never reaches the GPU (#1537). That replaces this branch's
zeroed scratch window, so the runtime and docs side take main's version.
The d3d12_title_window_cap test stays, rewritten for main's behaviour:
with a cap of 0, two CBVs of private title memory are written zero-size
(no base, no records, never the raw VA), no window is made, and a
dispatch through one reads zeros. Main had no test that dispatches
through an over-cap title address.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A build without SDL3 answered GameInputCreate with no devices and E_NOTIMPL
for every other slot, so the title waited at its first "press A" prompt
forever: no CI artifact or offline build could get past it.
With LEONIDA_INPUT_SCRIPT set, gameinput_no_sdl.cpp now serves one scripted
pad (an Xbox One gamepad) driven by the script alone: device enumeration
through RegisterDeviceCallback (blocking, async, dispatcher), FindDeviceFromId,
and GetCurrentReading/GetNextReading/GetPreviousReading whose gamepad and UI
navigation state follow the script. Readings are taken when the title asks,
so no thread polls. Without the variable it behaves as before.
The parser, the 300 ms timeline, the button names, the press log lines and
the UI navigation mapping move from gameinput.cpp into gameinput_script.h,
which both shims use; nothing in it needs SDL. gameinput.cpp checks the
shared SDL button numbers against SDL_GamepadButton at compile time.
gameinput_no_sdl_test --input-script (CTest gameinput_no_sdl_script) checks
the scripted pad on a fake clock. Docs: runtime/README.md, docs/gameinput.md,
docs/linux_proton.md, docs/README.md.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The last reference to a slot-70 texture can be a descriptor that write_placed_texture_view
overwrites while it holds title_texture_mutex: ~Resource then called title_texture_gone, which
took the same mutex again. MSVC's std::mutex throws resource_deadlock_would_occur there, and
Wine's msvcp140 has no _Throw_Cpp_error, so the boot aborted (stack: proxy_create_placed_view_x
< write_placed_texture_view < write_title_srv < CreateShaderResourceView < ~Resource). Inside
the registry the entry now only stops naming the resource, and drain_titles_gone removes it and
its GPU placement once the thread has released the mutex.
Fix edge-case arithmetic and boundary bugs in the title-memory window
planner and GPU address arena allocator:
1. runtime/src/title_window_plan.h:
- title_window_range(): reject va < lo (previously returned a range
starting at lo > va that excluded va, causing va - range.start to
underflow in resolve_title_memory), reject most == 0 (avoiding
std::clamp(want, 1, 0) UB), require power-of-two align, and avoid
UINT64_MAX - want - align unsigned underflow when computing end.
- title_pool_extent(): reject non-power-of-two align and unreadable
requests (readable_end <= va) before rounding end up to align, and
enforce the most cap after alignment rounding so out.size <= most
always holds and out contains va.
2. runtime/src/arena_blocks.h:
- Make align_up() overflow-checked so high alignments or keep_out_end
near UINT64_MAX cannot wrap to 0 and trigger an infinite loop in
ArenaBlocks::allocate().
- Guard against non-advancing keep_out_end <= start in allocate().
- Make consistent() safe against reserved_ > size_ underflow and
free_bytes_ + live_bytes overflow.
Extend runtime/test/arena_blocks_test.cpp and add portable
runtime/test/title_window_plan_bounds_test.cpp.
Item: RT-TITLE-WINDOW-AND-ARENA-BOUNDS
adrianmjim's review: the blend fallback ran on any E_NOTIMPL, which also stands for
refusals unrelated to blending (stream_unsupported, mesh limits, FullyCovered, and
#1703's dual-source refusal). Such a pipeline was retried with blending off, logged as
a blend refusal, and, if the retry succeeded, drew without the blend the title asked for.
Now only a pipeline with a blended B4G4R4A4 target is retried, with blending off on
those targets alone (without_stand_in_blend, its own header), and the line is printed
only when that retry succeeds. The "every target" step is gone: no real boot used it
(RTX 4070 Laptop and kawaii-shakal's RTX 5070 Ti only ever logged the B4G4R4A4 step).
native_pso_fallback_test covers the decision.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
No conflicts. Brings #1723's final form: the README rows for 32
LEONIDA_* variables and indirect_clamp_test's ctest registration.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#622's rows had slots 73 and 74 as no-ops and the GPU-label path as the
only writer of 71 and 72. Under #1723 the list values record all four
for the lists they track (waits included), and LEONIDA_GPU_LABELS=1
writes 71/72 only for the others. The command-object table, the value
write paragraph, §5.5 of the console ABI notes and the README row now
say so, and name the arena and xmem fallbacks (src/value_write_sink.h).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#1723's list values own list slots 71 to 74 of the lists they track and
hand 71/72 to the GPU-label path only for lists they do not track. This
keeps that, and keeps what #622 adds that #1723 lacks: landing a value
in the GPU address arena and on an xmem page the title's view refuses.
- LEONIDA_GPU_LABELS stays opt-in (`1`), as on main and in #1723.
- src/value_write_sink.h: one place for the two targets a host store
cannot reach, used by both writers. The device answers for the arena
(an address below its size that is not the title's memory, the rule
resolve_title_memory uses); xmem registers write_title_bytes for its
pages. Replaces #622's label-only sink and xmem's title_write_fn.
- The pump's land(): the arena is asked before any host store, so an
arena offset is never stored to as a host address; then
kernel_writes::store as before; then xmem's mapping when that store
faults. Counted as writes_arena and writes_title_pages.
- The arena refuses its reserved start (null region, zero page, quad
indices) and a write past its end, stores an aligned value in one
access, and is unregistered when its device goes.
- xmem's write_title_bytes stores an aligned value in one access.
- The label path keeps the same order for untracked lists.
Test, no game data: xbox_list_value_fallback. A tracked list's writes to
title memory land through the host store; to a default-heap buffer's
arena offset land in the arena (read back by a GPU copy); to the arena's
reserved start are dropped; to an xmem page the title made PAGE_NOACCESS
land through xmem's mapping. LEONIDA_GPU_LABELS=1 throughout, and no
write is handed to the label path. Without the arena step it fails at
line 168, without the xmem step at line 208 (llvmpipe and the iGPU).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
indirect_clamp_test came with #1723 and was registered through the foreach
over ${test}_test with the other self-checking Vulkan tests, which
tests/test_ctest_registration.py cannot see, so it reported the executable as
built but never run. It is now registered next to its own add_executable, with
the same SKIP_RETURN_CODE 77 and BUILD_TESTING guard, and left out of the
foreach. Built and run with ctest on Linux: passed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The integration of the post-story-load runtime changes (#1723) reads 32
environment variables that no document named, so tests/test_docs_env_vars.py
failed. Each now has a row in the D3D12 switches table of runtime/README.md
with its default and what its values do, read from the code: the console heap
fix and retry budgets, the per-frame and asynchronous refresh trade-offs, the
title texture fill and view settings, the XMem and queue slot 31 switches,
the hang exit, the thread sampler, the submit-phase line and the Aftermath
extras. Diagnostic-only switches are marked so.
LEONIDA_QUEUE_PAGE_MAPPINGS is read by rt::xmem::queue_page_mappings_enabled(),
which nothing calls; its row says it has no effect at present.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Conflicts:
- runtime/README.md: #1723's LEONIDA_GPU_LABELS row (opt-in, only for
lists the list values do not track). The rows #622 changed are
brought in line with #1723 in the commits after this merge.
- runtime/shims/d3d12_vulkan_device.cpp: both sides. #622 tracks the
arena for the label path; #1723 resets the arena's block tree under
its lock and starts the arena after the null region.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
runtime/test/xs_proxy_mock_test.cpp: main added check_depth_provider_refusal()
next to check_placed_resource_x() (#1558); this branch added the list-values
checks, which must run last because they start and stop the list-values
threads. Both kept, the list-values checks still last.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Found by the comparison agent, not by a failing test. #1723's console
list slot path still handed slots 71 and 72 to main's GPU-label hook
(record_gpu_label, LEONIDA_GPU_LABELS) when its own list values track
the list (track_values, on by default with LEONIDA_LIST_VALUES). The
--first-run-ui profile sets LEONIDA_GPU_LABELS=1 (runtime/src/main.cpp).
With both on, two threads stored to one title address, one from a
Vulkan command buffer's completion and query, the other from the part's
fence and a D3D12 timestamp query. A late label could rewind a rising
counter or split a begin/end pair. The suite did not catch it:
xbox_gpu_label_completion submits its real list straight to the queue,
so only the label path landed there.
The list values are the right owner. They land slots 71 to 74 in
queue order behind the gates, and they also do the waits the labels
cannot do. Now:
- The label hook runs only for a list the list values do not track
(LEONIDA_LIST_VALUES=0, bundles). Each handoff is counted in
ListValueStats::labels_handed.
- The --first-run-ui profile no longer sets LEONIDA_GPU_LABELS.
runtime/README.md and docs/first_run_ui.md say so.
- xbox_gpu_label_completion runs its label section with the list values
off, as it always measured the label path alone, and checks that all
five writes went there. A new section turns both on and records one
write32 and one write64 on a tracked list, submitted through the queue
proxy. Each lands exactly once: writes_landed == 2, labels_handed == 0,
and the values are in memory. With the old condition this section
fails at line 136 (labels_handed was 2), on llvmpipe and the iGPU.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#1723's new d3d12_compile_teardown failed on llvmpipe at line 436: no
pipeline_cache.session.bin after teardown. It passes on the iGPU.
The runtime was right and the test assumed every driver keeps compiled
pipelines in a VkPipelineCache. Measured with a print in
save_session_pipeline_cache: after the warm-up build, llvmpipe's
vkGetPipelineCacheData returns VK_SUCCESS and 32 bytes, the header alone
(the save limit was 64 MiB). The runtime writes no file of a bare header
on purpose, as it is no cache.
The check now reads the driver ID (VkPhysicalDeviceDriverProperties).
On llvmpipe the file must be absent; on every other driver it must be
written at teardown, as before. The rest of the case (the background
build ran, the worker joined) is unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
d3d12_rebound_tables, d3d12_rebound_second_device and
d3d12_api_descriptor_copy went from pass (main) to fail at line 896 of
d3d12_title_va_test: 0/64 GPU words matched on both submissions,
although the mirrored CBV in the layer heap was right (checked by
printing the slot after the submission). They had been hidden behind
line 502 in the same executable.
main was right. Its rebound-table check selects the test shader's
indexed CBV mode with s5 = 0xFFFFFFFB. #1723 added a mode that returns
a descriptor's words instead, also on 0xFFFFFFFB, ahead of main's
branch, so the rebound tables read the words at s8 (unset, address 0)
instead of the CBV's data. #1723's mode now uses 0xFFFFFFFA, which
nothing else uses, and its one caller (the out[3] dispatch in
check_title_va) passes that. Both checks run as written.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
d3d12_xmem_placed failed at line 113 (to_cpu(dst) == dst) and line 244
(independently_mapped(gpu, size)). #1723 added View::independent_pages,
the to_cpu branch and independently_mapped for a GPU-only range that
queue slot 31 maps by itself, but nothing ever sets the flag: xmem
calls note_reservation without it, so every pair it makes is the legacy
kind, read at its CPU view. Nothing in the runtime read
independently_mapped either, although its comment says a slot-133
buffer there must be a shadow, not an import.
Neither side is simply right. The tests express #1723's design, but no
rule exists for which of the title's pairs are independent, and none
can be inferred: marking every GPU-only pair independent (tried) breaks
main's xmem test, whose pair (attributes 0x23/0x3, protect 0x2004 then
0x801, two constant sets docs/xmem_exe_calls.md lists among the
title's sites) must read at the CPU view, and
would move where DirectStorage, slot-70 views and slot 31's CPU mirror
read and write in the game. That rule is left to #1723's author.
So:
- title_gpu_views::set_independent_pages(gpu) marks a pair. Nothing in
the runtime calls it yet, so the game's behaviour does not change.
- create_host_buffer declines an import over an independently mapped
range before making anything, as independently_mapped's comment asks,
and the shadow answers. On llvmpipe the import otherwise succeeded and
the twin was never guarded (line 264, seen with every pair marked).
- d3d12_xmem_placed marks its two windows. It now also checks that xmem
made them the legacy kind and that marking changes to_cpu and
independently_mapped. Everything after that (slot 31 remapping only
the GPU range, the CPU view untouched, the guarded twin, pins, host
and kernel writes) is checked as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On the RDNA2 iGPU (RADV) d3d12_compute went from pass (main) to fail at
line 676, and #1723's d3d12_unmapped_vsharp failed at line 135 and
d3d12_graphics_indirect_heap_resources at line 478. In each, a view of
an upload buffer came out zero-size ("buffer SRV: address
0xffffffffffe20000 (16 bytes) has no GPU copy; zero records"). Measured
there: RADV puts upload buffers below the arena, so their VA (device
address minus heap_base) is negative, 0xffffffffffe30000 in
unmapped_vsharp. llvmpipe puts them above it, and all three pass there.
main was right. docs/design.md ("Heap-relative buffer device
addresses") makes the V# base a signed 48-bit offset: the translator
sign-extends it, and main wrote such an offset for the layer's own
buffers. d3d12_compute's shader reads it sign-extended, as the contract
says. #1723's vsharp_address accepted a negative offset only with
LEONIDA_IMPORT_BELOW_ARENA=1, the switch that decides whether title
memory may be imported below the arena, and wrote every other view of
such a buffer as zero-size.
encodable() now accepts a negative offset that fits in 48 signed bits
whatever the switch says. backed() still requires a buffer registered
in gpu_backings or the geometry copies, so the switch alone still
decides whether there is a title import there. An already-written V#
whose bit 47 is set is also read as negative when that is what names a
backing. Unbacked addresses still become zero-size, as #1723 wants.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
d3d12_title_va failed at line 502 and d3d12_unmapped_vsharp at line 191:
a V# the CPU writes into a bound table of a layer heap, naming an
address nothing backs (an unknown high address, a view past the end of a
real buffer), kept its base after the table scan, so a shader would add
heap_base to it and fault.
The tests were right. #1723 added fix_vsharp, which rebases a V# into
the title's memory and writes one with no GPU backing as zero-size, and
gave it an "inline table" path (VsharpPath::table), but only the console
heap copy calls it; scan_title_table still called rebase_title_vsharp,
which leaves an unbacked base as it is, and only the slot's first V#.
scan_title_table now runs fix_vsharp on both V#s of each slot (the
second is a UAV's counter), as fix_console_slot does.
With line 502 passing, the rest of d3d12_title_va runs again, #1539's
check_declined_imports and check_import_over_shadow (run by d3d12_geometry_views)
included. The same executable's d3d12_api_descriptor_copy and
d3d12_rebound_* registrations stopped at line 502 too.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merging.
**Why merged**
- Fixes the NVIDIA startup stop that #939 introduced. Slot 133 asks for a D16S8 (format 118) depth texture with unordered access. On NVIDIA, `pc_format()` maps it to D24S8 and the depth provider refuses UAV on that, so the boot died early. With this PR the refusal is logged once and the texture is made without UAV, i.e. the pre-#939 stand-in.
- Independent real-boot confirmations past the stop, each against the same build without the PR stopping there:
- RTX 4070 SUPER, Windows 11;
- RTX 5060 Ti, Windows 10 (Snooozs);
- RTX 3070 Ti, Wine 11.22 (djrolfes);
- RTX 3080 Ti, Wine 11.17 (slauko: 603 s, no exception);
- RTX 3090, Proton (azsekazvamdimitar).
- On AMD (adrianmjim, RX 7900 XT) the native path is untouched and the related tests pass, including a GPU without D24S8 that falls back to D32S8.
- Re-tested here merged onto 3de22a190 (after #1547) on an RTX 4070 SUPER, and the merge onto the current main (57ba40bd6, which only adds #1493's translator change) is also clean:
- fresh RelWithDebInfo build;
- full ctest 410/411, where the only failure is `d3d12_depth_resolve`, the known NVIDIA MFAA sample-position case fixed by #1505;
- the PR's 16 related tests pass, also with `LEONIDA_VK_VALIDATION=1`;
- the consistency set is the same as main.
**Why it's good**
- It's a narrow fallback, used only when the provider refuses. Consoles and GPUs that accept the format keep the native path.
- It comes with tests for the refused and accepted cases.
- It unblocks every NVIDIA tester at the same spot.
**Known and separate:** the slot 133 stand-in leaks about 60 Vulkan objects at device teardown under validation. That leak predates this PR and is reported in #1608. Full D16S8 UAV semantics on NVIDIA remain a follow-up.
AI-assisted (Claude Code, Claude Opus 5.5).
xs_proxy_mock failed at line 4791: on a queue whose presentation is
managed outside (set_presentation_handler), raw(queue, 31, 0) must be
E_NOTIMPL and must not reach SuspendX. #1723's CopyPageMappingsBatchX
dispatch answered every slot-31 call itself, and with no hook and no
xmem (a proxy compiled alone) queue_copy_page_mappings returned S_OK
having mapped nothing.
The test was right. The file's own comment says that without xmem the
slot is refused as before, and check_queue_copy_page_mappings expects
the console-only answer every unmodelled queue slot gets in that case.
The new dispatch now runs only when something can remap (a hook set
with set_copy_page_mappings_hook, or xmem's copy_page_mappings_fn);
otherwise the call goes on to the presentation routes and the
unmodelled-slot answer as on main. With xmem linked, as in gta6rt,
nothing changes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The list-values test of #1723 installs `record_page_mapping` as the
CopyPageMappingsBatchX hook and checks `g_page_mapping`, but neither was
defined, so xs_proxy_mock_test did not compile. The recorder counts the
calls and keeps the arguments plus a copy of the first batch's first
ranges, so section 6 can tell a deferred remap of the proxy's copy from
one made at the call with the caller's array. It is written from
`CopyPageMappingsHook` (shims/d3d12_xs_device.h) and the fields the test
reads.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Both headers now include <windows.h>, and the test targets that reach them
through vulkan_replay.h (replay_uav_clear_offset_test first) do not define
NOMINMAX, so windows.h's max/min macros broke the std::max/std::min calls in
vulkan_replay.h ("expected unqualified-id") and the runtime's test build
failed. Guard the include the way vulkan_gpu_labels.h already does.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A lazy graphics pipeline (the default) makes its stage words again in build()
through stage_words(), which replayed the B4G4R4A4 quantisation but not the
Location 0 / Index 1 remap. Both now go through rewrite_fragment(), the same at
creation and in a lazy build.
d3d12_translated_draw_test --dual-source draws ps_m15_dual (ps_m15 plus a
constant SV_Target1) with ONE + SRC1_COLOR / SRC1_ALPHA, lazy and at creation:
every covered pixel is exact. Without the lazy fix, NVIDIA still draws the
right colours but LEONIDA_VK_VALIDATION=1 reports 3 errors.
Found by kawaii-shakal's review of #1703.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>