Metal shader for mutliple clipping plane

I am using [[clip_distance]] in the VertexOut as below in a metal shader:

struct VertexOut {
    float4 position [[position]];
    float4 color;
    float clip_distance [[clip_distance]];
};

It is working fine for single plane. But my requirement is multiple planes, I want to clip from 2 sides.

In this reference post https://developer.apple.com/forums/thread/69701, that we can have multiple clip planes as array as below:

float clip_distance [[clip_distance]] [2];

But when I use it, xcode gives error regards to structure of Vertex Out in the fragment function as below

Type 'VertexOut' is not valid for attribute 'stage_in'

Is there any structure change for this? Can anyone please help with a solution for using multiple clip planes?

Just don't put it in the struct. Output directly from the VS. I think that will fix it.

I'm a bit late to the party, but the [[clip_distance]] is a vertex-only attribute and the fragment functions would produce undefined behavior if they read and use it, thus the compiler error. What you can do is define one structure for the output for the vertex shader and another for the input to the fragment shader. For example:

// Structure used as the output of the vertex shader.
struct VertexOut {
    float4 position [[position]];
    float clipDistance [[clip_distance]] [2];
};

// Structure defined as the input of the fragment shader.
struct FragmentIn {
    float4 position;
};

You can find more about the matching vertex and fragment attributes in the Metal Shading Language specification, in section 5.7.1 "Vertex-Fragment Signature Matching".

Metal shader for mutliple clipping plane
 
 
Q