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

Dispose Exception for Native Array of Texture2D

Discussion in 'General Graphics' started by TheCelt, Feb 21, 2021.

  1. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    741
    Hello


    I have a simple function where i get the native array of my Texture2D, edit the data then apply it.

    I am then trying to Get the native array from the image again to read the data to confirm its the same what i inserted previously, but i get an exception:

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         _data = _result.GetPixelData<Vector4>(0);
    4.         for (int i = 0; i < _size * _size; i++)
    5.         {
    6.  
    7.             Vector4 a = _data1[i];
    8.             Vector4 b = _data2[i];
    9.  
    10.             Vector4 c = Vector4.Lerp(a, b, _t);
    11.             _data[i] = c;
    12.         }
    13.  
    14.         _result.Apply();
    15.         _data = _result.GetPixelData<Vector4>(0); // error here
    16.     }
    Why can't i get the pixel data after applying in the same frame, the next frame i can do it but not the same frame?
     
    DragonCoder likes this.
  2. yzRambler

    yzRambler

    Joined:
    Jan 24, 2019
    Posts:
    37
    _result.Apply();
    This function is not processed immediately.
     
  3. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    741
    So theres no way to read the data after applying to confirm it worked in the same frame?
     
  4. AljoshaD

    AljoshaD

    Unity Technologies

    Joined:
    May 27, 2019
    Posts:
    220
    The Apply copies/uploads the data to video memory. It should not impact the use of GetPixelData unless you call Apply(makeNoLongerReadable = true). This would remove the CPU copy that is accessed by GetPixelData.
     
  5. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    741
    Ah so it's default true - sneaky. I didn't notice that. That's one of the reasons I don't like default method parameters, easy to not notice. :(
     
  6. AljoshaD

    AljoshaD

    Unity Technologies

    Joined:
    May 27, 2019
    Posts:
    220
    the default should actually be false. Did changing this value fix it for you?
     
  7. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    741
    I tried both true and false and got the same error, did you by chance see if it reproduced the same error for you? Should be able to copy paste my code with the necessary setup variables to see if it does for you.
     
  8. DragonCoder

    DragonCoder

    Joined:
    Jul 3, 2015
    Posts:
    1,679
    Ran into this same issue. In my case it only happened once I started messing with processing the texture data in burst jobs, but all jobs were Completed() when calling GetPixelData on the main thread.

    However apparently it was a bug that has been fixed in the meantime.
    It occurs in 2021.1.14f1 but not anymore in 2021.3.2f1 (the currently actual one).
    Don't know the exact version it got fixed unfortunately.
     
    Last edited: May 8, 2022