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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
namespace Pathfinding.Jobs {
using UnityEngine;
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using UnityEngine.Assertions;
using Pathfinding.Graphs.Grid;
using Pathfinding.Util;
/// <summary>
/// Slice of a 3D array.
///
/// This is a helper struct used in many jobs to make them work on a part of the data.
///
/// The outer array has the size <see cref="outerSize"/>.x * <see cref="outerSize"/>.y * <see cref="outerSize"/>.z, laid out as if the coordinates were sorted by the tuple (Y,Z,X).
/// The inner array has the size <see cref="slice.size"/>.x * <see cref="slice.size"/>.y * <see cref="slice.size"/>.z, also laid out as if the coordinates were sorted by the tuple (Y,Z,X).
/// </summary>
public readonly struct Slice3D {
public readonly int3 outerSize;
public readonly IntBounds slice;
public Slice3D (IntBounds outer, IntBounds slice) : this(outer.size, slice.Offset(-outer.min)) {}
public Slice3D (int3 outerSize, IntBounds slice) {
this.outerSize = outerSize;
this.slice = slice;
Assert.IsTrue(slice.min.x >= 0 && slice.min.y >= 0 && slice.min.z >= 0);
Assert.IsTrue(slice.max.x <= outerSize.x && slice.max.y <= outerSize.y && slice.max.z <= outerSize.z);
Assert.IsTrue(slice.size.x > 0 && slice.size.y > 0 && slice.size.z > 0);
}
public void AssertMatchesOuter<T>(UnsafeSpan<T> values) where T : unmanaged {
Assert.AreEqual(outerSize.x * outerSize.y * outerSize.z, values.Length);
}
public void AssertMatchesOuter<T>(NativeArray<T> values) where T : struct {
Assert.AreEqual(outerSize.x * outerSize.y * outerSize.z, values.Length);
}
public void AssertMatchesInner<T>(NativeArray<T> values) where T : struct {
Assert.AreEqual(slice.size.x * slice.size.y * slice.size.z, values.Length);
}
public void AssertSameSize (Slice3D other) {
Assert.AreEqual(slice.size, other.slice.size);
}
public int InnerCoordinateToOuterIndex (int x, int y, int z) {
var(dx, dy, dz) = outerStrides;
return (x + slice.min.x) * dx + (y + slice.min.y) * dy + (z + slice.min.z) * dz;
}
public int length => slice.size.x * slice.size.y * slice.size.z;
public (int, int, int)outerStrides => (1, outerSize.x * outerSize.z, outerSize.x);
public (int, int, int)innerStrides => (1, slice.size.x * slice.size.z, slice.size.x);
public int outerStartIndex {
get {
var(dx, dy, dz) = outerStrides;
return slice.min.x * dx + slice.min.y * dy + slice.min.z * dz;
}
}
/// <summary>True if the slice covers the whole outer array</summary>
public bool coversEverything => math.all(slice.size == outerSize);
}
/// <summary>Helpers for scheduling simple NativeArray jobs</summary>
static class NativeArrayExtensions {
/// <summary>this[i] = value</summary>
public static JobMemSet<T> MemSet<T>(this NativeArray<T> self, T value) where T : unmanaged {
return new JobMemSet<T> {
data = self,
value = value,
};
}
/// <summary>this[i] &= other[i]</summary>
public static JobAND BitwiseAndWith (this NativeArray<bool> self, NativeArray<bool> other) {
return new JobAND {
result = self,
data = other,
};
}
/// <summary>to[i] = from[i]</summary>
public static JobCopy<T> CopyToJob<T>(this NativeArray<T> from, NativeArray<T> to) where T : struct {
return new JobCopy<T> {
from = from,
to = to,
};
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static SliceActionJob<T> WithSlice<T>(this T action, Slice3D slice) where T : struct, GridIterationUtilities.ISliceAction {
return new SliceActionJob<T> {
action = action,
slice = slice,
};
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public static IndexActionJob<T> WithLength<T>(this T action, int length) where T : struct, GridIterationUtilities.ISliceAction {
return new IndexActionJob<T> {
action = action,
length = length,
};
}
public static JobRotate3DArray<T> Rotate3D<T>(this NativeArray<T> arr, int3 size, int dx, int dz) where T : unmanaged {
return new JobRotate3DArray<T> {
arr = arr,
size = size,
dx = dx,
dz = dz,
};
}
}
/// <summary>
/// Treats input as a 3-dimensional array and copies it into the output at the specified position.
///
/// The <see cref="input"/> is a 3D array, and <see cref="inputSlice"/> refers to a rectangular slice of this array.
/// The <see cref="output"/> is defined similarly.
///
/// The two slices must naturally have the same shape.
/// </summary>
[BurstCompile]
public struct JobCopyRectangle<T> : IJob where T : struct {
[ReadOnly]
[DisableUninitializedReadCheck] // TODO: Fix so that job doesn't run instead
public NativeArray<T> input;
[WriteOnly]
public NativeArray<T> output;
public Slice3D inputSlice;
public Slice3D outputSlice;
public void Execute () {
Copy(input, output, inputSlice, outputSlice);
}
/// <summary>
/// Treats input as a 3-dimensional array and copies it into the output at the specified position.
///
/// The input is a 3D array, and inputSlice refers to a rectangular slice of this array.
/// The output is defined similarly.
///
/// The two slices must naturally have the same shape.
/// </summary>
public static void Copy (NativeArray<T> input, NativeArray<T> output, Slice3D inputSlice, Slice3D outputSlice) {
inputSlice.AssertMatchesOuter(input);
outputSlice.AssertMatchesOuter(output);
inputSlice.AssertSameSize(outputSlice);
if (inputSlice.coversEverything && outputSlice.coversEverything) {
// One contiguous chunk
// TODO: Check can be made better by only checking if it is a contiguous chunk instead of covering the whole arrays
input.CopyTo(output);
} else {
// Copy row-by-row
for (int y = 0; y < outputSlice.slice.size.y; y++) {
for (int z = 0; z < outputSlice.slice.size.z; z++) {
var rowOffsetInput = inputSlice.InnerCoordinateToOuterIndex(0, y, z);
var rowOffsetOutput = outputSlice.InnerCoordinateToOuterIndex(0, y, z);
// Using a raw MemCpy call is a bit faster, but that requires unsafe code
// Using a for loop is *a lot* slower (except for very small arrays, in which case it is about the same or very slightly faster).
NativeArray<T>.Copy(input, rowOffsetInput, output, rowOffsetOutput, outputSlice.slice.size.x);
}
}
}
}
}
/// <summary>result[i] = value</summary>
[BurstCompile]
public struct JobMemSet<T> : IJob where T : unmanaged {
[WriteOnly]
public NativeArray<T> data;
public T value;
public void Execute() => data.AsUnsafeSpan().Fill(value);
}
/// <summary>to[i] = from[i]</summary>
[BurstCompile]
public struct JobCopy<T> : IJob where T : struct {
[ReadOnly]
public NativeArray<T> from;
[WriteOnly]
public NativeArray<T> to;
public void Execute () {
from.CopyTo(to);
}
}
[BurstCompile]
public struct IndexActionJob<T> : IJob where T : struct, GridIterationUtilities.ISliceAction {
public T action;
public int length;
public void Execute () {
for (int i = 0; i < length; i++) action.Execute((uint)i, (uint)i);
}
}
[BurstCompile]
public struct SliceActionJob<T> : IJob where T : struct, GridIterationUtilities.ISliceAction {
public T action;
public Slice3D slice;
public void Execute () {
GridIterationUtilities.ForEachCellIn3DSlice(slice, ref action);
}
}
/// <summary>result[i] &= data[i]</summary>
public struct JobAND : GridIterationUtilities.ISliceAction {
public NativeArray<bool> result;
[ReadOnly]
public NativeArray<bool> data;
public void Execute (uint outerIdx, uint innerIdx) {
result[(int)outerIdx] &= data[(int)outerIdx];
}
}
[BurstCompile]
public struct JobMaxHitCount : IJob {
[ReadOnly]
public NativeArray<RaycastHit> hits;
public int maxHits;
public int layerStride;
[WriteOnly]
public NativeArray<int> maxHitCount;
public void Execute () {
int maxHit = 0;
for (; maxHit < maxHits; maxHit++) {
int offset = maxHit * layerStride;
bool any = false;
for (int i = offset; i < offset + layerStride; i++) {
if (math.any(hits[i].normal)) {
any = true;
break;
}
}
if (!any) break;
}
maxHitCount[0] = math.max(1, maxHit);
}
}
/// <summary>
/// Copies hit points and normals.
/// points[i] = hits[i].point (if anything was hit), normals[i] = hits[i].normal.normalized.
/// </summary>
[BurstCompile(FloatMode = FloatMode.Fast)]
public struct JobCopyHits : IJob, GridIterationUtilities.ISliceAction {
[ReadOnly]
public NativeArray<RaycastHit> hits;
[WriteOnly]
public NativeArray<Vector3> points;
[WriteOnly]
public NativeArray<float4> normals;
public Slice3D slice;
public void Execute () {
// The number of hits may be larger than the number of points. The remaining hits are not actually hits.
Assert.IsTrue(hits.Length >= slice.length);
slice.AssertMatchesOuter(points);
slice.AssertMatchesOuter(normals);
GridIterationUtilities.ForEachCellIn3DSlice(slice, ref this);
}
public void Execute (uint outerIdx, uint innerIdx) {
Unity.Burst.CompilerServices.Aliasing.ExpectNotAliased(points, normals);
var normal = hits[(int)innerIdx].normal;
var normalV4 = new float4(normal.x, normal.y, normal.z, 0);
normals[(int)outerIdx] = math.normalizesafe(normalV4);
// Check if anything was hit. The normal will be zero otherwise
// If nothing was hit then the existing data in the points array is reused
if (math.lengthsq(normalV4) > math.FLT_MIN_NORMAL) {
points[(int)outerIdx] = hits[(int)innerIdx].point;
}
}
}
[BurstCompile]
public struct JobRotate3DArray<T>: IJob where T : unmanaged {
public NativeArray<T> arr;
public int3 size;
public int dx, dz;
public void Execute () {
int width = size.x;
int height = size.y;
int depth = size.z;
var span = arr.AsUnsafeSpan();
dx = dx % width;
dz = dz % depth;
if (dx != 0) {
if (dx < 0) dx = width + dx;
var tmp = new NativeArray<T>(dx, Allocator.Temp);
var tmpSpan = tmp.AsUnsafeSpan();
for (int y = 0; y < height; y++) {
var offset = y * width * depth;
for (int z = 0; z < depth; z++) {
span.Slice(offset + z * width + width - dx, dx).CopyTo(tmpSpan);
span.Move(offset + z * width, offset + z * width + dx, width - dx);
tmpSpan.CopyTo(span.Slice(offset + z * width, dx));
}
}
}
if (dz != 0) {
if (dz < 0) dz = depth + dz;
var tmp = new NativeArray<T>(dz * width, Allocator.Temp);
var tmpSpan = tmp.AsUnsafeSpan();
for (int y = 0; y < height; y++) {
var offset = y * width * depth;
span.Slice(offset + (depth - dz) * width, dz * width).CopyTo(tmpSpan);
span.Move(offset, offset + dz * width, (depth - dz) * width);
tmpSpan.CopyTo(span.Slice(offset, dz * width));
}
}
}
}
}
|