Search Unity

Question Pros and cons of using half

Discussion in 'Shader Graph' started by DevDunk, Jul 25, 2022.

  1. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,060
    I am starting with shader graph and wonder what the real world benefits are of fp16 (half) or fp32 (single) precision points.
    Does anyone have more info on this?
    Is fp16 usually faster and does it take up less memory? Does it have issues with some things?

    Couldn't find clear docs on this topic
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    1. fp16 can be faster.
    2. fp32 has more precision.

    fp16 should never be used for positions, and used only sparingly for UVs. fp16 is usually good enough for normals and directional vectors, unless you use those to modify the position. You can safely use fp16 for colors and lighting.

    However there's a giant caveat ... almost nothing actually supports fp16. The "half" type is defined as having "at least 16 bits of precision", and 32 bit single precision floats fulfill that requirement, so the vast majority of GPUs do just that. The only case this isn't true is for some mobile GPUs which support fp16 in hardware, and thus is actually faster. Technically modern desktop GPUs also support fp16 in hardware, but it is rife with bugs so no one uses it outside of hardcore compute tasks.
     
    Egad_McDad, Ben_at_Work and DevDunk like this.
  3. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,060
    Thanks a lot!
    So basically use half when possible in case it improves performance on some devices
     
    Last edited: Jul 25, 2022