Chaotic alignment bug in Metal shaders

All I wanted to do was put some texture on a cube. 😒

This doesn't look right

I ported the Metal tutorial at to OSX and came across this bug in the shader compiler backend, leading to distored textures.

When using a VertexIn struct with packed floats such as

struct VertexIn {
    packed_float3 position;
    packed_float4 color;
    packed_float2 texCoord;

the shader compiler backend will generate invalid offsets for the struct member texCoord. This seems to be related specifically to the combination of packed_float3 and packed_float2 since:

  • using packed_float4 fixes the issue
  • color is unaffected
  • reordering the members produces different invalid output
  • manually calculating the correct texCoord member offset fixes the issue

The issue is specific to Nvidia 750M cards as found in mid 2014 rMBPs. Running the same code on the built-in Intel Iris Pro produces valid output. From reports, the 650M is not affected either, but I was unable to verify that myself.

Big thanks to @warrenm for helping to diagnose and providing the workaround code.

I submitted the bug to Apple at rdar://24249981. You can find the code I used at Github.