简体   繁体   English

在 Metal 语言中,“位置”周围的双方括号是什么意思?

[英]What do double square brackets around “position” mean in the Metal language?

What does float4 position [[position]]; float4 position [[position]];是什么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.我对[[position]]部分和函数定义中的类似用法感到困惑。

The Metal Shading Language is documented at https://developer.apple.com/metal/metal-shading-language-specification.pdf金属着色语言记录在https://developer.apple.com/metal/metal-shading-language-specification.pdf

In particular look at "Table 9" on page 68 of that document.特别是查看该文档第 68 页上的“表 9”。 There [[position]] is identified as an attribute qualifier for the return type of a Vertex Function. [[position]] 被标识为顶点函数返回类型的属性限定符。 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. Metal 基于 C++,这只是 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. 因此,您拥有的是一个顶点着色器,该顶点着色器的输出(在本例中为struct Vertex)进入光栅化器,而光栅化器的输出进入片段着色器。

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. 这里的[[position]]只是标记位置值,因此光栅化器不要触摸它。 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 如果您不将自己的职位命名为“ position”,而是将其命名为“ blah”并用[[position]]标记,则它将“ blah”解释为“ position”,而不必管它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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