根据GLSL中向量的特定分量进行最小-最大的最快方法? [英] Fastest way to do min-max based on specific component of vectors in GLSL?
问题描述
我需要在GLSL代码中多次调用这种函数.
I need to call this kind of function many many times in my GLSL code.
vec2 minx(vec2 a, vec2 b) {
if (a.x < b.x) return a;
else return b;
}
我担心分支过多.有没有办法避免if-else构造?
I'm concerned of the excessive branching. Is there a way to do this avoiding if-else constructs?
推荐答案
我建议使用GLSL函数 step
.
I suggest to use the GLSL functions mix
and step
.
mix
插入2个值之间根据浮点内插值a
在[0.0,1.0]范围内.如果a
等于0.0,则返回第一个值;如果a
等于1.0,则返回第二个值.
mix
interpolates between 2 values according to a floating point interpolation value a
in the range [0.0, 1.0]. If the a
is equal 0.0 then the 1st value is returned and if the a
is equal 1.0 then the 2nd value is returned.
step
测试值是否小于边缘值.如果小于此值,则返回0.0,否则返回1.0.
step
tests whether a value is less than an edge value. If it is less then 0.0 is returned, else 1.0 is returned.
如果将这两个功能结合在一起,则代码将如下所示:
If you combine the 2 functions your code will look like this:
vec2 minx(vec2 a, vec2 b)
{
return mix( a, b, step( b.x, a.x ) );
}
请注意, step
恰好是0.0或恰好是1.0,这会导致
Note, the result of step
is either exactly 0.0 or exactly 1.0, this causes that mix
either returns the 1st value or returns the 2nd value.
这篇关于根据GLSL中向量的特定分量进行最小-最大的最快方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!