Would doing something like this be faster on mobile than discard is? I have not profiled it but I know discard has issues and am wondering why something as simple as this isn’t used instead other than maybe overdraw being more of a concern than discard is on other platforms.
I am not an expert of shader, but I think that “if” should be avoided as in any parallel processing (and discard is somehow similar to an “if”…). I would use a strongly steep interpolation between alpha = 1 and alpha = 0, so strong that it is equivalent to an “if”.
(Moreover I don’t understand the line
tint_pm.xyz = tint.xyz * 0;
Why don’t you define a vec3 0 constant outside main and set tint_pm.xyz equal to this constant in the “if” body? But maybe there is something I am missing…)
I wonder how if and discard compares for tile based rendering? What I get the impression of is that discard is more expensive and should be almost always avoided. Meanwhile if blocks can be optimized at compile time to simple instructions if they are simple enough.
That does seem reasonable.
I need to get better with ways to avoid if/else blocks with math. It seems like even with the higher number of instructions it’s generally better than using if/else.
In your example I think you don’t need too much math. Let me try to explain:
float m = step(var_texcoord0.y, clip.x);
so m is 0 if var_texcoord0.y > clip.x and 1 otherwise. Hence we just need to do
tint_pm.xyz = tint.xyz * m;
And this should be it. In this case, as you guessed the “if” block has just been optimized in one single instruction; however I am not sure the compiler is smart enough to do it itself… maybe.
The difference between “if” and “discard” is that “if” breaks the parallelism of execution of the shader while “discard” break the parallelism of the pipeline of the whole fragment computation since it stop at all the pipeline for the fragment that is discarded. (I hope this is somehow close to reality… )