public static unsafe JobHandle ScheduleBatch <T>(this T jobData, int arrayLength, int minIndicesPerJobCount, JobHandle dependsOn = new JobHandle()) where T : struct, IJobParallelForBatch { #if UNITY_SINGLETHREADED_JOBS jobData.Execute(0, arrayLength); DoDeallocateOnJobCompletion(jobData); return(new JobHandle()); #elif UNITY_DOTSPLAYER var jobStruct = new ParallelForBatchJobStruct <T>() { JobData = jobData, Ranges = new JobRanges() { ArrayLength = arrayLength, IndicesPerPhase = JobsUtility.GetDefaultIndicesPerPhase(arrayLength) }, }; var jobDataPtr = UnsafeUtility.Malloc(UnsafeUtility.SizeOf <ParallelForBatchJobStruct <T> >(), UnsafeUtility.AlignOf <ParallelForBatchJobStruct <T> >(), Allocator.TempJob); UnsafeUtility.CopyStructureToPtr(ref jobStruct, jobDataPtr); var scheduleParams = new JobsUtility.JobScheduleParameters(jobDataPtr, ParallelForBatchJobStruct <T> .Initialize(), dependsOn, ScheduleMode.Batched); return(JobsUtility.ScheduleParallelFor(ref scheduleParams, arrayLength, minIndicesPerJobCount)); #else var scheduleParams = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf(ref jobData), ParallelForBatchJobStruct <T> .Initialize(), dependsOn, ScheduleMode.Batched); return(JobsUtility.ScheduleParallelFor(ref scheduleParams, arrayLength, minIndicesPerJobCount)); #endif }
public static unsafe void Execute(void *structPtr, int jobIndex) { var jobStruct = UnsafeUtility.AsRef <JobStructDefer <T> >(structPtr); var jobData = jobStruct.JobData; var ranges = new JobRanges(); var arrayLength = UnsafeUtility.AsRef <int>(jobStruct.ArrayLengthPtr.ToPointer()); ranges.ArrayLength = arrayLength; ranges.IndicesPerPhase = JobsUtility.GetDefaultIndicesPerPhase(arrayLength); while (true) { if (!JobsUtility.GetWorkStealingRange(ref ranges, jobIndex, out var begin, out var end)) { break; } #if ENABLE_UNITY_COLLECTIONS_CHECKS JobsUtility.PatchBufferMinMaxRanges(IntPtr.Zero, UnsafeUtility.AddressOf(ref jobData), begin, end - begin); #endif for (var i = begin; i < end; ++i) { jobData.Execute(i); } break; } }
public static unsafe JobHandle ScheduleBatch <T>(this T jobData, int arrayLength, int minIndicesPerJobCount, JobHandle dependsOn = new JobHandle()) where T : struct, IJobParallelForBatch { #if UNITY_AVOID_REFLECTION // Protect against garbage collection if (!ParallelForBatchJobStruct <T> .ExecuteHandle.IsAllocated) { ParallelForBatchJobStruct <T> .ExecuteDelegate = ParallelForBatchJobStruct <T> .Execute; ParallelForBatchJobStruct <T> .ExecuteHandle = GCHandle.Alloc(ParallelForBatchJobStruct <T> .ExecuteDelegate); ParallelForBatchJobStruct <T> .ExecuteFunctionPtr = Marshal.GetFunctionPointerForDelegate(ParallelForBatchJobStruct <T> .ExecuteDelegate); } // Protect against garbage collection if (!ParallelForBatchJobStruct <T> .CleanupHandle.IsAllocated) { ParallelForBatchJobStruct <T> .CleanupDelegate = ParallelForBatchJobStruct <T> .Cleanup; ParallelForBatchJobStruct <T> .CleanupHandle = GCHandle.Alloc(ParallelForBatchJobStruct <T> .CleanupDelegate); ParallelForBatchJobStruct <T> .CleanupFunctionPtr = Marshal.GetFunctionPointerForDelegate(ParallelForBatchJobStruct <T> .CleanupDelegate); } var jobFunctionPtr = ParallelForBatchJobStruct <T> .ExecuteFunctionPtr; var completionFuncPtr = ParallelForBatchJobStruct <T> .CleanupFunctionPtr; var jobStruct = new ParallelForBatchJobStruct <T>() { JobData = jobData, Ranges = new JobRanges() { ArrayLength = arrayLength, IndicesPerPhase = JobsUtility.GetDefaultIndicesPerPhase(arrayLength) }, }; var jobDataPtr = UnsafeUtility.Malloc(UnsafeUtility.SizeOf <ParallelForBatchJobStruct <T> >(), UnsafeUtility.AlignOf <ParallelForBatchJobStruct <T> >(), Allocator.TempJob); UnsafeUtility.CopyStructureToPtr(ref jobStruct, jobDataPtr); return(JobsUtility.ScheduleJobForEach(jobFunctionPtr, completionFuncPtr, new IntPtr(jobDataPtr), arrayLength, minIndicesPerJobCount, dependsOn)); #else var scheduleParams = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf(ref jobData), ParallelForBatchJobStruct <T> .Initialize(), dependsOn, ScheduleMode.Batched); return(JobsUtility.ScheduleParallelFor(ref scheduleParams, arrayLength, minIndicesPerJobCount)); #endif }