Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

AsyncGPUReadback.RequestIntoNativeArray causes InvalidOperationException on NativeArray

Discussion in 'Entity Component System' started by Dimetrock, Nov 25, 2020.

  1. gregoire_unity128

    gregoire_unity128

    Joined:
    Jan 18, 2021
    Posts:
    11
    I tried another approach: create a nativearray each AsyncGPUReadback.RequestIntoNativeArray call but when i can't deallocate this data, i got this error:


    InvalidOperationException: The Unity.Collections.NativeArray`1[System.Byte] has been set to undisposable and cannot be deallocated. 



    Code (CSharp):
    1.  
    2.             Debug.Log("create request");
    3.             NativeArray<byte> buffer = new NativeArray<byte>(w * h * 16, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
    4.  
    5.             AsyncGPUReadbackRequest request = AsyncGPUReadback.RequestIntoNativeArray(ref buffer, rt);
    6.             float acc = 0;
    7.             while (!request.done && acc < 1f)
    8.             {
    9.                 acc += Time.deltaTime;
    10.                 yield return null;
    11.             }
    12.  
    13.             OnReadbackComplete(request);
    14.             buffer.Dispose();
    15.  
    16.             Debug.Log("release request");

    Any help is very welcome.
     
    LooperVFX likes this.
  2. gregoire_unity128

    gregoire_unity128

    Joined:
    Jan 18, 2021
    Posts:
    11
    Thank you so much, I made it work with something like this

    Code (CSharp):
    1.         private Queue<NativeArray<byte>> buffers;
    2.  
     
    LooperVFX and bb8_1 like this.
  3. QuariYune

    QuariYune

    Joined:
    Jul 1, 2021
    Posts:
    20
    This issue is still happening on 2022.2.1f1.

    Code (CSharp):
    1. NativeArray<float> floatArray = new NativeArray<float>(floatBuffer.count, Allocator.Persistent);
    2. NativeArray<States> statesArray = new NativeArray<States>(statesBuffer.count, Allocator.Persistent);
    3.  
    4. // Requests from GPU to CPU
    5. AsyncGPUReadbackRequest[] requests = new AsyncGPUReadbackRequest[]
    6. {
    7. AsyncGPUReadback.RequestIntoNativeArray(ref floatArray, floatBuffer),
    8. AsyncGPUReadback.RequestIntoNativeArray(ref statesArray, statesBuffer),
    9. };
    10.  
    11. while (!requests.All(x => x.done))
    12. {
    13.     yield return null;
    14. }
    15.  
    16. // Processes GPU data
    17. if (!requests.Any(x => x.hasError))
    18. {
    19.     // Try and access any of the created native arrays. Errors out
    20. }
    This code also leads to "InvalidOperationException: NativeArray can no longer be accessed, since its owner has been invalidated."

    Definitely doesn't seem to be the case from the example above, so https://issuetracker.unity3d.com/is...uses-invalidoperationexception-on-nativearray isn't properly fixed unless I'm misunderstanding something.
     
    Last edited: Jan 15, 2023
    FizzFab and LooperVFX like this.
  4. Sponge2k

    Sponge2k

    Joined:
    Sep 22, 2021
    Posts:
    12
    Suffering from it as well, in 2022.2.10.

    OK, I solved it for now, but not sure if this will suit everybody.

    I am using the readback for objectpicking (using colors as ID). It seems you cannot execute the readbacks in an (Late)Update and wait for the request to finish. The request might happen before the other is finished. Indeed, similar someone else mentioned a bit earlier.

    I just solved it using a boolean check, setting it to false if the event is triggered - and then a new request is allowed to be made (setting the bool to true). In my case I do not want to deal with queues, it is not needed anyway.
     
    Last edited: Mar 23, 2023
  5. Fobri

    Fobri

    Joined:
    Jan 28, 2014
    Posts:
    22
    Bump.
    Still a problem in 2022.2.8f1. It only happens when a request into a native array is made before a previous request has been resolved, as mentioned above. Is this a bug or is it not supported to queue multiple requests like this?
     
  6. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,239
    I recommend submitting a bug report for this. We should either support it, or provide good error feedback if we can't. Anything else is a bug imo.
     
    Walter_Hulsebos and Gooren like this.
  7. Fobri

    Fobri

    Joined:
    Jan 28, 2014
    Posts:
    22
    Hi, thank you for replying.
    I have submitted a bug report 36698, though my case seems to be a bit more specific than I originally thought.

    Basically AsyncGPUReadback.RequestIntoNativeArray causes an InvalidOperationException when requesting data into a NativeArray created with NativeArray.GetSubArray.

    AsyncGPUReadback.Request works normally, but then you have to do an extra array copy, so while it works it would be nice to get this fixed.
    Thanks
     
    richardkettlewell likes this.
  8. Fobri

    Fobri

    Joined:
    Jan 28, 2014
    Posts:
    22
  9. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    863
    And now we need to wait around 6 months to get a fix into latest stable version of Unity. Which is better than nothing, thanks for reporting!
     
    Menion-Leah likes this.
  10. nicb_cerebralfix

    nicb_cerebralfix

    Joined:
    Aug 9, 2022
    Posts:
    1
    I'm still getting this error in 2021.3.15f1. Is it possible that this failed to be ported from 2021.2 to 2021.3?
     
  11. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,239
    2021.3 contains everything in 2021.2.

    you may need to submit a new bug report to get this looked at, or wait for the bug posted above your post to get fixed, if it’s the same thing.
     
  12. jribijed6

    jribijed6

    Joined:
    Mar 7, 2019
    Posts:
    4
    Still happening in 2022.3.5f1

    Odd thing is that it is not easily reproducible. I tried opening a project (exact same unity version + exact same code), that does some compute shader stuff and reads it back, no errors, everything works fine. Now if I open a second project with the exact same code, version, etc, and execute it, bunch of those errors pop up (yet it still functions normally, the data is read back fine)
     
    Last edited: Aug 10, 2023
    LooperVFX likes this.
  13. Hypergeanu

    Hypergeanu

    Joined:
    Feb 20, 2023
    Posts:
    1
    We see this happening in 2022.3.3f1 (to be honest the error has been following us through all version we've updated, since back 2021).

    Our flow is pretty simple and we have two code paths that throw the same error (NativeArray does not have read/write access and NativeArray should not be undisposable) which are very much the same:

    - Create persistent NativeArray (only once);
    - Call AsyncGPUReadback.RequestIntoNativeArray;
    - Wait for completion;
    - Create and schedule a job that reads from the array (after that gpu request is done);
    - Run the job, call complete on the job handle (the job transfers the data from the nativearray into a different one, so the usage cycle of the nativearray used for gpu request ends here).

    The sequence above executes with no errors for the first pass, but when it is called again (the buffer stays the same, its purpose is to perform these requests and job processing repeatedly using the pre-allocated memory), the specified errors are thrown when the array is touched by the gpu async request.
     
  14. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,239
    Vote on https://issuetracker.unity3d.com/is...g-asyncgpureadback-dot-requestintonativearray if you haven’t already
     
  15. Hyperg

    Hyperg

    Joined:
    Jul 6, 2015
    Posts:
    19
    richardkettlewell likes this.
  16. thebarryman

    thebarryman

    Joined:
    Nov 22, 2012
    Posts:
    122
    I just ran into this bug. So, until this is fixed, is there any difference between pre-allocating a NativeArray and calling AsyncGPUReadback.RequestIntoNativeArray with a reference to it, vs. just calling AsyncGPUReadback?

    It seems it is inevitable to allocate new memory every time you want to do a readback?
     
    Last edited: Sep 12, 2023
  17. JollyTheory

    JollyTheory

    Joined:
    Dec 30, 2018
    Posts:
    134
    In docs it says
    output - Reference to the NativeArray to write the data into. The NativeArray or underlying memory cannot be Disposed until the request is complete.

    So if RequestIntoNativeArray "locks" the NativeArray passed to it, how does one Dispose on editor play stop? Is the intended solution just to ignore the editor leaks? Is there a way to stop RequestIntoNativeArray request while it's happening, unlocking the array?
     
    LooperVFX likes this.
  18. brofessordoucette

    brofessordoucette

    Joined:
    Sep 3, 2023
    Posts:
    1
  19. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,239
    Can you use https://docs.unity3d.com/ScriptReference/EditorApplication-playModeStateChanged.html to complete any pending async readbacks prior to exiting playmode?

    It does however feel like something we should do inside the engine, if it's not safe to allow these readbacks to persist across a playmode state change. You would be welcome to submit a bug report about it.
     
    LooperVFX likes this.
  20. richardkettlewell

    richardkettlewell

    Unity Technologies

    Joined:
    Sep 9, 2015
    Posts:
    2,239
    If you have a different repro, can you submit a bug report with it?
     
    LooperVFX likes this.