# How does HLSL handle clip(vector)?

My goal is to convert clip() to GLSL, version 1.5. I'm currently doing a basic:

if (x < 0) discard;


But if x is not a scalar, obviously I can't use the "<" operator.

So I'm wanting to understand exactly how HLSL treats a vector (or a matrix) in the clip function so I can account for those as well. Any resources would be much appreciated, as I couldn't find any detailed info about the function. Thank you!

• You can specify custom clip planes with GLSL using gl_ClipDistance. This link looks like a fine introduction to this feature of OpenGL. Commented Nov 20, 2013 at 11:11
• That is a very good intro to the clip planes with glsl, thank you. Commented Nov 20, 2013 at 16:01

But if x is not a scalar, obviously I can't use the "<" operator.

You can simply do this:

if (x < 0 || y < 0 ) discard;


According to clip(x) documentation (emphasis mine):

Use the clip HLSL intrinsic function to simulate clipping planes if each component of the x parameter represents the distance from a plane.

According to the docs x could be a scalar a vector or a matrix.

Which means, if any of the components where less than zero it discards the pixel, this is true since if each of x,y,z represented a distance to a plane, this means than anyone of them being less than zero means the pixel should be clipped/discarded. Which makes the implementation boils down to the original form:

if (x < 0 || y < 0 || z < 0 ) discard;


but I will be surprised if the compiler was not able to detect this pattern and optimize by replacing it with it's appropriate assembly instruction.

• Thank you for a great explanation. I had assumed that was the case, but I wanted to know for sure. Commented Nov 20, 2013 at 15:56