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

Can't figure out how to use IJobParallelForFilter

Discussion in 'Entity Component System' started by cjddmut, Feb 12, 2019.

  1. cjddmut

    cjddmut

    Joined:
    Nov 19, 2012
    Posts:
    178
    Below I have a simple test to try to figure out how to use IJobParallelForFilter. The test, I think, should select one of the indices to increment the underlying value. But the filter job never actually populates the indexes NativeList. I'm at a loss on how to use the filter and could use some tips!

    Code (CSharp):
    1.  
    2. public struct TestData
    3. {
    4.    public int num;
    5.    public byte filter;
    6.  
    7.    public TestData(int num, byte filter)
    8.    {
    9.       this.num = num;
    10.       this.filter = filter;
    11.    }
    12. }
    13.  
    14. private void RunFilteredIncrementTest()
    15. {
    16.     NativeArray<TestData> nData = new NativeArray<TestData>(2, Allocator.TempJob);
    17.        
    18.     // Won't filter to use...or won't be filtered away? Not sure how to interpret
    19.     nData[0] = new TestData(0, 0);
    20.        
    21.     // Opposite, I expect one of these to end up having a value of 1
    22.     nData[1] = new TestData(0, 1);
    23.        
    24.     NativeList<int> indexes = new NativeList<int>(2, Allocator.TempJob);
    25.  
    26.     FilterJob filterJob = new FilterJob();
    27.     filterJob.nData = nData;
    28.  
    29.     JobHandle handle = filterJob.ScheduleFilter(indexes, 32);
    30.    
    31.     // Even forcing a complete here doesn't seem to matter. As best as I can tell the filter doesn't run
    32.     // handle.Complete();
    33.    
    34.     IncrementIndices incJob = new IncrementIndices();
    35.     incJob.indexes = indexes;
    36.     incJob.nData = nData;
    37.     handle = incJob.Schedule(handle);
    38.    
    39.     handle.Complete();
    40.    
    41.     Debug.Log(nData[0].num + " " + nData[1].num);
    42.    
    43.     nData.Dispose();
    44.     indexes.Dispose();
    45. }
    46.  
    47. private struct FilterJob : IJobParallelForFilter
    48. {
    49.     [ReadOnly]
    50.     public NativeArray<TestData> nData;
    51.  
    52.     public bool Execute(int index)
    53.     {
    54.         return nData[index].filter > 0;
    55.     }
    56. }
    57.  
    58. private struct IncrementIndices : IJob
    59. {
    60.     public NativeArray<TestData> nData;
    61.  
    62.     [ReadOnly]
    63.     public NativeList<int> indexes;
    64.  
    65.     public void Execute()
    66.     {
    67.         for (int i = 0; i < indexes.Length; i++)
    68.         {
    69.             TestData value = nData[indexes[i]];
    70.             value.num++;
    71.             nData[indexes[i]] = value;
    72.         }
    73.     }
    74. }
    75.  
     
  2. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    You need to do ScheduleAppend in your code.

    Code (CSharp):
    1. JobHandle handle = filterJob.ScheduleAppend(indexes, 2, 1);
     
    cjddmut likes this.
  3. cjddmut

    cjddmut

    Joined:
    Nov 19, 2012
    Posts:
    178
    Thanks that worked! I'm trying to work out what this means. Was it that ScheduleFilter assumed my list was the size of the array it was filtering against and ScheduleAppend got the list to that size?

    *EDIT* Actually looks like I didn't need ScheduleFilter and just needed ScheduleAppend. It looks like ScheduleAppend does what I thought ScheduleFilter would do so now I'm not sure what ScheduleFilter does.
     
  4. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    Yup, np! For ScheduleFilter, you can use it for subsequent schedulings (if there are any).
     
    cjddmut likes this.