Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

BurstJobTester.cs 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using System;
  2. using System.Diagnostics;
  3. using Unity.Burst;
  4. using Unity.Collections;
  5. using Unity.Jobs;
  6. using Unity.Mathematics;
  7. using UnityEngine;
  8. /// <summary>
  9. /// Shared class used for Unit tests.
  10. /// </summary>
  11. [BurstCompile] // attribute added just to check that static methods are getting compiled
  12. public class BurstJobTester2 : IDisposable
  13. {
  14. private NativeArray<float> _array;
  15. private NativeArray<float> _arrayAsyncJobDefault;
  16. private NativeArray<float> _arrayAsyncJobFast;
  17. public BurstJobTester2()
  18. {
  19. _array = new NativeArray<float>(10, Allocator.Persistent);
  20. _arrayAsyncJobDefault = new NativeArray<float>(10, Allocator.Persistent);
  21. _arrayAsyncJobFast = new NativeArray<float>(10, Allocator.Persistent);
  22. }
  23. public void Dispose()
  24. {
  25. _array.Dispose();
  26. _arrayAsyncJobDefault.Dispose();
  27. _arrayAsyncJobFast.Dispose();
  28. }
  29. public float Calculate()
  30. {
  31. // Schedule the job on each frame to make sure that it will be compiled async on the next frame
  32. _array[0] = 0.0f;
  33. // Launch synchronous job
  34. var job = new MyJob { Result = _array };
  35. job.Schedule().Complete();
  36. var rotation = job.Result[0];
  37. // Launch an async compilation
  38. var asyncJobNoOptim = new MyJobWithDefaultOptimizations() {Result = _arrayAsyncJobDefault};
  39. var asyncJobFastOptim = new MyJobWithFastOptimizations() {Result = _arrayAsyncJobFast};
  40. var asyncJobNoOptimHandle = asyncJobNoOptim.Schedule();
  41. var asyncJobFastOptimHandle = asyncJobFastOptim.Schedule();
  42. // Wait for async completion
  43. asyncJobNoOptimHandle.Complete();
  44. asyncJobFastOptimHandle.Complete();
  45. return rotation;
  46. }
  47. public float CheckFunctionPointer()
  48. {
  49. var functionPointer1 = BurstCompiler.CompileFunctionPointer<Add2NumbersDelegate>(Add2Numbers);
  50. var result = functionPointer1.Invoke(1.0f, 2.0f);
  51. var functionPointer2 = BurstCompiler.CompileFunctionPointer<Add2NumbersDelegate>(Add2NumbersThrows);
  52. return functionPointer2.Invoke(1.0f, 2.0f);
  53. }
  54. [BurstCompile(CompileSynchronously = true)] // attribute used for a static method
  55. public static float Add2Numbers(float a, float b)
  56. {
  57. DiscardFunction(ref a);
  58. DiscardFunction(ref b);
  59. return a + b;
  60. }
  61. [BurstCompile(CompileSynchronously = true)] // attribute used for a static method
  62. public static float Add2NumbersThrows(float a, float b)
  63. {
  64. DiscardFunction(ref a);
  65. DiscardFunction(ref b);
  66. if (a > 0) ThrowNewArgumentException();
  67. return a + b;
  68. }
  69. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  70. private static void ThrowNewArgumentException()
  71. {
  72. throw new ArgumentException("Invalid a must be < 0");
  73. }
  74. [BurstDiscard]
  75. private static void DiscardFunction(ref float x)
  76. {
  77. x = 0;
  78. }
  79. public delegate float Add2NumbersDelegate(float a, float b);
  80. [BurstCompile(CompileSynchronously = true)]
  81. public struct MyJob : IJob
  82. {
  83. [WriteOnly]
  84. public NativeArray<float> Result;
  85. public void Execute()
  86. {
  87. Result[0] = ChangeValue();
  88. EraseRotation();
  89. }
  90. // Use an indirection: Execute -> instance method -> static method
  91. // (to check caching manually, change "1.0f" in ChangeValue() and 2.0f in ChangeValueStatic())
  92. private float ChangeValue()
  93. {
  94. return 1.0f + ChangeValueStatic();
  95. }
  96. private static float ChangeValueStatic()
  97. {
  98. return 2.0f;
  99. }
  100. // Use BurstDiscard, if burst is not available, this method will get executed and it will make the cube static on the screen.
  101. [BurstDiscard]
  102. private void EraseRotation()
  103. {
  104. Result[0] = 0.0f;
  105. }
  106. // static method in a burst job, but we still want to compile separately
  107. [BurstCompile(FloatMode = FloatMode.Deterministic, CompileSynchronously = true)]
  108. public static float CheckFmaSlow(float a, float b, float c)
  109. {
  110. return a * b + c + math.sin(c);
  111. }
  112. // static method in a burst job, but we still want to compile separately
  113. // Used only to check that compilation is working for different burst compile options
  114. [BurstCompile(FloatPrecision.Low, FloatMode.Fast, CompileSynchronously = true)]
  115. public static float CheckFmaFast(float a, float b, float c)
  116. {
  117. return a * b + c + math.sin(c);
  118. }
  119. }
  120. [BurstCompile(CompileSynchronously = false)]
  121. public struct MyJobAsync : IJob
  122. {
  123. [WriteOnly]
  124. public NativeArray<float> Result;
  125. public void Execute()
  126. {
  127. Result[0] = ChangeValue();
  128. EraseRotation();
  129. }
  130. private float ChangeValue()
  131. {
  132. return 1.0f + ChangeValueStatic();
  133. }
  134. private static float ChangeValueStatic()
  135. {
  136. return 2.0f;
  137. }
  138. [BurstDiscard]
  139. private void EraseRotation()
  140. {
  141. Result[0] = 0.0f;
  142. }
  143. }
  144. [BurstCompile]
  145. public struct MyJobWithDefaultOptimizations : IJob
  146. {
  147. public NativeArray<float> Result;
  148. public void Execute()
  149. {
  150. Result[0] = math.cos(Result[0]);
  151. }
  152. }
  153. /// <summary>
  154. /// This Job is checking that we can allocate and dispose a NativeArray from a Burst Job
  155. /// </summary>
  156. [BurstCompile(CompileSynchronously = true)]
  157. public struct MyJobCreatingAndDisposingNativeArray : IJob
  158. {
  159. public int Length;
  160. public NativeArray<int> Result;
  161. public void Execute()
  162. {
  163. var array = new NativeArray<float>(Length, Allocator.Temp);
  164. for (int i = 0; i < array.Length; i++)
  165. {
  166. array[i] = i;
  167. }
  168. int result = array.Length;
  169. array.Dispose();
  170. DiscardFromManaged(ref result);
  171. Result[0] = result;
  172. }
  173. [BurstDiscard]
  174. public static void DiscardFromManaged(ref int result)
  175. {
  176. result = 0;
  177. }
  178. }
  179. // Used only to check that compilation is working for different burst compile options
  180. [BurstCompile(FloatPrecision.Low, FloatMode.Fast)]
  181. public struct MyJobWithFastOptimizations : IJob
  182. {
  183. public NativeArray<float> Result;
  184. public void Execute()
  185. {
  186. Result[0] = math.cos(Result[0]);
  187. }
  188. }
  189. }