summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Graphs/Grid/Jobs/JobCopyBuffers.cs
blob: 690448ff656d791e999f068cf4e0afd460bb7894 (plain)
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
using UnityEngine;
using Unity.Burst;
using Unity.Collections;
using Unity.Jobs;
using Unity.Mathematics;
using Pathfinding.Jobs;
using UnityEngine.Assertions;

namespace Pathfinding.Graphs.Grid.Jobs {
	/// <summary>
	/// Copies 3D arrays with grid data from one shape to another.
	///
	/// Only the data for the nodes that exist in both buffers will be copied.
	///
	/// This essentially is several <see cref="JobCopyRectangle"/> jobs in one (to avoid scheduling overhead).
	/// See that job for more documentation.
	/// </summary>
	[BurstCompile]
	public struct JobCopyBuffers : IJob {
		[ReadOnly]
		[DisableUninitializedReadCheck]
		public GridGraphNodeData input;

		[WriteOnly]
		public GridGraphNodeData output;
		public IntBounds bounds;

		public bool copyPenaltyAndTags;

		public void Execute () {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
			if (!input.bounds.Contains(bounds)) throw new System.ArgumentException("Bounds are outside the source buffer");
			if (!output.bounds.Contains(bounds)) throw new System.ArgumentException("Bounds are outside the destination buffer");
#endif
			var inputSlice = new Slice3D(input.bounds, bounds);
			var outputSlice = new Slice3D(output.bounds, bounds);
			// Note: Having a single job that copies all of the buffers avoids a lot of scheduling overhead.
			// We do miss out on parallelization, however for this job it is not that significant.
			JobCopyRectangle<Vector3>.Copy(input.positions, output.positions, inputSlice, outputSlice);
			JobCopyRectangle<float4>.Copy(input.normals, output.normals, inputSlice, outputSlice);
			JobCopyRectangle<ulong>.Copy(input.connections, output.connections, inputSlice, outputSlice);
			if (copyPenaltyAndTags) {
				JobCopyRectangle<uint>.Copy(input.penalties, output.penalties, inputSlice, outputSlice);
				JobCopyRectangle<int>.Copy(input.tags, output.tags, inputSlice, outputSlice);
			}
			JobCopyRectangle<bool>.Copy(input.walkable, output.walkable, inputSlice, outputSlice);
			JobCopyRectangle<bool>.Copy(input.walkableWithErosion, output.walkableWithErosion, inputSlice, outputSlice);
		}
	}
}