Search Unity

Issues with IParallelFilter in a JobComponentSystem

Discussion in 'Entity Component System' started by Matt_De_Boss_Developer, May 24, 2019.

  1. Matt_De_Boss_Developer

    Matt_De_Boss_Developer

    Joined:
    Oct 17, 2014
    Posts:
    46
    So I am honestly super lost on this one since the error message that I get completely denies me the fact that my NativeArray is indeed allocated as a TempJob. Also, if anyone knows a better way of structuring my system to be more performance oriented, then please speak up (because at the current moment, the code is super freaking slow). Code Below:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Unity.Entities;
    5. using Unity.Jobs;
    6. using Unity.Collections;
    7.  
    8. [UpdateAfter(typeof(MarchingCubesSystem))]
    9. public class FilterMeshSystem : JobComponentSystem
    10. {
    11.  
    12.     EntityQuery eq;
    13.  
    14.     protected override void OnCreate()
    15.     {
    16.  
    17.         eq = World.Active.EntityManager.CreateEntityQuery(typeof(shouldFilterMesh));
    18.  
    19.  
    20.     }
    21.  
    22.     protected override JobHandle OnUpdate(JobHandle inputDeps)
    23.     {
    24.         NativeArray<Entity> entities = eq.ToEntityArray(Allocator.TempJob);
    25.  
    26.  
    27.         if (entities.Length == 0)
    28.         {
    29.             return inputDeps;
    30.         }
    31.  
    32.         World.Active.EntityManager.RemoveComponent(entities[0], typeof(shouldFilterMesh));
    33.  
    34.         NativeList<int> filteredIndices = new NativeList<int>(Allocator.Persistent);
    35.  
    36.  
    37.         int SizeX = World.Active.EntityManager.GetComponentData<ChunkStatus>(entities[0]).SizeX;
    38.         int SizeY = World.Active.EntityManager.GetComponentData<ChunkStatus>(entities[0]).SizeY;
    39.         int SizeZ = World.Active.EntityManager.GetComponentData<ChunkStatus>(entities[0]).SizeY;
    40.  
    41.  
    42.  
    43.         MeshDataCompactionJob filterHandle = new MeshDataCompactionJob
    44.         {
    45.             unFilteredVerticesData = GetBufferFromEntity<UnfilteredVerticesArray>(false),
    46.             entities = entities
    47.         };
    48.  
    49.         JobHandle MCJ = filterHandle.ScheduleAppend(filteredIndices, (SizeX - 1) * (SizeY - 1) * (SizeZ - 1) * 15, 1, inputDeps);
    50.  
    51.         MCJ.Complete();
    52.  
    53.         //BuildComapctionArrayJob filteredVertices = new NativeArray<float4>(filteredIndices.Length, Allocator.TempJob);
    54.         BuildComapctionArrayJob buildHandle = new BuildComapctionArrayJob
    55.         {
    56.             unFilteredVerticesData = GetBufferFromEntity<UnfilteredVerticesArray>(false),
    57.             FilteredIndices = filteredIndices,
    58.             filteredVerticesData = GetBufferFromEntity<filteredVerticesArray>(false),
    59.             entities = entities
    60.         };
    61.  
    62.         JobHandle BCA = buildHandle.Schedule(filteredIndices.Length, 1);
    63.  
    64.  
    65.         return BCA;
    66.     }
    67.  
    68.  
    69.  
    70. }
    71.  
    And the error message that I get is:

    InvalidOperationException: The NativeArray BuildComapctionArrayJob.FilteredIndices may not be deallocated from the job BuildComapctionArrayJob because the Allocation type can not be deallocated from a job. (Only Allocation.Persistent & Allocation.TempJob can be deallocated from a job)

     
    Last edited: May 24, 2019
  2. Sarkahn

    Sarkahn

    Joined:
    Jan 9, 2013
    Posts:
    440
    You didnt include your job in the code you posted. I'm guessing you're trying to use [DeallocateOnJobCompletion] on filteredindices. You cant deallocate NativeList from inside a job. Not sure if it's a bug or intended. Just keep your list as a member of your system and Clear it before you schedule your job
     
  3. Matt_De_Boss_Developer

    Matt_De_Boss_Developer

    Joined:
    Oct 17, 2014
    Posts:
    46
    Totally didn't think of that. However, I think I can save on performance by moving the second job of my system into another system so that I am not forced to sync. This would take more memory than the solution you proposed (in fact, memory savings would be a huge amount since I am creating 100s of voxel chunks at a time), but I think moving each job to another system and saving the FilteredIndices in a DynamicBuffer somehow to be used into another system would be faster. Thanks for the help!