Regions: clamp loose WC windows outside GPU-owned buffers and skip taken ranges in Shadow::copy #392

Open
github-bot wants to merge 2 commits from github-bot/gh-392 into main AGit
Owner

@⁠Lakanya on GitHub · 2026-12-06 00:04 UTC · original
Lakanya:pr/regions-clamp-wc-windows → main · +144 −7 · 2 files · 2 commits · open

Unregistered small buffer views in the write-combined heap fall through shadow::loose(), which unconditionally expands [address, end) to a 4 MiB grid (WINDOW = 4 MiB). Because the WC heap tightly packs small CPU buffers adjacent to Owner::Taken GPU-owned buffers, the 4 MiB expansion overlaps them—triggering ERROR larp_gpu::regions (window_over_taken), allocating a 4 MiB GPU_UPLOAD window over GPU-owned ranges, and causing stale_read refreshes in Shadow::copy().

What is in it

  • crates/larp-gpu/src/regions.rs: add clamp_outside_taken() to clamp a candidate loose window to the maximal gap containing [address, end) that overlaps no Owner::Taken range (preferring 64 KiB, then 4 KiB inward alignment), plus taken_in() for clipped taken sub-ranges.
  • crates/larp-gpu/src/shadow.rs:
    • In shadow::loose(), clamp the window via regions::clamp_outside_taken() when the 4 MiB grid touches Owner::Taken.
    • In Shadow::copy(), skip Owner::Taken sub-ranges via outside(from, to, regions::taken_in(from, to)) so pre-existing windows (WindowBefore) never overwrite/read stale bytes over GPU-owned ranges.

No rendering shortcuts, no game data.

Tested

  • Hardware: Windows 11 x64, RTX 5070 Mobile (8 GB VRAM), 32 GB RAM, Series X profile @ 2560x1440 DLSS Performance (FG off).
  • cargo test --lib -p larp-gpu -p larp-hle -- --test-threads=4: larp-gpu 24 passed / 0 failed / 6 ignored; larp-hle 76 passed / 0 failed / 4 ignored.
  • Live A/B (opening 3D gameplay scene, 0 dropped draws): 0 ERROR larp_gpu::regions, prep time 5.9 -> 3.4 ms, p95 113.8 -> 56.0 ms.
  • Not tested on Linux/Wine.

AI-assisted by Antigravity (Gemini 4 Neon Medium).

**@⁠Lakanya** on GitHub · 2026-12-06 00:04 UTC · [original](https://github.com/gamingwithhomie/larp/pull/392) `Lakanya:pr/regions-clamp-wc-windows` → `main` · +144 −7 · 2 files · 2 commits · open Unregistered small buffer views in the write-combined heap fall through `shadow::loose()`, which unconditionally expands `[address, end)` to a 4 MiB grid (`WINDOW = 4 MiB`). Because the WC heap tightly packs small CPU buffers adjacent to `Owner::Taken` GPU-owned buffers, the 4 MiB expansion overlaps them—triggering `ERROR larp_gpu::regions` (`window_over_taken`), allocating a 4 MiB `GPU_UPLOAD` window over GPU-owned ranges, and causing `stale_read` refreshes in `Shadow::copy()`. ### What is in it - `crates/larp-gpu/src/regions.rs`: add `clamp_outside_taken()` to clamp a candidate loose window to the maximal gap containing `[address, end)` that overlaps no `Owner::Taken` range (preferring 64 KiB, then 4 KiB inward alignment), plus `taken_in()` for clipped taken sub-ranges. - `crates/larp-gpu/src/shadow.rs`: - In `shadow::loose()`, clamp the window via `regions::clamp_outside_taken()` when the 4 MiB grid touches `Owner::Taken`. - In `Shadow::copy()`, skip `Owner::Taken` sub-ranges via `outside(from, to, regions::taken_in(from, to))` so pre-existing windows (`WindowBefore`) never overwrite/read stale bytes over GPU-owned ranges. No rendering shortcuts, no game data. ### Tested - Hardware: Windows 11 x64, RTX 5070 Mobile (8 GB VRAM), 32 GB RAM, Series X profile @ 2560x1440 DLSS Performance (FG off). - `cargo test --lib -p larp-gpu -p larp-hle -- --test-threads=4`: `larp-gpu` 24 passed / 0 failed / 6 ignored; `larp-hle` 76 passed / 0 failed / 4 ignored. - Live A/B (opening 3D gameplay scene, 0 dropped draws): 0 `ERROR larp_gpu::regions`, prep time `5.9 -> 3.4 ms`, p95 `113.8 -> 56.0 ms`. - Not tested on Linux/Wine. AI-assisted by Antigravity (Gemini 4 Neon Medium).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-06 00:06:59 +00:00
Author
Owner

@⁠vanya890 on GitHub · 2026-12-06 00:52 UTC · original

One edge case worth making explicit: clamp_outside_taken() returns None when the requested interval [address, end) itself intersects an Owner::Taken range. In shadow::loose(), the caller only replaces (start, len) on Some; on None it continues with the original 4 MiB window, which can still overlap the taken range and proceeds through the normal Shadow creation path. Shadow::copy() now skips taken sub-ranges, but the requested view may then contain no valid mirrored bytes for the portion that intersects Owner::Taken.

Is an ask intersecting a taken buffer impossible by invariant? If so, a focused test/assert would make that assumption clear. Otherwise, it seems worth handling the None case explicitly rather than silently retaining the original overlapping window. I haven't reproduced this edge case at runtime; this is a control-flow review question.

**@⁠vanya890** on GitHub · 2026-12-06 00:52 UTC · [original](https://github.com/gamingwithhomie/larp/pull/392#issuecomment-7448722462) One edge case worth making explicit: `clamp_outside_taken()` returns `None` when the requested interval `[address, end)` itself intersects an `Owner::Taken` range. In `shadow::loose()`, the caller only replaces `(start, len)` on `Some`; on `None` it continues with the original 4 MiB window, which can still overlap the taken range and proceeds through the normal `Shadow` creation path. `Shadow::copy()` now skips taken sub-ranges, but the requested view may then contain no valid mirrored bytes for the portion that intersects `Owner::Taken`. Is an ask intersecting a taken buffer impossible by invariant? If so, a focused test/assert would make that assumption clear. Otherwise, it seems worth handling the `None` case explicitly rather than silently retaining the original overlapping window. I haven't reproduced this edge case at runtime; this is a control-flow review question.
Author
Owner

@⁠Lakanya on GitHub · 2026-12-06 01:02 UTC · original

Good catch, thanks for the review!

In normal operation shadow::buffers() resolves asks inside taken WC buffers before shadow::loose() is reached, so an ask reaching loose() should not intersect Owner::Taken. However, if an unindexed/spanning ask ever does intersect Owner::Taken (clamp_outside_taken() returns None), skipping taken sub-ranges in Shadow::copy() on that fallback window would indeed leave the intersecting portion unmirrored.

Pushed a follow-up commit to make this explicit and safe:

  • Added skip_taken: bool on Shadow: true for windows created clean of Owner::Taken (protecting against later WindowBefore overlaps), and false for fallback window_over_taken windows so Shadow::copy() preserves the original fallback mirroring behavior.
  • Factored clamp_in_map() in regions.rs and added unit tests covering 64 KiB/4 KiB gap clamping and the None return when [address, end) itself intersects Owner::Taken.

Re-tested cargo test --lib -p larp-gpu -- --test-threads=4 (26 passed, 0 failed, 6 ignored).

**@⁠Lakanya** on GitHub · 2026-12-06 01:02 UTC · [original](https://github.com/gamingwithhomie/larp/pull/392#issuecomment-7448857569) Good catch, thanks for the review! In normal operation `shadow::buffers()` resolves asks inside taken WC buffers before `shadow::loose()` is reached, so an ask reaching `loose()` should not intersect `Owner::Taken`. However, if an unindexed/spanning ask ever does intersect `Owner::Taken` (`clamp_outside_taken()` returns `None`), skipping taken sub-ranges in `Shadow::copy()` on that fallback window would indeed leave the intersecting portion unmirrored. Pushed a follow-up commit to make this explicit and safe: - Added `skip_taken: bool` on `Shadow`: `true` for windows created clean of `Owner::Taken` (protecting against later `WindowBefore` overlaps), and `false` for fallback `window_over_taken` windows so `Shadow::copy()` preserves the original fallback mirroring behavior. - Factored `clamp_in_map()` in `regions.rs` and added unit tests covering 64 KiB/4 KiB gap clamping and the `None` return when `[address, end)` itself intersects `Owner::Taken`. Re-tested `cargo test --lib -p larp-gpu -- --test-threads=4` (26 passed, 0 failed, 6 ignored).
Author
Owner

@⁠vanya890 on GitHub · 2026-12-06 01:09 UTC · original

Additional cross-vendor evidence from Issue #454: an RX 9070 XT user on the v1.1.6 AMD test build has the same larp_gpu::regions window_over_taken diagnostic. At 00:53:46 UTC, descriptor/host.rs:2599 reports a 4 MiB window (0xd76800000+0x400000) overlapping GPU-owned WC buffers, including 0xd76a222c0+0x344 and several adjacent allocations. About seven minutes later, D3D12 reports device removal (0x887A0006) with 5 submissions ahead of 69 command lists.

This is a second GPU vendor/family showing the exact overlap class your PR addresses. The later device removal is correlated, not proof that the overlap caused the hang. The reporter's v1.1.6 build predates this PR, so it does not validate the patch; after merge, Issue #454 would be a useful AMD RDNA4 retest. If it still fails, the log explicitly recommends LARP_D3D12_DRED=1 to capture breadcrumbs and the faulting address.

**@⁠vanya890** on GitHub · 2026-12-06 01:09 UTC · [original](https://github.com/gamingwithhomie/larp/pull/392#issuecomment-7448952552) Additional cross-vendor evidence from Issue #454: an RX 9070 XT user on the v1.1.6 AMD test build has the same `larp_gpu::regions` `window_over_taken` diagnostic. At 00:53:46 UTC, `descriptor/host.rs:2599` reports a 4 MiB window (`0xd76800000+0x400000`) overlapping GPU-owned WC buffers, including `0xd76a222c0+0x344` and several adjacent allocations. About seven minutes later, D3D12 reports device removal (`0x887A0006`) with 5 submissions ahead of 69 command lists. This is a second GPU vendor/family showing the exact overlap class your PR addresses. The later device removal is correlated, not proof that the overlap caused the hang. The reporter's v1.1.6 build predates this PR, so it does not validate the patch; after merge, Issue #454 would be a useful AMD RDNA4 retest. If it still fails, the log explicitly recommends `LARP_D3D12_DRED=1` to capture breadcrumbs and the faulting address.
Author
Owner

@⁠TrinityForever15 on GitHub · 2026-12-06 03:17 UTC · original

Confirmed on RX 6650 XT: before this PR, car chases logged larp_gpu::regions window_over_taken with spikes up to 671 ms. With it (plus #526), those errors are gone. https://github.com/iceboerg00/larp-amd/issues/64

**@⁠TrinityForever15** on GitHub · 2026-12-06 03:17 UTC · [original](https://github.com/gamingwithhomie/larp/pull/392#issuecomment-7450572679) Confirmed on RX 6650 XT: before this PR, car chases logged `larp_gpu::regions` `window_over_taken` with spikes up to 671 ms. With it (plus #526), those errors are gone. https://github.com/iceboerg00/larp-amd/issues/64
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/392/head:github-bot/gh-392'
git switch 'github-bot/gh-392'

Merge

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