下面是一个表现不佳的片段着色器。删除条件分支似乎并没有提高性能。只有 150 个多边形,我在 Kindle Fire 上获得 10fps,在 Galaxy S3 上获得 20fps。如果有的话,对优化这个的最佳方法有什么想法吗?iPad 2 上的类似着色器运行 30+fps。
在下面的代码中,texture1 到 texture8 被绑定到八个纹理。VertexTexturesOut1 和 VertexTexturesOut2 是从顶点着色器传递过来的,它们的值是 0.0 到 1.0,表示要混合的着色量。这个想法是混合景观纹理,因此草均匀地融入泥土、岩石、沙子等。
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform sampler2D texture3;
uniform sampler2D texture4;
uniform sampler2D texture5;
uniform sampler2D texture6;
uniform sampler2D texture7;
uniform sampler2D texture8;
varying mediump vec2 TextureCoordOut;
varying lowp vec4 VertexTexturesOut1;
varying lowp vec4 VertexTexturesOut2;
...
lowp vec4 Color = vec4( 0.0, 0.0, 0.0, 0.0);
if (VertexTexturesOut1.x != 0.0) Color = Color + texture2D ( texture1, TextureCoordOut ) * VertexTexturesOut1.x;
if (VertexTexturesOut1.y != 0.0) Color = Color + texture2D ( texture2, TextureCoordOut ) * VertexTexturesOut1.y;
if (VertexTexturesOut1.z != 0.0) Color = Color + texture2D ( texture3, TextureCoordOut ) * VertexTexturesOut1.z;
if (VertexTexturesOut1.w != 0.0) Color = Color + texture2D ( texture4, TextureCoordOut ) * VertexTexturesOut1.w;
if (VertexTexturesOut2.x != 0.0) Color = Color + texture2D ( texture5, TextureCoordOut ) * VertexTexturesOut2.x;
if (VertexTexturesOut2.y != 0.0) Color = Color + texture2D ( texture6, TextureCoordOut ) * VertexTexturesOut2.y;
if (VertexTexturesOut2.z != 0.0) Color = Color + texture2D ( texture7, TextureCoordOut ) * VertexTexturesOut2.z;
if (VertexTexturesOut2.w != 0.0) Color = Color + texture2D ( texture8, TextureCoordOut ) * VertexTexturesOut2.w;
gl_FragColor = Color;