Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Results of job execution not propogating back?

Discussion in 'Entity Component System' started by jwvanderbeck, Sep 30, 2022.

  1. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    825
    I have a small IJobParallelFor that I have written that just basically loops through a bunch of triangles, computes it neighbors, then writes that data back to the triangle.

    As far as I can tell everything is executing fine, and the correct results are being computed. But the results are not writing back to the triangle.

    Here is my code for the job. I don't think the code for Triangle is relevant but if it is let me know and I can post it or anything additional as requested.

    Code (CSharp):
    1.     public struct CalculateNeighborsJob : IJobParallelFor
    2.     {
    3.         public NativeArray<Triangle> triangles;
    4.         [Unity.Collections.ReadOnly]
    5.         public NativeParallelHashMap<int, UnsafeList<Triangle>> triangleCache;
    6.  
    7.         public void Execute(int index)
    8.         {
    9.             var poly = triangles[index];
    10.             if (poly.HasCheckedNeighbors)
    11.             {
    12.                 return;
    13.             }
    14.  
    15.             UnsafeList<Triangle> neighbors = new UnsafeList<Triangle>(12, Allocator.TempJob);
    16.             foreach (var vertex in poly.VertexIndices)
    17.             {
    18.                 var sharedTriangles = triangleCache[vertex];
    19.                 foreach (var otherPoly in sharedTriangles)
    20.                 {
    21.                     if (poly.Equals(otherPoly))
    22.                         continue;
    23.                      
    24.                     if (poly.IsNeighborOf(otherPoly))
    25.                     {
    26.                         neighbors.Add(otherPoly);
    27.                     }
    28.                 }
    29.             }
    30.  
    31.             poly.neighbors = new UnsafeList<Triangle>(neighbors.Length, Allocator.Persistent);
    32.             poly.neighbors.CopyFrom(neighbors);
    33.             poly.HasCheckedNeighbors = true;
    34.             triangles[index] = poly;
    35.             neighbors.Dispose();
    36.         }
    37.     }
    38.  
    And here is the code that runs the job

    Code (CSharp):
    1.         private void CalculateNeighborsJob()
    2.         {
    3.             // setup the data structures needed
    4.             var jobTriangles = new NativeList<Triangle>(Polygons.Count, Allocator.TempJob);
    5.             foreach (var polygon in Polygons)
    6.             {
    7.                 jobTriangles.Add(polygon);
    8.             }
    9.  
    10.             var jobTriangleCache = new NativeParallelHashMap<int, UnsafeList<Triangle>>(triangleCache.Count, Allocator.TempJob);
    11.             foreach (var item in triangleCache)
    12.             {
    13.                 var cacheList = new UnsafeList<Triangle>(item.Value.Count, Allocator.TempJob);
    14.                 foreach (var triangle in item.Value)
    15.                 {
    16.                     cacheList.Add(triangle);
    17.                 }
    18.                 jobTriangleCache.Add(item.Key, cacheList);
    19.             }
    20.          
    21.             // create the job
    22.             var job = new CalculateNeighborsJob
    23.             {
    24.                 triangles = jobTriangles,
    25.                 triangleCache = jobTriangleCache
    26.             };
    27.          
    28.             // Schedule the job
    29.             var jobHandle = job.Schedule(job.triangles.Length, 1);
    30.             // Run the job
    31.             jobHandle.Complete();
    32.          
    33.             // Cleanup collections
    34.             jobTriangles.Dispose(jobHandle);
    35.             foreach (var item in jobTriangleCache)
    36.             {
    37.                 item.Value.Dispose(jobHandle);
    38.             }
    39.  
    40.             jobTriangleCache.Dispose(jobHandle);
    41.         }
    42.  
     
  2. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    825
    Oh crap I think I see the problem, as usual after posting.

    Yep, ok fixed this. For anyone else that may run into the same (stupid) pitfall, here is the scoop,

    I was copying data from my main class into a Native container so it could be processed in the job (without having to rewrite my whole main class) and THAT is the collection that had to updated triangles after the job run! Duh! What I was missing was a step to copy the Native container data after the job BACK to my master collection.