Shader: a compute sample takes its derivatives from the quad, not level 0 #591

Open
github-bot wants to merge 3 commits from github-bot/gh-591 into main AGit
Owner

@⁠oxybes on GitHub · 2026-12-04 08:48 UTC · original
oxybes:pr/sample-grad → main · +535 −13 · 4 files · 3 commits · draft

lower.rs documents a shortcut: a sample with an implicit level outside a pixel shader reads mip level 0. In this title that is about 730 compute programs - the sky, the screen-space reflections and the occlusion resolves - and it shows as shimmering gradients at the horizon and dirty reflections on water.

A compute program's lanes are threads in the group's linear order, quads are lanes 0-3, 4-7 and so on, and the console's quad hardware computes exactly these derivatives (host waves hold 32 or 64 lanes and every base this emitter relabels is 4-aligned, so a quad never straddles a wave - including the wave64 split into two 32-lane waves).

The emitter now reads each coordinate in the neighbouring lane across x and y (dx.op.quadOp.f32, kinds 0 and 1), takes the difference against its own lane, and samples with dx.op.sampleGrad.f32 (three gradient slots per axis, undef in the unused one, as the DXIL signature requires). Mip offsets and the clamp are preserved.

Level 0 stays where the lanes cannot be read:

  • a stage without lanes (vertex, geometry, tessellation, mesh);
  • a group of fewer than four threads, or one whose size is not a multiple of 4 (the tail quad would be half-filled: quadOp on lanes 4 and 5 of a 6-thread group reads lanes 6 and 7, which belong to the next group);
  • a sample inside a region a per-lane branch reaches (lane_blocks), where the neighbouring lanes execute different code.

Behind VICTORY_SAMPLE_GRAD, on unless the value is exactly 0. The switch is part of the translator cache key, so both states keep their own translations.

Measured

RTX 5070, Windows 11, Series S profile, DLSS Quality, frame generation on, one route, both states fully warmed, window in front; medians per draws/frame slice:

VICTORY_SAMPLE_GRAD 4-6k 6-8k 8-10k 10-12k
0 (level 0) 39.6 (GPU 7.4) 46.2 (GPU 16.7) 53.6 (GPU 11.2) 57.4 (GPU 16.1)
on (sampleGrad) 35.4 (GPU 5.5) 41.9 (GPU 9.6) 46.3 (GPU 6.9) 49.3 (GPU 6.2)

The GPU time on those slices nearly halves: level 0 was fetching unfiltered texels, so the gain is bandwidth, not a trade against quality. The sky at the horizon and the water's reflections were checked by eye in both states - no regular grid pattern (which would mean the quad pairing is wrong), no over-blur (a level too high), no popping.

Both sessions: zero warnings or errors, the same drop categories.

Testing

Four emitter tests:

  • a_compute_sample_reads_its_gradients_from_the_lanes_across: a 64-thread compute program declares dx.op.sampleGrad.f32 and dx.op.quadOp.f32, not sampleLevel; the module is validated with dxil.dll and a compute PSO is created on hardware.
  • a_wave64_group_of_64_takes_the_grad_form_from_its_quads: with the wave64 relabelling to 32-lane host waves, the quad reads pair lanes 0/1 and 2/3 (quad_reads == [0, 0, 1, 1]), never across a wave.
  • the_switch_keeps_the_level_zero_sample, a_group_without_a_quad_keeps_the_level_zero_sample ([1,1,1] and [6,1,1]), four_threads_are_one_quad_and_take_the_grad_form, a_two_by_two_group_takes_the_grad_form.
  • a_sample_in_a_per_lane_branch_keeps_the_level_zero_sample and a_pixel_shaders_sample_is_unchanged.

cargo test -p victory-shader: 338 passed.

Notes for review

  • The test-only accessors Module::function_names and Lowered::functions are #[cfg(test)].
  • A group whose x size is not a multiple of 4 can pair a row's end with the next row's start on both the host and the console; that is the console's own quad layout, not a host divergence, and the fix does not change it.
  • Cube samples take differences of the cube direction, so a face seam can raise the level slightly; a NaN/inf coordinate now poisons the gradient where level 0 was immune.
**@⁠oxybes** on GitHub · 2026-12-04 08:48 UTC · [original](https://github.com/readmet3xt-lab/foxhound/pull/591) `oxybes:pr/sample-grad` → `main` · +535 −13 · 4 files · 3 commits · draft `lower.rs` documents a shortcut: a `sample` with an implicit level outside a pixel shader reads mip level 0. In this title that is about 730 compute programs - the sky, the screen-space reflections and the occlusion resolves - and it shows as shimmering gradients at the horizon and dirty reflections on water. A compute program's lanes are threads in the group's linear order, quads are lanes 0-3, 4-7 and so on, and the console's quad hardware computes exactly these derivatives (host waves hold 32 or 64 lanes and every base this emitter relabels is 4-aligned, so a quad never straddles a wave - including the wave64 split into two 32-lane waves). The emitter now reads each coordinate in the neighbouring lane across x and y (`dx.op.quadOp.f32`, kinds 0 and 1), takes the difference against its own lane, and samples with `dx.op.sampleGrad.f32` (three gradient slots per axis, `undef` in the unused one, as the DXIL signature requires). Mip offsets and the clamp are preserved. Level 0 stays where the lanes cannot be read: - a stage without lanes (vertex, geometry, tessellation, mesh); - a group of fewer than four threads, or one whose size is not a multiple of 4 (the tail quad would be half-filled: `quadOp` on lanes 4 and 5 of a 6-thread group reads lanes 6 and 7, which belong to the next group); - a sample inside a region a per-lane branch reaches (`lane_blocks`), where the neighbouring lanes execute different code. Behind `VICTORY_SAMPLE_GRAD`, on unless the value is exactly `0`. The switch is part of the translator cache key, so both states keep their own translations. ## Measured RTX 5070, Windows 11, Series S profile, DLSS Quality, frame generation on, one route, both states fully warmed, window in front; medians per `draws/frame` slice: | `VICTORY_SAMPLE_GRAD` | 4-6k | 6-8k | 8-10k | 10-12k | |---|---|---|---|---| | `0` (level 0) | 39.6 (GPU 7.4) | 46.2 (GPU 16.7) | 53.6 (GPU 11.2) | 57.4 (GPU 16.1) | | on (`sampleGrad`) | 35.4 (GPU 5.5) | 41.9 (GPU 9.6) | 46.3 (GPU 6.9) | 49.3 (GPU 6.2) | The GPU time on those slices nearly halves: level 0 was fetching unfiltered texels, so the gain is bandwidth, not a trade against quality. The sky at the horizon and the water's reflections were checked by eye in both states - no regular grid pattern (which would mean the quad pairing is wrong), no over-blur (a level too high), no popping. Both sessions: zero warnings or errors, the same drop categories. ## Testing Four emitter tests: - `a_compute_sample_reads_its_gradients_from_the_lanes_across`: a 64-thread compute program declares `dx.op.sampleGrad.f32` and `dx.op.quadOp.f32`, not `sampleLevel`; the module is validated with `dxil.dll` and a compute PSO is created on hardware. - `a_wave64_group_of_64_takes_the_grad_form_from_its_quads`: with the wave64 relabelling to 32-lane host waves, the quad reads pair lanes 0/1 and 2/3 (`quad_reads == [0, 0, 1, 1]`), never across a wave. - `the_switch_keeps_the_level_zero_sample`, `a_group_without_a_quad_keeps_the_level_zero_sample` (`[1,1,1]` and `[6,1,1]`), `four_threads_are_one_quad_and_take_the_grad_form`, `a_two_by_two_group_takes_the_grad_form`. - `a_sample_in_a_per_lane_branch_keeps_the_level_zero_sample` and `a_pixel_shaders_sample_is_unchanged`. `cargo test -p victory-shader`: 338 passed. ## Notes for review - The test-only accessors `Module::function_names` and `Lowered::functions` are `#[cfg(test)]`. - A group whose x size is not a multiple of 4 can pair a row's end with the next row's start on both the host and the console; that is the console's own quad layout, not a host divergence, and the fix does not change it. - Cube samples take differences of the cube direction, so a face seam can raise the level slightly; a `NaN`/`inf` coordinate now poisons the gradient where level 0 was immune.
A `sample` with an implicit level outside a pixel shader read level 0,
which leaves the sky, the screen-space reflections and the occlusion
resolves shimmering: their footprints span several texels, so the level
they read is far from the one the screen asks for. The console takes
the derivatives from the neighbouring lanes of the quad; the translation
now does the same (`quadOp` across x and y) and emits `sampleGrad`.

Level 0 stays where the lanes cannot be read: a stage without lanes, a
mesh program, a group of fewer than four threads, and a block reached
through a per-lane branch. Pixel shaders keep the host's own derivatives
and every explicit level (`_l`, `_lz`, `_d`) is untouched.

`VICTORY_SAMPLE_GRAD=0` restores the level-0 sample. The switch is part
of the translation and lands in the shader cache key through build.rs,
so an A/B keeps its own translations.

Tests: emit::sample_grad_tests: a compute sample emits the grad form
(validated by dxil.dll), the switch and a one-thread group keep level 0,
and a pixel shader's sample is unchanged.

cargo test --locked -p victory-shader: 334 passed, 0 failed, 4 ignored.
cargo test --locked -p victory-gpu --lib: 649 passed, 0 failed.
cargo build --locked --release -p victory: ok.
A group whose size is not a multiple of four has a half-filled tail quad: lanes 4 and 5 of six would read lanes 6 and 7 through quadOp, so the gradients now also need a size that is a multiple of four. A debug assertion holds the sample's gates to cross_lane's. The cube-seam and NaN notes sit with the gradients.

The launcher's hand-kept switch list is not part of this branch: adding VICTORY_SAMPLE_GRAD to it lives in the launcher change.

Tests: a wave64 group of 64 on a host of 32-lane waves takes the grad form through the relabelling and its four quad reads are the across reads in x and y; a sample inside a per-lane branch keeps level 0; a group of six keeps it too. The validator's skip now says so.

cargo test --locked -p victory-shader: 336 passed, 0 failed, 4 ignored.
cargo test --locked -p victory-gpu --lib: 649 passed, 0 failed.
cargo build --locked --release -p victory: ok.
A group of exactly four threads is one quad and a [2, 2, 1] group is the ideal 2x2 one; both take the grad form with the four across reads in x for u and v and then in y. The 2x2 case is the one a size check that rounded up to eight or compared with >= would wrongly drop.

The debug assertion now holds the invariants the function relies on - a group that is a whole number of quads, and a current block the lane blocks carry - instead of the two early returns' conjuncts, and does not assert console_lanes, which is legitimately false for an unrelabelled compute program.

The wave64 test's doc no longer claims the quad reads stay inside a host wave, which its recorded kinds cannot observe, and the tail-quad note says lanes 6 and 7 are the next group's threads rather than nonexistent.

cargo test --locked -p victory-shader: 338 passed, 0 failed, 4 ignored.
cargo test --locked -p victory-gpu --lib: 649 passed, 0 failed.
cargo build --locked --release -p victory: ok.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-04 08:52:26 +00:00
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/591/head:github-bot/gh-591'
git switch 'github-bot/gh-591'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-591'
git switch 'github-bot/gh-591'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-591'
git switch 'github-bot/gh-591'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-591'
git switch 'main'
git merge --squash 'github-bot/gh-591'
git switch 'main'
git merge --ff-only 'github-bot/gh-591'
git switch 'main'
git merge 'github-bot/gh-591'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.