Search Unity

Shaders: smoothstep is broken on iPhone 5 and below

Discussion in 'iOS and tvOS' started by cgJames, May 28, 2015.

  1. cgJames

    cgJames

    Joined:
    Dec 3, 2014
    Posts:
    30
    We've reported a bug (700619) where using the smoothstep function in shaders returns incorrect values. We've confirmed this on iPhone 5, 4s and 4. The function is not broken on iPhone 5s, 6, 6 plus, iPad air. We've had reports of similar symptoms on the Galaxy S4 too, although the S5 is unaffected.

    To prove the problem, we used the following function over the domain [0,1] with 0.05 increments:
    smoothstep(0, 1, x)

    The chart below demonstrates the problem quite clearly:



    You can see the iPhone 5 returns spectacularly different results.

    For anyone else suffering the same problem, you can implement your own smoothstep function as follows:

    Code (Shader):
    1. float y_smoothstep(float a, float b, float x) {
    2.     float t = clamp((x - a) / (b - a), 0.0, 1.0);
    3.  
    4.     return t * t * (3.0 - (2.0 * t));
    5. }
    In implementing this workaround we (possibly) discovered the underlying problem: when Unity compiles the smoothstep function, the generated GLSL ES uses lowp (low precision) qualifiers for temporary variables, whereas when it compiles our version of the function it generates highp qualifiers. We are working with the assumption that lowp provides insufficient precision to properly calculate the smoothstep.

    If this is true, then why does it affect the iPhone 5 and not the iPhone 6 since they both use the same compiled shader? The following post provides a clue:
    http://stackoverflow.com/questions/...-for-iphone-ipod-touch-ipad/27771575#27771575

    We have not confirmed the post's assertions, but if correct the hardware differences between the devices would mean lowp provides different levels of precision: 8 for iPhone 5 and 10 for iPhone 6. Further, this could explain why we have reports of the problem on Galaxy S4 but not on S5.

    The workaround function solves our particular problem. We're not shader experts by any means, but I hope it helps anyone else with a similar issue.
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,735
    I can confirm the issue on an iPad 4. What is weird, is that it works fine on an iPhone 4. I also echo the sentiment that it appears to be related to precision.
     
  3. povilas

    povilas

    Unity Technologies

    Joined:
    Jan 28, 2014
    Posts:
    427
    Thanks for the submitted bug. We are looking into it.