[Job System, Example] Starting with simple optimizations : Using a NativeArray<struct> ?

Hi everyone,

Compile a Job with the burst compiler

To start with simple optimizations i followed this small tutorial.
https://docs.unity3d.com/Packages/com.unity.burst@0.2/manual/index.html#standalone-player-support

This simple example in the docs use a native array to run a job over the NativeArray****. That works fine.

using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;

public class MyBurst2Behavior : MonoBehaviour
{
    void Start()
    {
        var input = new NativeArray<float>(10, Allocator.Persistent);
        var output = new NativeArray<float>(1, Allocator.Persistent);
        for (int i = 0; i < input.Length; i++) input[i] = 1.0f * i;

        var job = new MyJob
        {
            Input = input,
            Output = output
        };
        job.Schedule().Complete();

        Debug.Log("The result of the sum is: " + output[0]);
        input.Dispose();  output.Dispose();
    }

    [BurstCompile(CompileSynchronously = true)]
    private struct MyJob : IJob
    {
        [ReadOnly]
        public NativeArray<float> Input;

        [WriteOnly]
        public NativeArray<float> Output;

        public void Execute()
        {
            float result = 0.0f;
            for (int i = 0; i < Input.Length; i++)
            {
                result += Input[i];
            }
            Output[0] = result;
        }
    }
}

But how to use NativeArray****. in this case?

  • Struct types
  • Burst supports regular structs with any field with supported types.

This following example use a NativeArray**** instead of NativeArray****
```csharp
**using System;
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using UnityEngine;
using Unity.Entities;

public class Test_NativeArrayStruct : MonoBehaviour {

[Serializable]
public struct theStruct {
    public int int1;
}

public NativeArray<theStruct> structNative = new NativeArray<theStruct>();

private void Awake() {
    structNative = new NativeArray<theStruct>(10000, Allocator.Persistent);
}

private void Update() {
    var job = new MyJob {
        StructNative = structNative, // <------------ Fail ---------------
    };
    job.Schedule().Complete();
}

[BurstCompile(CompileSynchronously = true)]
private struct MyJob : IJob {

    public theStruct[] StructNative;
    public void Execute() {

        for (int i = 0; i < StructNative.Length; i++) {
            StructNative[i].int1 = 77;
        }
    }
}

private void OnDestroy() {
    structNative.Dispose();
}

}**
```

But it fail with the data conversion message:

StructNative = structNative,

Any ideas how this can be solved?

    ....
    private struct MyJob : IJob {
        public NativeArray<theStruct> StructNative;
        ....

Thanks I over read it. -)

Unfortunately it cause “fail access because its not a variable error”

for (int i = 0; i < StructNative.Length; i++) {
                    StructNative[I].int1 = 77; // <--------------
    }

yeah to edit component data you should do the following:

// get a copy of the data
var data = StructNative[i];
// do manipulations to the copy of the data
data.Int1 = 77;
// overwrite the existing data inside the StructNative array
StructNative[i] = data;
1 Like

Seems that works for the code but not for the compiler. Simply crash mono in this case… :open_mouth:

StructNative[i] = new theStruct { Int1 = 77 };
3 Likes

@mike_acton is correct to use a struct to encapsulate native arrays?

Right now i’m working on a Ear Clipping triangulator and I want to create something like this:

    public struct PolygonJobData : IDisposable
    {
        [ReadOnly]
        public NativeArray<float2> Vertices;
        [ReadOnly]
        public int NumContournPoints;
        [ReadOnly]
        public NativeArray<int> StartPointsHoles;
        [ReadOnly]
        public NativeArray<int> NumPointsPerHole;

        public PolygonJobData (Vector2[] contourn, Vector2[][]holes, Allocator allocator)
        {
            ...
        }

        public void Dispose()
        {
            ...
        }
    }

Hi, dousi96. Did you manage to create an NativeArray with your PolygonJobData structure? I have the similar structure, but I can’t create NativeArray, here is topic with my problem https://discussions.unity.com/t/812831 If you have any ideas, I will be very grateful