static internal NativeArray <ArchetypeChunk> Create(NativeList <EntityArchetype> archetypes, Allocator allocator)
#endif
        {
            int length         = 0;
            var archetypeCount = archetypes.Length;
            var offsets        = new NativeArray <int>(archetypeCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);

            for (var i = 0; i < archetypeCount; i++)
            {
                offsets[i] = length;
                length    += archetypes[i].Archetype->ChunkCount;
            }

            var chunks          = new NativeArray <ArchetypeChunk>(length, allocator, NativeArrayOptions.UninitializedMemory);
            var gatherChunksJob = new GatherChunks
            {
                Archetypes = archetypes,
                Offsets    = offsets,
                Chunks     = chunks
            };
            var gatherChunksJobHandle = gatherChunksJob.Schedule(archetypeCount, 1);

            gatherChunksJobHandle.Complete();

            offsets.Dispose();
            return(chunks);
        }
Example #2
0
        internal static unsafe NativeArray <ArchetypeChunk> Create(NativeList <EntityArchetype> archetypes, Allocator allocator, AtomicSafetyHandle safetyHandle)
        {
            int length = 0;
            int num2   = archetypes.Length;
            NativeArray <int> array = new NativeArray <int>(num2, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
            int index = 0;

            while (true)
            {
                if (index >= num2)
                {
                    NativeArray <ArchetypeChunk> array2 = new NativeArray <ArchetypeChunk>(length, allocator, NativeArrayOptions.UninitializedMemory);
                    GatherChunks jobData = new GatherChunks {
                        Archetypes = archetypes,
                        Offsets    = array,
                        Chunks     = array2
                    };
                    JobHandle dependsOn = new JobHandle();
                    jobData.Schedule <GatherChunks>(num2, 1, dependsOn).Complete();
                    array.Dispose();
                    return(array2);
                }
                array.set_Item(index, length);
                length += archetypes[index].Archetype.ChunkCount;
                index++;
            }
        }
Example #3
0
        /// <summary>
        ///     Creates a NativeArray with all the chunks in a given archetype.
        /// </summary>
        /// <param name="firstMatchingArchetype">First node of MatchingArchetypes linked list.</param>
        /// <param name="allocator">Allocator to use for the array.</param>
        /// <param name="jobHandle">Handle to the GatherChunks job used to fill the output array.</param>
        /// <returns>NativeArray of all the chunks in the firstMatchingArchetype list.</returns>
        public static NativeArray <ArchetypeChunk> CreateArchetypeChunkArray(MatchingArchetypes *firstMatchingArchetype, Allocator allocator, out JobHandle jobHandle)
        {
            var archetypeCount = 0;

            for (var match = firstMatchingArchetype; match != null; match = match->Next)
            {
                archetypeCount++;
            }

            var matchingArchetypes = new NativeArray <EntityArchetype>(archetypeCount, Allocator.TempJob);
            var offsets            = new NativeArray <int>(archetypeCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
            var chunkCount         = 0;
            {
                int i      = 0;
                var length = 0;
                for (var match = firstMatchingArchetype; match != null; match = match->Next)
                {
                    var archetype = match->Archetype;
                    matchingArchetypes[i] = new EntityArchetype

                    {
                        Archetype = archetype
                    };
                    offsets[i] = length;
                    length    += archetype->Chunks.Count;
                    i++;
                }
                chunkCount = length;
            }

            var chunks          = new NativeArray <ArchetypeChunk>(chunkCount, allocator, NativeArrayOptions.UninitializedMemory);
            var gatherChunksJob = new GatherChunks
            {
                Archetypes = matchingArchetypes,
                Offsets    = offsets,
                Chunks     = chunks
            };
            var gatherChunksJobHandle = gatherChunksJob.Schedule(archetypeCount, 1);

            jobHandle = gatherChunksJobHandle;

            return(chunks);
        }
Example #4
0
        /// <summary>
        /// Creates a NativeArray with all the chunks in a given archetype filtered by the provided EntityQueryFilter.
        /// This function will not sync the needed types in the EntityQueryFilter so they have to be synced manually before calling this function.
        /// </summary>
        /// <param name="matchingArchetypes">List of matching archetypes.</param>
        /// <param name="allocator">Allocator to use for the array.</param>
        /// <param name="jobHandle">Handle to the GatherChunks job used to fill the output array.</param>
        /// <param name="filter">Filter used to filter the resulting chunks</param>
        /// <param name="dependsOn">All jobs spawned will depend on this JobHandle</param>
        /// <returns>NativeArray of all the chunks in the matchingArchetypes list.</returns>
        public static NativeArray <ArchetypeChunk> CreateArchetypeChunkArrayWithoutSync(UnsafeMatchingArchetypePtrList matchingArchetypes,
                                                                                        Allocator allocator, out JobHandle jobHandle, ref EntityQueryFilter filter,
                                                                                        JobHandle dependsOn = default(JobHandle))
        {
            var archetypeCount = matchingArchetypes.Length;

            var offsets =
                new NativeArray <int>(archetypeCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
            var chunkCount = 0;

            {
                for (int i = 0; i < matchingArchetypes.Length; ++i)
                {
                    var archetype = matchingArchetypes.Ptr[i]->Archetype;
                    offsets[i]  = chunkCount;
                    chunkCount += archetype->Chunks.Count;
                }
            }

            if (!filter.RequiresMatchesFilter)
            {
                var chunks          = new NativeArray <ArchetypeChunk>(chunkCount, allocator, NativeArrayOptions.UninitializedMemory);
                var gatherChunksJob = new GatherChunks
                {
                    MatchingArchetypes   = matchingArchetypes.Ptr,
                    entityComponentStore = matchingArchetypes.entityComponentStore,
                    Offsets = offsets,
                    Chunks  = chunks
                };
                var gatherChunksJobHandle = gatherChunksJob.Schedule(archetypeCount, 1, dependsOn);
                jobHandle = gatherChunksJobHandle;

                return(chunks);
            }
            else
            {
                var filteredCounts  = new NativeArray <int>(archetypeCount + 1, Allocator.TempJob);
                var sparseChunks    = new NativeArray <ArchetypeChunk>(chunkCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                var gatherChunksJob = new GatherChunksWithFiltering
                {
                    MatchingArchetypes   = matchingArchetypes.Ptr,
                    Filter               = filter,
                    Offsets              = offsets,
                    FilteredCounts       = filteredCounts,
                    SparseChunks         = sparseChunks,
                    entityComponentStore = matchingArchetypes.entityComponentStore
                };
                gatherChunksJob.Schedule(archetypeCount, 1, dependsOn).Complete();

                // accumulated filtered counts: filteredCounts[i] becomes the destination offset
                int totalChunks = 0;
                for (int i = 0; i < archetypeCount; ++i)
                {
                    int currentCount = filteredCounts[i];
                    filteredCounts[i] = totalChunks;
                    totalChunks      += currentCount;
                }
                filteredCounts[archetypeCount] = totalChunks;

                var joinedChunks = new NativeArray <ArchetypeChunk>(totalChunks, allocator, NativeArrayOptions.UninitializedMemory);

                jobHandle = new JoinChunksJob
                {
                    DestinationOffsets = filteredCounts,
                    SparseChunks       = sparseChunks,
                    Offsets            = offsets,
                    JoinedChunks       = joinedChunks
                }.Schedule(archetypeCount, 1);

                return(joinedChunks);
            }
        }