Ejemplo n.º 1
0
 internal ComponentGroup(ComponentGroup parentComponentGroup, ComponentGroupData componentGroupData)
 {
     m_ComponentGroupData = componentGroupData;
     m_SafetyManager      = parentComponentGroup.m_SafetyManager;
     m_TypeManager        = parentComponentGroup.m_TypeManager;
     m_EntityDataManager  = parentComponentGroup.m_EntityDataManager;
 }
 public unsafe object[] GetManagedObjectRange(ArchetypeManager typeMan, int cachedBeginIndex, int index, out int rangeStart, out int rangeLength)
 {
     object[] objArray = typeMan.GetManagedObjectRange(this.m_CurrentChunk, &this.m_CurrentMatchingArchetype.IndexInArchetype.FixedElementField[this.IndexInComponentGroup], out rangeStart, out rangeLength);
     rangeStart  += index - cachedBeginIndex;
     rangeLength -= index - cachedBeginIndex;
     return(objArray);
 }
        public void SetFilter(ArchetypeManager typeManager, ComponentGroupFilter filter)
        {
            Assert.IsTrue(filter.FilterCount <= 2 && filter.FilterCount >= 0);

            ResetFilter(typeManager);
            m_Filter = filter;
        }
Ejemplo n.º 4
0
        public unsafe bool AllSharedComponentReferencesAreFromChunks(ArchetypeManager archetypeManager)
        {
            var chunkCount = new NativeArray <int>(m_SharedComponentRefCount.Length, Allocator.Temp);

            var archetype = archetypeManager.m_LastArchetype;

            while (archetype != null)
            {
                for (var c = archetype->ChunkList.Begin; c != archetype->ChunkList.End; c = c->Next)
                {
                    var chunk = (Chunk *)c;

                    for (int i = 0; i < archetype->NumSharedComponents; ++i)
                    {
                        chunkCount[chunk->SharedComponentValueArray[i]] += 1;
                    }
                }

                archetype = archetype->PrevArchetype;
            }

            chunkCount[0] = 1;
            int cmp = UnsafeUtility.MemCmp(m_SharedComponentRefCount.GetUnsafePtr(), chunkCount.GetUnsafeReadOnlyPtr(), sizeof(int) * chunkCount.Length);

            chunkCount.Dispose();

            return(cmp == 0);
        }
Ejemplo n.º 5
0
        public void InstantiateEntities(ArchetypeManager archetypeManager,
                                        SharedComponentDataManager sharedComponentDataManager, EntityGroupManager groupManager, Entity srcEntity, Entity *outputEntities, int count, ComponentTypeInArchetype *componentTypeInArchetypeArray)
        {
            var srcIndex     = m_Entities[srcEntity.Index].IndexInChunk;
            var srcChunk     = m_Entities[srcEntity.Index].Chunk;
            var srcArchetype = GetArchetype(srcEntity);
            var dstArchetype = GetInstantiableArchetype(srcEntity, archetypeManager, groupManager, componentTypeInArchetypeArray);
            var srcSharedComponentDataIndices = GetComponentChunk(srcEntity)->SharedComponentValueArray;

            while (count != 0)
            {
                var chunk = archetypeManager.GetChunkWithEmptySlots(dstArchetype, srcSharedComponentDataIndices);
                int indexInChunk;
                var allocatedCount = archetypeManager.AllocateIntoChunk(chunk, count, out indexInChunk);

                ChunkDataUtility.ReplicateComponents(srcChunk, srcIndex, chunk, indexInChunk, allocatedCount);

                AllocateEntities(dstArchetype, chunk, indexInChunk, allocatedCount, outputEntities);

                outputEntities += allocatedCount;
                count          -= allocatedCount;
            }

            IncrementComponentOrderVersion(dstArchetype, srcChunk, sharedComponentDataManager);
        }
Ejemplo n.º 6
0
        public void SetSharedComponentDataIndex(ArchetypeManager archetypeManager, SharedComponentDataManager sharedComponentDataManager, Entity entity, int typeIndex, int newSharedComponentDataIndex)
        {
            var archetype = GetArchetype(entity);

            var indexInTypeArray = ChunkDataUtility.GetIndexInTypeArray(archetype, typeIndex);

            var srcChunk = GetComponentChunk(entity);
            var srcSharedComponentValueArray = srcChunk->SharedComponentValueArray;
            var sharedComponentOffset        = archetype->SharedComponentOffset[indexInTypeArray];
            var oldSharedComponentDataIndex  = srcSharedComponentValueArray[sharedComponentOffset];

            if (newSharedComponentDataIndex == oldSharedComponentDataIndex)
            {
                return;
            }

            var sharedComponentIndices        = stackalloc int[archetype->NumSharedComponents];
            var srcSharedComponentDataIndices = srcChunk->SharedComponentValueArray;

            ArchetypeManager.CopySharedComponentDataIndexArray(sharedComponentIndices, srcSharedComponentDataIndices, archetype->NumSharedComponents);
            sharedComponentIndices[sharedComponentOffset] = newSharedComponentDataIndex;

            var newChunk      = archetypeManager.GetChunkWithEmptySlots(archetype, sharedComponentIndices);
            var newChunkIndex = archetypeManager.AllocateIntoChunk(newChunk);

            IncrementComponentOrderVersion(archetype, srcChunk, sharedComponentDataManager);

            MoveEntityToChunk(archetypeManager, entity, newChunk, newChunkIndex);
        }
Ejemplo n.º 7
0
        public static void CopyManagedObjects(ArchetypeManager typeMan, Chunk *srcChunk, int srcStartIndex,
                                              Chunk *dstChunk, int dstStartIndex, int count)
        {
            var srcArch = srcChunk->Archetype;
            var dstArch = dstChunk->Archetype;

            var srcI = 0;
            var dstI = 0;

            while (srcI < srcArch->TypesCount && dstI < dstArch->TypesCount)
            {
                if (srcArch->Types[srcI] < dstArch->Types[dstI])
                {
                    ++srcI;
                }
                else if (srcArch->Types[srcI] > dstArch->Types[dstI])
                {
                    ++dstI;
                }
                else
                {
                    if (srcArch->ManagedArrayOffset[srcI] >= 0)
                    {
                        for (var i = 0; i < count; ++i)
                        {
                            var obj = typeMan.GetManagedObject(srcChunk, srcI, srcStartIndex + i);
                            typeMan.SetManagedObject(dstChunk, dstI, dstStartIndex + i, obj);
                        }
                    }

                    ++srcI;
                    ++dstI;
                }
            }
        }
Ejemplo n.º 8
0
        public unsafe bool AllSharedComponentReferencesAreFromChunks(ArchetypeManager archetypeManager)
        {
            var refCounts = new NativeArray <int>(m_SharedComponentRefCount.Length, Allocator.Temp);

            for (var i = archetypeManager.m_Archetypes.Count - 1; i >= 0; --i)
            {
                var archetype  = archetypeManager.m_Archetypes.p[i];
                var chunkCount = archetype->Chunks.Count;
                for (int j = 0; j < archetype->NumSharedComponents; ++j)
                {
                    var values = archetype->Chunks.GetSharedComponentValueArrayForType(j);
                    for (var ci = 0; ci < chunkCount; ++ci)
                    {
                        refCounts[values[ci]] += 1;
                    }
                }
            }

            refCounts[0] = 1;
            int cmp = UnsafeUtility.MemCmp(m_SharedComponentRefCount.GetUnsafePtr(), refCounts.GetUnsafeReadOnlyPtr(), sizeof(int) * refCounts.Length);

            refCounts.Dispose();

            return(cmp == 0);
        }
Ejemplo n.º 9
0
        protected override void OnDestroyManager()
        {
            EndExclusiveEntityTransaction();

            ComponentJobSafetyManager.PreDisposeCheck();

            // Clean up all entities. This is needed to free all internal buffer allocations so memory is not leaked.
            using (var allEntities = GetAllEntities())
            {
                DestroyEntity(allEntities);
            }

            ComponentJobSafetyManager.Dispose();
            ComponentJobSafetyManager = null;

            Entities->OnDestroy();
            UnsafeUtility.Free(Entities, Allocator.Persistent);
            Entities = null;
            ArchetypeManager.Dispose();
            ArchetypeManager = null;
            m_GroupManager.Dispose();
            m_GroupManager = null;
            m_ExclusiveEntityTransaction.OnDestroyManager();

            m_SharedComponentManager.Dispose();

            UnsafeUtility.Free(m_CachedComponentTypeArray, Allocator.Persistent);
            m_CachedComponentTypeArray = null;

            UnsafeUtility.Free(m_CachedComponentTypeInArchetypeArray, Allocator.Persistent);
            m_CachedComponentTypeInArchetypeArray = null;
        }
Ejemplo n.º 10
0
 internal ComponentGroup(EntityGroupData *groupData, ComponentJobSafetyManager safetyManager, ArchetypeManager typeManager, EntityDataManager *entityDataManager)
 {
     m_ComponentGroupData = new ComponentGroupData(groupData, entityDataManager);
     m_SafetyManager      = safetyManager;
     m_TypeManager        = typeManager;
     m_EntityDataManager  = entityDataManager;
 }
Ejemplo n.º 11
0
 internal ComponentArray(ComponentChunkIterator iterator, int length, ArchetypeManager typeMan)
 {
     m_Length           = length;
     m_Cache            = default(ComponentChunkCache);
     m_Iterator         = iterator;
     m_ArchetypeManager = typeMan;
 }
Ejemplo n.º 12
0
        public ComponentGroup CreateEntityGroup(ArchetypeManager typeMan, EntityDataManager *entityDataManager,
                                                ArchetypeQuery *archetypeQueries, int archetypeFiltersCount, ComponentType *requiredComponents, int requiredComponentsCount)
        {
            //@TODO: Validate that required types is subset of archetype filters all...

            var grp = (EntityGroupData *)m_GroupDataChunkAllocator.Allocate(sizeof(EntityGroupData), 8);

            grp->PrevGroup  = m_LastGroupData;
            m_LastGroupData = grp;
            grp->RequiredComponentsCount = requiredComponentsCount;
            grp->RequiredComponents      = requiredComponents;
            InitializeReaderWriter(grp, requiredComponents, requiredComponentsCount);

            grp->ArchetypeQuery         = archetypeQueries;
            grp->ArchetypeQueryCount    = archetypeFiltersCount;
            grp->FirstMatchingArchetype = null;
            grp->LastMatchingArchetype  = null;
            for (var i = typeMan.m_Archetypes.Count - 1; i >= 0; --i)
            {
                var archetype = typeMan.m_Archetypes.p[i];
                AddArchetypeIfMatching(archetype, grp);
            }

            return(new ComponentGroup(grp, m_JobSafetyManager, typeMan, entityDataManager));
        }
Ejemplo n.º 13
0
        public unsafe Chunk *GetChunkWithEmptySlots(int *sharedComponentDataIndices, int numSharedComponents)
        {
            uint  hashCode = this.GetHashCode(sharedComponentDataIndices, numSharedComponents);
            Node *buckets  = this.buckets + ((hashCode & this.hashMask) * sizeof(Node));
            Node *nodePtr2 = this.buckets + (this.hashMask * sizeof(Node));

            while (true)
            {
                Chunk *chunkFromEmptySlotNode;
                if (buckets.IsFree())
                {
                    chunkFromEmptySlotNode = null;
                }
                else
                {
                    if (!(!buckets.IsDeleted() && buckets.CheckEqual(hashCode, sharedComponentDataIndices, numSharedComponents)))
                    {
                        buckets++;
                        if (buckets <= nodePtr2)
                        {
                            continue;
                        }
                        buckets = this.buckets;
                        continue;
                    }
                    chunkFromEmptySlotNode = ArchetypeManager.GetChunkFromEmptySlotNode(buckets->list.Begin);
                }
                return(chunkFromEmptySlotNode);
            }
        }
        public object[] GetManagedObjectRange(ArchetypeManager typeMan, int cachedBeginIndex, int index, out int rangeStart, out int rangeLength)
        {
            var objs = typeMan.GetManagedObjectRange(m_CurrentChunk, m_CurrentMatchingArchetype->TypeIndexInArchetypeArray[IndexInComponentGroup], out rangeStart, out rangeLength);

            rangeStart  += index - cachedBeginIndex;
            rangeLength -= index - cachedBeginIndex;
            return(objs);
        }
Ejemplo n.º 15
0
        public ComponentGroup CreateEntityGroup(ArchetypeManager typeMan, EntityDataManager *entityDataManager, ComponentType *inRequiredComponents, int inRequiredComponentsCount)
        {
            ComponentType *requiredComponentPtr;
            int            requiredComponentCount;

            CreateRequiredComponents(inRequiredComponents, inRequiredComponentsCount, out requiredComponentPtr, out requiredComponentCount);
            return(CreateEntityGroup(typeMan, entityDataManager, CreateQuery(inRequiredComponents, inRequiredComponentsCount), 1, requiredComponentPtr, requiredComponentCount));
        }
        public unsafe ComponentGroup CreateEntityGroup(ArchetypeManager typeMan, EntityDataManager *entityDataManager, ComponentType[] requiredComponents)
        {
            ComponentType *typePtr;
            int            num;

            this.CreateRequiredComponents(requiredComponents, out typePtr, out num);
            return(this.CreateEntityGroup(typeMan, entityDataManager, this.CreateQuery(requiredComponents), 1, typePtr, num));
        }
Ejemplo n.º 17
0
 internal ComponentGroup(EntityGroupData *groupData, ComponentJobSafetyManager safetyManager, ArchetypeManager typeManager, EntityDataManager *entityDataManager)
 {
     m_GroupData         = groupData;
     m_EntityDataManager = entityDataManager;
     m_Filter            = default(ComponentGroupFilter);
     m_SafetyManager     = safetyManager;
     ArchetypeManager    = typeManager;
     EntityDataManager   = entityDataManager;
 }
Ejemplo n.º 18
0
        public void SetFilter(ArchetypeManager typeManager, ref ComponentGroupFilter filter)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            filter.AssertValid();
#endif
            var version = m_Filter.RequiredChangeVersion;
            ResetFilter(typeManager);
            m_Filter = filter;
            m_Filter.RequiredChangeVersion = version;
        }
Ejemplo n.º 19
0
        public void CheckInternalConsistency()
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            //@TODO: Validate from perspective of componentgroup...
            //@TODO: Validate shared component data refcounts...
            var entityCountEntityData       = Entities->CheckInternalConsistency();
            var entityCountArchetypeManager = ArchetypeManager.CheckInternalConsistency();

            Assert.AreEqual(entityCountEntityData, entityCountArchetypeManager);
#endif
        }
Ejemplo n.º 20
0
        internal void SetComponentObject(Entity entity, ComponentType componentType, object componentObject)
        {
            Entities->AssertEntityHasComponent(entity, componentType.TypeIndex);

            //@TODO
            Chunk *chunk;
            int    chunkIndex;

            Entities->GetComponentChunk(entity, out chunk, out chunkIndex);
            ArchetypeManager.SetManagedObject(chunk, componentType, chunkIndex, componentObject);
        }
        internal ExclusiveEntityTransaction(ArchetypeManager archetypes, EntityGroupManager entityGroupManager,
                                            SharedComponentDataManager sharedComponentDataManager, EntityDataManager *data)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            m_Safety = new AtomicSafetyHandle();
#endif
            m_Entities                   = data;
            m_ArchetypeManager           = GCHandle.Alloc(archetypes, GCHandleType.Weak);
            m_EntityGroupManager         = GCHandle.Alloc(entityGroupManager, GCHandleType.Weak);
            m_SharedComponentDataManager = GCHandle.Alloc(sharedComponentDataManager, GCHandleType.Weak);
        }
Ejemplo n.º 22
0
        public object[] GetManagedObjectRange(ArchetypeManager typeMan, int cachedBeginIndex, int index,
                                              out int rangeStart, out int rangeLength)
        {
            var objs = typeMan.GetManagedObjectRange(*m_CurrentChunk,
                                                     m_CurrentMatchingArchetype->IndexInArchetype[IndexInEntityQuery], out rangeStart,
                                                     out rangeLength);

            rangeStart  += index - cachedBeginIndex;
            rangeLength -= index - cachedBeginIndex;
            return(objs);
        }
Ejemplo n.º 23
0
        internal ExclusiveEntityTransaction(ArchetypeManager archetypes, EntityGroupManager entityGroupManager, SharedComponentDataManager sharedComponentDataManager, EntityDataManager *data)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            m_Safety = new AtomicSafetyHandle();
#endif
            m_Entities                   = data;
            m_ArchetypeManager           = GCHandle.Alloc(archetypes, GCHandleType.Weak);
            m_EntityGroupManager         = GCHandle.Alloc(entityGroupManager, GCHandleType.Weak);
            m_SharedComponentDataManager = GCHandle.Alloc(sharedComponentDataManager, GCHandleType.Weak);

            m_CachedComponentTypeInArchetypeArray = (ComponentTypeInArchetype *)UnsafeUtility.Malloc(sizeof(ComponentTypeInArchetype) * 32 * 1024, 16, Allocator.Persistent);
        }
        internal void ResetFilter(ArchetypeManager typeManager)
        {
            var filteredCount = m_Filter.FilterCount;

            fixed(int *sharedComponentIndexPtr = m_Filter.SharedComponentIndex)
            {
                for (var i = 0; i < filteredCount; ++i)
                {
                    typeManager.GetSharedComponentDataManager().RemoveReference(sharedComponentIndexPtr[i]);
                }
            }
        }
Ejemplo n.º 25
0
        public ComponentGroup CreateEntityGroupIfCached(ArchetypeManager typeMan, EntityDataManager *entityDataManager, ComponentType *requiredTypes,
                                                        int requiredCount)
        {
            var hash = HashUtility.Fletcher32((ushort *)requiredTypes,
                                              requiredCount * sizeof(ComponentType) / sizeof(short));
            EntityGroupData *grp = GetCachedGroupData(hash, requiredTypes, requiredCount);

            if (grp != null)
            {
                return(new ComponentGroup(grp, m_JobSafetyManager, typeMan, entityDataManager));
            }
            return(null);
        }
Ejemplo n.º 26
0
        void AddMultiple(UnsafeLinkedListNode *list)
        {
            var    firstChunk = ArchetypeManager.GetChunkFromEmptySlotNode(list->Begin);
            UInt32 hash       = GetHashCode(firstChunk->SharedComponentValueArray, firstChunk->Archetype->NumSharedComponents);

            int * sharedComponentDataIndices = firstChunk->SharedComponentValueArray;
            int   numSharedComponents        = firstChunk->Archetype->NumSharedComponents;
            Node *node     = &buckets[hash & hashMask];
            Node *lastNode = &buckets[hashMask];
            Node *freeNode = null;

            while (!node->IsFree())
            {
                if (!node->IsDeleted())
                {
                    if (node->CheckEqual(hash, sharedComponentDataIndices, numSharedComponents))
                    {
                        UnsafeLinkedListNode.InsertListBefore(node->list.End, list);
                        return;
                    }
                }
                else
                {
                    if (freeNode == null)
                    {
                        freeNode = node;
                    }
                }

                node = node + 1;
                if (node > lastNode)
                {
                    node = buckets;
                }
            }

            if (freeNode == null)
            {
                freeNode = node;
                --emptyNodes;
            }

            freeNode->hash = hash;
            UnsafeLinkedListNode.InitializeList(&freeNode->list);
            UnsafeLinkedListNode.InsertListBefore(freeNode->list.End, list);

            if (ShouldGrow(emptyNodes))
            {
                Grow();
            }
        }
Ejemplo n.º 27
0
        internal void MoveEntitiesFrom(EntityManager srcEntities, NativeArray <ArchetypeChunk> chunks, NativeArray <EntityRemapUtility.EntityRemapInfo> entityRemapping)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            if (srcEntities == this)
            {
                throw new ArgumentException("srcEntities must not be the same as this EntityManager.");
            }
#endif

            BeforeStructuralChange();
            srcEntities.BeforeStructuralChange();

            ArchetypeManager.MoveChunks(srcEntities, chunks, ArchetypeManager, m_GroupManager, Entities, m_SharedComponentManager, m_CachedComponentTypeInArchetypeArray, entityRemapping);
        }
Ejemplo n.º 28
0
        internal ComponentGroupData GetVariation <SharedComponent1>(ArchetypeManager typeManager, SharedComponent1 sharedComponent1)
            where SharedComponent1 : struct, ISharedComponentData
        {
            var       componentIndex1 = GetIndexInComponentGroup(TypeManager.GetTypeIndex <SharedComponent1>());
            const int filteredCount   = 1;

            var filtered = (int *)UnsafeUtility.Malloc((filteredCount * 2 + 1) * sizeof(int), sizeof(int), Allocator.Temp);

            filtered[0] = filteredCount;
            filtered[1] = componentIndex1;
            filtered[2] = typeManager.GetSharedComponentDataManager().InsertSharedComponent(sharedComponent1);

            return(new ComponentGroupData(this, filtered));
        }
Ejemplo n.º 29
0
        public void MoveEntitiesFrom(EntityManager srcEntities)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            if (srcEntities == this)
            {
                throw new ArgumentException("srcEntities must not be the same as this EntityManager.");
            }
#endif

            BeforeStructuralChange();
            srcEntities.BeforeStructuralChange();

            ArchetypeManager.MoveChunks(srcEntities.m_ArchetypeManager, srcEntities.m_Entities, srcEntities.m_SharedComponentManager, m_ArchetypeManager, m_GroupManager, m_SharedComponentManager, m_Entities, m_SharedComponentManager);

            //@TODO: Need to incrmeent the component versions based the moved chunks...
        }
Ejemplo n.º 30
0
        public void CreateEntities(ArchetypeManager archetypeManager, Archetype *archetype, Entity *entities, int count)
        {
            while (count != 0)
            {
                var chunk = archetypeManager.GetChunkWithEmptySlots(archetype, null);
                int allocatedIndex;
                var allocatedCount = archetypeManager.AllocateIntoChunk(chunk, count, out allocatedIndex);
                AllocateEntities(archetype, chunk, allocatedIndex, allocatedCount, entities);
                ChunkDataUtility.ClearComponents(chunk, allocatedIndex, allocatedCount);

                entities += allocatedCount;
                count    -= allocatedCount;
            }

            IncrementComponentTypeOrderVersion(archetype);
        }