perf(shader): experiment with fixed mesh output limits and index initialization #469

Open
github-bot wants to merge 9 commits from github-bot/gh-469 into main AGit
Owner

@⁠tommiehansen on GitHub · 2026-12-04 14:38 UTC · original
tommiehansen:codex/mesh-output-counts → main · +335 −45 · 6 files · 9 commits · open

Why

Mesh shaders with small proven output counts can reserve storage for much larger outputs and initialize indices that are subsequently overwritten. On RDNA, declared output maxima may constrain export occupancy.

What changes

Two independent modes: VICTORY_MESH_OPT=1 uses a proven entry-block GS_ALLOC_REQ/reaching constant M0 to update counts, metadata, PSV0, export guards and connectivity together; mode 2 skips redundant index initialization only when a straight-line unmasked export covers every declared primitive. Mode 3 combines them; mode 0 remains default. Unproven paths fall back. Translator version and mode participate in cache identity.

Benchmark results

Historical synthetic fixture (not physical mesh time):

Mode Static vertex / primitive limits (DXIL + PSV0) Initial index writes / group DXIL bytes CPU µs / translation
0 — Baseline 32 / 32 32 2,168 40.06
1 — Counts 3 / 1 1 2,200 44.22
2 — Init elision 32 / 32 0 2,152 39.70
3 — Both 3 / 1 0 2,176 42.76

Both modes increase CPU translation time on this small fixture except init-only. Twelve production mesh variants and one pixel shader passed static DXIL→SPIR-V validation; no verified physical mesh speedup. Earlier audit.

Why this should be merged

Eliminate demonstrably redundant output reservation/initialization under proven conditions; the potential AMD occupancy benefit still requires representative physical measurements. Keep opt-in and review the two modes independently.

Validation

Current 755d41d CI: Linux full tests, Windows focused debug/release, production compilation and mesh translation variants passed. Full Windows suite retains the unchanged-main descriptor PSO failure (control).

Compatibility and dependencies

No HWRT dependency; overlaps lowering/cache with #467 and other shader changes. Separate from literal lane-count/NGG work; retain native AMD execution, exact output and occupancy/timing gates.

Previous detailed description, historical measurements and checklist.

**@⁠tommiehansen** on GitHub · 2026-12-04 14:38 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/469) `tommiehansen:codex/mesh-output-counts` → `main` · +335 −45 · 6 files · 9 commits · open ## Why Mesh shaders with small proven output counts can reserve storage for much larger outputs and initialize indices that are subsequently overwritten. On RDNA, declared output maxima may constrain export occupancy. ## What changes Two independent modes: VICTORY_MESH_OPT=1 uses a proven entry-block GS_ALLOC_REQ/reaching constant M0 to update counts, metadata, PSV0, export guards and connectivity together; mode 2 skips redundant index initialization only when a straight-line unmasked export covers every declared primitive. Mode 3 combines them; mode 0 remains default. Unproven paths fall back. Translator version and mode participate in cache identity. ## Benchmark results Historical synthetic fixture (not physical mesh time): | Mode | Static vertex / primitive limits (DXIL + PSV0) | Initial index writes / group | DXIL bytes | CPU µs / translation | | --- | ---: | ---: | ---: | ---: | | 0 — Baseline | 32 / 32 | 32 | 2,168 | 40.06 | | 1 — Counts | 3 / 1 | 1 | 2,200 | 44.22 | | 2 — Init elision | 32 / 32 | 0 | 2,152 | 39.70 | | 3 — Both | 3 / 1 | 0 | 2,176 | 42.76 | Both modes increase CPU translation time on this small fixture except init-only. Twelve production mesh variants and one pixel shader passed static DXIL→SPIR-V validation; no verified physical mesh speedup. [Earlier audit](https://github.com/tommiehansen/VIctory-xmem/actions/runs/47466412636). ## Why this should be merged Eliminate demonstrably redundant output reservation/initialization under proven conditions; the potential AMD occupancy benefit still requires representative physical measurements. Keep opt-in and review the two modes independently. ## Validation [Current 755d41d CI](https://github.com/tommiehansen/VIctory-xmem/actions/runs/47577544858): Linux full tests, Windows focused debug/release, production compilation and mesh translation variants passed. Full Windows suite retains the unchanged-main descriptor PSO failure ([control](https://github.com/tommiehansen/VIctory-xmem/actions/runs/47577277910)). ## Compatibility and dependencies No HWRT dependency; overlaps lowering/cache with #467 and other shader changes. Separate from literal lane-count/NGG work; retain native AMD execution, exact output and occupancy/timing gates. [Previous detailed description, historical measurements and checklist](https://github.com/tommiehansen/VIctory-xmem/blob/04dd44857abe222961157663b89a4dfed4c7277c/archive/pr-descriptions-20261206.md).
Recognize a single entry-wave GS_ALLOC_REQ with a constant reaching M0 definition. Use the allocated counts for DXIL declarations, initialization, export guards and connectivity validation. Keep the existing maximum-count path when compiler control flow, masks, or runtime counts prevent the proof, and invalidate cached translations.
When a fixed allocation fits within the thread count, a straight-line shader with an unconditional primitive export writes every allocated primitive. Skip the redundant degenerate-index stores after the uniform mesh count call. Preserve initialization for masked exports, control flow, missing exports and unproven allocations. Test the emitted index calls on both paths and invalidate cached translations.
Gate fixed output limits and primitive initialization elision independently and key the translation cache on the selected mode. Default to the original path until native validation. Add a four-way production-lowering comparison of static limits and emitted index calls, a portable translation benchmark, and four-way Windows framebuffer fixtures.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-04 14:41:58 +00:00
Author
Owner

@⁠Kal0s-design on GitHub · 2026-12-04 16:54 UTC · original

Checked 0ddf76c natively on Windows (x86_64-pc-windows-msvc, not Wine) on one NVIDIA desktop card and on WARP, with VICTORY_D3D12_DEBUG=1 and the DXIL validator from dxil.dll. No AMD card here, so nothing about occupancy or frame time, only whether the output is legal and draws the same.

Off by default

VICTORY_MESH_OPT is read once and anything other than 0 to 3 falls back to 0 (lower.rs:403-414). With mode 0, allocation is None and the init skip can't fire (lower.rs:7365-7391), so the mesh path is the same as on the merge base. I hashed every DXIL container the victory-shader lib tests produce (local hook in Shader::finish, not pushed) on the merge base 0fd7614 and on this PR with the switch unset. All 834 containers produced by tests that exist on both sides came out byte-identical. The only differences come from the new tests and from edge_flagged_triangles_draw_as_unflagged_ones, which the PR rewrote, and the six programs that test drew on main still come out with the same hashes from primitive_export_indices_drop_the_edge_flags and other tests on the PR.

Modes 1 to 3 on the devices

I wrote a local test (not pushed) that runs the synthetic triangle in all four modes, culled and not, with three prefixes: the PR's 3v/1p allocation, a 3v/2p allocation, and no allocation. For each it runs the DXIL validator, creates the mesh PSO and draws on both adapters, then drains the info queue after the PSO and after the draw. All 24 cases per adapter (3 prefixes x 4 modes x culled or not) validated, the validator's hash matched ours, the PSOs were created, the draws came out right (256/256 lit, or 0 for the culled triangle), and the info queue stayed empty on both the NVIDIA card and WARP. The PSV0 limits were 3/1 and 3/2 in the count modes and 32/32 otherwise, and the no-allocation program stayed 32/32 in every mode. The DXIL metadata takes the same reduced numbers (shader.rs:1086-1096 reads self.mesh), and the validator accepted all of them. The PR's own device test edge_flagged_triangles_draw_as_unflagged_ones also passes on both adapters under the debug layer.

On the D3D12 limits, mode 1 only ever lowers the maxima (lower.rs:7369-7371 keeps a proven count only if it's at or below the declared and lane-limited one), and the 32 KiB check runs before that (lower.rs:7350-7364), so the limits can't go up. setMeshOutputCounts is still a single call in the entry block, before any output write (lower.rs:7398-7399). Vertex and primitive exports past the new counts are still guarded by the same max_vertices and max_primitives (lower.rs:18554-18576, 18719-18731), so with 3/1 only lanes 0-2 write vertices and only lane 0 writes a primitive.

For the init skip I tried to break it. It needs a single block (exits == [End]) with an unguarded primitive export and max_primitives <= threads (lower.rs:7383-7391). Every primitive below the count then has a lane that writes it, since the export is only guarded by flat < max_primitives. Writing EXEC with a scalar op (s_mov_b32 exec_lo, 1 or s_and_b32 exec_lo, ...) between the request and the export can't slip through, because the translator refuses those programs in every mode ("a constant lane mask", "an opaque lane mask"). wave64 with 64 threads skips init as expected. With 16 threads and a 32/32 declaration it keeps init in mode 2 and only skips in mode 3, where the count is down to 1. I didn't find a case where an index is left unwritten.

Some things to look at

  1. TRANSLATOR_VERSION goes from 20 to 23 (lib.rs:30), one step per commit. The comment above it (lib.rs:22-29) says to bump only when a translation changes. Mode 0 output is identical, and the mode is already in the key (shader_cache.rs:133), so the bump just throws away everyone's cached translations and pipeline library (pipeline/known.rs:591-593 hashes the same key) for nothing. If you keep it, one step would do.
  2. The mask goes into the key even when it's 0 (shader_cache.rs:133). The switches just above only add bytes when on (shader_cache.rs:118-128), so mode 0 keys would stay valid. Together with 1, the PR could avoid invalidating caches for people who never set it.
  3. edge_flagged_triangles_draw_as_unflagged_ones now always uses the allocation prefix (mod.rs:2929-2940), so the program without the s_sendmsg that main drew is no longer drawn there. Mode 0 DXIL for the two differs (2164 vs 2168 bytes here), so one plain case could stay.
  4. Setting VICTORY_MESH_OPT=3 for the whole run changes nothing in the existing tests. The mesh, emit::tests, io and native tests give byte-identical output with it set and unset, because the only programs with a provable allocation are the PR's own fixtures, and those use the thread-local override. translated_mesh_shaders_draw_as_pc_ones skips without the console compiler, so I couldn't check how many real title mesh shaders fixed_mesh_counts would actually prove.

victory-shader lib tests (release, two threads): 419 passed and 1 failed on the merge base, 422 passed and 1 failed on the PR, not counting my two local tests. The failure is emit::size_tests::a_reused_descriptor_register_read_only_behind_its_flag on both sides, which fails on WARP here anyway. With VICTORY_MESH_OPT=3 set, the emit::tests, io, native and mesh filtered runs all pass. victory-gpu lib on the PR: 340 passed, 4 failed (the two indirect::host_list_* tests plus a_mesh_pipeline_loads_from_the_library and depth_bounds_pipeline_loads_and_filters_draws), the same four fail on the merge base here (it also had one watch:: failure that passed on the PR, unrelated). descriptor::tests::views_die_with_their_resource crashes the process on both sides here, so I skipped it. a_mesh_pipeline_loads_from_the_library compiles its shaders with the PC dxc, so it doesn't go through this code.

AI-assisted: yes

**@⁠Kal0s-design** on GitHub · 2026-12-04 16:54 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/469#issuecomment-7418146080) Checked 0ddf76c natively on Windows (x86_64-pc-windows-msvc, not Wine) on one NVIDIA desktop card and on WARP, with VICTORY_D3D12_DEBUG=1 and the DXIL validator from dxil.dll. No AMD card here, so nothing about occupancy or frame time, only whether the output is legal and draws the same. Off by default `VICTORY_MESH_OPT` is read once and anything other than 0 to 3 falls back to 0 (lower.rs:403-414). With mode 0, `allocation` is `None` and the init skip can't fire (lower.rs:7365-7391), so the mesh path is the same as on the merge base. I hashed every DXIL container the victory-shader lib tests produce (local hook in `Shader::finish`, not pushed) on the merge base 0fd7614 and on this PR with the switch unset. All 834 containers produced by tests that exist on both sides came out byte-identical. The only differences come from the new tests and from `edge_flagged_triangles_draw_as_unflagged_ones`, which the PR rewrote, and the six programs that test drew on main still come out with the same hashes from `primitive_export_indices_drop_the_edge_flags` and other tests on the PR. Modes 1 to 3 on the devices I wrote a local test (not pushed) that runs the synthetic triangle in all four modes, culled and not, with three prefixes: the PR's 3v/1p allocation, a 3v/2p allocation, and no allocation. For each it runs the DXIL validator, creates the mesh PSO and draws on both adapters, then drains the info queue after the PSO and after the draw. All 24 cases per adapter (3 prefixes x 4 modes x culled or not) validated, the validator's hash matched ours, the PSOs were created, the draws came out right (256/256 lit, or 0 for the culled triangle), and the info queue stayed empty on both the NVIDIA card and WARP. The PSV0 limits were 3/1 and 3/2 in the count modes and 32/32 otherwise, and the no-allocation program stayed 32/32 in every mode. The DXIL metadata takes the same reduced numbers (shader.rs:1086-1096 reads `self.mesh`), and the validator accepted all of them. The PR's own device test `edge_flagged_triangles_draw_as_unflagged_ones` also passes on both adapters under the debug layer. On the D3D12 limits, mode 1 only ever lowers the maxima (lower.rs:7369-7371 keeps a proven count only if it's at or below the declared and lane-limited one), and the 32 KiB check runs before that (lower.rs:7350-7364), so the limits can't go up. `setMeshOutputCounts` is still a single call in the entry block, before any output write (lower.rs:7398-7399). Vertex and primitive exports past the new counts are still guarded by the same `max_vertices` and `max_primitives` (lower.rs:18554-18576, 18719-18731), so with 3/1 only lanes 0-2 write vertices and only lane 0 writes a primitive. For the init skip I tried to break it. It needs a single block (`exits == [End]`) with an unguarded primitive export and `max_primitives <= threads` (lower.rs:7383-7391). Every primitive below the count then has a lane that writes it, since the export is only guarded by `flat < max_primitives`. Writing EXEC with a scalar op (`s_mov_b32 exec_lo, 1` or `s_and_b32 exec_lo, ...`) between the request and the export can't slip through, because the translator refuses those programs in every mode ("a constant lane mask", "an opaque lane mask"). wave64 with 64 threads skips init as expected. With 16 threads and a 32/32 declaration it keeps init in mode 2 and only skips in mode 3, where the count is down to 1. I didn't find a case where an index is left unwritten. Some things to look at 1. `TRANSLATOR_VERSION` goes from 20 to 23 (lib.rs:30), one step per commit. The comment above it (lib.rs:22-29) says to bump only when a translation changes. Mode 0 output is identical, and the mode is already in the key (shader_cache.rs:133), so the bump just throws away everyone's cached translations and pipeline library (pipeline/known.rs:591-593 hashes the same key) for nothing. If you keep it, one step would do. 2. The mask goes into the key even when it's 0 (shader_cache.rs:133). The switches just above only add bytes when on (shader_cache.rs:118-128), so mode 0 keys would stay valid. Together with 1, the PR could avoid invalidating caches for people who never set it. 3. `edge_flagged_triangles_draw_as_unflagged_ones` now always uses the allocation prefix (mod.rs:2929-2940), so the program without the `s_sendmsg` that main drew is no longer drawn there. Mode 0 DXIL for the two differs (2164 vs 2168 bytes here), so one plain case could stay. 4. Setting `VICTORY_MESH_OPT=3` for the whole run changes nothing in the existing tests. The mesh, `emit::tests`, `io` and `native` tests give byte-identical output with it set and unset, because the only programs with a provable allocation are the PR's own fixtures, and those use the thread-local override. `translated_mesh_shaders_draw_as_pc_ones` skips without the console compiler, so I couldn't check how many real title mesh shaders `fixed_mesh_counts` would actually prove. victory-shader lib tests (release, two threads): 419 passed and 1 failed on the merge base, 422 passed and 1 failed on the PR, not counting my two local tests. The failure is `emit::size_tests::a_reused_descriptor_register_read_only_behind_its_flag` on both sides, which fails on WARP here anyway. With `VICTORY_MESH_OPT=3` set, the `emit::tests`, `io`, `native` and `mesh` filtered runs all pass. victory-gpu lib on the PR: 340 passed, 4 failed (the two `indirect::host_list_*` tests plus `a_mesh_pipeline_loads_from_the_library` and `depth_bounds_pipeline_loads_and_filters_draws`), the same four fail on the merge base here (it also had one `watch::` failure that passed on the PR, unrelated). `descriptor::tests::views_die_with_their_resource` crashes the process on both sides here, so I skipped it. `a_mesh_pipeline_loads_from_the_library` compiles its shaders with the PC dxc, so it doesn't go through this code. AI-assisted: yes
Author
Owner

@⁠Xough on GitHub · 2026-12-04 21:43 UTC · original

Tested on an RX 6700 XT (12 GB, RDNA 2), Ryzen 9 7900X, CachyOS, ray tracing off, preset high, FSR 4.1 performance, no frame generation, 1920x1080 output. Built-in benchmark (VICTORY_BENCH=nort, 3 min each, first 60 s excluded), this PR merged on top of main. A warm-up run came first, so the translation cache was rebuilt before measuring.

VICTORY_MESH_OPT avg fps 1% low
0 (off) 31.3 18.0
3 (both), run 1 30.4 17.4
3 (both), run 2 30.8 17.4

No gain in the real title on this card; mode 3 is about 0.5 fps below mode 0, close to run-to-run noise (about 0.3-0.5 fps here). I only measured frame rates, not rendering correctness. The GPU frame here is spread over thousands of small dispatches and about 575 mesh dispatches per frame, so mesh occupancy may simply not be the limiter on this GPU.

**@⁠Xough** on GitHub · 2026-12-04 21:43 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/469#issuecomment-7425056098) Tested on an RX 6700 XT (12 GB, RDNA 2), Ryzen 9 7900X, CachyOS, ray tracing off, preset high, FSR 4.1 performance, no frame generation, 1920x1080 output. Built-in benchmark (`VICTORY_BENCH=nort`, 3 min each, first 60 s excluded), this PR merged on top of main. A warm-up run came first, so the translation cache was rebuilt before measuring. | `VICTORY_MESH_OPT` | avg fps | 1% low | |---|---|---| | 0 (off) | 31.3 | 18.0 | | 3 (both), run 1 | 30.4 | 17.4 | | 3 (both), run 2 | 30.8 | 17.4 | No gain in the real title on this card; mode 3 is about 0.5 fps below mode 0, close to run-to-run noise (about 0.3-0.5 fps here). I only measured frame rates, not rendering correctness. The GPU frame here is spread over thousands of small dispatches and about 575 mesh dispatches per frame, so mesh occupancy may simply not be the limiter on this GPU.
github-bot changed title from shader: experiment with fixed mesh output limits and index initialization to perf(shader): experiment with fixed mesh output limits and index initialization 2026-12-04 22:19:55 +00:00
Author
Owner

@⁠tommiehansen on GitHub · 2026-12-05 10:19 UTC · original

Thanks @⁠Kal0s-design for the cache and fixture findings. 9469e2a retains translator version 20, namespaces only nonzero mesh modes and includes no-allocation and 3v/2p cases. All 12 production mesh variants and the pixel shader passed static Vulkan conversion/validation; actual Vulkan mesh execution remains unverified. Thanks also @⁠Xough for the neutral RDNA2 measurements — no FPS gain is claimed.

**@⁠tommiehansen** on GitHub · 2026-12-05 10:19 UTC · [original](https://github.com/lobomfz/VIctory-xmem/pull/469#issuecomment-7436363032) Thanks @⁠Kal0s-design for the cache and fixture findings. [9469e2a](https://github.com/tommiehansen/VIctory-xmem/commit/9469e2a82fdecc53e77e99c8a2de492e652a359e) retains translator version 20, namespaces only nonzero mesh modes and includes no-allocation and 3v/2p cases. All 12 production mesh variants and the pixel shader passed static Vulkan conversion/validation; actual Vulkan mesh execution remains unverified. Thanks also @⁠Xough for the neutral RDNA2 measurements — no FPS gain is claimed.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/469/head:github-bot/gh-469'
git switch 'github-bot/gh-469'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-469'
git switch 'github-bot/gh-469'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-469'
git switch 'github-bot/gh-469'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-469'
git switch 'main'
git merge --squash 'github-bot/gh-469'
git switch 'main'
git merge --ff-only 'github-bot/gh-469'
git switch 'main'
git merge 'github-bot/gh-469'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.