gl4es icon indicating copy to clipboard operation
gl4es copied to clipboard

shaders optimisation

Open kas1e opened this issue 6 years ago • 3 comments

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 ?

kas1e avatar Mar 29 '19 12:03 kas1e

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.

ptitSeb avatar Mar 29 '19 14:03 ptitSeb

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

kas1e avatar Feb 23 '20 09:02 kas1e

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

ptitSeb avatar Feb 23 '20 09:02 ptitSeb