Skip to content

Instantly share code, notes, and snippets.

@LizzyFox-code
Last active May 16, 2024 09:49
Show Gist options
  • Save LizzyFox-code/7cf6411860d3b582c969eee6f6a492d4 to your computer and use it in GitHub Desktop.
Save LizzyFox-code/7cf6411860d3b582c969eee6f6a492d4 to your computer and use it in GitHub Desktop.
Custom parallel thread job example
[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