Search code examples
syntaxmetal

What do double square brackets around "position" mean in the Metal language?


What does float4 position [[position]]; do in the following snippet?

#include <metal_stdlib>
using namespace metal;

struct Vertex
{
    float4 position [[position]];
    float4 color;
};

vertex Vertex vertex_main(device Vertex *vertices [[buffer(0)]], uint vid [[vertex_id]])
{
    return vertices[vid];
}

I am confused by the [[position]] part and similar usage in the function definition especially.


Solution

  • The Metal Shading Language is documented at https://developer.apple.com/metal/metal-shading-language-specification.pdf

    In particular look at "Table 9" on page 68 of that document. There [[position]] is identified as an attribute qualifier for the return type of a Vertex Function. I assume that means that when your vertex shader returns the caller will use the values in that part of the struct to determine the positions of the vertices the shader would like to modify.