shaders optimisation
When we test Lugaru, we found that gl4es created such fragment shader
#version 100
precision mediump float;
precision mediump int;
// FPE_Shader generated
varying vec4 Color;
varying vec2 _gl4es_TexCoord_0;
uniform sampler2D _gl4es_TexSampler_0;
uniform float _gl4es_AlphaRef;
void main()
{
vec4 fColor = Color;
vec4 texColor0 = texture2D(_gl4es_TexSampler_0, _gl4es_TexCoord_0);
fColor.rgb *= texColor0.rgb;
if (floor(fColor.a*255.) <= _gl4es_AlphaRef) discard;
gl_FragColor = fColor;
}
And one of devs says: I don't know how clever those GLSL compilers are, but it seems to me that it might be possible to discard the fragment before texture is sampled and color multiplied, i.e:
void main()
{
vec4 fColor = Color;
if (floor(fColor.a*255.) <= _gl4es_AlphaRef) discard;
vec4 texColor0 = texture2D(_gl4es_TexSampler_0, _gl4es_TexCoord_0);
fColor.rgb *= texColor0.rgb;
gl_FragColor = fColor;
}
But probabaly it done like this just because you need to cope with all the situations, so the generated shaders can't be 100% optimized ?
Yes, indeed. Because th eshader is generated, in many possible case the final Alpha color can only be known after the fetch of the texel. Still, I agree that in some cases like this one, doing the discard before may help perf. I'll see if I can do something.
Wasn't that optimization take place in all the recent work last year ?:) Just if so we can close this one.
Nope, this particular optimization is not in place in fpe_shader for now. It's a bit tricky to do (but I do have some idea on how to implement it without to much changes). Maybe a bit later (I am doing some refactoring on VertexAttribute for now...).