internal void MoveChunksFromAll( NativeArray <EntityRemapUtility.EntityRemapInfo> entityRemapping, EntityComponentStore *srcEntityComponentStore, ManagedComponentStore srcManagedComponentStore) { var access = GetCheckedEntityDataAccess(); var ecs = access->EntityComponentStore; var mcs = access->ManagedComponentStore; var moveChunksJob = new MoveAllChunksJob { srcEntityComponentStore = srcEntityComponentStore, dstEntityComponentStore = ecs, entityRemapping = entityRemapping }.Schedule(); int managedComponentCount = srcEntityComponentStore->ManagedComponentIndexUsedCount; NativeArray <int> srcManagedIndices = default; NativeArray <int> dstManagedIndices = default; JobHandle gatherManagedComponentIndices = default; if (managedComponentCount > 0) { srcManagedIndices = new NativeArray <int>(managedComponentCount, Allocator.TempJob); dstManagedIndices = new NativeArray <int>(managedComponentCount, Allocator.TempJob); ecs->ReserveManagedComponentIndices(managedComponentCount); gatherManagedComponentIndices = new GatherAllManagedComponentIndicesJob { SrcEntityComponentStore = srcEntityComponentStore, DstEntityComponentStore = ecs, SrcManagedIndices = srcManagedIndices, DstManagedIndices = dstManagedIndices }.Schedule(); } JobHandle.ScheduleBatchedJobs(); int chunkCount = 0; for (var i = 0; i < srcEntityComponentStore->m_Archetypes.Length; ++i) { var srcArchetype = srcEntityComponentStore->m_Archetypes.Ptr[i]; chunkCount += srcArchetype->Chunks.Count; } var remapChunks = new NativeArray <RemapChunk>(chunkCount, Allocator.TempJob); var remapArchetypes = new NativeArray <RemapArchetype>(srcEntityComponentStore->m_Archetypes.Length, Allocator.TempJob); int chunkIndex = 0; int archetypeIndex = 0; for (var i = 0; i < srcEntityComponentStore->m_Archetypes.Length; ++i) { var srcArchetype = srcEntityComponentStore->m_Archetypes.Ptr[i]; if (srcArchetype->Chunks.Count != 0) { var dstArchetype = ecs->GetOrCreateArchetype(srcArchetype->Types, srcArchetype->TypesCount); remapArchetypes[archetypeIndex] = new RemapArchetype { srcArchetype = srcArchetype, dstArchetype = dstArchetype }; for (var j = 0; j < srcArchetype->Chunks.Count; ++j) { var srcChunk = srcArchetype->Chunks.p[j]; remapChunks[chunkIndex] = new RemapChunk { chunk = srcChunk, dstArchetype = dstArchetype }; chunkIndex++; } archetypeIndex++; ecs->IncrementComponentTypeOrderVersion(dstArchetype); } } moveChunksJob.Complete(); mcs.Playback(ref ecs->ManagedChangesTracker); srcManagedComponentStore.Playback(ref srcEntityComponentStore->ManagedChangesTracker); k_ProfileMoveSharedComponents.Begin(); var remapShared = mcs.MoveAllSharedComponents(srcManagedComponentStore, Allocator.TempJob); k_ProfileMoveSharedComponents.End(); gatherManagedComponentIndices.Complete(); k_ProfileMoveManagedComponents.Begin(); mcs.MoveManagedComponentsFromDifferentWorld(srcManagedIndices, dstManagedIndices, srcManagedIndices.Length, srcManagedComponentStore); srcEntityComponentStore->m_ManagedComponentFreeIndex.Length = 0; srcEntityComponentStore->m_ManagedComponentIndex = 1; k_ProfileMoveManagedComponents.End(); new ChunkPatchEntities { RemapChunks = remapChunks, EntityRemapping = entityRemapping, EntityComponentStore = ecs }.Run(); var remapAllChunksJob = new RemapAllChunksJob { dstEntityComponentStore = ecs, remapChunks = remapChunks, entityRemapping = entityRemapping }.Schedule(remapChunks.Length, 1); var remapArchetypesJob = new RemapAllArchetypesJob { remapArchetypes = remapArchetypes, remapShared = remapShared, dstEntityComponentStore = ecs, chunkHeaderType = TypeManager.GetTypeIndex <ChunkHeader>() }.Schedule(archetypeIndex, 1, remapAllChunksJob); mcs.Playback(ref ecs->ManagedChangesTracker); if (managedComponentCount > 0) { srcManagedIndices.Dispose(); dstManagedIndices.Dispose(); } remapArchetypesJob.Complete(); remapShared.Dispose(); remapChunks.Dispose(); }
public static void MoveChunks( NativeArray <EntityRemapUtility.EntityRemapInfo> entityRemapping, EntityComponentStore *srcEntityComponentStore, ManagedComponentStore srcManagedComponentStore, EntityComponentStore *dstEntityComponentStore, ManagedComponentStore dstManagedComponentStore) { var moveChunksJob = new MoveAllChunksJob { srcEntityComponentStore = srcEntityComponentStore, dstEntityComponentStore = dstEntityComponentStore, entityRemapping = entityRemapping }.Schedule(); JobHandle.ScheduleBatchedJobs(); int chunkCount = 0; for (var i = srcEntityComponentStore->m_Archetypes.Count - 1; i >= 0; --i) { var srcArchetype = srcEntityComponentStore->m_Archetypes.p[i]; chunkCount += srcArchetype->Chunks.Count; } var remapChunks = new NativeArray <RemapChunk>(chunkCount, Allocator.TempJob); var remapArchetypes = new NativeArray <RemapArchetype>(srcEntityComponentStore->m_Archetypes.Count, Allocator.TempJob); int chunkIndex = 0; int archetypeIndex = 0; for (var i = srcEntityComponentStore->m_Archetypes.Count - 1; i >= 0; --i) { var srcArchetype = srcEntityComponentStore->m_Archetypes.p[i]; if (srcArchetype->Chunks.Count != 0) { if (srcArchetype->NumManagedArrays != 0) { throw new ArgumentException("MoveEntitiesFrom is not supported with managed arrays"); } var dstArchetype = EntityManagerCreateArchetypeUtility.GetOrCreateArchetype(srcArchetype->Types, srcArchetype->TypesCount, dstEntityComponentStore); remapArchetypes[archetypeIndex] = new RemapArchetype { srcArchetype = srcArchetype, dstArchetype = dstArchetype }; for (var j = 0; j < srcArchetype->Chunks.Count; ++j) { var srcChunk = srcArchetype->Chunks.p[j]; remapChunks[chunkIndex] = new RemapChunk { chunk = srcChunk, dstArchetype = dstArchetype }; chunkIndex++; } archetypeIndex++; dstEntityComponentStore->IncrementComponentTypeOrderVersion(dstArchetype); } } moveChunksJob.Complete(); k_ProfileMoveSharedComponents.Begin(); var remapShared = dstManagedComponentStore.MoveAllSharedComponents(srcManagedComponentStore, Allocator.TempJob); k_ProfileMoveSharedComponents.End(); var remapAllChunksJob = new RemapAllChunksJob { dstEntityComponentStore = dstEntityComponentStore, remapChunks = remapChunks, entityRemapping = entityRemapping }.Schedule(remapChunks.Length, 1); var remapArchetypesJob = new RemapArchetypesJob { remapArchetypes = remapArchetypes, remapShared = remapShared, dstEntityComponentStore = dstEntityComponentStore, chunkHeaderType = TypeManager.GetTypeIndex <ChunkHeader>() }.Schedule(archetypeIndex, 1, remapAllChunksJob); remapArchetypesJob.Complete(); remapShared.Dispose(); remapChunks.Dispose(); }
internal void MoveChunksFrom( NativeArray <EntityRemapUtility.EntityRemapInfo> entityRemapping, EntityComponentStore *srcEntityComponentStore, ManagedComponentStore srcManagedComponentStore) { var moveChunksJob = new MoveAllChunksJob { srcEntityComponentStore = srcEntityComponentStore, dstEntityComponentStore = EntityComponentStore, entityRemapping = entityRemapping }.Schedule(); JobHandle.ScheduleBatchedJobs(); int chunkCount = 0; for (var i = 0; i < srcEntityComponentStore->m_Archetypes.Length; ++i) { var srcArchetype = srcEntityComponentStore->m_Archetypes.Ptr[i]; chunkCount += srcArchetype->Chunks.Count; } var remapChunks = new NativeArray <RemapChunk>(chunkCount, Allocator.TempJob); var remapArchetypes = new NativeArray <RemapArchetype>(srcEntityComponentStore->m_Archetypes.Length, Allocator.TempJob); var managedArrayChunks = new NativeList <IntPtr>(Allocator.Temp); int chunkIndex = 0; int archetypeIndex = 0; for (var i = 0; i < srcEntityComponentStore->m_Archetypes.Length; ++i) { var srcArchetype = srcEntityComponentStore->m_Archetypes.Ptr[i]; if (srcArchetype->Chunks.Count != 0) { var dstArchetype = EntityComponentStore->GetOrCreateArchetype(srcArchetype->Types, srcArchetype->TypesCount); remapArchetypes[archetypeIndex] = new RemapArchetype { srcArchetype = srcArchetype, dstArchetype = dstArchetype }; for (var j = 0; j < srcArchetype->Chunks.Count; ++j) { var srcChunk = srcArchetype->Chunks.p[j]; remapChunks[chunkIndex] = new RemapChunk { chunk = srcChunk, dstArchetype = dstArchetype }; chunkIndex++; } if (srcArchetype->NumManagedArrays > 0) { for (var j = 0; j < srcArchetype->Chunks.Count; ++j) { var chunk = srcArchetype->Chunks.p[j]; managedArrayChunks.Add((IntPtr)chunk); } } archetypeIndex++; EntityComponentStore->IncrementComponentTypeOrderVersion(dstArchetype); } } moveChunksJob.Complete(); var managedArrayDstIndices = new NativeArray <int>(managedArrayChunks.Length, Allocator.TempJob); var managedArraySrcIndices = new NativeArray <int>(managedArrayChunks.Length, Allocator.TempJob); EntityComponentStore->ReserveManagedObjectArrays(managedArrayDstIndices); var remapManaged = new RemapManagedArraysJob { chunks = managedArrayChunks, dstIndices = managedArrayDstIndices, srcIndices = managedArraySrcIndices, }.Schedule(managedArrayChunks.Length, 64); ManagedComponentStore.Playback(ref EntityComponentStore->ManagedChangesTracker); srcManagedComponentStore.Playback(ref srcEntityComponentStore->ManagedChangesTracker); k_ProfileMoveObjectComponents.Begin(); remapManaged.Complete(); m_ManagedComponentStore.MoveManagedObjectArrays(managedArraySrcIndices, managedArrayDstIndices, srcManagedComponentStore); k_ProfileMoveObjectComponents.End(); managedArrayDstIndices.Dispose(); managedArraySrcIndices.Dispose(); managedArrayChunks.Dispose(); k_ProfileMoveSharedComponents.Begin(); var remapShared = ManagedComponentStore.MoveAllSharedComponents(srcManagedComponentStore, Allocator.TempJob); k_ProfileMoveSharedComponents.End(); new ChunkPatchEntities { RemapChunks = remapChunks, EntityRemapping = entityRemapping, EntityComponentStore = EntityComponentStore }.Run(); var remapAllChunksJob = new RemapAllChunksJob { dstEntityComponentStore = EntityComponentStore, remapChunks = remapChunks, entityRemapping = entityRemapping }.Schedule(remapChunks.Length, 1); var remapArchetypesJob = new RemapArchetypesJob { remapArchetypes = remapArchetypes, remapShared = remapShared, dstEntityComponentStore = EntityComponentStore, chunkHeaderType = TypeManager.GetTypeIndex <ChunkHeader>() }.Schedule(archetypeIndex, 1, remapAllChunksJob); ManagedComponentStore.Playback(ref EntityComponentStore->ManagedChangesTracker); remapArchetypesJob.Complete(); remapShared.Dispose(); remapChunks.Dispose(); }