Android Opengl:gl_FragColor的备用值

时间:2016-02-27 17:06:27

标签: android opengl-es glsl glsles

我正在编写一个Android应用程序,它利用opengl对摄像头输出进行一些更改。我以这样的方式编写代码,以便最终弄清楚导致性能问题的原因。

#extension GL_OES_EGL_image_external : require
precision mediump float;
uniform samplerExternalOES sTexture;
uniform vec4 vColor;
const int MAX_COLORS = 6;
uniform float vHues[MAX_COLORS];
uniform float vOffsets[MAX_COLORS];
varying vec2 v_CamTexCoordinate;

float rgb2hue(vec4 c) {
    vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
    vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));
    vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));

    float d = q.x - min(q.w, q.y);
    float e = 1.0e-10;
    return abs(q.z + (q.w - q.y) / (6.0 * d + e));
}

bool isInRange(float a1,float a2, float vOffset) {
    if(a2 < 0.0) {
        return false;
    } else if(abs(a1-a2) < vOffset) {
        return true;
    } else if( a1 > a2) {
        return (1.0 - a1 + a2) < vOffset;
    } else {
        return (1.0 + a1 - a2) < vOffset;
    }
}

vec4 getGrey(vec4 c) {
    float grey = (c.r + c.g + c.b) / 3.0;
    return vec4(grey, grey, grey, c.a);
}

void main() {
    vec4 c = texture2D(sTexture, v_CamTexCoordinate);
    bool hasColor = vHues[0] >= 0.0;
    float hue = rgb2hue(c);
    vec4 test = getGrey(c);
    for(int i=0; i < MAX_COLORS; i++) {
       if(isInRange(hue, vHues[i], vOffsets[i])) {
          //If I uncomment this line the performance gets terrible
          //test = c;
       }
    }
    gl_FragColor = test;
}

当我取消注释上面的行时,性能受到很大影响(跳过很多帧)。我基本上想要根据某些条件使用原始颜色和不同颜色。它有效,但有更有效的方法吗?另外,为什么表现这么差?

2 个答案:

答案 0 :(得分:1)

当您注释掉该行时,您几乎肯定会允许编译器删除整个循环,并且您将代码段缩减为:

gl_FragColor = getNewColor(color);

我认为我们需要先了解MAX_COLORS,vColors和isValid函数的值,然后才能了解为什么性能如此差。

通常,OpenGLES上的片段着色器中的条件是坏消息。如果可能,那么用某种查找表替换你的循环(即你可以采样的纹理)将解决你的性能问题。

这样做是否可行取决于您尝试解决的问题以及没有足够的背景。

编辑:好的,现在发布了更多信息,我可以看到您的色调比较函数(isInRange)是1维的,并且非常适合更改为查找表。您应该考虑使用纹理查找替换整个循环。这就是:

vec4 test = getGrey(c);
for(int i=0; i < MAX_COLORS; i++) {
   if(isInRange(hue, vHues[i], vOffsets[i])) {
      //If I uncomment this line the performance gets terrible
      //test = c;
   }
}
gl_FragColor = test;

会变成这样:

gl_FragColor = mix(getGrey(c), c, texture1d(sLookupTableTex, hue).r);

构建这样的查找表无疑会很繁琐,并且如果vHues和vOffsets一直在变化,那么查找表也需要一直改变,这也会影响它自己的性能,但是我认为用纹理查找替换循环和所有条件的收益将是巨大的。

答案 1 :(得分:1)

如果一般来说陈述和篡改是一个坏主意,因为您的GPU无法正确优化。看到 Efficiency of branching in shaders 更多细节。 您应该尝试避开分支,即使它意味着更多的计算。

以下代码应该可以解决问题

//summarise your isInRange function
bool isInRange = a2 >= 0.0 && ((abs(a1-a2) < vOffset) || (1.0 - max(a1, a2) + min(a1, a2)) < vOffset); 

//this will be 0.0 if false and 1.0 if true
float isInRangef = float(isInRange);

//multiply by the float condition to set the value
gl_FragColor = isInRangef * c + (1.0 - isInRangef) * getGrey(c);

作为通用公式

  • 将您分支的bool转换为浮动条件
  • 将目标值设置为 (condition)*valueIfTrue + (1.0 - condition)*valueIfFalse