4 ms·
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 enc
by mahkoh 2y 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.
- Thorrez 2y agoThe 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]".
- mahkoh 2y agoThe 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.
- dahart 2y agoIf 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.
- mahkoh 2y agoa can be a vector of booleans.
- Thorrez 2y agoDoes 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.
- dahart 2y agoSo 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.
- dahart 2y agoIQ is most definitely aware of that. Are you unaware of who IQ is?
- sva_ 2y agoI'm going to go on a limb and claim the author is aware: https://www.shadertoy.com/user/iq https://www.shadertoy.com/user/iq
- petermcneeley 2y agoWGSL has select https://www.w3.org/TR/WGSL/#select-builtin https://www.w3.org/TR/WGSL/#select-builtin