Exemple #1
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            //var collisionData = new NativeList<CollisionData>(Allocator.TempJob);
            
            var allocateCellsJobHandle = new AllocateCellsJob
            {
                QuadrantIndexToEntityMap = _quadrantIndexToEntityMap,
                CapacityWanted = _translationQuery.CalculateLength()
            }.Schedule(inputDeps);

            var addQuadrantDataToHashMapJobHandle = new AddQuadrantDataToHashMapJob
            {
                QuadrantIndexToEntityMap = _quadrantIndexToEntityMap.ToConcurrent()
            }.Schedule(this, allocateCellsJobHandle);
            
            var collisionDetectJobHandle = new CollisionDetectJob
            {
                QuadrantIndexToEntityMap = _quadrantIndexToEntityMap,
                //CollisionData = collisionData
            }.Schedule(this, addQuadrantDataToHashMapJobHandle);
            
            var clearCellsJobHandle = new ClearCellsJob
            {
                QuadrantIndexToEntityMap = _quadrantIndexToEntityMap
            }.Schedule(collisionDetectJobHandle);
            
            /*var collisionsCountJobHandle = new CollisionsCountJob
            {
                CollisionData = collisionData
            }.Schedule(collisionDetectJobHandle);
            
            return JobHandle.CombineDependencies(clearCellsJobHandle, collisionsCountJobHandle);*/
            return clearCellsJobHandle;
        }
Exemple #2
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");
            for (int i = 0; i != uniqueEntityTypes.Count; i++)
            {
                EntityTypeData entityTypeData = uniqueEntityTypes[i];
                boundDataGroup.SetFilter(entityTypeData);
                subsetEntityArrayArray[i]     = boundDataGroup.GetEntityArray();
                subsetMinMaxDataArrayArray[i] = boundDataGroup.GetComponentDataArray <EntityBoundMinMaxData>();

                if (subsetEntityArrayArray[i].Length != 0)
                {
                    CreateCellHashMap(uniqueEntityTypes[i].entityType);
                }
            }
            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();

            allocateCellJobHandle.Complete();

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

            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);
                NativeArray <EntityBoundMinMaxData> subsetMinMaxDataArrayOutput = new NativeArray <EntityBoundMinMaxData>(subsetEntityArray.Length, Allocator.TempJob);
                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);
            }

            //JobHandle.ScheduleBatchedJobs();


            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);
        }