ValhallaTeam / angleproject

Automatically exported from code.google.com/p/angleproject
Other
0 stars 0 forks source link

wrong float math behaviour #417

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
1. Declare in a fragment shader:

    uniform vec2 p;

2.Initialize it with (0.392, 1e-10), if using Qt this way for example:

    QVector2D p(0.392f, 1e-10f);
    program.setUniformValue("p", p);

3. In the fragment shader add the strings:

    float a = p.x + p.y;
    float b = p.y - (a - p.x);

    gl_FragColor = vec4(b == 0.0 ? 1.0 : 0.0, 0.0, 0.0, 1.0);

p.x + p.y precise result is impossible to put in a, so b should contain 
rounding error and must not be zero, but b == 0.0 appears true. Seems it is 
true not only for such values but always. 

Win7-64, Angle 1.0.0.1318, tested on NV GT 9800 and NV GTX 560.

I use code for double precision emulation based on 
http://andrewthall.org/papers/df64_qf128.pdf and it works OK with GLSL. 

Now I'm trying to run it with GLSL ES and in all cases it produces zero 
rounding error; results are similar as with single precision.

Original issue reported on code.google.com by okurt...@gmail.com on 5 Mar 2013 at 10:25

GoogleCodeExporter commented 9 years ago
The GLSL ES specification makes very few guarantees about the actual precision 
at which calculations are done, what precision results are stored at, and which 
optimizations are allowed.

http://www.khronos.org/registry/gles/specs/2.0/GLSL_ES_Specification_1.0.17.pdf
Section 4.5.2: "Implementations may use greater range and precision than 
requested, but not less."
Section 4.6.2, Example 2: "There is no mechanism to enforce invariance between 
a and b."
Section 10.30: "The precision of operations are [...] implementation dependent."
Section 10.31: "A specified set of [compiler] transforms (in addition to those 
permitted by C++) are allowed."

In this case there's likely a compiler transform where 'b' is symbolically 
determined to be be zero, and thus the operations are entirely eliminated. This 
is valid since they are allowed to be performed at higher precision and thus 
rounding errors would be eliminated.

Original comment by nicolas....@gmail.com on 5 Mar 2013 at 2:31