Search Unity

Using 'half' format floating point on PC

Discussion in 'Shaders' started by tonemcbride, Jul 31, 2014.

  1. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Hi,

    I have a custom fragment shader on PC/iOS which uses the 'half' floating point format. It works fine on PC but not quite the same on iOS. It looks like an accuracy issue so I was wondering if perhaps the 'half' floating point format I'm using is maybe being turned into a normal float on PC. If I use 'half' in a shader will a PC graphics card support that? If not is there any way I can emulate it so I can see exactly how it would look on iOS?

    Thanks,
    Tony
     
  2. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    Unfortunately, yes.. same issue can happen with Android, it's a GL-ES thing rather than an iOS thing.
     
  3. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
  4. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Yeah. All DX10+ PC GPUs pretty much ignore "half" and treat it as "float".

    Not so much on mobile; some GPUs treat half & float the same (most Adreno GPUs, I think), whereas for example Mali & PowerVR GPUs do have actual support for low precision ("fixed"), half precision and full float precision.
     
  5. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Well, if Aras is right, just try computing full floats... that might fix it.

    @Aras: please correct me if i'm wrong! :D
     
  6. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Yes. Though keep in mind that full precision floats run slower, or use more power (or both of these) on many mobile GPUs.

    Half precision floats are generally ok for colors, short vectors, object space positions (but not world space), etc.
     
    morepixels likes this.
  7. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Yeah, the problem was that I want to use the 'half' format for speed on mobiles but I can't test it properly without doing iOS builds for every change (which is really tedious!).

    If anyone knows a way of simulating the half format on newer DX cards that would be handy. I suppose after each instruction I could perhaps multiply by a fixed value then cast to an integer and back again to truncate?
     
  8. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Alternative could be doing an iOS build, running through Xcode, do a frame capture there, and editing shaders there to see the effect. When Xcode's frame capture + shader editing works, then it's pretty good.

    You'd have to edit the cross-compiled GLSL, instead of Unity's HLSL, so it might be a bit cumbersome. But worth trying.
     
  9. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    I'll give that a go thanks, it's a shame there's no easier way but I suppose you can't change the way graphics cards work :)