Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Precision

Discussion in 'Shaders' started by opel_cobalt, May 19, 2021.

  1. opel_cobalt

    opel_cobalt

    Joined:
    May 18, 2021
    Posts:
    26
    A bunch of questions that I couldn’t find precise answers to:

    - IIRC there is a precision conversion overhead, does it occur both ways (float->half and half->float)?

    - Does mesh data (normals/UVs) always come into vertex program as floats?

    - If it does, when is it reasonable to use half for unit vectors (normals, tangents), despite the conversion overhead? (how complex should subsequent operations on the vector be for half to be viable in such cases?)

    - half h = .00000001, will there be a conversion overhead, or different shader variants will be generated for GPUs that do and do not support half?

    - How do unity macros get along with half? Looking at the source code, some macros use float, some use half, constants are float, does it all get optimized in compile time to prevent conversions?

    - What percentage of current mobile devices supports half precision? When do you think half will become obsolete like fixed currently is?
     
    Last edited: May 19, 2021
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,028
    Yes, both ways.

    UVs should be floats, otherwise you don't have enough precision.
    Not sure about the normals part.

    This is a compile time constant, no conversion overhead here.

    No, there is no automatic precision optimisation.

    All of them

    Probably, never. Half offers a good performance and memory footprint improvement over float.
     
    opel_cobalt and SugoiDev like this.
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,363
    The one caveat is the above answers are all in the context of mobile. The answers for desktop would be:

    No, because
    half
    is always treated as a
    float
    .

    Yes, because it's always a float.

    No, because it's always a float.

    No, because it's a compile time constant. Also, it's always a float even if you write it as a half precision value.

    Yes, because it's always a float.

    All of them.

    Never, and
    half
    precision has been re-implemented into desktop and consoles GPUs as well because it can offer a not insignificant performance improvement if used properly. However it's only available when using Vulkan or Direct3D 12 when using the DXC shader compiler and when running on those GPUs that support it, or at least will be once that works.
     
    opel_cobalt and SugoiDev like this.