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

Merged
Kal0s-design merged 1 commit from upstream-pr/2766 into main 2026-12-01 04:53:29 +00:00
Kal0s-design commented 2026-11-30 16:59:10 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-11-30T16:59:10Z.


Claimed in the coordination issue as: D3D12-GPU-SAMPLER-HEAP-2048 (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7276016092)

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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5569845038 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2766). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-11-30T16:59:10Z. --- Claimed in the coordination issue as: D3D12-GPU-SAMPLER-HEAP-2048 (https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7276016092) ### 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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] 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](https://claude.com/claude-code)
KelboLive commented 2026-11-30 18:14:47 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-11-30T18:14:47Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7287561897 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2766#issuecomment-7287561897). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-30T18:14:47Z. --- 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 commented 2026-12-01 03:16:27 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T03:16:27Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7299530580 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2766#issuecomment-7299530580). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-12-01T03:16:27Z. --- **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 commented 2026-12-01 04:53:36 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-12-01T04:53:36Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7301209770 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2766#issuecomment-7301209770). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-12-01T04:53:36Z. --- 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 commented 2026-12-01 05:07:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T05:07:19Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6125477810 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2766#pullrequestreview-6125477810). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-12-01T05:07:19Z. **Review:** COMMENTED --- 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 commented 2026-12-01 10:25:10 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by z3r0rc on 2026-12-01T10:25:10Z.


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?

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7308160884 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2766#issuecomment-7308160884). Originally posted by [z3r0rc](https://github.com/z3r0rc) on 2026-12-01T10:25:10Z. --- 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 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
ProjectLeonida/mandy!2766
No description provided.