Skip to content

Shaders: gathers from the first level take the console's footprint - #3613

Open
kilolimaVI wants to merge 6 commits into
Marvisak:mainfrom
kilolimaVI:shader/gather-console-footprint
Open

kilolimaVI wants to merge 6 commits into
Marvisak:mainfrom
kilolimaVI:shader/gather-console-footprint

Conversation

@kilolimaVI

Copy link
Copy Markdown

image_gather4_lz(_o) goes to DXIL textureGather with the program's coordinate, so near a texel's edge the host decides which four texels a gather takes. The console rounds u × size to 1/256 of a texel, half up, before taking floor(u × size − 0.5). Lower::gather_first already implements this rule for gathers at a level (gather_at_level), but the first-level path never used it.

Hosts differ here. At 1/1024 of a texel below an edge, the Radeon 780M rounds up like the console, while WARP stays below and takes the footprint one texel back.

Change

  • New lower/gather.rs: gather_console_coordinate moves each axis to (first + 1) / size, with first from gather_first. That is the middle of the console's footprint, half a texel from either edge, so every host takes the same four texels in any address mode or border.
  • NaN takes the footprint at 0, as on the console. Beyond ±2 (where gather_first clamps) and at infinities the coordinate is left unchanged. Cubes are left as they were.
  • lower.rs gains 15 lines (521,359 / 524,288 bytes).
  • gather_tests (from Shaders: quad reads at a triangle edge without the console's compiler #3583) now requires the console's footprint on every host. It used to only report the boundary case.

Builds on #3583. The first four commits are #3583's; the change itself is the last commit.

Results, AMD Radeon 780M (RDNA3) + WARP, Windows 11:

d below the edge console 780M before WARP before both after
+0.25, +0.75 x x x x
−1/1024 x x x − 1 x
−1/512 (tie) x x x − 1 x
−(1/512 + 1/65536) x − 1 x − 1 x − 1 x − 1
−3/1024 x − 1 x − 1 x − 1 x − 1

256/256 pixels per case per host after the change. 5 of 5 runs, full suite (victory-shader + victory-gpu --features diagnostics) 555 passed, clippy clean.

Side note: on the 780M, a point sample (image_sample_lz) at −1/1024 stays below while a gather rounds up, so even one host resolves coordinates differently per path. NVIDIA numbers would be welcome.

AI-assisted (Claude).

kilolimaVI and others added 5 commits November 27, 2026 21:02
flow_tests draws the derivative and quad-read cases from console-compiled
HLSL, so it needs the console SDK's dxc.exe and most machines skip it.
quad_read_tests hand-assembles the same shapes: each pixel computes a key
from its position and reads its x, y and diagonal neighbours' keys with
DPP quad_perm, on a triangle whose edge crosses quads (helper lanes), and
again after the console's form of a discard (v_cmpx, s_andn2 of the live
mask, s_cbranch_scc0 to a null export, s_wqm), whose killed pixels stay
helpers and must not be written. wave32 and wave64, hardware and WARP.

Radeon 780M (RDNA3) and WARP at 63bcca4: edge 136 pixels, discard 104,
0 wrong in all eight.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
lod_tests takes image_get_lod of coordinates linear in the pixel position
(u = 0.3 x, v = 0.11 y of a 16x16 texture, so log2(4.8) at every pixel)
next to a triangle edge and next to pixels the console's form of a discard
killed: their helper lanes must compute the coordinates the derivatives
take. wave32 and wave64, hardware and WARP, tolerance 0.02 of a level.

Radeon 780M (RDNA3): 2.2617 at all 136 / 104 pixels (the level in 1/256
steps); WARP 2.2630; 0 wrong in all eight.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A NaN compares false with the tolerance, so it passed; the levels must also
be finite (pointed out by pickles976).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gather_tests looks up at t = x + d texels in u and v over a 16x16 texture
whose texels hold their index, with image_gather4_lz (footprint at
floor(t), half a texel back) and image_sample_lz with a point sampler
(texel at floor(t)). At d 0.25 and 0.75 every host must take floor(t).
Just below a boundary (d = -1/1024) it reports, without failing, whether
the host rounds t up to x or keeps it below.

Radeon 780M (RDNA3): gathers 256 of 256 round up, point samples 256 of 256
stay below; WARP: both stay below. Exact cases 0 wrong everywhere.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
image_gather4_lz(_o) went to DXIL textureGather with the program's
coordinate, so which four texels a gather took near a texel's edge was
the host's choice. The console rounds u x size to 1/256 of a texel, half
up, before taking floor(u x size - 0.5), the rule gather_first already
implements for gathers at a level. Hosts differ: 1/1024 of a texel below
an edge, the Radeon 780M rounds up as the console does and WARP stays
below, taking the footprint one texel back.

The coordinate along each axis is now moved to (first + 1) / size, the
middle of the console's footprint, with first from gather_first: half a
texel from either edge, so every host takes the same four texels in any
address mode. NaN takes the footprint at 0, as on the console; beyond
+-2 (where gather_first clamps) and at infinities the coordinate is kept.
Cubes are left as they were. The helper lives in lower/gather.rs to keep
lower.rs under the file size limit.

gather_tests now requires the console's footprint on every host: 1/1024
and the tie of 1/512 below an edge round up, 1/512 + 1/65536 and 3/1024
stay below.

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

Copy link
Copy Markdown
Contributor

@kilolimaVI I reproduced the 16×16 case on app-local WARP, with the loaded SDK and WARP modules verified. At the two offsets closest to the texel boundary, f0191df selected the previous footprint in all 512 readbacks. bbe216c selected the expected footprint in all 512, and the other four controls matched their expected footprints.

I also checked a 13×13 texture with two wrapped tile edges and three border-mode cases. All 845 readbacks matched on both revisions with this WARP runtime. I put those two checks in a test-only commit on top of your exact head. Both strict shader Clippy configurations and the asset policy check passed. Test commit

If reusing the tests, please retain the Git author when cherry-picking or rebasing. If folding them into another commit, please add the trailer below.

Co-authored-by: andrewpozdnakov7-jpg andrewpozdnakov7-jpg@users.noreply.github.com

@ColorMeBoom

Copy link
Copy Markdown

Hardware run on AMD RDNA1, to go with the 780M and RTX 4060 runs on #3583: RX 5700 XT (Navi 10, RDNA1, driver 32.0.21045.29003, Windows 11 26200), head bbe216c on 63bcca4.

  • gathers_take_the_footprint_at_the_coordinate and point_samples_take_the_texel_at_the_coordinate pass on the card, including the offsets closest to the texel boundary:
    gather d -0.000977 hardware: 256 pixels, kept below 0, at x 256, neither 0
    gather d -0.001953 hardware: 256 pixels, kept below 0, at x 256, neither 0
    gather d -0.001968 hardware: 256 pixels, kept below 256, at x 0, neither 0
    point d -0.000977 hardware: 256 pixels, kept below 256, at x 0, neither 0
    
  • Full victory-shader --lib --features host: 306 passed, 0 failed, 3 ignored.

AI use: Claude Code (Claude Opus 5.5).

@akimfomin

Copy link
Copy Markdown

For the open question about NVIDIA near the 1/256-texel footprint boundary, I published a PortBridge Lab task pinned to bbe216c3 and invited separate native D3D12 and Proton testers with public NVIDIA results. The receipt asks for selected texels and matched offsets, with the software and physical runs separated. No new result is available yet. Participants can give their agents just that issue link, and any result will be credited here.

@upipibi

upipibi commented Nov 28, 2026 •

Copy link
Copy Markdown

The NVIDIA row, as asked: RTX 3070 8 GB, driver 616.56, Windows 11, the hardware adapter and WARP in the same run. emit::gather_tests:: at #3583 head f0191df (before) and this head bbe216c (after), each 3 times with identical output. Pixels out of 256: "below" is the texel at x - 1, "at x" is the texel at x.

Offset d Before, RTX 3070 Before, WARP After, RTX 3070 After, WARP
gather +0.25 at x 256 at x 256 at x 256 at x 256
gather +0.75 at x 256 at x 256 at x 256 at x 256
gather -1/1024 at x 256 below 256 at x 256 at x 256
gather -1/512 (tie) — — at x 256 at x 256
gather -1/512 - 1/65536 — — below 256 below 256
gather -3/1024 — — below 256 below 256
point -1/1024 below 256 below 256 below 256 below 256

"—": that offset is not in the test at #3583's head.

So before this PR, NVIDIA's own gather rounds up at -1/1024 like the 780M, and WARP doesn't. With this PR, the RTX 3070 and WARP agree at every offset and follow the rule in the test: 1/1024 and the 1/512 tie round up to x, while 1/512 + 1/65536 and 3/1024 stay at x - 1. Point samples are unchanged. Both tests pass on both heads (2 passed, 3 runs each).

AI-assisted (Claude Code, Claude Opus 5.5 built both heads and ran the tests on this card).

@kilolimaVI

Copy link
Copy Markdown
Author

Thanks all. @andrewpozdnakov7-jpg's test commit is now on this branch as-is, with his authorship (head 4fb5462).

780M (RDNA3) + WARP on 4fb5462, with the tests run against the old lower.rs (before) and this PR (after, 5 of 5 runs):

test before after
gathers_take_the_footprint_at_the_coordinate (16×16, wrap) fails on WARP passes
gathers_round_npot_wrapped_tile_edges (13×13) passes passes
gathers_round_npot_border_edges (13×13) passes passes

Full suite 557 passed, clippy clean. Like Andrew saw, WARP already lands on the console's footprint at 13×13 (the 1/13 coordinate is not exact), so these two tests don't catch the old behaviour. What they guard is that (first + 1) / size keeps the right texels at a non-power-of-two size, across a wrapped tile edge and in border mode.

All results so far:

host before after
Radeon 780M (RDNA3) console footprint console footprint
RX 5700 XT (RDNA1, ColorMeBoom) console footprint console footprint
RTX 3070 (upipibr) console footprint console footprint
WARP one texel back at −1/1024 and the 1/512 tie console footprint

So every hardware adapter tested so far already rounds like the console. The change makes WARP agree, and it removes the dependency on the host's sampler precision for anything not tested yet.

AI-assisted (Claude).

@upipibi

upipibi commented Nov 28, 2026

Copy link
Copy Markdown

NVIDIA row for 4fb5462 (with @andrewpozdnakov7-jpg's tests). RTX 3070, driver 616.56, Windows 11 Pro 10.0.26100, debug. cargo test --locked -p victory-shader --lib gather_tests::gathers, 3 runs each, hardware adapter and WARP in each run:

test before (main 63bcca4's lower.rs) after (4fb5462)
gathers_take_the_footprint_at_the_coordinate (16x16, wrap) fails, 3 of 3 runs passes, 3 of 3
gathers_round_npot_wrapped_tile_edges (13x13) passes passes
gathers_round_npot_border_edges (13x13) passes passes

The "before" failure here comes from WARP. At offsets -0.000977 and -0.001953 the 3070 already gives the console's footprint with main's code (kept below 0, at x 256), and WARP doesn't (kept below 256, at x 0). With this PR both report kept below 0, at x 256 at those offsets, and from -0.001968 on both report kept below 256. So on this card the PR changes WARP's result and leaves the hardware's as it was, and the two new 13x13 tests agree on both. This matches the boundary in my earlier report above.

AI-assisted (Claude Code, Claude Opus 5.5).

@kilolimaVI

Copy link
Copy Markdown
Author

@upipibi @JurajSimicevic one more NVIDIA check that may be worth a minute, since you both reported synthetic_tests::gathers_at_a_level_match_the_hardware failing on main (RTX 3070 in #3749, RTX 5080 in #3584).

That test does three gathers per lane: two image_gather4_l (emulated with gather_first, the console's rounding) and one image_gather4_lz, which on main goes straight to the host's textureGather. On lanes 0-15 it asserts, on hardware only, that the host's own gather equals the emulated one, at the coordinates where rounding decides: f32 products just across a texel threshold, the 1/512 ties, NaN, ±inf, zeros. The 780M rounds like the console there and passes; my guess is NVIDIA doesn't on some of those lanes.

This PR moves the _lz coordinate to the middle of the console's footprint with the same gather_first, so all three gathers should agree on any GPU. If you can, please run at 4fb5462:

cargo test -p victory-shader --lib -- gathers_at_a_level_match_the_hardware --nocapture

If it passes there and fails on main, that's an existing test fixed on NVIDIA. If it still fails, the panic line (which lane, which assertion) would tell me what's left.

AI-assisted (Claude).

@upipibi

upipibi commented Nov 28, 2026 •

Copy link
Copy Markdown

@kilolimaVI it still fails at 4fb5462 on its own, the same as on main. The cause on NVIDIA is the record read that #2323 (Bullet18x) fixes, not gather rounding. With #2323's one line, NVIDIA's own gather matches the emulated one. RTX 3070, driver 616.56, Windows 11 Pro 10.0.26100, debug.

cargo test --locked -p victory-shader --lib -- gathers_at_a_level_match_the_hardware --nocapture, 3 runs per row:

tree hardware (the 3070, then WARP) VICTORY_WARP=1 (every device WARP)
main 63bcca4 0 passed, 1 failed: hardware: lane 0, left [0.0, 1.0, 0.0, 0.0], right [0.0, 0.0, 0.0, 0.0] not run
4fb5462 (this PR) 0 passed, 1 failed: the same panic not run
main + #2323's 9c6e0942 1 passed, 0 failed 0 passed, 1 failed: hardware: lane 4 against its gather, left [66.0, 67.0, 35.0, 34.0], right [65.0, 66.0, 34.0, 33.0]
4fb5462 + #2323's 9c6e0942 1 passed, 0 failed 1 passed, 0 failed

AI use: Claude Code (Claude Opus 5.5) ran the tests and the probe and wrote this.

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.

5 participants