summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Utilities/IJobParallelForBatched.cs
blob: 761993b2f88bbe179f9e2d7bae1188bf0c54adac (plain)
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
61
62
63
64
65
// This file is only included because the Unity.Jobs package is currently experimental and it seems bad to rely on it.
// The Unity.Jobs version of this interface will be used when it is stable.
using System;
using Unity.Jobs.LowLevel.Unsafe;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;

namespace Pathfinding.Jobs {
	[JobProducerType(typeof(JobParallelForBatchedExtensions.ParallelForBatchJobStruct<>))]
	public interface IJobParallelForBatched {
		bool allowBoundsChecks { get; }
		void Execute(int startIndex, int count);
	}

	public static class JobParallelForBatchedExtensions {
		internal struct ParallelForBatchJobStruct<T> where T : struct, IJobParallelForBatched {
			static public IntPtr jobReflectionData;

			public static IntPtr Initialize () {
				if (jobReflectionData == IntPtr.Zero) {
#if UNITY_2020_2_OR_NEWER
					jobReflectionData = JobsUtility.CreateJobReflectionData(typeof(T), (ExecuteJobFunction)Execute, null, null);
#else
					jobReflectionData = JobsUtility.CreateJobReflectionData(typeof(T), JobType.ParallelFor, (ExecuteJobFunction)Execute);
#endif
				}
				return jobReflectionData;
			}

			public delegate void ExecuteJobFunction(ref T data, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, ref JobRanges ranges, int jobIndex);
			public unsafe static void Execute (ref T jobData, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, ref JobRanges ranges, int jobIndex) {
				while (true) {
					int begin;
					int end;
					if (!JobsUtility.GetWorkStealingRange(ref ranges, jobIndex, out begin, out end))
						return;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
					if (jobData.allowBoundsChecks) JobsUtility.PatchBufferMinMaxRanges(bufferRangePatchData, UnsafeUtility.AddressOf(ref jobData), begin, end - begin);
#endif

					jobData.Execute(begin, end - begin);
				}
			}
		}

		unsafe static public JobHandle ScheduleBatch<T>(this T jobData, int arrayLength, int minIndicesPerJobCount, JobHandle dependsOn = new JobHandle()) where T : struct, IJobParallelForBatched {
#if UNITY_2020_2_OR_NEWER
			// This was renamed in Unity 2020.2
			var scheduleMode = ScheduleMode.Parallel;
#else
			var scheduleMode = ScheduleMode.Batched;
#endif
			var scheduleParams = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf(ref jobData), ParallelForBatchJobStruct<T>.Initialize(), dependsOn, scheduleMode);

			return JobsUtility.ScheduleParallelFor(ref scheduleParams, arrayLength, minIndicesPerJobCount);
		}

		unsafe static public void RunBatch<T>(this T jobData, int arrayLength) where T : struct, IJobParallelForBatched {
			var scheduleParams = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf(ref jobData), ParallelForBatchJobStruct<T>.Initialize(), new JobHandle(), ScheduleMode.Run);

			JobsUtility.ScheduleParallelFor(ref scheduleParams, arrayLength, arrayLength);
		}
	}
}