Swift 5 中的金属顶点着色器警告

Metal Vertex Shader Warning in Swift 5

我从 Apple 的示例代码中得到了这个直通顶点着色器:

vertex VertexIO vertexPassThrough(device packed_float4 *pPosition  [[ buffer(0) ]],
                                  device packed_float2 *pTexCoords [[ buffer(1) ]],
                                  uint                  vid        [[ vertex_id ]])
{
    VertexIO outVertex;

    outVertex.position = pPosition[vid];
    outVertex.textureCoord = pTexCoords[vid];

    return outVertex;
}

这在 Swift 4/Xcode 10/iOS 12 中有效。现在我用 Swift 5/Xcode 11/iOS 13,我收到这个警告:

writable resources in non-void vertex function

您需要确保着色器只能从这些缓冲区中读取,因此您需要将声明更改为 const device:

vertex VertexIO vertexPassThrough(const device packed_float4 *pPosition  [[ buffer(0) ]],
                                  const device packed_float2 *pTexCoords [[ buffer(1) ]],
                                  uint                  vid        [[ vertex_id ]])
{
...
}