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

Does Burst support FieldOffset?

Discussion in 'Burst' started by jdtec, Jul 10, 2019.

  1. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    I'm getting this error with Burst enabled on a job using ResizableArray64Byte, no problems with Burst disabled.

    Code (CSharp):
    1. Code/ProtoAS/Scripts/Box2d/Collision/b2CollideEdge.cs(878,13): error:
    2. Unexpected error while processing instruction `IL_0bcd: ldfld b2ContactFeature
    3. b2ContactID::cf args(IL_0bc8)`.
    4. Reason: The struct field b2ContactID b2ClipVertex::id's struct comes from an unaddressable source: IL_0bc3: call !0
    5. Unity.Collections.ResizableArray64Byte`1<b2ClipVertex>::get_Item(System.Int32) args(IL_0bbf, IL_0bc1)
    6. at b2CollideEdge.b2EPCollider.Collide(b2Manifold* $___struct_ret, ref
    7. b2CollideEdge.b2EPCollider collider, b2Manifold* manifold, b2EdgeShape* edgeA,
    8. b2Transform xfA, b2Shape shapeA, Unity.Collections.NativeArray`1<b2VertexElement>*
    9. edgeA_polygonB_verts, Unity.Collections.NativeArray`1<b2NormalElement>*
    10. edgeA_polygonB_normals, b2PolygonShape polygonB, b2Transform xfB, b2Shape shapeB,
    11. Unity.Entities.DynamicBuffer`1<b2VertexElement>* polygonB_verts,
    12. Unity.Entities.DynamicBuffer`1<b2NormalElement>*
     
  2. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    Replacing with NativeArrays hasn't fixed the issue. I get the same error about an unaddressable source. Will keep investigating.
     
  3. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    The issue lies with the type you are putting into it, without seeing what that is nothing anyone can do to help.
     
  4. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Although I'm guessing from the Box2d name that you are working with structs that somehow use native code, making parts of it as the error say's unaddressable.
     
  5. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    Ah ok, that makes sense as the type has some funky things going on.

    No native code. I've ported box2d to Unity DOTS.

    The struct is the b2ClipVertex. Thanks for letting me know it's to do with the type. I can probably work from that information.

    Code (CSharp):
    1. ResizableArray64Byte<b2ClipVertex> clipPoints1 = new ResizableArray64Byte<b2ClipVertex> ();
    2.  
    3. public struct b2ClipVertex
    4. {
    5.   public float2 v;
    6.   public b2ContactID id;
    7. };
    8.  
    9. [StructLayout(LayoutKind.Explicit)]
    10. public struct b2ContactID
    11. {
    12. [FieldOffset(0)]
    13.   public b2ContactFeature cf;
    14.  
    15. [FieldOffset(0)]
    16.   public uint key;          ///< Used to quickly compare contact ids.
    17. };
    18.  
    19. public struct b2ContactFeature
    20. {
    21. public const uint8 b2_nullFeature = uint8.MaxValue;
    22.  
    23.   public enum Type
    24.   {
    25.     e_vertex = 0,
    26.     e_face = 1
    27.   };
    28.  
    29.   public uint8 indexA;    ///< Feature index on shapeA
    30.   public uint8 indexB;    ///< Feature index on shapeB
    31.   public uint8 typeA;   ///< The feature type on shapeA
    32.   public uint8 typeB;   ///< The feature type on shapeB
    33. };
     
  6. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    You are specifying two types with different lengths at the same FieldOffset. Not surprised burst blew up on this. C# won't blow up but I'm sure it's not doing what you would expect, or what Box2D does.
     
  7. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    Using:
    Code (CSharp):
    1. int size1 = System.Runtime.InteropServices.Marshal.SizeOf (typeof(b2ContactFeature));
    2. int size2 = System.Runtime.InteropServices.Marshal.SizeOf (typeof(uint));
    Both are 4 bytes. Are you referring to something else when you say length other than size in bytes?

    The equivalent in C++ Box2d:

    Code (CSharp):
    1. union b2ContactID
    2. {
    3.     b2ContactFeature cf;
    4.     uint32 key;                    ///< Used to quickly compare contact ids.
    5. };
     
    Last edited: Jul 11, 2019
  8. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    I can reproduce with the following test code.

    Code (CSharp):
    1. [StructLayout(LayoutKind.Explicit)]
    2.   struct TestLayoutStruct
    3.   {
    4.     [FieldOffset(0)]
    5.     public int a;
    6.  
    7.     [FieldOffset(0)]
    8.     public int b;
    9.   }
    10.  
    11.   struct TestLayoutStruct2
    12.   {
    13.     public TestLayoutStruct c;
    14.   }
    15.  
    16.   [BurstCompile]
    17.   struct TestLayoutJob : IJob
    18.   {
    19.     public void Execute ()
    20.     {
    21.       NativeArray<TestLayoutStruct2> testArray = new NativeArray<TestLayoutStruct2> (2, Allocator.Temp);
    22.       testArray [0] = new TestLayoutStruct2 ();
    23.       testArray [1] = new TestLayoutStruct2 ();
    24.  
    25.       var r1 = testArray [0].c.a;
    26.       var r2 = testArray [0].c.b;
    27.     }
    28.   }
    Code (CSharp):
    1. error: Unexpected error while processing instruction `IL_003c: ldfld System.Int32 Sim.b2World/TestLayoutStruct::a args(IL_0037)`. Reason: The struct field Sim.b2World/TestLayoutStruct Sim.b2World/TestLayoutStruct2::c's struct comes from an unaddressable source: IL_0032: call !0 Unity.Collections.NativeArray`1<Sim.b2World/TestLayoutStruct2>::get_Item(System.Int32) args(IL_002f, IL_0031)
     
  9. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Ya you are right I was reading it wrong. I don't know what all the rules are for FieldOffset I've only ever used it in the context of a single struct. But I took your last example and used explicit and FieldOffset on the second struct, and that works. So it seems burst only supports very explicit mapping scenarios maybe.
     
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    We had a couple of issues with FieldOffset. I think they are all addressed in Burst 1.1
     
    jdtec likes this.