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); }
unsafe protected override void OnUpdate() { UnityEngine.Profiling.Profiler.BeginSample("complete"); m_InstanceRendererGroup.GetDependency().Complete(); UnityEngine.Profiling.Profiler.EndSample(); UnityEngine.Profiling.Profiler.BeginSample("GetAllUniqueSharedComponentDatas"); EntityManager.GetAllUniqueSharedComponentDatas(m_CacheduniqueRendererTypes); UnityEngine.Profiling.Profiler.EndSample(); CopyMatricesJob[] copyMatricesJobArray = new CopyMatricesJob[m_matrixPtrCount]; for (int i = 0; i < m_matrixPtrCount; i++) { copyMatricesJobArray[i] = new CopyMatricesJob(); } for (int i = 0; i != m_CacheduniqueRendererTypes.Count; i++) { UnityEngine.Profiling.Profiler.BeginSample("Unique renderer"); EntityInstanceRenderer renderer = m_CacheduniqueRendererTypes[i]; m_InstanceRendererGroup.SetFilter(renderer); ComponentDataArray <EntityInstanceRendererTransform> transforms = m_InstanceRendererGroup.GetComponentDataArray <EntityInstanceRendererTransform>(); int beginIndex = 0; int previousFilledMatrixJobIndex = -1; JobHandle previousMatrixJobHandle = new JobHandle(); int previousJobCount = 0; int previousJobBeginIndex = 0; #if !ENABLE_IL2CPP if (Input.GetKey(KeyCode.O)) { #endif while (beginIndex < transforms.Length) { int length = math.min(m_MatricesArray.Length, transforms.Length - beginIndex); UnityEngine.Profiling.Profiler.BeginSample("CopyMatrices"); CopyMatrices(transforms, beginIndex, length, m_MatricesArray); UnityEngine.Profiling.Profiler.EndSample(); UnityEngine.Profiling.Profiler.BeginSample("DrawMeshInstanced"); for (int subMeshIndex = 0; subMeshIndex < renderer.mesh.subMeshCount; subMeshIndex++) { Graphics.DrawMeshInstanced(renderer.mesh, subMeshIndex, renderer.materials[subMeshIndex], m_MatricesArray, length, null, renderer.castShadows, renderer.receiveShadows); } UnityEngine.Profiling.Profiler.EndSample(); beginIndex += length; } #if !ENABLE_IL2CPP } else { while (beginIndex < transforms.Length) { int totalJobLength = math.min(m_batchSize * m_matrixPtrCount, transforms.Length - beginIndex); int jobCount = (totalJobLength / m_batchSize); if (totalJobLength % m_batchSize > 0) { jobCount += 1; } if (jobCount < 10) { if (previousFilledMatrixJobIndex != -1) { DrawPreviousJobs(previousMatrixJobHandle, previousFilledMatrixJobIndex, previousJobCount, previousJobBeginIndex, transforms, renderer); //We are not filling any matrices from jobs in this loop, reset the counter previousFilledMatrixJobIndex = -1; } int length = math.min(m_MatricesArray.Length, transforms.Length - beginIndex); UnityEngine.Profiling.Profiler.BeginSample("CopyMatrices"); CopyMatrices(transforms, beginIndex, length, m_MatricesArray); UnityEngine.Profiling.Profiler.EndSample(); UnityEngine.Profiling.Profiler.BeginSample("DrawMeshInstanced"); for (int subMeshIndex = 0; subMeshIndex < renderer.mesh.subMeshCount; subMeshIndex++) { Graphics.DrawMeshInstanced(renderer.mesh, subMeshIndex, renderer.materials[subMeshIndex], m_MatricesArray, length, null, renderer.castShadows, renderer.receiveShadows); } UnityEngine.Profiling.Profiler.EndSample(); beginIndex += length; } else { int matrixJobIndexToFill = previousFilledMatrixJobIndex == 0 ? 1 : 0; //TODO: find a better way to create an arraym_MatricesArrayList of Ptr to the matrices List <Matrix4x4[]> matrixJobIndexToFillList = m_MatricesArrayList[matrixJobIndexToFill]; fixed(Matrix4x4 *matrixPtr_0 = matrixJobIndexToFillList[0], matrixPtr_1 = matrixJobIndexToFillList[1], matrixPtr_2 = matrixJobIndexToFillList[2], matrixPtr_3 = matrixJobIndexToFillList[3], matrixPtr_4 = matrixJobIndexToFillList[4], matrixPtr_5 = matrixJobIndexToFillList[5], matrixPtr_6 = matrixJobIndexToFillList[6], matrixPtr_7 = matrixJobIndexToFillList[7], matrixPtr_8 = matrixJobIndexToFillList[8], matrixPtr_9 = matrixJobIndexToFillList[9], matrixPtr_10 = matrixJobIndexToFillList[10], matrixPtr_11 = matrixJobIndexToFillList[11], matrixPtr_12 = matrixJobIndexToFillList[12], matrixPtr_13 = matrixJobIndexToFillList[13], matrixPtr_14 = matrixJobIndexToFillList[14], matrixPtr_15 = matrixJobIndexToFillList[15], matrixPtr_16 = matrixJobIndexToFillList[16], matrixPtr_17 = matrixJobIndexToFillList[17], matrixPtr_18 = matrixJobIndexToFillList[18], matrixPtr_19 = matrixJobIndexToFillList[19], matrixPtr_20 = matrixJobIndexToFillList[20], matrixPtr_21 = matrixJobIndexToFillList[21], matrixPtr_22 = matrixJobIndexToFillList[22], matrixPtr_23 = matrixJobIndexToFillList[23], matrixPtr_24 = matrixJobIndexToFillList[24], matrixPtr_25 = matrixJobIndexToFillList[25], matrixPtr_26 = matrixJobIndexToFillList[26], matrixPtr_27 = matrixJobIndexToFillList[27], matrixPtr_28 = matrixJobIndexToFillList[28], matrixPtr_29 = matrixJobIndexToFillList[29], matrixPtr_30 = matrixJobIndexToFillList[30], matrixPtr_31 = matrixJobIndexToFillList[31], matrixPtr_32 = matrixJobIndexToFillList[32], matrixPtr_33 = matrixJobIndexToFillList[33], matrixPtr_34 = matrixJobIndexToFillList[34], matrixPtr_35 = matrixJobIndexToFillList[35], matrixPtr_36 = matrixJobIndexToFillList[36], matrixPtr_37 = matrixJobIndexToFillList[37], matrixPtr_38 = matrixJobIndexToFillList[38], matrixPtr_39 = matrixJobIndexToFillList[39] ) { Matrix4x4 *[] matrixPtrArray = { matrixPtr_0, matrixPtr_1, matrixPtr_2, matrixPtr_3, matrixPtr_4, matrixPtr_5, matrixPtr_6, matrixPtr_7, matrixPtr_8, matrixPtr_9, matrixPtr_10, matrixPtr_11, matrixPtr_12, matrixPtr_13, matrixPtr_14, matrixPtr_15, matrixPtr_16, matrixPtr_17, matrixPtr_18, matrixPtr_19, matrixPtr_20, matrixPtr_21, matrixPtr_22, matrixPtr_23, matrixPtr_24, matrixPtr_25, matrixPtr_26, matrixPtr_27, matrixPtr_28, matrixPtr_29, matrixPtr_30, matrixPtr_31, matrixPtr_32, matrixPtr_33, matrixPtr_34, matrixPtr_35, matrixPtr_36, matrixPtr_37, matrixPtr_38, matrixPtr_39, }; UnityEngine.Profiling.Profiler.BeginSample("Setup Copy Jobs"); for (int matrixJobIndex = 0; matrixJobIndex < jobCount; matrixJobIndex++) { copyMatricesJobArray[matrixJobIndex].matricesPtr = matrixPtrArray[matrixJobIndex]; copyMatricesJobArray[matrixJobIndex].transforms = transforms; copyMatricesJobArray[matrixJobIndex].beginIndex = beginIndex + (matrixJobIndex * m_batchSize); copyMatricesJobArray[matrixJobIndex].length = math.min(m_batchSize, transforms.Length - copyMatricesJobArray[matrixJobIndex].beginIndex); } JobHandle allCurrentMatrixJobHandle = copyMatricesJobArray[0].Schedule(); for (int matrixJobIndex = 0; matrixJobIndex < jobCount; matrixJobIndex++) { allCurrentMatrixJobHandle = JobHandle.CombineDependencies(allCurrentMatrixJobHandle, copyMatricesJobArray[matrixJobIndex].Schedule()); } JobHandle.ScheduleBatchedJobs(); UnityEngine.Profiling.Profiler.EndSample(); if (previousFilledMatrixJobIndex != -1) { DrawPreviousJobs(previousMatrixJobHandle, previousFilledMatrixJobIndex, previousJobCount, previousJobBeginIndex, transforms, renderer); } previousMatrixJobHandle = allCurrentMatrixJobHandle; previousFilledMatrixJobIndex = matrixJobIndexToFill; previousJobCount = jobCount; previousJobBeginIndex = beginIndex; } beginIndex += totalJobLength; } } //draw anything left if (previousFilledMatrixJobIndex != -1) { DrawPreviousJobs(previousMatrixJobHandle, previousFilledMatrixJobIndex, previousJobCount, previousJobBeginIndex, transforms, renderer); } } #endif UnityEngine.Profiling.Profiler.EndSample(); } m_CacheduniqueRendererTypes.Clear(); }
protected override void BeforeRowsGUI() { currentSystem?.GetDependency().Complete(); base.BeforeRowsGUI(); }