暫無描述
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.

BurstRuntime.cs 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System;
  2. using System.Diagnostics;
  3. #if !BURST_COMPILER_SHARED
  4. using Unity.Jobs.LowLevel.Unsafe;
  5. #endif
  6. namespace Unity.Burst
  7. {
  8. /// <summary>
  9. /// Provides helper intrinsics that can be used at runtime.
  10. /// </summary>
  11. #if BURST_COMPILER_SHARED
  12. internal static class BurstRuntimeInternal
  13. #else
  14. public static class BurstRuntime
  15. #endif
  16. {
  17. /// <summary>
  18. /// Gets a 32-bits hashcode from a type computed for the <see cref="System.Type.AssemblyQualifiedName"/>
  19. /// </summary>
  20. /// <typeparam name="T">The type to compute the hash from</typeparam>
  21. /// <returns>The 32-bit hashcode.</returns>
  22. public static int GetHashCode32<T>()
  23. {
  24. return HashCode32<T>.Value;
  25. }
  26. /// <summary>
  27. /// Gets a 32-bits hashcode from a type computed for the <see cref="System.Type.AssemblyQualifiedName"/>
  28. /// This method cannot be used from a burst job.
  29. /// </summary>
  30. /// <param name="type">The type to compute the hash from</param>
  31. /// <returns>The 32-bit hashcode.</returns>
  32. public static int GetHashCode32(Type type)
  33. {
  34. return HashStringWithFNV1A32(type.AssemblyQualifiedName);
  35. }
  36. /// <summary>
  37. /// Gets a 64-bits hashcode from a type computed for the <see cref="System.Type.AssemblyQualifiedName"/>
  38. /// </summary>
  39. /// <typeparam name="T">The type to compute the hash from</typeparam>
  40. /// <returns>The 64-bit hashcode.</returns>
  41. public static long GetHashCode64<T>()
  42. {
  43. return HashCode64<T>.Value;
  44. }
  45. /// <summary>
  46. /// Gets a 64-bits hashcode from a type computed for the <see cref="System.Type.AssemblyQualifiedName"/>.
  47. /// This method cannot be used from a burst job.
  48. /// </summary>
  49. /// <param name="type">Type to calculate a hash for</param>
  50. /// <returns>The 64-bit hashcode.</returns>
  51. public static long GetHashCode64(Type type)
  52. {
  53. return HashStringWithFNV1A64(type.AssemblyQualifiedName);
  54. }
  55. // method internal as it is used by the compiler directly
  56. internal static int HashStringWithFNV1A32(string text)
  57. {
  58. // Using http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-1a
  59. // with basis and prime:
  60. const uint offsetBasis = 2166136261;
  61. const uint prime = 16777619;
  62. uint result = offsetBasis;
  63. foreach (var c in text)
  64. {
  65. result = prime * (result ^ (byte)(c & 255));
  66. result = prime * (result ^ (byte)(c >> 8));
  67. }
  68. return (int)result;
  69. }
  70. // method internal as it is used by the compiler directly
  71. // WARNING: This **must** be kept in sync with the definition in ILPostProcessing.cs!
  72. internal static long HashStringWithFNV1A64(string text)
  73. {
  74. // Using http://www.isthe.com/chongo/tech/comp/fnv/index.html#FNV-1a
  75. // with basis and prime:
  76. const ulong offsetBasis = 14695981039346656037;
  77. const ulong prime = 1099511628211;
  78. ulong result = offsetBasis;
  79. foreach (var c in text)
  80. {
  81. result = prime * (result ^ (byte)(c & 255));
  82. result = prime * (result ^ (byte)(c >> 8));
  83. }
  84. return (long)result;
  85. }
  86. private struct HashCode32<T>
  87. {
  88. public static readonly int Value = HashStringWithFNV1A32(typeof(T).AssemblyQualifiedName);
  89. }
  90. private struct HashCode64<T>
  91. {
  92. public static readonly long Value = HashStringWithFNV1A64(typeof(T).AssemblyQualifiedName);
  93. }
  94. #if !BURST_COMPILER_SHARED
  95. /// <summary>
  96. /// Allows for loading additional Burst native libraries
  97. /// Important: Designed for Play mode / Desktop Standalone Players ONLY
  98. /// In Editor, any libraries that have been loaded will be unloaded on exit of playmode
  99. /// Only supported from 2021.1 and later. You can use BurstCompiler.IsLoadAdditionalLibrarySupported() to confirm it is available.
  100. /// </summary>
  101. /// <param name="pathToLibBurstGenerated">Absolute filesystem location of bursted library to load</param>
  102. /// <returns>true if the library was loaded successfully</returns>
  103. public static bool LoadAdditionalLibrary(string pathToLibBurstGenerated)
  104. {
  105. if (BurstCompiler.IsLoadAdditionalLibrarySupported())
  106. {
  107. return LoadAdditionalLibraryInternal(pathToLibBurstGenerated);
  108. }
  109. return false;
  110. }
  111. internal static bool LoadAdditionalLibraryInternal(string pathToLibBurstGenerated)
  112. {
  113. return (bool)typeof(Unity.Burst.LowLevel.BurstCompilerService).GetMethod("LoadBurstLibrary").Invoke(null, new object[] { pathToLibBurstGenerated });
  114. }
  115. #if UNITY_2022_1_OR_NEWER
  116. [Preserve]
  117. internal static unsafe void RuntimeLog(byte* message, int logType, byte* fileName, int lineNumber)
  118. {
  119. Unity.Burst.LowLevel.BurstCompilerService.RuntimeLog((byte*) 0, (Unity.Burst.LowLevel.BurstCompilerService.BurstLogType)logType, message, fileName, lineNumber);
  120. }
  121. #endif
  122. internal static void Initialize()
  123. {
  124. }
  125. // Prevent BurstCompilerService.Log from being stripped, introduce PreserveAttribute to avoid
  126. //requiring a unityengine using directive, il2cpp will see the attribute and know to not strip
  127. //the Log method and its BurstCompilerService.Log dependency
  128. internal class PreserveAttribute : System.Attribute {}
  129. [Preserve]
  130. internal static void PreventRequiredAttributeStrip()
  131. {
  132. new BurstDiscardAttribute();
  133. // We also need to retain [Condition("UNITY_ASSERTION")] attributes in order to compile
  134. // some assertion correctly (i.e. not compile them)
  135. new ConditionalAttribute("HEJSA");
  136. new JobProducerTypeAttribute(typeof(BurstRuntime));
  137. }
  138. [Preserve]
  139. internal static unsafe void Log(byte* message, int logType, byte* fileName, int lineNumber)
  140. {
  141. Unity.Burst.LowLevel.BurstCompilerService.Log((byte*) 0, (Unity.Burst.LowLevel.BurstCompilerService.BurstLogType)logType, message, (byte*) 0, lineNumber);
  142. }
  143. #endif // !BURST_COMPILER_SHARED
  144. /// <summary>
  145. /// Return a pointer to read-only memory consisting of the literal UTF-8 bytes of a string constant.
  146. /// </summary>
  147. /// <param name="str">A string which must a string literal</param>
  148. /// <param name="byteCount">Receives the number of UTF-8 encoded bytes the constant contains (excluding null terminator)</param>
  149. /// <returns>A pointer to constant data representing the UTF-8 encoded bytes of the string literal, terminated with a null terminator</returns>
  150. public unsafe static byte* GetUTF8LiteralPointer(string str, out int byteCount)
  151. {
  152. throw new NotImplementedException("This function only works from Burst");
  153. }
  154. }
  155. }