Skip to content

runtime: track title host memory regions and import into Vulkan (RT-TITLE-HOST-REGION) - #1273

Open
madhyn wants to merge 3 commits into
ProjectLeonida:mainfrom
madhyn:exp/title-host-region
Open

madhyn wants to merge 3 commits into
ProjectLeonida:mainfrom
madhyn:exp/title-host-region

Conversation

@madhyn

@madhyn madhyn commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Follow-up to #927 (which added the vk_host_region_import_test probe) for RT-TITLE-HOST-REGION on #14.

Summary

  • runtime/src/title_host_regions.h: Thread-safe (std::mutex) non-overlapping interval table (leonida::TitleHostRegions) mapping [host_va, host_va + size) to {gpu_bda, tag, handle} with $O(\log N)$ find(), resolve_bda(), partial-unmap splitting in unregister_range(), pending_imports(), mark_imported(), take_retired(), and process-wide title_host_regions().
  • runtime/src/vulkan_title_regions.h: Lazy VK_EXT_external_memory_host importer (leonida::VulkanTitleRegions) creating VkBuffers with VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, VirtualQuery committed-page validation on Windows, HOST_WRITE -> SHADER_READ barrier (record_host_write_barrier()), and vkDeviceWaitIdle cleanup of retired regions (collect_retired()).
    • Unlike whole-arena pinning, regions are imported piecewise as allocated/used, avoiding VK_ERROR_OUT_OF_HOST_MEMORY on 16 GB hosts.
  • runtime/src/vulkan_backend.h, runtime/shims/d3d12_vulkan_device.cpp, runtime/shims/xmem.cpp:
    • Registers/unregisters committed ranges in TitleVirtualAlloc, TitleVirtualFree (added to SHIM_TABLE(title_memory)), XMemVirtualAlloc (MEM_COMMIT), map_page, and unmap_page.
    • Configures VulkanContext::title_regions_ when host_pointer_alignment != 0, syncs pending imports and emits the host-write barrier at command-buffer start, and falls back to title_regions_.lookup(...) in replay_.buffer_at and replay_.host_at.
  • Tests:
    • runtime/test/title_host_regions_test.cpp: interval table, partial unmap splitting, retired handle lifecycle, and concurrent access.
    • runtime/test/title_host_region_gpu_test.cpp: end-to-end VulkanContext import, host_at / buffer_at / resolve_title_bda, GPU copy + readback verification of CPU-written bytes, and clean retirement before freeing host pages.

Verification (NVIDIA GeForce RTX 3060 Ti)

  • Linux (ctest): title_host_regions Passed (9.35 s), title_host_region_gpu Passed (0.23 s).
  • Windows cross-build (clang-cl + xwin) under Wine 11.22:
    • title_host_regions_test.exe: PASS
    • title_host_region_gpu_test.exe: ok (align=4096, bda supported)

AI-assisted: yes

@qhukz13

qhukz13 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

A native Windows data point, and some questions from reading the import path.

Run: GeForce GTX 1050 Ti (Pascal, 4 GB, no Resizable BAR), NVIDIA 582.66, Windows 10 Pro 19045, MSVC RelWithDebInfo, this PR at ceed3d36b against main 89d7dfcf5. title_host_regions and title_host_region_gpu pass (ok (align=4096, bda supported)), with and without LEONIDA_VK_VALIDATION=1, and the validation layer reports nothing. d3d12_xmem_placed and xmem pass on both sides; d3d12_existing_heap fails identically on both (the "64 bytes into a page" slot-133 case that #1192 fixes on this card). So no regression in what the tests reach here.

What the tests do not reach, from reading ceed3d36b:

  1. Host pages released before the imported memory. TitleVirtualFree (runtime/shims/xmem.cpp:824-837) calls unregister_range and then VirtualFree at once, and unmap_page (:473) does the same before the view is unmapped; unregister_range only queues the handle, and vkFreeMemory runs later in collect_retired. VK_EXT_external_memory_host requires the host range to stay valid for the lifetime of the VkDeviceMemory imported from it, and a submission recorded before the free may still read it. The slot-133 path in main ties the import to the resource and frees it when the title releases the resource (Resource::~Resource). Would a retire-then-wait before the host free fit here?
  2. Every committed title allocation is imported, page by page for xmem. TitleVirtualAlloc (xmem.cpp:819) registers every MEM_COMMIT, not only kMemGraphics ranges, and VulkanQueue::execute (runtime/src/vulkan_backend.h:2605) imports all pending regions on each submit, so CPU-only heaps get pinned and imported too. map_page (xmem.cpp:500) registers each 64 KiB page as its own region, and VulkanTitleRegions::lookup clamps the size to what is left of that one region (runtime/src/vulkan_title_regions.h:149-150): a buffer that spans two mapped pages resolves to its first 64 KiB, and host_at returns absent for a read that crosses the page (vulkan_backend.h:1035). Neighbouring pages that map contiguously could be merged, or buffer_at could walk them.
  3. One VirtualQuery block per region. import_region (vulkan_title_regions.h:267-275) requires the whole aligned range to sit in the block at its start (mbi_end < aligned_end returns). A TitleVirtualAlloc range that was later re-protected in part, or an XMemVirtualAlloc range made of several views, is then never imported. Resource::create_host_buffer in main walks every block for this reason (its comment: the retail buffer spans three mapped windows).
  4. Failures are silent and retried. Every early return in import_region leaves the region pending with gpu_bda == 0 and logs nothing, and lookup retries the whole sync_pending() on each miss. A log_once per reason, as the slot-133 path has, would make a run on a card that refuses imports diagnosable, and marking a region as failed would stop the retries.
  5. allocationSize = max(reqs.size, aligned_size) (vulkan_title_regions.h:320): if a driver ever reports reqs.size above aligned_size, the import would cover bytes past the checked range. Refusing that case (as create_host_buffer does with requirements.size > bytes) would be safer than growing the import.

Items 2 and 3 matter most for the boot as I read it: streaming lives in mapped xmem pages, so a buffer larger than one page is the normal case there.

No game files. AI-assisted (Claude Code, with Antigravity for the build and test run).

@madhyn

madhyn commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

@qhukz13 Thank you for the native Windows 10 / GTX 1050 Ti run and the thorough review! Addressed all 5 points in 31c072a42:

  1. Retire-before-free (TitleHostRegions::set_retire_hook): VulkanTitleRegions::configure registers a retire hook (collect_retired(true)) invoked right after unregister_range detaches a live handle, so vkDeviceWaitIdle + vkDestroyBuffer + vkFreeMemory finish before TitleVirtualFree / unmap_page releases the host pages.
  2. CPU-only TitleVirtualAlloc: TitleVirtualAlloc now registers only (kMemGraphics | MEM_COMMIT) allocations instead of every MEM_COMMIT, avoiding pinning CPU-only heaps. (Next step if needed for multi-page xmem buffers: coalescing contiguous XMemMapPhysicalPages runs in xmem.cpp or walking adjacent regions in lookup.)
  3. Multi-block VirtualQuery walk: import_region now walks every MEMORY_BASIC_INFORMATION block across [aligned_start, aligned_end) (matching create_host_buffer).
  4. Failure tracking & diagnostics: mark_failed + import_failed stops repeated sync_pending() retries on unimportable ranges, and log_once reports the refusal reason once per failure mode.
  5. reqs.size > aligned_size: import_region now refuses if reqs.size > aligned_size instead of expanding allocationSize.

Re-verified after 31c072a42 on RTX 3060 Ti:

  • Linux ctest: title_host_regions Passed (3.65 s), title_host_region_gpu Passed (0.21 s).
  • Wine 11.22 (clang-cl + xwin): title_host_regions_test.exe PASS, title_host_region_gpu_test.exe ok (align=4096, bda supported).

AI-assisted: yes

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

Thanks, the table and the importer are tidy and the tests pass here. A few things need fixing before this can go in, and one question is for the owner (next comment).

  • Lazily committed pages are lost for good. map_page registers a page before a reserve-only section commits it; the first submission sees it uncommitted, calls mark_failed, and nothing retries. Pages only the GPU or DMA writes are never imported.
  • Two threads can import the same range (the submission path, and a view creation or lookup). The second mark_imported retires the first import while a CBV may already hold its address.
  • The retire hook calls vkDeviceWaitIdle on title threads, from unmap_page under g_phys_mutex and the exclusive fault lock, while other threads may be submitting.
  • One VkDeviceMemory per 64 KiB page reaches maxMemoryAllocationCount (4096 on NVIDIA under Windows) at about 256 MiB of mapped pages.
  • TitleVirtualFree with MEM_RELEASE and size 0 unregisters only the first RegionSize run; imports past it stay alive on freed memory.
  • Sharing mode: the imports are EXCLUSIVE, while the layer creates its own buffers CONCURRENT across queue families.
  • Scope: resolve_shader_va covers views the API creates. Root arguments and descriptors the title writes into its own memory, which is the black-frame path, are untouched.
  • Claim: RT-TITLE-HOST-REGION is roanh47's claim from 13:24 UTC, and I found no handover in #14. Could you sort that out with them there?

Tested on main 507029d1e + 31c072a42: Linux portable ctest on llvmpipe, 77 of 77; the Windows suite under Wine on llvmpipe and an RDNA2 iGPU (RADV), no test from pass to fail; title_host_regions and title_host_region_gpu pass on both, and the iGPU run took the import path.

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

@aslan2142

Copy link
Copy Markdown
Collaborator

@ubiquitarianism this is the first title-memory PR to reach review, and merging it would pick a direction for main, so the question is better put to you directly.

What #1273 commits main to

  • One Vulkan import per title range, each at its own unrelated device address, translated only where the layer creates a view (CreateConstantBufferView, buffer SRV/UAV).
  • A VirtualFree hook and device-wide waits on title threads.
  • No change to the black frame: tables the title writes into its own memory, whose entries are title addresses again, are not reached. docs/d3d12_vulkan.md already says separate buffers cannot satisfy the single heap_base + va the shaders use.

The two approaches that have been shown to run

Two questions

  1. Should main go for a single base over title memory (the whole-region import) or for windows and copies?
  2. Until that is settled, should title-memory PRs land only behind an opt-in switch that is off by default?

Nothing past the black frame can be merged without this, and the stack to the frame loop is going in now (#375 to #391 so far).

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

@QemmHD

QemmHD commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator

Two notes from a real boot on Windows 11 / RTX 4070 SUPER, for when this is rebased:

  1. It no longer applies to main. The head 31c072a is based on e0f1719 (before [integration] Linux/Proton and native Windows: boot through the story load (RT-PROTON-STORY-LOAD) #984). Cherry-picking its 3 commits onto main c7e82e2 conflicts in runtime/shims/xmem.cpp (5 hunks; main replaced map_page/unmap_page with section views and write-watch), d3d12_vulkan_device.cpp (1) and vulkan_backend.h (1). That's a port, not a trivial resolution, so I didn't boot a guessed merge.
  2. A design point from the current first stop (details in Coordinating who works on what (continued from #14) #1608): a pipeline-library compute program writes through a raw 64-bit address that points into the physical-page pool window (the CopyPageMappingsBatchX pool) at its CPU address. That address comes from buffer data, not from a descriptor the layer rebases. Imports that get a driver-assigned buffer device address (vkGetBufferDeviceAddress) and are resolved host→BDA in buffer_at/host_at wouldn't help that shader. It needs the address the title uses to be the address the GPU sees (or a rewrite step).

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

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

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants