1
\$\begingroup\$

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!

\$\endgroup\$
2
  • \$\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 Nov 20 '13 at 11:11
  • \$\begingroup\$ That is a very good intro to the clip planes with glsl, thank you. \$\endgroup\$ – Ramon Johannessen Nov 20 '13 at 16:01
2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Thank you for a great explanation. I had assumed that was the case, but I wanted to know for sure. \$\endgroup\$ – Ramon Johannessen Nov 20 '13 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.