No Description
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.

BurstCompiledSchedulingTests.cs 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using NUnit.Framework;
  3. using UnityEngine.Scripting;
  4. using Unity.Collections;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using Unity.Jobs;
  7. using Unity.Jobs.LowLevel.Unsafe;
  8. using Unity.Burst;
  9. using System.Diagnostics;
  10. namespace Unity.Jobs.Tests.ManagedJobs
  11. {
  12. internal class BurstScheduleTests : JobTestsFixtureBasic
  13. {
  14. [BurstDiscard]
  15. static public void TestBurstCompiled(ref bool falseIfNot)
  16. {
  17. falseIfNot = false;
  18. }
  19. [BurstCompile(CompileSynchronously = true)]
  20. static public bool IsBurstEnabled()
  21. {
  22. bool burstCompiled = true;
  23. TestBurstCompiled(ref burstCompiled);
  24. return burstCompiled;
  25. }
  26. [BurstCompile(CompileSynchronously = true)]
  27. struct SimpleIJobParallelForDefer : IJobParallelForDefer
  28. {
  29. public NativeArray<int> executed;
  30. public void Execute(int index)
  31. {
  32. executed[0] = 1;
  33. }
  34. [BurstCompile(CompileSynchronously = true)]
  35. public static int TestBurstScheduleJob(JobRunType runType, ref RewindableAllocator allocator)
  36. {
  37. bool burstCompiled = true;
  38. TestBurstCompiled(ref burstCompiled);
  39. var dummyList = new NativeList<int>(Allocator.Temp);
  40. dummyList.Add(5);
  41. var job = new SimpleIJobParallelForDefer() { executed = new NativeArray<int>(1, allocator.ToAllocator) };
  42. switch (runType)
  43. {
  44. case JobRunType.Schedule: job.Schedule(dummyList, 1).Complete(); break;
  45. case JobRunType.ScheduleByRef: job.ScheduleByRef(dummyList, 1).Complete(); break;
  46. }
  47. dummyList.Dispose();
  48. int ret = (burstCompiled ? 2 : 0) + job.executed[0];
  49. job.executed.Dispose();
  50. return ret;
  51. }
  52. }
  53. [TestCase(JobRunType.Schedule)]
  54. [TestCase(JobRunType.ScheduleByRef)]
  55. public unsafe void IJobParallelForDefer_Jobs_FromBurst(JobRunType runType)
  56. {
  57. if (!IsBurstEnabled())
  58. return;
  59. int ret = SimpleIJobParallelForDefer.TestBurstScheduleJob(runType, ref RwdAllocator);
  60. Assert.IsTrue((ret & 2) != 0, "Job schedule site not burst compiled");
  61. Assert.IsTrue((ret & 1) != 0, "Job with burst compiled schedule site didn't execute");
  62. }
  63. [BurstCompile(CompileSynchronously = true)]
  64. struct SimpleIJobParallelForBatch : IJobParallelForBatch
  65. {
  66. public NativeArray<int> executed;
  67. public void Execute(int startIndex, int count)
  68. {
  69. executed[0] = 1;
  70. }
  71. [BurstCompile(CompileSynchronously = true)]
  72. public static int TestBurstScheduleJob(JobRunType runType, ref RewindableAllocator allocator)
  73. {
  74. bool burstCompiled = true;
  75. TestBurstCompiled(ref burstCompiled);
  76. var job = new SimpleIJobParallelForBatch() { executed = new NativeArray<int>(1, allocator.ToAllocator) };
  77. switch (runType)
  78. {
  79. case JobRunType.Schedule: job.ScheduleBatch(1, 1).Complete(); break;
  80. case JobRunType.ScheduleByRef: job.ScheduleBatchByRef(1, 1).Complete(); break;
  81. case JobRunType.Run: job.RunBatch(1); break;
  82. case JobRunType.RunByRef: job.RunBatchByRef(1); break;
  83. }
  84. int ret = (burstCompiled ? 2 : 0) + job.executed[0];
  85. job.executed.Dispose();
  86. return ret;
  87. }
  88. }
  89. [TestCase(JobRunType.Schedule)]
  90. [TestCase(JobRunType.ScheduleByRef)]
  91. [TestCase(JobRunType.Run)]
  92. [TestCase(JobRunType.RunByRef)]
  93. public unsafe void IJobParallelForBatch_Jobs_FromBurst(JobRunType runType)
  94. {
  95. if (!IsBurstEnabled())
  96. return;
  97. int ret = SimpleIJobParallelForBatch.TestBurstScheduleJob(runType, ref RwdAllocator);
  98. Assert.IsTrue((ret & 2) != 0, "Job schedule site not burst compiled");
  99. Assert.IsTrue((ret & 1) != 0, "Job with burst compiled schedule site didn't execute");
  100. }
  101. [BurstCompile(CompileSynchronously = true)]
  102. struct SimpleIJobFilter : IJobFilter
  103. {
  104. public NativeArray<int> executed;
  105. public bool Execute(int index)
  106. {
  107. executed[0] = 1;
  108. return false;
  109. }
  110. [BurstCompile(CompileSynchronously = true)]
  111. public static int TestBurstScheduleJob(JobRunType runType, ref RewindableAllocator allocator)
  112. {
  113. bool burstCompiled = true;
  114. TestBurstCompiled(ref burstCompiled);
  115. var dummyList = new NativeList<int>(Allocator.Temp);
  116. dummyList.Add(5);
  117. var job = new SimpleIJobFilter() { executed = new NativeArray<int>(1, allocator.ToAllocator) };
  118. switch (runType)
  119. {
  120. case JobRunType.Schedule:
  121. job.ScheduleFilter(dummyList).Complete();
  122. job.ScheduleAppend(dummyList, 1).Complete();
  123. break;
  124. case JobRunType.ScheduleByRef:
  125. job.ScheduleFilterByRef(dummyList).Complete();
  126. job.ScheduleAppendByRef(dummyList, 1).Complete();
  127. break;
  128. case JobRunType.Run:
  129. job.RunFilter(dummyList);
  130. job.RunAppend(dummyList, 1);
  131. break;
  132. case JobRunType.RunByRef:
  133. job.RunFilterByRef(dummyList);
  134. job.RunAppendByRef(dummyList, 1);
  135. break;
  136. }
  137. dummyList.Dispose();
  138. int ret = (burstCompiled ? 2 : 0) + job.executed[0];
  139. job.executed.Dispose();
  140. return ret;
  141. }
  142. }
  143. [TestCase(JobRunType.Schedule)]
  144. [TestCase(JobRunType.ScheduleByRef)]
  145. [TestCase(JobRunType.Run)]
  146. [TestCase(JobRunType.RunByRef)]
  147. public unsafe void IJobFilter_Jobs_FromBurst(JobRunType runType)
  148. {
  149. if (!IsBurstEnabled())
  150. return;
  151. int ret = SimpleIJobFilter.TestBurstScheduleJob(runType, ref RwdAllocator);
  152. Assert.IsTrue((ret & 2) != 0, "Job schedule site not burst compiled");
  153. Assert.IsTrue((ret & 1) != 0, "Job with burst compiled schedule site didn't execute");
  154. }
  155. }
  156. }