Skip to content

d3d12: draw the console rectangle lists and quad lists (topologies 17 and 19) as triangles - #207

Closed
Andretuta wants to merge 6 commits into
mainfrom
old-pr/9940
Closed

Andretuta wants to merge 6 commits into
mainfrom
old-pr/9940

Conversation

@Andretuta

Copy link
Copy Markdown
Collaborator

Carried over from old PR 9940 by tedin7 in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/9940, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
tedin7: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

Rebased cleanly on current main; supersedes #7448 closed during org migration.

What

The console's rectangle list (IASetPrimitiveTopology(17), PSO topology type 19) and quad list (IASetPrimitiveTopology(19)) are accepted and drawn as triangles, instead of refusing the PSO (type 19, E_INVALIDARG) or silently drawing a plain triangle list. This is a 3D-rendering and PSO blocker: a PSO of type 19 fails creation today.

  • Rectangle list: three corners; the fourth is v2 + v3 - v1 for the position and every interpolant. The right-angle corner is the vertex opposite the longest screen edge. A type-19 PSO becomes a triangle-list pipeline plus a small generated geometry stage (runtime/src/rect_list_geometry.h) written from the vertex shader's own SPIR-V outputs. It emits (v1 v2 v4) (v1 v4 v3).
  • Quad list: drawn as triangle-list indices (0 1 2) (0 2 3) per quad, so flat inputs use the first quad vertex. Indexed draws read the application's index buffer; non-indexed draws read a shared index buffer (16384 quads per draw, more is cut off with a log line).
  • Docs: docs/d3d12_vulkan.md, "Console rectangle and quad lists".

Why

The fourth corner is a function of what the vertex stage wrote, and the translated vertex shaders fetch vertices themselves through V# loads, so a CPU or compute expansion of vertex data would be wrong for non-linear shaders. A geometry stage after the vertex stage is exact and needs only the geometryShader feature. Facts credited to the xvalkyrie backlog V8 (GPL-3.0, commit 53af7df77c10); no code copied.

Tested

Run on this base (current main, native g++ on the build host):

  • replay_quad_list_test (quad list is one triangle-list indexed draw, topology, index buffer, count 64): exit 0; the layer logs "a non-indexed quad list of 3 quads draws only the first 2" as designed.
  • Negative control: the same test against origin/main's runtime/src does not compile (quad_indices / quad-list topology do not exist), so it fails on main and passes with the change.
  • rect_list_geometry.h compiles standalone with g++ -std=c++20.

NOT run on this base (be aware): d3d12_rect_list_test (the GPU test, Windows exe through Wine) and the compile of shims/d3d12_vulkan_device.cpp. The GPU pods are off and this host has no unpacked Windows SDK; mingw headers are too old to syntax-check the Windows code. The earlier version of this PR (#7448, older base) measured on an RTX 4090 under Wine: rectangle list 4096 of 4096 pixels on the colour plane (worst error 0); quad list 4096 of 4096 bit-exact; it failed at the PSO on main ("primitive topology type not supported: 19"). The cherry-picks onto current main applied with no conflicts and the merge with the latest main is clean, but the GPU result on this base has not been reproduced. Please run d3d12_rect_list in CI/maintainer hardware before merging.

…lists (19) as triangles

A rectangle list is three corners; the fourth is v2 + v3 - v1 for the position and every interpolant, after the vertex stage. A quad list is four vertices per quad. Vulkan has neither. A PSO with topology type 19 was refused (E_INVALIDARG) and the two list topologies drew as a plain triangle list.

Rectangle list: the PSO becomes a triangle-list pipeline with a generated geometry stage (src/rect_list_geometry.h), built from the vertex shader's own SPIR-V outputs, that emits the strip v1 v2 v3 v4. Quad list: one triangle fan per quad (0 1 2, 0 2 3), drawn with the index values 4q..4q+3 so the translated vertex id (VertexIndex - BaseVertex) is right; non-indexed draws read a shared 0, 1, 2, ... buffer reserved in the arena.

Facts from xvalkyrie (GPL-3.0, commit 53af7df77c10); no code copied.
…ty Geometry, log cut-off quads (review fixes)
…at inputs use the first quad vertex, and the PSO topology type is read as raw bits

Taken from the follow-up patch of @matheuskshn (930e78a2, tested by them on an RTX 4070 Ti: replay_quad_list_test fails the two non-indexed cases on a6b31d5 and passes with it). Re-run here: replay_quad_list_test and d3d12_rect_list_test exit 0 on the pod (Wine, RTX 4090).
…e told apart from float outputs on the vertex side (comment only)
…ngle (the vertex opposite the longest screen edge) instead of assuming the first; tests for the corner at the second and third vertex and for a non-right triangle
…s drawn; geometry notes edges are measured in NDC
@Sigtryggur-Mani-Gudmundsson

Copy link
Copy Markdown

I tested the indexed quad path at 21dc3889 on an RTX 4070 Ti with Khronos synchronization validation. A flat red quad came back black in all 4,096 pixels. Two overlapping instances left red on top where the explicit triangle-list reference was green. The non-indexed controls matched both references.

This follow-up to tedin7’s PR extends the triangle pattern credited to matheuskshn to GPU-resident 16- and 32-bit application indices. A single instanced draw keeps the base vertex and first instance. Out-of-view indices read zero. All eight 64 × 64 frames matched the reference after applying the patch. Five failed on this PR head. replay_quad_list, vulkan_scratch and vk_command_replay passed. Large-draw performance, the Windows D3D12 front end and retail startup remain unmeasured.

Patch, GPU fixture, full results and reproduction commands.

@VibeClauding

Copy link
Copy Markdown
Contributor

Windows test report, since this PR's CI was cancelled: head 21dc38892 merged onto main 8f481cf16 (no conflicts), MSVC, Windows 11, RTX 3070 (driver 610.88).

The PR's own tests draw non-indexed quads only, so they neither confirm nor rule out the indexed-quad failure reported above. The rectangle-list path (PSO topology type 19, the four PSOs the retail boot refuses per #432) works on this Windows/NVIDIA stack. AI-assisted.

@Mumogg

Mumogg commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Tested this on the retail boot, merged at 21dc38892 on top of main d7576d56a with my stacked pull requests #375 to #432, #602, #387, and #445, #360, #219. Linux (Nobara 44, kernel 7.3), Wine 11.21 staging, RTX 5070 Ti (driver 615.78.08), clang-cl 22.1.8, own 1.0.2.1 copy, network off, 120 s allowed, one run each. The merge was clean.

--run, 120 s without #207 with #207
CreatePipelineState ok / failed 50,466 / 4 (primitive topology type not supported: 19) 50,470 / 0
PresentX ok 177,572 46,467
DrawInstanced 177,571 (one per present) 139,350 (three per present)
How it ends still running, killed by the timeout still running, killed by the timeout

So the four pipelines that failed are created and the title now draws with them. The picture is still black: with LEONIDA_CAPTURE (8 frames, one every 5,000 presents) every captured frame is uniformly (0, 0, 0). I have not established why, so I am not attributing it to this pull request; it was black before it too.

AI-assisted.

@DesertGamer

DesertGamer commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Check on a second NVIDIA machine (Windows 11, RTX 5070, driver 617.14, LEONIDA_D3D12=vulkan, own 1.0.2.1 copy): this pull request merged on top of main 21625bc + #641 + #427 + #727 + #602 + #387 + #357 + #432, which reaches the frame loop, keeps it there: 473,041 PresentX ok and 18,985,555 D3D12 calls in 200 s, no exception, merged without conflicts. Without lazy PSOs only. I did not check whether the rectangle-list pipelines are created on this stack, so this says nothing about the draw itself.

AI-assisted: yes (Claude Code, Sonnet 5.5)

@Snooozs

Snooozs commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Regression check on Windows 10 Pro 22H2 (19045), RTX 5060 Ti (621.01), VS 2022 RelWithDebInfo, LEONIDA_VK_VALIDATION=1, merged onto main 3295055 without conflicts, targets rebuilt: replay_quad_list_test passes; d3d12_draw_test and d3d12_mesh_test exit 0 with 0 VUID lines. I could not run d3d12_rect_list_test: its target needs the translated M1.5 fixtures and there is no llvm-mc here, so this says nothing about the rectangle path itself. Synthetic tests, no game files. AI-assisted (Claude Code); I read the code diff.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 24, 2026
@aslan2142

Copy link
Copy Markdown
Collaborator

This carry-over of old upstream PR 9940 has been taken over in #682, so I'm closing this draft as #17 describes. The branch old-pr/9940 stays.

AI-assisted (Claude Code, Claude Opus 5.5).

@aslan2142 aslan2142 closed this Nov 24, 2026
aslan2142 pushed a commit that referenced this pull request Nov 25, 2026
Carry only runtime/src/rect_list_geometry.h, byte-for-byte from 6aa8eb1. The stage generator and its original commits are by tedin7 (#207, carried forward in #682). This makes the draw-time change buildable against main while the explicit rectangle/quad work is in review.
ReXooGen pushed a commit to ReXooGen/leonida that referenced this pull request Nov 25, 2026
…xpands on the GPU (OLD-9940)

An indexed quad list was one triangle fan per quad over the application's index buffer.
That is wrong in two ways, both measured on ProjectLeonida#207 (RTX 4070 Ti there, RTX 4070 SUPER here):

- A fan's triangles are (v1 v2 v0) and (v2 v3 v0), and Vulkan takes a flat input from a
  triangle's first vertex. So a quad showed its second and third vertex, never its first:
  a quad with a red first vertex came out black and blue, 0 of 4096 pixels red.
- One draw per quad runs every instance of quad 0 before quad 1. D3D12 draws instance by
  instance, so where instances overlap the wrong one ended on top (2048 of 4096 pixels).

Neither can be fixed inside that scheme (VK_EXT_provoking_vertex cannot name a fan's hub
either), so the indices are rewritten: a compute pass (quad_index_expand.comp) reads the
application's index buffer on the GPU and writes (i0 i1 i2) (i0 i2 i3) for each quad as
32-bit indices into copy scratch, and the draw is one indexed triangle list over that
scratch with the application's instance count, base vertex and first instance. Both
triangles start at the quad's first vertex, like the non-indexed path.

- The host never reads the indices: a copy or a shader earlier in the list can have
  written them. The pass runs before the draw's rendering scope, between a barrier for
  those writes and one for its own, and between two segments of an active
  pipeline-statistics query, as the other helper dispatches do (D3D-162).
- 16-bit and 32-bit indices, read as whole words, so a 16-bit view at an address of
  2 modulo 4 works. Index values are copied as they are: VertexIndex - BaseVertex is still
  the application's index.
- The strip-cut values are ordinary indices, as in every list topology. An index past the
  view reads 0, as in D3D12, and the pass reads nothing past the view. A draw's last one to
  three indices are dropped.
- It needs bufferDeviceAddress. Without it, or inside a statistics query that cannot be
  split, the list is refused as unsupported.
- vulkan_topology(19) is a triangle list for both paths; nothing draws a fan any more
  (MoltenVK has no fans).

replay_quad_list_test replays indexed draws through prepare() and the real expander on a
recording device: one dispatch before the rendering scope and one draw in it, the push
constants for both index sizes, the view's end, the split of a draw larger than one
dispatch, the refusals, and the order inside a statistics query.

AI-assisted (Claude Code; Claude Opus 5.5 and Claude Fable 5.2).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf
ReXooGen pushed a commit to ReXooGen/leonida that referenced this pull request Nov 25, 2026
…ex, flat inputs, overlapping instances (d3d12_quad_index)

d3d12_rect_list draws its quads without an index buffer, so it could not see what ProjectLeonida#207's
review measured on indexed ones. d3d12_quad_index_test draws six 64x64 frames through
DrawIndexedInstanced with IASetPrimitiveTopology(19) and compares every pixel bit for bit:

  0  non-indexed control
  1  16-bit indices, a quad whose corners all differ: the first vertex's colour everywhere
  2  32-bit indices from StartIndexLocation 3
  3  16-bit indices from StartIndexLocation 5 (an address of 2 modulo 4), BaseVertexLocation 3
  4  two quads and two instances whose quads overlap: the later instance on top
  5  a view that ends inside the second quad: its last two indices read 0

The index buffer is in a DEFAULT heap and filled by a copy in the first list, so only the
GPU can read it. Two translated fixtures, like vs_m15 and ps_m15: vs_quad_inst.asm reads
the instance id (v3) to move and recolour each later instance, ps_quad_flat.asm reads its
colour with v_interp_mov, which makes the input Flat.

RTX 4070 SUPER (621.24), Windows 11, LEONIDA_VK_VALIDATION=1:
  on the six commits of ProjectLeonida#207: frames 1, 2, 3 and 5 match in 0 of 4096 pixels (black and
  blue, the second and third vertex), frame 4 in 2048, frame 0 in 4096; exit 1
  with the expansion: 4096 of 4096 in all six, 0 validation errors

run_gpu_tests.py lists the two fixtures as run by this test, and docs/testing.md names the
test (and d3d12_rect_list) among those that need llvm-mc and the SPIR-V tools.

AI-assisted (Claude Code; Claude Opus 5.5 and Claude Fable 5.2).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SR1Fv7eQNgZjM6miWPKjHf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants