Skip to content

Scope title memory mapping locks to affected ranges - #175

Closed
Kamiloj9 wants to merge 1 commit into
fxceless:mainfrom
Kamiloj9:codex/range-mapped-texture-reads
Closed

Kamiloj9 wants to merge 1 commit into
fxceless:mainfrom
Kamiloj9:codex/range-mapped-texture-reads

Conversation

@Kamiloj9

Copy link
Copy Markdown

Summary

  • Let in-place texture reads overlap remappings of disjoint virtual-address ranges.
  • Protect each full old view that a mapping change temporarily tears down or repairs. Keep global exclusion for unknown extents and preserve the nonblocking texture-read path.
  • Keep overlapping queued writers ahead of new readers. Cover lock ordering, range boundaries, pending writers, nonblocking acquisition and Windows remap behavior in targeted tests.

Verification

  • All 17 targeted watch and xmem tests passed on the PR base. The integrated checkout passed 19 targeted tests, Clippy, a diagnostic build and the asset-policy check.
  • A 180-second RTX 4080 SUPER checkpoint replay reached 1,977 successful presents without a logged device loss.
  • In the first sampled 3D window, median reported texture mapping-lock wait fell from 671.1 ms to 0.0 ms and median present interval fell from 1,093 ms to 624 ms. In a later window the candidate took 828 ms versus 502 ms in the baseline even though reported lock waits remained near zero. Relative frame windows can contain different shots and streaming activity, so these results establish reduced lock contention and leave sustained frame-rate impact open.

@rostaddotcc

Copy link
Copy Markdown

Independent confirmation of the diagnosis, from a different ground (Linux/Proton, RX 7900 XT, RADV, fork 4e17478, diag build, three 3-minute runs in the streaming phase).

The counters you are moving are the ones I measured before touching anything: texture refreshes per frame, all threads splits into waiting for the texture's own lock, asking the write watch and the guards, and waiting for title memory to hold still — and the third is the whole of it: median 20.2 ms, max 4 076 ms, while asking was 7.3 ms and the texture's own lock 0.

Your 671 ms median to 0.0 ms matches my direction (mine: 20.2 ms to 0.0 ms with VICTORY_TEXTURE_NO_WAIT=1, which is the blunt version of what you did — try_read, and read through the kernel while the runtime holds the lock). Yours is the better fix, because it keeps the in-place read at 0.3 ms a megabyte instead of the kernel's 0.6, and it does not defer anything: with the switch, 3–7 refreshes a frame went through the kernel and 0–2 were left for the next frame, so my version pays a small lag that yours should not.

One thing that may be useful to you: I first assumed the wait was a convoy of our making — every refresh holding the read guard through its blake3 hashing and detiling, so the runtime's remap waited for us and then we waited for it — and measured it. Copying the texture out under the guard and dropping it before blake3/detile changes nothing (median 34.0 ms, max 5 221 ms, i.e. slightly worse). So the hold is the writer's, one remap under one global lock, which is exactly what scoping to ranges fixes.

A question on the design, since you are the one who knows: for a mapping change whose extent cannot be determined (the "unknown extents" case you keep global exclusion for), how often does that happen in practice, and is the nonblocking read path still reachable then? In our runs the deferral rate with try_read was 0–2 textures a frame, so a nonblocking fallback for that case seems cheap to keep.

@rostaddotcc

Copy link
Copy Markdown

Verified on the other ground this fork is used on: Linux/Proton (CachyOS, RX 7900 XT, Mesa 26.3.1 RADV, vkd3d-proton 3.2.0, Proton Experimental experimental-11.0-20261112), diag build of 8e0e7b6, 430-second run, d3d12 served by vkd3d-proton. The mapping-lock wait is 0.0 ms in all 113 sampled windows, and the run went deeper into streaming than the control: 0.2–0.3 fps, 3 478–4 223 ms a frame.

Same counter from texture refreshes per frame, all threads, five runs on this box (medians over the windows each run sampled, so the phase differs between rows):

build windows texture lane, median max asking, median wait, median wait, max
4e17478 (first baseline) 103 34.3 ms 4 112 ms 7.3 ms 20.2 ms 4 076 ms
4e17478 (control today, same session as the #175 run) 50 8.3 ms 1 179 ms 3.7 ms 4.5 ms 1 117 ms
reader-guard narrowed (my experiment, not merged) 90 53.7 ms 5 645 ms 8.4 ms 34.0 ms 5 221 ms
VICTORY_TEXTURE_NO_WAIT=1 96 22.2 ms 1 082 ms 19.4 ms 0.0 ms 0.0 ms
this PR (8e0e7b6) 113 84.6 ms 1 924 ms 62.9 ms 0.0 ms 0.0 ms

So on a backend you did not test on, the property you claim holds: no window waited at all, and it does not pay the deferred-texture price the NO_WAIT switch pays (0–2 a frame there).

What it hands over to is worth writing down: with the wait gone, the lane is now asking — 62.9 ms median, 936 ms in the worst window (write watch and guards). That is #177's target, and on this box it is the largest remaining item in the texture lane.

@AKA0GHOST

Copy link
Copy Markdown

Tested on Windows 11 D3D12, RTX 5070 (driver 621.14), Ryzen 5 5600, 16 GB: fork main + this PR at 8e0e7b6, diag build, VICTORY_RELAY=2, 10-minute runs. Thanks, this one made a visible difference here.

World frames only (samples over 1,000 draws), one run each:

median ms/frame preparing texture refresh draws/frame
fork main 798 541 160 ms 4,577
+ this PR 552 417 20 ms 4,796

The texture refresh time is where it shows: down by about 8x, which matches the mapping-lock waits going away. In game, walls and surfaces that stayed blank or stale before now fill in while streaming, and the prison interior is playable at 1–2 fps. No hangs or device removals in either 10-minute run (also with #220 on top). The lock tests pass here.

Caveat: one run per setup, and the run with this PR started on a cold shader cache.

AI-assisted (Claude Code); no game files attached.

@relativespace2K

Copy link
Copy Markdown

Native Windows D3D12 / AMD RDNA2 data point, if useful.

Setup: Windows 11 26100, RX 6800 XT 16 GB (RDNA2), driver 32.0.21045.29003, Ryzen 5 5600, 32 GB, Series X profile, diag build at 8e0e7b6, 1920x1080, no VICTORY_TEXTURE_NO_WAIT.

Two runs: 180 s from the checkpoint (prologue), and 300 s from a fresh data dir (first boot/streaming).

texture refreshes per frame, all threads, sampled windows in the fresh run:

before (main 2119b0e) this PR (8e0e7b6)
windows 25 107
wait median — 0.0 ms
wait mean — 0.37 ms
wait max 7,886.5 ms 35.5 ms
windows over 100 ms 14 0
texture-lane median — 3.1 ms
texture-lane max — 73.2 ms

Before the PR the frame line was 0.2 fps, 4.4-5.7 s/frame, textures 3.1-5.0 s; 14 of the 25 windows waited over 100 ms, the worst 7.9 s. With the PR the prologue runs at 1.2-1.5 fps, 669-822 ms/frame, 4,980-5,114 draws/frame, ~16-17 GB working set, with no device loss. The texture lane is 3.1 ms median, and the mapping wait is 0.0 ms in all but two windows (2.4 and 35.5 ms).

This matches the Linux/RX 7900 XT and RTX 5070 reports, and it keeps the in-place read rather than deferring textures. My earlier #393 (defaulting VICTORY_TEXTURE_NO_WAIT) is superseded by this; I am closing it.

AI-assisted: the runs above were made with an AI agent on my machine; I reviewed the logs and the result. No game files or captures are attached.

@LeonardoS11

Copy link
Copy Markdown

A measurement from another NVIDIA card, for what it is worth.

Caveats: the "with" runs carried #191 and #154 too, so the attribution to this PR is by the counter it changes, not by an isolated A/B; and those runs did not go through exactly the same scene. I have not yet checked the picture of this PR alone against main (doing that for each PR of the stack now, after finding that one of them, #158, breaks lighting here — details will go on that PR).

(Runs by me on the hardware above; numbers read from the logs and this note written with Claude Code.)

@rostaddotcc

Copy link
Copy Markdown

Two things from a Linux machine this morning, and the second one is the reason to rebase rather than close.

The branch no longer applies to main. On a26859f it is dirty: one hunk, in crates/victory-gpu/src/lib.rs, the re-export list — main added title_memory_reading_now while this adds title_memory_reading_range and title_memory_remapping_range. The union is all four plus what was there:

    title_memory_reading, title_memory_reading_now, title_memory_reading_range,
    title_memory_remapped, title_memory_remapping, title_memory_remapping_range,
    title_memory_written, writing as kernel_will_write,

Nothing else conflicts, and it builds and runs after that.

And it does exactly what it says on the newest main, on Linux/Proton (RX 7900 XT, RADV, vkd3d-proton 3.2.0, both runs entering the world with scripted keys, ~7 minutes each, 4 000-odd draws in the heaviest frames):

texture lane, per window main a26859f + this PR
waiting for title memory to hold still, median 430.6 ms 0.0 ms
…worst of 136 windows 2 977.7 ms 0.0 ms
the lane in all, median 599 ms 59 ms

Zero, not "smaller": in 136 out of 136 windows the readers never waited at all. That matters more since this morning's measurement (#178) that the title holds the mapping lock for 3 844–8 020 ms a frame across 1 721–5 041 separate changes — there is almost never a free moment for a global lock to be acquired, which is why scoping it per range removes the wait rather than shortening it.

I have the rebased commit (your change, the one-hunk resolution above) and can push it to my fork as a fresh PR if you would rather not force-push here — say which and I will.

Copy link
Copy Markdown

Independent Windows 11 run (build 26200, i5-8400, RTX 3060 12 GiB with driver 610.88, 32 GiB RAM). I compared the same warm-cache Series S checkpoint on local build 03c9405 and with 8e0e7b6 integrated as f33fa0a. Both builds had the same local display integration and idle-cache trim. The range-lock integration preserved main's existing remap counters, and all four watch::mapping_tests passed.

Mean draws in each logged interval Samples before / after Median texture-remap wait per frame before → after
1,500–3,499 8 / 5 277.55 → 0.0 ms
4,250–4,750 16 / 22 11.90 → 0.0 ms

The same heavier bin had a maximum reported wait of 257.2 ms before and 0.1 ms after. In later 50-second windows around 4,500 draws, actual presents averaged 4.33 FPS before (222 frames) and 4.94 FPS after (253 frames). There was one launch per build and the later windows were sampled at different times after loading, so the frame-rate comparison is directional. No device removal was logged in either run.

AI-assisted with OpenAI Codex.

@rostaddotcc

Copy link
Copy Markdown

Reading d510ee4 (two attempts on the title's page mapping, both taken back out) — two costs in that area get treated as one, and this PR only addresses the second.

The 32 ms of every cutscene frame in that note is the kernel map/unmap calls themselves. This PR does not touch them: unmap and the mapping are unchanged, the same pages end up mapped. There is no path here to the failure you hit — coalescing several pool pages into one view changes the shape of the mapping; scoping the lock does not.

What it changes is who waits. On 9376fd6 the write lock was held across the whole map operation (XMemMapPhysicalPages, from the unmap until every run is mapped again): 1 721–5 041 mapping changes per frame, lock held 3 844–8 020 ms of each frame, longest single change 46–175 ms. Readers' summed wait in those windows was 194–2 475 ms. Textures ask through the same lock, so the texture lane waited 430.6 ms median / 2 977.7 max and totalled 599 ms of the lane's 7 minutes.

With the diff, on the same save and scripted input, 7 minutes a side: texture wait 0.0 ms in 136 of 136 windows, lane total 599 → 59 ms median. Again as a caution: that is a wait figure, not a frame-rate one, and each side is a single run. Two runs of the same binary on this rig have differed by 2–4x at matched draw counts, so I would not put a fps number next to it.

The case worth checking when reviewing: a view reaching past the requested range, where unmap takes the other half away from a reader that asked about a range not overlapping the request. That is what mapping_extent widens to, and mapping_extent_includes_the_entire_old_view covers it; overflow, an unaligned base and an empty range all fall back to the global lock rather than guessing.

@rostaddotcc

Copy link
Copy Markdown

Rebased onto d510ee4, with the frame log's counters kept.

Conflict in watch.rs resolved by keeping everything upstream added since 2119b0e (view
tracking, the guards, ask_ahead) and replacing only the locking primitive: the plain
RwLock becomes the range-aware queue from this PR, and RemapUs/Remaps/RemapWaitUs are
now added by the new guard, so the frame log keeps measuring the same thing. lib.rs: the
union of the re-exports.

Measured on d510ee4a against d510ee4a + this, 330 s each, same save, scripted input, both
with VICTORY_FRAME_LOG=1 (Proton/Proton Experimental, RX 7900 XT, vkd3d-proton):

texture refreshes per frame, all threads
    base   893 ms median, of them 540 ms waiting for title memory to hold still   (n=137)
    +this  100 ms median, of them   0 ms                                          (n=146)

The texture lane moved 33 MB in 1,701 ms on the base and 161 MB in 330 ms here (19 MB/s
against 488 MB/s). By draw count, the frames the wait was bounding:

draws base +this
500–1,500 3,060 ms 1,716 ms −1,345 ms
1,500–3,000 2,860 ms 2,735 ms −125 ms
3,000–6,000 2,809 ms 2,806 ms −3 ms
6,000+ 3,286 ms 5,625 ms not matched (1,070 MB against 219 MB of textures in those windows)

So the wait is gone everywhere it was measured, and it is worth about 1.3 s a frame in the
lighter scenes; above 3,000 draws something else sets the frame, which is the next thing to
look at.

@rostaddotcc

Copy link
Copy Markdown

Measured the same way as my earlier comment, on the same rig, and this one corrects it: the wait goes away, but above 3,000 draws the queue costs more than the wait it saves.

Rig: RX 7900 XT (RDNA3), Mesa 26.3.1 RADV, Proton experimental, WINEDLLOVERRIDES="d3d12=n;d3d12core=n;dxgi=n", VICTORY_FRAME_LOG=1, 330 s a run, diag profile. Base d510ee4a against d510ee4a plus this PR.

The win is real and it is in the lane that waits:

  • texture refreshes per frame, all threads: 893 ms, of which 540 ms "waiting for title memory to hold still" (n=137) -> 100 ms with 0 ms of waiting (n=146).
  • The texture lane: 33 MB in 1,701 ms (19 MB/s) -> 161 MB in 330 ms (488 MB/s).
  • Frames of 500-1,500 draws: 3,060 -> 1,716 ms.

Bucked on draws and texture megabytes per frame, so the two runs are in comparable scenes, the same frames above 3,000 draws go the other way:

draws textures/frame base with this PR
< 1,500 < 50 MB 130 ms 43 ms
1,500-3,000 < 50 MB 3,228 ms 2,547 ms
>= 3,000 < 50 MB 2,809 ms 4,497 ms
>= 3,000 150-300 MB 2,927 ms 3,436 ms
>= 3,000 > 300 MB 3,058 ms 5,064 ms

Inside those frames the movement is copies 213 -> 672, copying back 323 -> 1,240 ms, everything else 559 -> 1,020 ms, textures 1,613 -> 330 ms, GPU 104 -> 78 ms. Whatever the copying back does when the readers are no longer serialised, it is not the wait.

What I think the cause is: a read in place does not take a shared lock, it takes one global mutex, twice - once to push a reader into a Vec, once to prune it with retain, then notify_all. A frame above 3,000 draws has thousands of reads in place, and they are serialised on that mutex. The 540 ms of wait it removes is smaller than what the registration costs at that rate.

I tried the cheap-reader version of this idea to see whether the shape is the problem: the address space cut into 512 stripes of 64 KiB, a read or a change taking only the stripes its range covers, ascending, all of them when the extent is unknown. The regression goes away - 3,000-6,000 draws, < 50 MB: 4,466 and 7,032 ms on base, 2,857 ms striped - so the shape is right. It also drops the property your test waiting_overlapping_writer_prevents_new_readers exists for, and that showed: host GPU device removed 0x887A0005 in 2 of 3 striped runs (at 1,129 and 5,860 draws), against none in 7 runs of base and of this PR at 7,096-10,360 draws. A read gets through a change of mapping, so I would not take that version anywhere near main, and I am not offering it as a patch.

So: the wait removal is worth having, and the read path has to be cheap before it can be had at every draw count. Worth knowing before this is merged as an improvement to frame time in general.

@Kamiloj9 Kamiloj9 closed this Nov 26, 2026
@Kamiloj9
Kamiloj9 deleted the codex/range-mapped-texture-reads branch November 26, 2026 00:29
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.

6 participants