Search code examples
objective-cxcodemacosmetalmetalkit

Illegal vector component name "x" (metal)


with Metal, at runtime, when I try to compile my shader I receive the error

Error: Illegal vector component name 'x'

It's work well when In macos 10.14/10.15 but on macos 10.11 on old mac mini it's crash! any idea why ?

using namespace metal;

struct Vertex {
  packed_float3 position;
};

struct ProjectedVertex {
  float4 position [[position]];
};

vertex ProjectedVertex vertexShader(constant Vertex *vertexArray [[buffer(0)]],
                                    const unsigned int vertexId [[vertex_id]],
                                    constant float4x4 &MVPMatrix [[buffer(1)]]) {
  Vertex in = vertexArray[vertexId];
  ProjectedVertex out;
  out.position = float4(in.position.x, in.position.y, in.position.z, 1) * MVPMatrix;
  return out;
}

Solution

  • Prior to Metal 2.1, accessing the named components of a packed vector type is not permitted in Metal Shading Language.

    To work around this, either access such elements via subscripting, or by creating a local variable of the corresponding non-packed type:

    out.position = float4(in.position[0], in.position[1], in.position[2], 1) * MVPMatrix;
    
    // OR:
    
    float3 position = in.position;
    out.position = float4(position.x, position.y, position.z, 1) * MVPMatrix;