Search Unity

Reasons for float2, float3, etc. ?

Discussion in 'Data Oriented Technology Stack' started by fholm, Mar 23, 2018.

  1. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,033
    I had a quick question, looking at the new APIs that come with the ECS there's now a group of numeric struct types like float2, float3, quaternion (lower cased), etc.

    What's the reason for these existing instead of the regular Vector2, Vector3, Quaternion, etc. ? The APIs the new types provide look similar to the old ones, with the addition of swizzle properties, etc.
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,649
    I guess it's not final yet. Seeking answers as well. Ideally I would like to be able to use Unity.Math or Unity.Mathematics and the api remains familiar.

    However what is familiar? This looks C# -> compute friendly as an API. I guess I never asked this question.
     
  3. Krajca

    Krajca

    Joined:
    May 6, 2014
    Posts:
    93
    They are all lower cases because of being struct instead of class for jobs purpose. I think there is a video of Joachim on YT that explains this question.
     
  4. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,033
    The regular Vector2, Vector3, etc. are also structs. What I'm asking is why the new types exist and why the old ones weren't used?

    I am sure there is a reason, I just have not found it.
     
  5. Krajca

    Krajca

    Joined:
    May 6, 2014
    Posts:
    93
    I think here's an explanation:
     
    el_boogie and mibo90 like this.
  6. Soaryn

    Soaryn

    Joined:
    Apr 17, 2015
    Posts:
    210
    Primarily I believe it is for the Burst compiler as they are more designed around the speed ups it can provide.
     
  7. RootCauseProductions

    RootCauseProductions

    Joined:
    Feb 1, 2018
    Posts:
    31
    The reason is speed. The Unity.Mathematics structures are exactly what the graphic API want for input. A NativeArray of them are ready to go without extra processing. The Vector3 has some extra fields so you have to copy them into the right format first, then send them to the graphics API.
     
    Krajca and hippocoder like this.
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    4,286
    Vector3 doesn't have any extra fields. They're both 12 byte structs with exactly an x, y and z field.

    Code (csharp):
    1. void Start() {
    2.     unsafe {
    3.         Debug.Log(sizeof(Vector3)); //12
    4.         Debug.Log(sizeof(float3)); //12
    5.     }
    6. }
    So I'm pretty sure that if you cast them to a byte-pointer, they'll have the exact same bytes in the same order.

    That being said, it is for speed. Looking at float3 in the decompiler, the difference seems to be that everything done in float3 is unsafe. As an example, the indexer for Vector3 just goes "if the index is 1, return y" and so on. The indexer for float3 does some pretty gnarly pointer juggling.

    So float3 is the same struct layout, but with a lot faster methods. I don't know why they didn't just go and make Vector3 fast, but unsafe. Maybe that breaks backwards compatability somewhere?
     
  9. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,311
    Also now, the naming fits with what you use in shaders. You send in a float4 and get a float4 in the shader.
     
    FlightOfOne and Krajca like this.
  10. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    25,649
    Yeah thought so.
     
  11. pvloon

    pvloon

    Joined:
    Oct 5, 2011
    Posts:
    586
    Yeah, while this new stuff does look great, I reaaally wonder why VectorX could not be made to work like that. There'll be a constant stream "hey please add float3 overloads" requests for a long time :/

    What is the long term plan? Will Vector3 be deprecated or are we supposed to use both?
     
  12. RootCauseProductions

    RootCauseProductions

    Joined:
    Feb 1, 2018
    Posts:
    31
    I was thinking there was a 4th field, but it is actually a const, so you are right, they are both the same size.
    Changing something from being safe code to unsafe code is sure to cause problems somewhere unless you want to do a full audit of all code that ever used Vector3. :eek:
     
  13. RootCauseProductions

    RootCauseProductions

    Joined:
    Feb 1, 2018
    Posts:
    31
    Vector3 if you are using GameObjects and float3 if you are using ECS/Jobs. Both if you use both. It is more painful right now since it is not complete. For example, there is no way to create meshes from a Job without converting to Vector3 (and others) first.
     
  14. pvloon

    pvloon

    Joined:
    Oct 5, 2011
    Posts:
    586
    Using both sounds horrible. Are we supposed to write overloads for all of our math utils / other functions? Or pay the cost of implicit conversions all the time?

    float3 being unsafe does require an audit, but just of that little unsafe code, which I'm sure unity has gone over. Also worst case they could just conditionally compiled the unsafe parts if unsafe is enabled
     
  15. RootCauseProductions

    RootCauseProductions

    Joined:
    Feb 1, 2018
    Posts:
    31
    This is an early preview so it is far from feature complete. The conversions are temporary and we are just waiting on the new APIs which are being worked on now.
     
    FROS7 likes this.
  16. pvloon

    pvloon

    Joined:
    Oct 5, 2011
    Posts:
    586
    Sure but it's not just unity's API (which will be a large enough task as is), it's also all user code, asset store code, etc.

    There are obvious reasons to have MonoBehviours and ECS at the same time - but so far just can't find the reason why we needed Vector3 and float3 at the same time.
     
  17. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,722
    The reason for float1234 etc was to establish a math library with a more consistent & simpler API. One that is also better fitting into a model where the compiled code should be automatically vectorized.

    It's very much work in progress.
     
    FlightOfOne, el_boogie and Krajca like this.
  18. pavelkouril

    pavelkouril

    Joined:
    Jul 22, 2016
    Posts:
    125
    While I do not mind adding an extra types for the job system, what I do not understand is the naming; didn't you (or someone else) from Unity sayyou will release all new APIs using the Microsoft C# naming/coding standards?
     
    RaL likes this.
  19. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    4,722
    Yes but this is math. We look at these types as builtins.
     
    pavelkouril likes this.
  20. RaL

    RaL

    Joined:
    Nov 29, 2013
    Posts:
    27
    Yeah... I was surprised by some of the names too. Things like math.lengthSquared() are going to trigger a lot of people's OCDs for sure. Would it hurt to just name it Math.LengthSquared()? :)
     
  21. XaneFeather

    XaneFeather

    Joined:
    Sep 4, 2013
    Posts:
    88
    I surely triggered mine, haha.
     
    illinar and RaL like this.