Search Unity

Bug AsyncGPUReadback in Chrome

Discussion in 'General Graphics' started by Elinsa, Apr 17, 2023.

  1. Elinsa

    Elinsa

    Joined:
    Aug 13, 2021
    Posts:
    3
    Hi, I'm trying to use AsyncGPUReadback to get data from the GPU asynchronously. I need to render several times to the same texture and then get data on CPU. The following code works as expected in Editor, but when I run it in Chrome, I found that it takes much more frames than I expected.
    Code (CSharp):
    1. public class AsyncGPUReadbackTest : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     private Camera orthographicCamera;
    5.  
    6.     [SerializeField]
    7.     private Button button;
    8.  
    9.     [SerializeField]
    10.     private TMP_Text txt_Time;
    11.  
    12.     private NativeArray<Vector4> nativeArray;
    13.  
    14.     private int framesCount = 0;
    15.  
    16.     private void Awake()
    17.     {
    18.         button.onClick.AddListener(OnButtonClick);
    19.         orthographicCamera.targetTexture = new RenderTexture(256, 256, 24, RenderTextureFormat.ARGBFloat);
    20.     }
    21.  
    22.     private void Start()
    23.     {
    24.         nativeArray = new NativeArray<Vector4>(256 * 256, Allocator.Persistent);
    25.     }
    26.  
    27.     private void Update()
    28.     {
    29.         framesCount++;
    30.     }
    31.  
    32.     private void OnButtonClick()
    33.     {
    34.         button.enabled = false;
    35.         for (int i = 0; i < 200; i++)
    36.         {
    37.             orthographicCamera.Render();
    38.         }
    39.  
    40.         framesCount = 0;
    41.  
    42.         AsyncGPUReadback.RequestIntoNativeArray(ref nativeArray,
    43.             orthographicCamera.targetTexture, 0, TextureFormat.RGBAFloat, OnCompleteCallback);
    44.     }
    45.  
    46.     private void OnCompleteCallback(AsyncGPUReadbackRequest obj)
    47.     {
    48.         txt_Time.text = $"Frames Count {framesCount}";
    49.         button.enabled = true;
    50.     }
    51.  
    52.     private void OnDestroy()
    53.     {
    54.         button.onClick.RemoveListener(OnButtonClick);
    55.         nativeArray.Dispose();
    56.     }
    57. }
    I found out that the duration of AsyncGPUReadback depends on amount of calls camera.render. But I don't understand, why?
    upload_2023-4-16_21-16-53.png


    It's also interesting that this code works fine in Firefox.

    upload_2023-4-16_21-30-9.png
     

    Attached Files: