GLSL equivalent to HLSL () clip?

The HLSL clip() function is described here .

I intend to use this to crop alpha in OpenGL. Would there be an equivalent in GLSL just

 if (gl_FragColor.a < cutoff) { discard; } 

Or is there a more efficient equivalent?

+4
source share
1 answer

OpenGL does not have this feature. And he is not needed.

Or is there a more efficient equivalent?

The question suggests that this conditional statement is less efficient than calling the HLSL clip function. It is very possible that it is more efficient (although even then, this is full micro-optimization). clip checks if the value is less than 0, and if it is, discards the fragment. But you are not testing against zero; you are testing against cutoff , which is probably not 0. So you should name clip as follows (using the GLSL style): clip(gl_FragColor.a - cutoff)

If the clip is not directly supported by the hardware, your call is equivalent to if(gl_FragColor.a - cutoff < 0) discard; . This is a mathematical operation and a conditional test. This is slower than just a conditional test. And if it isn’t ... the driver will almost certainly rebuild your code to perform a conditional test this way.

The only way a condition would be slower than clip is that the hardware has some support for clip and that the driver is too dumb to turn if(gl_FragColor.a < cutoff) discard; in clip(gl_FragColor.a - cutoff) . If the driver is so stupid, if it lacks this basic optimization, then you have more performance problems than this.

In short: don't worry about it.

+8
source

Source: https://habr.com/ru/post/1438192/


All Articles