Skip to content

vulkan_present: the capture buffer is made of host-cached memory (RT-PRESENT-CAPTURE-CACHED) - #1694

Open
DadaCap wants to merge 1 commit into
ProjectLeonida:mainfrom
DadaCap:present-capture-cached
Open

DadaCap wants to merge 1 commit into
ProjectLeonida:mainfrom
DadaCap:present-capture-cached

Conversation

@DadaCap

@DadaCap DadaCap commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-PRESENT-CAPTURE-CACHED (#1608 (comment))

What and why

VulkanPresenter::make_capture_buffer took the first memory type that is host visible and coherent. On NVIDIA the first such type is uncached (flags 0x6 on this RTX 5080; the cached one, 0xe, comes after it), and a host read of uncached memory is many times slower than of cached memory.

That did not matter much while the buffer was read once per capture. Since the launch splash (#939), finish_startup_frame reads the whole buffer on the CPU for every frame the title presents before its first non-black one, with the submit and tracker locks held. On a large window that scan takes over the boot:

  • one title thread sits in the scan at 97 % CPU (29 of 30 samples of an external sampler, and the same frame in two LEONIDA_STACKS dumps 20 s apart: submit_now -> present -> PresentX);
  • the other submissions starve: device-stall: could not sample the submit lock for 15 s; submitted ticket 424, last observed retired ticket 328;
  • the title's game state stays 2, it never accepts A, and its hang watchdog ends the run at the landing screen.

The change: choose a host-cached, non-device-local type first and fall back to the first host-visible coherent one, exactly as probe_read_source in the same file already does. One function, 11 lines.

Measured

Native Windows 11, RTX 5080 16 GB, NVIDIA 621.24, 64 GB, own 1.0.2.1, MSVC RelWithDebInfo with SDL3. Window client area 3824x1841 when the first frame is presented. Every run: keyboard pad with A every 5 s from 30 s, LEONIDA_SKIP_INT3=1, LEONIDA_PEEK=0x7446510,0x98f828c, an existing save, shaders from main's translator.

tree switches game state 3 A accepted (session d) how the run ends
main 3f81d46 LEONIDA_TITLE_WINDOWS_MB=4096 LEONIDA_ARENA_MB=4096 LEONIDA_DEVICE_FAULT=1 never never title's hang watchdog at 125.8 s, still at the landing screen; fewer than 1,500 presents in the whole run
main + #1558 + #1631 LEONIDA_ARENA_MB=2048 never never watchdog at 107.8 s; a second run was still at the landing screen at its 160 s limit
main + #1558 + #1631, LEONIDA_WINDOW=960x540 (no code change, 14 times fewer texels to scan) LEONIDA_ARENA_MB=2048 95 s 105 s reaches the world phase
main + #1558 + #1631 + this change LEONIDA_ARENA_MB=2048 25 s 35 s reaches the world phase; 1,501 presents by about 22 s
main + this change only (this branch) same as the first row 25 s 35 s story load reads from about 35 s to 90 s, then the world phase; the run ends at 179.8 s on the title's hang watchdog there, which is the stop already described in #1537, not this one

So on this machine the first and last rows differ only by this commit. Before #939 (main 3ff2a2a) the same launcher passed the landing screen every time, which is how I found it.

It probably costs time on smaller windows too, in proportion to the texel count: the report on #939 of 182 to 362 PresentX in 51 to 74 s on an RTX 4070 SUPER is a few presents a second. I have not measured another machine.

Tested

  • The ten presenter tests of runtime/ pass on this branch on the same GPU: xbox_present_readback_size, vk_presenter_capture_colour, present_window, xs_present_lazy, vk_present_dump, vk_present_dump_default, vk_present_dump_invalid, vk_presenter_sync, d3d12_present, xbox_present.
  • The real boot, as in the table. No new test: which memory type a driver lists first is a property of the device, and the effect only shows with a real swapchain and the title presenting black frames.

Not done, not tested

Counters, switch names and addresses of the executable only; no game data. AI-assisted (Claude Code).

🤖 Generated with Claude Code

…PRESENT-CAPTURE-CACHED)

make_capture_buffer took the first host-visible, coherent memory type. On NVIDIA that is an uncached type, and a host
read of it is many times slower than of cached memory. Since the launch splash (ProjectLeonida#939) every frame the title presents
before its first non-black one is scanned on the CPU in finish_startup_frame, with the submit and tracker locks held,
so on a large window the scan starves every other submission and the title's hang watchdog ends the boot at the
landing screen. The cached type is chosen first, the way probe_read_source already does.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@kawaii-shakal kawaii-shakal 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.

Review (read-only, not run). Correct and minimal. The buffer is only read on the CPU, so host-cached system memory is the right first choice. The two-pass search mirrors probe_read_source. Requiring HOST_COHERENT in both passes means no vkInvalidateMappedMemoryRanges is needed before the reads. On a driver whose only cached type is non-coherent, it falls back to the old choice rather than becoming incorrect.

Nit: the same two-pass search now exists twice in vulkan_present.h. A small pick_host_read_type(requirements) helper would keep them from drifting apart.

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

@DadaCap

DadaCap commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

@kawaii-shakal thank you for the review. On the helper: I agree the two searches should not drift apart, and I would do it as a follow-up once this and #1223 are in. #1223 adds its guard directly above the vkGetPhysicalDeviceMemoryProperties_ call in make_capture_buffer, which is the line a shared pick_host_read_type would take out of that function, so folding it in here would put the two pull requests in conflict for a nit. If a maintainer prefers it in this one, I will add it and help rebase #1223. AI-assisted (Claude Code).

@adrianmjim

Copy link
Copy Markdown

Tested 3768be502 applied onto main 42537e7b6 on AMD: Radeon RX 7900 XT, AMD driver 32.0.32029.2010 (26.11.2), no Resizable BAR, Windows 11 build 26300, VS 2022 RelWithDebInfo. No game files.

What the change picks here. In this driver's memory-type list, type 1 is HOST_VISIBLE | HOST_COHERENT in system memory and comes first, so main takes it. Type 3 adds HOST_CACHED in the same heap, and this PR takes it.

I measured CPU reads from each host-visible type with a small standalone Vulkan program. It maps a 3840x2160x4 buffer (33 MB) and memcpys it out 10 times:

type flags heap one 3840x2160x4 read
1 (main) 0x006 coherent, uncached system 114 ms (291 MB/s)
3 (this PR) 0x00e coherent, cached system 1.5 ms (22,040 MB/s)
2 0x007 device-local (BAR window) BAR 114 ms
6, 10, 14 0x0c7 / 0x007 device-local (BAR window) BAR 114 ms
5, 9, 13 0x0c6 / 0x006 uncached variants system 114 ms
7, 11, 15 0x0ce / 0x00e cached variants system 1.4–1.6 ms

(0x0c6/0x0c7/0x0ce add AMD's device-coherent/uncached bits. They are allocated here without enabling deviceCoherentMemory, for reference only. Neither main nor this PR picks them.)

So the order is the same as on your RTX 5080 (uncached 0x6 first, cached 0xe later), and on AMD the uncached type is about 75 times slower to read. That is the raw read speed of each memory type, measured outside leonida, not the presenter's scan itself, which the description says was not timed. From it, a full scan of a 4K capture should take about 114 ms on main and about 2 ms with this PR on this card. It scales with the window's texel count. I did not time the scan itself or boot the title on AMD, so this is an estimate from the read speed.

Tests, main and this PR:

  • xbox_present, xbox_present_readback_size, vk_presenter_capture_colour, present_window, xs_present_lazy, vk_present_dump*, vk_presenter_sync, d3d12_present and package_splash: 11 of 11 pass on both.
  • xbox_present, vk_present_dump and vk_presenter_sync with --repeat until-fail:10: no failure on either. xbox_present reuses one capture buffer while the colour changes between frames, so a stale cached read would show up there.
  • Whole ctest -j 6: 399 of 402 with this PR, with the same three failures as main 42537e7b6 (d3d12_texture_tiles, d3d12_clear, d3d12_geometry_views), none of them presenter tests.

I didn't run a retail boot.

@DadaCap

DadaCap commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, that is a useful second vendor: the same memory-type order and a 75 times slower uncached read on the RX 7900 XT. I agree the 114 ms per 4K capture on main is an estimate from the read speed, not a timed scan. AI-assisted (Claude Code).

@OreoFR

OreoFR commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Merge test for the #2464 campaign on an 8 GB laptop (profile M), done for the author. Main ec91d69 was merged without conflict, in one tree together with #1705, #1694 and #1284, which touch different code. The numbers were recomputed from the raw logs. AI-assisted (Claude Code, Claude Opus 5.5).

RUN dada3 | 2026-11-27 | OreoFR | AI-assisted: Claude Code (Claude Opus 5.5)
BASE:        main ec91d695c + #1705 (abe3f2042) + #1694 (3768be502) + #1284 (7358f3261), merged cleanly | shaders: main translation + #2447 (one-lane) pack
MACHINE:     RTX 4070 Laptop 8 GB, laptop (profile M) | NVIDIA 621.40 | Windows 11 | 40 GB
RECIPE:      documented + DEVIATIONS: ASYNC_PIPELINES=1 ASYNC_MIN_BYTES=1048576 VALUE_WAIT_TIMEOUT_MS=30000 MAP_READBACK=1 DEVICE_TYPE=6 ARENA_MB=4095 DEVICE_FAULT=1
OS/DRIVER:   TdrDelay 60
TARGET:      regression check against regA (main ec91d695c, same recipe and caches) | PREDICTED: world reached, no loss
MEASURED:    world entry ~237 s (regA 223 s) | 0 device lost, 0 exceptions (regA 0 / 0) | gpu-stall lines 2 (regA 10)
END:         stopped by our script 56 s after world entry (t=293 s): the known display stop after world entry is the same on main
CACHES:      warm | RUN 1 of 1
VERDICT:     no regression seen (n=1)
  • No test in this PR. The run above covers it: present and capture paths, with no exception and no device lost.

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