Skip to content

GPU: on NVIDIA, structured buffer views are made raw - #2464

Open
lhnizdo wants to merge 1 commit into
Marvisak:mainfrom
lhnizdo:pr/structured-scalar-loads
Open

lhnizdo wants to merge 1 commit into
Marvisak:mainfrom
lhnizdo:pr/structured-scalar-loads

Conversation

@lhnizdo

@lhnizdo lhnizdo commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

On NVIDIA hosts, a title SRV that holds a structured buffer view is now made as a raw view of the same bytes.

The problem. Scalar and offset-only buffer reads are translated as raw reads: rawBufferLoad through a raw handle, at a byte offset. When the slot holds the title's structured view, D3D12 leaves that read undefined, and NVIDIA took the byte offset as the element.

WindNew:CS_CopyLatestWindFieldForRenderingWithSmooth reads its loop counts that way, from t71 (680-byte records, offsets 0 and 0xC4). With other records' data as counts, its loops ran until Windows reset the device. That happened every run, in the opening credits, on Windows 10 with an NVIDIA wave32 card. DRED named that dispatch.

The change (descriptor/host.rs, buffer_srv):

  • On an NVIDIA host (vendor 0x10DE), a structured SRV becomes a raw view over the same bytes. Raw views must start on 16 bytes, so a view that wouldn't stays structured and is logged once.
  • Raw reads then read what they name.
  • Reads by element keep their structured handle, which NVIDIA and WARP read right on a raw view. AMD reads element 0 there, so other hosts are unchanged.
  • VICTORY_RAW_STRUCTURED=0 or =1 overrides the vendor check.
  • host::host_vendor() reads the device's adapter vendor once, by its LUID.

Why not in the shaders. The first version of this PR read such slots both ways in the shader and picked the result by the slot's stride. That made the largest shaders too much for the NVIDIA driver's compiler. I checked with translate --pipelines, filtered to the CS_RaytracingResolve and skinning mesh shaders:

  • Without shader changes: 0 of 4,683 pipelines refused.
  • With the both-ways reads: 569 refused with E_OUTOFMEMORY, and their draws dropped in game.

This version leaves the shaders unchanged.

Checked on Windows 10 with an NVIDIA card, together with the Agility SDK (#2460), heap-limit (#2461) and UAV-format (#2462) PRs:

  • A game run of almost 9 minutes, through the opening credits into the prologue, with no device removal and no loop cap. Before, every run hung in the credits.
  • 0 pipelines refused for lack of memory.
  • Clippy is clean. The victory-gpu tests are unchanged against main.

🤖 Generated with Claude Code. The change was written by Claude Code (Claude Opus 5.5) in my session; the commit carries a Co-Authored-By: Claude line.

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified against origin/main (940d32f) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP:

  • Structured scalar loads lowering builds cleanly and passes all lower tests.
  • cargo test -p victory-shader emit::lower::tests: 21/21 passed on RTX 5090, 21/21 passed on WARP.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

@lhnizdo

lhnizdo commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Heads-up before merging. In a game run, reading table SRVs both ways made the largest shaders (the materials' CS_RaytracingResolve variants and some deferred pixel shaders) too big for the NVIDIA driver's compiler: 145 pipelines were refused with E_OUTOFMEMORY and their draws dropped. I have a follow-up commit that looks the slot's stride up once per descriptor and block, and splits constant offsets at translation time. I'll push it once translate --pipelines confirms the refusals are gone. Please hold this one until then.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally: merged into main 940d32f, cargo test -p victory-shader --locked on macOS arm64 — 144 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — structured scalar loads.

@lhnizdo
lhnizdo force-pushed the pr/structured-scalar-loads branch from 7358e04 to 238286d Compare November 25, 2026 16:37
@lhnizdo lhnizdo changed the title Pr/structured scalar loads GPU: on NVIDIA, structured buffer views are made raw Nov 25, 2026
@lhnizdo

lhnizdo commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Update: I've replaced this PR's approach, so the branch was force-pushed.

As I mentioned above, reading table SRVs both ways in the shader made the largest shaders too big for the NVIDIA driver's compiler. A filtered translate --pipelines run on the CS_RaytracingResolve and skinning mesh shaders gave 569 pipelines refused with E_OUTOFMEMORY, against 0 without the shader change.

The PR now fixes it on the host instead: on NVIDIA, a structured SRV is made as a raw view of the same bytes. Raw reads then read what they name, reads by element keep their structured handle (which NVIDIA reads right on a raw view), and the shaders are unchanged. Other vendors are unchanged too.

In a game run with it, the opening credits played through into the prologue with no device removal and no loop cap, and no pipelines were refused for lack of memory. Details are in the updated description. The earlier approvals were for the old version, so a fresh look would be appreciated.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs work: needs the maintainer's decision, plus one small change. The head is unchanged (b9dc4b0). steven-seagull27's synthetic readback on an RTX 5080 confirms both the fix and its limit. Through 16-byte-aligned SRV origins, wrong dwords drop from 950/960 to 0/960. Unaligned origins (12, 24, 36 bytes) still misread, because buffer_srv in descriptor/host.rs keeps those views structured.

On your reply: #2885 is also open and makes structured SRVs and UAVs raw, with aligned copies for unaligned origins. The maintainer should pick one. If #2885 is chosen, this can close in its favour, as you offer. Until then, the earlier points stand:

  • The fix relies on vendor-specific driver behaviour, gated by host_vendor() == 0x10DE. That needs the maintainer's decision.
  • The commit message still names one of the title's shaders, its register and record size. Please reword it per docs/asset-policy.md, or the maintainer can squash with a new message.

CI: check passing.


Generated by Claude Code

@kotekompotek

Copy link
Copy Markdown

Mechanics check out: gated to stride > 0 so raw/typed views are untouched; guards byte % 16 == 0 (raw-view alignment) and stride % 4 == 0 (so count*stride/4 is exact) before converting to R32_TYPELESS + RAW with dword FirstElement — the fallback stays structured and logs once. Vendor detection via EnumAdapterByLuid is the right way (not name matching).

The hardware rationale is plausible — a byte-offset read through a structured SRV on NVIDIA indexing by element instead would return wrong records, which explains the wind-sim loop-count corruption. WARP already behaving correctly on raw views makes the raw path the safer default on the hosts that get it right.

Note: a structured view whose first*stride isn't 16-aligned keeps the buggy NVIDIA behaviour — unavoidable without a copy, but worth remembering if a title hits it.

juanfariasdev pushed a commit to juanfariasdev/VIctory that referenced this pull request Nov 25, 2026
# Conflicts:
#	crates/victory-gpu/src/host.rs
MagiccConch added a commit to MagiccConch/VIctory that referenced this pull request Nov 25, 2026
Port of upstream PR Marvisak#2464 (claude-GTA6, AI-assisted, design from
fxceless/victory-fork's raw_window). FIND-11 / upstream Marvisak#1744: a scalar
or offset-only buffer read is translated as a rawBufferLoad at a byte
offset; when the slot holds the title's structured view, D3D12 leaves
that read undefined and NVIDIA took the byte offset as the element,
so WindNew's loop counts came from other records' data and its loops
ran until the device was reset (opening-credits hang on NVIDIA).

buffer_srv: on an NVIDIA host a structured SRV becomes a raw view
(R32_TYPELESS + RAW) over the same bytes when they start on 16 bytes;
otherwise it stays structured and logs once. VICTORY_RAW_STRUCTURED
overrides the vendor check. host_vendor() reads the adapter vendor
once by LUID.

Shaders are unchanged: reads by element keep their structured handle,
which NVIDIA and WARP read right on a raw view (AMD reads element 0,
so other hosts are unchanged). No shader-cache or pipeline-library
rebuild is needed.

Co-authored-by: openhands <openhands@all-hands.dev>
@moe-miau

Copy link
Copy Markdown

Tested on an RTX 4070 SUPER (driver 32.0.16.1742, Windows 11 26300), title 1.0.2.1, Series X profile, diag build. 6-minute runs with scripted Enter presses. The stack is main 67e8ae1 + #1906 + #2098 + #2626 (RGBA8 stand-in for formats 86/115) + #2628 (capped view heap). This PR was merged on top; the only conflict was in host.rs, where both sides add a function in the same place.

device removed fps picture
stack without this PR no 0.1 (about 7.5 s a frame) black after the loading screen
stack + this PR no 0.2–1.6, mostly about 1 the world draws: night city during the opening credits, then the sunset harbour (overexposed, see below)
  • The log shows host adapter vendor vendor=0x10de and structured buffer views made raw on=true. One view stayed structured: not on 16 bytes byte=168744600 stride=100.
  • I also tried making structured UAVs raw the same way (buffer_uav, same 16-byte rule). That gave no visible difference in a 6-minute run, so I don't think this PR needs it.
  • What's still wrong in the picture: heavy overexposure and blocky bands. That fits the 141–162 WaveSize(64) compute pipelines NVIDIA still refuses here (exposure and lighting), not this PR.

AI-assisted (Claude Code); the runs were done on my machine.

Scalar and offset-only buffer reads are translated as raw reads
(rawBufferLoad through a raw handle, at a byte offset). On a slot that
holds the title's structured view, D3D12 leaves that undefined, and
NVIDIA took the byte offset as the element: the wind simulation's loop
counts (WindNew CS_CopyLatestWindFieldForRenderingWithSmooth, t71,
680-byte records) were other records' data, and its loops ran until
the device was reset, every run, in the opening credits.

On NVIDIA hosts a structured SRV is now made as a raw view of the same
bytes (starting on 16 bytes, as raw views must; others stay
structured). Raw reads then read what they name, and reads by element
keep their structured handle, which NVIDIA (and WARP) read right on a
raw view. AMD reads element 0 there, so other hosts are unchanged;
VICTORY_RAW_STRUCTURED=0 or =1 overrides.

This replaces reading such slots both ways in the shaders, which made
the largest shaders too much for the NVIDIA driver's compiler (569
pipelines refused with E_OUTOFMEMORY, none without it).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018sSQTeDQ6Kc6DEkgPbAww8
@lhnizdo
lhnizdo force-pushed the pr/structured-scalar-loads branch from 238286d to b9dc4b0 Compare November 25, 2026 18:07
@lhnizdo

lhnizdo commented Nov 25, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @kotekompotek for the review, and @moe-miau for the RTX 4070 SUPER run. A black screen at 0.1 fps turning into the world drawing at about 1 fps is a clearer result than mine.

  • Rebased onto current main (13f91ca). The conflict was the one moe-miau saw: both sides add a function at the same place in host.rs, and both are kept.
  • The view that stays structured (byte=168744600 stride=100, not on 16 bytes) shows up in my runs too. As kotekompotek says, it keeps the old NVIDIA behaviour, and fixing it would need a copy. I've left it as a logged shortcut.
  • Structured UAVs: I'll leave those out, given moe-miau saw no difference with them.

@frenziedcoder

Copy link
Copy Markdown

RTX 5080 (Blackwell), driver 621.40: @moe-miau's stack with this PR draws the world here too, at about 1.5–2.5 fps, and ran the full 10 minutes with no device removal.

Setup: NVIDIA GeForce RTX 5080 16 GB (driver 621.40, 32-lane waves), AMD Ryzen 7 9800X3D, 64 GB RAM, Windows 11 Pro 26200. Title 1.0.2.1, Series X profile, diag build with --features diagnostics, VICTORY_D3D12_DRED=1, scripted Enter from 15 s to 280 s. Shader cache and pipeline library made beforehand with translate --cache --pipelines on each tree.

Tree: main 13f91ca + #1906 (a554b15) + #2098 (33a65d1) + #2626 (d1dc7ad) + #2628 (46d530a) + this PR (b9dc4b0), merged in that order. Two conflicts, both in descriptor/host.rs and neither in code: #2098's doc comment on write_address against #1906's (merged), and #2628's two new tests at the same spot as #1906's (both kept).

Runs, one each, 600 s limit:

tree end how far
main 13f91ca 0x887A0001 at 9 s menus; just before it no host texture … format: 115 (2560x1440 render target), as in #1952 / #1781
main + #2626 0x887A0006 at 85 s menus at 30 fps, then the loading screen; ~40 compute pipelines refused (0x80070057) as the world loads, then a hang in the list that was running
the stack above no removal, stopped at the 600 s limit world's first frame (1,002 draws) at frame 1,191, 47 s in; frame 2,151 at 523 s

With the stack:

  • The world draws: lit night streets and later a daylight outdoor scene, with blocky and over-bright patches like those @moe-miau describes on the 4070 SUPER.
  • Frame rate in the world: 240 frames in 153 s, then 240 in 95 s, 377 in 152 s and 103 in 76 s (about 1.5–2.5 fps). 72 slow host submissions (median 334 ms, longest 1,628 ms).
  • 171 host pipelines refused, all 0x80070057 (WaveSize(64) on a 32-lane host; @moe-miau had 166), and 8 programs whose draws are dropped because they don't translate.
  • Peak video memory 11.6 GB (whole GPU); private memory never left less than 13 GB free.

translate --cache --pipelines on this card: 25,752 of 25,985 programs translate (233 refused); of the pack's 28,164 pipelines, 23,112 made and 3,624 already in the library in 770 s with --jobs 12, 397 refused by the host (the same count on main and on the stack, close to the 394 in #2220).

Not tested: more than one run per tree; #2176 / #189 or #2118 on top; Series S profile; release build.

AI use: Claude Code (Claude Opus 5.5) merged the trees, ran the title on my machine (sandboxed at Low integrity, game folder read-only), read the logs and frame captures, and wrote this report.

@steven-seagull27

Copy link
Copy Markdown

I tested the default vendor gate on this PR and its direct parent with an overlapping-view GPU write/readback on an RTX 5080 (driver 621.40) and built-in WARP. On the RTX, this change fixed every read through the two 16-byte-aligned SRV origins. The three unaligned origins reproduced the parent’s results.

Adapter and revision SRV origins 0 and 48 bytes SRV origins 12, 24 and 36 bytes Total wrong dwords
RTX 5080, parent 13f91ca 950/960 1,425/1,440 2,375/2,400
RTX 5080, PR b9dc4b0 0/960 1,425/1,440 1,425/2,400
WARP, parent and PR (each) 0/960 0/1,440 0/2,400

Five 12-byte-stride structured UAVs at different origins write into one allocation in separate trials. For each trial five overlapping SRVs read three fields across 32 lanes. With the writer at byte 0 and reader at byte 12, lane 1 of the first field read 0xa170000c instead of 0xa1700006. The unaligned reads diverged even on fields the writer left untouched. WARP reported the raw-view gate off and the RTX reported it on.

The test-only commit on this exact PR head is available if you want to compare other adapters. With VICTORY_RAW_STRUCTURED unset, run this twice in separate processes, setting VICTORY_D3D12_WARP to 0 and 1 respectively.

cargo test --locked -p victory-gpu --lib --all-features gpu_writes_visible_through_overlapping_views -- --ignored --nocapture --test-threads=1

For the merge comparison, the same fixture on #2178 gave 544/2,400 on each adapter. Those mismatches were confined to newly written words read through a different origin. These are synthetic readbacks, independent of the in-game frame-rate runs above.

AI-assisted with Codex.

@lhnizdo

lhnizdo commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @steven-seagull27, that's a clean measurement, and it shows exactly this PR's limit. A raw view has to start on 16 bytes, so buffer_srv only makes a structured view raw when first * stride % 16 == 0. Views at 12, 24 or 36 bytes stay structured and keep NVIDIA's misread, as your table shows.

#2885 goes further: a view off 16 bytes gets its own copy at an aligned start, and the table-bound path is lowered through raw handles too. So it should cover your unaligned origins as well. If the maintainers prefer #2885, this PR can be closed in its favour. Your fixture would be a good check of #2885's unaligned case.

lhnizdo added a commit to lhnizdo/VIctory that referenced this pull request Nov 26, 2026
Measured in the world on an RTX 2080 (1,000,000-view cap), `main` +
Marvisak#2460 Marvisak#2461 Marvisak#2464 and local work, 10 minutes each from a save that
loads into the world: with 65,533 dynamic slots the host ran out of
them and dropped draws (512 and more, and root views); with this PR's
layout before (721,469 title slots, 262,144 dynamic) none were dropped,
and no draw read a title slot past the kept ones (523,715 left out, all
for_draw=false). Back to that layout until a scene needs more title
slots.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018sSQTfPXZpSkjMMNa7RB5G
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants