Skip to content

d3d12: give host sampler heaps every slot the shaders can reach (D3D12-GPU-SAMPLER-HEAP-2048) - #2766

Merged
OreoFR merged 1 commit into
ProjectLeonida:mainfrom
Kal0s-design:fix/sampler-heap-2048
Dec 1, 2026
Merged

OreoFR merged 1 commit into
ProjectLeonida:mainfrom
Kal0s-design:fix/sampler-heap-2048

Conversation

@Kal0s-design

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-GPU-SAMPLER-HEAP-2048 (#2570 (comment))

What and why

Shaders clamp a sampler index to 2,047. leonida-shader emit/shader.rs notes this is "not the bound heap's own count, which a title heap smaller than 2,048 would need". But HostHeap::new (d3d12/crates/leonida-gpu/src/descriptor/host.rs) made a host sampler heap with exactly the title's count. With a title sampler heap of fewer than 2,048 samplers, an index between its count and 2,047 read past the end of the host heap. Such an index comes from a number read out of memory the host hasn't got yet, which happens while streaming.

The change:

  • A host sampler heap always has 2,048 slots. fill_null already makes every slot a null point sampler, so the slots past the title's count read as that.
  • The title's own count is kept (capped at 2,048), so nothing that walks the title's slots changes.
  • Unlike the sibling tree's version, slot 2,047 is not taken from a title heap that uses it: any clamped index now lands inside the heap anyway.

This ports ESAT's fix from the sibling VIctory tree (TaynanGT/Vice-Tay dd67ae7, GPL-3.0).

Tested

  • New test descriptor::host::tests::sampler_heaps_have_every_slot_the_shaders_can_reach: a 16-sampler title heap gets 2,048 host slots and keeps count 16, and a 2,048 one keeps 2,048. On main (7663c34fc) it fails (a 16-sampler heap backs 2,048 slots, left: 16, right: 2048); with the change it passes.
  • cargo test -p leonida-gpu --lib: 573 passed, 0 failed, 52 ignored (main: 572).
  • cargo clippy -p leonida-gpu --lib --tests: no warning in the changed lines.
  • Windows only, unit tests; the title wasn't run.

GPUs tested: the test makes its heaps on the local hardware D3D12 device; no game run.

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

Shaders clamp a sampler index to 2,047 (leonida-shader emit/shader.rs:
"not the bound heap's own count, which a title heap smaller than 2,048
would need"), but HostHeap::new made a sampler heap with exactly the
title's count. With a title sampler heap of fewer than 2,048 samplers, an
index between its count and 2,047 (a number read from memory the host has
not got yet) read past the end of the host heap.

A host sampler heap now always has 2,048 slots; fill_null already makes
every slot a null point sampler, so the slots past the title's count read
as that. The title's own count is kept (up to 2,048): unlike the sibling
tree's version, slot 2,047 is not taken from a title heap that uses it,
since any clamped index now lands inside the heap anyway.

Test: sampler_heaps_have_every_slot_the_shaders_can_reach (a 16-sampler
title heap gets 2,048 host slots and keeps count 16; a 2,048 one keeps
2,048). On main it fails: the 16-sampler heap has 16 slots.

After ESAT's fix in the sibling VIctory tree (TaynanGT/Vice-Tay dd67ae7,
GPL-3.0).

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

Copy link
Copy Markdown

Independent check on Windows (not the author), at e7b9959c5.

  • cargo test --locked -p leonida-gpu --lib (shared warm target): 573 passed, 0 failed, 52 ignored in 13.9 s.

Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, NVIDIA RTX 4070 Ti SUPER (driver 621.01), Rust 1.98.1 pinned.

AI-assisted: yes (opencode).

@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Verified on Windows, for a merge (not the author). AI-assisted (Claude Code agent oreofr-agent). RTX 4070 Laptop 8 GB, NVIDIA 621.40, Core Ultra 7 155H, Windows 11 Pro, Rust 1.98.1 pinned, cargo test --locked.

Head e7b9959c5 merged into current main 90c514437 (clean), tested in one batch with #2589, #2766, #2709 and #2802 (main + all four, merged in that order, clean).

  • Red by mutation: with the sampler path turned off (if false in place of if sampler), sampler_heaps_have_every_slot_the_shaders_can_reach fails on this machine (it has a device, so the test checks the heaps). The mutation was reverted after the run.
  • Green / neighbours: leonida-gpu main 589 passed / 0 failed, main + the four 598 / 0; leonida-hle 242 / 0 on both.
  • In-game run of the batch (diagnostics,mimalloc, no DLSS), fresh copy of our gate data, -SeriesS -NoDlss -NoFrameGen, BAR 0, texture budget 1024, walking (keys file, 115 key changes): 330 s in the world, 0 device loss, 0 ERROR lines, no crash, clean close. Early stalls while ~169 pipelines were first made (as on main), then steady 21-23 fps (median of the last 30 fps lines 21.6), 24 GB private at the end.

One run of several pull requests together covers each at its head (docs/maintaining.md, #2797); if a merge of one of them breaks main, the others are split out and re-run.

@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Merging (after #2589). Head e7b9959c5 merges cleanly with main 5dd6d444f (merge commit 10bed9ff2).
Claim: in the description, a comment in #2570 by the author (checked).
Red/green: by oreofr-agent (not the author), RTX 4070 Laptop, Windows 11: red by mutation on this head (comment above). I ran on main 86c30fcea + #2589, #2766, #2709, #2802 (this order): cargo test --locked -p leonida-gpu 599 passed, 0 failed (main alone 590/0); -p leonida-hle 242/0. An independent second check (separate agent) found no blocker.
In-game run: RTX 4070 Laptop 8 GB, NVIDIA 621.40, the four together at their heads on 90c514437: 330 s in the world walking, 0 device loss, 0 ERROR lines; same-scene picture against main: no visible change. Since then main only gained the two CI fixes (#2825, #2827: a test-module declaration and a tool's __main__), nothing that runs in the game. Default change with no switch: every host sampler heap now has 2,048 slots (a revert undoes it).
CI: green on the head (5/5) and on main (CI, No Game Data on 86c30fcea; Runtime last green on cd9fe51a9).
AI-assisted: yes, Claude Code agent oreofr-agent, merged on OreoFR's (maintainer) written instruction.

@KelboLive KelboLive 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 here: the new test passes and fails with the old sizing (left: 16, right: 2048), and full cargo test -p leonida-gpu --lib is 573 passed / 0 failed / 52 ignored.

The sizing is exactly right: the shader clamp is umin(index, 2047) (emit/shader.rs:650), so 2,048 slots is the reachable set, last slot included, no off-by-one; mirror refuses index >= self.count (:4544), samplers_again loops 0..self.count (:4521), and sync_heaps uses min(title, host) (descriptor.rs:411), so a title heap larger than 2,048 cannot write past it. Cost measured here: 32 B per sampler descriptor, 64 KiB per shader-visible sampler heap, made once per title heap through the OnceLock (descriptor.rs:97) — noise against a 10-13 GB run. No root-table or budget change.

Two test gaps worth closing, because the second is the PR's whole point:

  1. Nothing asserts that slots past the title's count are the null point sampler — passing count instead of size to fill_null (descriptor.rs/host.rs:2442) still passes the test while leaving those slots undefined.
  2. Nothing covers the 2,047 boundary or a title heap larger than 2,048 (the min cap). The test also returns silently when no D3D12 device exists, so a GPU-less run passes vacuously.

For the record, this does not touch the fallback we see in the logs: slot=368463 is the view heap (sized to the view limit, one million slots on this device), so those misses are store-marking, not a 2,048 sampler bound — that is #2831's territory.

@z3r0rc

z3r0rc commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

finding: fill_null's null POINT samplers in past-count slots change visible pixels in GTA VI — faces go to noise

Where: d3d12/crates/leonida-gpu/src/descriptor/host.rs fill_null (~:2451) — the 2048-slot heap from e7b9959c5.

Evidence: past the title's sampler count the slots get POINT-filtered null samplers, and character faces (which sample past-count slots) render as noise; making null samplers LINEAR (local arm LEONIDA_NULL_SAMPLER_LINEAR=1, host.rs ~:2512) brings them back — live A/B, same exe, Wine + vkd3d-proton master + RTX 4090. #2766's tests check slot counts, not what past-count sampling looks like; vkd3d's pre-#2766 default read as linear-ish and our older build proved that OK.

Question for the maintainers: what should past-count slots hold — a linear-ish default, or is past-count sampling considered title error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants