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!??
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.
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