private JobHandle ScheduleMembershipDebugJob(JobHandle inputDeps, List <PhysicsObject> uniquePhysicsObjects, ForEachComponentGroupFilter physicsObjectFilter)
        {
            var handles = new NativeArray <JobHandle>(physicsObjectFilter.Length, Allocator.Temp);

            for (int i = 0; i < physicsObjectFilter.Length; i++)
            {
                var entityArray = _membershipDebugGroup.GetEntityArray(physicsObjectFilter, i);
                handles[i] = new MembershipDebugJob
                {
                    EntityArray   = entityArray,
                    PhysicsObject = uniquePhysicsObjects[i]
                }.Schedule(entityArray.Length, 64, inputDeps);
            }
            var combinedHandle = JobHandle.CombineDependencies(handles);

            handles.Dispose();
            return(combinedHandle);
        }
        private JobHandle RunMultipleSimulationTimeStepJobs(JobHandle inputDeps, List <PhysicsObject> uniquePhysicsObjects, ForEachComponentGroupFilter physicsObjectFilter)
        {
            var timeStepHandle = ScheduleMultipleSimulationTimeStepJobs(inputDeps, uniquePhysicsObjects, physicsObjectFilter);

            physicsObjectFilter.Dispose();
            JobHandle.ScheduleBatchedJobs();
            return(timeStepHandle);
        }
 private JobHandle ScheduleMultipleSimulationTimeStepJobs(JobHandle inputDeps, List <PhysicsObject> uniquePhysicsObjects, ForEachComponentGroupFilter physicsObjectFilter)
 {
     return(ScheduleMembershipDebugJob(inputDeps, uniquePhysicsObjects, physicsObjectFilter));
 }
Ejemplo n.º 4
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            UnityEngine.Profiling.Profiler.BeginSample("System Init");

            currentCellDictionary++;
            if (currentCellDictionary >= cellEntityTypeDictionaryArray.Length)
            {
                currentCellDictionary = 0;
            }

            //Make sure we cleared all the hash maps
            allClearCellsJobHandle.Complete();

            uniqueEntityTypes.Clear();
            EntityManager.GetAllUniqueSharedComponentDatas(uniqueEntityTypes);

            entityTypeList.Clear();
            subsetEntityDictionary.Clear();
            subsetMinMaxDataDictionary.Clear();
            fillCellJobHandleDictionary.Clear();

            JobHandle allBoundGroupDependencies = boundDataGroup.GetDependency();

            UnityEngine.Profiling.Profiler.EndSample();

            UnityEngine.Profiling.Profiler.BeginSample("FillJobSetup");

            JobHandle allocateCellJobDependency = allClearCellsJobHandle;
            JobHandle allocateCellJobHandle     = new JobHandle();

            EntityArray[] subsetEntityArrayArray = new EntityArray[uniqueEntityTypes.Count];
            ComponentDataArray <EntityBoundMinMaxData>[] subsetMinMaxDataArrayArray = new ComponentDataArray <EntityBoundMinMaxData> [uniqueEntityTypes.Count];

            //create the hashMaps if needed and get the subset arrays we will use
            UnityEngine.Profiling.Profiler.BeginSample("GetEntityArray");

            ForEachComponentGroupFilter boundDataforEachFilter = boundDataGroup.CreateForEachFilter(uniqueEntityTypes);

            for (int i = 0; i != uniqueEntityTypes.Count; i++)
            {
                subsetEntityArrayArray[i]     = boundDataGroup.GetEntityArray(boundDataforEachFilter, i);
                subsetMinMaxDataArrayArray[i] = boundDataGroup.GetComponentDataArray <EntityBoundMinMaxData>(boundDataforEachFilter, i);

                if (subsetEntityArrayArray[i].Length != 0)
                {
                    CreateCellHashMap(uniqueEntityTypes[i].entityType);
                }
            }

            boundDataforEachFilter.Dispose();
            UnityEngine.Profiling.Profiler.EndSample();


            //set the cells capacity now
            UnityEngine.Profiling.Profiler.BeginSample("Resize Hash Map");
            for (int i = 0; i != uniqueEntityTypes.Count; i++)
            {
                EntityTypeData entityTypeData    = uniqueEntityTypes[i];
                EntityArray    subsetEntityArray = subsetEntityArrayArray[i];

                if (subsetEntityArray.Length == 0)
                {
                    continue;
                }

                NativeMultiHashMap <int, HashMapData> tmpOutputCell = cellEntityTypeDictionary[entityTypeData.entityType];

                //TODO: Test the memory usage
                //We are setting the capacity really high to not run out of space while running our jobs
                if (tmpOutputCell.Capacity < subsetEntityArray.Length * 10)
                {
                    AllocateCellsJob allocateCellJob = new AllocateCellsJob
                    {
                        outputCells    = tmpOutputCell,
                        capacityWanted = subsetEntityArray.Length * 20,
                    };

                    allocateCellJobHandle = JobHandle.CombineDependencies(allocateCellJob.Schedule(allocateCellJobDependency), allocateCellJobHandle);
                }
            }
            UnityEngine.Profiling.Profiler.EndSample();

            JobHandle fillCellJobDependency = JobHandle.CombineDependencies(inputDeps, allBoundGroupDependencies, allocateCellJobHandle);

            for (int i = 0; i != uniqueEntityTypes.Count; i++)
            {
                EntityTypeData entityTypeData    = uniqueEntityTypes[i];
                EntityArray    subsetEntityArray = subsetEntityArrayArray[i];
                ComponentDataArray <EntityBoundMinMaxData> subsetMinMaxDataArray = subsetMinMaxDataArrayArray[i];

                if (subsetEntityArray.Length == 0)
                {
                    continue;
                }

                NativeMultiHashMap <int, HashMapData> .Concurrent tmpOutputCell = cellEntityTypeDictionary[entityTypeData.entityType];
                float3 tmpOutputCellSize = cellSizeEntityDictionary[entityTypeData.entityType];

                UnityEngine.Profiling.Profiler.BeginSample("Allocate tmp Array");
                NativeArray <Entity> subsetEntityArrayOutput = new NativeArray <Entity>(subsetEntityArray.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                NativeArray <EntityBoundMinMaxData> subsetMinMaxDataArrayOutput = new NativeArray <EntityBoundMinMaxData>(subsetEntityArray.Length, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
                UnityEngine.Profiling.Profiler.EndSample();

                FillCellJob fillCellJob = new FillCellJob
                {
                    entityArray = subsetEntityArray,
                    entityBoundMinMaxDataArray       = subsetMinMaxDataArray,
                    entityArrayOutput                = subsetEntityArrayOutput,
                    entityBoundMinMaxDataArrayOutput = subsetMinMaxDataArrayOutput,
                    outputCells = tmpOutputCell,
                    cellSizes   = tmpOutputCellSize,
                };

                JobHandle previousFillJobDependency;

                boundDataGroup.SetFilter(entityTypeData);
                JobHandle jobDependency = JobHandle.CombineDependencies(fillCellJobDependency, boundDataGroup.GetDependency());
                if (fillCellJobHandleDictionary.TryGetValue(entityTypeData.entityType, out previousFillJobDependency))
                {
                    jobDependency = JobHandle.CombineDependencies(jobDependency, previousFillJobDependency);
                }


                JobHandle fillCellJobHandle = fillCellJob.Schedule(subsetEntityArray.Length,
                                                                   MonoBehaviourECSBridge.Instance.GetJobBatchCount(subsetEntityArray.Length),
                                                                   jobDependency);


                entityTypeList.Add(entityTypeData.entityType);
                subsetEntityDictionary.Add(entityTypeData.entityType, subsetEntityArrayOutput);
                subsetMinMaxDataDictionary.Add(entityTypeData.entityType, subsetMinMaxDataArrayOutput);
                fillCellJobHandleDictionary.Add(entityTypeData.entityType, fillCellJobHandle);
            }
            UnityEngine.Profiling.Profiler.EndSample();

            if (fillCellJobHandleDictionary.Count == 0)
            {
                return(inputDeps);
            }

            UnityEngine.Profiling.Profiler.BeginSample("CollisionJobSetup");

            JobHandle previousCollisionJobHandle = new JobHandle();

            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.Asteroid, EntityTypeData.EntityType.Bolt, previousCollisionJobHandle);
            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.Asteroid, EntityTypeData.EntityType.EnemyShip, previousCollisionJobHandle);
            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.Asteroid, EntityTypeData.EntityType.AllyShip, previousCollisionJobHandle);
            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.EnemyShip, EntityTypeData.EntityType.Bolt, previousCollisionJobHandle);
            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.EnemyShip, EntityTypeData.EntityType.AllyShip, previousCollisionJobHandle);
            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.AllyShip, EntityTypeData.EntityType.Bolt, previousCollisionJobHandle);

            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.PlayerShip, EntityTypeData.EntityType.Asteroid, previousCollisionJobHandle);
            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.PlayerShip, EntityTypeData.EntityType.Bolt, previousCollisionJobHandle);
            previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.PlayerShip, EntityTypeData.EntityType.EnemyShip, previousCollisionJobHandle);


            UnityEngine.Profiling.Profiler.EndSample();

            JobHandle.ScheduleBatchedJobs();

            UnityEngine.Profiling.Profiler.BeginSample("DisposeJobSetup");

            JobHandle jobHandleToReturn = new JobHandle();

            List <JobHandle> clearCellJobHandleList = new List <JobHandle>(entityTypeList.Count);

            for (int i = 0; i < entityTypeList.Count; i++)
            {
                if (subsetEntityDictionary.ContainsKey(entityTypeList[i]))
                {
                    jobHandleToReturn = JobHandle.CombineDependencies(fillCellJobHandleDictionary[entityTypeList[i]], jobHandleToReturn);

                    ClearCellsJob clearCellsJob = new ClearCellsJob
                    {
                        entityArray = subsetEntityDictionary[entityTypeList[i]],
                        entityBoundMinMaxDataArray = subsetMinMaxDataDictionary[entityTypeList[i]],
                        outputCells = cellEntityTypeDictionary[entityTypeList[i]],
                    };

                    JobHandle clearCellsJobHandle = clearCellsJob.Schedule(JobHandle.CombineDependencies(fillCellJobHandleDictionary[entityTypeList[i]], previousCollisionJobHandle));
                    clearCellJobHandleList.Add(clearCellsJobHandle);
                }
            }

            jobHandleToReturn = JobHandle.CombineDependencies(previousCollisionJobHandle, jobHandleToReturn);

            UnityEngine.Profiling.Profiler.EndSample();

            NativeArray <JobHandle> clearCellsJobHandleArray = new NativeArray <JobHandle>(clearCellJobHandleList.ToArray(), Allocator.Temp);

            allClearCellsJobHandle = JobHandle.CombineDependencies(clearCellsJobHandleArray);
            clearCellsJobHandleArray.Dispose();


            return(jobHandleToReturn);
        }