I see allot of unity code using ijobchunk in the hybrid renderer package, and i’m having to reimplement some of the functionality relating to light probes. but i’m not sure if i have to use ijobchunk and what specific advantage it gives you if any?
are there any other useful job interfaces? only one i know of is ijob
There are many cases where you can make assumptions based on the layout of a full chunk, not having to do certain operations per entity in the chunk. An example is if you’re ever having to do “HasComponent” while in an Entities.ForEach, that operation could have done on the chunk level instead of for each entity in the chunk. There’s an upcoming Chunks.ForEach API that will allow for this in regular systems. It has been in development for a long while, but is still not yet released. I think it’s development has been halted in favour of other features.
Apart from that, as of right now they still have a restriction on code/systems that run in the Editor World can’t use CodeGen (Entities.ForEach or Job.WithCode). Rendering systems are required to preview SubScenes which is probably a main reason to why they’re implemented using Job structs.
Edit:
There’s also a restriction in how many components you can accept as parameters in Entities.ForEach. With IJobChunk, you can create jobs that accept more component types.
This goes for nested loops as well. There are two options for inner loops that relate to your question: EntityQuery and chunk iteration. If the entities you want to iterate through the inner loop are in different archetype chunks (basically, fragmented entities), then use EntityQuery. Otherwise, if those entities are mostly packed together due to being in the same archetype chunks, use chunk iteration. Here’s an illustration of the two scenarios:
As for other useful IJob types apart from IJob and IJobChunk, there are a few other useful types, but I haven’t personally used them that much:
IJobParallelFor / IJobParallelForBatch
You can use this to iterate over a NativeList or any other collection. The Batch variant is useful if you want to parallelize it.
IJobEntityBatch / IJobEntityBatchWithIndex
This is basically an IJobChunk, but where you can split one ArchetypeChunk up into parallelized sub-batches. Say that if you could fit 100 entities per ArchetypeChunk, but only want to iterate 10 entities per batch. This job allows you to do that, and spread out that work on separate threads.
An example where I used this was for pathfinding requests. Each pathfinding request was quite expensive, but a lot of requests fit in the same archetypechunk. The IJobChunk I previously used for it caused very long jobs, so I switched to IJobEntityBatch.
Then there are some that I haven’t ever used personally, so I can’t comment on their usefulness:
IJobParallelForDefer
IJobParallelForFilter
IJobNativeMultiHashMapVisitKeyMutableValue
IJobNativeMultiHashMapVisitKeyValue
Finally, there’s also an upcoming API called IJobEntity which is to replace the deprecated IJobForEach APIs. You can read more about IJobEntity here:
FYI Instead of these two jobs just use one IJobFor.
Extremely useful job type for scheduling parallel jobs with the unknown at schedule time collection length.
For example, you scheduling parallel job which gathers some elements into NativeHashMap by some condition (and you don’t know how many items will be in NHM eventually), and then want schedule second job, which will process your gathered in NHM keys in parallel in a batched way. With IJobFor and etc. you can’t do that, because you can’t schedule them without array length known at schedule time and you don’t want to complete the previous parallel job (which populating NHM) for getting gathered items length for next job. Thus you can add intermediate IJob which will just get keys array from NHM and AddRange to deferred list, which you used for IJobParallelForDefer scheduling which then automatically will split populated in IJob deferred list to worker threads by you batch count you decided at the schedule time. Same case if you just have one threaded IJob which populate list of items by some condition and then In IJobParallelForDefer you just process that list in parallel in batch way.