d3d12: the every-byte hash of a mapped title texture reads write-combined memory through a streaming copy (RT-TITLE-HASH-WC-STREAM) #1705

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

@⁠DadaCap on GitHub · 2026-11-25 08:49 UTC · original
DadaCap:title-hash-streaming → main · +61 −1 · 2 files · 1 commits · open

Claimed in the coordination issue as: RT-TITLE-HASH-WC-STREAM (#1608)

What and why

refresh_title_textures hashes every byte of a mapped title texture of up to 4 MiB each sixth pass (every_byte()), with ordinary loads straight from the title's pages. In a real boot those pages are write-combined, and ordinary loads from write-combined memory are not cached; copy_guarded in the same file says so and reads them with MOVNTDQA for that reason. The hash did not.

Measured on the intro (a temporary timer around hash_guarded, not in this PR): one every-byte hash takes 36 ms on average and up to 78 ms over 4,096 KiB, so about every sixth pass spends one to two frames of a title that paces itself for 30 Hz. The title then presents 24 frames a second, the launch movie's sound stutters and its picture jumps ahead and back.

The change: when the texture's memory is write-combined, the every-byte hash reads it through copy_guarded's streaming copy into the scratch buffer the fill of the same texture already uses, and hashes the copy. Ordinary pages and the sampled hash are untouched. 12 lines.

Measured

Native Windows 11, RTX 5080 16 GB, NVIDIA 621.24, 64 GB, own 1.0.2.1, MSVC RelWithDebInfo with SDL3. Tree for both columns: main 3f81d46ac + #1558 + #1631 + #1694 (without #1694 this machine does not get past the landing screen). LEONIDA_ARENA_MB=2048 LEONIDA_FRAME_PACING=1 LEONIDA_PRESENT_NO_TEAR=1, keyboard pad with A every 5 s from 30 s, an existing save.

before with this change
title texture hash time printed by the runtime, intro and landing screen 15.3, 13.2, 15.2 ms/pass 3.5, 3.4, 3.3 ms/pass
every-byte hashes in the first 10 s window (timer) 60, 36.3 ms each, longest 65.3 ms 70, 1.1 ms each, longest 3.0 ms
passes that spend over 33 ms hashing, per 10 s window (timer) 30 of 182, 34 of 210, 40 of 244, 41 of 248 0 of 211, 0 of 257, 0 of 306, 0 of 309
presents from 8 s to 30 s (a capture every 30 presents) 510, 23.2 a second 630 and 600 in two runs, 28.6 and 27.3 a second
presents from 8 s to 60 s 1,260, 24.2 a second 1,530 and 1,470, 29.4 and 28.3 a second
the launch movie, watched by a person sound stutters, the picture jumps ahead and back plays cleanly

In the "before" window all 1,151 hashes were of write-combined memory. The two mapped textures there are 2048x2048 and 1024x1024, format 65.

Tested

  • New case in d3d12_existing_heap_test: a mapped 1024x512 R8 plane (above 256 KiB, so hashed by samples, and under 4 MiB, so hashed in full each sixth pass) in ordinary pages and in write-combined ones. One byte is changed 1,000 bytes into a page, where the sampled hash does not look, and must reach the GPU within 14 submissions. Both pass.
  • Negative check: with the every-byte hash switched off, both new cases fail at changed == 1.
  • d3d12_existing_heap, d3d12_existing_heap_no_write_watch, d3d12_existing_heap_write_watch pass on the same GPU.
  • The real boot, two runs with the change, as in the table; the second ran through the story load to the world phase and ended there on the title's hang watchdog at 146.8 s, the stop described in #1537.

Not done, not tested

  • The sampled hash still reads write-combined memory in place: about 0.5 ms a texture here, 3.3 to 3.5 ms a pass in total. I left it; it is no longer a frame.
  • AMD, Intel, Linux: not run. copy_guarded's streaming path is the existing one.
  • I did not look at whether the title's pages could be read from a cached view instead; that would be a different change.

Counters, switch names and timings only; no game data. AI-assisted (Claude Code).

🤖 Generated with Claude Code

**@⁠DadaCap** on GitHub · 2026-11-25 08:49 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1705) `DadaCap:title-hash-streaming` → `main` · +61 −1 · 2 files · 1 commits · open Claimed in the coordination issue as: RT-TITLE-HASH-WC-STREAM (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) ### What and why `refresh_title_textures` hashes every byte of a mapped title texture of up to 4 MiB each sixth pass (`every_byte()`), with ordinary loads straight from the title's pages. In a real boot those pages are write-combined, and ordinary loads from write-combined memory are not cached; `copy_guarded` in the same file says so and reads them with MOVNTDQA for that reason. The hash did not. Measured on the intro (a temporary timer around `hash_guarded`, not in this PR): one every-byte hash takes **36 ms on average and up to 78 ms** over 4,096 KiB, so about every sixth pass spends one to two frames of a title that paces itself for 30 Hz. The title then presents 24 frames a second, the launch movie's sound stutters and its picture jumps ahead and back. The change: when the texture's memory is write-combined, the every-byte hash reads it through `copy_guarded`'s streaming copy into the scratch buffer the fill of the same texture already uses, and hashes the copy. Ordinary pages and the sampled hash are untouched. 12 lines. ### Measured Native Windows 11, RTX 5080 16 GB, NVIDIA 621.24, 64 GB, own 1.0.2.1, MSVC RelWithDebInfo with SDL3. Tree for both columns: `main` 3f81d46ac + #1558 + #1631 + #1694 (without #1694 this machine does not get past the landing screen). `LEONIDA_ARENA_MB=2048 LEONIDA_FRAME_PACING=1 LEONIDA_PRESENT_NO_TEAR=1`, keyboard pad with A every 5 s from 30 s, an existing save. | | before | with this change | |---|---|---| | `title texture hash time` printed by the runtime, intro and landing screen | 15.3, 13.2, 15.2 ms/pass | 3.5, 3.4, 3.3 ms/pass | | every-byte hashes in the first 10 s window (timer) | 60, 36.3 ms each, longest 65.3 ms | 70, 1.1 ms each, longest 3.0 ms | | passes that spend over 33 ms hashing, per 10 s window (timer) | 30 of 182, 34 of 210, 40 of 244, 41 of 248 | 0 of 211, 0 of 257, 0 of 306, 0 of 309 | | presents from 8 s to 30 s (a capture every 30 presents) | 510, 23.2 a second | 630 and 600 in two runs, 28.6 and 27.3 a second | | presents from 8 s to 60 s | 1,260, 24.2 a second | 1,530 and 1,470, 29.4 and 28.3 a second | | the launch movie, watched by a person | sound stutters, the picture jumps ahead and back | plays cleanly | In the "before" window all 1,151 hashes were of write-combined memory. The two mapped textures there are 2048x2048 and 1024x1024, format 65. ### Tested - New case in `d3d12_existing_heap_test`: a mapped 1024x512 R8 plane (above 256 KiB, so hashed by samples, and under 4 MiB, so hashed in full each sixth pass) in ordinary pages and in write-combined ones. One byte is changed 1,000 bytes into a page, where the sampled hash does not look, and must reach the GPU within 14 submissions. Both pass. - Negative check: with the every-byte hash switched off, both new cases fail at `changed == 1`. - `d3d12_existing_heap`, `d3d12_existing_heap_no_write_watch`, `d3d12_existing_heap_write_watch` pass on the same GPU. - The real boot, two runs with the change, as in the table; the second ran through the story load to the world phase and ended there on the title's hang watchdog at 146.8 s, the stop described in #1537. ### Not done, not tested - The sampled hash still reads write-combined memory in place: about 0.5 ms a texture here, 3.3 to 3.5 ms a pass in total. I left it; it is no longer a frame. - AMD, Intel, Linux: not run. `copy_guarded`'s streaming path is the existing one. - I did not look at whether the title's pages could be read from a cached view instead; that would be a different change. Counters, switch names and timings only; no game data. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
refresh_title_textures hashes every byte of a mapped texture of up to 4 MiB each sixth pass, straight from the
title's pages. In a real boot those pages are write-combined, where ordinary loads are not cached: on the intro one
such hash took 36 ms on average and up to 78 ms, so every sixth pass cost one to two frames and the title presented
24 frames a second where it paces itself for 30. The bytes now come through copy_guarded's streaming copy, as the
fill of the same texture already does, and the copy is hashed: 0.4 to 1.1 ms.

The test places a mapped plane in ordinary and in write-combined pages and changes one byte the sampled hash does not
cover.

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:42 +00:00
Author
Owner

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

Review (read-only, not run). The measurements make the case well (0 of ~300 passes over 33 ms afterwards).

  • Reusing title_tiled_ is safe: the fill path (d3d12_vulkan_device.cpp ~11047) always copies the texture into it again before it uses it, and both run under title_texture_mutex.
  • The new test's negative check, failing with the every-byte hash off, shows the case is real.

One possible follow-up, not for this PR: when the every-byte hash finds a change, the fill streams the same 4 MiB into the same buffer again. Passing a flag that the scratch already holds this pass's bytes would save the second copy on the passes that upload.

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/1705#pullrequestreview-6055210741) · **reviewed** Review (read-only, not run). The measurements make the case well (0 of ~300 passes over 33 ms afterwards). - Reusing `title_tiled_` is safe: the fill path (`d3d12_vulkan_device.cpp` ~11047) always copies the texture into it again before it uses it, and both run under `title_texture_mutex`. - The new test's negative check, failing with the every-byte hash off, shows the case is real. One possible follow-up, not for this PR: when the every-byte hash finds a change, the fill streams the same 4 MiB into the same buffer again. Passing a flag that the scratch already holds this pass's bytes would save the second copy on the passes that upload. 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, and for checking the title_tiled_ reuse against the fill path. The second copy on the passes that upload is a fair point: at 4 to 5 GiB/s it is about 1 ms for a 4 MiB texture, on at most one pass in six, so I left it out to keep this change to the hash alone. I am not claiming that follow-up; it is free for whoever wants it. AI-assisted (Claude Code).

**@⁠DadaCap** on GitHub · 2026-11-25 10:34 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1705#issuecomment-7159873030) @⁠kawaii-shakal thank you, and for checking the `title_tiled_` reuse against the fill path. The second copy on the passes that upload is a fair point: at 4 to 5 GiB/s it is about 1 ms for a 4 MiB texture, on at most one pass in six, so I left it out to keep this change to the hash alone. I am not claiming that follow-up; it is free for whoever wants it. 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/1705/head:github-bot/gh-1705'
git switch 'github-bot/gh-1705'

Merge

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