JobGraphProcessor.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.Linq;
  5. using Unity.Jobs;
  6. using Unity.Collections;
  7. // using Unity.Entities;
  8. namespace GraphProcessor
  9. {
  10. /// <summary>
  11. /// Graph processor
  12. /// </summary>
  13. public class JobGraphProcessor : BaseGraphProcessor
  14. {
  15. GraphScheduleList[] scheduleList;
  16. internal class GraphScheduleList
  17. {
  18. public BaseNode node;
  19. public BaseNode[] dependencies;
  20. public GraphScheduleList(BaseNode node)
  21. {
  22. this.node = node;
  23. }
  24. }
  25. /// <summary>
  26. /// Manage graph scheduling and processing
  27. /// </summary>
  28. /// <param name="graph">Graph to be processed</param>
  29. public JobGraphProcessor(BaseGraph graph) : base(graph) {}
  30. public override void UpdateComputeOrder()
  31. {
  32. scheduleList = graph.nodes.OrderBy(n => n.computeOrder).Select(n => {
  33. GraphScheduleList gsl = new GraphScheduleList(n);
  34. gsl.dependencies = n.GetInputNodes().ToArray();
  35. return gsl;
  36. }).ToArray();
  37. }
  38. /// <summary>
  39. /// Schedule the graph into the job system
  40. /// </summary>
  41. public override void Run()
  42. {
  43. int count = scheduleList.Length;
  44. var scheduledHandles = new Dictionary< BaseNode, JobHandle >();
  45. for (int i = 0; i < count; i++)
  46. {
  47. JobHandle dep = default(JobHandle);
  48. var schedule = scheduleList[i];
  49. int dependenciesCount = schedule.dependencies.Length;
  50. for (int j = 0; j < dependenciesCount; j++)
  51. dep = JobHandle.CombineDependencies(dep, scheduledHandles[schedule.dependencies[j]]);
  52. // TODO: call the onSchedule on the current node
  53. // JobHandle currentJob = schedule.node.OnSchedule(dep);
  54. // scheduledHandles[schedule.node] = currentJob;
  55. }
  56. JobHandle.ScheduleBatchedJobs();
  57. }
  58. }
  59. }