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!

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

1 Answer 1


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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .