Hello,
I’m implementing the renderer for my game in OpenGL ES 2 and targeting Android mobile devices, like Galaxy S.
I have a fragment shader which does some tricky texture masking to a screen aligned quad (ortho projection).
Thing is it’s currently running with 17 fps. I have tried many things to optimize it, like:
+ removed all branching code (ifs) and float comparison (3 fps increase)
+ tried to put lowp to everything (1 fps increase on average)
For instance if I use comment out line in main() as the color output… performance goes up to 40-50 fps.
Any ideas why this runs so slow?
#ifdef GL_FRAGMENT_PRECISION_HIGH
// Default precision
precision highp float;
#else
precision mediump float;
#endif
uniform sampler2D ScreenSampler;
uniform sampler2D BGSampler;
uniform sampler2D RockSampler;
uniform sampler2D HomeSampler;
uniform sampler2D GrassSampler;
uniform sampler2D GoldenSampler;
uniform vec2 Texel;
uniform vec2 Aspect;
uniform float Wave;
varying vec2 vTextureCoord;
vec4 PixelShaderFn()
{
vec4 bg = texture2D(BGSampler, vTextureCoord * Aspect);
vec4 wave1 = texture2D(BGSampler, vec2(vTextureCoord.x - Wave * 0.03, vTextureCoord.y - Wave * 0.01));
vec4 wave2 = texture2D(BGSampler, vec2(vTextureCoord.x + Wave * 0.02, vTextureCoord.y + Wave * 0.02));
vec4 rock = texture2D(RockSampler, vTextureCoord * Aspect);
vec4 home = texture2D(HomeSampler, vTextureCoord * Aspect);
vec4 grass = texture2D(GrassSampler, vTextureCoord * Aspect);
vec4 golden = texture2D(GoldenSampler, vTextureCoord * Aspect * 4.0);
vec4 color = texture2D(ScreenSampler, vTextureCoord);
float top = texture2D(ScreenSampler, vec2(vTextureCoord.x, vTextureCoord.y - Texel.y * 4.0)).r;
float top2 = texture2D(ScreenSampler, vec2(vTextureCoord.x, vTextureCoord.y - Texel.y * 9.0)).r;
float top3 = texture2D(ScreenSampler, vec2(vTextureCoord.x, vTextureCoord.y - Texel.y * 22.0)).r;
float bottom = texture2D(ScreenSampler, vec2(vTextureCoord.x, vTextureCoord.y + Texel.y * 2.0)).r;
float bottom2 = texture2D(ScreenSampler, vec2(vTextureCoord.x, vTextureCoord.y + Texel.y * 4.0)).r;
float bottom3 = texture2D(ScreenSampler, vec2(vTextureCoord.x, vTextureCoord.y + Texel.y * 7.0)).r;
float groundX = step(0.5, color.g);
vec4 ground = grass * (1.0 - groundX) + home * groundX;
float goldX = color.b * color.r;
float lavaX = color.b - color.r;
vec4 lava = vec4(1.0, wave1.g + wave2.g, wave1.ba);
bg = bg * (1.0 - lavaX) + lava * lavaX;
top = (top * 0.05 + top2 * 0.09 + top3 * 0.15) * (1.0 - color.r);
bottom = (bottom * 0.2 + bottom2 * 0.6 + bottom3) * (1.0 - color.r);
vec4 base = bg * (1.0 - color.r) + rock * color.r;
base = base * (1.0 - bottom) + ground * (bottom - top);
base = base * (1.0 - goldX) + golden * goldX;
return base;
}
void main(void)
{
//gl_FragColor = texture2D( ScreenSampler, vTextureCoord );
gl_FragColor = PixelShaderFn();
}