7

I'm currently chasing some bugs in my OpenGL ES 2.0 fragment shader code which is running on iOS devices. The code runs fine in the simulator, but on the iPad it has huge problems and some of the calculations yield vastly different results, I had for example 0.0 on the iPad and 4013.17 on the simulator, so I'm not talking about small differences which could be the result of some rounding errors.

One of the things I noticed is that, on the iPad,

float1 = pow(float2, 2.0);

can yield results which are very different from the results of

float1 = float2 * float2;

Specifically, when using pow(x, 2.0) on a variable containing a larger negative number like -8, it seemed to return a value which satified the condition if (powResult <= 0.0).

Also, the result of both operations (pow(x, 2.0) as well as x*x) yields different results in the simulator than on the iPad.

Used floats are mediump, but I get the same stuff with highp.

Is there a simple explanation for those differences?

I'm narrowing the problem down, but it takes so much time, so maybe someone can help me here with a simple explanation.

1
  • 1
    I think you'll find that you'll even get slightly different results on the various iOS device GPUs themselves (iPad vs. iPad 2 vs. iPhone 4, etc.). This is one of the areas where you almost need to test on all devices you wish to support.
    – Brad Larson
    Aug 31, 2012 at 21:59

3 Answers 3

17

The GLSL ES documentation says pow is undefined if x < 0 or if x = 0 and y ≤ 0.

2
1

The simulator uses an x86 floating point unit and Mac OS X numerical libraries. The iPad uses either an ARM FPU.

Also pow() is a library routine that uses an approximation algorithm.

6
  • So you're saying that the iPad and the simulator can yield such different numbers, even though both conform to the OpenGL ES 2.0 'standard', and thus (IIRC) to the IEEE Standard for Floating-Point Arithmetic (IEEE 754)? I thought those standards are made to prevent exactly something like this. Aug 31, 2012 at 21:56
  • 6
    Note that they're asking about GLSL shaders, so this is actually a calculation performed on the GPU, not on CPU-bound standard libraries. We're not talking ARM vs. x86 here, but PowerVR vs. the (mostly software) emulation used in the iOS Simulator.
    – Brad Larson
    Aug 31, 2012 at 21:57
  • 3
    @cheeesus OpenGL ES 2 standard does not mandate a strict conformance to IEEE 754 standard, take a look at GLSL ES specification in 'Precision and Precision Qualifiers' chapter, the requirements are about minimum precision ranges.
    – rotoglup
    Aug 31, 2012 at 22:11
  • Thank you everyone. The pow() function was the reason for those vast differences, the implementation of pow() on the iPad's GPU really has some issues, specially with negative numbers. @JamesThiele, maybe you could edit your post to reflect BradLarson's remarks. Aug 31, 2012 at 22:55
  • 4
    In OpenGL, pow is incompletely implemented; the specification does not define it for x < 0. (See my answer.) On the main CPU, pow should be pretty good, and pow(x, 2) may be optimized to x*x by the compiler. Aug 31, 2012 at 23:18
1

In GLSL pow is implemented as a function of exp2 and log2. Since the logarithm function is not defined for negative real numbers, pow() is not either.

See the GLSL ES 3.0 specification page 47, or GLSL 4.4 specification page 88.

pow(x, y) Inherited from exp2(x * log2(y))

Also from the specification:

genType pow(genType x, genType y)

  • Returns x raised to the y power, i.e., x^y
  • Results are undefined if x < 0.
  • Results are undefined if x = 0 and y <= 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.