Buffer views that start off a 16-byte boundary are read at the right element #4984

Open
github-bot wants to merge 3 commits from github-bot/gh-4983 into main AGit
Owner

@⁠TheDarkDnKTv on GitHub · 2026-12-01 16:25 UTC · original
TheDarkDnKTv:pr/view-offset-remainder → main · +896 −111 · 22 files · 3 commits · open

What it changes

What was wrong. Shop and neon signs and their glow are dark on main. The console can start a buffer view anywhere on a 4-byte boundary, the card only on a multiple of 16. A structured view that starts off 16 bytes was left as a structured view of the host buffer, and a translated shader reads a view by byte offset, so it landed on the wrong element. Measured in the title on a table of 24-byte records: a shader asking for element n got element 3n. About 300 to 370 views of this kind are made a frame.

The change.

  • Shaders: the translator reads a 0 to 15 byte remainder from the view's stride word (bits 26 to 29), from the same slot as the descriptor, and adds it to the byte offset once the console offset is computed (loads, stores, atomics, formatted and sub-dword accesses, scalar loads). The access is first checked against the console view's own extent, so the bytes before the console start are never reachable (out-of-range reads give zero, out-of-range writes and atomics do nothing). Programs that cannot resolve a shifted view keep their old lowering. New module emit/lower/viewstart.rs.
  • GPU: the host makes such a view raw, from the 16-byte boundary below its start, on the original buffer, and publishes the remainder in the stride word after the descriptor (persistent slots, dynamic tables and the listed copy). descriptor/host/viewstart.rs decides which views qualify and logs unaligned structured views per frame: ....
  • README: one row in the settings table.

Switch. VICTORY_VIEW_START_REMAINDER: on by default, =0 gives the old reading in both the layer and the translator. It is in the shader cache key, and the change is under victory-shader/src, so every shader re-translates once after this merges and the pipelines are rebuilt.

Not changed by this. The flicker in reflections and in the wall mirror is there on main and is the same with this branch. The phone screen is unchanged.

How it was tested

  • GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): RTX 5090, the defaults of victory play at 5fabb46e (no ray tracing), the same for before and after.
  • Before: pure main (5fabb46e), shop and neon signs are dark.
  • After: this branch, default, shop and neon signs glow. The wall mirror and the UI layers already work on main.
  • For shader changes, the wave sizes tested (32 and/or 64 lanes): the title run was on an NVIDIA card; no 64-lane host.
  • cargo test passed for the crates it touches: yes (commit 674402aa, Windows, rustc 1.98.0):
    • cargo test -p victory-shader --lib: 538 passed, 0 failed, 10 ignored
    • cargo test -p victory-gpu -- --test-threads=1: 1098 passed, 0 failed, 66 ignored
    • cargo test --workspace --locked --no-fail-fast: 2493 passed, 0 failed, 96 ignored
    • cargo clippy --workspace --all-targets --locked -- -D warnings (and --all-features) does not pass: 5 findings in host/font.rs, host/menu.rs and host/panel.rs, files this change does not touch (the same on main). Nothing in the files of this change.
    • cargo xtask policy: ok
  • New tests are synthetic (hand-built programs and generated buffers, card and WARP): raw views of strides 4 to 68 read and written at the right element, including an offset that would reach the prefix; lanes that pick their own unaligned slots; the published word in dynamic tables and the listed copy; view reads across copies in one submission; the cache key differs with the switch off.

Not tested: any AMD card; Series S; the cost of the extra loads (a shader reading through a heap view now also loads the stride word and the view's size); a run with the switch off in the same scene; victory translate or victory shaders over the whole game on this branch.

Known limit. A persistent slot's descriptor and its remainder are two writes, so a draw recorded before the slot is rewritten sees the later descriptor and the later word, as it did before for the descriptor alone.

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches. Related: #3894 (merged) makes scalar and dword buffer addresses ignore their two low bits, and #4046 (open) stops the components of a heap-picked scalar load from wrapping to the view's start. Both change the address a shader computes; neither moves where a view begins, so a view that starts off 16 bytes is still read at the wrong element with both. #4046 edits the same dword-chunk loop (raw_dwords, view_dwords here), so whichever lands second needs a few lines of rebase. None found with the same symptom.

🤖 Generated with Claude Code

**@⁠TheDarkDnKTv** on GitHub · 2026-12-01 16:25 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4983) `TheDarkDnKTv:pr/view-offset-remainder` → `main` · +896 −111 · 22 files · 3 commits · open ## What it changes **What was wrong.** Shop and neon signs and their glow are dark on `main`. The console can start a buffer view anywhere on a 4-byte boundary, the card only on a multiple of 16. A structured view that starts off 16 bytes was left as a structured view of the host buffer, and a translated shader reads a view by byte offset, so it landed on the wrong element. Measured in the title on a table of 24-byte records: a shader asking for element `n` got element `3n`. About 300 to 370 views of this kind are made a frame. **The change.** - `Shaders:` the translator reads a 0 to 15 byte remainder from the view's stride word (bits 26 to 29), from the same slot as the descriptor, and adds it to the byte offset once the console offset is computed (loads, stores, atomics, formatted and sub-dword accesses, scalar loads). The access is first checked against the console view's own extent, so the bytes before the console start are never reachable (out-of-range reads give zero, out-of-range writes and atomics do nothing). Programs that cannot resolve a shifted view keep their old lowering. New module `emit/lower/viewstart.rs`. - `GPU:` the host makes such a view raw, from the 16-byte boundary below its start, on the original buffer, and publishes the remainder in the stride word after the descriptor (persistent slots, dynamic tables and the listed copy). `descriptor/host/viewstart.rs` decides which views qualify and logs `unaligned structured views per frame: ...`. - `README:` one row in the settings table. **Switch.** `VICTORY_VIEW_START_REMAINDER`: on by default, `=0` gives the old reading in both the layer and the translator. It is in the shader cache key, and the change is under `victory-shader/src`, so every shader re-translates once after this merges and the pipelines are rebuilt. **Not changed by this.** The flicker in reflections and in the wall mirror is there on `main` and is the same with this branch. The phone screen is unchanged. ## How it was tested - GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): RTX 5090, the defaults of `victory play` at `5fabb46e` (no ray tracing), the same for before and after. - Before: pure `main` (`5fabb46e`), shop and neon signs are dark. - After: this branch, default, shop and neon signs glow. The wall mirror and the UI layers already work on `main`. - For shader changes, the wave sizes tested (32 and/or 64 lanes): the title run was on an NVIDIA card; no 64-lane host. - `cargo test` passed for the crates it touches: yes (commit `674402aa`, Windows, rustc 1.98.0): - `cargo test -p victory-shader --lib`: 538 passed, 0 failed, 10 ignored - `cargo test -p victory-gpu -- --test-threads=1`: 1098 passed, 0 failed, 66 ignored - `cargo test --workspace --locked --no-fail-fast`: 2493 passed, 0 failed, 96 ignored - `cargo clippy --workspace --all-targets --locked -- -D warnings` (and `--all-features`) does not pass: 5 findings in `host/font.rs`, `host/menu.rs` and `host/panel.rs`, files this change does not touch (the same on `main`). Nothing in the files of this change. - `cargo xtask policy`: ok - New tests are synthetic (hand-built programs and generated buffers, card and WARP): raw views of strides 4 to 68 read and written at the right element, including an offset that would reach the prefix; lanes that pick their own unaligned slots; the published word in dynamic tables and the listed copy; view reads across copies in one submission; the cache key differs with the switch off. **Not tested:** any AMD card; Series S; the cost of the extra loads (a shader reading through a heap view now also loads the stride word and the view's size); a run with the switch off in the same scene; `victory translate` or `victory shaders` over the whole game on this branch. **Known limit.** A persistent slot's descriptor and its remainder are two writes, so a draw recorded before the slot is rewritten sees the later descriptor and the later word, as it did before for the descriptor alone. ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. Related: #3894 (merged) makes scalar and dword buffer addresses ignore their two low bits, and #4046 (open) stops the components of a heap-picked scalar load from wrapping to the view's start. Both change the address a shader computes; neither moves where a view begins, so a view that starts off 16 bytes is still read at the wrong element with both. #4046 edits the same dword-chunk loop (`raw_dwords`, `view_dwords` here), so whichever lands second needs a few lines of rebase. None found with the same symptom. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
What was wrong: the card cannot start a buffer view anywhere but on a multiple
of 16 bytes. The layer left a structured view whose start is not on one as a
structured view of the host buffer, and a translated shader addresses a view by
byte offset, so it landed on the wrong element. Measured in the title: a table
of 24-byte records read through such a view gave element 3n where the shader
asked for element n, and 300 to 370 such views were made a frame.

What this does: the host side (next commit) makes the view raw, starting on the
16-byte boundary below the console view's start, on the original buffer, and
publishes the 0 to 15 byte difference in the view's stride word (bits 26 to 29,
`VIEW_START_MASK` and `VIEW_START_SHIFT`). The translator reads the word from
the same slot as the descriptor and adds the difference to the byte offset once
the console offset is computed (loads, stores, atomics, formatted and
sub-dword accesses, scalar loads). Before that it checks the access against the
console view's own extent, so the bytes before the console start are never
reachable through the adjusted address: an out-of-range read gives zero, an
out-of-range write or atomic does nothing. A bias is loaded once per resolved
handle and shared by the accesses that use it. Programs that cannot resolve a
shifted view keep their old lowering.

`VICTORY_VIEW_START_REMAINDER` (on by default; 0 restores both old paths) enters
the shader cache key (`KEYED`, 47 to 48 entries), so a run with the switch off is
not served translations made with it on.

Tests (synthetic programs, run on the card and on WARP where one is present):
- fixed table entries and per-lane heap indices, read and written through
  raw views of strides 4, 8, 12, 24, 36 and 68, including an offset that
  would reach the prefix if the remainder were added before the check;
- a waterfall loop whose lanes pick their own unaligned slots;
- the cache key differs with the switch off;
- three existing tests now allow for the extra stride-table root SRV.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
The host half of the previous commit. A structured view of a dword-aligned
start that is not a multiple of 16 bytes was made on a copy of its own bytes
(or left structured); it is now a raw descriptor (and raw twin) on the original
buffer, from the 16-byte boundary below its start, wide enough to cover the
prefix, on AMD and NVIDIA alike. Typed buffers, constants and root views keep
their own paths.

- `descriptor/host/viewstart.rs` decides which views qualify (dword-aligned
  stride and start, extent that fits the buffer and the shader's out-of-range
  coordinate, a buffer that allows unordered access for a UAV) and counts them
  per frame; the log line `unaligned structured views per frame` says how many
  were seen, converted, or refused and why, and how many persistent slots had
  their remainder rewritten.
- The remainder goes into the view's stride word (bits 26 to 29), written after
  the descriptor and its twin under the slot lock, for persistent slots, dynamic
  tables and the listed copy alike; a deferred update that keeps the old
  descriptor keeps its word. A publication on a title slot stays as volatile as
  it was: a draw recorded before the slot is rewritten sees the later
  descriptor, and now the later word with it.
- A write view whose buffer does not allow unordered access is made on an exact
  copy of its bytes, so its refresh (`buffer_copy`) must go to that copy and not
  to the buffer: `widens` decides it the same way `buffer_uav` does.
- `VICTORY_VIEW_START_REMAINDER=0` gives the old exact copy and the old words;
  it is kept across `victory play`'s clearing of the environment.

Tests: the slot-reuse test checks the published word for strides 4 to 68 in a
dynamic table and the listed copy; the copy-and-refresh test checks that a
descriptor and its refresh use one copy for read and write views, with and
without a buffer that allows unordered access; an ordering test on WARP reads a
view across copies made in one submission and sees each value in order.
(`shadow::with_read_only_parent` is a small test helper that registers a real
upload buffer as the parent of a view.)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
One row under the advanced settings: what the switch does, that 0 gives the
old reading, and that changing it rebuilds the shaders.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-01 16:29:09 +00:00
Author
Owner

@⁠mstrmoos on GitHub · 2026-12-01 17:26 UTC · original

I measured the cost the description lists as not tested. On an RTX 5080 it's large, so worth knowing before this merges.

Setup. RTX 5080 16 GB, Ryzen 7 9800X3D. Series X, 2560x1440, DLSS Balanced, frame generation off, ray tracing off. Standing on a pier for 120 s after a warm-up run (each switch value re-translates): water, a terrace with people, the far shore, about 3,800-5,400 draws a frame. Diagnostics build of my branch (#4836's code) with this PR and #4927 picked on top.

It does its job. unaligned structured views per frame: SRV 357.7 seen, 357.7 with remainder, 0.0 disabled …, and no crash.

But the card time doubles:

frame (median) the card
this PR on (default) 59.1 ms (16.9 fps) 100 % busy, submitting thread waiting 32 ms a frame
VICTORY_VIEW_START_REMAINDER=0, same build 30.6 ms (32.7 fps) 99 %

Where it goes. Per-command card time (VICTORY_GPU_TIME=1) with it on, against my earlier profile of the same scene: the 21 programs that are in both top-40 lists go from 5.25 to 9.86 ms a frame together. Some single programs:

program before with this PR
915ed81e (compute, indirect) 0.585 ms 1.190 ms
b7a81f5b / 43e24028 (mesh / pixel) 0.114 0.654
5b6eed17 (compute) 0.130 0.616
2dc364df (compute, indirect) 0.105 0.451
56a84ca7 / 02ff1207 (mesh / pixel) 0.184 0.562

Why. victory translate with VICTORY_SHADER_CENSUS over all 26,180 programs, before against this PR:

per program, summed before with this PR
instructions 98.1 M 122.7 M (+25 %)
buffer loads 2.92 M 3.70 M (+27 %)
descriptor handles 0.98 M 1.27 M (+30 %)
selects 11.8 M 17.8 M (+50 %)

So every access through a heap buffer view pays for the stride-word load, the size load, the extent check and the selects, while about 360 views a frame are actually unaligned. The extra loads depend on the descriptor, so they lengthen the chains the card is already waiting on (long-scoreboard stalls dominate this title on NVIDIA; numbers in #4917).

Ideas, untested:

  • Gate the remainder path per program: only programs that have been seen binding an unaligned view (the host logs them) get the extended lowering, through a re-translate, the way other per-program keys work.
  • Or skip the extent check and selects where the view is known aligned (remainder 0), so the common path is one extra load, not a load plus compares and selects.

I'm not putting it on my play branch as it is, but the signs are worth fixing, and I'm happy to measure a revised version on the same scene.

**@⁠mstrmoos** on GitHub · 2026-12-01 17:26 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4983#issuecomment-7319297235) I measured the cost the description lists as not tested. On an RTX 5080 it's large, so worth knowing before this merges. **Setup.** RTX 5080 16 GB, Ryzen 7 9800X3D. Series X, 2560x1440, DLSS Balanced, frame generation off, ray tracing off. Standing on a pier for 120 s after a warm-up run (each switch value re-translates): water, a terrace with people, the far shore, about 3,800-5,400 draws a frame. Diagnostics build of my branch (#4836's code) with this PR and #4927 picked on top. **It does its job.** `unaligned structured views per frame: SRV 357.7 seen, 357.7 with remainder, 0.0 disabled …`, and no crash. **But the card time doubles:** | | frame (median) | the card | | --- | --- | --- | | this PR on (default) | 59.1 ms (16.9 fps) | 100 % busy, submitting thread waiting 32 ms a frame | | `VICTORY_VIEW_START_REMAINDER=0`, same build | 30.6 ms (32.7 fps) | 99 % | **Where it goes.** Per-command card time (`VICTORY_GPU_TIME=1`) with it on, against my earlier profile of the same scene: the 21 programs that are in both top-40 lists go from 5.25 to 9.86 ms a frame together. Some single programs: | program | before | with this PR | | --- | --- | --- | | `915ed81e` (compute, indirect) | 0.585 ms | 1.190 ms | | `b7a81f5b` / `43e24028` (mesh / pixel) | 0.114 | 0.654 | | `5b6eed17` (compute) | 0.130 | 0.616 | | `2dc364df` (compute, indirect) | 0.105 | 0.451 | | `56a84ca7` / `02ff1207` (mesh / pixel) | 0.184 | 0.562 | **Why.** `victory translate` with `VICTORY_SHADER_CENSUS` over all 26,180 programs, before against this PR: | per program, summed | before | with this PR | | --- | --- | --- | | instructions | 98.1 M | 122.7 M (+25 %) | | buffer loads | 2.92 M | 3.70 M (+27 %) | | descriptor handles | 0.98 M | 1.27 M (+30 %) | | selects | 11.8 M | 17.8 M (+50 %) | So every access through a heap buffer view pays for the stride-word load, the size load, the extent check and the selects, while about 360 views a frame are actually unaligned. The extra loads depend on the descriptor, so they lengthen the chains the card is already waiting on (long-scoreboard stalls dominate this title on NVIDIA; numbers in #4917). **Ideas, untested:** - Gate the remainder path per program: only programs that have been seen binding an unaligned view (the host logs them) get the extended lowering, through a re-translate, the way other per-program keys work. - Or skip the extent check and selects where the view is known aligned (remainder 0), so the common path is one extra load, not a load plus compares and selects. I'm not putting it on my play branch as it is, but the signs are worth fixing, and I'm happy to measure a revised version on the same scene.
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/descriptor/host.rs
  • crates/victory-gpu/src/shader_cache.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/4984/head:github-bot/gh-4983'
git switch 'github-bot/gh-4983'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-4983'
git switch 'github-bot/gh-4983'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-4983'
git switch 'github-bot/gh-4983'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-4983'
git switch 'main'
git merge --squash 'github-bot/gh-4983'
git switch 'main'
git merge --ff-only 'github-bot/gh-4983'
git switch 'main'
git merge 'github-bot/gh-4983'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/trustno1-coder--VIctory-NVIDIA!4984
No description provided.