Skip to content

Shaders: read a pixel program's barycentrics as SV_Barycentrics - #442

Closed
AnoHunted wants to merge 1 commit into
Marvisak:mainfrom
AnoHunted:ps-sv-barycentrics
Closed

AnoHunted wants to merge 1 commit into
Marvisak:mainfrom
AnoHunted:ps-sv-barycentrics

Conversation

@AnoHunted

Copy link
Copy Markdown
Contributor

What

A pixel program gets its barycentric pairs (i, j) in VGPRs, as SPI_PS_INPUT_ADDR enables them. When it uses
them only in v_interp_*, it translates already. When it also reads them as data (arithmetic, an export, a
join), lowering stopped with barycentrics read directly. Such a read is now a channel of SV_Barycentrics
(SM 6.1):

  • lower.rs: i is SV_Barycentrics.y and j is .z, loadInput from an element declared on first read with the
    pair's interpolation mode: perspective center, centroid and sample become linear, linear centroid and
    linear sample (DXIL modes 2, 3, 6); the linear pairs become the noperspective forms (4, 5, 7). The mode
    table v_interp_* already used is now one function (interp_mode) for both.
  • The element is declared only when a barycentric is read as data, so programs that only interpolate keep
    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.
  • D3D allows one perspective-correct and one noperspective SV_Barycentrics (indices 0 and 1). A program
    that 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: an ISG1 row with system value 23 (SV_Barycentrics) is parsed; until now it stopped the
    translation as a system value in the signature. That row is left out and declared again from the pair the
    program reads, because a signature carries no interpolation mode. container.rs writes kind 28 as system
    value 23. shader.rs sets the barycentrics shader flag (bit 22) and its SFI0 bit (0x20000), which the
    validator expects whenever the input signature has SV_Barycentrics.
  • New tests in emit/barycentric_tests.rs (below).

Why

#97 lists barycentrics read directly among 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:

  • What i and j weight on the hardware. Attribute interpolation is P0 + i·P10 + j·P20 (v_interp_p1_f32
    takes i, v_interp_p2_f32 takes j). Mesa's ACO does exactly that: it passes channel 0 of the barycentric pair
    to v_interp_p1_f32 and channel 1 to v_interp_p2_f32 (aco_isel_helpers.cpp). So i is the weight of
    vertex 1, j the weight of vertex 2, and P0 gets 1 − i − j.
  • The hardware's vertex order. ACO loads flat inputs with v_interp_mov_f32 from P0 (vertex_id = 0; /* P0 */). This translator already maps P0 to nointerpolation. RADV implements VK_KHR_fragment_shader_barycentric
    in radv_nir_lower_fs_barycentric.c (MIT). It builds the API-order weights as (1 − i − j, i, j) when a per-quad
    hardware value (load_provoking_vtx, GFX10.3 and later) is 0. For 1 and 2 it uses the two cyclic rotations of
    that. 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.
  • What D3D defines. DXC's SV_Barycentrics documentation 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]).
  • So: i = y and j = z is the unrotated case. The host has no rotation to reproduce. A program whose use of i
    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.
  • The rest of the DXIL. From DXC's sources: SV_Barycentrics in a pixel input is NotPacked (SM 6.1), so it
    gets 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).
  • The host side, tested. On the GPU, the translated program's (1 − i − j, i, j) equals the weights D3D gives
    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_Barycentrics needs D3D12_FEATURE_DATA_D3D12_OPTIONS3::BarycentricsSupported on the host. These programs did
not 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_Barycentrics row and reads through a join, and it is on by
default. #136 (open) adds the same one-line 23 => 28 mapping in mod.rs signature() and takes interpolation
modes from the console's PSV0. It does not lower barycentric reads. If both land, the conflict is that one
line. 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 through
ir::build → resource::track → io::raise → structure → emit. The checks read the container's ISG1,
PSV0 and SFI0:

  • barycentrics_read_as_data_are_sv_barycentrics_y_and_z: a program exporting (1 − i − j, i, j, 1). It runs for
    each of the six pairs, and once with a console SV_Barycentrics row in ISG1. Checks: one SV_Barycentrics
    row (system value 23, no register, float, mask xyz, y and z read), the pair's mode in PSV0, and the SFI0 bit.
  • a_perspective_and_a_linear_pair_are_two_elements: perspective center plus linear center gives indices 0 and
    1 with modes 2 and 4. Center plus centroid is refused with the new message.
  • interpolation_alone_declares_no_barycentrics: a v_interp_p1/p2 program keeps only its TEXCOORD row and
    does not set the SFI0 bit.
  • 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 vertex
    shader, 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:

  • Main with these tests added (test-only branch, not part of the PR): 4 of the 5 fail with barycentrics read directly (the GPU test at translation). Only interpolation_alone_declares_no_barycentrics passes. Branch:
    all 5 pass, and the draw matches on the RTX 5080 and on WARP.
  • Whole runs, main and branch side by side in one job (cargo test --workspace -- --test-threads=1,
    cargo clippy --workspace --all-targets, cargo xtask policy): main 264 passed, 0 failed, 4 ignored; branch
    269 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=4
    hung in the existing emit::valu_tests::bfe_signed_and_unsigned_match_rdna2, which uses WARP from parallel
    test threads (Tests: victory-shader tests crash (0xC0000005) when several tests use WARP devices in parallel #181). Every crate before victory-shader passed, as did 73 of its 74 tests, the new ones
    included.
  • cargo clippy -p victory-shader --all-targets: no warnings, on main or on the branch.

dxil.dll was not available on the test machine, so the validator step of these tests (and of the existing
ones) was skipped. The DXIL checks above are structural, plus pipeline creation and the draw on two drivers.

Not tested

  • The title, and any console program that reads barycentrics this way: no such shader was available.
  • AMD and Intel GPUs.
  • The DXIL validator (dxil.dll), as above.
  • Centroid and sample modes on the GPU (only their declarations are checked). MSAA, lines and points.
  • The D3D12 debug layer.

AI-assisted (Claude Code, Claude Opus 5.5). No game files were used.

A pixel program gets its barycentric pairs (i, j) in VGPRs, as
SPI_PS_INPUT_ADDR enables them. Programs that use them only in
v_interp_* translate; one that also reads them as data (arithmetic, an
export, a join) stopped with "barycentrics read directly", one of the
common reasons pixel shaders do not translate (Marvisak#97).

Such a read is now a channel of SV_Barycentrics (SM 6.1) with the
pair's interpolation mode: perspective or linear (noperspective),
center, centroid or sample. The hardware's i and j weight vertices 1
and 2 (P0 + i P10 + j P20); D3D gives the provoking vertex's weight in
x and the other two in y and z in winding order, so i is y and j is z
when P0 is the provoking vertex. The hardware may rotate a primitive's
vertices cyclically; the host has no such rotation, and gives the
unrotated weights. D3D allows one perspective-correct and one linear
SV_Barycentrics, so a program reading two perspective (or two linear)
pairs as data still stops, as does the pull model.

The element is declared on first read, so programs that only
interpolate keep their signatures. A console SV_Barycentrics row
(system value 23, refused as an unknown system value until now) is
left out and declared again from the pair read: a signature has no
interpolation modes. The barycentrics shader flag and SFI0 bit are set
with it.

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

Copy link
Copy Markdown

Verified b6ccc95 on 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

Copy link
Copy Markdown

Follow-up: this overlaps #453 (both emit SV_Barycentrics for pixel programs), and the two conflict textually — git merge-tree --write-tree pr442 pr453 reports CONFLICT (content) in crates/victory-shader/src/emit/lower.rs (plus auto-merges in mod.rs/shader.rs). Worth choosing one.

Measured on Arch Linux x86_64 (rustc 1.98.1, cargo test -p victory-shader):

#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

Copy link
Copy Markdown
Contributor Author

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:

  • A draw test on real GPUs. drawn_weights_are_the_hosts_vertex_weights (in emit/barycentric_tests.rs on 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 Shaders: pixel barycentrics through SV_Barycentrics (values and computed interpolation) #453's emitter with small changes.
  • A caveat on vertex order. RADV's radv_nir_lower_fs_barycentric.c shows 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.

@upipibi upipibi mentioned this pull request Nov 24, 2026
r4tedx added a commit to r4tedx/VIctory that referenced this pull request Nov 24, 2026
…out)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@slint6614-dev

Copy link
Copy Markdown

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 main c7a5f49 first (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

Copy link
Copy Markdown
Contributor Author

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.

@AnoHunted AnoHunted closed this Nov 24, 2026
@MrCloudy2

Copy link
Copy Markdown

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 translate in two world runs, each matched to its container by the BLAKE3 VIctory logs. Each PR was merged on its own onto one base: main 8e79c80 + #151 #190 #454 #493 #579 + #177's key fix + the wave64 stack via #439. I then ran victory 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).

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.

4 participants