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

Job system radix sort, struct not "blitable" error

Discussion in 'C# Job System' started by firestar9114_unity, Mar 2, 2018.

  1. firestar9114_unity

    firestar9114_unity

    Joined:
    Mar 2, 2018
    Posts:
    11
    So, I am attempting to sort an integer, and a pointer inside of a structure using a parallelized radix sort. The compiler tells me that the struct isn't blitible:

    public struct EncodedData
    {
    public int key;
    public GameObject reference;
    }

    Unfortunately when I tried to treat the data as parrallel NativeArrays I got an error saying it couldn't contain a Nullable type or something like that(I assume this was an issue with the game object). It should never be Null after it's integrated anyway, but when I bundle it in a struct to get around this, I get this blitable error. I'll attach the script if anyone wants it:
     

    Attached Files:

  2. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,649
    You can't have references types in job data or native containers - including GameObject/Component references.
     
  3. firestar9114_unity

    firestar9114_unity

    Joined:
    Mar 2, 2018
    Posts:
    11
    Is there anyway I can convert it to a clasical pointer safely, sort it, then convert back with casting?
     
  4. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,649
    Even if you could, GameObjects and Components cannot be used from off the main thread - all their APIs will throw exceptions when you try to use them.
     
  5. firestar9114_unity

    firestar9114_unity

    Joined:
    Mar 2, 2018
    Posts:
    11
    All I need to do is sort them, they never get used off the main thread
     
  6. amarcolina

    amarcolina

    Joined:
    Jun 19, 2014
    Posts:
    65
    You could put all of your GameObjects into an array, and use their index in that array to identify them instead of the direct GameObject reference itself.
     
    richardkettlewell likes this.
  7. firestar9114_unity

    firestar9114_unity

    Joined:
    Mar 2, 2018
    Posts:
    11
    I've managed to rewrite the code to do the casting, implemented a custom linked list to work in an unsafe context, and made some minor tweaks. This fixed the blitable problem, but now I am getting an error saying I haven't disposed of a NativeArray, despite calling
    Dispose()
    on both native arrays I created. Here's the code:

    EDIT: The script has gotten longer than I thought so I moved it into the attached file
     

    Attached Files:

  8. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,649
    Oh, I see... you're not dereferencing the GameObject at all in the job, you just want to swap around GameObject references in your array?

    In principle, storing the InstanceID of the GameObjects in a NativeArray, sorting them by their keys, and then converting them back to GameObjects afterwards, should be safe. But I don't think we have any API that lets you turn an InstanceID back into an object...
     
  9. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    Could we pretty please with sugar on top get that? We've run into a lot of situations where that would've been very, very useful.
     
    S_Darkwell and laurentlavigne like this.
  10. firestar9114_unity

    firestar9114_unity

    Joined:
    Mar 2, 2018
    Posts:
    11
    This might be how I work around the problem. I will attempt to write the implementation for this tonight.
     
  11. firestar9114_unity

    firestar9114_unity

    Joined:
    Mar 2, 2018
    Posts:
    11
    yeah, that's what I want to attempt. It would be great if that gets supported at some point. In the meantime I will attempt to use Amarcolina's idea to use an extra layer of indexing, then I can just try to retarget the array from the indexes.
     
  12. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    That is the right solution to the problem. Even in general thats the way to do it. Sorting just the key but not the value is a good optimization in any case. It reduces internal copying during sort.
     
    angusmf likes this.
  13. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    So you guys released your C# source, which means that we can check those kinds of things! Thank you. At the bottom of this page:
    Code (csharp):
    1. [VisibleToOtherModules]
    2. [FreeFunction("UnityEngineObjectBindings::FindObjectFromInstanceID")]
    3. internal extern static Object FindObjectFromInstanceID(int instanceID);
    Used in eg. RaycastHit.collider.get

    So... what's the chance of that method getting a public modifier? That would help a lot with a lot of things.
     
    Guerro323 and Peter77 like this.