Skip to content

Recompiler: filter OOB triangles, gate no-store emits, and clamp LDS in HLSL geometry shaders - #2517

Open
Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-hlsl-geom-epilogue-oob
Open

Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-hlsl-geom-epilogue-oob

Conversation

@Decodere

Copy link
Copy Markdown
Contributor

Part of #1401
Part of #1833

What is in it

Deduplicates the native-wave64 and split-wave64 HLSL Mode::Geometry epilogues in crates/xcl/src/gpu/recomp/emit.rs into a single emit_gs_epilogue helper and fixes four record-packing bugs:

  • Cap per-lane MSG_GS emits at max_vertices (<= 256): guards the op & 2 != 0 emit path in Em::sopp with if (k < {maxv}u) so runaway emits cannot overflow the 10-bit packed vertex index fields in xcl_tri or inflate wave prefix sums past the record vertex budget.
  • Gate no-store emit lanes before wave sums: zeroes xcl_emitted and xcl_ntri when xcl_gs_base == 0xFFFFFFFFu before WavePrefixSum / WaveActiveSum, preventing lanes that never wrote LDS from reserving uninitialized vertex slots and emitting phantom triangles that point to them.
  • Filter out-of-bounds triangles on straddling/overflow lanes: computes each lane's in-bounds vertex count xcl_vw = (xcl_vb < maxv) ? min(xcl_emitted, maxv - xcl_vb) : 0u before summing triangles and trims xcl_ntri to the exact prefix whose top vertex index (xcl_tri[xcl_vn] >> 20) is < xcl_vw. Because xcl_tri entries are recorded in strictly increasing k order with k in bits 20..29, every kept triangle has all three vertices < maxv.
  • Clamp split-wave64 epilogue LDS reads to XCL_X_LDS_TOP: mirrors Em::ds (min(((src + off) >> 2) & 8191u, XCL_X_LDS_TOP)) when x64 == true so vertex reads cannot alias the wave64 exchange region at the top of the 32 KB groupshared uint xcl_lds[8192] array.

Tested

  • Failing-first on unmodified origin/main (6d12f58a229bc73889878a448c64dc3af25eeba0) with the two new tests added to gpu::recomp::emit::tests:
    • hlsl_geometry_epilogue_filter_and_split_lds_clamp panicked at emit.rs:6855: missing k < maxv guard (split=false).
    • gpu_hlsl_geometry_epilogue_oob_and_no_store panicked on RTX 4060 at emit.rs:6882: 15x20 overflow emitted (256, 256, ...) instead of (256, 230, ...) (26 triangles referencing out-of-bounds vertices >= 256).
  • Post-fix targeted release tests on NVIDIA GeForce RTX 4060 (DXC 1.9.2609.5, 0 compiler warnings in emit.rs):
    • cargo test --release -p xcl --lib gpu::recomp::emit::tests -- --test-threads=1 --nocapture: 6 passed; 0 failed; 0 ignored (0 skipped; both hlsl_geometry_epilogue_filter_and_split_lds_clamp and gpu_hlsl_geometry_epilogue_oob_and_no_store passed, verifying the 15x20 overflow trims to (256, 230) with final straddled triangle 254 | (253 << 10) | (255 << 20) and all 230 triangles < 256, and lane-0 no-store gating packs lane 1 at (3, 1) with vertices 0x1111_0000..0x1111_0002).
    • cargo test --release -p xcl --lib gpu::dxil::geometry_tests -- --test-threads=1 --nocapture: 4 passed; 0 failed; 0 ignored (0 skipped).
  • Post-fix targeted release tests on WARP (XCL_ADAPTER=warp):
    • cargo test --release -p xcl --lib gpu::recomp::emit::tests -- --test-threads=1 --nocapture: 6 passed; 0 failed; 0 ignored (hlsl_geometry_epilogue_filter_and_split_lds_clamp passed with 0 skips including DXC cs_6_6 compilation of both split=false and split=true; gpu_hlsl_geometry_epilogue_oob_and_no_store cleanly reported SKIPPED: no GPU / DXC / mesh shaders).
  • Full release workspace (cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture):
    • 37 test binaries: 1425 passed; 0 failed; 46 ignored.
    • 29 pre-existing runtime SKIPPED messages (21 no wave64 on wave32-only RTX 4060, 3 per-lane form: refused, 3 no D3D12 debug layer / no d3d12SDKLayers.dll, 1 cannot disable the driver cache, 1 no heap windows).

Regression run

Before (origin/main 6d12f58a229bc73889878a448c64dc3af25eeba0):

xcl-smoke v1
commit: 6d12f58a229bc73889878a448c64dc3af25eeba0
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1523 (PresentX 1524)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13983 MB

After (fix-hlsl-geom-epilogue-oob 1017d247ff8677f5505d33aecbae9efb167719a2):

xcl-smoke v1
commit: 1017d247ff8677f5505d33aecbae9efb167719a2
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1490 (PresentX 1490)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13978 MB

Both 60 s runs reach steady 60 60 60 presents per 2 s with 0 device removals, 0 crashes, 1490 vs 1523 frames shown (97.8%), and 13978 MB vs 13983 MB peak commit.

Not tested

  • Native wave64 GPU execution of the split=false HLSL geometry epilogue (RTX 4060 only supports wave32; split=false was verified via HLSL codegen checks and DXC cs_6_6 compilation, while GPU execution exercised split=true).
  • D3D12 debug-layer tests (d3d12SDKLayers.dll is not installed on this machine).

AI use

Written and verified with Google Antigravity (Gemini 4 Neon). All code changes, failing-first reproductions, targeted RTX 4060 and WARP test runs, the full release workspace suite, and both paired 60 s GPU smoke runs were executed and checked locally.

…in HLSL geometry shaders

Deduplicate the native-wave64 and split-wave64 HLSL Mode::Geometry epilogues
into emit_gs_epilogue and fix four record-packing bugs:

1. Cap per-lane MSG_GS emits at the record vertex budget (k < maxv, <= 256)
   so runaway emits cannot overflow the 10-bit packed index fields in xcl_tri.
2. Zero xcl_emitted and xcl_ntri before wave prefix/active sums for lanes that
   emitted without ever storing to LDS (xcl_gs_base == 0xFFFFFFFFu), preventing
   uninitialized vertex holes and phantom triangles referencing them.
3. Compute each lane's in-bounds vertex count xcl_vw before summing triangles
   and trim xcl_ntri to the exact prefix whose top vertex index (t >> 20) fits
   within xcl_vw, preventing out-of-bounds vertex indices when a lane straddles
   or exceeds max_vertices.
4. Clamp split-wave64 epilogue LDS reads to XCL_X_LDS_TOP, matching ds() so
   geometry vertex reads cannot alias the wave64 exchange region at the top of
   the 32 KB groupshared array.

Part of xcl-dev#1401
Part of xcl-dev#1833

Assisted-by: Google Antigravity (Gemini 4 Neon)
@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

@DarkPanda69

Copy link
Copy Markdown

AMD run (RX 9070 XT, RDNA4) plus an RTX 3060: the new GPU test passes on both cards.

Head 1017d24 applied on main 6d12f58, debug, Windows 11 Pro 26200, cargo test -p xcl --lib -- gpu:: --test-threads=4, card chosen with XCL_ADAPTER:

One note on the "native wave64" gap: the test builds its program with translate_geometry(code, g.clone(), true, 1), which means split_wave64 = true. So on the RX 9070 XT it runs the split epilogue too, and the native (split=false) path still has no GPU run. A variant that passes runner::split_wave64() there (as supported_modes() does in #2488) would run native wave64 on this card.

Not tested: retail geometry shaders, Intel.
AI use: Claude Code (Claude Opus 5.5) ran the tests on this machine and wrote this report.

@SlopTart

Copy link
Copy Markdown

Tested: current PR head merged onto current origin/main (clean), Linux (Arch, kernel 7.2.15), Mesa 26.2.5 RADV, Radeon RX 7900 XT (NAVI31), cargo target x86_64-pc-windows-gnu run under wine-11.0 (Proton), rustc 1.94.0, vkd3d-proton 3.1 with the XCL patch, official MS DXC (binaries 2026-09-29).

gpu_ suite (cold cache): 165 passed / 3 failed, no crashes. The three are the known baseline on this lane (seed-1003 fuzz, mad/mac contraction, promoted-UAV-clear driver gap, PR #2490); none caused by this PR.
gpu_hlsl_geometry_epilogue_oob_and_no_store ran inside the suite and is clean. Targeted run of hlsl_geometry_epilogue_filter_and_split_lds_clamp: passed.

Not tested: retail titles (no title data on this lane, counts only).

AI use: Hermes agent (local Qwen-class) ran both machines and drafted this; I checked it before posting.

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.

4 participants