Alternatives to mix( vec4, vec4, bvec4 ) in openGL ES

Hi Everyone

I’m currently in the process of porting some GLSL code to OpenGL ES GLSL
One thing I’ve found very curious and frustrating is that OpenGL ES GSLS does not supply any way to do vectorized conditional selection.

CG can do this
float4 v1, v2, v3, v4;
float4 v5 = ( v1 < v2 ) ? v3 : v4;

GLSL can do this
vec4 v1, v2, v3, v4;
vec4 v5 = mix( v4, v3, lessThan( v1, v2 ) );

GLSL ES does NOT provide a bvec mix function.
ie, vec4 mix( vec4, vec4, bvec4 )

This is very confusing. Why would they go to the effort of having a vectorized bool type, with no operations in which to use it!?? :frowning:
Have I simply missed something or is this indeed the case?

Ok. So if this is the case then I need to create my own vectorized conditional selection.
There are a few ways we can do this.

eg,
vec4 mix( vec4 arg0, vec4 arg1, bvec4 bools )
{
return vec4( bools.x ? arg1.x : arg0.x, bools.y ? arg1.y : arg0.y, bools.z ? arg1.z : arg0.z, bools.w ? arg1.w : arg0.w );
}
vec4 mix( vec4 arg0, vec4 arg1, bvec4 bools )
{
return arg1 * vec4( bools ) + arg0 * vec4( not( bools ) );
}

What I’m actually looking for is a replacement to this…
vec4 v5 = mix( v4, v3, lessThan( v1, v2 ) );

So something like this might be better…?
vec4 lessThanChoice( vec4 arg0, vec4 arg1, vec4 cond0, vec4 cond1 )
{
// NOTE: invokes a floating point blend operation for something that is essentially a conditional selection. :frowning:
return mix( arg0, arg1, step( cond1, cond0 ) );
}

Any ideas which way I should approach this?
Which of these methods should I use? Or is there another way which is better?
Thanks all!
Ren

I have had the same pain as you… Open GL ES2 just plain flatly sucks at times… and the embedded hardware is all over the place in terms of conformance… the best thing to do dramtically depends on the hardware… my usual leaning is to to realize the selection bvec4 with a vec4, each component as 0 or 1 and doing mix, as you have… ARM claims there Mali is happy with if’s… I know SGX’s branching support in fragment shader is vey touchy… can fail on a dime… I have had isues with boolean variales on SGX just not working correctly (i.e. somebool=osomevalue, later use thst somebool) and naturally weather or not it is worht the bother of vectorizing dramtically depends on the hardware, i.e. do ?: on each component seperately might be just fine…

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