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

759 views Asked by At

In a Metal shader, I am trying to make use of the [[clip_distance]] attribute in the output structure of a vertex shader function as follows:

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

However, this results in the following shader compilation error:

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

I am trying to compile this for running on a Mac running OS X El Capitan.

Why am I getting this error, and how can I make use of the [[clip_distance]] attribute?

1

There are 1 answers

0
Danger Ranger On BEST ANSWER

Use this:

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

In the Metal shading language clip_distance is a declaration attribute. The C++ spec [dcl.array] states:

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.

This is why placing the attribute at the end makes Clang treat it like a type attribute and you get the error that you see.