Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

System.Numerics

Discussion in 'Experimental Scripting Previews' started by bdovaz, Jan 14, 2017.

  1. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    793
    Have you evaluated to adopt this internally?

    https://github.com/dotnet/corefx/tree/master/src/System.Numerics.Vectors/src/System/Numerics

    In .NET there are equal Vector2, Vector3, Vector4, Quaternion and so on implemented...

    Like Unity's "Mathf" class, there is a .NET Math class with all that and if it's not, you can contribute with it to .NET Framework with a pull request:

    https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/Math.cs

    https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/MathF.cs

    When you upgrade to .NET 4.6 are you going to adopt it? So we don't have redundant API and you can clean your codebase and focus on your specific API and leave the rest to Microsoft and developers community on github.

    The "problem" is how you will deal existing projects compatibility.
     
    Last edited: Jan 14, 2017
    Qbit86 and rakkarage like this.
  2. XaneFeather

    XaneFeather

    Joined:
    Sep 4, 2013
    Posts:
    90
    I've been wondering the same and vote for adopting the .NET Numerics if possible. No more converting Unity types to .NET and vice-versa when dealing with multithreading would be fantastic.
     
    rakkarage likes this.
  3. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    793
    I think that it's the smartest choice but it's up to Unity.
     
  4. xoofx

    xoofx

    Unity Technologies

    Joined:
    Nov 5, 2016
    Posts:
    296
    As you know, Unity is based on Mono Runtime (an older version, though there is an undergoing work to migrate to the newest one) and even until very recently, Mono didn't support `System.Numerics.Vectors` (see http://www.mono-project.com/news/2016/12/20/system-numeric-vectors/)

    Also, `System.Numerics.Vectors` has some design issues that makes it quite impractical to use. Typically, many methods exposed by it are passing arguments by value instead of passing them by ref. Though when using vectorized methods that are marked as "intrinsics", they will be inlined correctly by the JIT (and it will avoid a costly roundtrip to the stack), there are still many methods in their API that don't provide by ref and don't have an intrinsic (most of the methods in Matrix4x4 for example).

    Note that it is not only Unity that has some concerns with the API, you can check on this issue on corefx. You can see later in the comments that by value vs by ref has significant performance issues on some platforms (like Android) and can cost around 10 to 20%... So they have been considering to bring up by ref, but it is still in the development pipe (and only valid for CoreCLR)

    That being said, when Unity will have switched to a newer Mono, it will be possible to allow the Unity Vectors structs to be SIMD accelerated... Though, this will also require to add support for IL2CPP platform as well (used to target iOS platform for example)

    In terms of API convergence, ideally, I agree that it would be great to have an interchangeable API for this... but upgrading legacy is not always easy!
     
    ackro, zwcloud, x4000 and 2 others like this.
  5. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    793
    So I see that it's not something that it's going to be available tomorrow but when we get .NET 4.6 it will be easier to get this.

    The positive thing it's that you are on github actively reading and commenting issues and talking with Microsoft employees.
     
  6. DamonJager

    DamonJager

    Joined:
    Jul 16, 2015
    Posts:
    45
    The mono's simd library works in the editor preview. And probably you can use system.numerics with the windows store stuff? I've not tried that, but it would be 64bit alone, and you can't use the editor (yet).
     
  7. laurentlavigne

    laurentlavigne

    Joined:
    Aug 16, 2012
    Posts:
    3,603
    Wow simd in managed. How are they performance wise?
     
  8. DamonJager

    DamonJager

    Joined:
    Jul 16, 2015
    Posts:
    45
    laurentlavigne likes this.
  9. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,438
    Mono.Simd is a pretty old technology. All the current Unity builds contain Mono.Simd.dll, it's just not referenced by default. Simply drop it into your Assets folder. Work's fine.
     
    PrimalCoder and rakkarage like this.
  10. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    793
    Any news on this @xoofx? It's been a year.

    I see that the issue was closed and they agreed to add ref/in/out overloads on other issue:

    https://github.com/dotnet/corefx/pull/25388
     
  11. pavelkouril

    pavelkouril

    Joined:
    Jul 22, 2016
    Posts:
    127
    This is why I think actually supporting https://github.com/dotnet/corefx/issues/22940 is much better choice, and once fesasible, updating Unity's math library to use the intrinsics internally, would be more beneficial.

    Is it possible that Unity will support this API in near future (near future after .NET Standard 2.1 hits, to be more precise)? :)
     
  12. x4000

    x4000

    Joined:
    Mar 17, 2010
    Posts:
    314
    How does System.Numerics, which is available now, compare to Unity.Mathematics? Specifically if I just want to do SIMD vector and quaternion math, with the best possible speed, but I'm not using the ECS/Jobs stuff and thus not the burst compiler. I can't tell if the Unity version's main benefit is the ability to hit better IL in a burst environment, or if it's actually SIMD.
     
    Qbit86 likes this.
  13. SLGSimon

    SLGSimon

    Joined:
    Jul 23, 2019
    Posts:
    58
    The problem with using a unity-specific math library means you can't share it with any external code. So now with the new math library I have to convert vectors between my vector type and two other unity types...

    (Unity.Mathematics only has a reference to UnityEngine just for implicit conversion which is pretty annoying)
     
    Qbit86 and goncalo-vasconcelos like this.
  14. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,010
    Just add UnityEngine as a reference also. You can add pretty much any Unity dll in an external project. You can use most of the types like Vector3, etc.. You just can't call methods that forward to native implementations in the engine.
     
  15. SLGSimon

    SLGSimon

    Joined:
    Jul 23, 2019
    Posts:
    58
    Can't - and don't - want to be shipping Unity DLLs with a game server...
     
  16. x4000

    x4000

    Joined:
    Mar 17, 2010
    Posts:
    314
    There are no legal restrictions around this. Nonetheless, if you want to simply use SIMD, then I suggest that you import System.Numerics and use Vector4 from that. This is what I do, simply because I don't trust the performance of unity's custom implementation.

    If you wish to do Matrix math, that is something you somewhat have to re-implement, but the source for unity's matrix math is open source and can be converted to use with Vector4.

    Converting from Vector4 back to Vector3 is not something you would have to do on a server without the unity engine dlls, and from a serialization standpoint back to clients they are just getting a list of floats anyway. (Since you're not including unity dlls in your server there's already no way for you to use unity's serialization or Vector3 class, anyway).

    System.Numerics has proven to be very high-performance and reliable in the last year or two I've been using it. Given that these are also structs, you can keep this entirely on the stack and avoid any heap usage if you avoid the constructor and define it instead like this.

    Vector3 v;
    v.x = x;
    v.y = y;
    v.z = z;

    And then use the code from there. If you do this:

    Vector3 v = new Vector3 (x, y, z );

    Then you may incur some heap overhead. That's true with any struct, but YMMV. It's something we've had different test results from with differing versions of unity and mono and .net and different machines and OSes over the years.
     
  17. Qbit86

    Qbit86

    Joined:
    Sep 2, 2013
    Posts:
    439
    But System.Numerics have Matrix4x4 as well, don't they?
     
  18. x4000

    x4000

    Joined:
    Mar 17, 2010
    Posts:
    314
    It does, but to my recollection (been a few years) it doesn't have the same sort of methods for things like TRS() and other basic static constructors or object methods. So if you want to do unity-style translations, rotations, and scalings, you have to reimplement those methods.

    Further, there's a certain "handedness" to Matrix4x4 that Unity uses, and IIRC it's backwards from what a lot of the main public algorithms would show. I'm really stretching my memory now. But at any rate, it means that results will be slightly different if you don't use the same underlying way of filling the matrix before doing operations on them.

    Whether the results are substantially different enough to warrant notice, I have no idea. I've not tested that, largely because I haven't had a need to in my own work. And all of my recollections about Matrix4x4 in System.Numerics is from about two years ago, so big grains of salt. I do use the unity matrices and the system.numercs.vector4 on a daily basis, though.
     
  19. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,438
    The math is always the same no matter what. The handedness is the way you interpret the data.
     
  20. x4000

    x4000

    Joined:
    Mar 17, 2010
    Posts:
    314
    That's good to know!
     
unityunity