GPU: fix Shadow::sparse_writing tile base for 4 KiB-aligned Map() pointers #100

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

@⁠luniiya on GitHub · 2026-11-25 14:04 UTC · original
luniiya:fix/sparse-writing-tile-alignment → main · +6 −1 · 1 files · 1 commits · open

Problem

In Shadow::sparse_writing (crates/victory-gpu/src/shadow.rs:1949), the start of the mapped 64 KiB tile was computed by masking the host pointer down to a 64 KiB boundary:

let whole = data as usize & !(PAGE - 1);

On Windows WDDM, ID3D12Resource::Map() on a 32 MiB tile heap returns 64 KiB-aligned pointers, so this happens to land on the tile base. Under Linux / Wine / vkd3d-proton, vkMapMemory / mmap only guarantees 4 KiB alignment, so masking down to 64 KiB steps before the start of the mapping and crashes immediately with 0xc0000005 (STATUS_ACCESS_VIOLATION) on world entry when sparse copies are enabled.

Fix

Compute the tile base relative to at % PAGE instead:

let whole = data as usize - at % PAGE;
  • Identical to data & !(PAGE - 1) whenever the mapping is 64 KiB-aligned (Windows unchanged).
  • Works with any alignment (including 4 KiB under Linux / vkd3d-proton).
  • Checked the rest of crates/victory-gpu for any similar !(PAGE - 1) mask on mapped host pointers; this was the only one.

(Complementary to #69: even when sparse copies are turned off by default under Wine, explicitly enabling VICTORY_SPARSE=1 under Wine/vkd3d-proton will no longer segfault.)

Tested

  • Arch Linux, AMD Radeon RX 7800 XT (RADV), GE-Proton11-14 + vkd3d-proton, x86_64-pc-windows-msvc diagnostics build with VICTORY_COPY_MEMORY=1 and sparse copies enabled.
  • Previously crashed reliably with 0xc0000005 in ucrtbase.dll (memcpy from Shadow::sparse_writing) on world entry; with this fix, enters and renders live 3D gameplay cleanly.
**@⁠luniiya** on GitHub · 2026-11-25 14:04 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/100) `luniiya:fix/sparse-writing-tile-alignment` → `main` · +6 −1 · 1 files · 1 commits · open ## Problem In `Shadow::sparse_writing` (`crates/victory-gpu/src/shadow.rs:1949`), the start of the mapped 64 KiB tile was computed by masking the host pointer down to a 64 KiB boundary: ```rust let whole = data as usize & !(PAGE - 1); ``` On Windows WDDM, `ID3D12Resource::Map()` on a 32 MiB tile heap returns 64 KiB-aligned pointers, so this happens to land on the tile base. Under Linux / Wine / vkd3d-proton, `vkMapMemory` / `mmap` only guarantees 4 KiB alignment, so masking down to 64 KiB steps before the start of the mapping and crashes immediately with `0xc0000005` (`STATUS_ACCESS_VIOLATION`) on world entry when sparse copies are enabled. ## Fix Compute the tile base relative to `at % PAGE` instead: ```rust let whole = data as usize - at % PAGE; ``` - Identical to `data & !(PAGE - 1)` whenever the mapping is 64 KiB-aligned (Windows unchanged). - Works with any alignment (including 4 KiB under Linux / vkd3d-proton). - Checked the rest of `crates/victory-gpu` for any similar `!(PAGE - 1)` mask on mapped host pointers; this was the only one. *(Complementary to #69: even when sparse copies are turned off by default under Wine, explicitly enabling `VICTORY_SPARSE=1` under Wine/vkd3d-proton will no longer segfault.)* ## Tested - Arch Linux, AMD Radeon RX 7800 XT (RADV), GE-Proton11-14 + vkd3d-proton, `x86_64-pc-windows-msvc` diagnostics build with `VICTORY_COPY_MEMORY=1` and sparse copies enabled. - Previously crashed reliably with `0xc0000005` in `ucrtbase.dll` (`memcpy` from `Shadow::sparse_writing`) on world entry; with this fix, enters and renders live 3D gameplay cleanly.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:35:47 +00:00
Author
Owner

@⁠TobiasFornell on GitHub · 2026-11-25 21:34 UTC · original

Independent confirmation on a different setup: CachyOS, RX 9070 XT (RDNA4, RADV), Proton Experimental, x86_64-pc-windows-msvc build, Belobug base.

Without this change we hit the same 0xc0000005 in Shadow::sparse_writing, about 3.5 minutes into the run. We arrived at the same data - at % PAGE change independently. With it: one 480 s run, 0 kernel GPU page faults, no crash (a single run, not a long-term test).

**@⁠TobiasFornell** on GitHub · 2026-11-25 21:34 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/100#issuecomment-7175256886) Independent confirmation on a different setup: CachyOS, RX 9070 XT (RDNA4, RADV), Proton Experimental, `x86_64-pc-windows-msvc` build, Belobug base. Without this change we hit the same `0xc0000005` in `Shadow::sparse_writing`, about 3.5 minutes into the run. We arrived at the same `data - at % PAGE` change independently. With it: one 480 s run, 0 kernel GPU page faults, no crash (a single run, not a long-term test).
@ -1950,0 +1951,4 @@
// masking with `!(PAGE - 1)` lands on the tile base; under
// Linux/vkd3d-proton, `vkMapMemory`/`mmap` is only 4 KiB-aligned
// and masking reads before the mapping (`0xc0000005`).
let whole = data as usize - at % PAGE;
Author
Owner

@⁠gut0to on GitHub · 2026-11-25 14:53 UTC · original

Could this calculation get a regression test with a 4 KiB-aligned mapped pointer where at % PAGE != 0? That is the case where the old 64 KiB mask returned an address before the mapping, while this subtraction should recover the tile base. AI-assisted review.

**@⁠gut0to** on GitHub · 2026-11-25 14:53 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/100#discussion_r4696695455) Could this calculation get a regression test with a 4 KiB-aligned mapped pointer where at % PAGE != 0? That is the case where the old 64 KiB mask returned an address before the mapping, while this subtraction should recover the tile base. AI-assisted review.
Author
Owner

@⁠gut0to on GitHub · 2026-11-25 14:53 UTC · original · reviewed

AI-assisted review: I traced the old and new tile-base calculations against the reported alignment failure.

**@⁠gut0to** on GitHub · 2026-11-25 14:53 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/100#pullrequestreview-6059933979) · **reviewed** AI-assisted review: I traced the old and new tile-base calculations against the reported alignment failure.
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/shadow.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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