Skip to content

Rect lists: the CPU expansion finds the rect's corner, not always v1 - #266

Open
Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/rect-list-cpu-corner
Open

Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:fix/rect-list-cpu-corner

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Title: Rect lists: the CPU expansion finds the rect's corner, not always v1

The CPU expansion of rect lists (#195) makes every rect's fourth corner as v0 + v2 − v1. On #189 (closed) the question was whether that holds for every vertex order. It does not: it is right when the rect's corner (the vertex between the other two) is v1, and for a rect with its corner at v0 it puts the fourth vertex outside the rect, at (x2, 2·y1 − y2). This PR finds the corner first.

Both orders are in use on AMD hardware:

order corner who sends it
(x1,y1) (x1,y2) (x2,y1) v0 Mesa r600 r600_draw_rectangle ("The hw rectangle has only 3 vertices, The 4th one is derived from the first 3"); Mesa radeonsi's blit VS, from the vertex index (load_vs_input_from_blit_sgpr); radv's meta passes (nir_gen_rect_vertices)
(x1,y1) (x1,y2) (x2,y2) v1 xf86-video-ati's r600 EXA (r600_exa.c, drawn as DI_PT_RECTLIST in r6xx_accel.c); the full-screen passes draw_rects was written from

The two r600 drivers run on the same chips, so the hardware does not take a fixed vertex as the corner. The rect list GS (#199) already finds the corner from the positions; only the CPU path assumed v1. Both drivers were read for the vertex orders; no code is taken from them.

What is in it

  • crates/xcl/src/hle/d3d12x/bind.rs, rect_corner: which of a rect's three vertices is the corner. It goes by words: in a word two vertices share (an x or a y, and a texture coordinate that follows it) the third vertex is the far end of an edge, and the corner is the vertex that differs alone in the fewest words. No single such vertex (not three corners of a screen-aligned rect): v1, so those draws are expanded as before.
  • bind.rs, expand_rect: the fourth corner is a + b − corner, word by word as before (a word equal in all three vertices is kept). Both triangles start with v0 and keep the winding of v0 v1 v2. For a rect with its corner at v1 the six vertices are exactly the ones main makes.
  • bind.rs, expand_rect_streams: one corner for a rect whose vertices come from several vertex buffers, and every slot's six vertices use it (review by shdann: each slot on its own could find another corner and pair a position with another corner's attribute). It is found from the positions alone where the input layout names them (review by Alpha325: with every word voting, another attribute could outvote the position). Where the layout does not, it is found from the words of all slots, as if they were one interleaved vertex.
  • crates/xcl/src/hle/d3d12x/real.rs, layout_position and rect_position: where a rect list pipeline's input layout has the position, as slot, byte offset and bytes. That is its element named POSITION or SV_Position (index 0), two to four 32-bit floats read per vertex. It is noted when a translated rect list pipeline is made (translated_graphics_pso) and forgotten with the pipeline.
  • bind.rs, draw_rects copies each rect from every slot, looks up where the bound pipeline has the position and calls it; nothing else there changes. This path only runs for a rect list pipeline without the rect list GS, for DrawInstanced with vertex buffers.
  • Tests: in bind.rs rects_expanded_on_the_cpu_have_the_corner_opposite_their_own, a_rect_split_over_vertex_buffers_has_one_corner and a_rects_corner_is_found_from_its_positions; in real.rs rect_list_pipelines_know_where_their_layout_has_the_position. +294/−22 in all, about 125 of them the tests.

Overlap: #267 and #533 edit bind.rs, and #267 also adds five lines to forget_pso in real.rs, which this PR adds to as well. Both already conflict with main 18c105a in bind.rs, as #150 and #164 do in real.rs. The other open PRs that touch bind.rs or real.rs merge with this head without conflicts in these two files (git merge-tree): #214, #236, #292, #303, #310, #317, #433, #466, #473, #539, #541, #562, #567, #578, #625, #630, #634, #637. No open PR or issue is about rect lists.

Tested

  • NVIDIA GeForce RTX 5070 Ti, driver 610.74; Ryzen 9 9950X; Windows 11 (10.0.26200); rustc 1.99.0; debug. Head 14fecf8, one commit on main 18c105a. It was rebased from 7f08b78 on 508a0c7: D3D12.X: ExecuteIndirect with a pipeline still being built is skipped (Austin's fork fix, upstreamed) #574 and this PR each added a test at the same place in bind.rs's test module, and both are kept; no other line of the commit changed.
  • cargo test --workspace -- --test-threads=4 on 14fecf8: 693 passed, 0 failed, 41 ignored (the xcl library: 653 passed, 21 ignored). SKIPPED GPU tests: 2, both no wave64 (gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware).
  • The previous head 7f08b78, on main 508a0c7, hung in D3D12.X: replacing the sparse state deadlocks when the old state still holds placed buffers #344 on its own (a watchdog stopped the run after 240 s without output) and ran 685 passed, 0 failed, 41 ignored with D3D12.X: the sparse state is replaced outside its lock #443's commit (16fc79d) on top. 14fecf8 did not hang in its one run, without D3D12.X: the sparse state is replaced outside its lock #443.
  • cargo test -p xcl --lib -- hle::d3d12x::bind::tests rect_list on 14fecf8: 16 passed, 0 failed. Those are the eight tests of bind.rs (one of them main's new one from D3D12.X: ExecuteIndirect with a pipeline still being built is skipped (Austin's fork fix, upstreamed) #574), the four of real.rs's rect list pipelines and four of the rect list GS; the four tests named above need no GPU.
  • Alpha325's case, positions (0,0) (1,0) (0,1) and a float2 attribute (1,1) (½,½) (½,½), in two vertex buffers and in one: on b224d77 the fourth position is (−1,1) in both (a scratch test: no fourth corner at (1,1): [(0.0, 0.0, 1.0, 1.0), (1.0, 0.0, 0.5, 0.5), (0.0, 1.0, 0.5, 0.5), (0.0, 0.0, 1.0, 1.0), (0.0, 1.0, 0.5, 0.5), (-1.0, 1.0, 1.0, 1.0)]). With the positions named it is (1,1) and every vertex carries its own position's attribute. Without them it is still (−1,1); the test asserts both.
  • The layout test: Position, three floats, in slot 1 at offset 4 gives (1, 4, 12), and is forgotten with the pipeline. No position: a pipeline that is not a rect list, a layout without the name, 16-bit floats, a per-instance element, index 1, and an appended element after another of its slot.
  • The split-stream test, positions (0,0) (1,0) (0,1) in one buffer and 10 + 10x + 20y in another: every output vertex must carry its own position's attribute. With each slot finding its own corner, as e30794d did, it fails: [(0.0, 0.0, 10.0), (1.0, 0.0, 20.0), (1.0, 1.0, 30.0), (0.0, 0.0, 10.0), (1.0, 1.0, 30.0), (0.0, 1.0, 20.0)]. With one corner for the rect it passes.
  • The corner test, for the corner at v0, at v1 in both directions, and at v2: the six vertices are the rect's four corners, the two triangles are its halves along one diagonal, both wind as v0 v1 v2 and start with v0, the fourth corner's texture coordinate continues the others, and z, w and a packed colour are kept. Three vertices that are not rect corners give v0 + v2 − v1 as before.
  • It fails with main's rule: with the corner fixed at v1 it stops at corner v0: no vertex at [48.0, 24.0] in [[16.0, 8.0], [16.0, 24.0], [48.0, 8.0], [16.0, 8.0], [48.0, 8.0], [48.0, -8.0]]. main has this arithmetic inline in draw_rects, where a test cannot call it.
  • The expansion drawn on the GPU (a scratch test, not in the PR, expand_rect as of e30794d, one vertex buffer): for each of the four orders 0 of 256 pixels wrong (rect covered, texture coordinate right), 36 wrong when only the three vertices are drawn.
  • Earlier smoke runs by @theaustinayers, RTX 5090, release, 60 s, on main 9ab7d76 and the previous head e30794d (another title), no crash or device removal (Rect lists: the CPU expansion finds the rect's corner, not always v1 #266 (comment)).

Regression run

Our own runs, one after the other on the machine under Tested (RTX 5070 Ti), release builds, no uncommitted changes.

main:

xcl-smoke v1
commit: 18c105af86929d6ae26c44096c85b66f1644e743
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1666 (PresentX 1666)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14439 MB

PR:

xcl-smoke v1
commit: 14fecf8d1b3a0cca75bd876d3f580587c2160206
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 60
exit: 0x0
frames shown: 1630 (PresentX 1630)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14434 MB

Not tested

  • Whether the title's own frames reach draw_rects: the smoke runs above are a regression check only.
  • No console: the rect is the expectation, not a measurement of hardware. AMD's public register references (R6xx, SI, CIK) only name the primitive (17 - DI_PT_RECTLIST: Rect List); they do not say how the fourth vertex is derived.
  • draw_rects itself (a console command list, title vertex buffers, the upload memory) has no test, as on main; only the expansion it calls does.
  • The position is found by its name in the input layout. That is the title's convention: XCL does not follow the vertex shader's code from its inputs to SV_Position. A layout with no element named POSITION or SV_Position, a position that is not 32-bit floats, and one appended (D3D12_APPEND_ALIGNED_ELEMENT) after other elements of its slot are expanded from the words of all slots. There an attribute in which the corner alone differs from the other two vertices can outvote the position, and the corner is then taken as v1, as before this PR. The same goes for rect list pipelines with a console GS or with PC shaders, which note no position.
  • Which names the layouts of a retail title's rect list pipelines use: not looked at.
  • The lookup in draw_rects (the bound pipeline, a stand-in resolved, to the slot's stream) has no test, like the rest of draw_rects.
  • A slot the input layout reads per instance is still read as per-vertex data here, as on main.
  • Still not expanded, as on main: indexed rect list draws, ExecuteIndirect, and vertices made from the vertex index alone, in pipelines without the GS. They draw half of each rect.
  • AMD and Intel GPUs.

AI use

  • Claude Code (Claude Fable 5.2): code, tests, sources and this description
  • Claude Code, Claude Opus 5.5: the one-corner-per-rect change for several vertex buffers and its test
  • Claude Code (Claude Fable 5.2): the corner from the positions the input layout names, its tests, the rebase onto 18c105a and the runs on 14fecf8

@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Regression run wanted. We have no title on our machine. If you can run scripts/smoke.ps1 -Game <your title folder> twice, once built from main (9ab7d76) and once from this PR's head (e30794d), please paste both xcl-smoke blocks here. I'll copy them into the description under Regression run, with your name and hardware.

🤖 Claude Opus 5.5

theaustinayers added a commit to theaustinayers/xcl that referenced this pull request Nov 23, 2026
… rect's corner

Their work, merged as it is. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@theaustinayers

Copy link
Copy Markdown
Collaborator

Regression run on a retail title, as asked. Both blocks are the unedited output of scripts/smoke.ps1 -Game <folder> (60 s, release builds, no uncommitted changes), run one after the other on the same machine.

Hardware: Ryzen 9 9950X3D, NVIDIA GeForce RTX 5090 (32 GB), Windows 11 Pro 26300, Rust 1.100.0.

Note on the gpu: line: the script prints the first Win32_VideoController, which on this PC is the CPU's integrated graphics. XCL itself ran on the RTX 5090 in both runs (D3D12.X: backend 'real' – NVIDIA GeForce RTX 5090 (32187 MiB VRAM) in the log). I left the blocks as the script wrote them.

main (9ab7d76):

xcl-smoke v1
commit: 9ab7d763197c7f420bff10cd9bf1310f07e2a9aa
gpu: AMD Radeon(TM) Graphics
title: NYCMain
seconds: 60
exit: 0x0
frames shown: 1078 (PresentX 1079)
presents per 2 s (last 3): 60 60 34
device removals: 0
crashes: 0
peak commit: 18216 MB

this PR's head (e30794d):

xcl-smoke v1
commit: e30794d41bf4ba75f68cfcb2894c40b1ba0cd81f
gpu: AMD Radeon(TM) Graphics
title: NYCMain
seconds: 60
exit: 0x0
frames shown: 1060 (PresentX 1060)
presents per 2 s (last 3): 60 32 8
device removals: 0
crashes: 0
peak commit: 18515 MB

What it covers: the title's first screens draw their full-screen passes as rect lists through draw_rects (corner at v1), so the changed code runs. No device removal and no crash in either run.

What it does not show: the PR's run has 18 fewer frames (1060 vs 1078) and 299 MB more peak commit. One run each, with no input, so I cannot say whether that is the change or run-to-run variation; the title starts streaming near the end of the 60 s, which is where the last "presents per 2 s" figures differ. I did not compare the picture between the two runs, and this title has no rect with its corner at v0 that I know of, so the new branch itself is not exercised.

Both runs were on my machine; Claude Code (Claude Fable 5.2) ran the script and wrote this report.

@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@theaustinayers thank you, both blocks are in the description now.

One question from your note that the full-screen passes go through draw_rects: that path only runs for a rect list pipeline that did not get the rect list GS (#199). When main leaves the GS out it says why, once per reason. Could you search the main run's log for rect list drawn as triangles and paste the lines you find?

No need for another run if the log from the one above is still there.

🤖 Claude Fable 5.2, Claude Opus 5.5

T-Crypt added a commit to T-Crypt/bathyal that referenced this pull request Nov 23, 2026
theaustinayers added a commit to theaustinayers/xcl that referenced this pull request Nov 23, 2026
…inds the rect's corner"

This reverts commit d0867d8, reversing
changes made to 207e287.
@shdann

shdann commented Nov 24, 2026

Copy link
Copy Markdown

At bind.rs:278, draw_rects calls expand_rect independently for each bound slot. Consider positions (0,0), (1,0), (0,1) in slot 0 and scalar attributes 10,20,30 in slot 1. The positions select corner 0, so their first triangle uses vertices [0,1,3]. The scalar values tie and select the fallback corner 1, so their first triangle uses [0,1,2]. The generated position gets attribute 30 instead of 40. Could you compute one corner and vertex order per rectangle from the positions, reuse it across slots, and cover this split-stream case? Static review at e30794d, assisted by OpenAI Codex (GPT-6).

@Kalmarv

Kalmarv commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, @shdann, that was right: each slot picked its own corner, and your example paired the fourth position (1,1) with 30.

Changed in b224d77 (squashed into the PR's commit, rebased on main 2997c9a):

  • draw_rects now picks one corner and vertex order for each rect and uses them for every bound slot (expand_rect_streams in bind.rs). The rect list path has no input-layout semantics, so it can't tell which slot holds the positions. The corner is found from the words of all slots together, as if they were one interleaved vertex. In your example the positions decide it (corner v0), and the scalar adds no vote.
  • New test a_rect_split_over_vertex_buffers_has_one_corner, your case: positions (0,0) (1,0) (0,1) in one buffer, 10 + 10x + 20y in another. Every output vertex must carry the attribute of its own position. With the old per-slot corner it fails, giving (1,1) → 30 and (0,1) → 20. With the change it passes.

🤖 Claude Opus 5.5

@Alpha325

Copy link
Copy Markdown

I tested clean head b224d77699f6af5452169e9eae1773e8ded56e94. All six hle::d3d12x::bind::tests passed in release mode. A separate synthetic probe using the unchanged rectangle helpers found a case the split-stream test doesn't cover.

  • Position stream: (0,0), (1,0), (0,1).
  • Separate float2 attribute stream: (1,1), (0.5,0.5), (0.5,0.5). Each component is affine: 1 - 0.5*x - 0.5*y.
  • Expected fourth position: (1,1). Actual: (-1,1). With constant attributes, the same positions expand correctly.

The position words give alone = [0,1,1]. The two attribute words change that to [2,1,1], so the tie falls back to v1. All streams still agree on one corner, but it's the wrong one.

Can the CPU path use position data for corner selection when the layout identifies it? If that data isn't available, this seems worth documenting as a limitation of the fallback.

AI use: OpenAI Codex (GPT-6.2 Sol) ran the tests, wrote the synthetic probe, and drafted this report.

@Kalmarv

Kalmarv commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, @Alpha325, confirmed. On b224d77 your two streams give the fourth position (−1,1), and the same data in one interleaved buffer does too.

Changed in 7f08b78 (squashed into the PR's commit, rebased on main 508a0c7):

  • The layout does identify the position, so the CPU path now uses it. When a translated rect list pipeline is made, XCL notes where its input layout has the element named POSITION or SV_Position (index 0, two to four 32-bit floats per vertex): slot, byte offset and size (layout_position in real.rs). draw_rects finds the corner from those bytes alone and uses it for every slot.
  • New test a_rects_corner_is_found_from_its_positions, your case in two buffers and in one: the fourth position is (1,1) and every vertex carries its own position's attribute. A second test covers which layouts give a position.
  • The limit, as you suggested, is in the comment on rect_corner and under Not tested. The name is the title's convention; XCL does not follow the vertex shader's code from its inputs to SV_Position. A layout without such an element, a position that is not 32-bit floats, and one appended after other elements of its slot are still expanded from the words of all slots. There your case still gives (−1,1), and the test asserts that too.

Tested on an RTX 5070 Ti (driver 610.74), debug: the bind and rect list tests on 7f08b78, 15 passed, 0 failed. The whole suite on 7f08b78 alone hung in #344, as the earlier head did. With #443's commit (16fc79d) on top it ran 685 passed, 0 failed, 41 ignored, with 2 GPU tests skipped (no wave64). Our title smoke pair on 7f08b78 is in the description. Not tested: which names a retail title's rect list layouts use.

🤖 Claude Fable 5.2

draw_rects made every rect's fourth corner as v0 + v2 - v1, which is right
when the rect's corner (the vertex between the other two) is v1. Rects are
also sent with it at v0 - (x1,y1) (x1,y2) (x2,y1), as Mesa's r600 and
radeonsi blits do - and then the fourth vertex landed outside the rect, at
(x2, 2*y1 - y2).

The corner is now found from the vertices (the one that differs alone in
the fewest words; v1 when there is no single one, as before), and the
fourth corner is a + b - corner. Both triangles start with v0 and keep the
winding of v0 v1 v2. For a rect with its corner at v1 the six vertices are
the ones made before.

A rect's vertices in several vertex buffers get one corner, and every
slot's six vertices use it. Each slot on its own could find another: an
attribute with no word two vertices share falls back to v1, and paired a
position with another corner's attribute (review by shdann).

The corner is found from the positions alone where the pipeline's input
layout names them: an element POSITION or SV_Position of two to four
floats, noted when a translated rect list pipeline is made. With every
word of the vertex voting an attribute could outvote the position: with
(0,0) (1,0) (0,1) and a float2 of (1,1) (.5,.5) (.5,.5) v0 differs alone
in two words, the corner was taken as v1 and the fourth position came out
at (-1,1) instead of (1,1) (review by Alpha325). A layout that does not
name the positions is still expanded from the words of all slots, as if
they were one interleaved vertex.

Assisted-by: Claude Code (Claude Fable 5.2)
Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@MesshM

MesshM commented Nov 24, 2026

Copy link
Copy Markdown

Test run of 14fecf8d1b3a0cca75bd876d3f580587c2160206 on AMD RDNA4. I merged it onto current main d5442d2 locally (clean merge) and tested that tree.

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4), driver 32.0.32029.2010; Ryzen 7 5700X, 64 GB RAM, Windows 11 Pro build 26200 (native Windows), Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug
  • cargo build --workspace + cargo test --workspace -- --test-threads=4 on the merge: 708 passed, 0 failed, 41 ignored, exit code 0; xcl lib tests 667 passed, 0 failed, 21 ignored
  • 0 SKIPPED; a_rects_corner_is_found_from_its_positions, a_rect_split_over_vertex_buffers_has_one_corner and the gpu_rect_list_* tests pass.

Review: The CPU expansion now takes the rect's missing corner from its positions instead of assuming a vertex order, and the GPU rect-list tests agree. I found nothing to change.

Not tested: no retail title or regression run (I have no unpacked game), no XCL_SAMPLE_* tests, release build; the PR's head on its old base was not run, only its merge onto main.

Run and review by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the 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.

5 participants