Damian
Damian

Reputation: 5561

OpenGL ES shader degrades too much performance

I'm optimizing a game that works for both, iPhone and Android. I'm using 4 shader to draw the scene and I noticed that if I change one of them to another the fps goes from 32 to 42, even though there's only 1 sprite being drawn with that shader, and the only difference in this 2 shaders is just a product in the fragmente shader.

These are the shaders:

default-2d-tex.shader

#ifdef GL_ES
precision highp float;
precision lowp int;
#endif

#ifdef VERTEX

uniform mat4        umvp;

attribute vec4      avertex;
attribute vec2      auv;

varying vec2        vuv;

void main()
{
    // Pass the texture coordinate attribute to a varying.
    vuv = auv;

    // Here we set the final position to this vertex.
    gl_Position = umvp * avertex;
}

#endif

#ifdef FRAGMENT

uniform sampler2D   map0;
uniform vec4 ucolor;

varying vec2        vuv;

void main()
{
    gl_FragColor =  texture2D(map0, vuv) * ucolor;
}

#endif

default-2d-tex-white.shader

#ifdef GL_ES
precision highp float;
precision lowp int;
#endif

#ifdef VERTEX

uniform mat4        umvp;

attribute vec4      avertex;
attribute vec2      auv;

varying vec2        vuv;

void main()
{
    // Pass the texture coordinate attribute to a varying.
    vuv = auv;

    // Here we set the final position to this vertex.
    gl_Position = umvp * avertex;
}

#endif

#ifdef FRAGMENT

uniform sampler2D   map0;

varying vec2        vuv;

void main()
{
    gl_FragColor =  texture2D(map0, vuv);
}

#endif

Again,
If I modify default-2d-tex.shader and remove the product "* ucolor", the fps goes from 32 to 42, and I'm using it for just one sprite in the scene!

Is this normal? Why is this shader being so slow and how can I improve it?

EDIT:

I see this performance slowdown on both iPod and Android in an equal ratio. Both are PowerVr SGX GPUs (iPod 3rd gen and Samsung Galaxy SL -PowerVR SGX 530-). iOS version is 4.1 and Android is 2.3.3

The sprite I'm drawing is scaled to fill the screen (scaled to 4x) and I'm drawing it once per frame. It's taken from a texture map so the texture is actually larger (1024x1024) but the portion taken is 80x120. Alpha blending is enabled.

EDIT 2

I made a mistake. The sprite is scaled 11x: its 32x48. If I don't draw that sprite at all, fps goes to 45. I'm drawing a lot of sprites in the scene, why is that one taking so much time? Could it be because it's scaled so much?

Upvotes: 1

Views: 3680

Answers (2)

StiX
StiX

Reputation: 44

Your problem is in your fragment shader in default-2d-tex.shader. You have uniform vec4 ucolor; That means, that each of your color's components (RGBA) will be converted to 32-bits float value. And this drops performance heavily. Should be: uniform lowp vec4 ucolor;

Upvotes: 2

Sergey K.
Sergey K.

Reputation: 25386

When you remove "* ucolor" from the abovementioned code many things happen:

  1. uniform ucolor becomes unused
  2. GLSL compiler removes this uniform from the active uniforms set
  3. when you have you program linked the ID of the missing uniform becomes -1
  4. when you do glUniform4fv( -1, value ) for the removed uniform it just exits instantly, without any update

This is pretty much less work compared to having "* ucolor" in place.

And additionally, this is one less operation per fragment in the fragment shader.

Upvotes: 4

Related Questions