/
Physics.FindPairs.cs
301 lines (284 loc) · 10.8 KB
/
Physics.FindPairs.cs
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
using System;
using System.Diagnostics;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Jobs;
using Unity.Mathematics;
//Todo: Stream types, caches, scratchlists, and inflations
namespace Latios.Psyshock
{
/// <summary>
/// An interface whose Execute method is invoked for each pair found in a FindPairs operations.
/// </summary>
public interface IFindPairsProcessor
{
void Execute(FindPairsResult result);
}
[NativeContainer]
public struct FindPairsResult
{
public ColliderBody bodyA;
public ColliderBody bodyB;
public int bodyAIndex;
public int bodyBIndex;
public int jobIndex;
internal bool isThreadSafe;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
public SafeEntity entityA => new SafeEntity
{
entity = new Entity
{
Index = math.select(-bodyA.entity.Index, bodyA.entity.Index, isThreadSafe),
Version = bodyA.entity.Version
}
};
public SafeEntity entityB => new SafeEntity
{
entity = new Entity
{
Index = math.select(-bodyB.entity.Index, bodyB.entity.Index, isThreadSafe),
Version = bodyB.entity.Version
}
};
#else
public SafeEntity entityA => new SafeEntity { entity = bodyA.entity };
public SafeEntity entityB => new SafeEntity { entity = bodyB.entity };
#endif
//Todo: Shorthands for calling narrow phase distance and manifold queries
}
public static partial class Physics
{
/// <summary>
/// Request a FindPairs broadphase operation to report pairs within the layer. This is the start of a fluent expression.
/// </summary>
/// <param name="layer">The layer in which pairs should be detected</param>
/// <param name="processor">The job-like struct which should process each pair found</param>
public static FindPairsConfig<T> FindPairs<T>(CollisionLayer layer, T processor) where T : struct, IFindPairsProcessor
{
return new FindPairsConfig<T>
{
processor = processor,
layerA = layer,
isLayerLayer = false,
disableEntityAliasChecks = false
};
}
/// <summary>
/// Request a FindPairs broadphase operation to report pairs between the two layers. Only pairs containing one element from layerA and one element from layerB will be reported. This is the start of a fluent expression.
/// </summary>
/// <param name="layerA">The first layer in which pairs should be detected</param>
/// <param name="layerB">The second layer in which pairs should be detected</param>
/// <param name="processor">The job-like struct which should process each pair found</param>
public static FindPairsConfig<T> FindPairs<T>(CollisionLayer layerA, CollisionLayer layerB, T processor) where T : struct, IFindPairsProcessor
{
CheckLayersAreCompatible(layerA, layerB);
return new FindPairsConfig<T>
{
processor = processor,
layerA = layerA,
layerB = layerB,
isLayerLayer = true,
disableEntityAliasChecks = false
};
}
#region SafetyChecks
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
static void CheckLayersAreCompatible(CollisionLayer layerA, CollisionLayer layerB)
{
if (math.any(layerA.worldMin != layerB.worldMin | layerA.worldAxisStride != layerB.worldAxisStride | layerA.worldSubdivisionsPerAxis !=
layerB.worldSubdivisionsPerAxis))
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
throw new InvalidOperationException(
"The two layers used in the FindPairs operation are not compatible. Please ensure the layers were constructed with identical settings.");
#endif
}
}
#endregion
}
public partial struct FindPairsConfig<T> where T : struct, IFindPairsProcessor
{
internal T processor;
internal CollisionLayer layerA;
internal CollisionLayer layerB;
internal bool isLayerLayer;
internal bool disableEntityAliasChecks;
#region Settings
/// <summary>
/// Disables entity aliasing checks on parallel jobs when safety checks are enabled. Use this only when entities can be aliased but body indices must be thread-safe.
/// </summary>
public FindPairsConfig<T> WithoutEntityAliasingChecks()
{
disableEntityAliasChecks = true;
return this;
}
#endregion
#region Schedulers
/// <summary>
/// Run the FindPairs operation without using a job. This method can be invoked from inside a job.
/// </summary>
public void RunImmediate()
{
if (isLayerLayer)
{
FindPairsInternal.RunImmediate(layerA, layerB, processor);
}
else
{
FindPairsInternal.RunImmediate(layerA, processor);
}
}
/// <summary>
/// Run the FindPairs operation on the main thread using a Bursted job.
/// </summary>
public void Run()
{
if (isLayerLayer)
{
new FindPairsInternal.LayerLayerSingle
{
layerA = layerA,
layerB = layerB,
processor = processor
}.Run();
}
else
{
new FindPairsInternal.LayerSelfSingle
{
layer = layerA,
processor = processor
}.Run();
}
}
/// <summary>
/// Run the FindPairs operation on a single worker thread.
/// </summary>
/// <param name="inputDeps">The input dependencies for any layers or processors used in the FindPairs operation</param>
/// <returns>A JobHandle for the scheduled job</returns>
public JobHandle ScheduleSingle(JobHandle inputDeps = default)
{
if (isLayerLayer)
{
return new FindPairsInternal.LayerLayerSingle
{
layerA = layerA,
layerB = layerB,
processor = processor
}.Schedule(inputDeps);
}
else
{
return new FindPairsInternal.LayerSelfSingle
{
layer = layerA,
processor = processor
}.Schedule(inputDeps);
}
}
/// <summary>
/// Run the FindPairs operation using multiple worker threads in multiple phases.
/// </summary>
/// <param name="inputDeps">The input dependencies for any layers or processors used in the FindPairs operation</param>
/// <returns>The final JobHandle for the scheduled jobs</returns>
public JobHandle ScheduleParallel(JobHandle inputDeps = default)
{
if (isLayerLayer)
{
JobHandle jh = new FindPairsInternal.LayerLayerPart1
{
layerA = layerA,
layerB = layerB,
processor = processor
}.Schedule(layerB.BucketCount, 1, inputDeps);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
if (disableEntityAliasChecks)
{
jh = new FindPairsInternal.LayerLayerPart2
{
layerA = layerA,
layerB = layerB,
processor = processor
}.Schedule(2, 1, jh);
}
else
{
jh = new FindPairsInternal.LayerLayerPart2_WithSafety
{
layerA = layerA,
layerB = layerB,
processor = processor
}.Schedule(3, 1, jh);
}
#else
jh = new FindPairsInternal.LayerLayerPart2
{
layerA = layerA,
layerB = layerB,
processor = processor
}.Schedule(2, 1, jh);
#endif
return jh;
}
else
{
JobHandle jh = new FindPairsInternal.LayerSelfPart1
{
layer = layerA,
processor = processor
}.Schedule(layerA.BucketCount, 1, inputDeps);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
if (disableEntityAliasChecks)
{
jh = new FindPairsInternal.LayerSelfPart2
{
layer = layerA,
processor = processor
}.Schedule(jh);
}
else
{
jh = new FindPairsInternal.LayerSelfPart2_WithSafety
{
layer = layerA,
processor = processor
}.ScheduleParallel(2, 1, jh);
}
#else
jh = new FindPairsInternal.LayerSelfPart2
{
layer = layerA,
processor = processor
}.Schedule(jh);
#endif
return jh;
}
}
/// <summary>
/// Run the FindPairs operation using multiple worker threads all at once without entity or body index thread-safety.
/// </summary>
/// <param name="inputDeps">The input dependencies for any layers or processors used in the FindPairs operation</param>
/// <returns>A JobHandle for the scheduled job</returns>
public JobHandle ScheduleParallelUnsafe(JobHandle inputDeps = default)
{
if (isLayerLayer)
{
return new FindPairsInternal.LayerLayerParallelUnsafe
{
layerA = layerA,
layerB = layerB,
processor = processor
}.ScheduleParallel(3 * layerA.BucketCount - 2, 1, inputDeps);
}
else
{
return new FindPairsInternal.LayerSelfParallelUnsafe
{
layer = layerA,
processor = processor
}.ScheduleParallel(2 * layerA.BucketCount - 1, 1, inputDeps);
}
}
#endregion Schedulers
}
}