internal ComponentDataFromEntity(int typeIndex, EntityDataManager *entityData) { m_TypeIndex = typeIndex; m_Entities = entityData; m_TypeLookupCache = 0; m_GlobalSystemVersion = entityData->GlobalSystemVersion; }
internal void OnDestroyManager() { m_ArchetypeManager.Free(); m_EntityGroupManager.Free(); m_SharedComponentDataManager.Free(); m_Entities = null; }
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)); }
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; }
private static void DeallocateBuffers(EntityDataManager *entityDataManager, Entity *entities, Chunk *chunk, int batchCount) { var archetype = chunk->Archetype; for (var ti = 0; ti < archetype->TypesCount; ++ti) { var type = archetype->Types[ti]; if (!type.IsBuffer) { continue; } var basePtr = chunk->Buffer + archetype->Offsets[ti]; var stride = archetype->SizeOfs[ti]; for (int i = 0; i < batchCount; ++i) { Entity e = entities[i]; EntityData ed = entityDataManager->m_Entities[e.Index]; int indexInChunk = ed.IndexInChunk; byte * bufferPtr = basePtr + stride * indexInChunk; BufferHeader.Destroy((BufferHeader *)bufferPtr); } } }
private static Chunk *EntityChunkBatch(EntityDataManager *entityDataManager, Entity *entities, int count, out int indexInChunk, out int batchCount) { /// This is optimized for the case where the array of entities are allocated contigously in the chunk /// Thus the compacting of other elements can be batched // Calculate baseEntityIndex & chunk var baseEntityIndex = entities[0].Index; var chunk = entityDataManager->m_Entities[baseEntityIndex].Chunk; indexInChunk = entityDataManager->m_Entities[baseEntityIndex].IndexInChunk; batchCount = 0; var entityDatas = entityDataManager->m_Entities; while (batchCount < count) { var entityIndex = entities[batchCount].Index; var data = entityDatas + entityIndex; if (data->Chunk != chunk || data->IndexInChunk != indexInChunk + batchCount) { break; } batchCount++; } return(chunk); }
internal ComponentGroup(ComponentGroup parentComponentGroup, ComponentGroupData componentGroupData) { m_ComponentGroupData = componentGroupData; m_SafetyManager = parentComponentGroup.m_SafetyManager; m_TypeManager = parentComponentGroup.m_TypeManager; m_EntityDataManager = parentComponentGroup.m_EntityDataManager; }
internal ComponentDataFromEntity(int typeIndex, EntityDataManager *entityData, AtomicSafetyHandle safety) { m_Safety = safety; m_TypeIndex = typeIndex; m_Entities = entityData; m_TypeLookupCache = 0; }
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)); }
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)); }
internal BufferFromEntity(int typeIndex, EntityDataManager *entityData, bool isReadOnly) { m_TypeIndex = typeIndex; m_Entities = entityData; m_IsReadOnly = isReadOnly; m_TypeLookupCache = 0; m_GlobalSystemVersion = entityData->GlobalSystemVersion; }
internal ComponentDataFromEntity(int typeIndex, EntityDataManager *entityData) { m_TypeIndex = typeIndex; m_Entities = entityData; m_TypeLookupCache = 0; m_GlobalSystemVersion = entityData->GlobalSystemVersion; m_IsZeroSized = ComponentType.FromTypeIndex(typeIndex).IsZeroSized; }
internal void OnDestroyManager() { UnsafeUtility.Free(m_CachedComponentTypeInArchetypeArray, Allocator.Persistent); m_ArchetypeManager.Free(); m_EntityGroupManager.Free(); m_SharedComponentDataManager.Free(); m_Entities = null; }
internal unsafe ExclusiveEntityTransaction(Unity.Entities.ArchetypeManager archetypes, EntityGroupManager entityGroupManager, Unity.Entities.SharedComponentDataManager sharedComponentDataManager, EntityDataManager *data) { this.m_Safety = new AtomicSafetyHandle(); this.m_Entities = data; this.m_ArchetypeManager = GCHandle.Alloc(archetypes, GCHandleType.Weak); this.m_EntityGroupManager = GCHandle.Alloc(entityGroupManager, GCHandleType.Weak); this.m_SharedComponentDataManager = GCHandle.Alloc(sharedComponentDataManager, GCHandleType.Weak); this.m_CachedComponentTypeInArchetypeArray = (ComponentTypeInArchetype *)UnsafeUtility.Malloc((long)((sizeof(ComponentTypeInArchetype) * 0x20) * 0x400), 0x10, Allocator.Persistent); }
internal unsafe ComponentDataFromEntity(int typeIndex, EntityDataManager *entityData, AtomicSafetyHandle safety) { this.m_Safety = safety; this.m_TypeIndex = typeIndex; this.m_Entities = entityData; this.m_TypeLookupCache = 0; this.m_GlobalSystemVersion = entityData.GlobalSystemVersion; this.m_IsZeroSized = ComponentType.FromTypeIndex(typeIndex).IsZeroSized; }
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; }
static Chunk *DeallocateDataEntitiesInChunk(EntityDataManager *entityDataManager, Entity *entities, int count, out int indexInChunk, out int batchCount) { /// This is optimized for the case where the array of entities are allocated contigously in the chunk /// Thus the compacting of other elements can be batched // Calculate baseEntityIndex & chunk var baseEntityIndex = entities[0].Index; var chunk = entityDataManager->m_Entities[baseEntityIndex].Chunk; indexInChunk = entityDataManager->m_Entities[baseEntityIndex].IndexInChunk; batchCount = 0; var freeIndex = entityDataManager->m_EntitiesFreeIndex; var entityDatas = entityDataManager->m_Entities; while (batchCount < count) { var entityIndex = entities[batchCount].Index; var data = entityDatas + entityIndex; if (data->Chunk != chunk || data->IndexInChunk != indexInChunk + batchCount) { break; } data->Chunk = null; data->Version++; data->IndexInChunk = freeIndex; freeIndex = entityIndex; batchCount++; } entityDataManager->m_EntitiesFreeIndex = freeIndex; // We can just chop-off the end, no need to copy anything if (chunk->Count == indexInChunk + batchCount) { return(chunk); } // updates EntitityData->indexInChunk to point to where the components will be moved to //Assert.IsTrue(chunk->archetype->sizeOfs[0] == sizeof(Entity) && chunk->archetype->offsets[0] == 0); var movedEntities = (Entity *)(chunk->Buffer) + (chunk->Count - batchCount); for (var i = 0; i != batchCount; i++) { entityDataManager->m_Entities[movedEntities[i].Index].IndexInChunk = indexInChunk + i; } // Move component data from the end to where we deleted components ChunkDataUtility.Copy(chunk, chunk->Count - batchCount, chunk, indexInChunk, batchCount); return(chunk); }
internal BufferFromEntity(int typeIndex, EntityDataManager *entityData, bool isReadOnly) { m_TypeIndex = typeIndex; m_Entities = entityData; m_IsReadOnly = isReadOnly; m_TypeLookupCache = 0; m_GlobalSystemVersion = entityData->GlobalSystemVersion; m_InternalCapacity = TypeManager.GetTypeInfo <T>().BufferCapacity; }
internal FixedArrayFromEntity(int typeIndex, EntityDataManager *entityData, AtomicSafetyHandle safety) { m_Safety = safety; m_TypeIndex = typeIndex; m_Entities = entityData; if (TypeManager.GetComponentType(m_TypeIndex).Category != TypeManager.TypeCategory.OtherValueType) { throw new ArgumentException($"GetComponentFixedArray<{typeof(T)}> may not be IComponentData or ISharedComponentData"); } }
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); }
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); }
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); }
internal BufferDataFromEntity(int typeIndex, EntityDataManager *entityData, bool isReadOnly, AtomicSafetyHandle safety) { m_Safety = safety; m_TypeIndex = typeIndex; m_Entities = entityData; m_IsReadOnly = isReadOnly; m_TypeLookupCache = 0; m_GlobalSystemVersion = entityData->GlobalSystemVersion; if (TypeManager.GetComponentType(m_TypeIndex).Category != TypeManager.TypeCategory.BufferData) { throw new ArgumentException( $"GetComponentBufferArray<{typeof(T)}> must be IBufferElementData"); } }
internal unsafe BufferFromEntity(int typeIndex, EntityDataManager *entityData, bool isReadOnly, AtomicSafetyHandle safety, AtomicSafetyHandle arrayInvalidationSafety) { this.m_Safety0 = safety; this.m_ArrayInvalidationSafety = arrayInvalidationSafety; this.m_SafetyReadOnlyCount = isReadOnly ? 2 : 0; this.m_SafetyReadWriteCount = isReadOnly ? 0 : 2; this.m_TypeIndex = typeIndex; this.m_Entities = entityData; this.m_IsReadOnly = isReadOnly; this.m_TypeLookupCache = 0; this.m_GlobalSystemVersion = entityData.GlobalSystemVersion; if (TypeManager.GetTypeInfo(this.m_TypeIndex).Category != TypeManager.TypeCategory.BufferData) { throw new ArgumentException($"GetComponentBufferArray<{typeof(T)}> must be IBufferElementData"); } }
public static void MoveChunks(ArchetypeManager srcArchetypeManager, EntityDataManager *srcEntityDataManager, SharedComponentDataManager srcSharedComponents, ArchetypeManager dstArchetypeManager, EntityGroupManager dstGroupManager, SharedComponentDataManager dstSharedComponentDataManager, EntityDataManager *dstEntityDataManager, SharedComponentDataManager dstSharedComponents) { var entitiesArray = new NativeArray <Entity>(Chunk.kMaximumEntitiesPerChunk, Allocator.Temp); var entitiesPtr = (Entity *)entitiesArray.GetUnsafePtr(); var srcArchetype = srcArchetypeManager.m_LastArchetype; while (srcArchetype != null) { if (srcArchetype->EntityCount != 0) { if (srcArchetype->NumManagedArrays != 0) { throw new ArgumentException("MoveEntitiesFrom is not supported with managed arrays"); } var dstArchetype = dstArchetypeManager.GetOrCreateArchetype(srcArchetype->Types, srcArchetype->TypesCount, dstGroupManager); for (var c = srcArchetype->ChunkList.Begin; c != srcArchetype->ChunkList.End; c = c->Next) { Chunk *chunk = (Chunk *)c; EntityDataManager.FreeDataEntitiesInChunk(srcEntityDataManager, chunk, chunk->Count); dstEntityDataManager->AllocateEntities(dstArchetype, chunk, 0, chunk->Count, entitiesPtr); chunk->Archetype = dstArchetype; if (dstArchetype->NumSharedComponents > 0) { dstSharedComponents.MoveSharedComponents(srcSharedComponents, chunk->SharedComponentValueArray, dstArchetype->NumSharedComponents); } } //@TODO: Patch Entity references in IComponentData... UnsafeLinkedListNode.InsertListBefore(dstArchetype->ChunkList.End, &srcArchetype->ChunkList); UnsafeLinkedListNode.InsertListBefore(dstArchetype->ChunkListWithEmptySlots.End, &srcArchetype->ChunkListWithEmptySlots); dstArchetype->EntityCount += srcArchetype->EntityCount; srcArchetype->EntityCount = 0; } srcArchetype = srcArchetype->PrevArchetype; } entitiesArray.Dispose(); }
protected override void OnCreateManager(int capacity) { TypeManager.Initialize(); m_Entities = (EntityDataManager *)UnsafeUtility.Malloc(sizeof(EntityDataManager), 64, Allocator.Persistent); m_Entities->OnCreate(capacity); m_SharedComponentManager = new SharedComponentDataManager(); m_ArchetypeManager = new ArchetypeManager(m_SharedComponentManager); ComponentJobSafetyManager = new ComponentJobSafetyManager(); m_GroupManager = new EntityGroupManager(ComponentJobSafetyManager); m_ExclusiveEntityTransaction = new ExclusiveEntityTransaction(m_ArchetypeManager, m_GroupManager, m_SharedComponentManager, m_Entities); m_CachedComponentTypeArray = (ComponentType *)UnsafeUtility.Malloc(sizeof(ComponentType) * 32 * 1024, 16, Allocator.Persistent); m_CachedComponentTypeInArchetypeArray = (ComponentTypeInArchetype *)UnsafeUtility.Malloc(sizeof(ComponentTypeInArchetype) * 32 * 1024, 16, Allocator.Persistent); }
public unsafe ComponentGroup CreateEntityGroup(ArchetypeManager typeMan, EntityDataManager *entityDataManager, ArchetypeQuery *archetypeQueries, int archetypeFiltersCount, ComponentType *requiredComponents, int requiredComponentsCount) { EntityGroupData *grp = (EntityGroupData *)ref this.m_GroupDataChunkAllocator.Allocate(sizeof(EntityGroupData), 8); grp->PrevGroup = this.m_LastGroupData; this.m_LastGroupData = grp; grp->RequiredComponentsCount = requiredComponentsCount; grp->RequiredComponents = requiredComponents; this.InitializeReaderWriter(grp, requiredComponents, requiredComponentsCount); grp->ArchetypeQuery = archetypeQueries; grp->ArchetypeQueryCount = archetypeFiltersCount; grp->FirstMatchingArchetype = null; grp->LastMatchingArchetype = null; for (Archetype *archetypePtr = typeMan.m_LastArchetype; archetypePtr != null; archetypePtr = archetypePtr->PrevArchetype) { this.AddArchetypeIfMatching(archetypePtr, grp); } return(new ComponentGroup(grp, this.m_JobSafetyManager, typeMan, entityDataManager)); }
internal BufferFromEntity(int typeIndex, EntityDataManager *entityData, bool isReadOnly, AtomicSafetyHandle safety, AtomicSafetyHandle arrayInvalidationSafety) { m_Safety0 = safety; m_ArrayInvalidationSafety = arrayInvalidationSafety; m_SafetyReadOnlyCount = isReadOnly ? 2 : 0; m_SafetyReadWriteCount = isReadOnly ? 0 : 2; m_TypeIndex = typeIndex; m_Entities = entityData; m_IsReadOnly = isReadOnly; m_TypeLookupCache = 0; m_GlobalSystemVersion = entityData->GlobalSystemVersion; if (!TypeManager.IsBuffer(m_TypeIndex)) { throw new ArgumentException( $"GetComponentBufferArray<{typeof(T)}> must be IBufferElementData"); } }
public static void FreeDataEntitiesInChunk(EntityDataManager *entityDataManager, Chunk *chunk, int count) { var freeIndex = entityDataManager->m_EntitiesFreeIndex; var entityDatas = entityDataManager->m_Entities; var chunkEntities = (Entity *)chunk->Buffer; for (var i = 0; i != count; i++) { var entityIndex = chunkEntities[i].Index; var data = entityDatas + entityIndex; data->Chunk = null; data->Version++; data->IndexInChunk = freeIndex; freeIndex = entityIndex; } entityDataManager->m_EntitiesFreeIndex = freeIndex; }
private static void DeallocateDataEntitiesInChunk(EntityDataManager *entityDataManager, Entity *entities, Chunk *chunk, int indexInChunk, int batchCount) { DeallocateBuffers(entityDataManager, entities, chunk, batchCount); var freeIndex = entityDataManager->m_EntitiesFreeIndex; var entityDatas = entityDataManager->m_Entities; for (var i = batchCount - 1; i >= 0; --i) { var entityIndex = entities[i].Index; var data = entityDatas + entityIndex; data->Chunk = null; data->Version++; data->IndexInChunk = freeIndex; freeIndex = entityIndex; } entityDataManager->m_EntitiesFreeIndex = freeIndex; // Compute the number of things that need to moved and patched. int patchCount = Math.Min(batchCount, chunk->Count - indexInChunk - batchCount); if (0 == patchCount) { return; } // updates EntitityData->indexInChunk to point to where the components will be moved to //Assert.IsTrue(chunk->archetype->sizeOfs[0] == sizeof(Entity) && chunk->archetype->offsets[0] == 0); var movedEntities = (Entity *)chunk->Buffer + (chunk->Count - patchCount); for (var i = 0; i != patchCount; i++) { entityDataManager->m_Entities[movedEntities[i].Index].IndexInChunk = indexInChunk + i; } // Move component data from the end to where we deleted components ChunkDataUtility.Copy(chunk, chunk->Count - patchCount, chunk, indexInChunk, patchCount); }