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

AsyncGPUReadbackRequest.getData returns array that is too big

Discussion in 'General Graphics' started by pyjamaslug, Jun 28, 2019.

  1. pyjamaslug

    pyjamaslug

    Joined:
    Jul 5, 2017
    Posts:
    51
    I'm experimenting with AsyncGPUReadbackRequest and have pulled the code from https://github.com/keijiro/AsyncCaptureTest/blob/master/Assets/AsyncCapture.cs
    The request is pulled out of a queue like this:

    var req = _requests.Peek();
    var buffer = req.GetData<Color32>();


    When I try to use the buffer to populate a texture, I get this:
    SetPixels32 called with invalid number of pixels in the array
    UnityEngine.Texture2D:SetPixels32(Color32[])
    Capture:SaveBitmap(NativeArray`1, Int32, Int32, Int32, String, Int32) (at Assets/Capture.cs:113)
    Capture:Update() (at Assets/Capture.cs:64)

    and when I check the sizes:
    buffer size is 1425424 texture size is 712712

    In other words, the NativeArray buffer returned by the request is twice as big as the texture I created to hold it (the texture is the expected size)

    If I trim the array to the right size then go ahead and encode it to a png, when I look at it the image it looks vaguely correct but as though every pixel has been interleaved with some other data.
    I'm on 2018.2 - any ideas?
     
  2. pyjamaslug

    pyjamaslug

    Joined:
    Jul 5, 2017
    Posts:
    51
    OK, Solved it using the GIGO principle.
    The request was returning some unknown texture format because I wasn't setting the target format at the point where the request was made.
    setting it to AsyncGPUReadback.Request(source,0,TextureFormat.RGBA32) solved the problem.
     
    richardkettlewell likes this.
  3. GamesEngineer

    GamesEngineer

    Joined:
    Aug 22, 2014
    Posts:
    9
    I'm noticing the same issue where the size of the GetData result is twice the size of the buffer given to AsyncGPUReadback.Request. That's worrisome from a performance perspective.