SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
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.

87 lines
4.2 KiB

4 years ago
  1. using Unity.Collections;
  2. using Unity.Collections.LowLevel.Unsafe;
  3. namespace UnityEngine.XR.ARSubsystems
  4. {
  5. /// <summary>
  6. /// Utilities for copying native arrays.
  7. /// </summary>
  8. public static class NativeCopyUtility
  9. {
  10. /// <summary>
  11. /// Creates a <c>NativeArray</c> from a pointer by first copying <paramref name="length"/>
  12. /// <paramref name="defaultT"/>s into the <c>NativeArray</c>, and then overwriting the
  13. /// data in the array with <paramref name="source"/>, assuming each element in <paramref name="source"/>
  14. /// is <paramref name="sourceElementSize"/> bytes.
  15. /// </summary>
  16. /// <remarks>
  17. /// This is useful for native interops with structs that may change over time. This allows
  18. /// new fields to be added to the C# struct without breaking data obtained from data calls.
  19. /// </remarks>
  20. /// <typeparam name="T">The type of struct to copy.</typeparam>
  21. /// <param name="defaultT">A default version of <typeparamref name="T"/>, which will be used to first fill the array
  22. /// before copying from <paramref name="source"/>.</param>
  23. /// <param name="source">A pointer to a contiguous block of data of size <paramref name="sourceElementSize"/> * <paramref name="length"/>.</param>
  24. /// <param name="sourceElementSize">The size of one element in <paramref name="source"/>.</param>
  25. /// <param name="length">The number of elements to copy.</param>
  26. /// <param name="allocator">The allocator to use when creating the <c>NativeArray</c>.</param>
  27. /// <returns>
  28. /// A new <c>NativeArray</c> populating with <paramref name="defaultT"/> and <paramref name="source"/>.
  29. /// The caller owns the memory.
  30. /// </returns>
  31. public static unsafe NativeArray<T> PtrToNativeArrayWithDefault<T>(
  32. T defaultT,
  33. void* source,
  34. int sourceElementSize,
  35. int length,
  36. Allocator allocator) where T : struct
  37. {
  38. var array = CreateArrayFilledWithValue(defaultT, length, allocator);
  39. // Then overwrite with the source data, which may have a different size
  40. UnsafeUtility.MemCpyStride(
  41. array.GetUnsafePtr(),
  42. UnsafeUtility.SizeOf<T>(),
  43. source,
  44. sourceElementSize,
  45. sourceElementSize, length);
  46. return array;
  47. }
  48. /// <summary>
  49. /// Fills <paramref name="array"/> with repeated copies of <paramref name="value"/>.
  50. /// </summary>
  51. /// <typeparam name="T">The type of the <c>NativeArray</c>. Must be a <c>struct</c>.</typeparam>
  52. /// <param name="array">The array to fill.</param>
  53. /// <param name="value">The value with which to fill the array.</param>
  54. public static unsafe void FillArrayWithValue<T>(NativeArray<T> array, T value) where T : struct
  55. {
  56. // Early out if array is zero, or iOS will crash in MemCpyReplicate.
  57. if (array.Length == 0)
  58. return;
  59. UnsafeUtility.MemCpyReplicate(
  60. array.GetUnsafePtr(),
  61. UnsafeUtility.AddressOf(ref value),
  62. UnsafeUtility.SizeOf<T>(),
  63. array.Length);
  64. }
  65. /// <summary>
  66. /// Creates a new array allocated with <paramref name="allocator"/> initialized with <paramref name="length"/>
  67. /// copies of <paramref name="value"/>.
  68. /// </summary>
  69. /// <typeparam name="T">The type of the <c>NativeArray</c> to create. Must be a <c>struct</c>.</typeparam>
  70. /// <param name="value">The value with which to fill the array.</param>
  71. /// <param name="length">The length of the array to create.</param>
  72. /// <param name="allocator">The allocator with which to create the <c>NativeArray</c>.</param>
  73. /// <returns>A new <c>NativeArray</c> initialized with copies of <paramref name="value"/>.</returns>
  74. public static unsafe NativeArray<T> CreateArrayFilledWithValue<T>(T value, int length, Allocator allocator) where T : struct
  75. {
  76. var array = new NativeArray<T>(length, allocator, NativeArrayOptions.UninitializedMemory);
  77. FillArrayWithValue(array, value);
  78. return array;
  79. }
  80. }
  81. }