在金属着色器中使用 [[clip_distance]] 属性?

Using the [[clip_distance]] attribute in a Metal shader?

在 Metal 着色器中,我尝试在顶点着色器函数的输出结构中使用 [[clip_distance]] 属性,如下所示:

struct vtx_out
{
    float4 gl_Position [[position]];
    float gl_ClipDistance[1] [[clip_distance]];
};

但是,这会导致以下着色器编译错误:

<program source>:86:32: error: 'clip_distance' attribute cannot be applied to types
    float gl_ClipDistance[1] [[clip_distance]];
                               ^

我正在尝试在 Mac 运行 OS X El Capitan 上为 运行 编译此文件。

为什么会出现此错误,如何使用 [[clip_distance]] 属性?

使用这个:

struct vtx_out
{
  float4 gl_Position [[position]];
  float gl_ClipDistance [[clip_distance]] [1];
};

在 Metal 着色语言中 clip_distance 是声明属性。 C++ 规范 [dcl.array] 指出:

In a declaration T D where D has the form

D1 [ constant-expressionopt] attribute-specifier-seqopt

... The optional attribute-specifier-seq appertains to the array.

这就是为什么将属性放在末尾会使 Clang 将其视为类型属性并且您会得到您所看到的错误。