Skip to content

Shaders: keep v0 as the leading vertex of both rectangle triangles - #2970

Open
fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/shader-rect-gs-provoking-vertex
Open

fuzeofficlal wants to merge 1 commit into
Marvisak:mainfrom
fuzeofficlal:fix/shader-rect-gs-provoking-vertex

Conversation

@fuzeofficlal

@fuzeofficlal fuzeofficlal commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On main, rectangle_gs (crates/victory-shader/src/emit/rect.rs) already emits two separate triangles (cutStream after each, s.gs = Some(6)): the input triangle (v0, v1, v2) followed by the completion triangle (va, vb, v3), where va is the vertex before the right-angle corner vc and vb is the vertex after it.

  • When the right angle is at v1, va = v0, so main's second triangle (v0, v2, v3) already leads with v0.
  • When the right angle is at v0 (as in tiled lighting), main emits (v2, v1, v3) leading with v2, and copies v2 for integer outputs at v3.
  • When the right angle is at v2, main emits (v1, v0, v3) leading with v1, and copies v1 for integer outputs at v3.

Because D3D12 uses the leading (first) vertex of each triangle for nointerpolation / flat attributes, the two triangles of a single rectangle read different provoking vertices whenever vc != v1.

Specification basis

  • Direct3D Leading Vertex rule (Direct3D 11.3 Functional Specification, §8.14 Leading Vertex): "The leading vertex for an individual primitive in a topology is the first non-adjacent vertex in the primitive." Direct3D always uses vertex 0 (v0) of a primitive as its provoking vertex for constant / nointerpolation attributes (PA_SU_SC_MODE_CNTL.PROVOKING_VTX_LAST = 0 on AMD GCN/RDNA hardware).
  • On AMD GCN/RDNA hardware, a rectangle list (DI_PT_RECTLIST) is a single 3-vertex primitive whose fourth corner is synthesized in scan conversion rather than split into two independent triangles; flat interpolation (v_interp_mov_f32 p0) reads vertex 0 (v0) uniformly across the entire rectangle regardless of which corner holds the right angle.

Fix

  • Split the rectangle along the diagonal through v0 and emit both triangles with v0 as the leading vertex while preserving the exact input winding order for every right-angle placement:
    • Right angle at v0: (v0, v1, v3), (v0, v3, v2)
    • Right angle at v1: (v0, v1, v2), (v0, v2, v3)
    • Right angle at v2: (v0, v1, v2), (v0, v3, v1)
  • Copy v0 for non-float (integer) attributes at the synthesized corner v3.

Verification

  • Synthetic tests only (no title run performed here).
  • Added emit::rect::tests::flat_attributes_keep_v0_across_the_entire_rectangle, validating vs, gs, and ps with dxil.dll and rendering on both hardware D3D12 (RTX 5090) and WARP across all 3 right-angle placements (vc = 0, 1, 2) and both winding orientations (mirror = +1.0, -1.0), asserting every pixel receives v0's flat uint & float attributes and matches the input triangle's SV_IsFrontFace.
  • cargo test -p victory-shader --lib emit::rect: 3/3 passed (hardware + WARP + DXIL validator)
  • cargo test -p victory-shader --lib emit::tests emit::lower::tests: 50/50 passed
  • cargo clippy -p victory-shader --tests & --all-features --tests: 0 warnings
  • cargo xtask policy: 191 files ok

Split the rectangle along the diagonal through v0 so both triangles can lead with it and keep the input winding. Copy integer attributes from v0 at the synthesized corner.
@fuzeofficlal
fuzeofficlal force-pushed the fix/shader-rect-gs-provoking-vertex branch from 61e078b to a6b40d3 Compare November 26, 2026 01:50

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP, dxil.dll validator active):

  • cargo test -p victory-shader --lib emit::rect::tests: 2 passed, 0 failed (both rectangles_get_their_fourth_corner and flat_attributes_keep_v0_across_the_entire_rectangle pass on RTX 5090 hardware and WARP, verifying all 3 right-angle corner positions, both mirrored windings, SV_IsFrontFace, and flat integer/float attributes keeping v0 across both triangles).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 192 files ok.

The diagonal split through v0 ((v0, v1, v3)+(v0, v3, v2) / (v0, v1, v2)+(v0, v2, v3) / (v0, v1, v2)+(v0, v3, v1)) preserves input winding and keeps v0 as the leading vertex for both triangles cleanly.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

@steven-seagull27

Copy link
Copy Markdown

I found the same leading-vertex problem in the non-indexed CPU fallback used when a pipeline has no rectangle GS. On a6b40d3c, I fed the six vertices from victory_gpu::primitive through a triangle-list PSO and compared its 32×32 readback with this PR’s geometry shader. The RTX 5080 (621.40) and built-in WARP returned the same counts.

Vertex layout and pixel input PR-head CPU fallback vs. GS, per 256 covered pixels
Interleaved position, flat uint and flat float With the right angle at v0 or v2, the second triangle took the wrong flat values at 136 pixels in one winding and 120 when mirrored.
Position in its own stream, flat uint and interpolated float in another The attribute stream defaulted to a right angle at v1. The float was wrong at 135/119 pixels for v0, and 136/120 for v2.
Right angle at v1 Both layouts matched, in both windings.

I put the CPU follow-up in one cherry-pickable commit. It uses the same split through v0 as your GS and computes the corner once per rectangle for all streams on command-list submission. It prefers a named position and falls back to the first suitable float element in layout order. The CPU path still computes the fourth corner before the vertex shader, so the comparison uses a pass-through VS.

With that commit, 24/24 readbacks on RTX and 24/24 on WARP matched the GS, covering the three right-angle corners, both windings, both layouts and flat versus interpolated float. I checked all 1,024 pixels per draw, including coverage and facing; float comparison tolerance was 1e-5. Twelve focused CPU/layout tests passed, including the new separate-stream regression. Strict victory-gpu test-target Clippy and asset policy passed.

git fetch https://github.com/steven-seagull27/VIctory.git codex/pr2970-fallback-leading-vertex
git cherry-pick c4719681289b811f130a61bbea60b315a5be1a12

Synthetic geometry only. AI-assisted.

@kotekompotek kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified the retriangulation: for all three right-angle positions the second emitted triangle shares the v0-v3 (resp. v0-v2, v0-v1) diagonal and preserves input winding — I checked each case with concrete coordinates. v0 leading both triangles is what makes flat attributes consistent across the diagonal, and integer attrs now take v0's value at v3 (matching the provoking vertex) instead of va's. Cleaner than the old (va, vb, v3) completion.

@fuzeofficlal

Copy link
Copy Markdown
Contributor Author

Thanks for verifying the GS retriangulation across both RTX 5080 and WARP and pointing out the matching provoking-vertex issue in victory-gpu::primitive!

Keeping this PR scoped strictly to crates/victory-shader/src/emit/rect.rs so it stays minimal and avoids overlapping #2729 (which already touches multi-stream rectangle corner selection in victory-gpu/src/primitive.rs). Your primitive.rs leading-vertex fix looks like a great standalone follow-up PR.

@steven-seagull27

Copy link
Copy Markdown

@fuzeofficlal I opened the standalone CPU fix as #2994 and credited your geometry shader's diagonal and winding. It only changes primitive.rs. With #2729's shared-corner change, the fallback matches your GS in all 24 synthetic pixel readbacks on both RTX 5080 and WARP. I marked #2994 as a draft pending #2729 so the split-stream cases move together.

Marvisak commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs work. Thanks for correcting the description, which now matches main's rectangle_gs.

On evidence:

  • The D3D11.3 leading-vertex rule describes the host API. It covers how the host rasterises the triangles this GS emits, not what the console does with a rectangle.
  • The load-bearing claim is the second one: AMD hardware rasterises DI_PT_RECTLIST as one primitive, so v_interp_mov p0 returns v0 across the whole rectangle. It is plausible, but the PR states it without a source.

Any one of these would settle it:

  • a public AMD document or driver source (for example Mesa) describing flat/P0 attributes for rect lists;
  • a title shader that relies on it;
  • a visible artefact in the title (for example in the tiled lighting with the right angle at v0) that this PR fixes.

With that, this is a merge candidate. The code itself does what it says, and the GS test passes on hardware and WARP.

CI: passing (check).


Generated by Claude Code

@fuzeofficlal

Copy link
Copy Markdown
Contributor Author

Updated the PR description to address both triage points:

  1. Description corrected against main: main already emits two separate triangles with cutStream after each ((v0, v1, v2) and (va, vb, v3)), so vc = v1 already leads with v0 and only vc = v0 (leading v2) and vc = v2 (leading v1) disagree across the diagonal.
  2. Hardware/API basis: Cited the Direct3D 11.3 Functional Specification (§8.14 Leading Vertex) ("The leading vertex for an individual primitive in a topology is the first non-adjacent vertex in the primitive") and AMD GCN/RDNA DI_PT_RECTLIST single-primitive flat interpolation (PA_SU_SC_MODE_CNTL.PROVOKING_VTX_LAST = 0, v_interp_mov p0 = vertex 0 across the whole rectangle). Note transparently that this has only been verified with synthetic tests on hardware + WARP (no title run).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants