-
Notifications
You must be signed in to change notification settings - Fork 0
/
IFilteredCollisionsJob.cs
76 lines (66 loc) · 3.29 KB
/
IFilteredCollisionsJob.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
using System;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
using Unity.Jobs.LowLevel.Unsafe;
using Unity.Physics;
namespace Arc.FilteredCollisions
{
[JobProducerType(typeof(IFilteredCollisionEventJobExtensions.FilteredEntityCollisionsJobStruct<>))]
public interface IFilteredCollisionsJob
{
void Execute(CollisionEvent collisionEvent);
}
public static class IFilteredCollisionEventJobExtensions
{
public struct FilteredCollisionsJobData<T>
{
public T UserJobData;
[NativeDisableContainerSafetyRestriction]
public NativeHashMap<EntityPair, CollisionEvent> CollisionEvents;
}
public struct FilteredEntityCollisionsJobStruct<T> where T : struct, IFilteredCollisionsJob
{
static IntPtr jobReflectionData;
public static IntPtr Initialize()
{
if (jobReflectionData == IntPtr.Zero)
{
jobReflectionData = JobsUtility.CreateJobReflectionData(typeof(FilteredCollisionsJobData<T>),
typeof(T), (ExecuteJobFunction) Execute);
}
return jobReflectionData;
}
// The Execute delegate and function have the same signature as IJob
public delegate void ExecuteJobFunction(ref FilteredCollisionsJobData<T> jobData,
IntPtr additionalData,
IntPtr bufferRangePatchData, ref JobRanges ranges, int jobIndex);
public static void Execute(ref FilteredCollisionsJobData<T> jobData, IntPtr additionalData,
IntPtr bufferRangePatchData, ref JobRanges ranges, int jobIndex)
{
var collisionEvents = jobData.CollisionEvents.GetValueArray(Allocator.Temp);
for (var index = 0; index < collisionEvents.Length; index++)
{
jobData.UserJobData.Execute(collisionEvents[index]);
}
collisionEvents.Dispose();
}
}
public static unsafe JobHandle Schedule<T>(this T filteredEntityCollisionsJob,
FilteredEntityCollisionsSystem filteredEntityCollisionsSystem,
JobHandle inputDeps)
where T : struct, IFilteredCollisionsJob
{
var data = new FilteredCollisionsJobData<T>
{
UserJobData = filteredEntityCollisionsJob,
CollisionEvents = filteredEntityCollisionsSystem.Collisions,
};
inputDeps = JobHandle.CombineDependencies(filteredEntityCollisionsSystem.EventsCollectedHandle, inputDeps);
var parameters = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf(ref data),
FilteredEntityCollisionsJobStruct<T>.Initialize(),
inputDeps, ScheduleMode.Single);
return JobsUtility.Schedule(ref parameters);
}
}
}