Пример #1
0
        internal Chunk *GetExistingChunkWithEmptySlots(SharedComponentValues sharedComponentValues)
        {
            if (NumSharedComponents == 0)
            {
                if (ChunksWithEmptySlots.Length != 0)
                {
                    var chunk = ChunksWithEmptySlots.Ptr[0];
                    Assert.AreNotEqual(chunk->Count, chunk->Capacity);
                    return(chunk);
                }
            }
            else
            {
                var chunk = FreeChunksBySharedComponents.TryGet(sharedComponentValues, NumSharedComponents);
                if (chunk != null)
                {
                    return(chunk);
                }
            }

            return(null);
        }
Пример #2
0
        public void AddToChunkList(Chunk *chunk, SharedComponentValues sharedComponentIndices, uint changeVersion)
        {
            chunk->ListIndex = Chunks.Count;
            if (Chunks.Count == Chunks.Capacity)
            {
                int newCapacity = Chunks.Capacity == 0 ? 1 : Chunks.Capacity * 2;
                if (Chunks.data <= sharedComponentIndices.firstIndex &&
                    sharedComponentIndices.firstIndex < Chunks.data + Chunks.Count)
                {
                    int sourceChunk = (int)(sharedComponentIndices.firstIndex - Chunks.data);
                    // The shared component indices we are inserting belong to the same archetype so they need to be adjusted after reallocation
                    Chunks.Grow(newCapacity);
                    sharedComponentIndices = Chunks.GetSharedComponentValues(sourceChunk);
                }
                else
                {
                    Chunks.Grow(newCapacity);
                }
            }

            Chunks.Add(chunk, sharedComponentIndices, changeVersion);
        }
Пример #3
0
        public void AddToChunkList(Chunk *chunk, SharedComponentValues sharedComponentIndices, uint changeVersion)
        {
            chunk->ListIndex = Chunks.Count;
            if (Chunks.Count == Chunks.Capacity)
            {
                var newCapacity = (Chunks.Capacity == 0) ? 1 : (Chunks.Capacity * 2);

                // The shared component indices we are inserting belong to the same archetype so they need to be adjusted after reallocation
                if (Chunks.InsideAllocation((ulong)sharedComponentIndices.firstIndex))
                {
                    int chunkIndex = (int)(sharedComponentIndices.firstIndex - Chunks.GetSharedComponentValueArrayForType(0));
                    Chunks.Grow(newCapacity);
                    sharedComponentIndices = Chunks.GetSharedComponentValues(chunkIndex);
                }
                else
                {
                    Chunks.Grow(newCapacity);
                }
            }

            Chunks.Add(chunk, sharedComponentIndices, changeVersion);
        }
Пример #4
0
        void ConstructChunk(Archetype *archetype, Chunk *chunk, SharedComponentValues sharedComponentValues)
        {
            chunk->Archetype       = archetype;
            chunk->Count           = 0;
            chunk->Capacity        = archetype->ChunkCapacity;
            chunk->SequenceNumber  = AssignSequenceNumber(chunk);
            chunk->metaChunkEntity = Entity.Null;

            var numSharedComponents = archetype->NumSharedComponents;

            if (numSharedComponents > 0)
            {
                for (var i = 0; i < archetype->NumSharedComponents; ++i)
                {
                    var sharedComponentIndex = sharedComponentValues[i];
                    ManagedChangesTracker.AddReference(sharedComponentIndex);
                }
            }

            archetype->AddToChunkList(chunk, sharedComponentValues, GlobalSystemVersion);

            Assert.IsTrue(archetype->Chunks.Count != 0);

            // Chunk can't be locked at at construction time
            archetype->EmptySlotTrackingAddChunk(chunk);

            if (numSharedComponents == 0)
            {
                Assert.IsTrue(archetype->ChunksWithEmptySlots.Length != 0);
            }
            else
            {
                Assert.IsTrue(archetype->FreeChunksBySharedComponents.TryGet(chunk->SharedComponentValues,
                                                                             archetype->NumSharedComponents) != null);
            }

            chunk->Flags = 0;
        }
Пример #5
0
        public void Add(Chunk *chunk, SharedComponentValues sharedComponentIndices)
        {
            var chunkIndex = Count++;

            p[chunkIndex] = chunk;

            int *dst = data + chunkIndex;
            int  i   = 0;

            for (; i < SharedComponentCount; ++i)
            {
                *dst = sharedComponentIndices[i];
                dst += Capacity;
            }

            for (; i < EntityCountIndex; ++i)
            {
                *dst = 0;
                dst += Capacity;
            }

            *dst = chunk->Count;
        }
Пример #6
0
        static uint GetHashCode(SharedComponentValues sharedComponentValues, int numSharedComponents)
        {
            UInt32 result;

            if (sharedComponentValues.stride == sizeof(int))
            {
                result = math.hash(sharedComponentValues.firstIndex, numSharedComponents * sizeof(int));
            }
            else
            {
                int *indexArray = stackalloc int[numSharedComponents];
                for (int i = 0; i < numSharedComponents; ++i)
                {
                    indexArray[i] = sharedComponentValues[i];
                }
                result = math.hash(indexArray, numSharedComponents * sizeof(int));
            }

            if (result == 0 || result == kSkipCode)
            {
                result = kAValidHashCode;
            }
            return(result);
        }
Пример #7
0
        public void Move(Entity entity, Archetype *archetype, SharedComponentValues sharedComponentValues)
        {
            var archetypeChunkFilter = new ArchetypeChunkFilter(archetype, sharedComponentValues);

            Move(entity, ref archetypeChunkFilter);
        }
Пример #8
0
        static int InstantiateEntitiesOne(Entity srcEntity, Entity *outputEntities,
                                          int instanceCount, InstantiateRemapChunk *remapChunks, int remapChunksCount,
                                          EntityComponentStore *entityComponentStore,
                                          ManagedComponentStore managedComponentStore)
        {
            var src          = entityComponentStore->GetEntityInChunk(srcEntity);
            var srcArchetype = src.Chunk->Archetype;
            var dstArchetype = srcArchetype->InstantiableArchetype;

            var temp = stackalloc int[dstArchetype->NumSharedComponents];

            if (RequiresBuildingResidueSharedComponentIndices(srcArchetype, dstArchetype))
            {
                BuildResidueSharedComponentIndices(srcArchetype, dstArchetype,
                                                   src.Chunk->SharedComponentValues, temp);
            }
            else
            {
                // Always copy shared component indices since GetChunkWithEmptySlots might reallocate the storage of SharedComponentValues
                src.Chunk->SharedComponentValues.CopyTo(temp, 0, dstArchetype->NumSharedComponents);
            }

            SharedComponentValues sharedComponentValues = temp;

            Chunk *chunk = null;

            int instanceBeginIndex = 0;

            while (instanceBeginIndex != instanceCount)
            {
                chunk = GetChunkWithEmptySlots(dstArchetype, sharedComponentValues,
                                               entityComponentStore, managedComponentStore);

                int indexInChunk;
                var allocatedCount = AllocateIntoChunk(chunk, instanceCount - instanceBeginIndex, out indexInChunk,
                                                       entityComponentStore, managedComponentStore);

                ChunkDataUtility.ReplicateComponents(src.Chunk, src.IndexInChunk, chunk, indexInChunk, allocatedCount);
                entityComponentStore->AllocateEntities(dstArchetype, chunk, indexInChunk, allocatedCount,
                                                       outputEntities + instanceBeginIndex);
                chunk->SetAllChangeVersions(entityComponentStore->GlobalSystemVersion);

#if UNITY_EDITOR
                for (var i = 0; i < allocatedCount; ++i)
                {
                    entityComponentStore->SetName(outputEntities[i + instanceBeginIndex], entityComponentStore->GetName(srcEntity));
                }
#endif

                if (remapChunks != null)
                {
                    remapChunks[remapChunksCount].Chunk              = chunk;
                    remapChunks[remapChunksCount].IndexInChunk       = indexInChunk;
                    remapChunks[remapChunksCount].AllocatedCount     = allocatedCount;
                    remapChunks[remapChunksCount].InstanceBeginIndex = instanceBeginIndex;
                    remapChunksCount++;
                }


                instanceBeginIndex += allocatedCount;
            }

            if (chunk != null)
            {
                managedComponentStore.IncrementComponentOrderVersion(dstArchetype, chunk->SharedComponentValues);
                entityComponentStore->IncrementComponentTypeOrderVersion(dstArchetype);
            }

            return(remapChunksCount);
        }