Skip to content

Fix Metal shader compilation for Polyworld - #41

Draft
treeform wants to merge 2 commits into
masterfrom
polyworld-metal-shaders
Draft

treeform wants to merge 2 commits into
masterfrom
polyworld-metal-shaders

Conversation

@treeform

@treeform treeform commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Polyworld terrain, water, particles, and post-processing shaders exposed invalid Metal output for entry uniforms, array textures, promoted float values, matrix narrowing, and builtin stage values. This fixes those translations so the same Shady procedures compile for Metal while retaining existing GLSL and WebGL generation.

Uniform entry parameters now become native buffers or textures. Fragment position and depth use Metal attributes, integer varyings are flat, and vertex early returns populate the stage output before returning. Array sampling now emits native Metal sample/read calls directly, and floor-based modulo expands inline to its arithmetic expression. The shared shader header contains only the Metal include and namespace declaration; it no longer injects the five modulo/array helper functions. When arguments require evaluation, function-local temporaries capture them at the original expression site, preserving single evaluation, argument snapshots, short circuits, ternaries, and loop conditions. Matrix narrowing still emits its helper only when used. Texture buffers return their full texel, and inverse square root uses the native Metal operation. Metal lowers float64 values to float32 because MSL does not support doubles. Fragment coordinates remain native Metal coordinates so renderers can manage texture orientation explicitly.

Validation:

  • nim check and Shady golden, backend, CPU, resource, parameter, and builtin regression tests pass.
  • Actual Metal device compilation passes all 69 Polyworld engine/game stages and 117 experimental/test stages. Older experiments with stale UI APIs were validated through shader-only extracts.
  • Nine glTF stages compile, using the existing shared-sampler specialization for IBL.
  • The new Metal regression links a render pipeline with position, facing, integer varyings, and fragment depth outputs.
  • GLSL3 and GLSL4 framebuffer tests pass with a temporary OpenGL 4.1 context on macOS; the existing helper requests unsupported OpenGL 4.5.
  • Ten focused native Metal GPU readbacks verify negative scalar/vector/infix modulo, argument evaluation, lazy control flow, all four array operations, and texture-size LOD evaluation; the shadow-coordinate case also compiles natively.
  • Twenty-two focused desktop GLSL/WebGL shaders pass glslang validation, and the existing GLSL golden output is unchanged. Silky Metal compositing/texture pixel tests and desktop/WebGL shader linking pass. GotA checks and builds with the updated compiler.
  • CI runs the portable Metal source regressions, including the inline-expression tests; their GPU checks run on macOS.

This branch has not been deployed

No deployments
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.

1 participant