Skip to content

msl: pipeline structs lay out vec3<f32> as float3, so a scalar after a vec3 in a uniform or storage buffer is read 4 bytes late on Metal #2340

Backlog

Description

@houko

Context

msl.CompilePipeline emits every WGSL struct as a Metal struct with the members' natural Metal types (internal/shaderir/msl/pipeline.go:95-100, via declVar at pipeline.go:176 and typeStr at pipeline.go:148). A vec3<f32> member becomes float3 through basicTypeString (internal/shaderir/msl/type.go:103-104). The same structs are then bound as constant S& [[buffer(n)]] for uniforms and device S* / device S& for storage buffers (pipeline.go:194-208), so Metal reads the app's bytes with Metal's struct layout.

The two layouts disagree around vec3:

  • WGSL: vec3<f32> has align 16 and size 12, so a following 4-byte member (f32, i32, u32) sits at offset 12, inside the vec3's 16-byte slot.
  • MSL: float3 has align 16 and size 16, so the following member starts at offset 16.

For struct P { d: vec3<f32>, s: f32 }, WGSL puts s at 12 and the struct is 16 bytes; the MSL struct NSGo emits (float3 M0; float M1;) puts s at 16 and is 32 bytes. Every member after the first such scalar is shifted, and in a storage buffer array<P> the element stride doubles, so every element after the first is read from the wrong place.

The other writers do not have this problem for this shape: GLSL declares uniform blocks std140 and storage blocks std430 (internal/shaderir/glsl/pipeline.go:183-194), where a float after a vec3 also lands at 12. The fragment-only WGSL path is not affected either, because it packs its flat Uniforms by lanes with msl.UniformLanes, which already counts Vec3 as 16 bytes (internal/shaderir/msl/uniform.go:83). Nothing in docs/architecture/gpu.md or docs/architecture/shaders.md says pipeline buffers use a layout other than WGSL's.

Why it matters

The app writes buffers in WGSL layout, which is what WebGPU and the GL backends read, and the shader compiles cleanly on Metal. On macOS and iOS the draw then silently reads shifted values: a light's intensity becomes the next member's value, a material parameter reads zero, an instance array reads garbage after its first element. Nothing fails, so tests that run on the software device or on non-Apple CI do not catch it.

struct { position: vec3f, radius: f32 }, { color: vec3f, intensity: f32 } and { normal: vec3f, distance: f32 } are the most common way to fill a 16-byte slot in WGSL, so most real lighting, material and instancing structs hit this. A game layer built on NSGo (nsgo-dev/game) currently has to refuse a scalar directly after a vec3 in its custom material uniform layouts and pad by hand, which no WGSL author would expect to need.

Repro

  1. Create a pipeline whose WGSL declares:

    struct P { d: vec3<f32>, s: f32 }
    @group(0) @binding(0) var<uniform> u: P;

    and whose fragment entry returns vec4f(u.s, 0.0, 0.0, 1.0).

  2. Write 16 bytes to the uniform buffer with Device.WriteBuffer: d = (0, 0, 0) at offset 0 and s = 1.0 at offset 12, as WGSL lays the struct out.

  3. Draw and read back the target with Device.ReadTexture.

A backend that follows WGSL's layout (WebGPU, or GL through std140) returns red. On macOS or iOS the fragment reads s from offset 16, past the data written, and returns black (or whatever follows). The emitted MSL from msl.CompilePipeline shows struct S0 { float3 M0; float M1; };.

Proposal

Make the MSL pipeline writer follow WGSL's host-shareable layout for structs used in uniform and storage buffers: emit vec3<f32> / vec3<i32> / vec3<u32> members as packed_float3 / packed_int3 / packed_uint3 (wrapping reads in float3(...) etc. where an expression needs the unpacked type), and insert explicit padding members wherever WGSL's offset is larger than Metal's would be. This is what Tint and Naga do for MSL. Structs used only as function-local values can keep float3.

While there, a layout test that compiles the same WGSL structs for every backend and compares member offsets against WGSL's rules would catch the same class elsewhere, for example HLSL cbuffer packing, where a float3 after a single float may start at offset 4 rather than WGSL's 16.

Acceptance

  • A uniform struct { d: vec3<f32>, s: f32 } reads s from offset 12 on Metal, and the struct is 16 bytes.
  • A storage buffer array<{ d: vec3<f32>, s: f32 }> has a 16-byte stride on Metal, matching WGSL.
  • Nested structs, arrays of vec3 and a vec3 as the last member keep WGSL's offsets and sizes.
  • A test in internal/shaderir/msl (and, if added, a cross-backend layout test) pins the emitted member offsets, and a Metal GPU test draws with a vec3-then-scalar uniform and gets the same pixels as the other backends.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions