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

Replacement for deprecated IJobNativeMultiHashMapMergedSharedKeyIndices?

Discussion in 'Entity Component System' started by snorkysnark, May 3, 2020.

  1. snorkysnark

    snorkysnark

    Joined:
    Oct 31, 2016
    Posts:
    4
    tonytopper likes this.
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    From the changelog

    Deprecated IJobNativeMultiHashMapMergedSharedKeyIndices, JobNativeMultiHashMapUniqueHashExtensions, IJobNativeMultiHashMapVisitKeyValue, JobNativeMultiHashMapVisitKeyValue, IJobNativeMultiHashMapVisitKeyMutableValue, JobNativeMultiHashMapVisitKeyMutableValue, and introduced NativeHashMap.GetUnsafeBucketData and NativeMultiHashMap.GetUnsafeBucketData to obtain internals to implement deprecated functionality inside user code. If this functionality is used, the best is to copy deprecated code into user code.
     
  3. martinjonsson01

    martinjonsson01

    Joined:
    Jan 2, 2016
    Posts:
    7
    Where do I find this code, so that I may copy it into my own?
     
  4. bigcheese_

    bigcheese_

    Joined:
    Sep 6, 2011
    Posts:
    31
    It's this code below, this was part of a boid demo if I remember correctly

    Code (CSharp):
    1.  
    2.  [JobProducerType(typeof(JobNativeMultiHashMapUniqueHashExtensions.JobNativeMultiHashMapMergedSharedKeyIndicesProducer<>))]
    3.     public interface IJobNativeMultiHashMapMergedSharedKeyIndices
    4.     {
    5.         // The first time each key (=hash) is encountered, ExecuteFirst() is invoked with corresponding value (=index).
    6.         void ExecuteFirst(int index);
    7.  
    8.         // For each subsequent instance of the same key in the bucket, ExecuteNext() is invoked with the corresponding
    9.         // value (=index) for that key, as well as the value passed to ExecuteFirst() the first time this key
    10.         // was encountered (=firstIndex).
    11.         void ExecuteNext(int firstIndex, int index);
    12.     }
    13.  
    14.     public static class JobNativeMultiHashMapUniqueHashExtensions
    15.     {
    16.         internal struct JobNativeMultiHashMapMergedSharedKeyIndicesProducer<TJob>
    17.             where TJob : struct, IJobNativeMultiHashMapMergedSharedKeyIndices
    18.         {
    19.             [ReadOnly] public NativeMultiHashMap<int, int> HashMap;
    20.             internal TJob JobData;
    21.  
    22.             private static IntPtr s_JobReflectionData;
    23.  
    24.             internal static IntPtr Initialize()
    25.             {
    26.                 if (s_JobReflectionData == IntPtr.Zero)
    27.                 {
    28.                     s_JobReflectionData = JobsUtility.CreateJobReflectionData(typeof(JobNativeMultiHashMapMergedSharedKeyIndicesProducer<TJob>), typeof(TJob), JobType.ParallelFor, (ExecuteJobFunction)Execute);
    29.                 }
    30.  
    31.                 return s_JobReflectionData;
    32.             }
    33.  
    34.             delegate void ExecuteJobFunction(ref JobNativeMultiHashMapMergedSharedKeyIndicesProducer<TJob> jobProducer, IntPtr additionalPtr, IntPtr bufferRangePatchData, ref JobRanges ranges, int jobIndex);
    35.  
    36.             public static unsafe void Execute(ref JobNativeMultiHashMapMergedSharedKeyIndicesProducer<TJob> jobProducer, IntPtr additionalPtr, IntPtr bufferRangePatchData, ref JobRanges ranges, int jobIndex)
    37.             {
    38.                 while (true)
    39.                 {
    40.                     int begin;
    41.                     int end;
    42.  
    43.                     if (!JobsUtility.GetWorkStealingRange(ref ranges, jobIndex, out begin, out end))
    44.                     {
    45.                         return;
    46.                     }
    47.  
    48.                     var bucketData = jobProducer.HashMap.GetUnsafeBucketData();
    49.                     var buckets = (int*)bucketData.buckets;
    50.                     var nextPtrs = (int*)bucketData.next;
    51.                     var keys = bucketData.keys;
    52.                     var values = bucketData.values;
    53.  
    54.                     for (int i = begin; i < end; i++)
    55.                     {
    56.                         int entryIndex = buckets[i];
    57.  
    58.                         while (entryIndex != -1)
    59.                         {
    60.                             var key = UnsafeUtility.ReadArrayElement<int>(keys, entryIndex);
    61.                             var value = UnsafeUtility.ReadArrayElement<int>(values, entryIndex);
    62.                             int firstValue;
    63.  
    64.                             NativeMultiHashMapIterator<int> it;
    65.                             jobProducer.HashMap.TryGetFirstValue(key, out firstValue, out it);
    66.  
    67.                             // [macton] Didn't expect a usecase for this with multiple same values
    68.                             // (since it's intended use was for unique indices.)
    69.                             // https://forum.unity.com/threads/ijobnativemultihashmapmergedsharedkeyindices-unexpected-behavior.569107/#post-3788170
    70.                             if (entryIndex == it.EntryIndex)
    71.                             {
    72. #if ENABLE_UNITY_COLLECTIONS_CHECKS
    73.                                 JobsUtility.PatchBufferMinMaxRanges(bufferRangePatchData, UnsafeUtility.AddressOf(ref jobProducer), value, 1);
    74. #endif
    75.                                 jobProducer.JobData.ExecuteFirst(value);
    76.                             }
    77.                             else
    78.                             {
    79. #if ENABLE_UNITY_COLLECTIONS_CHECKS
    80.                                 var startIndex = math.min(firstValue, value);
    81.                                 var lastIndex = math.max(firstValue, value);
    82.                                 var rangeLength = (lastIndex - startIndex) + 1;
    83.  
    84.                                 JobsUtility.PatchBufferMinMaxRanges(bufferRangePatchData, UnsafeUtility.AddressOf(ref jobProducer), startIndex, rangeLength);
    85. #endif
    86.                                 jobProducer.JobData.ExecuteNext(firstValue, value);
    87.                             }
    88.  
    89.                             entryIndex = nextPtrs[entryIndex];
    90.                         }
    91.                     }
    92.                 }
    93.             }
    94.         }
    95.  
    96.         public static unsafe JobHandle Schedule<TJob>(this TJob jobData, NativeMultiHashMap<int, int> hashMap, int minIndicesPerJobCount, JobHandle dependsOn = new JobHandle())
    97.             where TJob : struct, IJobNativeMultiHashMapMergedSharedKeyIndices
    98.         {
    99.             var jobProducer = new JobNativeMultiHashMapMergedSharedKeyIndicesProducer<TJob>
    100.             {
    101.                 HashMap = hashMap,
    102.                 JobData = jobData
    103.             };
    104.  
    105.             var scheduleParams = new JobsUtility.JobScheduleParameters(
    106.                 UnsafeUtility.AddressOf(ref jobProducer)
    107.                 , JobNativeMultiHashMapMergedSharedKeyIndicesProducer<TJob>.Initialize()
    108.                 , dependsOn
    109.                 , ScheduleMode.Batched
    110.             );
    111.  
    112.             return JobsUtility.ScheduleParallelFor(ref scheduleParams, hashMap.GetUnsafeBucketData().bucketCapacityMask + 1, minIndicesPerJobCount);
    113.         }
    114.     }
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,754
    Any news on this subject, what is planned as a replacement, or if any?
     
    tonytopper likes this.
  6. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    They exposed the inner arrays so you can implement it yourself
     
    Antypodish likes this.