Malisc进行Shader优化

   Malisc 是ARM提供的offline Shader编译器,可以方便的使用它验证自己Shader代码的语法和性能。Malisc并且可以给出其在不同的Mali GPU上的指令执行数量以及所需要的Circle数。可以从ARM的官方Graphics Development Tool中下载到。

   以下是采用Malisc和Mgd分析系统GPU计算性能瓶颈的记录:

   1. 采用Mgd抓取Fragments的细节如下:

    可以看出Shader 33 占用大量的Cycles,在T880上需要10条A指令,2.45个Cycles分析其Shader代码有:

void main() {
    mediump vec4 yuvColor = texture2D(yuvTexture, textureCoordinate);
    mediump vec4 nYColor = texture2D(newYTexture, textureCoordinate);
    
    mediump vec3 color = (textureCoordinate.x > split.y) ? vec3(nYColor.r, yuvColor.gb) : ((textureCoordinate.x < split.x) ? yuvColor.rgb : vec3(1, 0.5, 0.5));
    	    
    mediump vec3 yuv = vec3(color.r - 16.0/255.0, color.g - 0.5, color.b - 0.5);
    mediump vec3 rgb = colorConversion * yuv;
    gl_FragColor = vec4(rgb, 1);
}

     其中Shader为了实现左右分频的功能采用了两次逻辑判断,在shader中进行过多的逻辑判断是比较费时的,将其简化为:

void main() {
    mediump vec4 yuvColor = texture2D(yuvTexture, textureCoordinate);
    mediump vec4 nYColor = texture2D(newYTexture, textureCoordinate);
    
    mediump vec3 yuv = vec3(nYColor.r - 16.0/255.0, yuvColor.g - 0.5, yuvColor.b - 0.5);
    mediump vec3 rgb = colorConversion * yuv;
    gl_FragColor = vec4(rgb, 1);
}

    采用Malisc分析性能:

    malisc --fragment shader.cpp --core T880

Arm Mali Offline Compiler v6.2.0
(C) Copyright 2007-2017 Arm, Ltd.
All rights reserved.
No driver specified, using "Mali-T600_r20p0-00rel0" as default.
No core revision specified, using "r2p0" as default.
2 work registers used, 0 uniform registers used, spilling not used.
                                    A       L/S     T       Bound
Instructions Emitted:   6       1       2       A
Shortest Path Cycles:   1.65    1       2       T
Longest Path Cycles:    1.65    1       2       T

A = Arithmetic, L/S = Load/Store, T = Texture

    需要6条A指令,1.65个Cycles相比较于原来已经获得了明显的提高。由于是YUV to RGB的Convert操作,因此展开为浮点计算有:

void main() {
    mediump vec4 yuvColor = texture2D(yuvTexture, textureCoordinate);
    mediump vec4 nYColor = texture2D(newYTexture, textureCoordinate);
    
    highp float y = 1.1643*(nYColor.r-0.0625);
    highp float u= yuvColor.g-0.5;
    highp float v= yuvColor.b-0.5;

    highp float r= y+1.5958*v;
    highp float g= y-0.39173*u-0.81290*v;
highp float b= y+2.017*u;

    mediump vec3 rgb = vec3(r, g, b);
    rgb = min(max(rgb, 0.0), 1.0);
    gl_FragColor = vec4(rgb, 1);
}

    运行Malisc分析性能,花费指令和时钟如下:

                                   A       L/S     T       Bound

Instructions Emitted:   5       1       2            A

Shortest Path Cycles:   1.32       1       2       T

Longest Path Cycles:    1.32       1       2       T

    需要5条A指令,1.32个Cycles,制约因素为T纹理操作。

    因此优化过后shader的性能有2倍左右的提升。

猜你喜欢

转载自blog.csdn.net/smallhujiu/article/details/80964249