[libGDX] Can this small scanlines GLSL shader be further optimised?

I’ve implemented a simple scanline shader into a libGDX mobile game I’m working on. Here are the vertex and fragment scripts:


attribute vec4 a_position;
attribute vec4 a_color;
attribute vec2 a_texCoord0;

uniform mat4 u_projTrans;

varying vec4 v_color;
varying vec2 v_texCoords;

void main()
   v_color = a_color;
   v_texCoords = a_texCoord0;
   gl_Position =  u_projTrans * a_position;


#ifdef GL_ES
    precision mediump float;

const float INTENSITY = 0.15;

varying vec2 v_texCoords;
varying vec4 v_color;

uniform sampler2D u_texture;
uniform float u_scanlineCountMultipliedByScreenHeight;

void main()
    vec4 texture = texture2D(u_texture, v_texCoords);
    float screenV = gl_FragCoord.y * u_scanlineCountMultipliedByScreenHeight;
    float scanLine = INTENSITY * mod(screenV, 1.0);
    gl_FragColor = v_color * vec4(texture.rgb - scanLine, texture.a);

Before applying this shader, I get a pretty decent 55 to 60 fps on my 6 year old ZTE-BLADE. After applying the shader, I get 30 to 40fps. I’ve already optimised the game to within an inch of its life, so the only option left is to optimise the shader, if possible. I’m willing to accept that my ancient mobile simply isn’t up to the task, in which case I will add a scanlines on/off button, but if anyone could see something obvious that could be optimised, I’d be very interested to test it out!

I’d also be interested to know if it might be more efficient to use a full screen transparency instead of a shader to achieve the scanline effect.

That shader is already about as efficient (and simple!) as it is possible to get. If you are therefore suffering a 50% frame rate drop when you turn it on, it would probably be the case that you are up against the fill rate limit of your GPU… which I imagine is a terrible, terrible GPU.

Cas :slight_smile:

You can try draw on top of game - scanline texture with basic GL blend options
and move texture by texture matrix, or swap it like animation

p.s 25-30 fps for mobile - is ok
IMHO: Img looks brain eating.
but many companies use this standard

As a simple test, I manually created a sprite that is the same size as my output resolution. The sprite has alternating black lines and transparent lines, simulating a scanline effect. Drawing this full screen scanline image over my game is a lot more performant than using the shader, so I think I will go with this route instead. There’s a little hitch though, my game uses an OrthographicCamera to scale the game screen to whatever the resolution of the device is, so I can’t simply drawn the scanline sprite over the top of this as I don’t want the scanlines to scale.

I’ve created a new thread for the new set of problems I’ve encountered: