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

Open
github-bot wants to merge 1 commit from github-bot/gh-1694 into main AGit
Owner

@⁠DadaCap on GitHub · 2026-11-25 08:30 UTC · original
DadaCap:present-capture-cached → main · +11 −4 · 1 files · 1 commits · open

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

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 3f81d46ac 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 3ff2a2adc) 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

  • The scan itself still runs under the submit and tracker locks; this only makes it fast. Moving it out of the locks, or ending it earlier, is a separate change I am not claiming.
  • AMD, Intel, Linux: not run. On a device whose first host-visible coherent type is already cached, nothing changes.
  • I did not time the scan in isolation; the evidence is the thread samples and the before/after boots.
  • #1223 adds a guard in the same function, a few lines above; the two do not overlap.

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

🤖 Generated with Claude Code

**@⁠DadaCap** on GitHub · 2026-11-25 08:30 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1694) `DadaCap:present-capture-cached` → `main` · +11 −4 · 1 files · 1 commits · open Claimed in the coordination issue as: RT-PRESENT-CAPTURE-CACHED (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### 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` 3f81d46ac | `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` 3ff2a2adc) 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 - The scan itself still runs under the submit and tracker locks; this only makes it fast. Moving it out of the locks, or ending it earlier, is a separate change I am not claiming. - AMD, Intel, Linux: not run. On a device whose first host-visible coherent type is already cached, nothing changes. - I did not time the scan in isolation; the evidence is the thread samples and the before/after boots. - #1223 adds a guard in the same function, a few lines above; the two do not overlap. Counters, switch names and addresses of the executable only; no game data. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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 (#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 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:01:09 +00:00
Author
Owner

@⁠kawaii-shakal on GitHub · 2026-11-25 09:45 UTC · original · reviewed

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).

**@⁠kawaii-shakal** on GitHub · 2026-11-25 09:45 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1694#pullrequestreview-6055211126) · **reviewed** 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).
Author
Owner

@⁠DadaCap on GitHub · 2026-11-25 10:34 UTC · original

@⁠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).

**@⁠DadaCap** on GitHub · 2026-11-25 10:34 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1694#issuecomment-7159872484) @⁠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).
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-25 11:24 UTC · original

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.

**@⁠adrianmjim** on GitHub · 2026-11-25 11:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1694#issuecomment-7161007748) 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 `memcpy`s 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.
Author
Owner

@⁠DadaCap on GitHub · 2026-11-25 12:31 UTC · original

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).

**@⁠DadaCap** on GitHub · 2026-11-25 12:31 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1694#issuecomment-7162485820) 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).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1694/head:github-bot/gh-1694'
git switch 'github-bot/gh-1694'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1694'
git switch 'github-bot/gh-1694'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1694'
git switch 'github-bot/gh-1694'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1694'
git switch 'main'
git merge --squash 'github-bot/gh-1694'
git switch 'main'
git merge --ff-only 'github-bot/gh-1694'
git switch 'main'
git merge 'github-bot/gh-1694'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.