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

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?

Accepted Reply

It turns out the

[[clip_distance]]
attribute qualifier (or any attribute qualifier) must be applied to the array, not the array element. So...the correct declaration is...


float gl_ClipDistance [[clip_distance]] [1];


See this SO answer.

  • float gl_ClipDistance [[clip_distance]] [1]; (in VertexOut struct) This is not working anymore and xcode is giving error "Type 'VertexOut' is not valid for attribute 'stage_in'" . Does anyone have solution for this? Need to use mutliple clip planes

Add a Comment

Replies

This sounds like it could be a compiler bug. Could you file a Radar with a sample project that reproduces the issue and let us know the number here? Thanks.

It turns out the

[[clip_distance]]
attribute qualifier (or any attribute qualifier) must be applied to the array, not the array element. So...the correct declaration is...


float gl_ClipDistance [[clip_distance]] [1];


See this SO answer.

  • float gl_ClipDistance [[clip_distance]] [1]; (in VertexOut struct) This is not working anymore and xcode is giving error "Type 'VertexOut' is not valid for attribute 'stage_in'" . Does anyone have solution for this? Need to use mutliple clip planes

Add a Comment