Vector version of ternary operator

Is there any obstacle, that prevents to add to GLSL ternary operator, that can handle gvec operands?

vec x, y;
bvec b = lessThan(x, y);
vec min = b ? x : y;

(dimensionality omitted above)
It is pain to duplicate code every time.

Then don’t duplicate it. Use mix instead:

vec min = mix(y, x, lessThan(x, y));

This seems like a bit of a hack, but mix has specific overloads for taking boolean vectors and doing boolean conditions on the two vector arguments.

The language probably isn’t going to be changed to add vector-wise relational operators, so just use standard library functions.

1 Like

mix() with a boolean vector is the intended way to do what you want. It supports:

genFType mix (genFType x , genFType y , genBType a )
genDType mix (genDType x , genDType y , genBType a )
genIType mix (genIType x , genIType y , genBType a )
genUType mix (genUType x , genUType y , genBType a )
genBType mix (genBType x , genBType y , genBType a )

(?: on a vector condition is a different thing than on a scalar condition, making it itself an overloaded operator and a bit of a hack. But, with mix doing the job, it does seem unlikely to add ?: as syntactic sugar for it, complicating existing descriptions about scalar ?: that would not apply to vector ?:.)

1 Like

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.