-
Notifications
You must be signed in to change notification settings - Fork 73
/
InstantiateCommandBufferUntyped.cs
871 lines (810 loc) · 42 KB
/
InstantiateCommandBufferUntyped.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
using System;
using System.Diagnostics;
using Latios.Unsafe;
using Unity.Burst;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Entities.Exposed;
using Unity.Jobs;
using Unity.Mathematics;
namespace Latios
{
[NativeContainer]
internal unsafe struct InstantiateCommandBufferUntyped : INativeDisposable
{
#region Structure
[NativeDisableUnsafePtrRestriction]
private UnsafeParallelBlockList* m_prefabSortkeyBlockList;
[NativeDisableUnsafePtrRestriction]
private UnsafeParallelBlockList* m_componentDataBlockList;
[NativeDisableUnsafePtrRestriction]
private State* m_state;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
//Unfortunately this name is hardcoded into Unity. No idea how EntityCommandBuffer gets away with multiple safety handles.
AtomicSafetyHandle m_Safety;
static readonly SharedStatic<int> s_staticSafetyId = SharedStatic<int>.GetOrCreate<InstantiateCommandBufferUntyped>();
#endif
private struct State
{
public ComponentTypeSet tagsToAdd;
public FixedList64Bytes<int> typesWithData;
public FixedList64Bytes<int> typesSizes;
public AllocatorManager.AllocatorHandle allocator;
public bool playedBack;
}
private struct PrefabSortkey : IRadixSortableInt3
{
public Entity prefab;
public int sortKey;
public int3 GetKey3()
{
return new int3(prefab.Index, prefab.Version, sortKey);
}
}
#endregion
#region CreateDestroy
public InstantiateCommandBufferUntyped(AllocatorManager.AllocatorHandle allocator, FixedList64Bytes<ComponentType> typesWithData) : this(allocator, typesWithData, 1)
{
}
internal InstantiateCommandBufferUntyped(AllocatorManager.AllocatorHandle allocator, FixedList64Bytes<ComponentType> componentTypesWithData, int disposeSentinalStackDepth)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
CheckAllocator(allocator);
m_Safety = CollectionHelper.CreateSafetyHandle(allocator);
CollectionHelper.SetStaticSafetyId<EntityOperationCommandBuffer>(ref m_Safety, ref s_staticSafetyId.Data);
AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
#endif
int dataPayloadSize = 0;
FixedList64Bytes<int> typesSizes = new FixedList64Bytes<int>();
FixedList64Bytes<int> typesWithData = new FixedList64Bytes<int>();
for (int i = 0; i < componentTypesWithData.Length; i++)
{
var size = TypeManager.GetTypeInfo(componentTypesWithData[i].TypeIndex).ElementSize;
dataPayloadSize += size;
typesSizes.Add(size);
typesWithData.Add(componentTypesWithData[i].TypeIndex);
}
CheckComponentTypesValid(BuildComponentTypesFromFixedList(typesWithData));
m_prefabSortkeyBlockList = AllocatorManager.Allocate<UnsafeParallelBlockList>(allocator, 1);
m_componentDataBlockList = AllocatorManager.Allocate<UnsafeParallelBlockList>(allocator, 1);
m_state = AllocatorManager.Allocate<State>(allocator, 1);
*m_prefabSortkeyBlockList = new UnsafeParallelBlockList(UnsafeUtility.SizeOf<PrefabSortkey>(), 256, allocator);
*m_componentDataBlockList = new UnsafeParallelBlockList(dataPayloadSize, 256, allocator);
*m_state = new State
{
typesWithData = typesWithData,
tagsToAdd = default,
typesSizes = typesSizes,
allocator = allocator,
playedBack = false
};
}
[BurstCompile]
private struct DisposeJob : IJob
{
[NativeDisableUnsafePtrRestriction]
public State* state;
[NativeDisableUnsafePtrRestriction]
public UnsafeParallelBlockList* prefabSortkeyBlockList;
[NativeDisableUnsafePtrRestriction]
public UnsafeParallelBlockList* componentDataBlockList;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
internal AtomicSafetyHandle m_Safety;
#endif
public void Execute()
{
Deallocate(state, prefabSortkeyBlockList, componentDataBlockList);
}
}
public JobHandle Dispose(JobHandle inputDeps)
{
var jobHandle = new DisposeJob
{
prefabSortkeyBlockList = m_prefabSortkeyBlockList,
componentDataBlockList = m_componentDataBlockList,
state = m_state,
#if ENABLE_UNITY_COLLECTIONS_CHECKS
m_Safety = m_Safety
#endif
}.Schedule(inputDeps);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.Release(m_Safety);
#endif
m_state = null;
m_componentDataBlockList = null;
m_prefabSortkeyBlockList = null;
return jobHandle;
}
public void Dispose()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
CollectionHelper.DisposeSafetyHandle(ref m_Safety);
#endif
Deallocate(m_state, m_prefabSortkeyBlockList, m_componentDataBlockList);
}
private static void Deallocate(State* state, UnsafeParallelBlockList* prefabSortkeyBlockList, UnsafeParallelBlockList* componentDataBlockList)
{
var allocator = state->allocator;
prefabSortkeyBlockList->Dispose();
componentDataBlockList->Dispose();
AllocatorManager.Free(allocator, prefabSortkeyBlockList, 1);
AllocatorManager.Free(allocator, componentDataBlockList, 1);
AllocatorManager.Free(allocator, state, 1);
//UnsafeUtility.Free(prefabSortkeyBlockList, allocator);
//UnsafeUtility.Free(componentDataBlockList, allocator);
//UnsafeUtility.Free(state, allocator);
}
#endregion
#region API
[WriteAccessRequired]
public void Add<T0>(Entity prefab, T0 c0, int sortKey = int.MaxValue) where T0 : unmanaged
{
CheckWriteAccess();
CheckHasNotPlayedBack();
CheckEntityValid(prefab);
m_prefabSortkeyBlockList->Write(new PrefabSortkey { prefab = prefab, sortKey = sortKey }, 0);
byte* ptr = (byte*)m_componentDataBlockList->Allocate(0);
UnsafeUtility.CopyStructureToPtr(ref c0, ptr);
}
[WriteAccessRequired]
public void Add<T0, T1>(Entity prefab, T0 c0, T1 c1, int sortKey = int.MaxValue) where T0 : unmanaged where T1 : unmanaged
{
CheckWriteAccess();
CheckHasNotPlayedBack();
CheckEntityValid(prefab);
m_prefabSortkeyBlockList->Write(new PrefabSortkey { prefab = prefab, sortKey = sortKey }, 0);
byte* ptr = (byte*)m_componentDataBlockList->Allocate(0);
UnsafeUtility.CopyStructureToPtr(ref c0, ptr);
ptr += m_state->typesSizes[0];
UnsafeUtility.CopyStructureToPtr(ref c1, ptr);
}
[WriteAccessRequired]
public void Add<T0, T1, T2>(Entity prefab, T0 c0, T1 c1, T2 c2, int sortKey = int.MaxValue) where T0 : unmanaged where T1 : unmanaged where T2 : unmanaged
{
CheckWriteAccess();
CheckHasNotPlayedBack();
CheckEntityValid(prefab);
m_prefabSortkeyBlockList->Write(new PrefabSortkey { prefab = prefab, sortKey = sortKey }, 0);
byte* ptr = (byte*)m_componentDataBlockList->Allocate(0);
UnsafeUtility.CopyStructureToPtr(ref c0, ptr);
ptr += m_state->typesSizes[0];
UnsafeUtility.CopyStructureToPtr(ref c1, ptr);
ptr += m_state->typesSizes[1];
UnsafeUtility.CopyStructureToPtr(ref c2, ptr);
}
[WriteAccessRequired]
public void Add<T0, T1, T2, T3>(Entity prefab, T0 c0, T1 c1, T2 c2, T3 c3,
int sortKey = int.MaxValue) where T0 : unmanaged where T1 : unmanaged where T2 : unmanaged where T3 : unmanaged
{
CheckWriteAccess();
CheckHasNotPlayedBack();
CheckEntityValid(prefab);
m_prefabSortkeyBlockList->Write(new PrefabSortkey { prefab = prefab, sortKey = sortKey }, 0);
byte* ptr = (byte*)m_componentDataBlockList->Allocate(0);
UnsafeUtility.CopyStructureToPtr(ref c0, ptr);
ptr += m_state->typesSizes[0];
UnsafeUtility.CopyStructureToPtr(ref c1, ptr);
ptr += m_state->typesSizes[1];
UnsafeUtility.CopyStructureToPtr(ref c2, ptr);
ptr += m_state->typesSizes[2];
UnsafeUtility.CopyStructureToPtr(ref c3, ptr);
}
[WriteAccessRequired]
public void Add<T0, T1, T2, T3, T4>(Entity prefab, T0 c0, T1 c1, T2 c2, T3 c3, T4 c4,
int sortKey = int.MaxValue) where T0 : unmanaged where T1 : unmanaged where T2 : unmanaged where T3 : unmanaged where T4 : unmanaged
{
CheckWriteAccess();
CheckHasNotPlayedBack();
CheckEntityValid(prefab);
m_prefabSortkeyBlockList->Write(new PrefabSortkey { prefab = prefab, sortKey = sortKey }, 0);
byte* ptr = (byte*)m_componentDataBlockList->Allocate(0);
UnsafeUtility.CopyStructureToPtr(ref c0, ptr);
ptr += m_state->typesSizes[0];
UnsafeUtility.CopyStructureToPtr(ref c1, ptr);
ptr += m_state->typesSizes[1];
UnsafeUtility.CopyStructureToPtr(ref c2, ptr);
ptr += m_state->typesSizes[2];
UnsafeUtility.CopyStructureToPtr(ref c3, ptr);
ptr += m_state->typesSizes[3];
UnsafeUtility.CopyStructureToPtr(ref c4, ptr);
}
public int Count()
{
CheckReadAccess();
return m_prefabSortkeyBlockList->Count();
}
public void Playback(EntityManager entityManager)
{
CheckWriteAccess();
CheckHasNotPlayedBack();
var chunkRanges = new NativeList<int2>(Allocator.TempJob);
var chunks = new NativeList<ArchetypeChunk>(Allocator.TempJob);
var indicesInChunks = new NativeList<int>(Allocator.TempJob);
var componentDataPtrs = new NativeList<UnsafeParallelBlockList.ElementPtr>(Allocator.TempJob);
entityManager.CompleteAllTrackedJobs();
//var eet = entityManager.BeginExclusiveEntityTransaction();
//Run job that instantiates entities and populates hashmap
var job0 = new InstantiateAndBuildListsJob
{
icb = this,
//eet = eet,
em = entityManager,
chunks = chunks,
chunkRanges = chunkRanges,
indicesInChunks = indicesInChunks,
componentDataPtrs = componentDataPtrs
};
job0.RunOrExecute();
//entityManager.EndExclusiveEntityTransaction();
//Schedule parallel job to populate data
var chunkJob = new WriteComponentDataJob
{
icb = this,
chunks = chunks.AsArray(),
chunkRanges = chunkRanges.AsArray(),
indicesInChunks = indicesInChunks.AsArray(),
componentDataPtrs = componentDataPtrs.AsArray(),
entityHandle = entityManager.GetEntityTypeHandle(),
t0 = entityManager.GetDynamicComponentTypeHandle(ComponentType.ReadWrite(m_state->typesWithData[0]))
};
if (m_state->typesWithData.Length > 1)
chunkJob.t1 = entityManager.GetDynamicComponentTypeHandle(ComponentType.ReadWrite(m_state->typesWithData[1]));
if (m_state->typesWithData.Length > 2)
chunkJob.t2 = entityManager.GetDynamicComponentTypeHandle(ComponentType.ReadWrite(m_state->typesWithData[2]));
if (m_state->typesWithData.Length > 3)
chunkJob.t3 = entityManager.GetDynamicComponentTypeHandle(ComponentType.ReadWrite(m_state->typesWithData[3]));
if (m_state->typesWithData.Length > 4)
chunkJob.t4 = entityManager.GetDynamicComponentTypeHandle(ComponentType.ReadWrite(m_state->typesWithData[4]));
//The remaining types apparently need to be initialized. So set them to the dummy types.
if (m_state->typesWithData.Length <= 1)
chunkJob.t1 = entityManager.GetDynamicComponentTypeHandle(ComponentType.ReadWrite<DummyTypeT1>());
if (m_state->typesWithData.Length <= 2)
chunkJob.t2 = entityManager.GetDynamicComponentTypeHandle(ComponentType.ReadWrite<DummyTypeT2>());
if (m_state->typesWithData.Length <= 3)
chunkJob.t3 = entityManager.GetDynamicComponentTypeHandle(ComponentType.ReadWrite<DummyTypeT3>());
if (m_state->typesWithData.Length <= 4)
chunkJob.t4 = entityManager.GetDynamicComponentTypeHandle(ComponentType.ReadWrite<DummyTypeT4>());
//chunkJob.ScheduleParallel(chunks.Length, 1, default).Complete();
chunkJob.RunOrExecute(chunks.Length);
m_state->playedBack = true;
chunks.Dispose();
chunkRanges.Dispose();
indicesInChunks.Dispose();
componentDataPtrs.Dispose();
}
public void SetTags(ComponentTypeSet types)
{
CheckWriteAccess();
CheckHasNotPlayedBack();
m_state->tagsToAdd = types;
}
public void AddTag(ComponentType type)
{
CheckWriteAccess();
CheckHasNotPlayedBack();
switch (m_state->tagsToAdd.Length)
{
case 0: m_state->tagsToAdd = new ComponentTypeSet(type); break;
case 1: m_state->tagsToAdd = new ComponentTypeSet(m_state->tagsToAdd.GetComponentType(0), type); break;
case 2: m_state->tagsToAdd = new ComponentTypeSet(m_state->tagsToAdd.GetComponentType(0), m_state->tagsToAdd.GetComponentType(1), type); break;
case 3: m_state->tagsToAdd =
new ComponentTypeSet(m_state->tagsToAdd.GetComponentType(0), m_state->tagsToAdd.GetComponentType(1), m_state->tagsToAdd.GetComponentType(2), type); break;
case 4: m_state->tagsToAdd =
new ComponentTypeSet(m_state->tagsToAdd.GetComponentType(0),
m_state->tagsToAdd.GetComponentType(1),
m_state->tagsToAdd.GetComponentType(2),
m_state->tagsToAdd.GetComponentType(3),
type); break;
case var n when n >= 5 && n < 15:
{
FixedList128Bytes <ComponentType> types = default;
for (int i = 0; i < m_state->tagsToAdd.Length; i++)
types.Add(m_state->tagsToAdd.GetComponentType(i));
types.Add(type);
m_state->tagsToAdd = new ComponentTypeSet(types);
break;
}
default: ThrowTooManyTags(); break;
}
}
public ParallelWriter AsParallelWriter()
{
var writer = new ParallelWriter(m_prefabSortkeyBlockList, m_componentDataBlockList, m_state);
#if ENABLE_UNITY_COLLECTIONS_CHECKS
writer.m_Safety = m_Safety;
CollectionHelper.SetStaticSafetyId<ParallelWriter>(ref writer.m_Safety, ref ParallelWriter.s_staticSafetyId.Data);
#endif
return writer;
}
#endregion
#region Implementation
[BurstCompile]
private struct InstantiateAndBuildListsJob : IJob
{
[ReadOnly] public InstantiateCommandBufferUntyped icb;
//public ExclusiveEntityTransaction eet;
public EntityManager em;
public NativeList<ArchetypeChunk> chunks;
public NativeList<int2> chunkRanges;
public NativeList<int> indicesInChunks;
public NativeList<UnsafeParallelBlockList.ElementPtr> componentDataPtrs;
public void Execute()
{
//Step 1: Get the prefabs and sort keys
int count = icb.Count();
var prefabSortkeyArray = new NativeArray<PrefabSortkey>(count, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
icb.m_prefabSortkeyBlockList->GetElementValues(prefabSortkeyArray);
//Step 2: Get the componentData pointers
var unsortedComponentDataPtrs = new NativeArray<UnsafeParallelBlockList.ElementPtr>(count, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
icb.m_componentDataBlockList->GetElementPtrs(unsortedComponentDataPtrs);
//Step 3: Sort the arrays by sort key and collapse unique entities
var ranks = new NativeArray<int>(count, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
RadixSort.RankSortInt3(ranks, prefabSortkeyArray);
var sortedPrefabs = new NativeList<Entity>(count, Allocator.Temp);
var sortedPrefabCounts = new NativeList<int>(count, Allocator.Temp);
var sortedComponentDataPtrs = new NativeArray<UnsafeParallelBlockList.ElementPtr>(count, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
Entity lastEntity = Entity.Null;
for (int i = 0; i < count; i++)
{
var entity = prefabSortkeyArray[ranks[i]].prefab;
sortedComponentDataPtrs[i] = unsortedComponentDataPtrs[ranks[i]];
if (entity != lastEntity)
{
sortedPrefabs.AddNoResize(entity);
sortedPrefabCounts.AddNoResize(1);
lastEntity = entity;
}
else
{
ref var c = ref sortedPrefabCounts.ElementAt(sortedPrefabCounts.Length - 1);
c++;
}
}
//Step 4: Instantiate the prefabs
var instantiatedEntities = new NativeArray<Entity>(count, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
var typesWithDataToAdd = BuildComponentTypesFromFixedList(icb.m_state->typesWithData);
int startIndex = 0;
for (int i = 0; i < sortedPrefabs.Length; i++)
{
//var firstEntity = eet.Instantiate(sortedPrefabs[i]);
//eet.EntityManager.AddComponents(firstEntity, typesWithDataToAdd);
//eet.EntityManager.AddComponents(firstEntity, icb.m_state->tagsToAdd);
var firstEntity = em.Instantiate(sortedPrefabs[i]);
em.AddComponent(firstEntity, typesWithDataToAdd);
em.AddComponent(firstEntity, icb.m_state->tagsToAdd);
instantiatedEntities[startIndex] = firstEntity;
startIndex++;
if (sortedPrefabCounts[i] - 1 > 0)
{
var subArray = instantiatedEntities.GetSubArray(startIndex, sortedPrefabCounts[i] - 1);
//eet.Instantiate(firstEntity, subArray);
em.Instantiate(firstEntity, subArray);
startIndex += subArray.Length;
}
}
//Step 5: Get locations of new entities
var locations = new NativeArray<EntityLocationInChunk>(count, Allocator.Temp);
for (int i = 0; i < count; i++)
{
//locations[i] = eet.EntityManager.GetEntityLocationInChunk(instantiatedEntities[i]);
locations[i] = em.GetEntityLocationInChunk(instantiatedEntities[i]);
}
//Step 6: Sort chunks and build final lists
RadixSort.RankSortInt3(ranks, locations.Reinterpret<WrappedEntityLocationInChunk>());
chunks.Capacity = count;
chunkRanges.Capacity = count;
indicesInChunks.ResizeUninitialized(count);
componentDataPtrs.ResizeUninitialized(count);
ArchetypeChunk lastChunk = default;
for (int i = 0; i < count; i++)
{
var loc = locations[ranks[i]];
indicesInChunks[i] = loc.indexInChunk;
componentDataPtrs[i] = sortedComponentDataPtrs[ranks[i]];
if (loc.chunk != lastChunk)
{
chunks.AddNoResize(loc.chunk);
chunkRanges.AddNoResize(new int2(i, 1));
lastChunk = loc.chunk;
}
else
{
ref var c = ref chunkRanges.ElementAt(chunkRanges.Length - 1);
c.y++;
}
}
}
struct WrappedEntityLocationInChunk : IRadixSortableInt3
{
public EntityLocationInChunk elic;
public int3 GetKey3()
{
var c = elic.ChunkAddressAsUlong;
int x = (int)(c >> 32);
int y = (int)(c & 0xFFFFFFFF);
int z = elic.indexInChunk;
return new int3(x, y, z);
}
}
public void RunOrExecute()
{
bool ran = false;
TryRun(ref ran);
if (!ran)
Execute();
}
[BurstDiscard]
void TryRun(ref bool ran)
{
this.Run();
ran = true;
}
}
[BurstCompile]
private struct WriteComponentDataJob : IJobParallelFor
{
[ReadOnly] public InstantiateCommandBufferUntyped icb;
[ReadOnly] public NativeArray<ArchetypeChunk> chunks;
[ReadOnly] public NativeArray<int2> chunkRanges;
[ReadOnly] public NativeArray<int> indicesInChunks;
[ReadOnly] public NativeArray<UnsafeParallelBlockList.ElementPtr> componentDataPtrs;
[ReadOnly] public EntityTypeHandle entityHandle;
public DynamicComponentTypeHandle t0;
public DynamicComponentTypeHandle t1;
public DynamicComponentTypeHandle t2;
public DynamicComponentTypeHandle t3;
public DynamicComponentTypeHandle t4;
public void Execute(int i)
{
var chunk = chunks[i];
var range = chunkRanges[i];
var indices = indicesInChunks.GetSubArray(range.x, range.y);
var ptrs = componentDataPtrs.GetSubArray(range.x, range.y);
switch(icb.m_state->typesSizes.Length)
{
case 1: DoT0(chunk, indices, ptrs); return;
case 2: DoT1(chunk, indices, ptrs); return;
case 3: DoT2(chunk, indices, ptrs); return;
case 4: DoT3(chunk, indices, ptrs); return;
case 5: DoT4(chunk, indices, ptrs); return;
}
}
void DoT0(ArchetypeChunk chunk, NativeArray<int> indices, NativeArray<UnsafeParallelBlockList.ElementPtr> dataPtrs)
{
var entities = chunk.GetNativeArray(entityHandle);
var t0Size = icb.m_state->typesSizes[0];
var t0Array = chunk.GetDynamicComponentDataArrayReinterpret<byte>(ref t0, t0Size);
byte* t0Ptr = (byte*)t0Array.GetUnsafePtr();
for (int i = 0; i < indices.Length; i++)
{
var index = indices[i];
var dataPtr = dataPtrs[i].ptr;
UnsafeUtility.MemCpy(t0Ptr + index * t0Size, dataPtr, t0Size);
}
}
void DoT1(ArchetypeChunk chunk, NativeArray<int> indices, NativeArray<UnsafeParallelBlockList.ElementPtr> dataPtrs)
{
var entities = chunk.GetNativeArray(entityHandle);
var t0Size = icb.m_state->typesSizes[0];
var t1Size = icb.m_state->typesSizes[1];
var t0Array = chunk.GetDynamicComponentDataArrayReinterpret<byte>(ref t0, t0Size);
var t1Array = chunk.GetDynamicComponentDataArrayReinterpret<byte>(ref t1, t1Size);
byte* t0Ptr = (byte*)t0Array.GetUnsafePtr();
byte* t1Ptr = (byte*)t1Array.GetUnsafePtr();
for (int i = 0; i < indices.Length; i++)
{
var index = indices[i];
var dataPtr = dataPtrs[i].ptr;
UnsafeUtility.MemCpy(t0Ptr + index * t0Size, dataPtr, t0Size);
dataPtr += t0Size;
UnsafeUtility.MemCpy(t1Ptr + index * t1Size, dataPtr, t1Size);
}
}
void DoT2(ArchetypeChunk chunk, NativeArray<int> indices, NativeArray<UnsafeParallelBlockList.ElementPtr> dataPtrs)
{
var entities = chunk.GetNativeArray(entityHandle);
var t0Size = icb.m_state->typesSizes[0];
var t1Size = icb.m_state->typesSizes[1];
var t2Size = icb.m_state->typesSizes[2];
var t0Array = chunk.GetDynamicComponentDataArrayReinterpret<byte>(ref t0, t0Size);
var t1Array = chunk.GetDynamicComponentDataArrayReinterpret<byte>(ref t1, t1Size);
var t2Array = chunk.GetDynamicComponentDataArrayReinterpret<byte>(ref t2, t2Size);
byte* t0Ptr = (byte*)t0Array.GetUnsafePtr();
byte* t1Ptr = (byte*)t1Array.GetUnsafePtr();
byte* t2Ptr = (byte*)t2Array.GetUnsafePtr();
for (int i = 0; i < indices.Length; i++)
{
var index = indices[i];
var dataPtr = dataPtrs[i].ptr;
UnsafeUtility.MemCpy(t0Ptr + index * t0Size, dataPtr, t0Size);
dataPtr += t0Size;
UnsafeUtility.MemCpy(t1Ptr + index * t1Size, dataPtr, t1Size);
dataPtr += t1Size;
UnsafeUtility.MemCpy(t2Ptr + index * t2Size, dataPtr, t2Size);
}
}
void DoT3(ArchetypeChunk chunk, NativeArray<int> indices, NativeArray<UnsafeParallelBlockList.ElementPtr> dataPtrs)
{
var entities = chunk.GetNativeArray(entityHandle);
var t0Size = icb.m_state->typesSizes[0];
var t1Size = icb.m_state->typesSizes[1];
var t2Size = icb.m_state->typesSizes[2];
var t3Size = icb.m_state->typesSizes[3];
var t0Array = chunk.GetDynamicComponentDataArrayReinterpret<byte>(ref t0, t0Size);
var t1Array = chunk.GetDynamicComponentDataArrayReinterpret<byte>(ref t1, t1Size);
var t2Array = chunk.GetDynamicComponentDataArrayReinterpret<byte>(ref t2, t2Size);
var t3Array = chunk.GetDynamicComponentDataArrayReinterpret<byte>(ref t3, t3Size);
byte* t0Ptr = (byte*)t0Array.GetUnsafePtr();
byte* t1Ptr = (byte*)t1Array.GetUnsafePtr();
byte* t2Ptr = (byte*)t2Array.GetUnsafePtr();
byte* t3Ptr = (byte*)t3Array.GetUnsafePtr();
for (int i = 0; i < indices.Length; i++)
{
var index = indices[i];
var dataPtr = dataPtrs[i].ptr;
UnsafeUtility.MemCpy(t0Ptr + index * t0Size, dataPtr, t0Size);
dataPtr += t0Size;
UnsafeUtility.MemCpy(t1Ptr + index * t1Size, dataPtr, t1Size);
dataPtr += t1Size;
UnsafeUtility.MemCpy(t2Ptr + index * t2Size, dataPtr, t2Size);
dataPtr += t2Size;
UnsafeUtility.MemCpy(t3Ptr + index * t3Size, dataPtr, t3Size);
}
}
void DoT4(ArchetypeChunk chunk, NativeArray<int> indices, NativeArray<UnsafeParallelBlockList.ElementPtr> dataPtrs)
{
var entities = chunk.GetNativeArray(entityHandle);
var t0Size = icb.m_state->typesSizes[0];
var t1Size = icb.m_state->typesSizes[1];
var t2Size = icb.m_state->typesSizes[2];
var t3Size = icb.m_state->typesSizes[3];
var t4Size = icb.m_state->typesSizes[4];
var t0Array = chunk.GetDynamicComponentDataArrayReinterpret<byte>(ref t0, t0Size);
var t1Array = chunk.GetDynamicComponentDataArrayReinterpret<byte>(ref t1, t1Size);
var t2Array = chunk.GetDynamicComponentDataArrayReinterpret<byte>(ref t2, t2Size);
var t3Array = chunk.GetDynamicComponentDataArrayReinterpret<byte>(ref t3, t3Size);
var t4Array = chunk.GetDynamicComponentDataArrayReinterpret<byte>(ref t4, t4Size);
byte* t0Ptr = (byte*)t0Array.GetUnsafePtr();
byte* t1Ptr = (byte*)t1Array.GetUnsafePtr();
byte* t2Ptr = (byte*)t2Array.GetUnsafePtr();
byte* t3Ptr = (byte*)t3Array.GetUnsafePtr();
byte* t4Ptr = (byte*)t4Array.GetUnsafePtr();
for (int i = 0; i < indices.Length; i++)
{
var index = indices[i];
var dataPtr = dataPtrs[i].ptr;
UnsafeUtility.MemCpy(t0Ptr + index * t0Size, dataPtr, t0Size);
dataPtr += t0Size;
UnsafeUtility.MemCpy(t1Ptr + index * t1Size, dataPtr, t1Size);
dataPtr += t1Size;
UnsafeUtility.MemCpy(t2Ptr + index * t2Size, dataPtr, t2Size);
dataPtr += t2Size;
UnsafeUtility.MemCpy(t3Ptr + index * t3Size, dataPtr, t3Size);
dataPtr += t3Size;
UnsafeUtility.MemCpy(t4Ptr + index * t4Size, dataPtr, t4Size);
}
}
public void RunOrExecute(int length)
{
bool ran = false;
TryRun(length, ref ran);
if (!ran)
{
for (int i = 0; i < length; i++)
Execute(i);
}
}
[BurstDiscard]
void TryRun(int length, ref bool ran)
{
this.Run(length);
ran = true;
}
}
#pragma warning disable CS0649
private struct DummyTypeT1 : IComponentData { public int dummy; }
private struct DummyTypeT2 : IComponentData { public int dummy; }
private struct DummyTypeT3 : IComponentData { public int dummy; }
private struct DummyTypeT4 : IComponentData { public int dummy; }
#pragma warning restore CS0649
static ComponentTypeSet BuildComponentTypesFromFixedList(FixedList64Bytes<int> types)
{
switch (types.Length)
{
case 1: return new ComponentTypeSet(ComponentType.ReadWrite(types[0]));
case 2: return new ComponentTypeSet(ComponentType.ReadWrite(types[0]), ComponentType.ReadWrite(types[1]));
case 3: return new ComponentTypeSet(ComponentType.ReadWrite(types[0]), ComponentType.ReadWrite(types[1]), ComponentType.ReadWrite(types[2]));
case 4: return new ComponentTypeSet(ComponentType.ReadWrite(types[0]), ComponentType.ReadWrite(types[1]), ComponentType.ReadWrite(types[2]),
ComponentType.ReadWrite(types[3]));
case 5: return new ComponentTypeSet(ComponentType.ReadWrite(types[0]), ComponentType.ReadWrite(types[1]), ComponentType.ReadWrite(types[2]),
ComponentType.ReadWrite(types[3]), ComponentType.ReadWrite(types[4]));
default: return default;
}
}
#endregion
#region Checks
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void CheckWriteAccess()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.CheckWriteAndThrow(m_Safety);
#endif
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void CheckReadAccess()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
#endif
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
static void CheckComponentTypesValid(ComponentTypeSet types)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
for (int i = 0; i < types.Length; i++)
{
var t = types.GetComponentType(i);
if (t.IsZeroSized)
throw new InvalidOperationException(
"InstantiateCommandBuffer cannot be created with zero-sized component types. You can instead add such types using AddTagComponent() after creation.");
}
#endif
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void CheckEntityValid(Entity entity)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
if (entity == Entity.Null)
throw new InvalidOperationException("A null entity was added to the InstantiateCommandBuffer. This is not currently supported.");
#endif
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void CheckHasNotPlayedBack()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
if (m_state->playedBack)
throw new InvalidOperationException(
"InstantiateCommandBuffer has already been played back.");
#endif
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void ThrowTooManyTags()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
throw new InvalidOperationException(
"At least 15 tags have already been added and adding more is not supported.");
#endif
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
static void CheckAllocator(AllocatorManager.AllocatorHandle allocator)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
if (allocator.ToAllocator <= Allocator.None)
throw new System.InvalidOperationException("Allocator cannot be Invalid or None");
#endif
}
#endregion
#region ParallelWriter
[NativeContainer]
[NativeContainerIsAtomicWriteOnly]
public struct ParallelWriter
{
[NativeDisableUnsafePtrRestriction]
private UnsafeParallelBlockList* m_prefabSortkeyBlockList;
[NativeDisableUnsafePtrRestriction]
private UnsafeParallelBlockList* m_componentDataBlockList;
[NativeDisableUnsafePtrRestriction]
private State* m_state;
[NativeSetThreadIndex]
private int m_ThreadIndex;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
//More ugly Unity naming
internal AtomicSafetyHandle m_Safety;
internal static readonly SharedStatic<int> s_staticSafetyId = SharedStatic<int>.GetOrCreate<ParallelWriter>();
#endif
internal ParallelWriter(UnsafeParallelBlockList* prefabSortkeyBlockList, UnsafeParallelBlockList* componentDataBlockList, void* state)
{
m_prefabSortkeyBlockList = prefabSortkeyBlockList;
m_componentDataBlockList = componentDataBlockList;
m_state = (State*)state;
m_ThreadIndex = 0;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
m_Safety = default;
#endif
}
public void Add<T0>(Entity prefab, T0 c0, int sortKey) where T0 : unmanaged
{
CheckWriteAccess();
CheckHasNotPlayedBack();
CheckEntityValid(prefab);
m_prefabSortkeyBlockList->Write(new PrefabSortkey { prefab = prefab, sortKey = sortKey }, m_ThreadIndex);
byte* ptr = (byte*)m_componentDataBlockList->Allocate(m_ThreadIndex);
UnsafeUtility.CopyStructureToPtr(ref c0, ptr);
}
public void Add<T0, T1>(Entity prefab, T0 c0, T1 c1, int sortKey) where T0 : unmanaged where T1 : unmanaged
{
CheckWriteAccess();
CheckHasNotPlayedBack();
CheckEntityValid(prefab);
m_prefabSortkeyBlockList->Write(new PrefabSortkey { prefab = prefab, sortKey = sortKey }, m_ThreadIndex);
byte* ptr = (byte*)m_componentDataBlockList->Allocate(m_ThreadIndex);
UnsafeUtility.CopyStructureToPtr(ref c0, ptr);
ptr += m_state->typesSizes[0];
UnsafeUtility.CopyStructureToPtr(ref c1, ptr);
}
public void Add<T0, T1, T2>(Entity prefab, T0 c0, T1 c1, T2 c2, int sortKey) where T0 : unmanaged where T1 : unmanaged where T2 : unmanaged
{
CheckWriteAccess();
CheckHasNotPlayedBack();
CheckEntityValid(prefab);
m_prefabSortkeyBlockList->Write(new PrefabSortkey { prefab = prefab, sortKey = sortKey }, m_ThreadIndex);
byte* ptr = (byte*)m_componentDataBlockList->Allocate(m_ThreadIndex);
UnsafeUtility.CopyStructureToPtr(ref c0, ptr);
ptr += m_state->typesSizes[0];
UnsafeUtility.CopyStructureToPtr(ref c1, ptr);
ptr += m_state->typesSizes[1];
UnsafeUtility.CopyStructureToPtr(ref c2, ptr);
}
public void Add<T0, T1, T2, T3>(Entity prefab, T0 c0, T1 c1, T2 c2, T3 c3,
int sortKey) where T0 : unmanaged where T1 : unmanaged where T2 : unmanaged where T3 : unmanaged
{
CheckWriteAccess();
CheckHasNotPlayedBack();
CheckEntityValid(prefab);
m_prefabSortkeyBlockList->Write(new PrefabSortkey { prefab = prefab, sortKey = sortKey }, m_ThreadIndex);
byte* ptr = (byte*)m_componentDataBlockList->Allocate(m_ThreadIndex);
UnsafeUtility.CopyStructureToPtr(ref c0, ptr);
ptr += m_state->typesSizes[0];
UnsafeUtility.CopyStructureToPtr(ref c1, ptr);
ptr += m_state->typesSizes[1];
UnsafeUtility.CopyStructureToPtr(ref c2, ptr);
ptr += m_state->typesSizes[2];
UnsafeUtility.CopyStructureToPtr(ref c3, ptr);
}
public void Add<T0, T1, T2, T3, T4>(Entity prefab, T0 c0, T1 c1, T2 c2, T3 c3, T4 c4,
int sortKey) where T0 : unmanaged where T1 : unmanaged where T2 : unmanaged where T3 : unmanaged where T4 : unmanaged
{
CheckWriteAccess();
CheckHasNotPlayedBack();
CheckEntityValid(prefab);
m_prefabSortkeyBlockList->Write(new PrefabSortkey { prefab = prefab, sortKey = sortKey }, m_ThreadIndex);
byte* ptr = (byte*)m_componentDataBlockList->Allocate(m_ThreadIndex);
UnsafeUtility.CopyStructureToPtr(ref c0, ptr);
ptr += m_state->typesSizes[0];
UnsafeUtility.CopyStructureToPtr(ref c1, ptr);
ptr += m_state->typesSizes[1];
UnsafeUtility.CopyStructureToPtr(ref c2, ptr);
ptr += m_state->typesSizes[2];
UnsafeUtility.CopyStructureToPtr(ref c3, ptr);
ptr += m_state->typesSizes[3];
UnsafeUtility.CopyStructureToPtr(ref c4, ptr);
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void CheckWriteAccess()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.CheckWriteAndThrow(m_Safety);
#endif
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void CheckEntityValid(Entity entity)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
if (entity == Entity.Null)
throw new InvalidOperationException("A null entity was added to the InstantiateCommandBuffer. This is not currently supported.");
#endif
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
void CheckHasNotPlayedBack()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
if (m_state->playedBack)
throw new InvalidOperationException(
"InstantiateCommandBuffer has already been played back.");
#endif
}
}
#endregion
}
}