Skip to content

fix(shader,gpu): repack linked mesh outputs to remove Metal alignment holes - #401

Open
gdemontalivet wants to merge 1 commit into
pragyaangaur:mainfrom
gdemontalivet:fix/macos-repack-linked-mesh-outputs
Open

gdemontalivet wants to merge 1 commit into
pragyaangaur:mainfrom
gdemontalivet:fix/macos-repack-linked-mesh-outputs

Conversation

@gdemontalivet

Copy link
Copy Markdown
Contributor

#282 enabled VICTORY_LINK_MESH_OUTPUTS under Wine and refuses mesh pipelines whose aligned Metal mesh-output footprint still exceeds 32 KiB (32,768 B). Two gaps still cause 18–25 world mesh pipelines (34,816..38,400 aligned bytes) to be refused on D3DMetal:

  1. Linking triggered only on packed bytes: Shader::translated and HostPipeline::new checked only packed MeshOutputs::bytes() > 32,768 when retaining DXIL containers and running translate_linked. Shaders whose packed footprint is <= 32,768 bytes while their aligned Metal layout is 34,816..38,400 bytes skipped translate_linked altogether and hit fix(gpu): refuse mesh pipelines over Metal's 32 KiB under Wine, by the aligned layout too (#172) #282's refusal path unpruned.
  2. Alignment padding holes between scalar/pair and float4 outputs: Apple's Metal DXIL converter lays out clip/cull distances (4 B each), SV_Position (16 B), and each remaining vertex (OSG1) or primitive (PSG1) output sequentially aligned to its own byte size (end = end.next_multiple_of(4 * cols) + 4 * cols). Whenever a 1-component (4 B) or 2-component (8 B) output precedes a 4-component (16 B) output, each padding hole wastes up to 12 bytes per vertex or primitive (1,536 bytes across 128 vertices), keeping even pruned signatures above 32,768 aligned bytes.

Changes

  • Aligned mesh-output accounting (victory_shader::emit::mesh_aligned_bytes): Move Metal's per-vertex (OSG1) and per-primitive (PSG1) aligned byte calculation into victory-shader and check packed.max(aligned) > 32,768 in standalone count tightening (Lower::new), translate_linked, Shader::translated container retention (with spill reload fallback), and HostPipeline::new linking trigger.
  • Descending-size signature repacking (prune_signature & container::repack_rows): Order kept arbitrary (kind == 0) elements in MS (OSG1, PSG1) and PS (ISG1) by descending power-of-two size (4, 2, 1, 3) ahead of lowering, then repack (row, col) registers after same_mode_per_row and instruction lowering finish—preserving element IDs for loadInput / storeVertexOutput / storePrimitiveOutput while packing compatible sub-vector attributes into 4-component rows without alignment holes.

Synthetic Hardware Test (CrossOver Preview + D3DMetal & WARP on M5 Max)

  • linked_mesh_repacks_interleaved_scalar_and_vector_outputs_and_prunes_primitives: a 128-lane mesh shader (40,960 B standalone) with interleaved 1-col and 4-col reads across both OSG1 and PSG1 (27,648 B packed, 36,864 B aligned before repacking) prunes unread vertex and primitive outputs and repacks both stages down to 27,648 B aligned (0 alignment waste), passes DXIL Validator 1.6, builds a mesh+pixel PSO on D3DMetal and WARP, and renders the exact expected RGBA texels.

Verification

  • CrossOver Preview with D3DMetal on an M5 Max, using an isolated test bottle.
  • Native macOS workspace suite and Windows victory-shader + victory-gpu suites passed.
  • cargo xtask policy, git diff --check, and cargo clippy (-- -D warnings, with and without --features diagnostics) passed. All test inputs are synthetic.

Live M5 Max Verification (Combined Build on CrossOver Preview + D3DMetal)

  • In a combined warm-cache 720p opening-sequence run on Apple M5 Max (CrossOver Preview + D3DMetal, wave32=true), the runtime reached and sustained the 3D world through +8, +24, +48, +90 at 1.5–1.8 FPS (549.7–672.1 ms/frame on 1,021–1,258 draws/frame, 1.275 FPS overall across +8..+90) with 0 2.48 s allocation stalls, 7.2–9.0 ms/frame GUARDS lock hold time (down from 4,887 ms/frame before unlocking GUARDS), 149–154 MB/frame buffer copy volume, 0 DirectStorage decompression or access-violation failures, and 38/38 over-32 KiB aligned mesh/pixel pairs linked with 0 host mesh refusals.

@evan-kolberg

Copy link
Copy Markdown
Contributor

On an Apple M4 Pro with D3DMetal 4.0 beta 3, I applied #401’s shader repacking to #397 and retained #397’s standalone declaration rule. A generated mesh with all 56 channels live went from 51,200 to 30,720 aligned bytes with both declared maxima still at 128. The repacked draw returned all 256 expected RGBA32F pixels. Both builds also returned all 256 expected pixels in the same 4,096-byte sparse control. The 51,200-byte arm stopped at the CPU size check. gdemontalivet’s repacking commit · evan-kolberg’s #397 base

Prepared and tested with Codex.

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.

2 participants