Search Unity

Question Job with interface constrained with generic to struct.

Discussion in 'Entity Component System' started by Micz84, Apr 15, 2021.

  1. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    I wanted to use interface in my job, I know that only unmanaged types are allowed in jobs so I have tried something like this:

    Code (CSharp):
    1. [BurstCompile]
    2. public struct JobWithInterfaces<TGenericA,TGenericB>:IJobParallelFor
    3.     where TGenericA:struct,IMyInterface
    4.     where TGenericB:struct,IMyInterface
    5. {
    6.     [ReadOnly] public TGenericA A;
    7.     public TGenericA A;
    8.     public void Execute(int index)
    9.     {
    10.         //...
    11.     }
    12. }
    But burst is complaining that A is a managed type of IMyInterface. Constraining it to struct is not taken into account. Is it possible to do something like that or I just have to get unmanaged elements from the interface manually and assigned them to fields of the job?
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
  3. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    Oddly I have done it again because I have changed my approach thinking that it is not possible and now I do not get this error. But I do get an error that I am writing to [ReadOnly] native array

    InvalidOperationException: The Unity.Collections.NativeArray`1[System.Single] has been declared as [ReadOnly] in the job, but you are writing to it.

    I get this native array using Property of en interface:
    Code (CSharp):
    1. public struct AddMapAToMapBJobGeneric<T> : IJobParallelForBatch where T:struct, IMapIterface
    2.     {
    3.         public T MapA;
    4.  
    5.         public T MapB;
    6.  
    7.         public void Execute(int startIndex, int count)
    8.         {
    9.             var mapB = MapB.Data;
    10.             for (int j = startIndex, end = startIndex + count; j < end; j++)
    11.             {
    12.                 var d = MapA.Data[j];
    13.                 mapB[j] += d; // Exception is thrown here
    14.             }
    15.         }
    16.     }
    I have a new approach to my code so I do not need a Generic job, but it would be nice to know if what I am doing is OK for the future :)
     
  4. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    Without seeing the implementation for the concrete IMapInterface, I cannot help you.
     
  5. Micz84

    Micz84

    Joined:
    Jul 21, 2012
    Posts:
    451
    Code (CSharp):
    1.  public struct TestMap : IMapInterface
    2.     {
    3.         public int Width { get; }
    4.         public NativeArray<float> Data { get; }
    5.  
    6.         public TestMap(int width, Allocator allocator = Allocator.TempJob)
    7.         {
    8.             Width = width;
    9.             Data = new NativeArray<float>(width*width, allocator);
    10.         }
    11.  
    12.         public void Dispose()
    13.         {
    14.             Data.Dispose();
    15.         }
    16.     }
    17.  
    Here is my test implemetation
     
  6. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    I don't see any [ReadOnly] attribute anywhere in your code, so I am not sure what the error is referring to.