ptitSeb / gl4es

GL4ES is a OpenGL 2.1/1.5 to GL ES 2.0/1.1 translation library, with support for Pandora, ODroid, OrangePI, CHIP, Raspberry PI, Android, Emscripten and AmigaOS4.
http://ptitseb.github.io/gl4es/
MIT License
698 stars 159 forks source link

shaders optimisation #104

Open kas1e opened 5 years ago

kas1e commented 5 years ago

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 ?

ptitSeb commented 5 years ago

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.

kas1e commented 4 years ago

Wasn't that optimization take place in all the recent work last year ?:) Just if so we can close this one.

ptitSeb commented 4 years ago

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...).