예제 #1
0
        unsafe internal static void Initialize(ComponentSystemBase system, Type jobType, Type wrapperJobType, bool isParallelFor, ref JobProcessComponentDataCache cache, out ProcessIterationData iterator)
        {
            if ((isParallelFor && cache.JobReflectionDataParallelFor == IntPtr.Zero) ||
                (!isParallelFor && cache.JobReflectionData == IntPtr.Zero))
            {
                var iType = GetIJobProcessComponentDataInterface(jobType);
                if (cache.Types == null)
                {
                    cache.Types = GetComponentTypes(jobType, iType, out cache.ProcessTypesCount);
                }

                var res = GetJobReflection(jobType, wrapperJobType, iType, isParallelFor);

                if (isParallelFor)
                {
                    cache.JobReflectionDataParallelFor = res;
                }
                else
                {
                    cache.JobReflectionData = res;
                }
            }

            if (cache.ComponentSystem != system)
            {
                cache.ComponentGroup  = system.GetComponentGroup(cache.Types);
                cache.ComponentSystem = system;
            }

            var group = cache.ComponentGroup;

            // Readonly
            iterator.IsReadOnly0 = iterator.IsReadOnly1 = iterator.IsReadOnly2 = 0;
            fixed(int *isReadOnly = &iterator.IsReadOnly0)
            {
                for (var i = 0; i != cache.ProcessTypesCount; i++)
                {
                    isReadOnly[i] = cache.Types[i].AccessModeType == ComponentType.AccessMode.ReadOnly ? 1 : 0;
                }
            }

            // Iterator & length
            iterator.Iterator0 = default(ComponentChunkIterator);
            iterator.Iterator1 = default(ComponentChunkIterator);
            iterator.Iterator2 = default(ComponentChunkIterator);
            var length = -1;

            fixed(ComponentChunkIterator *iterators = &iterator.Iterator0)
            {
                for (var i = 0; i != cache.ProcessTypesCount; i++)
                {
                    group.GetComponentChunkIterator(out length, out iterators[i]);
                    iterators[i].IndexInComponentGroup = group.GetIndexInComponentGroup(cache.Types[i].TypeIndex);
                }
            }

            iterator.m_IsParallelFor = isParallelFor;
            iterator.m_Length        = length;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            iterator.m_MaxIndex = length - 1;
            iterator.m_MinIndex = 0;

            // Safety
            iterator.m_Safety0 = iterator.m_Safety1 = iterator.m_Safety2 = default(AtomicSafetyHandle);

            iterator.m_SafetyReadOnlyCount = 0;
            fixed(AtomicSafetyHandle *safety = &iterator.m_Safety0)
            {
                for (var i = 0; i != cache.ProcessTypesCount; i++)
                {
                    if (cache.Types[i].AccessModeType == ComponentType.AccessMode.ReadOnly)
                    {
                        safety[iterator.m_SafetyReadOnlyCount] = group.GetSafetyHandle(group.GetIndexInComponentGroup(cache.Types[i].TypeIndex));
                        iterator.m_SafetyReadOnlyCount++;
                    }
                }
            }

            iterator.m_SafetyReadWriteCount = 0;
            fixed(AtomicSafetyHandle *safety = &iterator.m_Safety0)
            {
                for (var i = 0; i != cache.ProcessTypesCount; i++)
                {
                    if (cache.Types[i].AccessModeType == ComponentType.AccessMode.ReadWrite)
                    {
                        safety[iterator.m_SafetyReadOnlyCount + iterator.m_SafetyReadWriteCount] = group.GetSafetyHandle(group.GetIndexInComponentGroup(cache.Types[i].TypeIndex));
                        iterator.m_SafetyReadWriteCount++;
                    }
                }
            }

            Assert.AreEqual(cache.ProcessTypesCount, iterator.m_SafetyReadWriteCount + iterator.m_SafetyReadOnlyCount);
#endif
        }
예제 #2
0
        static unsafe void Initialize <T>(ComponentSystemBase system, EntityQuery entityQuery, Type jobType, Type wrapperJobType,
                                          bool isParallelFor, ref JobForEachCache cache, out ProcessIterationData iterator, ref T jobData)
            where T : struct
#if UNITY_DOTSPLAYER
        , IBaseJobForEach
#endif
        {
            // Get the job reflection data and cache it if we don't already have it cached.
            if (isParallelFor && cache.JobReflectionDataParallelFor == IntPtr.Zero ||
                !isParallelFor && cache.JobReflectionData == IntPtr.Zero)
            {
#if UNITY_DOTSPLAYER
                if (cache.Types == null)
                {
                    cache.Types = jobData.GetComponentTypes(out cache.ProcessTypesCount, out cache.FilterChanged);
                }
                var res = jobData.GetJobReflection(isParallelFor);
#else
                var iType = GetIJobForEachInterface(jobType);
                if (cache.Types == null)
                {
                    cache.Types = GetComponentTypes(jobType, iType, out cache.ProcessTypesCount,
                                                    out cache.FilterChanged);
                }

                var res = GetJobReflection(jobType, wrapperJobType, iType, isParallelFor);
#endif
                if (isParallelFor)
                {
                    cache.JobReflectionDataParallelFor = res;
                }
                else
                {
                    cache.JobReflectionData = res;
                }
            }

            // Update cached EntityQuery and ComponentSystem data.
            if (system != null)
            {
                if (cache.ComponentSystem != system)
                {
                    cache.EntityQuery = system.GetEntityQueryInternal(cache.Types);

                    // If the cached filter has changed, update the newly cached EntityQuery with those changes.
                    if (cache.FilterChanged.Length != 0)
                    {
                        cache.EntityQuery.SetChangedVersionFilter(cache.FilterChanged);
                    }

                    // Otherwise, just reset our newly cached EntityQuery's filter.
                    else
                    {
                        cache.EntityQuery.ResetFilter();
                    }

                    cache.ComponentSystem = system;
                }
            }
            else if (entityQuery != default)
            {
                if (cache.EntityQuery != entityQuery)
                {
                    // Cache the new EntityQuery and cache that our system is null.
                    cache.EntityQuery     = entityQuery;
                    cache.ComponentSystem = null;
                }
            }

            var query = cache.EntityQuery;

            iterator.IsReadOnly0 = iterator.IsReadOnly1 = iterator.IsReadOnly2 = iterator.IsReadOnly3 = iterator.IsReadOnly4 = iterator.IsReadOnly5 = 0;
            fixed(int *isReadOnly = &iterator.IsReadOnly0)
            {
                for (var i = 0; i != cache.ProcessTypesCount; i++)
                {
                    isReadOnly[i] = cache.Types[i].AccessModeType == ComponentType.AccessMode.ReadOnly ? 1 : 0;
                }
            }

            iterator.TypeIndex0 = iterator.TypeIndex1 = iterator.TypeIndex2 = iterator.TypeIndex3 = iterator.TypeIndex4 = iterator.TypeIndex5 = -1;
            fixed(int *typeIndices = &iterator.TypeIndex0)
            {
                for (var i = 0; i != cache.ProcessTypesCount; i++)
                {
                    typeIndices[i] = cache.Types[i].TypeIndex;
                }
            }

            iterator.m_IsParallelFor = isParallelFor;
            iterator.m_Length        = query.CalculateChunkCountWithoutFiltering();

            iterator.GlobalSystemVersion = query._GetImpl()->_Access->EntityComponentStore->GlobalSystemVersion;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            iterator.m_MaxIndex = iterator.m_Length - 1;
            iterator.m_MinIndex = 0;

            iterator.m_Safety0     = iterator.m_Safety1 = iterator.m_Safety2 = iterator.m_Safety3 = iterator.m_Safety4 = iterator.m_Safety5 =
                iterator.m_Safety6 = iterator.m_Safety7 = iterator.m_Safety8 = iterator.m_Safety9 = iterator.m_Safety10 = iterator.m_Safety11 = default(AtomicSafetyHandle);

            var bufferTypeCount = 0;
            iterator.m_SafetyReadOnlyCount = 0;
            fixed(AtomicSafetyHandle *safety = &iterator.m_Safety0)
            {
                for (var i = 0; i != cache.ProcessTypesCount; i++)
                {
                    if (cache.Types[i].AccessModeType == ComponentType.AccessMode.ReadOnly)
                    {
                        safety[iterator.m_SafetyReadOnlyCount] = query.GetSafetyHandle(query.GetIndexInEntityQuery(cache.Types[i].TypeIndex));
                        iterator.m_SafetyReadOnlyCount++;
                        if (cache.Types[i].IsBuffer)
                        {
                            safety[iterator.m_SafetyReadOnlyCount] = query.GetBufferSafetyHandle(query.GetIndexInEntityQuery(cache.Types[i].TypeIndex));
                            iterator.m_SafetyReadOnlyCount++;
                            bufferTypeCount++;
                        }
                    }
                }
            }

            iterator.m_SafetyReadWriteCount = 0;
            fixed(AtomicSafetyHandle *safety = &iterator.m_Safety0)
            {
                for (var i = 0; i != cache.ProcessTypesCount; i++)
                {
                    if (cache.Types[i].AccessModeType == ComponentType.AccessMode.ReadWrite)
                    {
                        safety[iterator.m_SafetyReadOnlyCount + iterator.m_SafetyReadWriteCount] = query.GetSafetyHandle(query.GetIndexInEntityQuery(cache.Types[i].TypeIndex));
                        iterator.m_SafetyReadWriteCount++;
                        if (cache.Types[i].IsBuffer)
                        {
                            safety[iterator.m_SafetyReadOnlyCount + iterator.m_SafetyReadWriteCount] = query.GetBufferSafetyHandle(query.GetIndexInEntityQuery(cache.Types[i].TypeIndex));
                            iterator.m_SafetyReadWriteCount++;
                            bufferTypeCount++;
                        }
                    }
                }
            }

            Assert.AreEqual(cache.ProcessTypesCount + bufferTypeCount, iterator.m_SafetyReadWriteCount + iterator.m_SafetyReadOnlyCount);
#endif
        }
        internal static unsafe void Initialize(ComponentSystemBase system, Type jobType, Type wrapperJobType, bool isParallelFor, ref JobProcessComponentDataCache cache, out ProcessIterationData iterator)
        {
            int num;
            int num1;

            if (!isParallelFor || !(cache.JobReflectionDataParallelFor == IntPtr.Zero))
            {
                num1 = isParallelFor ? 0 : ((int)(cache.JobReflectionData == IntPtr.Zero));
            }
            else
            {
                num1 = 1;
            }
            if (num1 != 0)
            {
                Type iJobProcessComponentDataInterface = GetIJobProcessComponentDataInterface(jobType);
                if (cache.Types == null)
                {
                    cache.Types = GetComponentTypes(jobType, iJobProcessComponentDataInterface, out cache.ProcessTypesCount, out cache.FilterChanged);
                }
                IntPtr ptr = GetJobReflection(jobType, wrapperJobType, iJobProcessComponentDataInterface, isParallelFor);
                if (isParallelFor)
                {
                    cache.JobReflectionDataParallelFor = ptr;
                }
                else
                {
                    cache.JobReflectionData = ptr;
                }
            }
            if (cache.ComponentSystem != system)
            {
                cache.ComponentGroup = system.GetComponentGroupInternal(cache.Types);
                if (cache.FilterChanged.Length != 0)
                {
                    cache.ComponentGroup.SetFilterChanged(cache.FilterChanged);
                }
                else
                {
                    cache.ComponentGroup.ResetFilter();
                }
                cache.ComponentSystem = system;
            }
            ComponentGroup componentGroup = cache.ComponentGroup;

            iterator.IsReadOnly3 = num = 0;
            iterator.IsReadOnly2 = num = num;
            iterator.IsReadOnly1 = num = num;
            iterator.IsReadOnly0 = num;
            int *numPtr = &iterator.IsReadOnly0;
            int  index  = 0;

            while (true)
            {
                if (index == cache.ProcessTypesCount)
                {
                    fixed(int *numRef = null)
                    {
                        componentGroup.GetComponentChunkIterator(out iterator.Iterator);
                        iterator.IndexInGroup3 = num = -1;
                        iterator.IndexInGroup2 = num = num;
                        iterator.IndexInGroup0 = iterator.IndexInGroup1 = num;
                        int *numPtr2 = &iterator.IndexInGroup0;
                        int  num3    = 0;

                        while (true)
                        {
                            if (num3 == cache.ProcessTypesCount)
                            {
                                fixed(int *numRef2 = null)
                                {
                                    iterator.m_IsParallelFor = isParallelFor;
                                    iterator.m_Length        = componentGroup.CalculateNumberOfChunksWithoutFiltering();
                                    iterator.m_MaxIndex      = iterator.m_Length - 1;
                                    iterator.m_MinIndex      = 0;
                                    AtomicSafetyHandle handle = new AtomicSafetyHandle();

                                    iterator.m_Safety3             = handle = handle;
                                    iterator.m_Safety2             = handle = handle;
                                    iterator.m_Safety0             = iterator.m_Safety1 = handle;
                                    iterator.m_SafetyReadOnlyCount = 0;
                                    AtomicSafetyHandle *handlePtr = &iterator.m_Safety0;
                                    int num4 = 0;

                                    while (true)
                                    {
                                        if (num4 == cache.ProcessTypesCount)
                                        {
                                            fixed(AtomicSafetyHandle *handleRef = null)
                                            {
                                                iterator.m_SafetyReadWriteCount = 0;
                                                AtomicSafetyHandle *handlePtr2 = &iterator.m_Safety0;
                                                int num5 = 0;

                                                while (true)
                                                {
                                                    if (num5 == cache.ProcessTypesCount)
                                                    {
                                                        fixed(AtomicSafetyHandle *handleRef2 = null)
                                                        {
                                                            Assert.AreEqual(cache.ProcessTypesCount, iterator.m_SafetyReadWriteCount + iterator.m_SafetyReadOnlyCount);
                                                            return;
                                                        }
                                                    }
                                                    if (cache.Types[num5].AccessModeType == ComponentType.AccessMode.ReadWrite)
                                                    {
                                                        handlePtr2[iterator.m_SafetyReadOnlyCount + iterator.m_SafetyReadWriteCount] = componentGroup.GetSafetyHandle(componentGroup.GetIndexInComponentGroup(cache.Types[num5].TypeIndex));
                                                        int *numPtr1 = (int *)ref iterator.m_SafetyReadWriteCount;
                                                        numPtr1[0]++;
                                                    }
                                                    num5++;
                                                }
                                            }
                                        }
                                        if (cache.Types[num4].AccessModeType == ComponentType.AccessMode.ReadOnly)
                                        {
                                            handlePtr[iterator.m_SafetyReadOnlyCount] = componentGroup.GetSafetyHandle(componentGroup.GetIndexInComponentGroup(cache.Types[num4].TypeIndex));
                                            int *numPtr3 = (int *)ref iterator.m_SafetyReadOnlyCount;
                                            numPtr3[0]++;
                                        }
                                        num4++;
                                    }
                                }
                            }
                            numPtr2[num3] = componentGroup.GetIndexInComponentGroup(cache.Types[num3].TypeIndex);
                            num3++;
                        }
                    }
                }
                numPtr[index] = (cache.Types[index].AccessModeType == ComponentType.AccessMode.ReadOnly) ? 1 : 0;
                index++;
            }
        }
        static unsafe void Initialize(ComponentSystemBase system, ComponentGroup componentGroup, Type jobType, Type wrapperJobType,
                                      bool isParallelFor, ref JobProcessComponentDataCache cache, out ProcessIterationData iterator)
        {
            // Get the job reflection data and cache it if we don't already have it cached.
            if (isParallelFor && cache.JobReflectionDataParallelFor == IntPtr.Zero ||
                !isParallelFor && cache.JobReflectionData == IntPtr.Zero)
            {
                var iType = GetIJobProcessComponentDataInterface(jobType);
                if (cache.Types == null)
                {
                    cache.Types = GetComponentTypes(jobType, iType, out cache.ProcessTypesCount,
                                                    out cache.FilterChanged);
                }

                var res = GetJobReflection(jobType, wrapperJobType, iType, isParallelFor);

                if (isParallelFor)
                {
                    cache.JobReflectionDataParallelFor = res;
                }
                else
                {
                    cache.JobReflectionData = res;
                }
            }

            // Update cached ComponentGroup and ComponentSystem data.
            if (system != null)
            {
                if (cache.ComponentSystem != system)
                {
                    cache.ComponentGroup = system.GetComponentGroupInternal(cache.Types);

                    // If the cached filter has changed, update the newly cached ComponentGroup with those changes.
                    if (cache.FilterChanged.Length != 0)
                    {
                        cache.ComponentGroup.SetFilterChanged(cache.FilterChanged);
                    }

                    // Otherwise, just reset our newly cached ComponentGroup's filter.
                    else
                    {
                        cache.ComponentGroup.ResetFilter();
                    }

                    cache.ComponentSystem = system;
                }
            }
            else if (componentGroup != null)
            {
                if (cache.ComponentGroup != componentGroup)
                {
                    // Cache the new ComponentGroup and cache that our system is null.
                    cache.ComponentGroup  = componentGroup;
                    cache.ComponentSystem = null;
                }
            }

            var group = cache.ComponentGroup;

            iterator.IsReadOnly0 = iterator.IsReadOnly1 = iterator.IsReadOnly2 = iterator.IsReadOnly3 = iterator.IsReadOnly4 = iterator.IsReadOnly5 = 0;
            fixed(int *isReadOnly = &iterator.IsReadOnly0)
            {
                for (var i = 0; i != cache.ProcessTypesCount; i++)
                {
                    isReadOnly[i] = cache.Types[i].AccessModeType == ComponentType.AccessMode.ReadOnly ? 1 : 0;
                }
            }

            iterator.TypeIndex0 = iterator.TypeIndex1 = iterator.TypeIndex2 = iterator.TypeIndex3 = iterator.TypeIndex4 = iterator.TypeIndex5 = -1;
            fixed(int *typeIndices = &iterator.TypeIndex0)
            {
                for (var i = 0; i != cache.ProcessTypesCount; i++)
                {
                    typeIndices[i] = cache.Types[i].TypeIndex;
                }
            }

            iterator.m_IsParallelFor = isParallelFor;
            iterator.m_Length        = group.CalculateNumberOfChunksWithoutFiltering();

            iterator.GlobalSystemVersion = group.GetComponentChunkIterator().m_GlobalSystemVersion;

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            iterator.m_MaxIndex = iterator.m_Length - 1;
            iterator.m_MinIndex = 0;

            iterator.m_Safety0     = iterator.m_Safety1 = iterator.m_Safety2 = iterator.m_Safety3 = iterator.m_Safety4 =
                iterator.m_Safety5 = default(AtomicSafetyHandle);

            iterator.m_SafetyReadOnlyCount = 0;
            fixed(AtomicSafetyHandle *safety = &iterator.m_Safety0)
            {
                for (var i = 0; i != cache.ProcessTypesCount; i++)
                {
                    if (cache.Types[i].AccessModeType == ComponentType.AccessMode.ReadOnly)
                    {
                        safety[iterator.m_SafetyReadOnlyCount] =
                            group.GetSafetyHandle(group.GetIndexInComponentGroup(cache.Types[i].TypeIndex));
                        iterator.m_SafetyReadOnlyCount++;
                    }
                }
            }

            iterator.m_SafetyReadWriteCount = 0;
            fixed(AtomicSafetyHandle *safety = &iterator.m_Safety0)
            {
                for (var i = 0; i != cache.ProcessTypesCount; i++)
                {
                    if (cache.Types[i].AccessModeType == ComponentType.AccessMode.ReadWrite)
                    {
                        safety[iterator.m_SafetyReadOnlyCount + iterator.m_SafetyReadWriteCount] =
                            group.GetSafetyHandle(group.GetIndexInComponentGroup(cache.Types[i].TypeIndex));
                        iterator.m_SafetyReadWriteCount++;
                    }
                }
            }

            Assert.AreEqual(cache.ProcessTypesCount, iterator.m_SafetyReadWriteCount + iterator.m_SafetyReadOnlyCount);
#endif
        }