繁体   English   中英

优化重片段着色器的性能

[英]Optimizing performance of a heavy fragment shader

我需要帮助优化以下着色器集:

顶点:

    precision mediump float;

uniform vec2 rubyTextureSize;

attribute vec4 vPosition;
attribute vec2 a_TexCoordinate;

varying vec2 tc;

void main() {
    gl_Position = vPosition;

    tc = a_TexCoordinate;
}

分段:

precision mediump float;

/*
 Uniforms
 - rubyTexture: texture sampler
 - rubyTextureSize: size of the texture before rendering
 */

uniform sampler2D rubyTexture;
uniform vec2 rubyTextureSize;
uniform vec2 rubyTextureFract;

/*
 Varying attributes
 - tc: coordinate of the texel being processed
 - xyp_[]_[]_[]: a packed coordinate for 3 areas within the texture
 */

varying vec2 tc;

/*
 Constants
 */
/*
 Inequation coefficients for interpolation
 Equations are in the form: Ay + Bx = C
 45, 30, and 60 denote the angle from x each line the cooeficient variable set builds
 */
const vec4 Ai = vec4(1.0, -1.0, -1.0, 1.0);
const vec4 B45 = vec4(1.0, 1.0, -1.0, -1.0);
const vec4 C45 = vec4(1.5, 0.5, -0.5, 0.5);
const vec4 B30 = vec4(0.5, 2.0, -0.5, -2.0);
const vec4 C30 = vec4(1.0, 1.0, -0.5, 0.0);
const vec4 B60 = vec4(2.0, 0.5, -2.0, -0.5);
const vec4 C60 = vec4(2.0, 0.0, -1.0, 0.5);

const vec4 M45 = vec4(0.4, 0.4, 0.4, 0.4);
const vec4 M30 = vec4(0.2, 0.4, 0.2, 0.4);
const vec4 M60 = M30.yxwz;
const vec4 Mshift = vec4(0.2);

// Coefficient for weighted edge detection
const float coef = 2.0;
// Threshold for if luminance values are "equal"
const vec4 threshold = vec4(0.32);

// Conversion from RGB to Luminance (from GIMP)
const vec3 lum = vec3(0.21, 0.72, 0.07);

// Performs same logic operation as && for vectors
bvec4 _and_(bvec4 A, bvec4 B) {
    return bvec4(A.x && B.x, A.y && B.y, A.z && B.z, A.w && B.w);
}

// Performs same logic operation as || for vectors
bvec4 _or_(bvec4 A, bvec4 B) {
    return bvec4(A.x || B.x, A.y || B.y, A.z || B.z, A.w || B.w);
}

// Converts 4 3-color vectors into 1 4-value luminance vector
vec4 lum_to(vec3 v0, vec3 v1, vec3 v2, vec3 v3) {
    //    return vec4(dot(lum, v0), dot(lum, v1), dot(lum, v2), dot(lum, v3));

    return mat4(v0.x, v1.x, v2.x, v3.x, v0.y, v1.y, v2.y, v3.y, v0.z, v1.z,
            v2.z, v3.z, 0.0, 0.0, 0.0, 0.0) * vec4(lum, 0.0);
}

// Gets the difference between 2 4-value luminance vectors
vec4 lum_df(vec4 A, vec4 B) {
    return abs(A - B);
}

// Determines if 2 4-value luminance vectors are "equal" based on threshold
bvec4 lum_eq(vec4 A, vec4 B) {
    return lessThan(lum_df(A, B), threshold);
}

vec4 lum_wd(vec4 a, vec4 b, vec4 c, vec4 d, vec4 e, vec4 f, vec4 g, vec4 h) {
    return lum_df(a, b) + lum_df(a, c) + lum_df(d, e) + lum_df(d, f)
            + 4.0 * lum_df(g, h);
}

// Gets the difference between 2 3-value rgb colors
float c_df(vec3 c1, vec3 c2) {
    vec3 df = abs(c1 - c2);
    return df.r + df.g + df.b;
}

void main() {

    /*
     Mask for algorhithm
     +-----+-----+-----+-----+-----+
     |     |  1  |  2  |  3  |     |
     +-----+-----+-----+-----+-----+
     |  5  |  6  |  7  |  8  |  9  |
     +-----+-----+-----+-----+-----+
     | 10  | 11  | 12  | 13  | 14  |
     +-----+-----+-----+-----+-----+
     | 15  | 16  | 17  | 18  | 19  |
     +-----+-----+-----+-----+-----+
     |     | 21  | 22  | 23  |     |
     +-----+-----+-----+-----+-----+
     */

    float x = rubyTextureFract.x;
    float y = rubyTextureFract.y;

    vec4 xyp_1_2_3 = tc.xxxy + vec4(-x, 0.0, x, -2.0 * y);
    vec4 xyp_6_7_8 = tc.xxxy + vec4(-x, 0.0, x, -y);
    vec4 xyp_11_12_13 = tc.xxxy + vec4(-x, 0.0, x, 0.0);
    vec4 xyp_16_17_18 = tc.xxxy + vec4(-x, 0.0, x, y);
    vec4 xyp_21_22_23 = tc.xxxy + vec4(-x, 0.0, x, 2.0 * y);
    vec4 xyp_5_10_15 = tc.xyyy + vec4(-2.0 * x, -y, 0.0, y);
    vec4 xyp_9_14_9 = tc.xyyy + vec4(2.0 * x, -y, 0.0, y);

    // Get mask values by performing texture lookup with the uniform sampler
    vec3 P1 = texture2D(rubyTexture, xyp_1_2_3.xw).rgb;
    vec3 P2 = texture2D(rubyTexture, xyp_1_2_3.yw).rgb;
    vec3 P3 = texture2D(rubyTexture, xyp_1_2_3.zw).rgb;

    vec3 P6 = texture2D(rubyTexture, xyp_6_7_8.xw).rgb;
    vec3 P7 = texture2D(rubyTexture, xyp_6_7_8.yw).rgb;
    vec3 P8 = texture2D(rubyTexture, xyp_6_7_8.zw).rgb;

    vec3 P11 = texture2D(rubyTexture, xyp_11_12_13.xw).rgb;
    vec3 P12 = texture2D(rubyTexture, xyp_11_12_13.yw).rgb;
    vec3 P13 = texture2D(rubyTexture, xyp_11_12_13.zw).rgb;

    vec3 P16 = texture2D(rubyTexture, xyp_16_17_18.xw).rgb;
    vec3 P17 = texture2D(rubyTexture, xyp_16_17_18.yw).rgb;
    vec3 P18 = texture2D(rubyTexture, xyp_16_17_18.zw).rgb;

    vec3 P21 = texture2D(rubyTexture, xyp_21_22_23.xw).rgb;
    vec3 P22 = texture2D(rubyTexture, xyp_21_22_23.yw).rgb;
    vec3 P23 = texture2D(rubyTexture, xyp_21_22_23.zw).rgb;

    vec3 P5 = texture2D(rubyTexture, xyp_5_10_15.xy).rgb;
    vec3 P10 = texture2D(rubyTexture, xyp_5_10_15.xz).rgb;
    vec3 P15 = texture2D(rubyTexture, xyp_5_10_15.xw).rgb;

    vec3 P9 = texture2D(rubyTexture, xyp_9_14_9.xy).rgb;
    vec3 P14 = texture2D(rubyTexture, xyp_9_14_9.xz).rgb;
    vec3 P19 = texture2D(rubyTexture, xyp_9_14_9.xw).rgb;

    // Store luminance values of each point in groups of 4
    // so that we may operate on all four corners at once
    vec4 p7 = lum_to(P7, P11, P17, P13);
    vec4 p8 = lum_to(P8, P6, P16, P18);
    vec4 p11 = p7.yzwx; // P11, P17, P13, P7
    vec4 p12 = lum_to(P12, P12, P12, P12);
    vec4 p13 = p7.wxyz; // P13, P7,  P11, P17
    vec4 p14 = lum_to(P14, P2, P10, P22);
    vec4 p16 = p8.zwxy; // P16, P18, P8,  P6
    vec4 p17 = p7.zwxy; // P17, P13, P7,  P11
    vec4 p18 = p8.wxyz; // P18, P8,  P6,  P16
    vec4 p19 = lum_to(P19, P3, P5, P21);
    vec4 p22 = p14.wxyz; // P22, P14, P2,  P10
    vec4 p23 = lum_to(P23, P9, P1, P15);

    // Scale current texel coordinate to [0..1]
    vec2 fp = fract(tc * rubyTextureSize);

    // Determine amount of "smoothing" or mixing that could be done on texel corners
    vec4 AiMulFpy = Ai * fp.y;
    vec4 B45MulFpx = B45 * fp.x;
    vec4 ma45 = smoothstep(C45 - M45, C45 + M45, AiMulFpy + B45MulFpx);
    vec4 ma30 = smoothstep(C30 - M30, C30 + M30, AiMulFpy + B30 * fp.x);
    vec4 ma60 = smoothstep(C60 - M60, C60 + M60, AiMulFpy + B60 * fp.x);
    vec4 marn = smoothstep(C45 - M45 + Mshift, C45 + M45 + Mshift,
            AiMulFpy + B45MulFpx);

    // Perform edge weight calculations
    vec4 e45 = lum_wd(p12, p8, p16, p18, p22, p14, p17, p13);
    vec4 econt = lum_wd(p17, p11, p23, p13, p7, p19, p12, p18);
    vec4 e30 = lum_df(p13, p16);
    vec4 e60 = lum_df(p8, p17);

    // Calculate rule results for interpolation
    bvec4 r45_1 = _and_(notEqual(p12, p13), notEqual(p12, p17));
    bvec4 r45_2 = _and_(not (lum_eq(p13, p7)), not (lum_eq(p13, p8)));
    bvec4 r45_3 = _and_(not (lum_eq(p17, p11)), not (lum_eq(p17, p16)));
    bvec4 r45_4_1 = _and_(not (lum_eq(p13, p14)), not (lum_eq(p13, p19)));
    bvec4 r45_4_2 = _and_(not (lum_eq(p17, p22)), not (lum_eq(p17, p23)));
    bvec4 r45_4 = _and_(lum_eq(p12, p18), _or_(r45_4_1, r45_4_2));
    bvec4 r45_5 = _or_(lum_eq(p12, p16), lum_eq(p12, p8));
    bvec4 r45 = _and_(r45_1, _or_(_or_(_or_(r45_2, r45_3), r45_4), r45_5));
    bvec4 r30 = _and_(notEqual(p12, p16), notEqual(p11, p16));
    bvec4 r60 = _and_(notEqual(p12, p8), notEqual(p7, p8));

    // Combine rules with edge weights
    bvec4 edr45 = _and_(lessThan(e45, econt), r45);
    bvec4 edrrn = lessThanEqual(e45, econt);
    bvec4 edr30 = _and_(lessThanEqual(coef * e30, e60), r30);
    bvec4 edr60 = _and_(lessThanEqual(coef * e60, e30), r60);

    // Finalize interpolation rules and cast to float (0.0 for false, 1.0 for true)
    vec4 final45 = vec4(_and_(_and_(not (edr30), not (edr60)), edr45));
    vec4 final30 = vec4(_and_(_and_(edr45, not (edr60)), edr30));
    vec4 final60 = vec4(_and_(_and_(edr45, not (edr30)), edr60));
    vec4 final36 = vec4(_and_(_and_(edr60, edr30), edr45));
    vec4 finalrn = vec4(_and_(not (edr45), edrrn));

    // Determine the color to mix with for each corner
    vec4 px = step(lum_df(p12, p17), lum_df(p12, p13));

    // Determine the mix amounts by combining the final rule result and corresponding
    // mix amount for the rule in each corner
    vec4 mac = final36 * max(ma30, ma60) + final30 * ma30 + final60 * ma60
            + final45 * ma45 + finalrn * marn;

    /*
     Calculate the resulting color by traversing clockwise and counter-clockwise around
     the corners of the texel

     Finally choose the result that has the largest difference from the texel's original
     color
     */
    vec3 res1 = P12;
    res1 = mix(res1, mix(P13, P17, px.x), mac.x);
    res1 = mix(res1, mix(P7, P13, px.y), mac.y);
    res1 = mix(res1, mix(P11, P7, px.z), mac.z);
    res1 = mix(res1, mix(P17, P11, px.w), mac.w);

    vec3 res2 = P12;
    res2 = mix(res2, mix(P17, P11, px.w), mac.w);
    res2 = mix(res2, mix(P11, P7, px.z), mac.z);
    res2 = mix(res2, mix(P7, P13, px.y), mac.y);
    res2 = mix(res2, mix(P13, P17, px.x), mac.x);

    gl_FragColor = vec4(mix(res1, res2, step(c_df(P12, res1), c_df(P12, res2))),
            1.0);
}

着色器接收2D纹理,并且旨在在高分辨率2D表面(设备屏幕)上精美地缩放它。 如果重要的话,它是SABR缩放算法的优化。

它已经可以工作,并且在非常高端的设备(如LG Nexus 4)上运行正常,但在较弱的设备上它确实很慢。

对我来说真正重要的设备是三星Galaxy S 2 \\ 3,配备Mali 400MP GPU - 这个着色器表现非常糟糕。

到目前为止,我已经尝试过:

  1. 消除变化(来自ARM的马里指南的建议) - 做了微小的改进。
  2. 用我自己的函数覆盖mix()函数 - 没有用。
  3. 将浮点精度降低到lowp - 没有改变任何东西。

我通过计算渲染时间(在eglSwapBuffers之前和之后)测量性能 - 这给了我一个非常线性和一致的性能测量。

除此之外,我真的不知道在哪里看,或者在这里可以优化什么......

我知道这是一个繁重的算法,我不是在寻求使用什么替代缩放方法的建议 - 我尝试了很多,这个算法给出了最好的视觉效果。 我希望以优化的方式使用完全相同的算法。

UPDATE

  1. 我发现如果我使用常量向量而不是依赖向量进行所有纹理提取,我会得到一个重大的性能提升,所以这显然是一个很大的瓶颈 - 可能是因为缓存。 但是,我仍然需要做那些提取。 我玩至少一些vec2变化的提取(没有任何调整),但它没有改善任何东西。 我想知道什么是有效轮询21个纹素的好方法。

  2. 我发现计算的主要部分是使用完全相同的纹素集进行多次 - 因为输出至少缩放了x2,并且我使用GL_NEAREST进行轮询。 至少有4个片段落在完全相同的纹素上。 如果在高分辨率设备上缩放为x4,则有16个片段落在相同的纹素上 - 这是一个很大的浪费。 有没有办法执行额外的着色器传递,它将计算多个片段中不会更改的所有值? 我考虑渲染到额外的屏幕外纹理,但我需要为每个纹素存储多个值,而不仅仅是一个。

UPDATE

  1. 试图使用已知的布尔规则简化布尔表达式 - 为我节省了一些操作,但对性能没有任何影响。

UPDATE

  1. 考虑一种将计算传递给顶点着色器的方法 - 只需要一个“几何体”来创建我的全屏,但是在缩放之前有许多顶点对应于每个原始像素。 例如,如果我的原始纹理是320x200而我的目标屏幕是1280x800,则将均匀分布320x200个顶点。 然后,在这些顶点中进行大部分计算。 问题是 - 我的目标设备(S2 \\ S3)不支持顶点纹理采样。

UPDATE

  1. LG Nexus 4与三星Galaxy S3的测量性能表明,Nexus 4的运行速度提高了10倍以上。 怎么会这样? 这些是来自同一代,相同分辨率等的2个设备......在某些情况下,Mali 400MP是否真的很糟糕? 我确信有一些非常具体的东西使得它与Nexus 4相比运行得如此之慢(但还没有找到)。

根据我的经验,移动GPU性能大致与texture2D调用的数量texture2D 你有21个真的很多。 通常,内存查找比计算慢几百倍,因此您可以进行大量计算并仍然在纹理查找上遇到瓶颈。 (这也意味着优化其余的代码可能效果不大,因为它意味着在等待纹理查找时不是忙,而是在等待纹理查找时它将处于空闲状态。)所以你需要减少你打电话的texture2Ds的数量。

很难说如何做到这一点,因为我不太了解你的着色器,但有些想法:

  • 将它分成水平传递然后垂直传递。 这仅适用于某些着色器,例如模糊,但它可以严重减少纹理查找的数量。 例如,5x5高斯模糊天真地进行25次纹理查找; 如果水平然后垂直完成,它只使用10。
  • 使用线性过滤来“欺骗”。 如果您使用线性过滤精确地在4个像素之间进行采样而不是1个像素的中间,则可以免费获得所有4个像素的平均值。 但是我不知道它如何影响你的着色器。 再次在模糊示例中,使用线性滤波一次采样两个像素,中间像素的任一侧允许您使用3个texture2D调用采样5个像素,将5x5模糊减少为水平和垂直的6个样本。
  • 只需使用较小的内核(这样你就不需要那么多样本)。 这会影响质量,因此您可能需要一些方法来检测设备性能,并在设备看起来很慢时切换到质量较低的着色器。

您可能需要注意几个Mali-400奇怪的事情:

  • 你应该真正使用变化而不需要对纹理查找进行任何调整(即在顶点着色器中计算“xyp_1_2_3.xw”等,并使用一个不同的纹理查找而不是调整它们)。
  • 在一些特定数量的指令(不幸的是,NDAs阻止我泄露这个数字),性能下降得非常糟糕。 您可以从脱机编译器获取指令计数。 要解决此问题,您可以将着色器拆分为多个较小的着色器,并使用未记录的GL_ARM_framebuffer_read-extension来读取前一个的结果。 (谷歌似乎可以告诉你如何使用它。在离线着色器编译器的二进制文件中稍微改进一些也可能有帮助)

片段着色器性能的上限(执行时间的下限)由21个纹理加载和一个写入帧缓冲区( gl_FragColor = )设置。 构建一个简单执行21次加载的片段着色器,将每个加载的结果累积到一个vec4然后将其写出来是值得的。 如果在麻烦的目标硬件上运行此着色器,您将知道更复杂的着色器所在的位置与这些特定GPU /驱动程序/平台版本的最大潜在性能之间的差异。 你的真实着色器只能比这慢,所以如果这个简单的测试着色器本身太慢,你将不得不寻找更远的解决方案。

一旦确定了这个基线,我只会提出一些模糊的建议来改进你真正感兴趣的着色器,但也许我的理由很有意义。 我看到你的代码在顶部聚集了所有纹理加载。 在硬件级别,纹理负载具有极长的延迟,但GPU着色处理器能够在它们运行时执行其他操作,包括在同一工作块中运行其他线程。 这意味着最终的着色器二进制文件在负载的阴影下有很多算术工作,它将在负载的阴影下免费进行算术运算,并且使用少量寄存器的着色器程序将允许在同时,每个线程都可能执行其算术工作,而其他线程被阻止加载纹素。 希望任何着色器编译器都会移动代码以实现所需的交错。 但是,给它一只手也不会有害,因此:

  • 尝试将每个算术语句向上(词汇上)移动到文件中,尽可能高,而不会破坏。 如果编译器错过了一个技巧,这可能有助于扩散你的负载。
  • 尝试尽快使用所有中间结果,以便编译器能够识别出它们的变量已经死亡,从而释放寄存器。 可能会减少寄存器的使用,从而增加程序的占用率。 实现这种效果的一个想法是,如果你有一堆最终总结的部分结果,那么将生成每个部分结果时,将许多变量保持部分结果的最终总和转换为累积为单个变量。

与性能一样,YMMV

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM