Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved DynamicComponentHandle Full Support

Discussion in 'DOTS Dev Blitz Day 2022 - Q&A' started by Jonas_DM_, Dec 8, 2022.

  1. Jonas_DM_

    Jonas_DM_

    Joined:
    Feb 28, 2019
    Posts:
    22
    Hi, I was wondering if there are plans to extend support for DynamicComponentHandle.
    At the moment you can't get the EnabledMask from it.
    It would also be nice if we could get an IComponentData equivalent to UnsafeUntypedBufferAccessor via the DynamicComponentHandle.

    I added this functionality locally in my project (extending the entities package via assembly references), but official implementations are always preferred ofcourse.

    Cheers!
     
  2. jivalenzuela

    jivalenzuela

    Unity Technologies

    Joined:
    Dec 4, 2019
    Posts:
    83
    Maybe I'm misunderstanding, why isn't
    GetEnableableBits
    suitable?
     
  3. Jonas_DM_

    Jonas_DM_

    Joined:
    Feb 28, 2019
    Posts:
    22
    It returns a copy of the bitmask, so you can't change the enabled flag.
    It's also not trivial to do something for when the component is disabled instead of enabled.
    Here's some code to maybe clarify it:

    Code (CSharp):
    1.  
    2. private struct ExampleJob : IJobChunk
    3. {
    4.     public EntityTypeHandle EntityTypeHandle;
    5.     public DynamicComponentTypeHandle DynamicComponentTypeHandle;
    6.          
    7.     public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
    8.     {
    9.         v128 bitmaskCopy = chunk.GetEnableableBits(ref DynamicComponentTypeHandle);
    10.  
    11.         // This use-case is supported via the v128
    12.         // 1. Do something for every entity that has the component enabled
    13.         var enumerator = new ChunkEntityEnumerator(true, bitmaskCopy, unfilteredChunkIndex);
    14.         while (enumerator.NextEntityIndex(out int entityIndex))
    15.         {
    16.             // Do something
    17.         }
    18.  
    19.         // Use cases that are not trivial with v128
    20.         // 2. Do something for every entity that has the component disabled
    21.  
    22.         // Use cases that are not possible with v128
    23.         // 3. Change the enabled state of the component (v128 is a copy of the bitmask)
    24.      }
    25. }
     
  4. jivalenzuela

    jivalenzuela

    Unity Technologies

    Joined:
    Dec 4, 2019
    Posts:
    83
    Am I wrong that
    GetEnabledMask
    isn't readonly as well?

    As far as operating on disabled components, you can complement the bits in a v128 with
    X86.Sse.xor_ps(foo, new v128(-1))
    although we can probably make that more slick.
     
  5. Jonas_DM_

    Jonas_DM_

    Joined:
    Feb 28, 2019
    Posts:
    22
    The EnabledMask is read-write, it holds a SafeBitRef to the first enabled bit of the chunk. So its reads & writes are checked via an AtomicSafetyHandle.
    I've used it to make changes to the enabled bitmask & it works. (please don't make it read-only ;))

    Thanks for pointing out how I can flip the bits in the v128, that's helpful!
     
  6. jivalenzuela

    jivalenzuela

    Unity Technologies

    Joined:
    Dec 4, 2019
    Posts:
    83
    I double checked and you are of course right. I've make a note about that inconsistency, thanks!
     
    Jonas_DM_ likes this.
  7. cort_of_unity

    cort_of_unity

    Unity Technologies

    Joined:
    Aug 15, 2018
    Posts:
    98
    We agree that adding the missing GetEnabledMask() method to DynamicComponentTypeHandle looks feasible. I'll add a task for that. We should add it for BufferTypeHandle while we're in the neighborhood.
     
    Jonas_DM_ likes this.