Last active
May 16, 2024 09:49
-
-
Save LizzyFox-code/7cf6411860d3b582c969eee6f6a492d4 to your computer and use it in GitHub Desktop.
Custom parallel thread job example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[JobProducerType(typeof(JobCustomParallelExtensions.JobCustomParallelStruct<>))] | |
public interface IJobCustomParallel | |
{ | |
void Execute(); | |
} | |
public static class JobCustomParallelExtensions | |
{ | |
public static void EarlyJobInit<T>() where T : struct, IJobCustomParallel | |
{ | |
JobCustomParallelStruct<T>.Initialize(); | |
} | |
private static IntPtr GetReflectionData<T>() where T : struct, IJobCustomParallel | |
{ | |
JobCustomParallelStruct<T>.Initialize(); | |
return JobCustomParallelStruct<T>.jobReflectionData.Data; | |
} | |
public static unsafe JobHandle Schedule<TJob>(this TJob jobData, JobHandle dependsOn = default) | |
where TJob : struct, IJobCustomParallel | |
{ | |
var fullData = new JobCustomParallelStruct<TJob> | |
{ | |
JobData = jobData | |
}; | |
var scheduleParams = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf(ref fullData), | |
GetReflectionData<TJob>(), dependsOn, ScheduleMode.Parallel); | |
return JobsUtility.ScheduleParallelFor(ref scheduleParams, 100, 10); | |
} | |
public static unsafe JobHandle ScheduleByRef<TJob>(this ref TJob jobData, JobHandle dependsOn = default) | |
where TJob : struct, IJobCustomParallel | |
{ | |
var fullData = new JobCustomParallelStruct<TJob> | |
{ | |
JobData = jobData | |
}; | |
var scheduleParams = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf(ref fullData), | |
GetReflectionData<TJob>(), dependsOn, ScheduleMode.Parallel); | |
return JobsUtility.ScheduleParallelFor(ref scheduleParams, 100, 10); | |
} | |
[StructLayout(LayoutKind.Sequential)] | |
internal struct JobCustomParallelStruct<TJob> | |
where TJob : struct, IJobCustomParallel | |
{ | |
public TJob JobData; | |
internal static readonly SharedStatic<IntPtr> jobReflectionData = | |
SharedStatic<IntPtr>.GetOrCreate<JobCustomParallelStruct<TJob>>(); | |
[BurstDiscard] | |
internal static void Initialize() | |
{ | |
if (jobReflectionData.Data == IntPtr.Zero) | |
{ | |
jobReflectionData.Data = JobsUtility.CreateJobReflectionData(typeof(JobCustomParallelStruct<TJob>), | |
typeof(TJob), (ExecuteJobFunction)Execute); | |
} | |
} | |
private delegate void ExecuteJobFunction(ref JobCustomParallelStruct<TJob> fullData, IntPtr additionalPtr, | |
IntPtr bufferRangePatchData, | |
ref JobRanges ranges, int jobIndex); | |
public static void Execute(ref JobCustomParallelStruct<TJob> fullData, IntPtr additionalPtr, IntPtr bufferRangePatchData, | |
ref JobRanges ranges, int jobIndex) | |
{ | |
while (true) | |
{ | |
if (!JobsUtility.GetWorkStealingRange(ref ranges, jobIndex, out var begin, out var end)) | |
return; | |
for (var i = begin; i < end; i++) | |
{ | |
fullData.JobData.Execute(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment