Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.6KB

  1. #if UNITY_JOBS
  2. using System;
  3. using Svelto.ECS.SveltoOnDOTS;
  4. using Unity.Jobs;
  5. namespace Svelto.ECS
  6. {
  7. public static class UnityJobExtensions
  8. {
  9. public static JobHandle ScheduleDispose
  10. <T1>(this T1 disposable, JobHandle inputDeps) where T1 : struct, IDisposable
  11. {
  12. return new DisposeJob<T1>(disposable).Schedule(inputDeps);
  13. }
  14. public static JobHandle ScheduleDispose
  15. <T1, T2>(this T1 disposable1, T2 disposable2, JobHandle inputDeps)
  16. where T1 : struct, IDisposable where T2 : struct, IDisposable
  17. {
  18. return new DisposeJob<T1, T2>(disposable1, disposable2).Schedule(inputDeps);
  19. }
  20. public static JobHandle ScheduleParallel
  21. <JOB>(this JOB job, int iterations, JobHandle inputDeps) where JOB: struct, IJobParallelFor
  22. {
  23. if (iterations <= 0)
  24. return inputDeps;
  25. var innerloopBatchCount = ProcessorCount.BatchSize((uint) iterations);
  26. return job.Schedule((int)iterations, innerloopBatchCount, inputDeps);
  27. }
  28. public static JobHandle ScheduleParallel
  29. <JOB>(this JOB job, uint iterations, JobHandle inputDeps) where JOB: struct, IJobParallelFor
  30. {
  31. if (iterations == 0)
  32. return inputDeps;
  33. var innerloopBatchCount = ProcessorCount.BatchSize(iterations);
  34. return job.Schedule((int)iterations, innerloopBatchCount, inputDeps);
  35. }
  36. public static JobHandle ScheduleParallelAndCombine
  37. <JOB>(this JOB job, int iterations, JobHandle inputDeps, JobHandle combinedDeps) where JOB: struct, IJobParallelFor
  38. {
  39. if (iterations == 0)
  40. return inputDeps;
  41. var innerloopBatchCount = ProcessorCount.BatchSize((uint)iterations);
  42. var jobDeps = job.Schedule(iterations, innerloopBatchCount, inputDeps);
  43. return JobHandle.CombineDependencies(combinedDeps, jobDeps);
  44. }
  45. public static JobHandle ScheduleAndCombine
  46. <JOB>(this JOB job, JobHandle inputDeps, JobHandle combinedDeps) where JOB : struct, IJob
  47. {
  48. var jobDeps = job.Schedule(inputDeps);
  49. return JobHandle.CombineDependencies(combinedDeps, jobDeps);
  50. }
  51. public static JobHandle ScheduleAndCombine
  52. <JOB>(this JOB job, int arrayLength, JobHandle inputDeps, JobHandle combinedDeps) where JOB : struct, IJobFor
  53. {
  54. var jobDeps = job.Schedule(arrayLength, inputDeps);
  55. return JobHandle.CombineDependencies(combinedDeps, jobDeps);
  56. }
  57. }
  58. }
  59. #endif