-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
IJobParallelFor.cs
60 lines (50 loc) · 2.51 KB
/
IJobParallelFor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using Unity.Jobs.LowLevel.Unsafe;
using Unity.Collections.LowLevel.Unsafe;
namespace Unity.Jobs
{
[JobProducerType(typeof(IJobParallelForExtensions.ParallelForJobStruct < >))]
public interface IJobParallelFor
{
void Execute(int index);
}
public static class IJobParallelForExtensions
{
internal struct ParallelForJobStruct<T> where T : struct, IJobParallelFor
{
public static IntPtr jobReflectionData;
public static IntPtr Initialize()
{
if (jobReflectionData == IntPtr.Zero)
jobReflectionData = JobsUtility.CreateJobReflectionData(typeof(T), JobType.ParallelFor, (ExecuteJobFunction)Execute);
return jobReflectionData;
}
public delegate void ExecuteJobFunction(ref T data, IntPtr additionalPtr, IntPtr bufferRangePatchData, ref JobRanges ranges, int jobIndex);
public static unsafe void Execute(ref T jobData, IntPtr additionalPtr, IntPtr bufferRangePatchData, ref JobRanges ranges, int jobIndex)
{
while (true)
{
int begin;
int end;
if (!JobsUtility.GetWorkStealingRange(ref ranges, jobIndex, out begin, out end))
break;
for (var i = begin; i < end; ++i)
jobData.Execute(i);
}
}
}
unsafe public static JobHandle Schedule<T>(this T jobData, int arrayLength, int innerloopBatchCount, JobHandle dependsOn = new JobHandle()) where T : struct, IJobParallelFor
{
var scheduleParams = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf(ref jobData), ParallelForJobStruct<T>.Initialize(), dependsOn, ScheduleMode.Batched);
return JobsUtility.ScheduleParallelFor(ref scheduleParams, arrayLength, innerloopBatchCount);
}
unsafe public static void Run<T>(this T jobData, int arrayLength) where T : struct, IJobParallelFor
{
var scheduleParams = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf(ref jobData), ParallelForJobStruct<T>.Initialize(), new JobHandle(), ScheduleMode.Run);
JobsUtility.ScheduleParallelFor(ref scheduleParams, arrayLength, arrayLength);
}
}
}