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