0

Here is my fragment shader program which runs on iOS 5.1.1 on iPhone 3g.

#ifdef GL_ES
precision lowp float;
#endif

varying vec2 v_texCoord;

void main()
{
    float offset = sin(v_texCoord.x * 10.0);
    // offset = offset * 1.0; // (!!!)
    gl_FragColor = vec4(offset, 0.0, 0.0, 1.0);
}

Which produces nice sin:

sin generated by fragment shader

Note the line marked by (!!!). I suppose, multiplying float value by 1.0 must change nothing. (The same shader being run under Windows OS works exactly as expected.) So, I uncomment the line and receive this:

wtf?!

WTF?!


How to reproduce:

  1. Create iOS Game project from standard template in xcode. If you run the project then you will see two floating cubes. One red and one blue.
  2. Change contents of Shader.vsh to the following. (I've just added one varying parameter v_pos).

    attribute vec4 position;
    attribute vec3 normal;
    
    varying lowp vec4 colorVarying;
    varying lowp vec4 v_pos;
    
    uniform mat4 modelViewProjectionMatrix;
    uniform mat3 normalMatrix;
    
    void main()
    {
        vec3 eyeNormal = normalize(normalMatrix * normal);
        vec3 lightPosition = vec3(0.0, 0.0, 1.0);
        vec4 diffuseColor = vec4(0.4, 0.4, 1.0, 1.0);
    
        float nDotVP = max(0.0, dot(eyeNormal, normalize(lightPosition)));
    
        colorVarying = diffuseColor * nDotVP;
    
        gl_Position = modelViewProjectionMatrix * position;
        v_pos = gl_Position;
    }
    
  3. Chnge Shader.fsh to the following.

    varying lowp vec4 colorVarying;
    varying lowp vec4 v_pos;
    
    void main()
    {
        gl_FragColor = colorVarying;
        lowp float a = sin(v_pos.x * 10.0);
        // a = a * 1.0; // (!!!)
        gl_FragColor = vec4(a, 0.0, 0.0, 1.0);
    }
    
  4. Run program on a device and see cool bars on blue cube: Cool bars

  5. Uncomment the line (a = a * 1.0;) marked by (!!!) and run again: Uh?

lost_guadelenn
  • 447
  • 4
  • 14
  • Is a rounding error introduced by the graphics hardware on iOS? – deanWombourne Aug 01 '12 at 11:53
  • It look like. I've also tested this on iPad, and it works ok. I'will also try to test it on other iPhone devices. – lost_guadelenn Aug 01 '12 at 12:29
  • Problem also exists on iPhone 4. – lost_guadelenn Aug 01 '12 at 13:34
  • I'd file this as a bug report to Apple, with a sample project based on the above code and the images you provide here. This looks like a potential issue with the iOS shader compiler, and I know the folks there would like to take a look at it to fix it for iOS 6.0 (if it hasn't been already). – Brad Larson Aug 01 '12 at 16:06

1 Answers1

0

Well, let me answer my own question. The problem is in lowp precision qualifier used in shader. After changing it to mediump shader started to work as expected.

lost_guadelenn
  • 447
  • 4
  • 14