Skip to content

d3d12,xmem: sync TitleTexture readability against title_memory_map between passes (RT-TITLE-TEXTURE-MAP-GEN) - #2240

Merged
aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
zmasi:fix/title-texture-map-generation
Nov 26, 2026
Merged

aslan2142 merged 1 commit into
ProjectLeonida:mainfrom
zmasi:fix/title-texture-map-generation

Conversation

@zmasi

@zmasi zmasi commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-TITLE-TEXTURE-MAP-GEN (#1608 (comment))

What and why

Device::title_texture_readable (runtime/shims/d3d12_vulkan_device.cpp) cached positive readability (generation = 0, cache_success = true) and relied solely on refresh_title_textures() (throttled to LEONIDA_TITLE_TEXTURE_PERIOD_MS = 16 ms ahead of ExecuteCommandLists) to drain leonida::title_memory_map::changes_since and call t.memory.retry().

When CopyPageMappingsBatchX or XMemMapPhysicalPages unmaps or maps 64 KiB tiles (title_memory_map::changed) and a title thread calls synchronous Slot-70 write_placed_texture_view (or the async fill worker runs) before the next 16 ms pass (!title_pass_):

  1. Unmap direction: title_texture_readable returned stale true without probing VirtualQuery, faulting inside hash_bytes (:239) while holding title_texture_mutex.
  2. Map direction: A texture previously marked pending() (cache_failure = true) returned stale false without re-probing until the next pass, delaying bindless publication of newly mapped tiles.
  3. Host guarded-read classification & no-builtin hardening: xmem::physical_page_fault (runtime/shims/xmem.cpp:1675) recorded read faults (access == 0) over unmapped window pages as FaultExit::left_to_title without checking title_code(pc), and mix_words / hash_bytes lacked LEONIDA_GUARDED_NO_BUILTIN (and called std::memcpy inside a __try callee).

Changes:

  • Track map_generation_ in TextureReadability::sync_map(pass_generation, cpu, size) (runtime/src/title_texture_memory.h), called from title_texture_readable when !title_pass_ && guard && !mapped. Unchanged generations cost one O(1) atomic load; non-overlapping map changes advance map_generation_ with 0 extra VirtualQuery probes; overlapping unmaps defer immediately (0 first-chance AVs) and overlapping maps wake pending textures immediately.
  • Add LEONIDA_GUARDED_NO_BUILTIN and a packed UnalignedWord64 load to mix_words / hash_bytes.
  • Check from_title (title_code(pc)) on access == 0 in xmem::physical_page_fault so host guarded reads exit via FaultExit::passed_guarded without false left_to_title counts, while title-code reads and all write faults retain left_to_title.

Tested

  • ctest --test-dir runtime/build -C RelWithDebInfo --output-on-failure:
    • title_memory_map: verifies sync_map (0 probes on non-overlapping change, immediate defer on overlapping unmap, immediate wake on overlapping map).
    • d3d12_existing_heap, d3d12_existing_heap_no_write_watch, d3d12_existing_heap_write_watch: new check_placed_view_map_changes case on both VK_EXT_host_image_copy and staged devices verifies 0 first-chance AVs via AddVectoredExceptionHandler after interior MEM_DECOMMIT + title_memory_map::changed on a 512 KiB (> 256 KiB sampled-hash) placed texture, and immediate bindless publication after MEM_COMMIT + title_memory_map::changed without ExecuteCommandLists.
    • xmem, xmem_pool_zero_read, xmem_no_write_watch, xmem_write_watch: new non-title executable-stub read test verifies FaultExit::passed_guarded increments with 0 left_to_title increments while title-image reads still increment left_to_title.
    • Full Windows CTest suite: 460 / 463 runnable tests pass (0 regressions vs main).
  • python -I tests/test_text_write_line_endings.py & python -I scripts/check_no_game_data.py across all 7 touched files (exit 0).

GPUs tested: NVIDIA GeForce RTX 3090, driver 610.88 (Vulkan 1.4.341), Windows 11 x64

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

Between periodic refresh_title_textures passes, synchronous slot-70 view creation and the async fill worker checked TextureReadability with a constant generation, missing title_memory_map range changes since the last pass. Track the map generation on TextureReadability and consult title_memory_map::changes_since outside refresh passes so unmapped or decommitted pages defer before hashing and newly mapped pages publish immediately without waiting for a submission.

Also mark mix_words and hash_bytes LEONIDA_GUARDED_NO_BUILTIN using an unaligned 64-bit load so clang-cl cannot emit CRT memcpy calls inside the __try frame, and classify non-title read faults over unmapped title pages in xmem::physical_page_fault as FaultExit::passed_guarded.

@lixonik lixonik 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.

Built this head with MSVC on Windows 11. title_memory_map, all three d3d12_existing_heap variants, and the xmem_pool_zero_read and write-watch variants passed. GPU: NVIDIA GeForce RTX 5070, driver 610.88. The first default xmem run failed only the memory-charge assertion for touching 512 pages. Two subsequent default runs passed at this head, and two passed at the base with the same toolchain. The new host/title read-classification assertions passed even in the first run. I could not reproduce a failure in the changed behavior.

AI-assisted.

@Tyrannys

Copy link
Copy Markdown
Contributor

Overlap note: #2267 (RT-TITLE-TEXTURE-SHOW-AFTER-UPLOAD) also touches the TitleTexture path. The logic is separate (it changes Resource::create_texture and the fill worker, not title_texture_readable), but both PRs add a case to runtime/test/d3d12_existing_heap_test.cpp and its main(), so whichever lands second may need a small merge there. AI-assisted (Claude Code).

@Tyrannys

Copy link
Copy Markdown
Contributor

Independent verification of head d088ea79b8394c04a46ec19efb2922a7cd3faae3, with current main (a73a57e13) merged in, no conflicts.

Windows 11 Pro, RTX 4070 SUPER (NVIDIA 621.01), MSVC 19.52.37029, RelWithDebInfo.

  • With the PR: d3d12_existing_heap, d3d12_existing_heap_no_write_watch, d3d12_existing_heap_write_watch, title_memory_map, xmem, plus neighbours xmem_pool_zero_read, xmem_wine_commit, xmem_no_write_watch, xmem_write_watch: 9/9 pass.
  • The PR's tests against main's runtime: the three d3d12_existing_heap* tests fail at slot-70 view map changes line 1437: g_view_avs.load(std::memory_order_relaxed) == 0, so the new case catches the bug. The new xmem case passes on main too, and title_memory_map_test does not compile there (it calls the new sync_map).
  • Full suite with the PR: 475/477, the same as plain main on this machine (xbox_present and d3d12_set_descriptor_heaps fail on main too).

Diff read, one question: in physical_page_fault, a read fault on a title page that is not_ours and not from title code now exits as passed_guarded, which is not noted. That is right for the D3D12 layer's guarded reads, but a host read of an unmapped title page outside any guard now also leaves no note. If that is intended, a line in the FaultExit comment saying so would help.

No game files used. AI-assisted (Claude Code).

@aslan2142

Copy link
Copy Markdown
Collaborator

Thank you, @zmasi. This went into main through #2350 (RT-LINUX-AMD-WORLD), merged at your head d088ea79b with your commits and authorship kept, although it was still marked draft. It was part of the build that first drew the world on Linux with an AMD GPU. GitHub shows this PR as merged for that reason, so nothing needs closing.

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2)

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