gpu: lower primitive topology 0x11 (rectangle list) to triangle list (Refs #870) #1061

Merged
Holastor merged 3 commits from upstream-pr/1068 into main 2026-11-24 01:13:38 +00:00
Holastor commented 2026-11-24 00:12:11 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Holastor on 2026-11-24T00:12:11Z.


This pull request implements lowering of primitive topology 0x11 (VGT_PRIMITIVE_TYPE_RECTLIST) to standard Direct3D 12 triangle lists (D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST), addressing point 5 of the #870 checkpoint.

Interface & Provenance

  • Console / AMD RDNA2 hardware architecture defines VGT_PRIMITIVE_TYPE_RECTLIST = 0x11 (17 decimal), taking 3 vertices per primitive (v_0, v_1, v_2).
  • Hardware primitive setup derives the fourth rectangle corner algebraically as:
    v_3 = v_0 + v_2 - v_1
  • On desktop Direct3D 12, primitive topology enum values are limited to points, lines, triangles, and control-point patches (values 0x1 through 0xa, and 0x21..0x48). Passing raw 0x11 causes D3D12 debug layer validation error and driver rejection.

Implementation

  • tools/rectlist_topology.py:
    • derive_fourth_vertex: computes v_3 = v_0 + v_2 - v_1 for positions and linearly interpolates vertex attributes.
    • expand_rectangles: lowers N rectangles (3N vertices) into 2N triangles (6N vertices) forming counter-clockwise triangles (v_0, v_1, v_2) and (v_0, v_2, v_3).
    • expand_indices: generates index buffer sequences [0, 1, 2, 0, 2, 3] for indexed rectangle draws.
    • rect_raster_oracle: reference rasterizer comparing point-in-rectangle coverage against analytically derived triangles.

Verification

  • tests/rectlist_topology_check.py:
    • 11 unit and negative control checks covering:
      • Counter-clockwise winding preservation.
      • Full-screen viewport coverage ([-1, 1] \times [-1, 1]).
      • Off-center, scaled, and quadrant-clipped rects.
      • Multi-attribute linear interpolation for auxiliary vertex channels (UVs, colors).
      • Index buffer generation.
      • Negative controls: rejection of vertex counts not divisible by 3.
      • Mutation controls: wrong corner derivation, inverted winding, and dropped vertices detected by the raster oracle.

Tested Commands & Results

  • Environment: Windows 11 x64 (build 26100.3037), MSVC 19.51, AMD Radeon RX 6600M (driver 32.0.21045.11001), dev and release presets.
./out/pe-env/Scripts/python.exe -B tests/rectlist_topology_check.py
# Ran 11 tests in 0.001s: OK

./out/pe-env/Scripts/python.exe -B tests/docs_links_check.py
# Self-test passed. Checked 15 Markdown files and 239 links.

./out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py
# Self-test passed. Checked 891 tracked source, script and build/CI files: no bidirectional controls.

./out/pe-env/Scripts/python.exe -B tests/hardware_reports_check.py
# Self-test passed.

ctest --preset dev --no-tests=error -R '^rectlist_topology$' -V
# 1/1 Test #574: rectlist_topology ................ Passed 0.07 sec

Refs #870.
AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6571580438 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1068). Originally posted by [Holastor](https://github.com/Holastor) on 2026-11-24T00:12:11Z. --- This pull request implements lowering of primitive topology `0x11` (`VGT_PRIMITIVE_TYPE_RECTLIST`) to standard Direct3D 12 triangle lists (`D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST`), addressing point 5 of the #870 checkpoint. ### Interface & Provenance - Console / AMD RDNA2 hardware architecture defines `VGT_PRIMITIVE_TYPE_RECTLIST = 0x11` (17 decimal), taking 3 vertices per primitive $(v_0, v_1, v_2)$. - Hardware primitive setup derives the fourth rectangle corner algebraically as: $$v_3 = v_0 + v_2 - v_1$$ - On desktop Direct3D 12, primitive topology enum values are limited to points, lines, triangles, and control-point patches (values `0x1` through `0xa`, and `0x21`..`0x48`). Passing raw `0x11` causes D3D12 debug layer validation error and driver rejection. ### Implementation - `tools/rectlist_topology.py`: - `derive_fourth_vertex`: computes $v_3 = v_0 + v_2 - v_1$ for positions and linearly interpolates vertex attributes. - `expand_rectangles`: lowers $N$ rectangles ($3N$ vertices) into $2N$ triangles ($6N$ vertices) forming counter-clockwise triangles $(v_0, v_1, v_2)$ and $(v_0, v_2, v_3)$. - `expand_indices`: generates index buffer sequences $[0, 1, 2, 0, 2, 3]$ for indexed rectangle draws. - `rect_raster_oracle`: reference rasterizer comparing point-in-rectangle coverage against analytically derived triangles. ### Verification - `tests/rectlist_topology_check.py`: - 11 unit and negative control checks covering: - Counter-clockwise winding preservation. - Full-screen viewport coverage ($[-1, 1] \times [-1, 1]$). - Off-center, scaled, and quadrant-clipped rects. - Multi-attribute linear interpolation for auxiliary vertex channels (UVs, colors). - Index buffer generation. - Negative controls: rejection of vertex counts not divisible by 3. - Mutation controls: wrong corner derivation, inverted winding, and dropped vertices detected by the raster oracle. ### Tested Commands & Results - Environment: Windows 11 x64 (build 26100.3037), MSVC 19.51, AMD Radeon RX 6600M (driver 32.0.21045.11001), dev and release presets. ```powershell ./out/pe-env/Scripts/python.exe -B tests/rectlist_topology_check.py # Ran 11 tests in 0.001s: OK ./out/pe-env/Scripts/python.exe -B tests/docs_links_check.py # Self-test passed. Checked 15 Markdown files and 239 links. ./out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py # Self-test passed. Checked 891 tracked source, script and build/CI files: no bidirectional controls. ./out/pe-env/Scripts/python.exe -B tests/hardware_reports_check.py # Self-test passed. ctest --preset dev --no-tests=error -R '^rectlist_topology$' -V # 1/1 Test #574: rectlist_topology ................ Passed 0.07 sec ``` Refs #870. AI-assisted.
upstream-sync closed this pull request 2026-11-24 06:46:07 +00:00
R0Y00 commented 2026-11-24 06:46:07 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T00:14:47Z.

Review: CHANGES_REQUESTED


This is a useful CPU geometry prototype. The current expansion preserves the sign of the first triangle, so the same positions with v0 and v2 exchanged produce clockwise output. Please document and validate corner order as a defined input precondition, or state that winding follows input order. Before we can accept this as desktop topology lowering, please exercise the emitted triangles in a source-built WARP draw with culling enabled, distinct interpolated attributes and coverage against a separate expected rectangle. The reversed-winding and wrong-corner mutants should both fail GPU readback. Keep the runtime hook for a follow-up.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6031630031 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1068#pullrequestreview-6031630031). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T00:14:47Z. **Review:** CHANGES_REQUESTED --- This is a useful CPU geometry prototype. The current expansion preserves the sign of the first triangle, so the same positions with v0 and v2 exchanged produce clockwise output. Please document and validate corner order as a defined input precondition, or state that winding follows input order. Before we can accept this as desktop topology lowering, please exercise the emitted triangles in a source-built WARP draw with culling enabled, distinct interpolated attributes and coverage against a separate expected rectangle. The reversed-winding and wrong-corner mutants should both fail GPU readback. Keep the runtime hook for a follow-up.
R0Y00 commented 2026-11-24 06:46:07 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T00:50:06Z.

Review: APPROVED


I kept your rebased branch and added a check that feeds the GPU renderer the actual Python expansion. Windows Release passes the Python suite plus RTX 4060 Laptop and WARP draws. Each adapter had zero positive mismatches and detected the reversed-winding and wrong-corner controls (900 and 435). Mutating only the Python output also failed WARP with 282 mismatches. The runtime hook remains separate.
Candidate: cbf3c8bb84

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6031925503 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1068#pullrequestreview-6031925503). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T00:50:06Z. **Review:** APPROVED --- I kept your rebased branch and added a check that feeds the GPU renderer the actual Python expansion. Windows Release passes the Python suite plus RTX 4060 Laptop and WARP draws. Each adapter had zero positive mismatches and detected the reversed-winding and wrong-corner controls (900 and 435). Mutating only the Python output also failed WARP with 282 mismatches. The runtime hook remains separate. Candidate: cbf3c8bb844b605162794901bdeeeb7c3b579193
Holastor commented 2026-11-24 06:49:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Holastor on 2026-11-24T00:27:19Z.


Thanks for the review and guidance, @R0Y00! I've updated the branch (85ce3705) addressing all requested points:

  1. Corner Order Precondition & Winding Preservation:

    • Documented the input precondition in tools/rectlist_topology.py: (v_0, v_1, v_2) where v_0 and v_1 are adjacent corners and v_2 is diagonally opposite to v_1.
    • Proved algebraically that Triangle 0 (v_0, v_1, v_2) and Triangle 1 (v_0, v_2, v_3) possess identical signed 2D area:
      \text{cross\_2d}(v_0, v_2, v_3) = (x_2 - x_0)(y_2 - y_1) - (y_2 - y_0)(x_2 - x_1) = \text{cross\_2d}(v_0, v_1, v_2)
      So the winding order of both lowered triangles strictly follows the input winding.
    • Added validate_corner_order validating non-collinearity, CCW winding, and optional edge orthogonality.
  2. Source-Built D3D12 WARP Draw Fixture with Culling Enabled:

    • Added tests/rectlist_draw_check.cpp and tests/rectlist_quad.hlsl, registering rectlist_draw_warp and rectlist_draw_hardware in CMake.
    • Render target: 64x64, rasterizer state configured with CullMode = D3D12_CULL_MODE_BACK and FrontCounterClockwise = TRUE.
    • Distinct vertex attributes: v_0, v_1, v_2 have distinct RGBA colors, with v_3 color derived identically to positions.
    • Separate analytical expected rectangle: pixels in the rectangle interior [-0.46, 0.46]^2 are compared against bilinear analytical interpolation, and pixels outside [-0.54, 0.54]^2 must be clear color (0).
    • Positive draw: 0 mismatches against the expected rectangle.
  3. Mutant Controls against GPU Readback:

    • Reversed-winding mutant: swapping v_0 and v_2 flips winding to clockwise. The GPU hardware rasterizer discards all back-facing triangles under D3D12_CULL_MODE_BACK, leaving the buffer completely clear (900 culled mismatches against expected rectangle).
    • Wrong-corner mutant (v_3 = v_0 + v_2): distorts Triangle 1 and leaves the top-left quadrant unrendered (435 mismatches against expected rectangle).
    • Desktop raw topology 0x11 rejection: passing raw 0x11 fails pipeline creation with 0x80070057 (E_INVALIDARG).
  4. Runtime Hook:

    • Left untouched in production runtime shims as requested; reserved for follow-up PR.

Verification (Windows 11 x64, RX 6600M & WARP):

cmake --build --preset dev --target rectlist_draw_check
ctest --preset dev --no-tests=error -R 'rectlist' -V
# 1/3 Test  #52: rectlist_draw_warp ............... Passed 0.03 sec
# 2/3 Test  #53: rectlist_draw_hardware ........... Passed 0.09 sec
# 3/3 Test #576: rectlist_topology ................ Passed 0.07 sec

python -B tests/docs_links_check.py
# Self-test passed. Checked 15 Markdown files and 241 links.

python -B tests/bidi_controls_check.py
# Self-test passed. Checked 895 tracked files: no bidirectional controls.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7118085591 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1068#issuecomment-7118085591). Originally posted by [Holastor](https://github.com/Holastor) on 2026-11-24T00:27:19Z. --- Thanks for the review and guidance, @R0Y00! I've updated the branch (`85ce3705`) addressing all requested points: 1. **Corner Order Precondition & Winding Preservation:** - Documented the input precondition in `tools/rectlist_topology.py`: $(v_0, v_1, v_2)$ where $v_0$ and $v_1$ are adjacent corners and $v_2$ is diagonally opposite to $v_1$. - Proved algebraically that Triangle 0 $(v_0, v_1, v_2)$ and Triangle 1 $(v_0, v_2, v_3)$ possess identical signed 2D area: $$\text{cross\_2d}(v_0, v_2, v_3) = (x_2 - x_0)(y_2 - y_1) - (y_2 - y_0)(x_2 - x_1) = \text{cross\_2d}(v_0, v_1, v_2)$$ So the winding order of both lowered triangles strictly follows the input winding. - Added `validate_corner_order` validating non-collinearity, CCW winding, and optional edge orthogonality. 2. **Source-Built D3D12 WARP Draw Fixture with Culling Enabled:** - Added `tests/rectlist_draw_check.cpp` and `tests/rectlist_quad.hlsl`, registering `rectlist_draw_warp` and `rectlist_draw_hardware` in CMake. - Render target: 64x64, rasterizer state configured with `CullMode = D3D12_CULL_MODE_BACK` and `FrontCounterClockwise = TRUE`. - Distinct vertex attributes: $v_0, v_1, v_2$ have distinct RGBA colors, with $v_3$ color derived identically to positions. - Separate analytical expected rectangle: pixels in the rectangle interior $[-0.46, 0.46]^2$ are compared against bilinear analytical interpolation, and pixels outside $[-0.54, 0.54]^2$ must be clear color (0). - **Positive draw:** 0 mismatches against the expected rectangle. 3. **Mutant Controls against GPU Readback:** - **Reversed-winding mutant:** swapping $v_0$ and $v_2$ flips winding to clockwise. The GPU hardware rasterizer discards all back-facing triangles under `D3D12_CULL_MODE_BACK`, leaving the buffer completely clear (900 culled mismatches against expected rectangle). - **Wrong-corner mutant ($v_3 = v_0 + v_2$):** distorts Triangle 1 and leaves the top-left quadrant unrendered (435 mismatches against expected rectangle). - **Desktop raw topology 0x11 rejection:** passing raw `0x11` fails pipeline creation with `0x80070057` (`E_INVALIDARG`). 4. **Runtime Hook:** - Left untouched in production runtime shims as requested; reserved for follow-up PR. **Verification (Windows 11 x64, RX 6600M & WARP):** ```powershell cmake --build --preset dev --target rectlist_draw_check ctest --preset dev --no-tests=error -R 'rectlist' -V # 1/3 Test #52: rectlist_draw_warp ............... Passed 0.03 sec # 2/3 Test #53: rectlist_draw_hardware ........... Passed 0.09 sec # 3/3 Test #576: rectlist_topology ................ Passed 0.07 sec python -B tests/docs_links_check.py # Self-test passed. Checked 15 Markdown files and 241 links. python -B tests/bidi_controls_check.py # Self-test passed. Checked 895 tracked files: no bidirectional controls. ``` AI-assisted.
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
R0Y00/Xbox2Windows!1061
No description provided.