Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question What do you use for sortkey in ParallelWriter Command Buffers in iJobChunks?

Discussion in 'Entity Component System' started by ReadyPlayGames, Dec 21, 2020.

  1. ReadyPlayGames

    ReadyPlayGames

    Joined:
    Jan 24, 2015
    Posts:
    49
    When using command buffers in IJobChunk structs, I don't know what to use for sortkey.

    I found this: https://docs.unity3d.com/Packages/c...ities.EntityCommandBuffer.ParallelWriter.html
    This says that sortkey is:

    A unique index for each set of commands added to the concurrent command buffer across all parallel jobs writing commands to this buffer. The entityInQueryIndex argument provided by Entities is an appropriate value to use for this parameter. You can calculate a similar index in an IJobChunk by adding the current entity index within a chunk to the Execute(ArchetypeChunk, Int32, Int32) method's firstEntityIndex argument.​

    However, on this page https://docs.unity3d.com/Packages/com.unity.entities@0.16/manual/chunk_iteration_job.html
    says:

    Note: If you use a concurrent entity command buffer, pass the chunkIndex argument as the sortKey parameter to the command buffer functions.​

    What are we supposed to use? Thank you!
     
  2. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    I'd guess the first one is correct. chunkIndex would basically be the same as nativeThreadIndex.
     
    Last edited: Dec 23, 2020
  3. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    normally I use firstEntityIndex+i where i is the index of the entity in the chunk.
    but in fact most of the time this sortKey does not matter. say you want to use ECB to add components or destroy entities. the order does not matter that much.
    but if the order matter. you probably want to define your own strategy.
     
    RecursiveEclipse likes this.
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,223
    The main thing is that the sortKey needs to be deterministic and unique to that thread, so both the chunkIndex or the entityIndex (chunkIndex + i) work in the current version of Entities. It would be nice if the documentation was more consistent though.

    This is incorrect. The chunkIndex is deterministicly ordered and only one thread will ever see any given index, so it is perfectly valid and safe. nativeThreadIndex is not any slower, but you lose determinism as which chunks get processed by which thread index is effectively random every time.
     
    RecursiveEclipse likes this.
  5. RecursiveEclipse

    RecursiveEclipse

    Joined:
    Sep 6, 2018
    Posts:
    298
    Thanks for the corrections, good to know. Must've mixed up the exact reason why.
     
    Last edited: Dec 23, 2020
  6. ReadyPlayGames

    ReadyPlayGames

    Joined:
    Jan 24, 2015
    Posts:
    49
    So the answer is chunkIndex.
     
  7. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    by "entityIndex (chunkIndex + i) " I Think you mean firstEntityIndex+i. chunkIndex + i will not be de deterministic. it's the index of chunk not entity in chunk.
     
    DreamingImLatios likes this.
  8. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,223
    Yes, that should say "entityIndex (firstEntityIndex + i)". Thanks for the correction. I typically just use chunkIndex directly for ECB and only use that combo for writing to arrays.