The HLSL clip()
function is described here.
I intend to use this for alpha cutoff, in OpenGL. Would the equivalent in GLSL simply be
if (gl_FragColor.a < cutoff)
{
discard;
}
Or is there some more efficient equivalent?
OpenGL has no such function. And it doesn't need one.
Or is there some more efficient equivalent?
The question assumes that this conditional statement is less efficient than calling HLSL's clip
function. It's very possible that it's more efficient (though even then, it's a total micro-optimization). clip
checks if the value is less than 0, and if it is, discards the fragment. But you're not testing against zero; you're testing against cutoff
, which probably isn't 0. So, you must call clip
like this (using GLSL-style): clip(gl_FragColor.a - cutoff)
If clip is not directly support by the hardware, then your call is equivalent to if(gl_FragColor.a - cutoff < 0) discard;
. That's a math operation and a conditional test. That's slower than just a conditional test. And if it's not... the driver will almost certainly rearrange your code to do the conditional test that way.
The only way the conditional would be slower than clip
is if the hardware had specific support for clip
and that the driver is too stupid to turn if(gl_FragColor.a < cutoff) discard;
into clip(gl_FragColor.a - cutoff)
. If the driver is that stupid, if it lacks that basic pinhole optimization, then you've got bigger performance problems than this to deal with.
In short: don't worry about it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With