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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
OpenGL has no such function. And it doesn’t need one.
The question assumes that this conditional statement is less efficient than calling HLSL’s
clipfunction. It’s very possible that it’s more efficient (though even then, it’s a total micro-optimization).clipchecks if the value is less than 0, and if it is, discards the fragment. But you’re not testing against zero; you’re testing againstcutoff, which probably isn’t 0. So, you must callcliplike 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
clipis if the hardware had specific support forclipand that the driver is too stupid to turnif(gl_FragColor.a < cutoff) discard;intoclip(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.