0

Fragment shader causes serious lag when I run it on iPhone 4. I tried to comment part of calculations, however still there are some jitters even though I barely am doing any calculation in the Fragment Shader.

// Fragment Shader Code
uniform sampler2D texture;
varying lowp vec2 fragmentTexCoords;
uniform lowp float passAlpha;
uniform lowp vec2 inPosition;
uniform lowp float varUniform;
void main()
{
    gl_FragColor = texture2D(texture, fragmentTexCoords);
    lowp float disY = gl_FragCoord.y - inPosition.y;
    lowp float disMax = 250.0;
    lowp float coeff = 1.0 - varUniform;
    gl_FragColor.rgb *= coeff;
}

//My render function is:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glFlush();

I am still not sure what could be the problem, I am sure iPhone can handle way more complex calculations ... Any ideas ?

Thanks in advance.

Nima
  • 969
  • 3
  • 10
  • 14

2 Answers2

0

I would try this, it avoids several situations where you are doing calculations at enhanced precision because of the way your variables are declared... if this improves your performance, I can further explain why it works.

// Fragment Shader Code
uniform lowp sampler2D texture;
varying lowp vec2      fragmentTexCoords;
uniform lowp float     passAlpha;
uniform lowp vec2      inPosition;
uniform lowp float     varUniform;

void main ()
{
    lowp vec4  color  = texture2D (texture, fragmentTexCoords);
    lowp float disY   = gl_FragCoord.y - inPosition.y;
    lowp float disMax = 250.0;
    lowp float coeff  = 1.0 - varUniform;
    color.rgb        *= coeff;
    gl_FragColor      = color;
}
Andon M. Coleman
  • 42,359
  • 2
  • 81
  • 106
  • Thanks Andon for the response. I think it helped a bit, it is very difficult to tell because I commented all the calculations. But even now with your suggestion, when I only uncomment one "devision" operation, on device it goes black, not the kind of black that it is not rendering anything, kind of black that it can't process in time for each frag and used coeff=0. – Nima Sep 10 '13 at 19:27
  • The GPU would not do something like use a zero if it can't process in time, it would just take longer doing the calculation. If you're seeing black with the above code, there could be a problem with the texture or texture coordinates, or your varUniform value might be greater or equal to one. – Arttu Peltonen Sep 11 '13 at 05:49
0

Are you certain the stutter is caused by your fragment shader? Can you verify this by removing most operations from the fragment shader? I'm asking because you're really not doing anything too expensive in your shader code, and it would be odd for that to cause any performance problems. Are you sure you're not doing anything else, like uploading textures, in your update loop? What do the xcode profiling tools say about your performance?

Arttu Peltonen
  • 1,250
  • 14
  • 18
  • Yes great part of stutter is caused by fragment shader. When I add a simple division of two (lowp) floats, it can't handle the computation on device and renders black. However I solved my problem by moving calculations of "coeff" to Vertex Shader and pass it to Fragment Shader to be interpolated. This improved performance to acceptable level. But still I have no idea why calculation in fragment shader would cause such lag and stutter. – Nima Sep 12 '13 at 10:39
  • Interesting that moving the calculation made such a big difference. But glad to hear you got it working. – Arttu Peltonen Sep 12 '13 at 11:29