private static unsafe JobHandle ScheduleInternal <TJob, T>( this TJob jobData, EventSystemBase eventSystem, JobHandle dependsOn, bool isParallel) where TJob : struct, IJobEvent <T> where T : struct { dependsOn = eventSystem.GetEventReaders <T>(dependsOn, out var events); for (var i = 0; i < events.Count; i++) { var reader = events[i]; var fullData = new JobEventProducer <TJob, T> { Reader = reader, JobData = jobData, IsParallel = isParallel, }; #if UNITY_2020_2_OR_NEWER const ScheduleMode scheduleMode = ScheduleMode.Parallel; #else const ScheduleMode scheduleMode = ScheduleMode.Batched; #endif var scheduleParams = new JobsUtility.JobScheduleParameters( UnsafeUtility.AddressOf(ref fullData), isParallel ? JobEventProducer <TJob, T> .InitializeParallel() : JobEventProducer <TJob, T> .InitializeSingle(), dependsOn, scheduleMode); dependsOn = isParallel ? JobsUtility.ScheduleParallelFor(ref scheduleParams, reader.ForEachCount, 1) : JobsUtility.Schedule(ref scheduleParams); } eventSystem.AddJobHandleForConsumer <T>(dependsOn); return(dependsOn); }
public static void Execute( ref JobEventProducer <TJob, T> fullData, IntPtr additionalPtr, IntPtr bufferRangePatchData, ref JobRanges ranges, int jobIndex) { while (true) { int begin = 0; int end = fullData.Reader.ForEachCount; // If we are running the job in parallel, steal some work. if (fullData.IsParallel) { if (!JobsUtility.GetWorkStealingRange(ref ranges, jobIndex, out begin, out end)) { return; } } for (int i = begin; i < end; i++) { var count = fullData.Reader.BeginForEachIndex(i); for (var j = 0; j < count; j++) { var e = fullData.Reader.Read <T>(); fullData.JobData.Execute(e); } fullData.Reader.EndForEachIndex(); } if (!fullData.IsParallel) { break; } } }