Skip to content

P-4: Reduce texture refresh waits during remapping - #34

Draft
Ondra97 wants to merge 3 commits into
ProjectLeonidaL:mainfrom
Ondra97:codex/p4-texture-remap-lock
Draft

Ondra97 wants to merge 3 commits into
ProjectLeonidaL:mainfrom
Ondra97:codex/p4-texture-remap-lock

Conversation

@Ondra97

@Ondra97 Ondra97 commented Nov 26, 2026 •

Copy link
Copy Markdown

What changed

P-4 experiment. LEONIDA_TEXTURE_RANGE_LOCK=1 lets a texture refresh read in place while unrelated title memory is remapped. Overlapping reads still wait. The option is off by default, and P-4 remains open.

This follows the request to measure the existing no-wait switch. The tested branch is Ondra97:codex/p4-texture-remap-lock at 76d48cf, based on f4c0707.

How it was tested

  • Unattended runs with scripts\measure-run.ps1.
  • cargo test --workspace --locked on the rebased branch. 613 passed, 17 ignored.
  • cargo clippy --workspace --all-targets --locked -- -D warnings
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
  • cargo xtask policy

Playtest

GPU and driver RTX 4070 Ti SUPER, NVIDIA 621.40
CPU and RAM Ryzen 9 7900X, 32 GiB
Windows Windows 11 Pro 26H2, build 26300.10075
Console profile Series X
Where Opening 3D scene
Duration Three 330-second runs and two 190-second visual repeats

The 330-second control and existing-switch runs used e8aa615. The range-lock run used the patch on the same runtime base. Each started from the same save and a separate copy of the warmed shader and pipeline caches.

Switch added to base Median FPS Median frame window (ms) Mapping wait (ms/frame) Deferred refreshes/frame
None 0.2 4034.3 2995.4 0
LEONIDA_TEXTURE_NO_WAIT=1 2.2 449.4 0 16.2
LEONIDA_TEXTURE_RANGE_LOCK=1 3.2 307.8 3.8 0

FPS and frame times are medians of the script's windows after 170 seconds. Mapping wait and deferred refreshes use the windows with at least 500 draws per frame. The variants advanced through the opening at different rates.

  • Same gameplay view before and after. The automated opening did not yield a repeatable camera view.
  • All five runs reached the opening scene without a crash report or device loss.
  • Picture parity. Three early range-lock samples had near-black centers while hundreds of pipelines were pending. In the shorter reverse-order repeats with a fuller shared cache, the range lock had visible output in all eight samples and the existing switch had one near-black sample. FPS in these shorter runs was 1.1 with range locking and 4.0 with the existing switch. The cause of the near-black samples remains unresolved.

Handoff

Check out the published draft. From an upstream clone, run gh pr checkout 34 --repo ProjectLeonidaL/leonida. The reference benchmark and local validation are pinned above. Upstream main was at a141614 at handoff and includes #48, which moves graphics profile handling into the runtime. Recheck profile selection and rerun validation on any rebased head before comparing figures.

Reproduce the comparison on the pinned revisions. Use separate worktrees for e8aa615 and 76d48cf. In each, configure a local game directory via LEONIDA_GAME_DIR or leonida.toml and build on Windows.

cargo build --profile diag -p leonida --features diagnostics --locked
.\target\diag\leonida.exe run --console series-x

The second command binds without running the title. Warm the shader cache with translate --warm all after translator changes and prime the pipeline cache with an untimed run. For each timed run, set LEONIDA_DATA_DIR and LEONIDA_CACHE_DIR to separate copies of the same starting save and warmed cache, outside the checkout. Clear LEONIDA_KEEP_WINDOW. Add these opt-in entries to the ignored gpu-profiles.local.json used by the pinned scripts.

{
  "profiles": [
    {"name": "p4-no-wait", "opt_in": true, "env": {"LEONIDA_TEXTURE_NO_WAIT": "1"}},
    {"name": "p4-range-lock", "opt_in": true, "env": {"LEONIDA_TEXTURE_RANGE_LOCK": "1"}}
  ]
}

Run the first two commands from the control worktree and the third from the draft worktree. Set the same report root in both PowerShell sessions.

$out = Join-Path $env:LOCALAPPDATA 'leonida/runs/p4-handoff'
$shots = '20,30,40,60,80,100,120,600,1000'
& .\scripts\measure-run.ps1 -Root $out -Label base -Seconds 330 -From 170 -ShotAfter $shots
& .\scripts\measure-run.ps1 -Root $out -Label no-wait -Profile p4-no-wait -Seconds 330 -From 170 -ShotAfter $shots
& .\scripts\measure-run.ps1 -Root $out -Label range-lock -Profile p4-range-lock -Seconds 330 -From 170 -ShotAfter $shots

Read each summary.txt and check the actual run data directory for growth in crashes.log when using LEONIDA_DATA_DIR. Compare the same camera view from a checkpoint, repeat in reverse order, and record whether textures converge and whether mapping waits and deferred refreshes change. Keep captures, logs, saves and caches private. Share numeric results and visual observations in words.

Preserve the locking contract. xmem::remapping_views includes the complete footprint of any view Windows temporarily removes. Hold the writer guard through unmapping, replacement and the remap notifications. Texture readers take only the locks for their address spans, in table order. Readers that can follow aliases keep the global guard. The regression tests cover disjoint reads, overlaps, wraparound and the fringes of a split view.

Open work. Compare both opt-ins in the same steady gameplay view and check the intermittent dark samples before changing a default. P-4 also includes the kernel cost of the remaps themselves.

AI

  • AI-assisted implementation, synthetic tests, benchmark analysis and handoff.

@ShinniUwU

Copy link
Copy Markdown
Contributor

Thanks for claiming P-4. Before you write a narrower lock, try measuring with a switch we already have for this exact wait:

LEONIDA_TEXTURE_NO_WAIT=1 (off by default; it's read in no_wait() in crates/leonida-gpu/src/texture.rs)

  • With it on, a texture refresh asks for the mapping lock without waiting. If a remap holds the lock, the refresh reads the texture through the kernel instead, which needs no lock.
  • If a page isn't there, that read fails. The texture is then left as it is and read again at its next refresh, one frame later. It never shows a texture that's only partly read.
  • The cost is that a kernel read takes about 0.6 ms per MB, against 0.3 ms for an in-place read.

What we've measured so far:

  • RTX 4070, loading phase (docs/fork-notes.md): frames over a second dropped from 18 to 5.
  • RTX 3080, steady world after loading, one run each: usual frame 126 ms without it and 125.5 ms with it, so no change. That's expected, since the remaps happen while the game streams.
  • Not ruled out yet: textures went black on the 3080 with card windows on plus this switch. A later run with card windows on and this switch off still lost textures, so this switch wasn't the only cause. It's worth watching for black textures when you test it.

Your opening-scene baseline (1,038 ms a frame waiting for the lock) is the case this switch was written for. If it removes most of that wait, P-4 may only need it measured and turned on, maybe with a narrower lock as a follow-up. You're right that a remap can unmap a whole view, so any narrower lock has to cover the view's full span.

To compare without playing by hand, put the switch in an opt-in profile in gpu-profiles.local.json and run scripts/measure-run.ps1 once without -Profile and once with -Profile <name> (see "Picture and feel: unattended runs" in CLAUDE.md).

Also, #35 has merged and added to the top of the "Newest" section of fork-notes.md, so your branch will need a small rebase.

@ShinniUwU

Copy link
Copy Markdown
Contributor

Heads-up: this now conflicts with main, since #35, #38 and #43 all added to the top of the "Newest" section of docs/fork-notes.md. A rebase when you push the code should be all it needs.

@Ondra97
Ondra97 force-pushed the codex/p4-texture-remap-lock branch from 8c62950 to 7bbfff4 Compare November 26, 2026 19:22
@Ondra97
Ondra97 force-pushed the codex/p4-texture-remap-lock branch from 7bbfff4 to 76d48cf Compare November 26, 2026 20:05
@Ondra97

Ondra97 commented Nov 26, 2026

Copy link
Copy Markdown
Author

This session is nearly out of its Codex usage allowance, so I can’t continue working on P-4. Could someone take over this draft and finish the remaining validation?

The implementation is in commit 76d48cf. The PR description has the benchmark results and reproduction steps. The next step is to check profile selection on current main and compare frame times and texture rendering from the same gameplay checkpoint and camera view.

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.

2 participants