public NativeArray <int> MoveAllSharedComponents(ManagedComponentStore srcManagedComponents, Allocator allocator)
        {
            var remap = new NativeArray <int>(srcManagedComponents.GetSharedComponentCount(), allocator);

            remap[0] = 0;

            var srcInfos = srcManagedComponents.SharedComponentInfoPtr;

            for (int srcIndex = 1; srcIndex < remap.Length; ++srcIndex)
            {
                var srcData = srcManagedComponents.m_SharedComponentData[srcIndex];
                if (srcData == null)
                {
                    continue;
                }

                var typeIndex = srcInfos[srcIndex].ComponentType;
                var hashCode  = srcInfos[srcIndex].HashCode;
                var dstIndex  = InsertSharedComponentAssumeNonDefaultMove(typeIndex, hashCode, srcData);

                SharedComponentInfoPtr[dstIndex].RefCount += srcInfos[srcIndex].RefCount - 1;

                remap[srcIndex] = dstIndex;
            }

            srcManagedComponents.ResetSharedComponentData();

            return(remap);
        }
        public unsafe NativeArray <int> MoveAllSharedComponents(ManagedComponentStore srcManagedComponents,
                                                                Allocator allocator)
        {
            var remap = new NativeArray <int>(srcManagedComponents.GetSharedComponentCount(), allocator);

            remap[0] = 0;

            for (int srcIndex = 1; srcIndex < remap.Length; ++srcIndex)
            {
                var srcData = srcManagedComponents.m_SharedComponentData[srcIndex];
                if (srcData == null)
                {
                    continue;
                }

                var typeIndex = srcManagedComponents.m_SharedComponentType[srcIndex];

                var hashCode = TypeManager.GetHashCode(srcData, typeIndex);
                var dstIndex = InsertSharedComponentAssumeNonDefaultMove(typeIndex, hashCode, srcData);

                m_SharedComponentRefCount[dstIndex] += srcManagedComponents.m_SharedComponentRefCount[srcIndex] - 1;

                remap[srcIndex] = dstIndex;
            }

            srcManagedComponents.m_HashLookup.Clear();
            srcManagedComponents.m_SharedComponentVersion.ResizeUninitialized(1);
            srcManagedComponents.m_SharedComponentRefCount.ResizeUninitialized(1);
            srcManagedComponents.m_SharedComponentType.ResizeUninitialized(1);
            srcManagedComponents.m_SharedComponentData.Clear();
            srcManagedComponents.m_SharedComponentData.Add(null);
            srcManagedComponents.m_FreeListIndex = -1;

            return(remap);
        }
Beispiel #3
0
        public NativeArray <int> MoveSharedComponents(ManagedComponentStore srcManagedComponents, NativeArray <ArchetypeChunk> chunks, Allocator allocator)
        {
            var remap    = new NativeArray <int>(srcManagedComponents.GetSharedComponentCount(), allocator);
            var remapPtr = (int *)remap.GetUnsafePtr();

            // Build a map of all shared component values that will be moved
            // remap will have a refcount of how many chunks reference the shared component after this loop
            for (int i = 0; i < chunks.Length; ++i)
            {
                var chunk                 = chunks[i].m_Chunk;
                var archetype             = chunk->Archetype;
                var sharedComponentValues = chunk->SharedComponentValues;
                for (int sharedComponentIndex = 0; sharedComponentIndex < archetype->NumSharedComponents; ++sharedComponentIndex)
                {
                    remapPtr[sharedComponentValues[sharedComponentIndex]]++;
                }
            }

            remap[0] = 0;

            // Move all shared components that are being referenced
            // remap will have a remap table of src SharedComponentDataIndex -> dst SharedComponentDataIndex
            var srcInfos = srcManagedComponents.SharedComponentInfoPtr;

            for (int srcIndex = 1; srcIndex < remap.Length; ++srcIndex)
            {
                if (remapPtr[srcIndex] == 0)
                {
                    continue;
                }

                var srcData   = srcManagedComponents.m_SharedComponentData[srcIndex];
                var typeIndex = srcInfos[srcIndex].ComponentType;
                var hashCode  = srcInfos[srcIndex].HashCode;

                var dstIndex = InsertSharedComponentAssumeNonDefault(typeIndex, hashCode, srcData);

                // * remove refcount based on refcount table
                // * -1 because InsertSharedComponentAssumeNonDefault above adds 1 refcount
                int srcRefCount = remapPtr[srcIndex];
                SharedComponentInfoPtr[dstIndex].RefCount += srcRefCount - 1;
                srcManagedComponents.RemoveReference(srcIndex, srcRefCount);
                SharedComponentInfoPtr[dstIndex].Version++;

                remapPtr[srcIndex] = dstIndex;
            }

            return(remap);
        }
        public unsafe NativeArray <int> MoveSharedComponents(ManagedComponentStore srcManagedComponents,
                                                             NativeArray <ArchetypeChunk> chunks, NativeArray <EntityRemapUtility.EntityRemapInfo> remapInfos,
                                                             Allocator allocator)
        {
            var remap = new NativeArray <int>(srcManagedComponents.GetSharedComponentCount(), allocator);

            for (int i = 0; i < chunks.Length; ++i)
            {
                var chunk                 = chunks[i].m_Chunk;
                var archetype             = chunk->Archetype;
                var sharedComponentValues = chunk->SharedComponentValues;
                for (int sharedComponentIndex = 0;
                     sharedComponentIndex < archetype->NumSharedComponents;
                     ++sharedComponentIndex)
                {
                    remap[sharedComponentValues[sharedComponentIndex]]++;
                }
            }

            remap[0] = 0;

            for (int srcIndex = 1; srcIndex < remap.Length; ++srcIndex)
            {
                if (remap[srcIndex] == 0)
                {
                    continue;
                }

                var srcData   = srcManagedComponents.m_SharedComponentData[srcIndex];
                var typeIndex = srcManagedComponents.m_SharedComponentType[srcIndex];

                var hashCode = TypeManager.GetHashCode(srcData, typeIndex);

                var dstIndex = InsertSharedComponentAssumeNonDefault(typeIndex, hashCode, srcData);

                m_SharedComponentRefCount[dstIndex] += remap[srcIndex] - 1;
                srcManagedComponents.RemoveReference(srcIndex, remap[srcIndex]);

                remap[srcIndex] = dstIndex;
            }

            return(remap);
        }