Example #1
0
            public void Execute(int index)
            {
                var srcChunk = Chunks[index].m_Chunk;
                var dstChunk = ClonedChunks[index].m_Chunk;
                var copySize = Chunk.GetChunkBufferSize();

                UnsafeUtility.MemCpy((byte *)dstChunk + Chunk.kBufferOffset, (byte *)srcChunk + Chunk.kBufferOffset, copySize);
                BufferHeader.PatchAfterCloningChunk(dstChunk);
            }
        static Chunk *CloneChunkWithoutAllocatingEntities(EntityManager dstEntityManager, Chunk *srcChunk, ManagedComponentStore srcManagedComponentStore)
        {
            var dstEntityComponentStore  = dstEntityManager.EntityComponentStore;
            var dstManagedComponentStore = dstEntityManager.ManagedComponentStore;

            // Copy shared component data
            var dstSharedIndices = stackalloc int[srcChunk->Archetype->NumSharedComponents];

            srcChunk->SharedComponentValues.CopyTo(dstSharedIndices, 0, srcChunk->Archetype->NumSharedComponents);
            dstManagedComponentStore.CopySharedComponents(srcManagedComponentStore, dstSharedIndices, srcChunk->Archetype->NumSharedComponents);

            // @TODO: Why don't we memcpy the whole chunk. So we include all extra fields???

            // Allocate a new chunk
            var srcArchetype = srcChunk->Archetype;
            var dstArchetype = dstEntityComponentStore->GetOrCreateArchetype(srcArchetype->Types, srcArchetype->TypesCount);

            var dstChunk = dstEntityComponentStore->GetCleanChunkNoMetaChunk(dstArchetype, dstSharedIndices);

            dstManagedComponentStore.Playback(ref dstEntityComponentStore->ManagedChangesTracker);

            dstChunk->metaChunkEntity = srcChunk->metaChunkEntity;

            // Release any references obtained by GetCleanChunk & CopySharedComponents
            for (var i = 0; i < srcChunk->Archetype->NumSharedComponents; i++)
            {
                dstManagedComponentStore.RemoveReference(dstSharedIndices[i]);
            }

            dstEntityComponentStore->SetChunkCountKeepMetaChunk(dstChunk, srcChunk->Count);
            dstManagedComponentStore.Playback(ref dstEntityComponentStore->ManagedChangesTracker);

            dstChunk->Archetype->EntityCount += srcChunk->Count;

            var copySize = Chunk.GetChunkBufferSize();

            UnsafeUtility.MemCpy((byte *)dstChunk + Chunk.kBufferOffset, (byte *)srcChunk + Chunk.kBufferOffset, copySize);

            var numManagedComponents = dstChunk->Archetype->NumManagedComponents;

            for (int t = 0; t < numManagedComponents; ++t)
            {
                int type   = t + dstChunk->Archetype->FirstManagedComponent;
                var offset = dstChunk->Archetype->Offsets[type];
                var a      = (int *)(dstChunk->Buffer + offset);
                dstManagedComponentStore.CloneManagedComponentsFromDifferentWorld(a, dstChunk->Count, srcManagedComponentStore, ref *dstEntityManager.EntityComponentStore);
            }

            BufferHeader.PatchAfterCloningChunk(dstChunk);
            dstChunk->SequenceNumber = srcChunk->SequenceNumber;

            return(dstChunk);
        }
        static Chunk *CloneChunkWithoutAllocatingEntities(EntityManager dstEntityManager, Chunk *srcChunk, ManagedComponentStore srcManagedComponentStore)
        {
            var dstEntityComponentStore  = dstEntityManager.EntityComponentStore;
            var dstManagedComponentStore = dstEntityManager.ManagedComponentStore;

            // Copy shared component data
            var dstSharedIndices = stackalloc int[srcChunk->Archetype->NumSharedComponents];

            srcChunk->SharedComponentValues.CopyTo(dstSharedIndices, 0, srcChunk->Archetype->NumSharedComponents);
            dstManagedComponentStore.CopySharedComponents(srcManagedComponentStore, dstSharedIndices, srcChunk->Archetype->NumSharedComponents);

            // @TODO: Why don't we memcpy the whole chunk. So we include all extra fields???

            // Allocate a new chunk
            var srcArchetype = srcChunk->Archetype;
            var dstArchetype = dstEntityComponentStore->GetOrCreateArchetype(srcArchetype->Types, srcArchetype->TypesCount);

            var dstChunk = dstEntityComponentStore->GetCleanChunkNoMetaChunk(dstArchetype, dstSharedIndices);

            dstManagedComponentStore.Playback(ref dstEntityComponentStore->ManagedChangesTracker);

            dstChunk->metaChunkEntity = srcChunk->metaChunkEntity;

            // Release any references obtained by GetCleanChunk & CopySharedComponents
            for (var i = 0; i < srcChunk->Archetype->NumSharedComponents; i++)
            {
                dstManagedComponentStore.RemoveReference(dstSharedIndices[i]);
            }

            dstEntityComponentStore->SetChunkCountKeepMetaChunk(dstChunk, srcChunk->Count);
            dstManagedComponentStore.Playback(ref dstEntityComponentStore->ManagedChangesTracker);

            dstChunk->Archetype->EntityCount += srcChunk->Count;

            var copySize = Chunk.GetChunkBufferSize();

            UnsafeUtility.MemCpy((byte *)dstChunk + Chunk.kBufferOffset, (byte *)srcChunk + Chunk.kBufferOffset, copySize);

            // @TODO: Class components should be duplicated instead of copied by ref?
            if (dstChunk->ManagedArrayIndex != -1)
            {
                ManagedComponentStore.CopyManagedObjects(srcManagedComponentStore, srcChunk->Archetype, srcChunk->ManagedArrayIndex, srcChunk->Capacity, 0, dstManagedComponentStore, dstChunk->Archetype, dstChunk->ManagedArrayIndex, dstChunk->Capacity, 0, srcChunk->Count);
            }

            BufferHeader.PatchAfterCloningChunk(dstChunk);
            dstChunk->SequenceNumber = srcChunk->SequenceNumber;

            return(dstChunk);
        }
        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);
        }
Example #5
0
        static Chunk *CloneChunkWithoutAllocatingEntities(EntityManager dstEntityManager, Chunk *srcChunk, ManagedComponentStore srcManagedComponentStore)
        {
            var dstAccess = dstEntityManager.GetCheckedEntityDataAccess();
            var dstEntityComponentStore  = dstAccess->EntityComponentStore;
            var dstManagedComponentStore = dstAccess->ManagedComponentStore;

            // Copy shared component data
            var dstSharedIndices = stackalloc int[srcChunk->Archetype->NumSharedComponents];

            srcChunk->SharedComponentValues.CopyTo(dstSharedIndices, 0, srcChunk->Archetype->NumSharedComponents);
            dstManagedComponentStore.CopySharedComponents(srcManagedComponentStore, dstSharedIndices, srcChunk->Archetype->NumSharedComponents);

            // @TODO: Why don't we memcpy the whole chunk. So we include all extra fields???

            // Allocate a new chunk
            var srcArchetype = srcChunk->Archetype;
            var dstArchetype = dstEntityComponentStore->GetOrCreateArchetype(srcArchetype->Types, srcArchetype->TypesCount);

            var dstChunk = dstEntityComponentStore->GetCleanChunkNoMetaChunk(dstArchetype, dstSharedIndices);

            dstManagedComponentStore.Playback(ref dstEntityComponentStore->ManagedChangesTracker);

            dstChunk->metaChunkEntity = srcChunk->metaChunkEntity;

            // Release any references obtained by GetCleanChunk & CopySharedComponents
            for (var i = 0; i < srcChunk->Archetype->NumSharedComponents; i++)
            {
                dstManagedComponentStore.RemoveReference(dstSharedIndices[i]);
            }

            ChunkDataUtility.SetChunkCountKeepMetaChunk(dstChunk, srcChunk->Count);
            dstManagedComponentStore.Playback(ref dstEntityComponentStore->ManagedChangesTracker);

            dstChunk->Archetype->EntityCount += srcChunk->Count;

            var copySize = Chunk.GetChunkBufferSize();

            UnsafeUtility.MemCpy((byte *)dstChunk + Chunk.kBufferOffset, (byte *)srcChunk + Chunk.kBufferOffset, copySize);

            var numManagedComponents = dstChunk->Archetype->NumManagedComponents;
            var hasHybridComponents  = dstArchetype->HasHybridComponents;

            for (int t = 0; t < numManagedComponents; ++t)
            {
                int indexInArchetype = t + dstChunk->Archetype->FirstManagedComponent;

                if (hasHybridComponents)
                {
                    // We consider hybrid components as always different, there's no reason to clone those at this point
                    var typeCategory = TypeManager.GetTypeInfo(dstChunk->Archetype->Types[indexInArchetype].TypeIndex).Category;
                    if (typeCategory == TypeManager.TypeCategory.Class)
                    {
                        continue;
                    }
                }

                var offset = dstChunk->Archetype->Offsets[indexInArchetype];
                var a      = (int *)(dstChunk->Buffer + offset);

                dstManagedComponentStore.CloneManagedComponentsFromDifferentWorld(a, dstChunk->Count, srcManagedComponentStore, ref *dstAccess->EntityComponentStore);
            }

            BufferHeader.PatchAfterCloningChunk(dstChunk);
            dstChunk->SequenceNumber = srcChunk->SequenceNumber;

            return(dstChunk);
        }