Search Unity

Resolved Problem with getting Job to work and get results back from NativeList

Discussion in 'C# Job System' started by FederalRazer89, Jan 23, 2023.

  1. FederalRazer89

    FederalRazer89

    Joined:
    Nov 24, 2019
    Posts:
    83
    Version:
    Burst 1.4.1
    Entities 0.17-preview.42
    Jobs 0.8.0-preview.23


    Hello


    I am trying to make my function multithreaded with the job system and I have made other parts of that are multithreaded, now it don’t work as intended.
    I can run it fine on the main thread but when I run it with Ijob then it will compile and run but not doing the work that should be scheduled. Because there is no info to debug, so I am kind of stuck.

    The only thing i see is that the jobs that several NativeLists are diposed on the frame after. But i dont have anything happening on the next frame.

    The function use several positions from raycast to create grids were raycast have hit.

    Edit: Just hade the wrong scale on the profiler so i couldn't see the jobs doing work. But i still cant get the processed data from the job.

    upload_2023-1-30_9-19-30.png
    Edit: New picture

    Code (CSharp):
    1.  
    2.     [BurstCompile] public struct OptimizingJobs : IJob
    3.     {
    4.         public CustomTools.CustomGeometryTools customGeometryToolsJob;
    5.  
    6.         [ReadOnly] public NavTile navTileJob;
    7.         public NativeList<NavPoint> candidatePointsNativeListJob;
    8.         public NativeList<float3x4> finalGridNativeListJob;
    9.         public float3x4 cubeRoofJob;
    10.         public float3x4 cubeFloorJob;
    11.  
    12.         public float quadScaleJob;
    13.  
    14.         public void Execute()
    15.         {
    16.  
    17.             int gridCount = (int)(math.distance(navTileJob.navTileBottom.c0, navTileJob.navTileRoof.c0) / quadScaleJob);
    18.             customGeometryToolsJob.GridCreationWithNavPoints(candidatePointsNativeListJob, candidatePointsNativeListJob[0].navPosition, candidatePointsNativeListJob[0].navPositionTangent, gridCount, quadScaleJob, cubeRoofJob, cubeFloorJob, finalGridNativeListJob); // <=== Not working
    19.  
    20.         }
    21.     }
    22.  


    Code (CSharp):
    1.  
    2.     [BurstCompile] public struct NavpointGeneration
    3.     {
    4.      
    5.  
    6.         [ReadOnly] public BuildPhysicsWorld buildPhysicsWorldSystem;
    7.         public CustomNavMeshTile customNavMeshTile;
    8.  
    9.         public ECSDebugTools.DebugBoxSizeVisual debugTool;
    10.         public GetNavTileInfo getNavTileInfo;
    11.  
    12.         public List<NavPointGenerationJob> navpointGenerationsList;
    13.         public List<OptimizingJobs> optimizingJobsList;
    14.         public List<OptimizingGridMergsJobs> optimizingGridMergsJobsList;
    15.  
    16.         public NativeList<JobHandle> jobLists;
    17.         public NativeList<JobHandle> jobOptimizingLists;
    18.         public NativeList<JobHandle> jobGridMergeOptimizingLists;
    19.  
    20.         public JobHandle Dependency;
    21.  
    22.         public float deltaTime;
    23.  
    24.         public List<NavPoint> listHitTangents;
    25.         public List<NavTile> navTileList;
    26.  
    27.  
    28.         //Initialize Jobs --------------------------------------------
    29.  
    30.         public void InitializeOptimizingJobs(CustomTools.CustomGeometryTools customGeometryTools, NavTile navTile, List<NativeList<NavPoint>> candidatePoints, List<NativeList<float3x4>> finalGridList, float3x4 cubeRoof, float3x4 cubeFloor, float quadScale)
    31.         {
    32.             for (int i = 0; i < candidatePoints.Count; i++)
    33.             {
    34.  
    35.                 OptimizingJobs optimizingJobs = new OptimizingJobs()
    36.                 {
    37.                     navTileJob = navTile,
    38.                     customGeometryToolsJob = customGeometryTools,
    39.                     candidatePointsNativeListJob = candidatePoints[i],
    40.                     finalGridNativeListJob = finalGridList[i],
    41.  
    42.                     cubeFloorJob = cubeFloor,
    43.                     cubeRoofJob = cubeRoof,
    44.  
    45.                     quadScaleJob = quadScale
    46.  
    47.  
    48.                 };
    49.                 optimizingJobsList.Add(optimizingJobs);
    50.             }
    51.  
    52.         }
    53.  
    54.  
    55.         //Schedule Jobs -----------------------------------------------------------------
    56.  
    57.         public void ScheduleOptimizingJobs()
    58.         {
    59.             Dependency = JobHandle.CombineDependencies(Dependency, buildPhysicsWorldSystem.GetOutputDependency());
    60.  
    61.             for (int i = 0; i < optimizingJobsList.Count; i++)
    62.             {
    63.  
    64.                 jobOptimizingLists.Add(optimizingJobsList[i].Schedule(Dependency));
    65.             }
    66.  
    67.             foreach (var item in jobOptimizingLists)
    68.             {
    69.                 Dependency = JobHandle.CombineDependencies(Dependency, item);
    70.             }
    71.  
    72.             foreach (var item in jobOptimizingLists)
    73.             {
    74.                 item.Complete();
    75.             }
    76.             buildPhysicsWorldSystem.AddInputDependencyToComplete(Dependency);
    77.             jobOptimizingLists.Clear();
    78.         }
    79.  
    80.  
    81.     }
    82.  

    Code (CSharp):
    1.  
    2.     public class NavpointGenerationScheduleAndExecute
    3.     {
    4.         public NavpointGeneration navpointGenerationClass;
    5.  
    6.         public void OptimizeAndCreateNavCells(int jobCount, NavTile navTile)
    7.         {
    8.  
    9.             CustomTools.CustomGeometryTools customGeometryTools = new CustomTools.CustomGeometryTools();
    10.             ECSDebugTools.DebugShowQuad debugShowQuad = new ECSDebugTools.DebugShowQuad();
    11.  
    12.             float quadScale = 0.5f;
    13.             //float maxPointDistance = 1.5f * customNavMeshTile.scale;
    14.             List<NativeList<NavPoint>> candidatePointsList = new List<NativeList<NavPoint>>();
    15.             List<NativeList<float3x4>> finalGridList = new List<NativeList<float3x4>>();
    16.  
    17.  
    18.             NavQuad testNavCell = new NavQuad();
    19.  
    20.             SortNavPointsNormal(navTile, candidatePointsList);
    21.             //Create NavCell
    22.             int navCellIndex = 0;
    23.             for (int i = 0; i < 1; i++)
    24.             {
    25.                 navCellIndex = i;
    26.  
    27.             }
    28.             for (int i = 0; i < candidatePointsList.Count; i++) { finalGridList.Add(new NativeList<float3x4>(Allocator.Persistent)); }
    29.  
    30.        
    31.          
    32.  
    33.             navpointGenerationClass.InitializeOptimizingJobs(customGeometryTools, navTile, candidatePointsList, finalGridList, navTile.navTileBottom, navTile.navTileRoof, quadScale);
    34.             navpointGenerationClass.ScheduleOptimizingJobs();
    35.  
    36.  
    37.             for (int i = 0; i < finalGridList.Count; i++)
    38.             {
    39.                if (finalGridList[i].Length > 0)
    40.                {
    41.                   finalGridList[i] = customGeometryTools.GridMergeOptimatzion(finalGridList[i], 5000);
    42.                }
    43.             };
    44.  
    45.  
    46.             navpointGenerationClass.InitializeGridMergeOptimizing(customGeometryTools, finalGridList);
    47.             navpointGenerationClass.ScheduleGridMergeOptimizingJobs();
    48.  
    49.  
    50.             UnityEngine.Debug.Log("finalGridList " + finalGridList.Count);
    51.             UnityEngine.Debug.Log("navTile.navPointList.Length " + navTile.navPointList.Length);
    52.  
    53.  
    54.  
    55.             //Dispose nativeList
    56.             for (int i = 0; i < finalGridList.Count; i++)
    57.             {
    58.  
    59.                 UnityEngine.Debug.Log("finalGridList[i].Length " + finalGridList[i].Length);
    60.                 finalGridList[i].Dispose();
    61.             }
    62.             for (int i = 0; i < candidatePointsList.Count; i++)
    63.             {
    64.                 candidatePointsList[i].Dispose();
    65.             }
    66.  
    67.             navpointGenerationClass.jobLists.Dispose();
    68.             navpointGenerationClass.jobOptimizingLists.Dispose();
    69.             navpointGenerationClass.jobGridMergeOptimizingLists.Dispose();
    70.  
    71.  
    72.  
    73.  
    74.         }
    75.  
    76.  
    77.  
    78.  
    79.     }
    80.  


    Code (CSharp):
    1.  
    2.  
    3.  
    4.         CustomNavMeshGenerationTools.NavpointGenerationScheduleAndExecute navpointGenerationScheduleAndExecute = new CustomNavMeshGenerationTools.NavpointGenerationScheduleAndExecute()
    5.         {
    6.             navpointGenerationClass = navpointGeneration
    7.         };
    8.  
    9.  
    10.         navpointGenerationScheduleAndExecute.OptimizeAndCreateNavCells(20,navTile);
    11.  
    12.  

    Any help is helpful, usually i have some kind of warning, error or behavior to go on.
     

    Attached Files:

    Last edited: Jan 30, 2023
  2. FederalRazer89

    FederalRazer89

    Joined:
    Nov 24, 2019
    Posts:
    83
    Well i solved the problem it was just that i hade setup boundary in the wrong order so it didn't produce any values except 0.
     
    Haxel0rd likes this.
  3. Haxel0rd

    Haxel0rd

    Joined:
    May 20, 2021
    Posts:
    49
    Thank you for posting your solution after you solved it!
     
    FederalRazer89 likes this.