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

Open
luniiya wants to merge 1 commit from upstream-pr/100 into main
luniiya commented 2026-11-25 14:04:40 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by luniiya on 2026-11-25T14:04:40Z.


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.
<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:issue:6606292559 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/100). Originally posted by [luniiya](https://github.com/luniiya) on 2026-11-25T14:04:40Z. --- ## 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.
gut0to commented 2026-11-25 14:53:46 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by gut0to on 2026-11-25T14:53:46Z.

Review: COMMENTED


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

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:review:6059933979 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/100#pullrequestreview-6059933979). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-25T14:53:46Z. **Review:** COMMENTED --- AI-assisted review: I traced the old and new tile-base calculations against the reported alignment failure.
gut0to commented 2026-11-25 14:53:47 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by gut0to on 2026-11-25T14:53:47Z.

Code review location: crates/victory-gpu/src/shadow.rs; line 1954

@@ -1946,7 +1946,12 @@ impl Shadow {
             before.entry(tile).or_insert_with(|| {
                 #[cfg(feature = "diagnostics")]
                 SPARSE_WRITES[1].fetch_add(PAGE as u64, Ordering::Relaxed);
-                let whole = data as usize & !(PAGE - 1);
+                // `data` points `at % PAGE` bytes into the mapped 64 KiB tile.
+                // On Windows WDDM, `ID3D12Resource::Map` is 64 KiB-aligned, so
+                // 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;

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.

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:review-comment:4696695455 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/100#discussion_r4696695455). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-25T14:53:47Z. **Code review location:** `crates/victory-gpu/src/shadow.rs`; line 1954 ~~~~diff @@ -1946,7 +1946,12 @@ impl Shadow { before.entry(tile).or_insert_with(|| { #[cfg(feature = "diagnostics")] SPARSE_WRITES[1].fetch_add(PAGE as u64, Ordering::Relaxed); - let whole = data as usize & !(PAGE - 1); + // `data` points `at % PAGE` bytes into the mapped 64 KiB tile. + // On Windows WDDM, `ID3D12Resource::Map` is 64 KiB-aligned, so + // 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; ~~~~ --- 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.
TobiasFornell commented 2026-11-25 21:34:27 +00:00 (Migrated from github.com)

Imported from Belobug/VIctory-Belobug-AMD. Originally posted by TobiasFornell on 2026-11-25T21:34:27Z.


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).

<!-- greyfinch-sync:Belobug/VIctory-Belobug-AMD:comment:7175256886 --> Imported from [Belobug/VIctory-Belobug-AMD](https://github.com/Belobug/VIctory-Belobug-AMD/pull/100#issuecomment-7175256886). Originally posted by [TobiasFornell](https://github.com/TobiasFornell) on 2026-11-25T21:34:27Z. --- 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).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/100:upstream-pr/100'
git switch 'upstream-pr/100'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/100'
git switch 'upstream-pr/100'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/100'
git switch 'upstream-pr/100'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/100'
git switch 'main'
git merge --squash 'upstream-pr/100'
git switch 'main'
git merge --ff-only 'upstream-pr/100'
git switch 'main'
git merge 'upstream-pr/100'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Belobug/VIctory-Belobug-AMD!100
No description provided.