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

AsyncReadback in Command Buffer

Discussion in 'General Graphics' started by Marco-Playable, Jun 29, 2021.

  1. Marco-Playable

    Marco-Playable

    Joined:
    Nov 12, 2020
    Posts:
    53
    How does Unity handle async readback requests and how does scheduling work out with multiple requests?
    Let's say I have this toy example where a render target gets turned red, then copied into an array, then turned green and copied into another array. The Clear calls are synchronous, the readbacks are asynchronous and happen over multiple frames according to the documentation. What does this look like on the GPU side?
    Does the first Clear & Readback get invoked immediately, then the rest of the Command Buffer is treated like a Coroutine where it waits for a later frame to only execute the rest of the buffer?

            public void OnEnable()
    {
    RenderPipelineManager.beginFrameRendering += OnFrameRendering;
    }
    private void OnFrameRendering(ScriptableRenderContext rc, Camera[] cameras)
    {
    var cmd = new CommandBuffer();
    cmd.SetRenderTarget(m_rt);

    cmd.ClearRenderTarget(false, true, Color.red);
    cmd.RequestAsyncReadbackIntoNativeArray(ref outputTexPixelsRed, m_rt, OnAsyncReadbackRed);

    cmd.ClearRenderTarget(false, true, Color.green);
    cmd.RequestAsyncReadbackIntoNativeArray(ref outputTexPixelsGreen, m_rt, OnAsyncReadbackGreen);

    rc.ExecuteCommandBuffer(cmd);
    cmd.Clear();
    RenderPipelineManager.beginFrameRendering -= OnFrameRendering;
    }