简体   繁体   中英

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.

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.

我没有足够的声誉来回应您关于括号名称的评论,但 [[]] 括号是取自 C++11 的属性语法。

Metal is based on C++ and the this is just the syntax of attributes in C++11. See this for more details about the grammar.

So what you have is a vertex shader, the output of the vertex shader (in this case struct Vertex) goes into the rasterizer, and the output of the rasterizer goes to the fragment shader.

vertex shader -> rasterizer -> fragment shader

The rasterizer interpolates whatever you feed into it, which just means it averages out the values. The point here is you don't control this process (the rasterizer I mean), it just does its thing... and it feeds the output to the fragment shader, then you can do whatever extra you want with it.

The [[position]] here just marks the position value so the rasteriser don't touch it. It will touch and interpolate everything else.

If you don't name your position "position" but name it "blah" and mark it with [[position]], it'll interpret "blah" as "position" and leave it alone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM