Skip to content

R-7: sample with each immediate sampler a join picks - #56

Merged
ShinniUwU merged 2 commits into
ProjectLeonidaL:mainfrom
sanchaton:pr/r7-sampler-join
Nov 27, 2026
Merged

ShinniUwU merged 2 commits into
ProjectLeonidaL:mainfrom
sanchaton:pr/r7-sampler-join

Conversation

@sanchaton

@sanchaton sanchaton commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What changed

R-7 (partial): the 40 programs that failed with "emit: a descriptor chosen by control flow" now translate (37 pixel shaders: PS_TexturedLit*, PS_TexturedDeferred, PS_MirrorReflectionTexturedDeferred, PS_PuddleDetect; 3 mesh shaders: MST_DS_TerrainTransform, MST_DS_CascadeShadows_PNTriTexture). The join was between two immediate samplers (one clamping, one wrapping), and descriptor_phi only joins heap loads. The join now picks a static sampler number; the sample is made with each sampler it can be and its channels are selected by that number. Exact, at the cost of one more sample per affected read. A join of an immediate and a loaded sampler is still refused ("a sampler chosen by control flow").

How it was tested

  • Unattended runs: scripts\measure-run.ps1 before and after (Playtest below).

Checks run on this branch:

  • cargo test --workspace --locked: 656 passed, 0 failed, 18 ignored (run with -j 2 -- --test-threads=4)
  • cargo clippy --workspace --all-targets --locked -- -D warnings
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings
  • cargo xtask policy

New unit test: emit::lower::tests::a_sampler_chosen_by_control_flow_samples_with_each (a compute program picks one of two immediate samplers by a root constant; both are declared).

leonida translate --list --validate --jobs 2 with a release build:

Cause Before After
emit: a descriptor chosen by control flow 40 0

All 40 are emitted and pass the DXIL validator. The validator failures that remain (output signatures, mesh output over 32,768, out-of-bound coordinates) are on other programs; none of the 40 is among them.

Playtest

GPU and driver NVIDIA GeForce RTX 5060 Laptop GPU (8 GB), driver 32.0.16.1714
CPU, RAM Intel Core i7-13620H, 16 GB
Windows or Wine/Proton version Windows 11 Pro, build 26300
Console profile Series X
Where in the game Opening cutscene, from the start of a new game (the world is reached about 140 s in)
How long 4 runs of 330 s, alternated main / branch / main / branch; frames from 170 s on (about 27 frame lines per run)
Frame rate or frame time before (main f3c94ca) median 257.6 ms (3.9 fps) and 231.5 ms (4.3 fps); about 4,900-5,100 draws/frame
Frame rate or frame time after (this branch 12e25fa) median 256.5 ms (3.9 fps) and 238.3 ms (4.2 fps); about 4,900-5,000 draws/frame
  • Before and after were measured in the same place, and not on the first run after a shader change (translate --warm all on each build first)
  • No crash report was written and the device was not lost
  • The picture looks the same or better: the frame shots of both builds at the same point of the cutscene look the same.

The difference between the two builds (about 1 %) is smaller than the difference between the two main runs (26 ms), so the second sample has no cost that this measure can see on this machine. Per-part medians are close too: preparing 137.5 / 123.4 ms on main against 125.7 / 125.9 ms on the branch, GPU 55.5 / 62.9 ms against 58.8 / 58.7 ms.

One thing that went wrong, also on main: under Windows PowerShell 5.1, summary.txt reports lines: 0 and NaN for every run, because the colour codes are stripped with the `e escape that only PowerShell 7 knows, so no frame line matches. The figures above were taken from each run's stderr.log with the same patterns and the same 170 s start as the script. I will send that fix as its own PR.

AI

  • AI-assisted: investigation, fix, unit test, shader census and review.

@ShinniUwU

Copy link
Copy Markdown
Contributor

Two things before this can merge:

  1. In resampled, when call_with returns None the other sampler is skipped silently and the sample keeps the first one, so that path draws with the wrong sampler. Could that be an error instead ("a sampler chosen by control flow")? Same for anything else that takes the sampler handle, like LOD queries.
  2. This adds a second sample to PS_TexturedLit*, PS_TexturedDeferred and the cascade shadows, which run all the time. Could you do a scripts\measure-run.ps1 pair (main against this branch, after translate --warm all) and put the frame times in the PR?

Also: this and #55 both add to the top of "Newest" in docs/fork-notes.md, so the second one will need a quick rebase.

@sanchaton

Copy link
Copy Markdown
Contributor Author

Thanks, both points taken.

  1. Fixed in 10ebb6f. resampled now returns an error ("a sampler chosen by control flow") when the sample cannot be made again with one of the other samplers the join picks, instead of keeping the first one. A level-of-detail query (image_get_lod) on a picked sampler is refused the same way. The gather path that reads its four texels with textureLoad takes no sampler, so it keeps its path. cargo clippy -p leonida-shader --all-targets -- -D warnings is clean and cargo test -p leonida-shader gives 216 passed, 0 failed, 3 ignored.

  2. Agreed. I will do the scripts\measure-run.ps1 pair (main against this branch, after translate --warm all) and put the frame times in the Playtest section of the description. Until then, consider the cost of the second sample on PS_TexturedLit*, PS_TexturedDeferred and the cascade shadows unmeasured.

On docs/fork-notes.md: noted, I will rebase whichever of #55 and #56 is merged second.

@ShinniUwU

Copy link
Copy Markdown
Contributor

#54 and #55 are merged, so this now conflicts in docs/fork-notes.md. Please rebase on main. It still needs the scripts\measure-run.ps1 pair (main against this branch, after translate --warm all) in the Playtest section before it can merge.

A join of two immediate samplers (one clamping, one wrapping) was refused as a descriptor chosen by control flow: descriptor_phi only joins heap loads. The join now picks a static sampler number; the sample is made with each sampler it can be and its channels are selected by that number.
When the sample cannot be made again with another sampler a join picks, or a level-of-detail query takes a picked sampler, the program is refused as a sampler chosen by control flow instead of drawing with the first sampler. A gather's four texel reads take no sampler and keep their path. AI-assisted.
@sanchaton

Copy link
Copy Markdown
Contributor Author

Rebased on main (now 12e25fa on top of f3c94ca). The only conflict was the top of "Newest" in docs/fork-notes.md: the R-7 sampler note is first and every note from main is kept. The code change is line for line the one you reviewed (e0a9992 is the fix, 12e25fa the refusal of a picked sampler from 10ebb6f); lower.rs merged on its own with the POPS lines from #54.

The scripts\measure-run.ps1 pair (main against this branch, after translate --warm all) is still to come. The machine is running other game sessions for the next hour; the frame times will go in the Playtest section as soon as the pair is done, together with clippy and the tests on the rebased branch.

@sanchaton

Copy link
Copy Markdown
Contributor Author

The scripts\measure-run.ps1 pair is done and the frame times are in the Playtest section: 4 runs of 330 s (main, branch, main, branch) after translate --warm all on each build, Series X profile, RTX 5060 Laptop.

Median frame time from 170 s on: main 257.6 and 231.5 ms, this branch 256.5 and 238.3 ms. The gap between the builds is smaller than the gap between the two main runs, so the extra sample on PS_TexturedLit*, PS_TexturedDeferred and the cascade shadows has no cost I can measure here. No crash, no lost device, and the frame shots look the same.

Side note: under Windows PowerShell 5.1 summary.txt came out as NaN for every run (the `e escape is PowerShell 7 only), so I read the figures from stderr.log with the script's own patterns. I will open a separate PR for that.

@ShinniUwU
ShinniUwU merged commit 8560a6f into ProjectLeonidaL:main Nov 27, 2026
1 check passed
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.

2 participants