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

Open
github-bot wants to merge 1 commit from github-bot/gh-2970 into main AGit
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 01:48 UTC · original
fuzeofficlal:fix/shader-rect-gs-provoking-vertex → main · +145 −18 · 1 files · 1 commits · open

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
**@⁠fuzeofficlal** on GitHub · 2026-11-26 01:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/2970) `fuzeofficlal:fix/shader-rect-gs-provoking-vertex` → `main` · +145 −18 · 1 files · 1 commits · open ## 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](https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm)): *"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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:18:56 +00:00
Author
Owner

@⁠steven-seagull27 on GitHub · 2026-11-26 02:23 UTC · original

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.

**@⁠steven-seagull27** on GitHub · 2026-11-26 02:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/2970#issuecomment-7180755270) 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](https://github.com/steven-seagull27/VIctory/commit/c4719681289b811f130a61bbea60b315a5be1a12). 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. ```sh git fetch https://github.com/steven-seagull27/VIctory.git codex/pr2970-fallback-leading-vertex git cherry-pick c4719681289b811f130a61bbea60b315a5be1a12 ``` Synthetic geometry only. AI-assisted.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 02:36 UTC · original

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.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 02:36 UTC · [original](https://github.com/Marvisak/VIctory/pull/2970#issuecomment-7180953508) 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.
Author
Owner

@⁠steven-seagull27 on GitHub · 2026-11-26 02:47 UTC · original

@⁠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.

**@⁠steven-seagull27** on GitHub · 2026-11-26 02:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/2970#issuecomment-7181121165) @⁠fuzeofficlal I opened the standalone CPU fix as [#2994](https://git.gtahub.st/github/Marvisak--VIctory/pulls/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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 05:40 UTC · original · edited 2026-11-26 07:19 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-26 05:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/2970#issuecomment-7183698495) · edited 2026-11-26 07:19 UTC <!-- victory-triage v1 head=a6b40d3c03fc246a1aecf88ffcae1892080e486f verdict=needs-work at=2026-11-26T07:19:18Z --> 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](https://claude.ai/code)_
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 06:09 UTC · original

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).
**@⁠fuzeofficlal** on GitHub · 2026-11-26 06:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/2970#issuecomment-7184141191) 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)](https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm) (*"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).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2970/head:github-bot/gh-2970'
git switch 'github-bot/gh-2970'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2970'
git switch 'github-bot/gh-2970'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2970'
git switch 'github-bot/gh-2970'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2970'
git switch 'main'
git merge --squash 'github-bot/gh-2970'
git switch 'main'
git merge --ff-only 'github-bot/gh-2970'
git switch 'main'
git merge 'github-bot/gh-2970'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2970
No description provided.