Skip to content

Shaders: a program's constant table starts after its instructions (GBs of NVIDIA video memory) - #2143

Open
desplmfao wants to merge 2 commits into
Marvisak:mainfrom
desplmfao:fix/code-table-after-instructions
Open

desplmfao wants to merge 2 commits into
Marvisak:mainfrom
desplmfao:fix/code-table-after-instructions

Conversation

@desplmfao

Copy link
Copy Markdown

Problem

On NVIDIA under Wine (vkd3d-proton), several GB of video memory went to the driver as soon as pipelines were created, and runs filled the 16 GB of an RTX 5080 while the world loaded. With all of a run's shaders dumped as SPIR-V and compiled natively with VK_KHR_pipeline_executable_properties, six compute shaders stood out by their local memory per thread:

shader local memory per thread × 129,024 resident threads (84 SMs × 48 warps × 32)
0b341b15… (SPIR-V name) 57,168 B ~7.0 GB
c4c847ce… 21,648 B ~2.7 GB
4 more 16.6–18.4 KB ~2.1 GB each
every other of the 2,435 shaders ≤ 256 B ≤ 32 MB

Each holds one large constant uint[] array indexed at runtime. That's the program's constant table from code_dword. vkd3d-proton turns it into a private array, NVIDIA keeps it in local memory, and the driver reserves local memory for the worst shader times all resident threads. Tables of 5–14 KB stayed out of local memory (0–16 B), so the cut-off seems to be around 16 KB. That's from the driver's statistics, not from a documented limit.

Why the tables were that big: code_dword built the table from the lowest code offset any load or descriptor names. A load at a computed offset names offset 0, so the table was the whole program: instructions plus constants, up to 64 KiB.

Change

The program's constants follow its instructions, and a load at a computed offset reads them, not instruction bytes. So the table now starts where the instructions end, unless a load at a fixed offset reads below that, in which case it starts where it did before. A computed index below the table's start is clamped like one past its end. The start is computed in code_table_start.

Measured

Grand Theft Auto VI, Series X 1.0.2.1:

  • Offline, all 26,084 programs: tables of 8 KiB or more go from 35 (25 of them ≥ 16 KiB, up to 64 KiB) to 3 (9.4 KiB each). Programs emitted are unchanged: 14,205 / 2,621 / 8,765.
  • In the game, on an RTX 5080, proton-cachyos 11.0 with vkd3d-proton 3.2.0. "Other" is the game's video memory (nvidia-smi) minus its host textures:
before after
menu: game video memory 8.1–11.3 GB, textures 4.4 GB 5.5 GB, textures 4.6 GB
menu: other 4–7 GB about 0.9 GB
loading the world past 15.5 GB in total, so every run stopped 12.0 GB for the game, 14.1 GB in total
reached the world (2,800 draws/frame) no yes, and it stayed

The run used some other open PRs to get that far on NVIDIA under Wine (#259, #1849, and #177's handle key), but none of them touches this.

Not covered / limits

  • If a program deliberately read its own instruction bytes at a computed offset, it would now read clamped values. I don't know of a compiler that emits that, and a fixed-offset read of the instructions keeps the old start.
  • Windows: NVIDIA's D3D12 driver apparently doesn't keep such arrays per thread (an RTX 4060 report in Test report: RTX 4060 Laptop (8 GB), the world hangs the GPU, and VRAM is full #1508 has menu video memory near the texture size), so the gain there should be small. The table is still tens of KiB smaller per shader. AMD drivers also put large runtime-indexed arrays in scratch memory, but I haven't measured that.

Tested

Related

AI-assisted (Claude Code).

🤖 Generated with Claude Code

The table code_dword reads was the program's code from the lowest offset
any load or descriptor names, and a load at a computed offset names
offset 0: the table was the whole program, up to 64 KiB. Under
vkd3d-proton that constant array becomes a private one, which NVIDIA's
Vulkan driver keeps in every thread's local memory, and the driver
reserves that per thread for the whole GPU: one 57 KiB table made it
reserve GBs of video memory.

The program's constants follow its instructions, and a load at a
computed offset reads them, so the table now starts where the
instructions end, unless a load at a fixed offset reads below that.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@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 locally merged onto origin/main (940d32f, Windows 11 x64, Rust 1.98.1 MSVC):

  • cargo test -p victory-shader --lib the_code_table_starts_after_the_instructions — passed
  • cargo clippy -p victory-shader --all-targets — 0 warnings
  • cargo xtask policy — ok (183 files ok)

Clean fix starting the emitted code constant table after the program's instructions rather than at offset 0, avoiding huge wasted constant-buffer/VRAM allocations on NVIDIA. Looks great.

AI-assisted (Gemini 4 Neon).

@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: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, cargo test -p victory-shader --locked.

145 passed / 0 failed / 2 ignored. Program constant table starts after its instructions — constant-table layout fix.

oceansde012 pushed a commit to oceansde012/victory-gta6 that referenced this pull request Nov 25, 2026
…s of NVIDIA video memory)

Upstream: Marvisak/VIctory#2143 (import list and test section merged by hand)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FLuVRkAEvF29hMbrBJw8os
(cherry picked from commit 9c6a651)
oceansde012 pushed a commit to oceansde012/victory-gta6 that referenced this pull request Nov 25, 2026
…s of NVIDIA video memory)

Upstream: Marvisak/VIctory#2143 (import list and test section merged by hand)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FLuVRkAEvF29hMbrBJw8os
(cherry picked from commit 9c6a651)
(cherry picked from commit 8aaf44a)
@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

I merged head 524e283 onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. In crates/victory-shader/src/emit/lower.rs, code_dword used to start the program's constant table at the lowest CODE offset any load or descriptor names. A computed load names offset 0, so the table was the whole program. The new code_table_start starts it at the end of the instructions (the highest machine.offset + 4·words in the IR), unless a fixed-offset load reads below that, in which case the old start is kept.

I checked the edge cases in code_dword. A computed byte below the new start makes byte - base wrap, so it is clamped to the last dword by the existing IUGT select, as the description says. Fixed-offset reads inside the instructions keep the old behaviour. the_code_table_starts_after_the_instructions covers these cases. The offline count on the title's shaders (no change in emitted programs, and 23,918 validated both before and after in @sinan-cmyk's run) supports that nothing regresses.

A small optional point: the doc comment on code_table_start could drop the vkd3d-proton/NVIDIA background and keep only what the function does. CI: passing (check).


Generated by Claude Code

As suggested in the triage: the vkd3d-proton/NVIDIA background stays in
the pull request's description.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@desplmfao

Copy link
Copy Markdown
Author

Thanks. I took the optional point in a68ae55: code_table_start's comment now only says what the function does, and the vkd3d-proton/NVIDIA background stays in the description. No code change. Clippy is clean.

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.

5 participants