Search Unity

Allocating/deallocating native containers INSIDE job's Execute?

Discussion in 'Entity Component System' started by Mr-Mechanical, Jan 2, 2019.

  1. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    I have an algorithm that needs to allocate and deallocate a native container within the Execute function, though this is currently not possible. Any help would be appreciated. Thank you.

    void Execute(int i) {
    NativeArray<int> exampleContainer = new NativeArray(1, Allocator.Temp);
    //do something interesting with container
    exampleContainer.Dispose();
    }
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    You can't so not sure what we can help you with. (Though they said months ago they would like to allow temp allocations.)

    Why can't you allocate it before you enter the job?
     
    Mr-Mechanical likes this.
  3. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    Good suggestion. I tried it earlier but unfortunately it doesn't allow a NativeArray of NativeQueues(the container I'm using). (I have to use one temporary container per entity).
     
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Why does the container have to be unique to entity?

    If you have N entities make a
    new NativeArray(N*length, Allocator.TempJob);


    And use a NativeSlice to break it up per entity in the job.
     
    Mr-Mechanical likes this.
  5. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    This would be elegant but I have to use nativequeues of undetermined length for each entity.
     
  6. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Oh missed the native queue part, my bad. Thought it said or
     
  7. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Thinking about it. I'd probably just use a dynamic buffer per entity. Annoying to keep it attached when it's not really needed, but i think it'd be the best solution if you can't refactor your logic.
     
    Mr-Mechanical likes this.
  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    This is supported in both C# & Burst jobs in 19.1
     
    Sirius64, jgp80, Abbrew and 5 others like this.
  9. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    Thanks a lot Joachim. I am a big fan of unity and I'm really happy with the crazy innovative tech.
     
    FROS7 likes this.