Metal Vertex Shader Warning in Swift 5
Asked Answered
K

1

13

I got this passthrough vertex shader I used from Apple's sample code:

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;
}

This worked in Swift 4/Xcode 10/iOS 12. Now I with Swift 5/Xcode 11/iOS 13, I get this warning:

writable resources in non-void vertex function
Karee answered 28/8, 2019 at 12:35 Comment(2)
Try const device ... for those buffers.Antirrhinum
perfect! Warning went away! Please post this as an answer so I can mark.Karee
A
21

You need to ensure the shader can only read from those buffers, so you need to change the declaration to const device:

vertex VertexIO vertexPassThrough(const device packed_float4 *pPosition  [[ buffer(0) ]],
                                  const device packed_float2 *pTexCoords [[ buffer(1) ]],
                                  uint                  vid        [[ vertex_id ]])
{
...
}
Antirrhinum answered 28/8, 2019 at 12:52 Comment(2)
Can you please explain why we need to insure read-only access to those buffers? What's the reason behind it? I'm new to this and trying to understand the basic concepts. I also think it would make your answer better. Thanks!Keek
@Keek Graphics APIs, in general, are very interested whether the CPU and/or GPU can read/write buffers so it knows where to allocate those buffers. The use of const reinforces the notion that the GPU only wants read access to these buffers.Antirrhinum

© 2022 - 2024 — McMap. All rights reserved.