Skip to content

Shader & GPU: support merged ES+GS geometry stages (vs_6_6 / gs_6_6) - #1399

Open
wbkepler wants to merge 1 commit into
Marvisak:mainfrom
wbkepler:shader/merged-es-gs-stages
Open

wbkepler wants to merge 1 commit into
Marvisak:mainfrom
wbkepler:shader/merged-es-gs-stages

Conversation

@wbkepler

Copy link
Copy Markdown

Summary

Rebased cleanly onto upstream/main (ffe310e):

Decodes, raises, lowers, validates, and wires console merged ES+GS hardware geometry stages (HwStage::Es -> vs_6_6 and HwStage::Gs -> gs_6_6):

  • victory-shader/src/native.rs: Decodes HwStage::Es and HwStage::Gs stage data (StageIo::Es, StageIo::Gs(GsIo) including vertex stride, GSVS ring stride, invocations, and max vertices).
  • victory-shader/src/resource.rs: Recognizes the console merged-GS internal ring-table SGPR pair (GS_TABLE) without disrupting root parameter tracking.
  • victory-shader/src/io.rs: Raises ES->GS and GSVS LDS traffic (ds_read* -> gs_reads, ds_write* -> lds_outputs), ES system VGPRs (VertexId, PrimitiveId, InstanceId), GS GsInstanceId, and s_sendmsg(MSG_GS / MSG_GS_DONE) stream operations (gs_ops).
  • victory-shader/src/emit/{container,shader,lower,mod,host}.rs:
    • Emits gs_6_6 modules with GSState entry property (tag 1), single-stream dx.viewIdState, and GS PSV0 metadata.
    • Lowers merged ES+GS active-lane preamble wave masks, dx.op.loadInput with vertex axis, dx.op.primitiveID.i32 (108), dx.op.gsInstanceID.i32 (100), dx.op.storeOutput, and emitStream/cutStream/emitThenCutStream (97/98/99), plus ES barrier elision and indirect handover return.
    • Adds Host::gs_pipeline for live D3D12 VS+GS+PS pipeline creation.
  • victory-gpu/src/pipeline{,/host}.rs: Includes HwStage::Gs in shader resource extraction, allows "geometry" in HostPipeline::new, adds D3D12_SHADER_VISIBILITY_GEOMETRY, binds desc.GS, and includes GS bytecode in the pipeline library cache key.
  • apps/victory/src/translate.rs: Includes HwStage::Gs and HwStage::Es in raising, DXIL emission, validation, host pipeline verification, and corpus reporting.
  • Adds unit tests covering Es/Gs stage data decoding, LDS/sendmsg raising, and live dxil.dll validation + D3D12 pipeline creation for both Es (vs_6_6) and Gs (gs_6_6).

Verification

  • cargo clippy -p victory-shader --all-targets & -p victory-gpu --all-targets — passed
  • cargo test -p victory-shader & cargo test -p victory-gpu pipeline — passed
  • cargo xtask policy — passed

@shortselow-byte

Copy link
Copy Markdown

For triage: #1315 also translates console geometry programs (single-stream triangle geometry, with a new emit/geometry.rs and a StageIo::Gs), and neither PR mentions the other. git merge-tree against the current heads shows they conflict in victory-gpu/src/pipeline/host.rs, victory-shader/src/emit/container.rs and victory-shader/src/emit/host.rs. Both are large (about 1,400 and 1,500 changed lines), so it is worth settling which one carries geometry stages before either is reviewed in depth.

This PR on its own, at d01f9a6 on main ffe310e, isolated target directory, Windows 11, Rust 1.98.1 MSVC, AMD Radeon RX 6900 XT: workspace 346 passed, 0 failed, including its four new tests; both clippy runs clean.

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump, and the shader tests on Ada

Result: +195 programs over main, 0 lost.

Offline. I merged head d01f9a6 onto main ffe310e (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 21,724 on main, 21,919 with this PR.

Failure on main Gained
resources: an address in an SGPR that is no root parameter 195

None of the 195 is among the programs that #1402 gains, so this PR adds to the bundles instead of overlapping them. It is the only open PR I have measured that clears "an address in an SGPR that is no root parameter" (195 on main; #333, #302 and #489 gained 0 for it in #1247). I'm measuring it stacked on #1402 and #1597 as well.

Tests: with the same merge, cargo test --locked -p victory-shader --lib passed 124, failed 0. The bitcode benchmark is ignored, as on main, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with VICTORY_WARP unset.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

@sinan-cmyk

Copy link
Copy Markdown

Static review of the ES/GS lowering (head d01f9a6)

Summary: On a retail dump this PR gains 195 programs, 0 lost (measurement), and its tests pass on Ada. Those counts only prove that the DXIL validates. Below are the places where the output could still be wrong although it validates. Line numbers refer to the PR head.

Likely bug

1. The emitted vertex's index is computed, then ignored. Outputs are stored at the ds_write, not at their emit. (emit/lower.rs:2308–2335, io.rs:811)

raise_es_gs_lds works out which emitted vertex each GSVS write belongs to (LdsOutput.vertex = dword / vertex_dwords), but the lowering never reads out.vertex. Each write becomes a storeOutput at its own site, and each s_sendmsg(MSG_GS, EMIT) becomes an emitStream.

This is correct only if, for every vertex k, all of its writes come after emit k−1 and before emit k. The synthetic test (emit/mod.rs, gs_shader_validates_and_builds_host_pipeline) has exactly that order. A compiler that batches writes breaks it, and the result still validates:

  • Two vertices in one instruction. A ds_write2_b32 whose offset0 and offset1 fall into two vertex slots.
  • Writes hoisted ahead of an earlier emit. For example, all vertices written, then three emits.

In both cases the host emits the last-written values for several vertices, so primitives collapse or stretch.

Suggested fix: keep a counter of emits per stream. For every write between emit k−1 and emit k, require out.vertex == k, and refuse the program otherwise (for example "GSVS write to a vertex other than the next emitted"). Or buffer the writes per vertex and store them right before the matching emitStream.

Risks (cannot be confirmed without one real ES/GS pair)

2. The LDS layout is assumed to follow signature rows, not param slots. (io.rs GS reads and ES/GS writes; lower.rs:2310)

Both ES→GS and GSVS dwords are mapped as row = dword / 4, channel = dword % 4 and matched against the signature row (o.row == Some(out.row)). The VS export path maps rows through the native outputs table (VsOutput.param). The LDS path ignores that table.

If the console lays out ES→GS or GSVS memory by parameter slot, or compacts unused components, inputs and outputs are silently mixed up. Both ES and GS use the same rule, so they agree with each other but can both be wrong.

A cheap guard: check vertex_dwords against the signature, for example vertex_dwords == 4 * (max row + 1) of the ES outputs and GS inputs, and refuse when it does not match. Better: verify on one title pair whether the rows or the params decide the layout.

3. Silent defaults for the GS state. (emit/mod.rs:292–294)

When PSV0 is missing or holds 0, these defaults apply:

  • input primitive = triangle (3)
  • output topology = triangle strip (5)
  • stream mask = 1

A wrong input primitive changes how many vertices loadInput can address. It also changes the host PSO's topology-type compatibility. Refusing ("a geometry shader without PSV0 state") would be safer than guessing.

4. Non-zero streams have no host consumer. (io.rs:832)

Emit(stream) and Cut(stream) pass the stream through, and the PSO has no stream-output. A console GS that feeds stream-out, or rasterizes a stream other than 0, would produce output that the host silently drops. Refusing when the stream mask is not 1, or when an op targets a stream other than 0, would make that visible.

5. A GSVS or ES→GS write with no signature row is dropped silently. (lower.rs:2317: let Some((id, col)) = found else { continue; })

A real output whose row the signature lacks becomes undefined on the host. Other paths refuse in that case ("a GS input the signature lacks").

Nits

  • GS_TABLE accepts s[8:9] as the console GS table whenever no root parameter sits at user data 0–1 (resource.rs:42, 668). Any value loaded from it is refused (lower.rs:3072), so a wrong guess fails safely. Its only effect is the per-lane ring base, which the lowering ignores.
  • Barriers:
    • ES drops every barrier (lower.rs:527). That is fine for a VS on the host.
    • A GS that contains an s_barrier would emit a DXIL barrier that the validator rejects. That costs a refusal, not wrong code.
  • Unrelated formatting in victory-gpu/src/pipeline.rs (note_why) and pipeline/host.rs makes the diff harder to review.

What looks right

  • SGPRs: user data starts at s8 for the merged stages (HwStage::user_sgpr).
  • VGPRs:
    • ES system values: v5 vertex id, v7 primitive id, v8 instance id.
    • GS: v0, v1 and v4 carry the packed 16-bit ES→GS vertex offsets for 6 vertices (adjacency), v2 the primitive id, v3 the instance id.
    • This matches the GFX9/GFX10 legacy merged ES/GS layout that LLVM and Mesa use.
  • DXIL operations: 97 EmitStream, 98 CutStream, 99 EmitThenCutStream, 100 GSInstanceID, 108 PrimitiveID.
  • GS state: the GSState metadata order (input primitive, max vertices, stream mask, topology, instance count), and the GS part of PSV0.
  • Merged-wave EXEC: the preamble (s_bfe s3 → s_bfm → s_cselect) is treated as all lanes active. That is correct: one host invocation is one ES vertex or one GS primitive.
  • Indirect jump: the ES's s_setpc handover is lowered as a return.
  • Host pipeline:
    • The GS is bound only when no mesh shader is present (pipeline/host.rs:326).
    • The PSO cache key is unchanged for pipelines without a GS, because the GS bytes are appended only when present (pipeline/host.rs:552).

What the tests cover, and what they don't

Covered:

  • parsing ES/GS stage data
  • one GS with interleaved write/emit for 3 vertices and a cut
  • one ES write with a v_mad_u32_u24 base
  • a host GS pipeline, when a device exists

Not covered:

  • writes for two vertices before an emit (finding 1)
  • adjacency inputs (v4)
  • invocations > 1
  • non-zero streams
  • missing PSV0
  • ES outputs whose row differs from their param slot
  • an actual title ES/GS pair

AI-assisted (Claude Code); static review only, nothing run.

@wbkepler
wbkepler force-pushed the shader/merged-es-gs-stages branch from d01f9a6 to e80b166 Compare November 24, 2026 22:39
@wbkepler

Copy link
Copy Markdown
Author

Rebased onto 70564ae as e80b166 (one commit; the old branch conflicted in about ten files, so this is a re-port rather than a replay).

What changed in the design:

  • Reuses upstream's geometry-shader scaffolding from the rectangle-list work instead of the parallel state the old branch carried. Shader::gs (and the GSState / PSV0 / view-id output it drives) is generalized from "triangles in, strips out, n vertices" to one small Geometry value: input primitive, output topology, maximum vertices, instances, one stream. rectangle_gs and title geometry programs go through the same field; the rectangle test passes unchanged.
  • Lowering rewritten against the current lower.rs: vertex-axis loadInput shares the existing input loader, stream messages become emitStream / cutStream / emitThenCutStream, the merged prologue's lane masks are "every invocation", the export program's barrier is dropped and its handover jump is a return. The stage's own table (s[8:9]) gets its own address kind next to MEMORY / USER_DATA, so it is not taken for title memory.
  • Refused rather than guessed: 16-bit values in the vertex rings (the packing upstream added for exports is not followed into LDS), output streams other than 0, and input vertices past the declared primitive.
  • GPU: a title geometry shader takes the host pipeline's geometry stage. A pipeline that has one and a rectangle-list topology is refused with a logged reason; the two are not chained.
  • Container choice: where a vertex container holds both a plain vertex program and an export program, the vertex program is translated (it feeds the host geometry stage equally well); the export program is used only when it is the only one.
  • translate counts export and geometry programs separately; the pipeline pre-build makes records that have an export and a geometry stage instead of skipping them.

Checked on this base:

  • clippy -D warnings on victory-shader, victory-gpu, victory-game and on victory --features diagnostics: clean. cargo xtask policy: ok.
  • cargo test -p victory-shader -p victory-gpu -p victory-game: everything passes except five tests that fail the same way on unmodified 70564ae on this machine (RTX 5090): stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, synthetic_tests::gathers_at_a_level_match_the_hardware in victory-shader, and dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views in victory-gpu. None of them touch this change.
  • New/kept tests: the stage data, the I/O raising, and a merged pair executed on the GPU (export program as the vertex shader, geometry program after it, one triangle drawn and read back).

Not checked: the whole-title program counts were not re-measured on this base, no live run was made with these pipelines, and the pre-build path for these records has not been compared against a run's pipeline keys.

AI-assisted (Claude Code); checked on Windows 11 / RTX 5090.

@molodayamama

molodayamama commented Nov 24, 2026 •

Copy link
Copy Markdown

I reproduced the GSVS write/emit ordering issue raised above on the rebased head e80b166, using the PR's existing synthetic triangle and an RTX 4060 (driver 610.88, Windows 10, D3D12 Agility SDK 620). I moved the three stream-0 Emit messages after all LDS writes, keeping the three positions and final Cut unchanged.

Build Interleaved writes and emits All three vertices written before the first emit
e80b166 with only the expanded test 496 lit pixels 0 lit pixels
With the patch 496 lit pixels 496 lit pixels

With the patch the full 32 × 32 RGBA readbacks match exactly. The failure on the PR head was visible in rendering even though the generated shaders passed DXIL validation.

Patch and regression test · .patch file

The patch queues writes by LdsOutput.vertex for straight-line geometry shaders and lowers each slot's writes, in original order and with their original masks, just before its Emit or EmitCut. The regression runs with cargo test -p victory-shader --lib merged_geometry_stages_draw_their_triangle -- --nocapture. This tests the single-block case only. The whole-title impact is unmeasured.

Full victory-shader library suite: 207 passed, 5 failed, 2 ignored. All five failures reproduced on the unmodified PR head with the same local setup.

@slatefield

Copy link
Copy Markdown

Rebased this onto current main (4118f33) and folded in the batched-write fix from the thread, since without it a geometry shader that writes a vertex ahead of the previous emit renders 0 lit pixels while still validating. Two commits on the branch, both with their authors: your merged-geometry commit unchanged, and molodayamama's 9607c49 "Publish batched geometry ring writes at each vertex emit" on top, with their regression test.

Rebase conflicts were three hunks: native.rs keeps main's vs_outputs(data) wrapper from the mesh fix and moves the body into vs_outputs_at(data, at, count_at) for GS outputs, io.rs keeps main's corrected 9-bit mesh exp prim wording plus your paragraph, and one test literal gains gs: None. A per-file added/removed-line comparison against your e80b166 shows 20 of the 21 files identical in both directions, the only delta being that wrapper.

cargo test -p victory-shader --lib 146 passed, 2 ignored on Linux (main 144), Windows-target clippy clean both feature sets, asset policy ok. The #[cfg(windows)] validation and draw tests need a GPU.

It is open as #1924 so the maintainer can take a ready branch including the ordering fix. If you would rather update this PR yourself, reset shader/merged-es-gs-stages to the branch head there and I will close mine. The open review risks (LDS layout from signature rows vs param slots, silent PSV0 defaults, non-zero streams, missing-row writes) are listed in the description as follow-ups; I did not change them.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs work. Thanks for the rebase: 2900907 merges cleanly with main 940d32f, and this is the first content review. The PR decodes merged export and geometry stage data (es_io/gs_io in crates/victory-shader/src/native.rs) and raises LDS traffic and s_sendmsg(MSG_GS) in io.rs. It emits export programs as vs_6_6 and geometry programs as gs_6_6, traces the s[8:9] table as GS_TABLE in resource.rs, and wires a VS+GS+PS pipeline through victory-gpu, prebuild.rs and translate.rs.

This changes emulation behaviour, and the PR states no evidence for it:

  • Stage-data layout. The new offsets (ES_VERTEX_DWORDS = 0x1ac, GS_OUTPUTS = 0x150, GS_RING_DWORDS = 0x27c, GS_INVOCATIONS/GS_MAX_VERTICES/GS_VERTEX_DWORDS at 0x280-0x28c) and the PSV0 input-primitive read in Host::pipeline_gs need a stated source. For example, show that the decoded values agree with the programs' own [maxvertexcount]/ring strides across the title's geometry programs.
  • Behaviour. reads_es_and_gs_stage_data writes the same offsets the decoder reads, so it passes by construction. The other new tests check that the output validates and that a PSO can be created, not that it draws the right thing. Please add a rendered comparison against a reference geometry shader, as Shaders, GPU: translate single-stream triangle geometry programs #1315 does in emit/geometry_tests.rs, and give an offline --validate --list count on main showing which title programs this moves from refused to validated.
  • Overlap. Shaders, GPU: translate single-stream triangle geometry programs #1315 (single-stream triangle geometry programs, now a merge candidate) and Shader & GPU: merged geometry stages (rebase of #1399, with the batched GSVS write fix) #1924 cover the same geometry programs with a different design: the ring writes raised through the copy VS, with explicit refusals for other streams, mismatched PSV0 state and rect lists. This PR adds a third path through HwStage::Gs. Please say which programs this handles that those do not, or whether it supersedes them, so the maintainers can choose one direction.

CI: passing (check).


Generated by Claude Code

r4tedx added a commit to r4tedx/VIctory that referenced this pull request Nov 25, 2026
A GS that stores all three vertices' ring slots before its first EMIT,
the write batching that made Marvisak#1399 draw nothing: each EMIT still takes
its own vertex's values (checked through the input reads they come
from). raise takes each slot at the EMIT that consumes it, by its offset
in the invocation's ring, so it needs no change.

Untested at commit time: waiting for the machine's test queue (WARP).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…etry programs as gs_6_6)

The console runs a vertex shader that feeds a geometry shader, and that
geometry shader, as the two halves of one wave, the vertices between them
in LDS. An export program now translates to a plain vertex shader (its
LDS writes are its outputs) and a geometry program to a geometry shader
(its LDS reads are its input primitive's vertices, its LDS writes the
outputs of the vertex its next stream message emits).

- native: `StageIo::Es` / `StageIo::Gs(GsIo)` stage data.
- resource: the geometry stage's own table (`GS_TABLE`, s[8:9]).
- io: ring reads and writes raised to inputs and outputs by row and
  channel; `s_sendmsg` GS / GS_DONE raised to emit, cut and done.
- emit: the geometry-shader state of rectangle lists (`Shader::gs`, the
  `GSState` and `PSV0` it writes) is generalized from "triangles in,
  strips out, n vertices" to a `Geometry` (input primitive, output
  topology, maximum vertices, instances; one stream) that the generated
  rectangle shader and title shaders share. Lowering: vertex-axis
  `loadInput`, `emitStream` / `cutStream` / `emitThenCutStream`,
  `primitiveID`, `gsInstanceID`, the prologue's lane masks as "every
  invocation", no barrier and a return for the export program's handover.
  16-bit values in the rings and streams other than 0 are refused.
- gpu: a title's geometry shader takes the host pipeline's geometry
  stage (root parameters visible to it); a pipeline with one and a
  rectangle-list topology is refused with a reason rather than chained.
- app: `translate` counts export and geometry programs; the pipeline
  pre-build makes records with an export and a geometry stage.

Tests: stage data, I/O raising, and a merged pair drawn on the host
(export program as vertex shader, geometry program after it).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wbkepler
wbkepler force-pushed the shader/merged-es-gs-stages branch from e80b166 to 2900907 Compare November 25, 2026 07:10
@wbkepler

Copy link
Copy Markdown
Author

Rebased onto current main (940d32f) as 2900907; conflicts were with the new write-mark Options/Parts::view_marks (kept both, gs added alongside) and the split-out vs_outputs in native.rs (now takes the table offset so the geometry stage can share it).

AI-assisted (Claude Code); checked on Windows 11 / RTX 5090.

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.

6 participants