Hacker News new | ask | show | jobs
by mahkoh 495 days ago

    So, if you ever see somebody proposing this

    float a = mix( b, c, step( y, x ) );
The author seems unaware of

    float a = mix( b, c, y > x );
which encodes the desired behavior and also works for vectors:

    The variants of mix where a is genBType select which vector each returned component comes from. For a component of a that is false, the corresponding component of x is returned. For a component of a that is true, the corresponding component of y is returned.
4 comments

The author doesn't seem to say that mix should be avoided. Just that you shouldn't replace a ternary with step+mix. In your quote, you left out the 2nd half of the sentence: "as an optimization to [ternary]".
The author frames his post to be about education:

    please correct them for me. The misinformation has been around for 20 years
But his education will fail as soon as you're operating on more than scalars. It might in fact do more harm than good since it leads the uneducated to believe that mix is not the right tool to choose between two values.
If you only pass a boolean 0 or 1 for the “a” mix parameter, when is using mix better than a ternary? Can you give an example? I’m not sure mix is ever the right tool to choose between two values. It’s a great tool for blending two values, for linear interpolation when “a” is between 0 and 1. But if “a” is only 0 or 1, I don’t think mix will help you, and it could potentially hurt if the two values you mix are expensive function calls.
a can be a vector of booleans.
Does ternary work for that? Inigo is saying "if ternary is working, leave it as ternary". I think you're talking about a situation where ternary wouldn't work, so Inigo isn't saying anything about that situation.
So what? That’s a language convenience, not a performance feature. Calling mix with a vec4 is no faster than using 4 scalar mix calls, and possibly much slower than using 4 ternaries. If your x & y params are expensive functions, and you use a vector of booleans, then you might be really shooting yourself in the foot.
I'm going to go on a limb and claim the author is aware:

https://www.shadertoy.com/user/iq

He also is one of the main authors of the whole site... legend.
Hah, yeah, this is a brilliant "do you even know who you're talking to?" moment.
IQ is most definitely aware of that. Are you unaware of who IQ is?