Пример #1
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;
 }
Пример #2
0
        public AtomicSafetyHandle GetSafetyHandle(ComponentJobSafetyManager safetyManager, int indexInComponentGroup)
        {
            var type       = m_GroupData->RequiredComponents + indexInComponentGroup;
            var isReadOnly = type->AccessModeType == ComponentType.AccessMode.ReadOnly;

            return(safetyManager.GetSafetyHandle(type->TypeIndex, isReadOnly));
        }
Пример #3
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;
        }
Пример #4
0
        protected override void OnBeforeCreateManagerInternal(World world)
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            m_SystemID = World.AllocateSystemID();
#endif
            m_World         = world;
            m_EntityManager = world.GetOrCreateManager <EntityManager>();
            m_SafetyManager = m_EntityManager.ComponentJobSafetyManager;

            m_ComponentGroups = new ComponentGroup[0];
#if !UNITY_CSHARP_TINY
            m_CachedComponentGroupArrays = new ComponentGroupArrayStaticCache[0];
            m_AlwaysUpdateSystem         = GetType().GetCustomAttributes(typeof(AlwaysUpdateSystemAttribute), true).Length != 0;
#else
            m_AlwaysUpdateSystem = true;
#endif
            m_JobDependencyForReadingManagers = new NativeList <int>(10, Allocator.Persistent);
            m_JobDependencyForWritingManagers = new NativeList <int>(10, Allocator.Persistent);

#if !UNITY_ZEROPLAYER
            ComponentSystemInjection.Inject(this, world, m_EntityManager, out m_InjectedComponentGroups, out m_InjectFromEntityData);
            m_InjectFromEntityData.ExtractJobDependencyTypes(this);
#endif
            InjectNestedIJobProcessComponentDataJobs();

            UpdateInjectedComponentGroups();
        }
Пример #5
0
        public NativeArray <T> GetFixedArray <T>(Entity entity) where T : struct
        {
            var typeIndex = TypeManager.GetTypeIndex <T>();

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            m_Entities->AssertEntityHasComponent(entity, typeIndex);
            if (TypeManager.GetComponentType <T>().Category != TypeManager.TypeCategory.OtherValueType)
            {
                throw new ArgumentException($"GetComponentFixedArray<{typeof(T)}> may not be IComponentData or ISharedComponentData");
            }
#endif

            ComponentJobSafetyManager.CompleteWriteDependency(typeIndex);

            byte *ptr;
            int   length;
            m_Entities->GetComponentDataWithTypeAndFixedArrayLength(entity, typeIndex, out ptr, out length);

            var array = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray <T>(ptr, length, Allocator.Invalid);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref array, ComponentJobSafetyManager.GetSafetyHandle(typeIndex, false));
#endif

            return(array);
        }
Пример #6
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;
 }
Пример #7
0
        public ExclusiveEntityTransaction BeginExclusiveEntityTransaction()
        {
            ComponentJobSafetyManager.BeginExclusiveTransaction();
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            m_ExclusiveEntityTransaction.SetAtomicSafetyHandle(ComponentJobSafetyManager.ExclusiveTransactionSafety);
#endif
            return(m_ExclusiveEntityTransaction);
        }
Пример #8
0
        public FixedArrayFromEntity <T> GetFixedArrayFromEntity <T>(int typeIndex, bool isReadOnly = false) where T : struct
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            return(new FixedArrayFromEntity <T>(typeIndex, m_Entities, ComponentJobSafetyManager.GetSafetyHandle(typeIndex, isReadOnly)));
#else
            return(new FixedArrayFromEntity <T>(typeIndex, m_Entities));
#endif
        }
Пример #9
0
        internal ComponentDataFromEntity <T> GetComponentDataFromEntity <T>(int typeIndex, bool isReadOnly) where T : struct, IComponentData
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            return(new ComponentDataFromEntity <T>(typeIndex, m_Entities, ComponentJobSafetyManager.GetSafetyHandle(typeIndex, isReadOnly)));
#else
            return(new ComponentDataFromEntity <T>(typeIndex, m_Entities));
#endif
        }
Пример #10
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;
 }
Пример #11
0
        public ArchetypeChunkEntityType GetArchetypeChunkEntityType()
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            return(new ArchetypeChunkEntityType(
                       ComponentJobSafetyManager.GetSafetyHandle(TypeManager.GetTypeIndex <Entity>(), true)));
#else
            return(new ArchetypeChunkEntityType(false));
#endif
        }
Пример #12
0
        public ArchetypeChunkSharedComponentType <T> GetArchetypeChunkSharedComponentType <T>()
            where T : struct, ISharedComponentData
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            return(new ArchetypeChunkSharedComponentType <T>(
                       ComponentJobSafetyManager.GetSafetyHandle(TypeManager.GetTypeIndex <T>(), true)));
#else
            return(new ArchetypeChunkSharedComponentType <T>(false));
#endif
        }
        internal void SetComponentDataRaw(Entity entity, int typeIndex, void *data, int size)
        {
            m_Entities->AssertEntityHasComponent(entity, typeIndex);

            ComponentJobSafetyManager.CompleteReadAndWriteDependency(typeIndex);

            byte *ptr = m_Entities->GetComponentDataWithType(entity, typeIndex);

            UnsafeUtility.MemCpy(ptr, data, size);
        }
Пример #14
0
        internal void *GetComponentDataRawRW(Entity entity, int typeIndex)
        {
            Entities->AssertEntityHasComponent(entity, typeIndex);

            ComponentJobSafetyManager.CompleteReadAndWriteDependency(typeIndex);

            var ptr = Entities->GetComponentDataWithTypeRW(entity, typeIndex, Entities->GlobalSystemVersion);

            return(ptr);
        }
Пример #15
0
        internal void SetComponentDataRaw(Entity entity, int typeIndex, void *data, int size)
        {
            Entities->AssertEntityHasComponent(entity, typeIndex);

            ComponentJobSafetyManager.CompleteReadAndWriteDependency(typeIndex);

            var ptr = Entities->GetComponentDataWithTypeRW(entity, typeIndex, Entities->GlobalSystemVersion);

            UnsafeUtility.MemCpy(ptr, data, size);
        }
Пример #16
0
        internal void *GetBufferRawRW(Entity entity, int typeIndex)
        {
            Entities->AssertEntityHasComponent(entity, typeIndex);

            ComponentJobSafetyManager.CompleteReadAndWriteDependency(typeIndex);

            BufferHeader *header = (BufferHeader *)Entities->GetComponentDataWithTypeRW(entity, typeIndex, Entities->GlobalSystemVersion);

            return(BufferHeader.GetElementPointer(header));
        }
Пример #17
0
        void BeforeStructuralChange()
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            if (ComponentJobSafetyManager.IsInTransaction)
            {
                throw new InvalidOperationException("Access to EntityManager is not allowed after EntityManager.BeginExclusiveEntityTransaction(); has been called.");
            }
#endif
            ComponentJobSafetyManager.CompleteAllJobsAndInvalidateArrays();
        }
Пример #18
0
        internal int GetBufferLength(Entity entity, int typeIndex)
        {
            Entities->AssertEntityHasComponent(entity, typeIndex);

            ComponentJobSafetyManager.CompleteReadAndWriteDependency(typeIndex);

            BufferHeader *header = (BufferHeader *)Entities->GetComponentDataWithTypeRW(entity, typeIndex, Entities->GlobalSystemVersion);

            return(header->Length);
        }
        internal void *GetComponentDataRaw(Entity entity, int typeIndex)
        {
            m_Entities->AssertEntityHasComponent(entity, typeIndex);

            ComponentJobSafetyManager.CompleteReadAndWriteDependency(typeIndex);

            byte *ptr = m_Entities->GetComponentDataWithType(entity, typeIndex);

            return(ptr);
        }
Пример #20
0
        public BufferFromEntity <T> GetBufferFromEntity <T>(int typeIndex, bool isReadOnly = false)
            where T : struct, IBufferElementData
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            return(new BufferFromEntity <T>(typeIndex, Entities, isReadOnly,
                                            ComponentJobSafetyManager.GetSafetyHandle(typeIndex, isReadOnly),
                                            ComponentJobSafetyManager.GetBufferSafetyHandle(typeIndex)));
#else
            return(new BufferFromEntity <T>(typeIndex, m_Entities, isReadOnly));
#endif
        }
Пример #21
0
        public void SetComponentData <T>(Entity entity, T componentData) where T : struct, IComponentData
        {
            var typeIndex = TypeManager.GetTypeIndex <T>();

            m_Entities->AssertEntityHasComponent(entity, typeIndex);

            ComponentJobSafetyManager.CompleteReadAndWriteDependency(typeIndex);

            var ptr = m_Entities->GetComponentDataWithType(entity, typeIndex);

            UnsafeUtility.CopyStructureToPtr(ref componentData, ptr);
        }
Пример #22
0
        public ArchetypeChunkComponentType <T> GetArchetypeChunkComponentType <T>(bool isReadOnly)
            where T : struct, IComponentData
        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            var typeIndex = TypeManager.GetTypeIndex <T>();
            return(new ArchetypeChunkComponentType <T>(
                       ComponentJobSafetyManager.GetSafetyHandle(typeIndex, isReadOnly), isReadOnly,
                       GlobalSystemVersion));
#else
            return(new ArchetypeChunkComponentType <T>(isReadOnly, GlobalSystemVersion));
#endif
        }
Пример #23
0
        internal void SetBufferRaw(Entity entity, int componentTypeIndex, BufferHeader *tempBuffer, int sizeInChunk)
        {
            Entities->AssertEntityHasComponent(entity, componentTypeIndex);

            ComponentJobSafetyManager.CompleteReadAndWriteDependency(componentTypeIndex);

            var ptr = Entities->GetComponentDataWithTypeRW(entity, componentTypeIndex, Entities->GlobalSystemVersion);

            BufferHeader.Destroy((BufferHeader *)ptr);

            UnsafeUtility.MemCpy(ptr, tempBuffer, sizeInChunk);
        }
Пример #24
0
        public T GetComponentData <T>(Entity entity) where T : struct, IComponentData
        {
            var typeIndex = TypeManager.GetTypeIndex <T>();

            m_Entities->AssertEntityHasComponent(entity, typeIndex);
            ComponentJobSafetyManager.CompleteWriteDependency(typeIndex);

            var ptr = m_Entities->GetComponentDataWithType(entity, typeIndex);

            T value;

            UnsafeUtility.CopyPtrToStructure(ptr, out value);
            return(value);
        }
Пример #25
0
        internal void SetComponentDataRaw(Entity entity, int typeIndex, void *data, int size)
        {
            Entities->AssertEntityHasComponent(entity, typeIndex);

            ComponentJobSafetyManager.CompleteReadAndWriteDependency(typeIndex);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            if (TypeManager.GetTypeInfo(typeIndex).SizeInChunk != size)
            {
                throw new System.ArgumentException($"SetComponentDataRaw<{TypeManager.GetType(typeIndex)}> can not be called with a zero sized component and must have same size as sizeof(T).");
            }
#endif

            var ptr = Entities->GetComponentDataWithTypeRW(entity, typeIndex, Entities->GlobalSystemVersion);
            UnsafeUtility.MemCpy(ptr, data, size);
        }
Пример #26
0
        protected override void OnBeforeCreateManagerInternal(World world, int capacity)
        {
            m_World              = world;
            m_EntityManager      = world.GetOrCreateManager <EntityManager>();
            m_SafetyManager      = m_EntityManager.ComponentJobSafetyManager;
            m_AlwaysUpdateSystem = GetType().GetCustomAttributes(typeof(AlwaysUpdateSystemAttribute), true).Length != 0;

            m_ComponentGroups                 = new ComponentGroup[0];
            m_CachedComponentGroupArrays      = new ComponentGroupArrayStaticCache[0];
            m_JobDependencyForReadingManagers = new NativeList <int>(10, Allocator.Persistent);
            m_JobDependencyForWritingManagers = new NativeList <int>(10, Allocator.Persistent);

            ComponentSystemInjection.Inject(this, world, m_EntityManager, out m_InjectedComponentGroups, out m_InjectFromEntityData);
            m_InjectFromEntityData.ExtractJobDependencyTypes(this);

            UpdateInjectedComponentGroups();
        }
Пример #27
0
        internal void *GetComponentDataRawRW(Entity entity, int typeIndex)
        {
            Entities->AssertEntityHasComponent(entity, typeIndex);

            ComponentJobSafetyManager.CompleteReadAndWriteDependency(typeIndex);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            if (TypeManager.GetTypeInfo(typeIndex).IsZeroSized)
            {
                throw new System.ArgumentException($"GetComponentDataRaw<{TypeManager.GetType(typeIndex)}> can not be called with a zero sized component.");
            }
#endif


            var ptr = Entities->GetComponentDataWithTypeRW(entity, typeIndex, Entities->GlobalSystemVersion);
            return(ptr);
        }
        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);
        }
Пример #29
0
        public void SetComponentData <T>(Entity entity, T componentData) where T : struct, IComponentData
        {
            var typeIndex = TypeManager.GetTypeIndex <T>();

            Entities->AssertEntityHasComponent(entity, typeIndex);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            if (ComponentType.FromTypeIndex(typeIndex).IsZeroSized)
            {
                throw new System.ArgumentException($"GetComponentData<{typeof(T)}> can not be called with a zero sized component.");
            }
#endif

            ComponentJobSafetyManager.CompleteReadAndWriteDependency(typeIndex);

            var ptr = Entities->GetComponentDataWithTypeRW(entity, typeIndex, Entities->GlobalSystemVersion);
            UnsafeUtility.CopyStructureToPtr(ref componentData, ptr);
        }
Пример #30
0
        public T GetComponentData <T>(Entity entity) where T : struct, IComponentData
        {
            var typeIndex = TypeManager.GetTypeIndex <T>();

            Entities->AssertEntityHasComponent(entity, typeIndex);

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            if (ComponentType.FromTypeIndex(typeIndex).IsZeroSized)
            {
                throw new System.ArgumentException($"GetComponentData<{typeof(T)}> can not be called with a zero sized component.");
            }
#endif

            ComponentJobSafetyManager.CompleteWriteDependency(typeIndex);

            var ptr = Entities->GetComponentDataWithTypeRO(entity, typeIndex);

            T value;
            UnsafeUtility.CopyPtrToStructure(ptr, out value);
            return(value);
        }