Esempio n. 1
0
        internal EntityArchetype CreateArchetype(ComponentType *types, int count)
        {
            CheckAccess();

            ComponentTypeInArchetype *typesInArchetype = stackalloc ComponentTypeInArchetype[count + 1];
            var componentCount = EntityManager.FillSortedArchetypeArray(typesInArchetype, types, count);

            EntityArchetype type;

            type.Archetype = EntityManagerCreateArchetypeUtility.GetOrCreateArchetype(typesInArchetype, componentCount, EntityComponentStore);

            return(type);
        }
        public EntityArchetype GetEntityOnlyArchetype()
        {
            if (!m_EntityOnlyArchetype.Valid)
            {
                ComponentTypeInArchetype entityType = new ComponentTypeInArchetype(ComponentType.ReadWrite <Entity>());
                var archetype = EntityManagerCreateArchetypeUtility.GetOrCreateArchetype(&entityType,
                                                                                         1, EntityComponentStore, EntityGroupManager);
                m_EntityOnlyArchetype = new EntityArchetype {
                    Archetype = archetype
                };
            }

            return(m_EntityOnlyArchetype);
        }
        public static Chunk *CloneChunkForDiffing(Chunk *chunk,
                                                  ManagedComponentStore srcManagedManager,
                                                  EntityComponentStore *dstEntityComponentStore,
                                                  ManagedComponentStore dstManagedComponentStore,
                                                  EntityGroupManager dstEntityGroupManager)
        {
            int *sharedIndices = stackalloc int[chunk->Archetype->NumSharedComponents];

            chunk->SharedComponentValues.CopyTo(sharedIndices, 0, chunk->Archetype->NumSharedComponents);

            dstManagedComponentStore.CopySharedComponents(srcManagedManager, sharedIndices,
                                                          chunk->Archetype->NumSharedComponents);

            // Allocate a new chunk
            Archetype *arch = EntityManagerCreateArchetypeUtility.GetOrCreateArchetype(chunk->Archetype->Types,
                                                                                       chunk->Archetype->TypesCount, dstEntityComponentStore, dstEntityGroupManager);

            Chunk *targetChunk = EntityManagerCreateDestroyEntitiesUtility.GetCleanChunk(arch, sharedIndices,
                                                                                         dstEntityComponentStore, dstManagedComponentStore, dstEntityGroupManager);

            // GetCleanChunk & CopySharedComponents both acquire a ref, once chunk owns, release CopySharedComponents ref
            for (int i = 0; i < chunk->Archetype->NumSharedComponents; ++i)
            {
                dstManagedComponentStore.RemoveReference(sharedIndices[i]);
            }

            UnityEngine.Assertions.Assert.AreEqual(0, targetChunk->Count);
            UnityEngine.Assertions.Assert.IsTrue(targetChunk->Capacity >= chunk->Count);

            int copySize = Chunk.GetChunkBufferSize();

            UnsafeUtility.MemCpy(targetChunk->Buffer, chunk->Buffer, copySize);

            EntityManagerCreateDestroyEntitiesUtility.SetChunkCount(targetChunk, chunk->Count,
                                                                    dstEntityComponentStore, dstManagedComponentStore, dstEntityGroupManager);

            targetChunk->Archetype->EntityCount += chunk->Count;

            BufferHeader.PatchAfterCloningChunk(targetChunk);

            var tempEntities = new NativeArray <Entity>(targetChunk->Count, Allocator.Temp);

            dstEntityComponentStore->AllocateEntities(targetChunk->Archetype, targetChunk, 0, targetChunk->Count,
                                                      (Entity *)tempEntities.GetUnsafePtr());

            tempEntities.Dispose();

            return(targetChunk);
        }
        public EntityArchetype GetEntityOnlyArchetype()
        {
            if (!m_EntityOnlyArchetype.Valid)
            {
                var archetypeChanges = EntityComponentStore->BeginArchetypeChangeTracking();
                ComponentTypeInArchetype entityType = new ComponentTypeInArchetype(ComponentType.ReadWrite <Entity>());
                var archetype = EntityManagerCreateArchetypeUtility.GetOrCreateArchetype(&entityType,
                                                                                         1, EntityComponentStore);
                m_EntityOnlyArchetype = new EntityArchetype {
                    Archetype = archetype
                };
                var changedArchetypes = EntityComponentStore->EndArchetypeChangeTracking(archetypeChanges);
                EntityGroupManager.AddAdditionalArchetypes(changedArchetypes);
            }

            return(m_EntityOnlyArchetype);
        }
Esempio n. 5
0
        public static void MoveChunks(
            NativeArray <ArchetypeChunk> chunks,
            NativeArray <EntityRemapUtility.EntityRemapInfo> entityRemapping,
            EntityComponentStore *srcEntityComponentStore,
            ManagedComponentStore srcManagedComponentStore,
            EntityComponentStore *dstEntityComponentStore,
            ManagedComponentStore dstManagedComponentStore)
        {
            new MoveChunksJob
            {
                srcEntityComponentStore = srcEntityComponentStore,
                dstEntityComponentStore = dstEntityComponentStore,
                entityRemapping         = entityRemapping,
                chunks = chunks
            }.Run();

            int chunkCount  = chunks.Length;
            var remapChunks = new NativeArray <RemapChunk>(chunkCount, Allocator.TempJob);

            for (int i = 0; i < chunkCount; ++i)
            {
                var chunk     = chunks[i].m_Chunk;
                var archetype = chunk->Archetype;

                //TODO: this should not be done more than once for each archetype
                var dstArchetype =
                    EntityManagerCreateArchetypeUtility.GetOrCreateArchetype(archetype->Types, archetype->TypesCount,
                                                                             dstEntityComponentStore);

                remapChunks[i] = new RemapChunk {
                    chunk = chunk, dstArchetype = dstArchetype
                };
                chunk->SequenceNumber = dstEntityComponentStore->AssignSequenceNumber(chunk);

                if (archetype->MetaChunkArchetype != null)
                {
                    Entity srcEntity = chunk->metaChunkEntity;
                    Entity dstEntity;

                    EntityManagerCreateDestroyEntitiesUtility.CreateEntities(dstArchetype->MetaChunkArchetype,
                                                                             &dstEntity, 1,
                                                                             dstEntityComponentStore, dstManagedComponentStore);

                    srcEntityComponentStore->GetChunk(srcEntity, out var srcChunk, out var srcIndex);
                    dstEntityComponentStore->GetChunk(dstEntity, out var dstChunk, out var dstIndex);

                    ChunkDataUtility.SwapComponents(srcChunk, srcIndex, dstChunk, dstIndex, 1,
                                                    srcEntityComponentStore->GlobalSystemVersion, dstEntityComponentStore->GlobalSystemVersion);
                    EntityRemapUtility.AddEntityRemapping(ref entityRemapping, srcEntity, dstEntity);

                    EntityManagerCreateDestroyEntitiesUtility.DestroyEntities(&srcEntity, 1,
                                                                              srcEntityComponentStore, srcManagedComponentStore);
                }
            }

            k_ProfileMoveSharedComponents.Begin();
            var remapShared =
                dstManagedComponentStore.MoveSharedComponents(srcManagedComponentStore, chunks,
                                                              entityRemapping,
                                                              Allocator.TempJob);

            k_ProfileMoveSharedComponents.End();

            var remapChunksJob = new RemapChunksJob
            {
                dstEntityComponentStore = dstEntityComponentStore,
                remapChunks             = remapChunks,
                entityRemapping         = entityRemapping
            }.Schedule(remapChunks.Length, 1);

            var moveChunksBetweenArchetypeJob = new MoveChunksBetweenArchetypeJob
            {
                remapChunks         = remapChunks,
                remapShared         = remapShared,
                globalSystemVersion = dstEntityComponentStore->GlobalSystemVersion
            }.Schedule(remapChunksJob);

            moveChunksBetweenArchetypeJob.Complete();

            remapShared.Dispose();
            remapChunks.Dispose();
        }
Esempio n. 6
0
        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();
        }