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

Closed
64bitMango wants to merge 2 commits from upstream-pr/33 into main
64bitMango commented 2026-11-26 10:46:37 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by 64bitMango on 2026-11-26T10:46:37Z.


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6624949347 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33). Originally posted by [64bitMango](https://github.com/64bitMango) on 2026-11-26T10:46:37Z. --- **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.
LakshanSivakumar commented 2026-11-26 11:00:20 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T11:00:20Z.

Review: CHANGES_REQUESTED


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.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6072951837 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#pullrequestreview-6072951837). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T11:00:20Z. **Review:** CHANGES_REQUESTED --- 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 commented 2026-11-26 11:22:19 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T11:22:19Z.

Review: CHANGES_REQUESTED


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6073281726 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#pullrequestreview-6073281726). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T11:22:19Z. **Review:** CHANGES_REQUESTED --- 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.
64bitMango commented 2026-11-26 11:53:33 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by 64bitMango on 2026-11-26T11:53:33Z.


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.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7191299164 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#issuecomment-7191299164). Originally posted by [64bitMango](https://github.com/64bitMango) on 2026-11-26T11:53:33Z. --- 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 commented 2026-11-26 11:56:48 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by 64bitMango on 2026-11-26T11:56:48Z.


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

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7191368078 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#issuecomment-7191368078). Originally posted by [64bitMango](https://github.com/64bitMango) on 2026-11-26T11:56:48Z. --- @LakshanSivakumar ready for another look when you have time. The review points are addressed above.
LakshanSivakumar commented 2026-11-26 12:01:51 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T12:01:51Z.

Review: CHANGES_REQUESTED


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.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6073789766 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#pullrequestreview-6073789766). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T12:01:51Z. **Review:** CHANGES_REQUESTED --- 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.
64bitMango commented 2026-11-26 12:36:39 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by 64bitMango on 2026-11-26T12:36:39Z.


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.
<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7192225946 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#issuecomment-7192225946). Originally posted by [64bitMango](https://github.com/64bitMango) on 2026-11-26T12:36:39Z. --- 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 commented 2026-11-26 12:38:45 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by 64bitMango on 2026-11-26T12:38:45Z.


@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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7192272520 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#issuecomment-7192272520). Originally posted by [64bitMango](https://github.com/64bitMango) on 2026-11-26T12:38:45Z. --- @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.
LakshanSivakumar commented 2026-11-26 12:46:42 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T12:46:42Z.

Review: APPROVED


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:review:6074363323 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#pullrequestreview-6074363323). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T12:46:42Z. **Review:** APPROVED --- 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.
64bitMango commented 2026-11-26 13:17:18 +00:00 (Migrated from github.com)

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by 64bitMango on 2026-11-26T13:17:18Z.


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.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7193133521 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/33#issuecomment-7193133521). Originally posted by [64bitMango](https://github.com/64bitMango) on 2026-11-26T13:17:18Z. --- 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

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS!33
No description provided.