Search Unity

fitting RaycastCommand into a job chain

Discussion in 'Entity Component System' started by snacktime, Mar 5, 2018.

  1. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    So I have a job chain where I'm working with sparse data. But I'm having challenges figuring out how to fit raycasting into that.

    So my scenario I have a job that calculates some data for projectiles, then a dependent job moves the transforms based on that same data. The data is basically sparse as it's complex enough where without some smart partitioning I either end up allocating constantly or iterating over tons of items I don't need to work on. So I have a partitioning system where work is filled into partitions sequentially, so worst case there is one partition not fully assigned that I have to iterate over.

    The challenge is I need to create new NativeArray's here for raycasting, since I'm working with sparse data and the size changes every cycle. But I can't see a way to do that. I can only allocate a NativeArray in a job in the scope of Execute. So the option of allocating at the job scope and then disposing where I Complete won't work. It looks like I can pass a JobHandle to a job, so I could use that to kick off the raycasting. But that complains about being unsafe, so I'd rather find another route if possible.
     
  2. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    So the docs on this almost imply that RaycastCommand is meant to be used inside a job? I didn't try that just because it wasn't really clear.
     
  3. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Ok well as recent as B8 it appears it ignore dependencies anyways, so that makes this easy for now.
     
  4. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    So what I ended up doing is just caching NativeArray's by size, since I have partitions I'm guaranteed the max number of arrays is partition size.
     
  5. timjohansson

    timjohansson

    Unity Technologies

    Joined:
    Jul 13, 2016
    Posts:
    473
    You generally do need to create a NativeArray for the commands and schedule on the main thread, but you can fill in the commands from a job as long as your command generating job is a dependency for the raycasts.
    You do not actually need to process all entries in the array though. If you set an item in the NativeArray<RaycastCommand> to new RaycastCommand(); that raycast will be ignored. It will still have a single result which is never a hit.
     
  6. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Ah ok nice I can ditch my pre allocating a NativeArray for every unique size. Thanks for the info.
     
  7. jlalone

    jlalone

    Joined:
    Jan 18, 2018
    Posts:
    8
    Even if the generating job handle is passed into RaycastCommand.ScheduleBatched as a dependency, the RaycastCommand.ScheduleBatch call throws an exception.

    Code (csharp):
    1.  
    2.             PrepareRaycastJob prepareJob = new PrepareRaycastJob(commands, distance, layers, commandCount);
    3.             JobHandle prepareHandle = prepareJob.Schedule(transformAccess);
    4.  
    5.             jobHandle = RaycastCommand.ScheduleBatch(commands, hits, Mathf.Min(commandCount, 10), prepareHandle);
    6.  
    7.  
    8. InvalidOperationException: The previously scheduled job LegContactManager:PrepareRaycastJob writes to the NativeArray PrepareRaycastJob.commands. You must call JobHandle.Complete() on the job LegContactManager:PrepareRaycastJob, before you can write to the NativeArray safely.
    9.  
     
  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    We have a fix for this coming in Beta 10. Sorry for the inconvenience.
     
  9. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    That looks like a really useful error message. Great job people of Unity Technologies!
     
    Enrico-Monese and hippocoder like this.
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Thanks. We are proud of the error messages.

    In this particular case it should just work though, without giving any error message because the dependency is indeed passed in correctly. This will be fixed in beta 10.
     
    Enrico-Monese, hippocoder and Peter77 like this.
  11. Darkgaze

    Darkgaze

    Joined:
    Apr 3, 2017
    Posts:
    395
    Novack and richard_harrington like this.