Search Unity

Using a constant Array in Burst.

Discussion in 'Burst' started by jackmememe, Jul 23, 2019.

  1. jackmememe

    jackmememe

    Joined:
    Jan 21, 2016
    Posts:
    138
    So i am in having a little of a problem with burst that i still didn't find a solution.
    This is a simplified version of my current problem, the code have more stuff that is unrelated to the problem:

    In my burst i am using a NativeArray<MyStruct>:
    Code (CSharp):
    1. public struct MyStruct{
    2.         public enum Types{
    3.             UseMatrix1, UseMatrix2, UseMatrix3
    4.         }
    5.  
    6.         public Types myMatrix;
    7.     }


    Inside the Job(Burst).Execute() there is a switch(myMatrix), in all cases it will use a array of length = 9. Here are my arrays:
    Code (CSharp):
    1. public static readonly float[] Matrix1= {
    2.             -1, -1, -1,
    3.              -1, 9, -1,
    4.              -1, -1, -1 };
    5.  
    6.         public static readonly float[] Matrix2= {
    7.             -1, -1, -1,
    8.              -1, 8, -1,
    9.              -1, -1, -1 };
    10.  
    11.         public static readonly float[] Matrix3= {
    12.             .2f, .25f, .2f,
    13.              .25f, 0, .25f,
    14.              .2f, .125f, .2f};
    If i call the matrix inside the burst, it works! But it feel wrong since i am using static variables.

    If inside of MyStruct is used a NativeArray<float> for the matrix, MyStruct becomes non blittable.
    And if set the array as fixed, i will need to set the struct as unsafe, so I prefer to not do this.



    The only other solution i could think of is creating a struct like:
    Code (CSharp):
    1. float a1, a2, a3;
    2. float b1, b2, b3;
    3. float c1, c2, c3;
    And creating functions to get the index, but this still look like a hack. Just wondering if I am missing something obvious for this simple problem?
     
  2. M_R

    M_R

    Joined:
    Apr 15, 2015
    Posts:
    559
    use float3x3 from
    Unity.Mathematics
    ?
     
    Ghat-Smith likes this.
  3. jackmememe

    jackmememe

    Joined:
    Jan 21, 2016
    Posts:
    138
    Well, i feel stupid now. Thanks M_R. It was such a simple solution.
     
    Shinyclef likes this.
  4. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
    I know this is old, but is there a way to do this for arrays of a compile-time constant length? For example, I'd like to precompute some values like this...

    Code (CSharp):
    1. const int TESSELATION = 25;
    2. private static readonly float2[] arcCosSin = computeArcCosSin();
    3. private static float2[] computeArcCosSin()
    4. {
    5.     float2[] result = new float2[TESSELATION];
    6.     for(int i = 0; i < TESSELATION; ++i)
    7.     {
    8.         math.sincos(i * 1f / (TESSELATION - 1) * math.PI/2, out float s, out float c);
    9.         result[i] = new float2(c, s);
    10.     }
    11.     return result;
    12. }
    Then I can change the
    TESSELATION
    and the array will update. But the whole array is essentially a compile-time constant.

    (I guess I can use NativeArray with Allocator.Persistent and then make the caller know about this implementation detail, or I hardcode the values. Neither one is acceptable, IMO, especially if the use case is more advanced than just this).
     
    Last edited: Jan 8, 2023
  5. Unifikation

    Unifikation

    Joined:
    Jan 4, 2023
    Posts:
    1,086
    Is something like this helpful?
    https://docs.unity.cn/Packages/com....Unity.Collections.AllocatorManager.Range.html

    I hope so, as you've been a HUGE help with your sermon on shader variants! Thank you!!!
     
    Last edited: Jan 10, 2023
    burningmime likes this.
  6. burningmime

    burningmime

    Joined:
    Jan 25, 2014
    Posts:
    845
  7. Unifikation

    Unifikation

    Joined:
    Jan 4, 2023
    Posts:
    1,086
    I suspect they've created Blocks as a collection type that's pointer friendly for animation curves to be easily LUT'd... if/when they get around to making a DOTS animation system ;)