Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity Techs: Why are you doing this: !(!Math.fround) - WebGL

Discussion in 'WebGL' started by levwsr, Oct 16, 2015.

  1. levwsr

    levwsr

    Joined:
    Jul 23, 2012
    Posts:
    68
    in your default generated index.html you have a block of code as an else case to:
    if (!(!Math.fround)) {
    } else { ..}

    but it seems firefox and chrome always support Math.fround so its not clear why you care about the else case??

    Thanks
     
  2. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    See here for compatibility:

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/fround

    1. IE does not support it.
    2. Older versions of browsers don't support it.

    Actually, in Unity 5.3, we have changed this condition to always be invoked in Chrome, even though Chrome supports Math.fround. The reason is that we found out that Math.fround is actually:

    1. making the code significantly slower in Chrome (instead of faster like in other browsers).
    2. making the code use more memory in parsing, which increases the likely hood of crashes at startup time.
     
    holliebuckets likes this.
  3. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    holliebuckets likes this.
  4. levwsr

    levwsr

    Joined:
    Jul 23, 2012
    Posts:
    68
    I hope some other devs find this as useful as we did. Thanks J!
     
    holliebuckets likes this.
  5. support

    support

    Joined:
    Oct 24, 2013
    Posts:
    19
    We found that our game runs much slower in Chrome without Math.fround. Please leave this options to developers in 5.3! For example, via Module setting...
     
  6. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    Interesting. Do you have a link to your game we could try?
     
  7. support

    support

    Joined:
    Oct 24, 2013
    Posts:
    19
  8. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    This does show the fround version being faster (unlike all our benchmarks).

    My guess would be that the difference is in how you disable fround. You override it with a new function. But this is still a function call, which comes with a certain overhead. The way emscripten/Unity disables fround is by actually patching the code to remove any calls to it, which gets rid of that overhead.
     
  9. support

    support

    Joined:
    Oct 24, 2013
    Posts:
    19
    You are right, if we do "s/Math_fround\(/\(/g" substitution in the js file, we see no hit in performance in either Chrome or Firefox. Is there any reason to have Math.fround at all?
     
  10. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    Yes, it should make the code run faster in Firefox (and does in our benchmarks). It also makes the code match the behavior of floating point numerics of native code better.
     
  11. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    162
    hi jonas,

    in my experience the whole fround replacing in-browser adds up to the load time (which is crucial to us) and often leads to memory peaks and crashes in some browser configurations (*cough* IE! *cough*).

    The actual fround-removing is something that could easily be done at build time. Could you please consider making this an option?

    I currently take Unity's build output, create a non-fround copy of the JS file post-build and patch everything up with some build scripts, so that the right file is loaded. Although it works, this is tedious and breaks with smallest Unity changes (5.2 to 5.3 for example). A build option in the player settings (like "Prepare optimized JS for IE/Chrome") would be much appreciated and should be fairly easy to do for you.

    thanks,
    atti

    edit:
    if anyone could use the C# based method that generates the non-fround js file, poke me.
     
  12. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    We have considered doing this at build time, but won't do that. Some reasons:

    -There is a lot of confusion (check many threads on this forum) about the build size of WebGL builds. Which is why the 5.3 build setup will only emit compressed files, so that when you make a release build in 5.3, you can actually assume that the total size of the download will roughly be the sum of all written files. If we wrote different version of the files, there would again be questions on why the build is so big.

    -As for making this an option: I think this whole measure is rather temporary. For IE, you're really out of luck getting any decent results out of WebGL anyways, and that won't change. For Chrome, I do expect that they will fix the performance issues around Math.fround, so patching it out will no longer be needed. I don't want to add an option for what we consider a temporary measure, as it will make it much harder to drop it in the future.
     
  13. tteneder

    tteneder

    Unity Technologies

    Joined:
    Feb 22, 2011
    Posts:
    162
    Sad to hear, but I get your point (except the "some users can't differ between build size and actually loaded size" thingy;))
    off to my dirty patch-hacks i go....
    thanks!