JobHandle scheduleCollisionJob(EntityTypeData.EntityType entityTypeToCheck, EntityTypeData.EntityType entityTypeToCheckWith, DestroyEntityData destroyEntityData, JobHandle jobDependencies) { JobHandle collisionDetectJobHandle = new JobHandle(); if (entityTypeList.Contains(entityTypeToCheck) && entityTypeList.Contains(entityTypeToCheckWith)) { CollisionDetectJob collisionDetectJob = new CollisionDetectJob { entityArray = subsetEntityDictionary[entityTypeToCheck], entityBoundMinMaxData = subsetMinMaxDataDictionary[entityTypeToCheck], boundCells = cellEntityTypeDictionary[entityTypeToCheckWith], cellSizes = cellSizeEntityDictionary[entityTypeToCheckWith], collidedEntityQueue = destroyEntityData.entityCollisionQueueConcurrent, }; JobHandle jobDependenciesHandles = JobHandle.CombineDependencies(fillCellJobHandleDictionary[entityTypeToCheck], fillCellJobHandleDictionary[entityTypeToCheckWith], jobDependencies); collisionDetectJobHandle = collisionDetectJob.Schedule(collisionDetectJob.entityArray.Length, MonoBehaviourECSBridge.Instance.GetJobBatchCount(collisionDetectJob.entityArray.Length), jobDependenciesHandles); } return(JobHandle.CombineDependencies(collisionDetectJobHandle, jobDependencies)); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { JobHandle outputHandle = inputDeps; EntityArray destroyEntityArray = destroyEntityDataGroup.GetEntityArray(); if (destroyEntityArray.Length > 0) { Entity destroyEntity = destroyEntityArray[0]; ComponentDataFromEntity <DestroyEntityData> destroyEntityDataFromEntity = GetComponentDataFromEntity <DestroyEntityData>(); DestroyEntityData destroyEntityData = destroyEntityDataFromEntity[destroyEntity]; var outOfBoundJob = new EntityOutOfBoundJob { outOfBoundEntityQueue = destroyEntityData.entityOutOfBoundQueueConcurrent, cameraPosition = MonoBehaviourECSBridge.Instance.gameCamera.transform.position, halfFrustumHeightPreCalculation = Mathf.Tan(Camera.main.fieldOfView * 0.5f * Mathf.Deg2Rad), }; outputHandle = outOfBoundJob.Schedule(this, inputDeps); } return(outputHandle); }
protected override void OnCreateManager(int capacity) { base.OnCreateManager(capacity); //Allocate our queues entityOutOfBoundQueue = new NativeQueue <Entity>(Allocator.Persistent); entityCollisionQueue = new NativeQueue <Entity>(Allocator.Persistent); //Create the entity that will contain our queues dataEntity = EntityManager.CreateEntity(); //Create the compoenent data used to store our queues, other systems will look for that component data type DestroyEntityData data = new DestroyEntityData(); data.entityOutOfBoundQueueConcurrent = entityOutOfBoundQueue; data.entityCollisionQueueConcurrent = entityCollisionQueue; //Add that struct to the entity EntityManager.AddComponentData(dataEntity, data); }
protected override JobHandle OnUpdate(JobHandle inputDeps) { UnityEngine.Profiling.Profiler.BeginSample("System Init"); EntityArray destroyEntityArray = destroyEntityDataGroup.GetEntityArray(); if (destroyEntityArray.Length == 0) { return(inputDeps); } Entity destroyEntity = destroyEntityArray[0]; ComponentDataFromEntity <DestroyEntityData> destroyEntityDataFromEntity = GetComponentDataFromEntity <DestroyEntityData>(); DestroyEntityData destroyEntityData = destroyEntityDataFromEntity[destroyEntity]; currentCellDictionary++; if (currentCellDictionary >= cellEntityTypeDictionaryArray.Length) { currentCellDictionary = 0; } //Make sure we cleared all the hash maps allClearCellsJobHandle.Complete(); uniqueEntityTypes.Clear(); EntityManager.GetAllUniqueSharedComponentData(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++) { boundDataGroup.SetFilter(uniqueEntityTypes[i]); subsetEntityArrayArray[i] = boundDataGroup.GetEntityArray(); subsetMinMaxDataArrayArray[i] = boundDataGroup.GetComponentDataArray <EntityBoundMinMaxData>(); if (subsetEntityArrayArray[i].Length != 0) { CreateCellHashMap(uniqueEntityTypes[i].entityType); } } boundDataGroup.ResetFilter(); 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].ToConcurrent(); 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.PlayerBolt, destroyEntityData, previousCollisionJobHandle); previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.Asteroid, EntityTypeData.EntityType.EnemyShip, destroyEntityData, previousCollisionJobHandle); previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.Asteroid, EntityTypeData.EntityType.AllyShip, destroyEntityData, previousCollisionJobHandle); previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.EnemyShip, EntityTypeData.EntityType.AllyBolt, destroyEntityData, previousCollisionJobHandle); previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.EnemyShip, EntityTypeData.EntityType.PlayerBolt, destroyEntityData, previousCollisionJobHandle); previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.EnemyShip, EntityTypeData.EntityType.AllyShip, destroyEntityData, previousCollisionJobHandle); previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.AllyShip, EntityTypeData.EntityType.EnemyBolt, destroyEntityData, previousCollisionJobHandle); previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.PlayerShip, EntityTypeData.EntityType.Asteroid, destroyEntityData, previousCollisionJobHandle); previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.PlayerShip, EntityTypeData.EntityType.EnemyBolt, destroyEntityData, previousCollisionJobHandle); previousCollisionJobHandle = scheduleCollisionJob(EntityTypeData.EntityType.PlayerShip, EntityTypeData.EntityType.EnemyShip, destroyEntityData, 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); }