Mesh shaders on Metal: outputs stored after their values, and kept under Metal's 32 KiB output limit #33

Closed
github-bot wants to merge 2 commits from gh-pr/33/head into gh-pr/33/base
Owner

@⁠64bitMango on GitHub · 2026-11-26 10:46 UTC · original
64bitMango:apple-converter-mesh-fixes → main · +1618 −430 · 20 files · 2 commits · closed without merging

What. Two Metal Shader Converter / Metal problems with mesh shaders, fixed in the translator.

  1. The converter builds an output row's vector at the row's first store, so a later channel made after it (a widened 16-bit half, a bitcast) is used before it is defined, and Apple's GPU compiler crashes in register allocation on every pipeline whose pixel shader reads the row. Options::mesh_stores_last stores each export's outputs after all their values.
  2. Metal refuses mesh pipelines over 32,768 bytes of output, counted as max vertices × the converter's output struct (container::metal_record). Options::mesh_pack_outputs orders the outputs of a mesh over the limit for the smallest record, found exactly (a search over the elements left of each size and the offset modulo 48), then leaves out clip distances that are +0.0 at every vertex. Left-out outputs keep the console's 16-bit slot map. The emit API (Kept, translate_keeping, pixel_reads(_channels), stages_link) can leave out or narrow outputs a pixel shader doesn't read; the D3D12 host's per-pipeline pruning is not part of this PR.

Measured on the Wine route (D3DMetal, the same converter): 30 of 302 mesh pipelines crashed the compiler before, none after with mesh_stores_last. Of 797 mesh+pixel pairs over the limit, packing alone fixed 584 (measured with the earlier greedy order; the exact search can only fit more). On this tree with Apple's standalone converter, Metal's reported sizes match the count exactly.

Where on. D3D12 host under Wine, where D3DMetal hits both problems (VICTORY_MESH_STORES_LAST, VICTORY_MESH_PACK =0|1). Off on the native host until it has mesh pipelines, and on real Windows. Off, translations are byte-identical: through the native host's path, all 19,105 shaders of a capture give DXIL identical to main with the native defaults.

Also. lower::Parts::new gives tests a default, so a new Parts field no longer touches every test. Tests use synthetic layouts only. Findings in docs/native-host.md.

Tests: cache-key test (host_workarounds_change_the_cache_key, Windows-only module: compiles under Windows clippy, not run on macOS); the reviewer's 32,768-byte packing case and two cases no order fits; the 16-bit slot map with an output left out. victory-shader 231 passed, victory-gpu 144 passed; Windows clippy and xtask policy clean.

**@⁠64bitMango** on GitHub · 2026-11-26 10:46 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33) `64bitMango:apple-converter-mesh-fixes` → `main` · +1618 −430 · 20 files · 2 commits · closed without merging **What.** Two Metal Shader Converter / Metal problems with mesh shaders, fixed in the translator. 1. The converter builds an output row's vector at the row's first store, so a later channel made after it (a widened 16-bit half, a bitcast) is used before it is defined, and Apple's GPU compiler crashes in register allocation on every pipeline whose pixel shader reads the row. `Options::mesh_stores_last` stores each export's outputs after all their values. 2. Metal refuses mesh pipelines over 32,768 bytes of output, counted as max vertices × the converter's output struct (`container::metal_record`). `Options::mesh_pack_outputs` orders the outputs of a mesh over the limit for the smallest record, found exactly (a search over the elements left of each size and the offset modulo 48), then leaves out clip distances that are +0.0 at every vertex. Left-out outputs keep the console's 16-bit slot map. The `emit` API (`Kept`, `translate_keeping`, `pixel_reads(_channels)`, `stages_link`) can leave out or narrow outputs a pixel shader doesn't read; the D3D12 host's per-pipeline pruning is not part of this PR. **Measured** on the Wine route (D3DMetal, the same converter): 30 of 302 mesh pipelines crashed the compiler before, none after with `mesh_stores_last`. Of 797 mesh+pixel pairs over the limit, packing alone fixed 584 (measured with the earlier greedy order; the exact search can only fit more). On this tree with Apple's standalone converter, Metal's reported sizes match the count exactly. **Where on.** D3D12 host under Wine, where D3DMetal hits both problems (`VICTORY_MESH_STORES_LAST`, `VICTORY_MESH_PACK` `=0|1`). Off on the native host until it has mesh pipelines, and on real Windows. Off, translations are byte-identical: through the native host's path, all 19,105 shaders of a capture give DXIL identical to main with the native defaults. **Also.** `lower::Parts::new` gives tests a default, so a new `Parts` field no longer touches every test. Tests use synthetic layouts only. Findings in docs/native-host.md. **Tests:** cache-key test (`host_workarounds_change_the_cache_key`, Windows-only module: compiles under Windows clippy, not run on macOS); the reviewer's 32,768-byte packing case and two cases no order fits; the 16-bit slot map with an output left out. victory-shader 231 passed, victory-gpu 144 passed; Windows clippy and `xtask policy` clean.
Two Metal Shader Converter / Metal problems with mesh shaders, fixed in
the translator.

1. The converter builds an output row's vector at the row's first
   store, so a later channel made after it (a widened 16-bit half, a
   bitcast) is used before it is defined, and Apple's GPU compiler
   crashes in register allocation on every pipeline whose pixel shader
   reads the row. Options::mesh_stores_last stores each export's
   outputs after all their values.
2. Metal refuses mesh pipelines over 32,768 bytes of output, counted as
   max vertices x the converter's output struct
   (container::metal_record). Options::mesh_pack_outputs orders the
   outputs of a mesh over the limit for the smallest record, found
   exactly (a search over the elements left of each size and the offset
   modulo 48), then leaves out clip distances that are +0.0 at every
   vertex. Left-out outputs keep the console's 16-bit slot map.
   translate_keeping and pixel_reads(_channels) leave out or narrow the
   outputs a pixel shader does not read (emit API; the D3D12 host's
   per-pipeline pruning is not part of this change).

Where on: the D3D12 host under Wine, where D3DMetal hits both problems
(VICTORY_MESH_STORES_LAST, VICTORY_MESH_PACK =0|1). Off on the native
host until it has mesh pipelines, and on real Windows. Off, translations
are byte-identical. lower::Parts::new gives tests a default, so a new
Parts field no longer touches every test. Tests use synthetic layouts
only. Findings in docs/native-host.md.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:43:17 +00:00
github-bot closed this pull request 2026-11-26 14:43:17 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 11:00 UTC · original · requested changes

Thanks, the analysis is thorough and the translator side (stores-last, packing, Kept/translate_keeping) reads well, with good tests that run on macOS. Before this can merge:

  1. It no longer compiles against current main. Main added a Parts { ... } initialiser in crates/victory-shader/src/emit/gfx_refusal_tests.rs (4e3689d), which lacks dropped_rows, so cargo test -p victory-shader and clippy --all-targets fail with E0063. Please rebase and add the field. A Default or a constructor for Parts would stop this recurring.
  2. Add a small unit test for the shader cache key. It should show the key (options_build / host_shader_keeping) changes with mesh_stores_last, mesh_pack_outputs and Kept.
  3. Native defaults. The native host turns both options on by default, but no native mesh pipeline exists yet, and nothing here runs them through the standalone converter. Please default them off on native until a mesh pipeline lands, or show how you checked the native path.
  4. Drop the D3D12/Wine-only parts. That means pruned_mesh, Stage.own, Shader::kept, mesh_prune, the _keeping cache plumbing and the windows-only draw test. They are about 250 lines for a path this port is moving away from, and they keep every mesh and pixel container in memory under Wine. Please keep the emit API (Kept, translate_keeping, pixel_reads*, stages_link) and its tests.
  5. Docs and env vars. Please split the roughly 4,000-character docs/macos.md bullet, and put the findings in docs/native-host.md. Please also make VICTORY_MESH_* parsing consistent between the two hosts: read once, exact "0"/"1".
  6. Use synthetic test data. Please replace the title-derived shader hashes and signature layouts in the tests (e.g. 1c451759513efb4c, ac82b7984c664350, da22b2adfd4f76d3) with synthetic layouts. The project keeps nothing taken from the game in the repo, including metadata.
**@⁠LakshanSivakumar** on GitHub · 2026-11-26 11:00 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#pullrequestreview-6072951837) · **requested changes** Thanks, the analysis is thorough and the translator side (stores-last, packing, `Kept`/`translate_keeping`) reads well, with good tests that run on macOS. Before this can merge: 1. **It no longer compiles against current main.** Main added a `Parts { ... }` initialiser in `crates/victory-shader/src/emit/gfx_refusal_tests.rs` (4e3689d), which lacks `dropped_rows`, so `cargo test -p victory-shader` and clippy `--all-targets` fail with E0063. Please rebase and add the field. A `Default` or a constructor for `Parts` would stop this recurring. 2. **Add a small unit test for the shader cache key.** It should show the key (`options_build` / `host_shader_keeping`) changes with `mesh_stores_last`, `mesh_pack_outputs` and `Kept`. 3. **Native defaults.** The native host turns both options on by default, but no native mesh pipeline exists yet, and nothing here runs them through the standalone converter. Please default them off on native until a mesh pipeline lands, or show how you checked the native path. 4. **Drop the D3D12/Wine-only parts.** That means `pruned_mesh`, `Stage.own`, `Shader::kept`, `mesh_prune`, the `_keeping` cache plumbing and the windows-only draw test. They are about 250 lines for a path this port is moving away from, and they keep every mesh and pixel container in memory under Wine. Please keep the `emit` API (`Kept`, `translate_keeping`, `pixel_reads*`, `stages_link`) and its tests. 5. **Docs and env vars.** Please split the roughly 4,000-character `docs/macos.md` bullet, and put the findings in `docs/native-host.md`. Please also make `VICTORY_MESH_*` parsing consistent between the two hosts: read once, exact "0"/"1". 6. **Use synthetic test data.** Please replace the title-derived shader hashes and signature layouts in the tests (e.g. `1c451759513efb4c`, `ac82b7984c664350`, `da22b2adfd4f76d3`) with synthetic layouts. The project keeps nothing taken from the game in the repo, including metadata.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 11:22 UTC · original · requested changes

Thanks for fixing the fixture. The earlier requests are still open:

  • Keep the translator API and tests, but drop the D3D12/Wine integration and the Windows draw test.
  • Default both native switches off.
  • Add cache-key coverage for the settings that remain. Also, Kept's key concatenates name and index with no delimiter, so ("ATTR", 12) and ("ATTR1", 2) collide.
  • Replace the title-derived fixtures and shader IDs (in the tests and in docs/macos.md) with synthetic data.
  • Move the findings into shorter notes in docs/native-host.md.
  • Parse the variables as exact 0/1, read once.

Also, please remove the AI Co-Authored-By trailer from 1b0b393. This repo doesn't take AI attribution lines.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 11:22 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#pullrequestreview-6073281726) · **requested changes** Thanks for fixing the fixture. The earlier requests are still open: - Keep the translator API and tests, but drop the D3D12/Wine integration and the Windows draw test. - Default both native switches off. - Add cache-key coverage for the settings that remain. Also, `Kept`'s key concatenates name and index with no delimiter, so `("ATTR", 12)` and `("ATTR1", 2)` collide. - Replace the title-derived fixtures and shader IDs (in the tests and in `docs/macos.md`) with synthetic data. - Move the findings into shorter notes in `docs/native-host.md`. - Parse the variables as exact `0`/`1`, read once. Also, please remove the AI `Co-Authored-By` trailer from 1b0b393. This repo doesn't take AI attribution lines.
Author
Owner

@⁠64bitMango on GitHub · 2026-11-26 11:53 UTC · original

Thanks for the review. All six points are addressed:

  1. Merged current main and added the missing field. lower::Parts::new gives tests a default, so new Parts fields no longer touch every test initialiser.
  2. With the _keeping key gone (point 4), the key logic is a plain function, options_key (same bytes as before). host_workarounds_change_the_cache_key checks that mesh_stores_last, mesh_pack_outputs and both together each change it. The module is Windows-only: the test passes Windows clippy but hasn't run on my Mac.
  3. Both options are off on the native host until a mesh pipeline lands.
  4. Removed pruned_mesh, Stage.own, Shader::kept, mesh_prune, the _keeping plumbing and the windows-only draw test. The emit API (Kept, translate_keeping, pixel_reads*, stages_link) and its tests stay.
  5. The macos.md bullet is split, and the findings are in docs/native-host.md. Both hosts read VICTORY_MESH_* once and accept exactly "0"/"1".
  6. The tests use synthetic layouts only, and I removed the shader hashes from the commit messages too.
**@⁠64bitMango** on GitHub · 2026-11-26 11:53 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#issuecomment-7191299164) Thanks for the review. All six points are addressed: 1. Merged current main and added the missing field. `lower::Parts::new` gives tests a default, so new `Parts` fields no longer touch every test initialiser. 2. With the `_keeping` key gone (point 4), the key logic is a plain function, `options_key` (same bytes as before). `host_workarounds_change_the_cache_key` checks that `mesh_stores_last`, `mesh_pack_outputs` and both together each change it. The module is Windows-only: the test passes Windows clippy but hasn't run on my Mac. 3. Both options are off on the native host until a mesh pipeline lands. 4. Removed `pruned_mesh`, `Stage.own`, `Shader::kept`, `mesh_prune`, the `_keeping` plumbing and the windows-only draw test. The `emit` API (`Kept`, `translate_keeping`, `pixel_reads*`, `stages_link`) and its tests stay. 5. The macos.md bullet is split, and the findings are in docs/native-host.md. Both hosts read `VICTORY_MESH_*` once and accept exactly "0"/"1". 6. The tests use synthetic layouts only, and I removed the shader hashes from the commit messages too.
Author
Owner

@⁠64bitMango on GitHub · 2026-11-26 11:56 UTC · original

@⁠LakshanSivakumar ready for another look when you have time. The review points are addressed above.

**@⁠64bitMango** on GitHub · 2026-11-26 11:56 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#issuecomment-7191368078) @⁠LakshanSivakumar ready for another look when you have time. The review points are addressed above.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 12:01 UTC · original · requested changes

Thanks for switching the native defaults off, replacing the fixtures, and removing the pipeline-pruning path. Two correctness cases remain:

  1. Keep the original FP16 slot map when pruning (emit/mod.rs ~782 vs lower.rs ~11877). If two FP16 elements share an export and the first is pruned, the kept one moves.
  2. The greedy packing in container.rs ~150 can miss a layout that fits. For example, position + twelve float4 + four float3 at 128 vertices gives 33,792 bytes, while placing the float3s after the first two float4s gives exactly 32,768. Please add that case.
    Please also remove or disambiguate Kept::describe() (("ATTR", 12) vs ("ATTR1", 2)), confirm whether the remaining Wine defaults in shader_cache.rs ~123/138 are intended, and squash or rebase so the earlier title-derived layouts and IDs (in efdff7a and 8abe1d0) aren't in the landed history.
**@⁠LakshanSivakumar** on GitHub · 2026-11-26 12:01 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#pullrequestreview-6073789766) · **requested changes** Thanks for switching the native defaults off, replacing the fixtures, and removing the pipeline-pruning path. Two correctness cases remain: 1. Keep the original FP16 slot map when pruning (`emit/mod.rs` ~782 vs `lower.rs` ~11877). If two FP16 elements share an export and the first is pruned, the kept one moves. 2. The greedy packing in `container.rs` ~150 can miss a layout that fits. For example, position + twelve float4 + four float3 at 128 vertices gives 33,792 bytes, while placing the float3s after the first two float4s gives exactly 32,768. Please add that case. Please also remove or disambiguate `Kept::describe()` (`("ATTR", 12)` vs `("ATTR1", 2)`), confirm whether the remaining Wine defaults in `shader_cache.rs` ~123/138 are intended, and squash or rebase so the earlier title-derived layouts and IDs (in efdff7a and 8abe1d0) aren't in the landed history.
Author
Owner

@⁠64bitMango on GitHub · 2026-11-26 12:36 UTC · original

Thanks. All points:

  1. FP16 slot map: Parts now carries the left-out elements themselves (dropped: Vec<SigElement>) instead of only their rows, and the 16-bit slots in export() are worked out from kept plus left-out elements, so the console's layout is kept. a_kept_16_bit_output_keeps_its_slots_when_one_before_it_is_left_out has two FP16 elements sharing a row with only the second kept; it fails without the fix.
  2. Packing: the greedy rule is replaced by an exact search over (elements left of each size, offset mod 48), memoised: at most ~700k states for 32 rows, usually a few hundred, and only for a mesh over the limit. Among equally small orders it keeps the old preferences. Your case (position + twelve float4 + four float3 at 128 vertices = 32,768) is a test, plus two cases no order fits; both failed with the greedy rule.
  3. Kept::describe(): removed. It had no callers outside tests, which now compare Kept values.
  4. Wine defaults: intended. mesh_stores_last() and mesh_pack_outputs() default to under_wine(): on for the D3D12 host under Wine, where D3DMetal hits the crash and the 32 KiB limit, off on real Windows and on the native host. VICTORY_MESH_STORES_LAST / VICTORY_MESH_PACK override with 0/1.
  5. History: squashed to one commit, so efdff7a, 8abe1d0 and the earlier layouts and IDs are no longer in the branch. The PR body is updated to match.
**@⁠64bitMango** on GitHub · 2026-11-26 12:36 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#issuecomment-7192225946) Thanks. All points: 1. **FP16 slot map:** `Parts` now carries the left-out elements themselves (`dropped: Vec<SigElement>`) instead of only their rows, and the 16-bit slots in `export()` are worked out from kept plus left-out elements, so the console's layout is kept. `a_kept_16_bit_output_keeps_its_slots_when_one_before_it_is_left_out` has two FP16 elements sharing a row with only the second kept; it fails without the fix. 2. **Packing:** the greedy rule is replaced by an exact search over (elements left of each size, offset mod 48), memoised: at most ~700k states for 32 rows, usually a few hundred, and only for a mesh over the limit. Among equally small orders it keeps the old preferences. Your case (position + twelve float4 + four float3 at 128 vertices = 32,768) is a test, plus two cases no order fits; both failed with the greedy rule. 3. **`Kept::describe()`:** removed. It had no callers outside tests, which now compare `Kept` values. 4. **Wine defaults:** intended. `mesh_stores_last()` and `mesh_pack_outputs()` default to `under_wine()`: on for the D3D12 host under Wine, where D3DMetal hits the crash and the 32 KiB limit, off on real Windows and on the native host. `VICTORY_MESH_STORES_LAST` / `VICTORY_MESH_PACK` override with 0/1. 5. **History:** squashed to one commit, so efdff7a, 8abe1d0 and the earlier layouts and IDs are no longer in the branch. The PR body is updated to match.
Author
Owner

@⁠64bitMango on GitHub · 2026-11-26 12:38 UTC · original

@⁠LakshanSivakumar ready for another look when you have time. The review points are addressed in the comment above, and the branch is squashed to one commit.

**@⁠64bitMango** on GitHub · 2026-11-26 12:38 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#issuecomment-7192272520) @⁠LakshanSivakumar ready for another look when you have time. The review points are addressed in the comment above, and the branch is squashed to one commit.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 12:46 UTC · original · approved these changes

Thanks, all five points check out. The kept-FP16 fixture fails without the dropped chain, the exact packing finds the 32,768-byte case, describe() is gone, the Wine defaults and exact 0/1 parsing are clear, and the squashed commit has no title-derived IDs or layouts. The aggregate counts in the docs (pipeline and pass counts) are fine as observations. Approved.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 12:46 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#pullrequestreview-6074363323) · **approved these changes** Thanks, all five points check out. The kept-FP16 fixture fails without the `dropped` chain, the exact packing finds the 32,768-byte case, `describe()` is gone, the Wine defaults and exact 0/1 parsing are clear, and the squashed commit has no title-derived IDs or layouts. The aggregate counts in the docs (pipeline and pass counts) are fine as observations. Approved.
Author
Owner

@⁠64bitMango on GitHub · 2026-11-26 13:17 UTC · original

Closing this: I'm continuing this work on my fork, 64bitMango/Native-Victory-macOS, where it's merged with the other open PRs (#33–#37). Thanks for the thorough reviews; the fixes from your latest feedback are included there.

**@⁠64bitMango** on GitHub · 2026-11-26 13:17 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#issuecomment-7193133521) Closing this: I'm continuing this work on my fork, [64bitMango/Native-Victory-macOS](https://github.com/64bitMango/Native-Victory-macOS), where it's merged with the other open PRs (#33–#37). Thanks for the thorough reviews; the fixes from your latest feedback are included there.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.