forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeSlice.cs
More file actions
298 lines (237 loc) · 8.47 KB
/
Copy pathNativeSlice.cs
File metadata and controls
298 lines (237 loc) · 8.47 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine.Internal;
namespace Unity.Collections
{
public static class NativeSliceExtensions
{
public static NativeSlice<T> Slice<T>(this NativeArray<T> thisArray) where T : struct
{
return new NativeSlice<T>(thisArray);
}
public static NativeSlice<T> Slice<T>(this NativeArray<T> thisArray, int start) where T : struct
{
return new NativeSlice<T>(thisArray, start);
}
public static NativeSlice<T> Slice<T>(this NativeArray<T> thisArray, int start, int length) where T : struct
{
return new NativeSlice<T>(thisArray, start, length);
}
public static NativeSlice<T> Slice<T>(this NativeSlice<T> thisSlice) where T : struct
{
return thisSlice;
}
public static NativeSlice<T> Slice<T>(this NativeSlice<T> thisSlice, int start) where T : struct
{
return new NativeSlice<T>(thisSlice, start);
}
public static NativeSlice<T> Slice<T>(this NativeSlice<T> thisSlice, int start, int length) where T : struct
{
return new NativeSlice<T>(thisSlice, start, length);
}
}
[StructLayout(LayoutKind.Sequential)]
[NativeContainer]
[NativeContainerSupportsMinMaxWriteRestriction]
[DebuggerDisplay("Length = {Length}")]
[DebuggerTypeProxy(typeof(NativeSliceDebugView < >))]
unsafe public struct NativeSlice<T> : IEnumerable<T> where T : struct
{
[NativeDisableUnsafePtrRestriction]
internal byte* m_Buffer;
internal int m_Stride;
internal int m_Length;
public NativeSlice(NativeSlice<T> slice, int start) : this(slice, start, slice.Length - start) {}
public unsafe NativeSlice(NativeSlice<T> slice, int start, int length)
{
m_Stride = slice.m_Stride;
m_Buffer = slice.m_Buffer + m_Stride * start;
m_Length = length;
}
public NativeSlice(NativeArray<T> array) : this(array, 0, array.Length) {}
public NativeSlice(NativeArray<T> array, int start) : this(array, start, array.Length - start) {}
public static implicit operator NativeSlice<T>(NativeArray<T> array)
{
return new NativeSlice<T>(array);
}
public unsafe NativeSlice(NativeArray<T> array, int start, int length)
{
m_Stride = UnsafeUtility.SizeOf<T>();
byte* ptr = (byte*)array.m_Buffer + m_Stride * start;
m_Buffer = ptr;
m_Length = length;
}
// Keeps stride, changes length
public NativeSlice<U> SliceConvert<U>() where U : struct
{
var sizeofU = UnsafeUtility.SizeOf<U>();
NativeSlice<U> outputSlice;
outputSlice.m_Buffer = m_Buffer;
outputSlice.m_Stride = sizeofU;
outputSlice.m_Length = (m_Length * m_Stride) / sizeofU;
return outputSlice;
}
// Keeps length, changes stride
public unsafe NativeSlice<U> SliceWithStride<U>(int offset) where U : struct
{
NativeSlice<U> outputSlice;
outputSlice.m_Buffer = m_Buffer + offset;
outputSlice.m_Stride = m_Stride;
outputSlice.m_Length = m_Length;
return outputSlice;
}
public NativeSlice<U> SliceWithStride<U>() where U : struct
{
return SliceWithStride<U>(0);
}
// These are double-whammy excluded to we can elide bounds checks in the Burst disassembly view
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
private void CheckReadIndex(int index)
{
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
private void CheckWriteIndex(int index)
{
}
public unsafe T this[int index]
{
get
{
CheckReadIndex(index);
return UnsafeUtility.ReadArrayElementWithStride<T>(m_Buffer, index, m_Stride);
}
[WriteAccessRequired]
set
{
CheckWriteIndex(index);
UnsafeUtility.WriteArrayElementWithStride(m_Buffer, index, m_Stride, value);
}
}
[WriteAccessRequired]
public void CopyFrom(NativeSlice<T> slice)
{
UnsafeUtility.MemCpyStride(this.GetUnsafePtr(), Stride, slice.GetUnsafeReadOnlyPtr(), slice.Stride, UnsafeUtility.SizeOf<T>(), m_Length);
}
[WriteAccessRequired]
public void CopyFrom(T[] array)
{
for (var i = 0; i != m_Length; i++)
this[i] = array[i];
}
public void CopyTo(NativeArray<T> array)
{
int sizeOf = UnsafeUtility.SizeOf<T>();
UnsafeUtility.MemCpyStride(array.GetUnsafePtr(), sizeOf, this.GetUnsafeReadOnlyPtr(), Stride, sizeOf, m_Length);
}
public void CopyTo(T[] array)
{
for (var i = 0; i != m_Length; i++)
array[i] = this[i];
}
public T[] ToArray()
{
var array = new T[Length];
CopyTo(array);
return array;
}
public int Stride { get { return m_Stride; } }
public int Length { get { return m_Length; } }
public Enumerator GetEnumerator()
{
return new Enumerator(ref this);
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return new Enumerator(ref this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
[ExcludeFromDocs]
public struct Enumerator : IEnumerator<T>
{
private NativeSlice<T> m_Array;
private int m_Index;
public Enumerator(ref NativeSlice<T> array)
{
m_Array = array;
m_Index = -1;
}
public void Dispose()
{
}
public bool MoveNext()
{
m_Index++;
return m_Index < m_Array.Length;
}
public void Reset()
{
m_Index = -1;
}
public T Current
{
get
{
// Let NativeSlice indexer check for out of range.
return m_Array[m_Index];
}
}
object IEnumerator.Current
{
get { return Current; }
}
}
}
/// <summary>
/// DebuggerTypeProxy for <see cref="NativeArray{T}"/>
/// </summary>
internal sealed class NativeSliceDebugView<T> where T : struct
{
private NativeSlice<T> m_Array;
public NativeSliceDebugView(NativeSlice<T> array)
{
m_Array = array;
}
public T[] Items
{
get { return m_Array.ToArray(); }
}
}
}
namespace Unity.Collections.LowLevel.Unsafe
{
public static class NativeSliceUnsafeUtility
{
public static unsafe NativeSlice<T> ConvertExistingDataToNativeSlice<T>(void* dataPointer, int stride, int length) where T : struct
{
if (length < 0)
throw new System.ArgumentException(String.Format("Invalid length of '{0}'. It must be greater than 0.", length));
if (stride < 0)
throw new System.ArgumentException(String.Format("Invalid stride '{0}'. It must be greater than 0.", stride));
var newSlice = new NativeSlice<T>
{
m_Stride = stride,
m_Buffer = (byte*)dataPointer,
m_Length = length,
};
return newSlice;
}
unsafe public static void* GetUnsafePtr<T>(this NativeSlice<T> nativeSlice) where T : struct
{
return nativeSlice.m_Buffer;
}
unsafe public static void* GetUnsafeReadOnlyPtr<T>(this NativeSlice<T> nativeSlice) where T : struct
{
return nativeSlice.m_Buffer;
}
}
}