Shaders: read a pixel program's barycentrics as SV_Barycentrics #442
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!442
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/442/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@AnoHunted on GitHub · 2026-11-24 03:28 UTC · original
AnoHunted:ps-sv-barycentrics→main· +444 −11 · 5 files · 1 commits · closed without mergingWhat
A pixel program gets its barycentric pairs (i, j) in VGPRs, as
SPI_PS_INPUT_ADDRenables them. When it usesthem only in
v_interp_*, it translates already. When it also reads them as data (arithmetic, an export, ajoin), lowering stopped with
barycentrics read directly. Such a read is now a channel ofSV_Barycentrics(SM 6.1):
lower.rs: i isSV_Barycentrics.yand j is.z,loadInputfrom an element declared on first read with thepair's interpolation mode: perspective center, centroid and sample become
linear,linear centroidandlinear sample(DXIL modes 2, 3, 6); the linear pairs become thenoperspectiveforms (4, 5, 7). The modetable
v_interp_*already used is now one function (interp_mode) for both.their signatures and need no new device feature. A barycentric that reaches its reader through a join (a phi)
is loaded for the join too.
noperspectiveSV_Barycentrics(indices 0 and 1). A programthat reads two perspective pairs as data (say center and centroid), or two linear ones, still stops, now with
barycentrics of two sample positions read directly. The pull-model triple (i/w, j/w, 1/w) still stops too.mod.rs: anISG1row with system value 23 (SV_Barycentrics) is parsed; until now it stopped thetranslation as
a system value in the signature. That row is left out and declared again from the pair theprogram reads, because a signature carries no interpolation mode.
container.rswrites kind 28 as systemvalue 23.
shader.rssets the barycentrics shader flag (bit 22) and itsSFI0bit (0x20000), which thevalidator expects whenever the input signature has
SV_Barycentrics.emit/barycentric_tests.rs(below).Why
#97 lists
barycentrics read directlyamong the most common reasons pixel shaders do not translate.The vertex order is the part that has to be right. Here is what I checked, and how:
v_interp_p1_f32takes i,
v_interp_p2_f32takes j). Mesa's ACO does exactly that: it passes channel 0 of the barycentric pairto
v_interp_p1_f32and channel 1 tov_interp_p2_f32(aco_isel_helpers.cpp). So i is the weight ofvertex 1, j the weight of vertex 2, and P0 gets 1 − i − j.
v_interp_mov_f32from P0 (vertex_id = 0; /* P0 */). This translator already maps P0 tonointerpolation. RADV implementsVK_KHR_fragment_shader_barycentricin
radv_nir_lower_fs_barycentric.c(MIT). It builds the API-order weights as (1 − i − j, i, j) when a per-quadhardware value (
load_provoking_vtx, GFX10.3 and later) is 0. For 1 and 2 it uses the two cyclic rotations ofthat. So the hardware's P0, P1, P2 are the API vertices rotated, never mirrored. The rotation can differ per
primitive: AMD's GPUOpen article on stable barycentrics says the driver picks the first vertex by the
triangle's screen orientation.
SV_Barycentricsdocumentation says the provoking vertex's weight is always in x.The others follow in the primitive's order, flipped on every other strip triangle to keep the winding (strip
triangle 1 is [1, 3, 2]).
and j is symmetric (an edge distance, say) gets the console's result on every primitive. A program that
un-rotates with the hardware's per-quad value reads an SGPR this translator does not model, so it still
stops. A program that uses raw i and j unsymmetrically gets the API-order weights, which are one of the orders
the console itself can show.
SV_Barycentricsin a pixel input isNotPacked(SM 6.1), so itgets no register (row and column −1, PSV0 start 0 without the allocated bit). It must be a float3. Its mode must
be one of the six above. There are at most two, with different perspective, at semantic index 0 or 1. The
shader flag is computed from the input signature for validator 1.6 and newer (our
dx.valver).vertices 0, 1 and 2 at each pixel. Perspective-correct and screen-linear both match, on the RTX 5080 and on
WARP (test below).
What I could not check: which rotation the console hardware uses for a given primitive. Lines: D3D's z is exactly 0 on a line, so j reads 0 and i reads the
whole weight. RADV builds the line weight as i + j, so the hardware may split it between the two. A program
that uses i and j separately on lines could differ. This is marked
shortcut:.SV_BarycentricsneedsD3D12_FEATURE_DATA_D3D12_OPTIONS3::BarycentricsSupportedon the host. These programs didnot translate before, so no device loses anything. On a device without the feature, creating their pipeline
fails.
Overlap: #281 (open, opened while this was being written) makes the same mapping (i = y, j = z) for the
perspective-center pair only, behind an opt-in feature because of the rotation question above. This branch
covers all six pairs and two elements, a console
SV_Barycentricsrow and reads through a join, and it is on bydefault. #136 (open) adds the same one-line
23 => 28mapping inmod.rssignature()and takes interpolationmodes from the console's
PSV0. It does not lower barycentric reads. If both land, the conflict is that oneline. The row this PR leaves out is dropped before #136's mode loop, so the two do not fight.
Tested
Windows 11, NVIDIA GeForce RTX 5080, Rust 1.98.1; also WARP.
New tests in
crates/victory-shader/src/emit/barycentric_tests.rs. They use synthetic GFX10 programs throughir::build→resource::track→io::raise→structure→emit. The checks read the container'sISG1,PSV0andSFI0:barycentrics_read_as_data_are_sv_barycentrics_y_and_z: a program exporting (1 − i − j, i, j, 1). It runs foreach of the six pairs, and once with a console
SV_Barycentricsrow inISG1. Checks: oneSV_Barycentricsrow (system value 23, no register, float, mask xyz, y and z read), the pair's mode in
PSV0, and theSFI0bit.a_perspective_and_a_linear_pair_are_two_elements: perspective center plus linear center gives indices 0 and1 with modes 2 and 4. Center plus centroid is refused with the new message.
interpolation_alone_declares_no_barycentrics: av_interp_p1/p2program keeps only its TEXCOORD row anddoes not set the
SFI0bit.a_barycentric_joined_at_a_branch_is_loaded_for_the_join: i, or 0.5 behind a uniform branch, joined in a phi.drawn_weights_are_the_hosts_vertex_weights(host GPU): a triangle with w = 1, 2, 4 and a pass-through vertexshader, drawn at 32×32. In each of the 292 pixels well inside the triangle, the output must match the weights
of vertices 0, 1, 2 to within 2e-3. For the perspective-center pair those are the perspective-correct weights.
For the linear-center pair they are the screen-linear ones (the two differ by up to about 0.3 here). It skips
when the device lacks barycentrics. It ran on both.
Results:
barycentrics read directly(the GPU test at translation). Onlyinterpolation_alone_declares_no_barycentricspasses. Branch:all 5 pass, and the draw matches on the RTX 5080 and on WARP.
cargo test --workspace -- --test-threads=1,cargo clippy --workspace --all-targets,cargo xtask policy): main 264 passed, 0 failed, 4 ignored; branch269 passed (the five new tests), 0 failed, 4 ignored; clippy reports the same warnings on both (none in the
changed files); policy ok on both. A first run with
--test-threads=4hung in the existing
emit::valu_tests::bfe_signed_and_unsigned_match_rdna2, which uses WARP from paralleltest threads (#181). Every crate before
victory-shaderpassed, as did 73 of its 74 tests, the new onesincluded.
cargo clippy -p victory-shader --all-targets: no warnings, on main or on the branch.dxil.dllwas not available on the test machine, so the validator step of these tests (and of the existingones) was skipped. The DXIL checks above are structural, plus pipeline creation and the draw on two drivers.
Not tested
dxil.dll), as above.AI-assisted (Claude Code, Claude Opus 5.5). No game files were used.
@DaviTostes on GitHub · 2026-11-24 03:36 UTC · original
Verified
b6ccc95on Arch Linux x86_64 (rustc 1.98.1,cargo test -p victory-shader): 60 passed, 0 failed, 0 ignored.Not covered: the emitted barycentric signature on a real host (no D3D12 execution here) and title behaviour (no game data).
@DaviTostes on GitHub · 2026-11-24 03:42 UTC · original
Follow-up: this overlaps #453 (both emit
SV_Barycentricsfor pixel programs), and the two conflict textually —git merge-tree --write-tree pr442 pr453reportsCONFLICT (content)incrates/victory-shader/src/emit/lower.rs(plus auto-merges inmod.rs/shader.rs). Worth choosing one.Measured on Arch Linux x86_64 (rustc 1.98.1,
cargo test -p victory-shader):b6ccc95): 60 passed, 0 failed.87e9d41): 65 passed, 0 failed, 9 new tests; it also covers computed barycentrics (v_interp_p1/p2with i/j that are not the loaded input) and all six modes.#453 describes itself as the broader version of the same fix (it also notes draft #281). Not covered here: DXIL validation on Windows (no SM 6.6 device in this Wine prefix) and title shaders (no game data).
@AnoHunted on GitHub · 2026-11-24 03:49 UTC · original
Thanks for testing and for spotting the overlap. You're right: #453 does everything this PR does, plus computed barycentrics in
v_interp_p1/p2. I'm happy for #453 to be the one that lands, and will close this one once it does, or before if the owner prefers.Two things from here that #453 may want:
drawn_weights_are_the_hosts_vertex_weights(inemit/barycentric_tests.rson this branch) draws a triangle with w = 1, 2, 4 at 32×32. It checks all 292 interior pixels against the perspective-correct weights for the perspective pair, and against screen-linear weights for the linear pair, to within 2e-3. It passes on an NVIDIA GeForce RTX 5080 and on WARP. It should work against #453's emitter with small changes.radv_nir_lower_fs_barycentric.cshows that on GFX10.3+ the hardware's I/J can be one of three cyclic rotations of the API order: per quad, it reads the provoking-vertex value and picks the matching rotation. So i = y, j = z holds for the unrotated case only. Programs that use i and j symmetrically are fine. A program that reads them asymmetrically gets API-order weights, which can differ from the console on rotated primitives. This applies to both PRs.@slint6614-dev on GitHub · 2026-11-24 09:24 UTC · original
Measured on the full retail shader pack (26,503 containers) with
victory translate --validate --pipelines(pipelines created on an AMD RX 9070, Windows 11, dxil.dll from SDK 10.0.26100). Counts are emitted / validated / pipelines. This PR was merged locally with current mainc7a5f49first (main: VS/PS 12224 / 12148, CS 1440 / 1417, MS 8163 / 8101; it includes #612).VS/PS 12237 / 12158 (+10 validated): the 13 "barycentrics read directly" refusals are gone, but 3 of those programs fail the validator with "out of bound coord must be undef.". CS and MS unchanged. For comparison, #453 (measured on its base, since it conflicts with main) also clears the 207 "interpolation with computed barycentrics" and validates +175 pixel programs.
@AnoHunted on GitHub · 2026-11-24 10:21 UTC · original
Thanks @slint6614-dev for the full-pack numbers. They confirm #453 is the one to take. It clears the same 13 refusals plus the 207 computed-barycentric ones (+175 validated, against +10 here), and 3 of the programs this PR emits fail the validator ("out of bound coord must be undef"). I'm closing this in favour of #453. The GPU draw test and the vertex-rotation note in my comment above remain available to #453.
@MrCloudy2 on GitHub · 2026-11-24 10:59 UTC · original
Tester result: +2 of 339 previously failing world programs now emit (vertex/pixel 0 → 2 of 25; the two that logged
barycentrics read directly is not supported). Heads-up: on that base with #160 + #539 + #571 already merged, this PR no longer merges cleanly, so it is not in my combined build yet.How measured: 339 shader containers from my own 1.0.2.1 copy, the distinct programs that logged
shader does not translatein two world runs, each matched to its container by the BLAKE3 VIctory logs. Each PR was merged on its own onto one base:main8e79c80+ #151 #190 #454 #493 #579 + #177's key fix + the wave64 stack via #439. I then ranvictory translate <folder>(diag build, CPU only) and compared DXIL emitted per stage against the base (vertex/pixel 0 of 25, compute 32 of 272, mesh 0 of 13). Linux, Proton-CachyOS wine, cross-built with xwin, RTX 3060 host (only matters for the wave64 part of the base). Translation counts only, not image correctness. AI-assisted (Claude Code).Pull request closed