Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

floating point division by zero at line warning in Unity 5.1

Discussion in 'Shaders' started by defaxer, Jun 9, 2015.

  1. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
    I'm getting 'floating point division by zero' error in all of my old Legacy Surface shaders with rim lighting at this line:
    half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));

    They were fine in Unity 5.0.1. Did something changed?
     
    SamFernGamer4k likes this.
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It wasn't fine before; warnings are displayed properly now (bugfix). You should update the shaders not to generate the warning, since they won't necessarily work properly on all GPUs.

    --Eric
     
  3. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Apparently that code hasn't been updated/fixed yet.

    --Eric
     
  5. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
    Well, seems like getting rid of normalize here fixes warning (without producing any visual difference):

    half rim = 1.0 - saturate(dot(IN.viewDir, o.Normal));
     
  6. DaveHoskins

    DaveHoskins

    Joined:
    Sep 9, 2013
    Posts:
    190
    That would imply that IN.viewDir was a zero vector, which it should never be, perhaps figure out why that's happening before removing a normalise.
     
  7. defaxer

    defaxer

    Joined:
    Nov 15, 2010
    Posts:
    140
    That is a temporary solution (to get rid of warnings), but I think viewDirection should never happen to be unnormalized in the first place
     
  8. razielanarki

    razielanarki

    Joined:
    Jun 1, 2014
    Posts:
    58
    i'm getting the same error with the rim shader example from the docs, and i cannot seem to find a working solution.
    how would i go about fixing this?
     
  9. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,051
    Since this is a compile time error I assume its not due to viewDirection being a zero vector, or any other vector being zero, but that the compiler cannot in some cases guarantee the input would never cause a divide by zero error and thus gives the warning.

    If you check through Unity's built-in shaders you'll find all instances of cases where this would get a warning ( at least in standard shader code) now replace 'normalize()' with the following.

    Code (csharp):
    1. inline half3 Unity_SafeNormalize(half3 inVec)
    2. {
    3.     half dp3 = max(0.001f, dot(inVec, inVec));
    4.     return inVec * rsqrt(dp3);
    5. }
    This function can be found in 'UnityStandardBRDF.cginc' and thus you can as of Unity 5.1 ( maybe earlier versions?) simply replace 'Normalize()' with 'Unity_SafeNormalize()'.
     
  10. essimoon2

    essimoon2

    Joined:
    Nov 13, 2013
    Posts:
    195
    nice, this works great!
     
  11. manutoo

    manutoo

    Joined:
    Jul 13, 2010
    Posts:
    522
    I did that. It removed the warning. Great !

    I then put back Normalize(), and the warning didn't come back. I'm sure it's the expected behavior... :p
     
  12. Manny Calavera

    Manny Calavera

    Joined:
    Oct 19, 2011
    Posts:
    205
    Similarly, the following line is now also generating a warning.

    Code (CSharp):
    1. half2 screenUV = IN.screenPos.xy / IN.screenPos.w;
    This line is a somewhat common practice in surface shader screen effects, like...

    Code (CSharp):
    1.         struct Input
    2.         {
    3.             float2 uv_MainTex;
    4.             float4 screenPos;
    5.         };
    6.         void surf (Input IN, inout SurfaceOutput o)
    7.         {
    8.             // Get the screen space position of the current pixel
    9.             half2 screenUV = IN.screenPos.xy / IN.screenPos.w;
    10.  
    11.            ...
    12.         }
    13.  
    Is there an alternative way of writing it? I've also tried ComputeScreenPos() and ComputeGrabScreenPos() from UnityCG.gcinc but the result is way off...

    Or should I just do a max() like Unity_SafeNormalize() does in order to easy the compiler?
     
  13. jcuriel_gsn

    jcuriel_gsn

    Joined:
    Apr 4, 2016
    Posts:
    1
    @Manny Calavera : I know it's been a while, but did you ever find a solution? If so, could you share?
     
  14. Manny Calavera

    Manny Calavera

    Joined:
    Oct 19, 2011
    Posts:
    205
    Sorry, I haven't found a solution. The warning is still there haunting me from time to time.. :(
     
  15. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    A word of caution... I switched all of our shaders to Unity_SafeNormalize when this first cropped up but found it cross compiled to assorted platforms oddly. normalize on its own should be fine, every gpu probably has a normalize built in to it.
     
  16. steinbitglis

    steinbitglis

    Joined:
    Sep 22, 2011
    Posts:
    254
  17. Sailendu

    Sailendu

    Joined:
    Jul 23, 2009
    Posts:
    254
    I see it is an old thread, but did you find any solution for this issue? I am receiving this exact warning on a shader. It is the exact line:

    Code (CSharp):
    1. half2 screenUV = IN.screenPos.xy / IN.screenPos.w;