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

Open
oxybes wants to merge 3 commits from upstream-pr/591 into main
oxybes commented 2026-12-04 08:48:45 +00:00 (Migrated from github.com)

Imported from readmet3xt-lab/foxhound. Originally posted by oxybes on 2026-12-04T08:48:45Z.


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.
<!-- greyfinch-sync:readmet3xt-lab/foxhound:issue:5643334704 --> Imported from [readmet3xt-lab/foxhound](https://github.com/readmet3xt-lab/foxhound/pull/591). Originally posted by [oxybes](https://github.com/oxybes) on 2026-12-04T08:48:45Z. --- `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.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/591:upstream-pr/591'
git switch 'upstream-pr/591'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/591'
git switch 'upstream-pr/591'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/591'
git switch 'upstream-pr/591'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/591'
git switch 'main'
git merge --squash 'upstream-pr/591'
git switch 'main'
git merge --ff-only 'upstream-pr/591'
git switch 'main'
git merge 'upstream-pr/591'
git push origin 'main'
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
readmet3xt-lab/foxhound!591
No description provided.