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

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:
    69
    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:
    69
    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:
    69
    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.