Search Unity

Writable ComponentDataFromEntity with [NativeDisableParallelForRestriction]

Discussion in 'Entity Component System' started by JooleanLogic, Jan 14, 2019.

  1. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    If I use the following in an IJobProcessComponentData
    Code (CSharp):
    1. [NativeDisableParallelForRestriction]
    2. public ComponentDataFromEntity<TestComponent> testComponents;
    am I telling Unity that I know TestComponent is read/write safe within the context of this one job, or within all the jobs in my game?
    I.e. Is it up to me to make sure, via input dependencies, that no other jobs that reference TestComponent will run at the same time as this one?
    Or is Unity aware of that CDFE in my struct and because there's no [ReadOnly] on it, will ensure that no other TestComponent jobs run at the same time for me?
    I hope I've worded that not too confusingly.
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Just that specific job.

    You only need to ensure safety on this specific job. Unity depedency doesn't actually care about how the job is setup, just how you are getting the ComponentDataFromEntity

    A quick example, if you have this in your job

    Code (CSharp):
    1. [ReadOnly]
    2. public ComponentDataFromEntity<TestComponent> testComponents;
    and create the job with

    Code (CSharp):
    1. {
    2.     testComponents = this.GetComponentDataFromEntity()
    3. }
    GetComponentDataFromEntity is going to be considered read/write for system dependencies regardless that the job is marked as readonly (burst will still ensure read only safety within the job.)

    You'd need to mark the ComponentDataFromEntity as read only with the optional bool param

    Code (CSharp):
    1. {
    2.     testComponents = this.GetComponentDataFromEntity(true)
    3. }
     
  3. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Omg thankyou tertle! This was an epiphany!

    I'd been so confused how the whole jobs/system/dependency/scheduling mechanism worked but now I can finally start deciphering it. It's like the sun just came out. :)