Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

FunctionPointerTests.cs 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using AOT;
  4. using NUnit.Framework;
  5. using Unity.Burst;
  6. using UnityEngine;
  7. using UnityEngine.TestTools;
  8. #if UNITY_2021_2_OR_NEWER
  9. using System.Runtime.CompilerServices;
  10. using Unity.Collections;
  11. using Unity.Collections.LowLevel.Unsafe;
  12. using Unity.Jobs;
  13. #endif
  14. [TestFixture, BurstCompile]
  15. public class FunctionPointerTests
  16. {
  17. [BurstCompile(CompileSynchronously = true)]
  18. private static T StaticFunctionNoArgsGenericReturnType<T>()
  19. {
  20. return default;
  21. }
  22. private delegate int DelegateNoArgsIntReturnType();
  23. [Test]
  24. public void TestCompileFunctionPointerNoArgsGenericReturnType()
  25. {
  26. Assert.Throws<InvalidOperationException>(
  27. () => BurstCompiler.CompileFunctionPointer<DelegateNoArgsIntReturnType>(StaticFunctionNoArgsGenericReturnType<int>),
  28. "The method `Int32 StaticFunctionNoArgsGenericReturnType[Int32]()` must be a non-generic method");
  29. }
  30. [BurstCompile(CompileSynchronously = true)]
  31. private static int StaticFunctionConcreteReturnType()
  32. {
  33. return default;
  34. }
  35. private delegate T DelegateGenericReturnType<T>();
  36. [Test]
  37. public void TestCompileFunctionPointerDelegateNoArgsGenericReturnType()
  38. {
  39. Assert.Throws<InvalidOperationException>(
  40. () => BurstCompiler.CompileFunctionPointer<DelegateGenericReturnType<int>>(StaticFunctionConcreteReturnType),
  41. "The delegate type `FunctionPointerTests+DelegateGenericReturnType`1[System.Int32]` must be a non-generic type");
  42. }
  43. private static class GenericClass<T>
  44. {
  45. public delegate int DelegateNoArgsIntReturnType();
  46. }
  47. [Test]
  48. public void TestCompileFunctionPointerDelegateNoArgsGenericDeclaringType()
  49. {
  50. Assert.Throws<InvalidOperationException>(
  51. () => BurstCompiler.CompileFunctionPointer<GenericClass<int>.DelegateNoArgsIntReturnType>(StaticFunctionConcreteReturnType),
  52. "The delegate type `FunctionPointerTests+GenericClass`1+DelegateNoArgsIntReturnType[System.Int32]` must be a non-generic type");
  53. }
  54. // Doesn't work with IL2CPP yet - waiting for Unity fix to land.
  55. #if false // UNITY_2021_2_OR_NEWER
  56. [UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl) })]
  57. [BurstCompile]
  58. private static int CSharpFunctionPointerCallback(int value) => value * 2;
  59. [BurstCompile(CompileSynchronously = true)]
  60. public unsafe struct StructWithCSharpFunctionPointer : IJob
  61. {
  62. [NativeDisableUnsafePtrRestriction]
  63. [ReadOnly]
  64. public IntPtr Callback;
  65. [ReadOnly]
  66. public NativeArray<int> Input;
  67. [WriteOnly]
  68. public NativeArray<int> Output;
  69. public void Execute()
  70. {
  71. delegate* unmanaged[Cdecl]<int, int> callback = (delegate* unmanaged[Cdecl]<int, int>)Callback;
  72. Output[0] = callback(Input[0]);
  73. }
  74. }
  75. [Test]
  76. public unsafe void CSharpFunctionPointerInsideJobStructTest()
  77. {
  78. using (var input = new NativeArray<int>(new int[1] { 40 }, Allocator.Persistent))
  79. using (var output = new NativeArray<int>(new int[1], Allocator.Persistent))
  80. {
  81. delegate* unmanaged[Cdecl]<int, int> callback = &CSharpFunctionPointerCallback;
  82. var job = new StructWithCSharpFunctionPointer
  83. {
  84. Callback = (IntPtr)callback,
  85. Input = input,
  86. Output = output
  87. };
  88. job.Run();
  89. Assert.AreEqual(40 * 2, output[0]);
  90. }
  91. }
  92. [Test]
  93. public unsafe void CSharpFunctionPointerInStaticMethodSignature()
  94. {
  95. var fp = BurstCompiler.CompileFunctionPointer<DelegateWithCSharpFunctionPointerParameter>(EntryPointWithCSharpFunctionPointerParameter);
  96. delegate* unmanaged[Cdecl]<int, int> callback = &CSharpFunctionPointerCallback;
  97. var result = fp.Invoke((IntPtr)callback);
  98. Assert.AreEqual(10, result);
  99. }
  100. [BurstCompile(CompileSynchronously = true)]
  101. private static unsafe int EntryPointWithCSharpFunctionPointerParameter(IntPtr callback)
  102. {
  103. delegate* unmanaged[Cdecl]<int, int> typedCallback = (delegate* unmanaged[Cdecl]<int, int>)callback;
  104. return typedCallback(5);
  105. }
  106. private unsafe delegate int DelegateWithCSharpFunctionPointerParameter(IntPtr callback);
  107. #endif
  108. [Test]
  109. public void TestDelegateWithCustomAttributeThatIsNotUnmanagedFunctionPointerAttribute()
  110. {
  111. var fp = BurstCompiler.CompileFunctionPointer<TestDelegateWithCustomAttributeThatIsNotUnmanagedFunctionPointerAttributeDelegate>(TestDelegateWithCustomAttributeThatIsNotUnmanagedFunctionPointerAttributeHelper);
  112. var result = fp.Invoke(42);
  113. Assert.AreEqual(43, result);
  114. }
  115. [BurstCompile(CompileSynchronously = true)]
  116. private static int TestDelegateWithCustomAttributeThatIsNotUnmanagedFunctionPointerAttributeHelper(int x) => x + 1;
  117. [MyCustomAttribute("Foo")]
  118. private delegate int TestDelegateWithCustomAttributeThatIsNotUnmanagedFunctionPointerAttributeDelegate(int x);
  119. private sealed class MyCustomAttributeAttribute : Attribute
  120. {
  121. public MyCustomAttributeAttribute(string param) { }
  122. }
  123. }
  124. #if UNITY_2021_2_OR_NEWER
  125. // UnmanagedCallersOnlyAttribute is new in .NET 5.0. This attribute is required
  126. // when you declare an unmanaged function pointer with an explicit calling convention.
  127. // Fortunately, Roslyn lets us declare the attribute class ourselves, and it will be used.
  128. // Users will need this same declaration in their own projects, in order to use
  129. // C# 9.0 function pointers.
  130. namespace System.Runtime.InteropServices
  131. {
  132. [AttributeUsage(System.AttributeTargets.Method, Inherited = false)]
  133. public sealed class UnmanagedCallersOnlyAttribute : Attribute
  134. {
  135. public Type[] CallConvs;
  136. }
  137. }
  138. #endif